Home | History | Annotate | Download | only in graphics
      1 // Copyright (C) 2013 Google Inc. All rights reserved.
      2 //
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions are
      5 // met:
      6 //
      7 //    * Redistributions of source code must retain the above copyright
      8 // notice, this list of conditions and the following disclaimer.
      9 //    * Redistributions in binary form must reproduce the above
     10 // copyright notice, this list of conditions and the following disclaimer
     11 // in the documentation and/or other materials provided with the
     12 // distribution.
     13 //    * Neither the name of Google Inc. nor the names of its
     14 // contributors may be used to endorse or promote products derived from
     15 // this software without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 #ifndef GraphicsContextState_h
     30 #define GraphicsContextState_h
     31 
     32 #include "core/platform/graphics/Gradient.h"
     33 #include "core/platform/graphics/GraphicsTypes.h"
     34 #include "core/platform/graphics/Path.h"
     35 #include "core/platform/graphics/Pattern.h"
     36 #include "core/platform/graphics/StrokeData.h"
     37 #include "third_party/skia/include/core/SkColorPriv.h"
     38 #include "third_party/skia/include/core/SkDrawLooper.h"
     39 #include "third_party/skia/include/effects/SkDashPathEffect.h"
     40 #include "wtf/PassOwnPtr.h"
     41 #include "wtf/RefPtr.h"
     42 
     43 namespace WebCore {
     44 
     45 // Encapsulates the state information we store for each pushed graphics state.
     46 // Only GraphicsContext can use this class.
     47 class GraphicsContextState {
     48 private:
     49     friend class GraphicsContext;
     50 
     51     GraphicsContextState()
     52         : m_fillColor(Color::black)
     53         , m_fillRule(RULE_NONZERO)
     54         , m_textDrawingMode(TextModeFill)
     55         , m_alpha(1)
     56         , m_xferMode(SkXfermode::kSrcOver_Mode)
     57         , m_compositeOperator(CompositeSourceOver)
     58         , m_blendMode(BlendModeNormal)
     59         , m_clip(SkRect::MakeEmpty())
     60 #if USE(LOW_QUALITY_IMAGE_INTERPOLATION)
     61         , m_interpolationQuality(InterpolationLow)
     62 #else
     63         , m_interpolationQuality(InterpolationHigh)
     64 #endif
     65         , m_shouldAntialias(true)
     66         , m_shouldSmoothFonts(true)
     67     {
     68     }
     69 
     70     GraphicsContextState(const GraphicsContextState& other)
     71         : m_strokeData(other.m_strokeData)
     72         , m_fillColor(other.m_fillColor)
     73         , m_fillRule(other.m_fillRule)
     74         , m_fillGradient(other.m_fillGradient)
     75         , m_fillPattern(other.m_fillPattern)
     76         , m_looper(other.m_looper)
     77         , m_textDrawingMode(other.m_textDrawingMode)
     78         , m_alpha(other.m_alpha)
     79         , m_xferMode(other.m_xferMode)
     80         , m_compositeOperator(other.m_compositeOperator)
     81         , m_blendMode(other.m_blendMode)
     82         , m_clip(other.m_clip)
     83         , m_interpolationQuality(other.m_interpolationQuality)
     84         , m_shouldAntialias(other.m_shouldAntialias)
     85         , m_shouldSmoothFonts(other.m_shouldSmoothFonts)
     86     {
     87     }
     88 
     89     // Helper function for applying the state's alpha value to the given input
     90     // color to produce a new output color.
     91     SkColor applyAlpha(SkColor c) const
     92     {
     93         int s = roundf(m_alpha * 256);
     94         if (s >= 256)
     95             return c;
     96         if (s < 0)
     97             return 0;
     98 
     99         int a = SkAlphaMul(SkColorGetA(c), s);
    100         return (c & 0x00FFFFFF) | (a << 24);
    101     }
    102 
    103     // Returns a new State with all of this object's inherited properties copied.
    104     PassOwnPtr<GraphicsContextState> clone() { return adoptPtr(new GraphicsContextState(*this)); }
    105 
    106     // Not supported. No implementations.
    107     void operator=(const GraphicsContextState&);
    108 
    109     // Stroke.
    110     StrokeData m_strokeData;
    111 
    112     // Fill.
    113     Color m_fillColor;
    114     WindRule m_fillRule;
    115     RefPtr<Gradient> m_fillGradient;
    116     RefPtr<Pattern> m_fillPattern;
    117 
    118     // Shadow. (This will need tweaking if we use draw loopers for other things.)
    119     RefPtr<SkDrawLooper> m_looper;
    120 
    121     // Text. (See TextModeFill & friends.)
    122     TextDrawingModeFlags m_textDrawingMode;
    123 
    124     // Common shader state.
    125     float m_alpha;
    126     SkXfermode::Mode m_xferMode;
    127 
    128     // Compositing control, for the CSS and Canvas compositing spec.
    129     CompositeOperator m_compositeOperator;
    130     BlendMode m_blendMode;
    131 
    132     // If non-empty, the current State is clipped to this image.
    133     SkBitmap m_imageBufferClip;
    134 
    135     // If m_imageBufferClip is non-empty, this is the region the image is clipped to.
    136     SkRect m_clip;
    137 
    138     // Image interpolation control.
    139     InterpolationQuality m_interpolationQuality;
    140 
    141     bool m_shouldAntialias : 1;
    142     bool m_shouldSmoothFonts : 1;
    143 };
    144 
    145 } // namespace WebCore
    146 
    147 #endif // GraphicsContextState_h
    148 
    149