1e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// Copyright (C) 2013 Google Inc. All rights reserved.
2e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)//
3e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
4e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// modification, are permitted provided that the following conditions are
5e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// met:
6e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)//
7e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)//    * Redistributions of source code must retain the above copyright
8e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
9e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)//    * Redistributions in binary form must reproduce the above
10e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
11e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// in the documentation and/or other materials provided with the
12e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// distribution.
13e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)//    * Neither the name of Google Inc. nor the names of its
14e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// contributors may be used to endorse or promote products derived from
15e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// this software without specific prior written permission.
16e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)//
17e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
29e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)#ifndef GraphicsContextState_h
30e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)#define GraphicsContextState_h
31e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
32d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "platform/graphics/DrawLooperBuilder.h"
33a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/graphics/Gradient.h"
34f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#include "platform/graphics/GraphicsTypes.h"
35a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/graphics/Path.h"
36a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/graphics/Pattern.h"
37a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/graphics/StrokeData.h"
38c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles)#include "third_party/skia/include/core/SkColorFilter.h"
39d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "third_party/skia/include/core/SkPaint.h"
40e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)#include "wtf/PassOwnPtr.h"
41e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#include "wtf/RefPtr.h"
42e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
43c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
44e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
45e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// Encapsulates the state information we store for each pushed graphics state.
46e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)// Only GraphicsContext can use this class.
47d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)class PLATFORM_EXPORT GraphicsContextState FINAL {
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)public:
49323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    static PassOwnPtr<GraphicsContextState> create()
50323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    {
51323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        return adoptPtr(new GraphicsContextState());
52323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    }
53e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
545d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    static PassOwnPtr<GraphicsContextState> createAndCopy(const GraphicsContextState& other)
555d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    {
565d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        return adoptPtr(new GraphicsContextState(other));
575d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    }
585d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
595d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    void copy(const GraphicsContextState&);
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // SkPaint objects that reflect the current state. If the length of the
62d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // path to be stroked is known, pass it in for correct dash or dot placement.
63d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    const SkPaint& strokePaint(int strokedPathLength = 0) const;
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    const SkPaint& fillPaint() const;
65d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    uint16_t saveCount() const { return m_saveCount; }
67d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void incrementSaveCount() { ++m_saveCount; }
68d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void decrementSaveCount() { --m_saveCount; }
69d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
70d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Stroke data
717242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    Color strokeColor() const { return m_strokeColor; }
727242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    SkColor effectiveStrokeColor() const { return applyAlpha(m_strokeColor.rgb()); }
73d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setStrokeColor(const Color&);
74e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
757242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    Gradient* strokeGradient() const { return m_strokeGradient.get(); }
76d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setStrokeGradient(const PassRefPtr<Gradient>);
77d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void clearStrokeGradient();
78d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
797242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    Pattern* strokePattern() const { return m_strokePattern.get(); }
80d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setStrokePattern(const PassRefPtr<Pattern>);
81d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void clearStrokePattern();
82d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
837242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    const StrokeData& strokeData() const { return m_strokeData; }
847242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void setStrokeStyle(StrokeStyle);
857242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void setStrokeThickness(float);
86d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setLineCap(LineCap);
87d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setLineJoin(LineJoin);
88d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setMiterLimit(float);
89d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setLineDash(const DashArray&, float);
90d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
91d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Fill data
92d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    Color fillColor() const { return m_fillColor; }
93d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    SkColor effectiveFillColor() const { return applyAlpha(m_fillColor.rgb()); }
94d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setFillColor(const Color&);
95d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
96d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    Gradient* fillGradient() const { return m_fillGradient.get(); }
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setFillGradient(const PassRefPtr<Gradient>);
98d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void clearFillGradient();
99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
100d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    Pattern* fillPattern() const { return m_fillPattern.get(); }
101d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setFillPattern(const PassRefPtr<Pattern>);
102d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void clearFillPattern();
103d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
104d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Path fill rule
105d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    WindRule fillRule() const { return m_fillRule; }
106d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setFillRule(WindRule rule) { m_fillRule = rule; }
107d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
108d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Shadow. (This will need tweaking if we use draw loopers for other things.)
109d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    SkDrawLooper* drawLooper() const { return m_looper.get(); }
110d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setDrawLooper(PassRefPtr<SkDrawLooper>);
111d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void clearDrawLooper();
112d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
113d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Text. (See TextModeFill & friends.)
114d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    TextDrawingModeFlags textDrawingMode() const { return m_textDrawingMode; }
115d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setTextDrawingMode(TextDrawingModeFlags mode) { m_textDrawingMode = mode; }
116d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
117d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Common shader state.
118d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    int alpha() const { return m_alpha; }
119d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setAlphaAsFloat(float);
120d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
121d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    SkColorFilter* colorFilter() const { return m_colorFilter.get(); }
122d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setColorFilter(PassRefPtr<SkColorFilter>);
123d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
124d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Compositing control, for the CSS and Canvas compositing spec.
125e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    void setCompositeOperation(CompositeOperator, WebBlendMode);
126d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    CompositeOperator compositeOperator() const { return m_compositeOperator; }
127e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    WebBlendMode blendMode() const { return m_blendMode; }
128d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
129d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Image interpolation control.
130d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    InterpolationQuality interpolationQuality() const { return m_interpolationQuality; }
131d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setInterpolationQuality(InterpolationQuality);
132d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
133d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    bool shouldAntialias() const { return m_shouldAntialias; }
134d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setShouldAntialias(bool);
135d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
136d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    bool shouldClampToSourceRect() const { return m_shouldClampToSourceRect; }
137d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    void setShouldClampToSourceRect(bool shouldClampToSourceRect) { m_shouldClampToSourceRect = shouldClampToSourceRect; }
138d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
139d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)private:
140323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    GraphicsContextState();
1415d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    explicit GraphicsContextState(const GraphicsContextState&);
1425d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    GraphicsContextState& operator=(const GraphicsContextState&);
143323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
144e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    // Helper function for applying the state's alpha value to the given input
145e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    // color to produce a new output color.
146e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    SkColor applyAlpha(SkColor c) const
147e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    {
148d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        int a = SkAlphaMul(SkColorGetA(c), m_alpha);
149e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)        return (c & 0x00FFFFFF) | (a << 24);
150e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    }
151e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
152d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // These are mutbale to enable gradient updates when the paints are fetched for use.
153d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    mutable SkPaint m_strokePaint;
154d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    mutable SkPaint m_fillPaint;
155d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
156521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)    StrokeData m_strokeData;
157e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
1587242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    Color m_strokeColor;
1597242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    RefPtr<Gradient> m_strokeGradient;
1607242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    RefPtr<Pattern> m_strokePattern;
1617242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
162e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    Color m_fillColor;
163e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    WindRule m_fillRule;
164e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    RefPtr<Gradient> m_fillGradient;
165e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    RefPtr<Pattern> m_fillPattern;
166e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
167e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    RefPtr<SkDrawLooper> m_looper;
168e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
169e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    TextDrawingModeFlags m_textDrawingMode;
170e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
171d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    int m_alpha;
172c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles)    RefPtr<SkColorFilter> m_colorFilter;
173e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
174e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    CompositeOperator m_compositeOperator;
175e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    WebBlendMode m_blendMode;
176e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
177e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    InterpolationQuality m_interpolationQuality;
178e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
17909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    uint16_t m_saveCount;
18009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
181e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)    bool m_shouldAntialias : 1;
1829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_shouldClampToSourceRect : 1;
183e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)};
184e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
185c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
186e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)
187e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)#endif // GraphicsContextState_h
188