Changeset 87336 in webkit


Ignore:
Timestamp:
May 25, 2011 4:54:02 PM (13 years ago)
Author:
Martin Robinson
Message:

2011-05-25 Martin Robinson <mrobinson@igalia.com>

Reviewed by Dirk Schulze.

[Cairo] Move the global alpha property from GraphicsContext to PlatformContextCairo
https://bugs.webkit.org/show_bug.cgi?id=60185

Remove Cairo #ifdefs from GraphicsContext.h by pushing the global alpha
state into PlatformContextCairo. Since Cairo is the only platform that needs
to store this, this is the proper place for it. Change the image mask stack into
a more generic state stack that can keep track of the multiple bits of platform
specific state and properly handle save/restore pairs.

No new tests. This is just a code refactor.

  • platform/graphics/GraphicsContext.h: Remove Cairo #ifdefs. (WebCore::GraphicsContextState::GraphicsContextState): Ditto.
  • platform/graphics/cairo/ContextShadowCairo.cpp: Access global alpha from PlatformContextCairo now. (WebCore::ContextShadow::drawRectShadow):
  • platform/graphics/cairo/FontCairo.cpp: Ditto. (WebCore::Font::drawGlyphs):
  • platform/graphics/cairo/GraphicsContextCairo.cpp: (WebCore::setPlatformFill): Ditto. (WebCore::setPlatformStroke): Ditto. (WebCore::GraphicsContext::setAlpha): Ditto.
  • platform/graphics/cairo/PlatformContextCairo.cpp: Moved ImageMaskInformation class from the header file, since it can be private now. Abstract the image mask state and the global alpha in a generic State class. (WebCore::ImageMaskInformation::update): (WebCore::ImageMaskInformation::isValid): (WebCore::ImageMaskInformation::maskSurface): (WebCore::ImageMaskInformation::maskRect): (WebCore::PlatformContextCairo::State::State): Added. (WebCore::PlatformContextCairo::PlatformContextCairo): Intialize the state class when this class is constructed. (WebCore::PlatformContextCairo::restore): Now pop the last state off the state stack. (WebCore::PlatformContextCairo::~PlatformContextCairo): Added this so that we can forward declare the State class in the header. (WebCore::PlatformContextCairo::save): Push a new state onto the stack. (WebCore::PlatformContextCairo::pushImageMask): Operate on the state stack now. (WebCore::PlatformContextCairo::globalAlpha): Added. (WebCore::PlatformContextCairo::setGlobalAlpha): Added. (WebCore::PlatformContextCairo::drawSurfaceToContext: Call globalAlpha().
  • platform/graphics/cairo/PlatformContextCairo.h: Changed the image mask stack into a more generic state stack, much like PlatformContextChromium.
Location:
trunk/Source/WebCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r87335 r87336  
     12011-05-25  Martin Robinson  <mrobinson@igalia.com>
     2
     3        Reviewed by Dirk Schulze.
     4
     5        [Cairo] Move the global alpha property from GraphicsContext to PlatformContextCairo
     6        https://bugs.webkit.org/show_bug.cgi?id=60185
     7
     8        Remove Cairo #ifdefs from GraphicsContext.h by pushing the global alpha
     9        state into PlatformContextCairo. Since Cairo is the only platform that needs
     10        to store this, this is the proper place for it. Change the image mask stack into
     11        a more generic state stack that can keep track of the multiple bits of platform
     12        specific state and properly handle save/restore pairs.
     13
     14        No new tests. This is just a code refactor.
     15
     16        * platform/graphics/GraphicsContext.h: Remove Cairo #ifdefs.
     17        (WebCore::GraphicsContextState::GraphicsContextState): Ditto.
     18        * platform/graphics/cairo/ContextShadowCairo.cpp: Access global alpha from PlatformContextCairo now.
     19        (WebCore::ContextShadow::drawRectShadow):
     20        * platform/graphics/cairo/FontCairo.cpp: Ditto.
     21        (WebCore::Font::drawGlyphs):
     22        * platform/graphics/cairo/GraphicsContextCairo.cpp:
     23        (WebCore::setPlatformFill): Ditto.
     24        (WebCore::setPlatformStroke): Ditto.
     25        (WebCore::GraphicsContext::setAlpha): Ditto.
     26        * platform/graphics/cairo/PlatformContextCairo.cpp: Moved ImageMaskInformation class from the
     27        header file, since it can be private now. Abstract the image mask state and the global alpha
     28        in a generic State class.
     29        (WebCore::ImageMaskInformation::update):
     30        (WebCore::ImageMaskInformation::isValid):
     31        (WebCore::ImageMaskInformation::maskSurface):
     32        (WebCore::ImageMaskInformation::maskRect):
     33        (WebCore::PlatformContextCairo::State::State): Added.
     34        (WebCore::PlatformContextCairo::PlatformContextCairo): Intialize the state
     35        class when this class is constructed.
     36        (WebCore::PlatformContextCairo::restore): Now pop the last state off the state stack.
     37        (WebCore::PlatformContextCairo::~PlatformContextCairo): Added this so that we can forward
     38        declare the State class in the header.
     39        (WebCore::PlatformContextCairo::save): Push a new state onto the stack.
     40        (WebCore::PlatformContextCairo::pushImageMask): Operate on the state stack now.
     41        (WebCore::PlatformContextCairo::globalAlpha): Added.
     42        (WebCore::PlatformContextCairo::setGlobalAlpha): Added.
     43        (WebCore::PlatformContextCairo::drawSurfaceToContext: Call globalAlpha().
     44        * platform/graphics/cairo/PlatformContextCairo.h: Changed the image mask stack
     45        into a more generic state stack, much like PlatformContextChromium.
     46
    1472011-05-25  Mark Rowe  <mrowe@apple.com>
    248
  • trunk/Source/WebCore/platform/graphics/GraphicsContext.h

    r85419 r87336  
    155155            : strokeThickness(0)
    156156            , shadowBlur(0)
    157 #if USE(CAIRO)
    158             , globalAlpha(1)
    159 #endif
    160157            , textDrawingMode(TextModeFill)
    161158            , strokeColor(Color::black)
     
    190187        float shadowBlur;
    191188
    192 #if USE(CAIRO)
    193         float globalAlpha;
    194 #endif
    195189        TextDrawingModeFlags textDrawingMode;
    196190
     
    391385
    392386        void setAlpha(float);
    393 #if USE(CAIRO)
    394         float getAlpha();
    395 #endif
    396387
    397388        void setCompositeOperation(CompositeOperator);
  • trunk/Source/WebCore/platform/graphics/cairo/ContextShadowCairo.cpp

    r83932 r87336  
    200200
    201201    cairo_t* cr = context->platformContext()->cr();
     202    float globalAlpha = context->platformContext()->globalAlpha();
    202203
    203204    // drawShadowedRect still does not work with rotations.
     
    205206    if ((!context->getCTM().isIdentityOrTranslationOrFlipped()) || (internalShadowWidth > rect.width())
    206207        || (internalShadowHeight > rect.height()) || (m_type != BlurShadow)) {
    207         drawRectShadowWithoutTiling(context, rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, context->getAlpha());
     208        drawRectShadowWithoutTiling(context, rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, globalAlpha);
    208209        return;
    209210    }
     
    230231
    231232    if ((shadowTemplateSize.width() * shadowTemplateSize.height() > m_sourceRect.width() * m_sourceRect.height())) {
    232         drawRectShadowWithoutTiling(context, rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, context->getAlpha());
     233        drawRectShadowWithoutTiling(context, rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, globalAlpha);
    233234        return;
    234235    }
     
    252253    appendWebCorePathToCairoContext(m_layerContext, path);
    253254
    254     cairo_set_source_rgba(m_layerContext, 0, 0, 0, context->getAlpha());
     255    cairo_set_source_rgba(m_layerContext, 0, 0, 0, globalAlpha);
    255256    cairo_fill(m_layerContext);
    256257
  • trunk/Source/WebCore/platform/graphics/cairo/FontCairo.cpp

    r82962 r87336  
    114114    cairo_t* cr = platformContext->cr();
    115115    cairo_save(cr);
     116
     117    float globalAlpha = platformContext->globalAlpha();
     118
    116119    prepareContextForGlyphDrawing(cr, font, point);
    117120    if (context->textDrawingMode() & TextModeFill) {
    118121        if (context->fillGradient()) {
    119122            cairo_set_source(cr, context->fillGradient()->platformGradient());
    120             if (context->getAlpha() < 1.0f) {
     123            if (globalAlpha < 1) {
    121124                cairo_push_group(cr);
    122                 cairo_paint_with_alpha(cr, context->getAlpha());
     125                cairo_paint_with_alpha(cr, globalAlpha);
    123126                cairo_pop_group_to_source(cr);
    124127            }
     
    127130            cairo_pattern_t* pattern = context->fillPattern()->createPlatformPattern(affine);
    128131            cairo_set_source(cr, pattern);
    129             if (context->getAlpha() < 1.0f) {
     132            if (globalAlpha < 1) {
    130133                cairo_push_group(cr);
    131                 cairo_paint_with_alpha(cr, context->getAlpha());
     134                cairo_paint_with_alpha(cr, globalAlpha);
    132135                cairo_pop_group_to_source(cr);
    133136            }
     
    136139            float red, green, blue, alpha;
    137140            context->fillColor().getRGBA(red, green, blue, alpha);
    138             cairo_set_source_rgba(cr, red, green, blue, alpha * context->getAlpha());
     141            cairo_set_source_rgba(cr, red, green, blue, alpha * globalAlpha);
    139142        }
    140143        drawGlyphsToContext(cr, font, glyphs, numGlyphs);
     
    148151        if (context->strokeGradient()) {
    149152            cairo_set_source(cr, context->strokeGradient()->platformGradient());
    150             if (context->getAlpha() < 1.0f) {
     153            if (globalAlpha < 1) {
    151154                cairo_push_group(cr);
    152                 cairo_paint_with_alpha(cr, context->getAlpha());
     155                cairo_paint_with_alpha(cr, globalAlpha);
    153156                cairo_pop_group_to_source(cr);
    154157            }
     
    157160            cairo_pattern_t* pattern = context->strokePattern()->createPlatformPattern(affine);
    158161            cairo_set_source(cr, pattern);
    159             if (context->getAlpha() < 1.0f) {
     162            if (globalAlpha < 1) {
    160163                cairo_push_group(cr);
    161                 cairo_paint_with_alpha(cr, context->getAlpha());
     164                cairo_paint_with_alpha(cr, globalAlpha);
    162165                cairo_pop_group_to_source(cr);
    163166            }
     
    166169            float red, green, blue, alpha;
    167170            context->strokeColor().getRGBA(red, green, blue, alpha);
    168             cairo_set_source_rgba(cr, red, green, blue, alpha * context->getAlpha());
     171            cairo_set_source_rgba(cr, red, green, blue, alpha * globalAlpha);
    169172        }
    170173        cairo_glyph_path(cr, glyphs, numGlyphs);
  • trunk/Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp

    r87151 r87336  
    8686        setSourceRGBAFromColor(cr, context->fillColor());
    8787    cairo_clip_preserve(cr);
    88     cairo_paint_with_alpha(cr, state.globalAlpha);
     88    cairo_paint_with_alpha(cr, context->platformContext()->globalAlpha());
    8989    cairo_restore(cr);
    9090    if (pattern)
     
    9494static inline void setPlatformStroke(GraphicsContext* context, cairo_t* cr)
    9595{
     96    float globalAlpha = context->platformContext()->globalAlpha();
    9697    cairo_pattern_t* pattern = 0;
    9798    cairo_save(cr);
     
    105106        cairo_set_source(cr, state.strokeGradient->platformGradient());
    106107    else  {
    107         Color strokeColor = colorWithOverrideAlpha(context->strokeColor().rgb(), context->strokeColor().alpha() / 255.f * state.globalAlpha);
     108        Color strokeColor = colorWithOverrideAlpha(context->strokeColor().rgb(), context->strokeColor().alpha() / 255.f * globalAlpha);
    108109        setSourceRGBAFromColor(cr, strokeColor);
    109110    }
    110     if (state.globalAlpha < 1.0f && (state.strokePattern || state.strokeGradient)) {
     111    if (globalAlpha < 1.0f && (state.strokePattern || state.strokeGradient)) {
    111112        cairo_push_group(cr);
    112         cairo_paint_with_alpha(cr, state.globalAlpha);
     113        cairo_paint_with_alpha(cr, globalAlpha);
    113114        cairo_pop_group_to_source(cr);
    114115    }
     
    10111012void GraphicsContext::setAlpha(float alpha)
    10121013{
    1013     m_state.globalAlpha = alpha;
    1014 }
    1015 
    1016 float GraphicsContext::getAlpha()
    1017 {
    1018     return m_state.globalAlpha;
     1014    platformContext()->setGlobalAlpha(alpha);
    10191015}
    10201016
  • trunk/Source/WebCore/platform/graphics/cairo/PlatformContextCairo.cpp

    r87151 r87336  
    11/*
    22 * Copyright (C) 2011 Igalia S.L.
     3 * Copyright (c) 2008, Google Inc. All rights reserved.
    34 *
    45 * Redistribution and use in source and binary forms, with or without
     
    3233namespace WebCore {
    3334
     35// In Cairo image masking is immediate, so to emulate image clipping we must save masking
     36// details as part of the context state and apply them during platform restore.
     37class ImageMaskInformation {
     38public:
     39    void update(cairo_surface_t* maskSurface, const FloatRect& maskRect)
     40    {
     41        m_maskSurface = maskSurface;
     42        m_maskRect = maskRect;
     43    }
     44
     45    bool isValid() const { return m_maskSurface; }
     46    cairo_surface_t* maskSurface() const { return m_maskSurface.get(); }
     47    const FloatRect& maskRect() const { return m_maskRect; }
     48
     49private:
     50    RefPtr<cairo_surface_t> m_maskSurface;
     51    FloatRect m_maskRect;
     52};
     53
     54
     55// Encapsulates the additional painting state information we store for each
     56// pushed graphics state.
     57class PlatformContextCairo::State {
     58public:
     59    State()
     60        : m_globalAlpha(1)
     61    {
     62    }
     63
     64    State(const State& state)
     65        : m_globalAlpha(state.m_globalAlpha)
     66    {
     67        // We do not copy m_imageMaskInformation because otherwise it would be applied
     68        // more than once during subsequent calls to restore().
     69    }
     70
     71    ImageMaskInformation m_imageMaskInformation;
     72    float m_globalAlpha;
     73};
     74
    3475PlatformContextCairo::PlatformContextCairo(cairo_t* cr)
    3576    : m_cr(cr)
    3677{
     78    m_stateStack.append(State());
     79    m_state = &m_stateStack.last();
    3780}
    3881
    3982void PlatformContextCairo::restore()
    4083{
    41     const ImageMaskInformation& maskInformation = m_maskImageStack.last();
     84    const ImageMaskInformation& maskInformation = m_state->m_imageMaskInformation;
    4285    if (maskInformation.isValid()) {
    4386        const FloatRect& maskRect = maskInformation.maskRect();
     
    4588        cairo_mask_surface(m_cr.get(), maskInformation.maskSurface(), maskRect.x(), maskRect.y());
    4689    }
    47     m_maskImageStack.removeLast();
     90
     91    m_stateStack.removeLast();
     92    ASSERT(!m_stateStack.isEmpty());
     93    m_state = &m_stateStack.last();
    4894
    4995    cairo_restore(m_cr.get());
    5096}
    5197
     98PlatformContextCairo::~PlatformContextCairo()
     99{
     100}
     101
    52102void PlatformContextCairo::save()
    53103{
    54     m_maskImageStack.append(ImageMaskInformation());
     104    m_stateStack.append(State(*m_state));
     105    m_state = &m_stateStack.last();
    55106
    56107    cairo_save(m_cr.get());
     
    61112    // We must call savePlatformState at least once before we can use image masking,
    62113    // since we actually apply the mask in restorePlatformState.
    63     ASSERT(!m_maskImageStack.isEmpty());
    64     m_maskImageStack.last().update(surface, rect);
     114    ASSERT(!m_stateStack.isEmpty());
     115    m_state->m_imageMaskInformation.update(surface, rect);
    65116
    66117    // Cairo doesn't support the notion of an image clip, so we push a group here
     
    128179
    129180    cairo_save(m_cr.get());
    130     drawPatternToCairoContext(m_cr.get(), pattern.get(), destRect, context->getAlpha());
     181    drawPatternToCairoContext(m_cr.get(), pattern.get(), destRect, globalAlpha());
    131182    cairo_restore(m_cr.get());
    132183}
    133184
     185float PlatformContextCairo::globalAlpha() const
     186{
     187    return m_state->m_globalAlpha;
     188}
     189
     190void PlatformContextCairo::setGlobalAlpha(float globalAlpha)
     191{
     192    m_state->m_globalAlpha = globalAlpha;
     193}
    134194
    135195} // namespace WebCore
  • trunk/Source/WebCore/platform/graphics/cairo/PlatformContextCairo.h

    r87151 r87336  
    3333namespace WebCore {
    3434
    35 // In Cairo image masking is immediate, so to emulate image clipping we must save masking
    36 // details as part of the context state and apply them during platform restore.
    37 class ImageMaskInformation {
    38 public:
    39     void update(cairo_surface_t* maskSurface, const FloatRect& maskRect)
    40     {
    41         m_maskSurface = maskSurface;
    42         m_maskRect = maskRect;
    43     }
    44 
    45     bool isValid() const { return m_maskSurface; }
    46     cairo_surface_t* maskSurface() const { return m_maskSurface.get(); }
    47     const FloatRect& maskRect() const { return m_maskRect; }
    48 
    49 private:
    50     RefPtr<cairo_surface_t> m_maskSurface;
    51     FloatRect m_maskRect;
    52 };
    53 
    5435// Much like PlatformContextSkia in the Skia port, this class holds information that
    5536// would normally be private to GraphicsContext, except that we want to allow access
     
    6142public:
    6243    PlatformContextCairo(cairo_t*);
     44    ~PlatformContextCairo();
    6345
    6446    cairo_t* cr() { return m_cr.get(); }
     
    6749    void save();
    6850    void restore();
     51    void setGlobalAlpha(float);
     52    float globalAlpha() const;
     53
    6954    void pushImageMask(cairo_surface_t*, const FloatRect&);
    7055    void drawSurfaceToContext(cairo_surface_t*, const FloatRect& destRect, const FloatRect& srcRect, GraphicsContext*);
     
    7560private:
    7661    RefPtr<cairo_t> m_cr;
    77     Vector<ImageMaskInformation> m_maskImageStack;
     62
     63    class State;
     64    State* m_state;
     65    WTF::Vector<State> m_stateStack;
     66
    7867    InterpolationQuality m_imageInterpolationQuality;
    7968};
Note: See TracChangeset for help on using the changeset viewer.