Changeset 190894 in webkit


Ignore:
Timestamp:
Oct 12, 2015, 3:36:03 PM (10 years ago)
Author:
Simon Fraser
Message:

Add a CGContextStateSaver and use it
https://bugs.webkit.org/show_bug.cgi?id=150049

Reviewed by Tim Horton.

Add a stack-based graphics state save/restore class for CGContext,
like the one we have for GraphicsContext, and use it in GraphicsContextCG.

  • platform/graphics/cg/GraphicsContextCG.cpp:

(WebCore::GraphicsContext::drawNativeImage):
(WebCore::GraphicsContext::drawLine):
(WebCore::GraphicsContext::drawJoinedLines):
(WebCore::GraphicsContext::fillPath):
(WebCore::GraphicsContext::strokePath):
(WebCore::GraphicsContext::fillRect):
(WebCore::GraphicsContext::platformFillRoundedRect):
(WebCore::GraphicsContext::fillRectWithRoundedHole):
(WebCore::GraphicsContext::strokeRect):

  • platform/graphics/cg/GraphicsContextCG.h:

(WebCore::CGContextStateSaver::CGContextStateSaver):
(WebCore::CGContextStateSaver::~CGContextStateSaver):
(WebCore::CGContextStateSaver::save):
(WebCore::CGContextStateSaver::restore):

Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r190893 r190894  
     12015-10-12  Simon Fraser  <simon.fraser@apple.com>
     2
     3        Add a CGContextStateSaver and use it
     4        https://bugs.webkit.org/show_bug.cgi?id=150049
     5
     6        Reviewed by Tim Horton.
     7
     8        Add a stack-based graphics state save/restore class for CGContext,
     9        like the one we have for GraphicsContext, and use it in GraphicsContextCG.
     10       
     11        * platform/graphics/cg/GraphicsContextCG.cpp:
     12        (WebCore::GraphicsContext::drawNativeImage):
     13        (WebCore::GraphicsContext::drawLine):
     14        (WebCore::GraphicsContext::drawJoinedLines):
     15        (WebCore::GraphicsContext::fillPath):
     16        (WebCore::GraphicsContext::strokePath):
     17        (WebCore::GraphicsContext::fillRect):
     18        (WebCore::GraphicsContext::platformFillRoundedRect):
     19        (WebCore::GraphicsContext::fillRectWithRoundedHole):
     20        (WebCore::GraphicsContext::strokeRect):
     21        * platform/graphics/cg/GraphicsContextCG.h:
     22        (WebCore::CGContextStateSaver::CGContextStateSaver):
     23        (WebCore::CGContextStateSaver::~CGContextStateSaver):
     24        (WebCore::CGContextStateSaver::save):
     25        (WebCore::CGContextStateSaver::restore):
     26
    1272015-10-12  Zalan Bujtas  <zalan@apple.com>
    228
  • trunk/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp

    r189144 r190894  
    155155
    156156    CGContextRef context = platformContext();
    157     CGContextSaveGState(context);
     157    CGContextStateSaver stateSaver(context);
    158158
    159159#if PLATFORM(IOS)
     
    242242    // Draw the image.
    243243    CGContextDrawImage(context, adjustedDestRect, image.get());
    244 
    245     CGContextRestoreGState(context);
    246244}
    247245
     
    293291
    294292    CGContextRef context = platformContext();
     293
    295294    StrokeStyle strokeStyle = this->strokeStyle();
    296295    float cornerWidth = 0;
    297296    bool drawsDashedLine = strokeStyle == DottedStroke || strokeStyle == DashedStroke;
    298297
     298    CGContextStateSaver stateSaver(context, drawsDashedLine);
    299299    if (drawsDashedLine) {
    300         CGContextSaveGState(context);
    301300        // Figure out end points to ensure we always paint corners.
    302301        cornerWidth = strokeStyle == DottedStroke ? thickness : std::min(2 * thickness, std::max(thickness, strokeWidth / 3));
     
    312311        float patternWidth = strokeStyle == DottedStroke ? thickness : std::min(3 * thickness, std::max(thickness, strokeWidth / 3));
    313312        // Check if corner drawing sufficiently covers the line.
    314         if (strokeWidth <= patternWidth + 1) {
    315             CGContextRestoreGState(context);
     313        if (strokeWidth <= patternWidth + 1)
    316314            return;
    317         }
    318315
    319316        // Pattern starts with full fill and ends with the empty fill.
     
    363360    CGContextAddLineToPoint(context, p2.x(), p2.y());
    364361    CGContextStrokePath(context);
    365     if (drawsDashedLine)
    366         CGContextRestoreGState(context);
    367362    if (shouldAntialias())
    368363        CGContextSetShouldAntialias(context, true);
     
    378373    float width = CGContextGetLineWidth(context);
    379374
    380     CGContextSaveGState(context);
     375    CGContextStateSaver stateSaver(context);
    381376   
    382377    CGContextSetShouldAntialias(context, antialias);
    383 
    384378    CGContextSetLineWidth(context, width < 1 ? 1 : width);
    385 
    386379    CGContextBeginPath(context);
    387    
    388380    CGContextSetLineCap(context, lineCap);
    389    
    390381    CGContextMoveToPoint(context, points[0].x, points[0].y);
    391382   
     
    394385
    395386    CGContextStrokePath(context);
    396 
    397     CGContextRestoreGState(context);
    398387}
    399388#endif
     
    589578            CGContextBeginPath(context);
    590579            CGContextAddPath(context, path.platformPath());
    591             CGContextSaveGState(context);
     580            CGContextStateSaver stateSaver(context);
    592581            CGContextConcatCTM(context, m_state.fillGradient->gradientSpaceTransform());
    593582
     
    598587
    599588            m_state.fillGradient->paint(this);
    600             CGContextRestoreGState(context);
    601589        }
    602590
     
    655643            CGLayerRelease(layer);
    656644        } else {
    657             CGContextSaveGState(context);
     645            CGContextStateSaver stateSaver(context);
    658646            CGContextReplacePathWithStrokedPath(context);
    659647            CGContextClip(context);
    660648            CGContextConcatCTM(context, m_state.strokeGradient->gradientSpaceTransform());
    661649            m_state.strokeGradient->paint(this);
    662             CGContextRestoreGState(context);
    663650        }
    664651        return;
     
    678665
    679666    if (m_state.fillGradient) {
    680         CGContextSaveGState(context);
     667        CGContextStateSaver stateSaver(context);
    681668        if (hasShadow()) {
    682669            FloatSize layerSize = getCTM().mapSize(rect.size());
     
    699686            m_state.fillGradient->paint(this);
    700687        }
    701         CGContextRestoreGState(context);
    702688        return;
    703689    }
     
    707693
    708694    bool drawOwnShadow = !isAcceleratedContext() && hasBlurredShadow() && !m_state.shadowsIgnoreTransforms; // Don't use ShadowBlur for canvas yet.
     695    CGContextStateSaver stateSaver(context, drawOwnShadow);
    709696    if (drawOwnShadow) {
    710697        // Turn off CG shadows.
    711         CGContextSaveGState(context);
    712698        CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
    713699
     
    717703
    718704    CGContextFillRect(context, rect);
    719 
    720     if (drawOwnShadow)
    721         CGContextRestoreGState(context);
    722705}
    723706
     
    735718
    736719    bool drawOwnShadow = !isAcceleratedContext() && hasBlurredShadow() && !m_state.shadowsIgnoreTransforms; // Don't use ShadowBlur for canvas yet.
     720    CGContextStateSaver stateSaver(context, drawOwnShadow);
    737721    if (drawOwnShadow) {
    738722        // Turn off CG shadows.
    739         CGContextSaveGState(context);
    740723        CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
    741724
     
    747730   
    748731    if (drawOwnShadow)
    749         CGContextRestoreGState(context);
     732        stateSaver.restore();
    750733
    751734    if (oldFillColor != color || oldColorSpace != colorSpace)
     
    766749
    767750    bool drawOwnShadow = !isAcceleratedContext() && hasBlurredShadow() && !m_state.shadowsIgnoreTransforms; // Don't use ShadowBlur for canvas yet.
     751    CGContextStateSaver stateSaver(context, drawOwnShadow);
    768752    if (drawOwnShadow) {
    769753        // Turn off CG shadows.
    770         CGContextSaveGState(context);
    771754        CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
    772755
     
    789772
    790773    if (drawOwnShadow)
    791         CGContextRestoreGState(context);
     774        stateSaver.restore();
    792775
    793776    if (oldFillColor != color || oldColorSpace != colorSpace)
     
    819802    // fillRectWithRoundedHole() assumes that the edges of rect are clipped out, so we only care about shadows cast around inside the hole.
    820803    bool drawOwnShadow = !isAcceleratedContext() && hasBlurredShadow() && !m_state.shadowsIgnoreTransforms;
     804    CGContextStateSaver stateSaver(context, drawOwnShadow);
    821805    if (drawOwnShadow) {
    822806        // Turn off CG shadows.
    823         CGContextSaveGState(context);
    824807        CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
    825808
     
    831814
    832815    if (drawOwnShadow)
    833         CGContextRestoreGState(context);
     816        stateSaver.restore();
    834817   
    835818    setFillRule(oldFillRule);
     
    10471030            CGLayerRelease(layer);
    10481031        } else {
    1049             CGContextSaveGState(context);
     1032            CGContextStateSaver stateSaver(context);
    10501033            setStrokeThickness(lineWidth);
    10511034            CGContextAddRect(context, rect);
     
    10541037            CGContextConcatCTM(context, m_state.strokeGradient->gradientSpaceTransform());
    10551038            m_state.strokeGradient->paint(this);
    1056             CGContextRestoreGState(context);
    10571039        }
    10581040        return;
     
    10671049    // apply some attributes of the graphics state in certain cases
    10681050    // (as identified in https://bugs.webkit.org/show_bug.cgi?id=132948)
    1069     CGContextSaveGState(context);
     1051    CGContextStateSaver stateSaver(context);
    10701052    setStrokeThickness(lineWidth);
    10711053
    10721054    CGContextAddRect(context, rect);
    10731055    CGContextStrokePath(context);
    1074 
    1075     CGContextRestoreGState(context);
    10761056}
    10771057
  • trunk/Source/WebCore/platform/graphics/cg/GraphicsContextCG.h

    r172849 r190894  
    5151}
    5252
     53class CGContextStateSaver {
     54public:
     55    CGContextStateSaver(CGContextRef context, bool saveAndRestore = true)
     56        : m_context(context)
     57        , m_saveAndRestore(saveAndRestore)
     58    {
     59        if (m_saveAndRestore)
     60            CGContextSaveGState(m_context);
     61    }
     62   
     63    ~CGContextStateSaver()
     64    {
     65        if (m_saveAndRestore)
     66            CGContextRestoreGState(m_context);
     67    }
     68   
     69    void save()
     70    {
     71        ASSERT(!m_saveAndRestore);
     72        CGContextSaveGState(m_context);
     73        m_saveAndRestore = true;
     74    }
     75
     76    void restore()
     77    {
     78        ASSERT(m_saveAndRestore);
     79        CGContextRestoreGState(m_context);
     80        m_saveAndRestore = false;
     81    }
     82   
     83private:
     84    CGContextRef m_context;
     85    bool m_saveAndRestore;
     86};
     87
    5388}
    5489
Note: See TracChangeset for help on using the changeset viewer.