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