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 "platform/graphics/DrawLooperBuilder.h"
     33 #include "platform/graphics/Gradient.h"
     34 #include "platform/graphics/GraphicsTypes.h"
     35 #include "platform/graphics/Path.h"
     36 #include "platform/graphics/Pattern.h"
     37 #include "platform/graphics/StrokeData.h"
     38 #include "platform/graphics/skia/SkiaUtils.h"
     39 #include "third_party/skia/include/core/SkColorFilter.h"
     40 #include "third_party/skia/include/core/SkPaint.h"
     41 #include "wtf/PassOwnPtr.h"
     42 #include "wtf/RefPtr.h"
     43 
     44 namespace WebCore {
     45 
     46 // Encapsulates the state information we store for each pushed graphics state.
     47 // Only GraphicsContext can use this class.
     48 class PLATFORM_EXPORT GraphicsContextState FINAL {
     49 public:
     50     static PassOwnPtr<GraphicsContextState> create()
     51     {
     52         return adoptPtr(new GraphicsContextState());
     53     }
     54 
     55     static PassOwnPtr<GraphicsContextState> createAndCopy(const GraphicsContextState& other)
     56     {
     57         return adoptPtr(new GraphicsContextState(other));
     58     }
     59 
     60     void copy(const GraphicsContextState&);
     61 
     62     // SkPaint objects that reflect the current state. If the length of the
     63     // path to be stroked is known, pass it in for correct dash or dot placement.
     64     const SkPaint& strokePaint(int strokedPathLength = 0) const;
     65     const SkPaint& fillPaint() const;
     66 
     67     uint16_t saveCount() const { return m_saveCount; }
     68     void incrementSaveCount() { ++m_saveCount; }
     69     void decrementSaveCount() { --m_saveCount; }
     70 
     71     // Stroke data
     72     const StrokeData& strokeData() const { return m_strokeData; }
     73 
     74     void setStrokeStyle(StrokeStyle);
     75 
     76     void setStrokeThickness(float);
     77 
     78     SkColor effectiveStrokeColor() const { return applyAlpha(m_strokeData.color().rgb()); }
     79     void setStrokeColor(const Color&);
     80 
     81     void setStrokeGradient(const PassRefPtr<Gradient>);
     82     void clearStrokeGradient();
     83 
     84     void setStrokePattern(const PassRefPtr<Pattern>);
     85     void clearStrokePattern();
     86 
     87     void setLineCap(LineCap);
     88 
     89     void setLineJoin(LineJoin);
     90 
     91     void setMiterLimit(float);
     92 
     93     void setLineDash(const DashArray&, float);
     94 
     95     // Fill data
     96     Color fillColor() const { return m_fillColor; }
     97     SkColor effectiveFillColor() const { return applyAlpha(m_fillColor.rgb()); }
     98     void setFillColor(const Color&);
     99 
    100     Gradient* fillGradient() const { return m_fillGradient.get(); }
    101     void setFillGradient(const PassRefPtr<Gradient>);
    102     void clearFillGradient();
    103 
    104     Pattern* fillPattern() const { return m_fillPattern.get(); }
    105     void setFillPattern(const PassRefPtr<Pattern>);
    106     void clearFillPattern();
    107 
    108     // Path fill rule
    109     WindRule fillRule() const { return m_fillRule; }
    110     void setFillRule(WindRule rule) { m_fillRule = rule; }
    111 
    112     // Shadow. (This will need tweaking if we use draw loopers for other things.)
    113     SkDrawLooper* drawLooper() const { return m_looper.get(); }
    114     void setDrawLooper(PassRefPtr<SkDrawLooper>);
    115     void clearDrawLooper();
    116 
    117     // Text. (See TextModeFill & friends.)
    118     TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
    119     void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
    120 
    121     // Common shader state.
    122     int alpha() const { return m_alpha; }
    123     void setAlphaAsFloat(float);
    124 
    125     SkColorFilter* colorFilter() const { return m_colorFilter.get(); }
    126     void setColorFilter(PassRefPtr<SkColorFilter>);
    127 
    128     // Compositing control, for the CSS and Canvas compositing spec.
    129     void setCompositeOperation(CompositeOperator, blink::WebBlendMode);
    130     CompositeOperator compositeOperator() const { return m_compositeOperator; }
    131     blink::WebBlendMode blendMode() const { return m_blendMode; }
    132     SkXfermode* xferMode() const { return m_xferMode.get(); }
    133 
    134     // Image interpolation control.
    135     InterpolationQuality interpolationQuality() const { return m_interpolationQuality; }
    136     void setInterpolationQuality(InterpolationQuality);
    137 
    138     bool shouldAntialias() const { return m_shouldAntialias; }
    139     void setShouldAntialias(bool);
    140 
    141     bool shouldSmoothFonts() const { return m_shouldSmoothFonts; }
    142     void setShouldSmoothFonts(bool shouldSmoothFonts) { m_shouldSmoothFonts = shouldSmoothFonts; }
    143 
    144     bool shouldClampToSourceRect() const { return m_shouldClampToSourceRect; }
    145     void setShouldClampToSourceRect(bool shouldClampToSourceRect) { m_shouldClampToSourceRect = shouldClampToSourceRect; }
    146 
    147 private:
    148     GraphicsContextState();
    149     explicit GraphicsContextState(const GraphicsContextState&);
    150     GraphicsContextState& operator=(const GraphicsContextState&);
    151 
    152     // Helper function for applying the state's alpha value to the given input
    153     // color to produce a new output color.
    154     SkColor applyAlpha(SkColor c) const
    155     {
    156         int a = SkAlphaMul(SkColorGetA(c), m_alpha);
    157         return (c & 0x00FFFFFF) | (a << 24);
    158     }
    159 
    160     // These are mutbale to enable gradient updates when the paints are fetched for use.
    161     mutable SkPaint m_strokePaint;
    162     mutable SkPaint m_fillPaint;
    163 
    164     StrokeData m_strokeData;
    165 
    166     Color m_fillColor;
    167     WindRule m_fillRule;
    168     RefPtr<Gradient> m_fillGradient;
    169     RefPtr<Pattern> m_fillPattern;
    170 
    171     RefPtr<SkDrawLooper> m_looper;
    172 
    173     TextDrawingModeFlags m_textDrawingMode;
    174 
    175     int m_alpha;
    176     RefPtr<SkXfermode> m_xferMode;
    177     RefPtr<SkColorFilter> m_colorFilter;
    178 
    179     CompositeOperator m_compositeOperator;
    180     blink::WebBlendMode m_blendMode;
    181 
    182     InterpolationQuality m_interpolationQuality;
    183 
    184     uint16_t m_saveCount;
    185 
    186     bool m_shouldAntialias : 1;
    187     bool m_shouldSmoothFonts : 1;
    188     bool m_shouldClampToSourceRect : 1;
    189 };
    190 
    191 } // namespace WebCore
    192 
    193 #endif // GraphicsContextState_h
    194