• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Gradient.h"
33 #include "platform/graphics/GraphicsTypes.h"
34 #include "platform/graphics/Path.h"
35 #include "platform/graphics/Pattern.h"
36 #include "platform/graphics/StrokeData.h"
37 #include "third_party/skia/include/core/SkColorFilter.h"
38 #include "third_party/skia/include/core/SkColorPriv.h"
39 #include "third_party/skia/include/core/SkDrawLooper.h"
40 #include "third_party/skia/include/effects/SkDashPathEffect.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 GraphicsContextState {
49 private:
50     friend class GraphicsContext;
51 
GraphicsContextState()52     GraphicsContextState()
53         : m_fillColor(Color::black)
54         , m_fillRule(RULE_NONZERO)
55         , m_textDrawingMode(TextModeFill)
56         , m_alpha(1)
57         , m_xferMode(0)
58         , m_compositeOperator(CompositeSourceOver)
59         , m_blendMode(blink::WebBlendModeNormal)
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         , m_shouldClampToSourceRect(true)
68     {
69     }
70 
GraphicsContextState(const GraphicsContextState & other)71     GraphicsContextState(const GraphicsContextState& other)
72         : m_strokeData(other.m_strokeData)
73         , m_fillColor(other.m_fillColor)
74         , m_fillRule(other.m_fillRule)
75         , m_fillGradient(other.m_fillGradient)
76         , m_fillPattern(other.m_fillPattern)
77         , m_looper(other.m_looper)
78         , m_textDrawingMode(other.m_textDrawingMode)
79         , m_alpha(other.m_alpha)
80         , m_xferMode(other.m_xferMode)
81         , m_colorFilter(other.m_colorFilter)
82         , m_compositeOperator(other.m_compositeOperator)
83         , m_blendMode(other.m_blendMode)
84         , m_interpolationQuality(other.m_interpolationQuality)
85         , m_shouldAntialias(other.m_shouldAntialias)
86         , m_shouldSmoothFonts(other.m_shouldSmoothFonts)
87         , m_shouldClampToSourceRect(other.m_shouldClampToSourceRect)
88     {
89     }
90 
91     // Helper function for applying the state's alpha value to the given input
92     // color to produce a new output color.
applyAlpha(SkColor c)93     SkColor applyAlpha(SkColor c) const
94     {
95         int s = roundf(m_alpha * 256);
96         if (s >= 256)
97             return c;
98         if (s < 0)
99             return 0;
100 
101         int a = SkAlphaMul(SkColorGetA(c), s);
102         return (c & 0x00FFFFFF) | (a << 24);
103     }
104 
105     // Returns a new State with all of this object's inherited properties copied.
clone()106     PassOwnPtr<GraphicsContextState> clone() { return adoptPtr(new GraphicsContextState(*this)); }
107 
108     // Not supported. No implementations.
109     void operator=(const GraphicsContextState&);
110 
111     // Stroke.
112     StrokeData m_strokeData;
113 
114     // Fill.
115     Color m_fillColor;
116     WindRule m_fillRule;
117     RefPtr<Gradient> m_fillGradient;
118     RefPtr<Pattern> m_fillPattern;
119 
120     // Shadow. (This will need tweaking if we use draw loopers for other things.)
121     RefPtr<SkDrawLooper> m_looper;
122 
123     // Text. (See TextModeFill & friends.)
124     TextDrawingModeFlags m_textDrawingMode;
125 
126     // Common shader state.
127     float m_alpha;
128     RefPtr<SkXfermode> m_xferMode;
129     RefPtr<SkColorFilter> m_colorFilter;
130 
131     // Compositing control, for the CSS and Canvas compositing spec.
132     CompositeOperator m_compositeOperator;
133     blink::WebBlendMode m_blendMode;
134 
135     // Image interpolation control.
136     InterpolationQuality m_interpolationQuality;
137 
138     bool m_shouldAntialias : 1;
139     bool m_shouldSmoothFonts : 1;
140     bool m_shouldClampToSourceRect : 1;
141 };
142 
143 } // namespace WebCore
144 
145 #endif // GraphicsContextState_h
146 
147