initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / skia / PlatformContextSkia.h
1 /*
2  * Copyright (c) 2008, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef PlatformContextSkia_h
32 #define PlatformContextSkia_h
33
34 #include "GraphicsContext.h"
35 #include "Noncopyable.h"
36
37 #include "SkCanvas.h"
38 #include "SkDashPathEffect.h"
39 #include "SkDrawLooper.h"
40 #include "SkPaint.h"
41 #include "SkPath.h"
42
43 #include <wtf/Vector.h>
44
45 namespace WebCore {
46
47 enum CompositeOperator;
48 class GraphicsContext3D;
49 class Texture;
50
51 // This class holds the platform-specific state for GraphicsContext. We put
52 // most of our Skia wrappers on this class. In theory, a lot of this stuff could
53 // be moved to GraphicsContext directly, except that some code external to this
54 // would like to poke at our graphics layer as well (like the Image and Font
55 // stuff, which needs some amount of our wrappers and state around SkCanvas).
56 //
57 // So in general, this class uses just Skia types except when there's no easy
58 // conversion. GraphicsContext is responsible for converting the WebKit types to
59 // Skia types and setting up the eventual call to the Skia functions.
60 //
61 // This class then keeps track of all the current Skia state. WebKit expects
62 // that the graphics state that is pushed and popped by save() and restore()
63 // includes things like colors and pen styles. Skia does this differently, where
64 // push and pop only includes transforms and bitmaps, and the application is
65 // responsible for managing the painting state which is store in separate
66 // SkPaint objects. This class provides the adaptor that allows the painting
67 // state to be pushed and popped along with the bitmap.
68 class PlatformContextSkia {
69     WTF_MAKE_NONCOPYABLE(PlatformContextSkia);
70 public:
71     // For printing, there shouldn't be any canvas. canvas can be NULL. If you
72     // supply a NULL canvas, you can also call setCanvas later.
73     PlatformContextSkia(SkCanvas*);
74     ~PlatformContextSkia();
75
76     // Sets the graphics context associated with this context.
77     // GraphicsContextSkia calls this from its constructor.
78     void setGraphicsContext(const GraphicsContext* gc) { m_gc = gc; }
79
80     // Sets the canvas associated with this context. Use when supplying NULL
81     // to the constructor.
82     void setCanvas(SkCanvas*);
83
84     // If false we're rendering to a GraphicsContext for a web page, if false
85     // we're not (as is the case when rendering to a canvas object).
86     // If this is true the contents have not been marked up with the magic
87     // color and all text drawing needs to go to a layer so that the alpha is
88     // correctly updated.
89     void setDrawingToImageBuffer(bool);
90     bool isDrawingToImageBuffer() const;
91
92     void save();
93     void restore();
94
95     // Begins a layer that is clipped to the image |imageBuffer| at the location
96     // |rect|. This layer is implicitly restored when the next restore is
97     // invoked.
98     // NOTE: |imageBuffer| may be deleted before the |restore| is invoked.
99     void beginLayerClippedToImage(const FloatRect&, const ImageBuffer*);
100     void clipPathAntiAliased(const SkPath&);
101
102     // Sets up the common flags on a paint for antialiasing, effects, etc.
103     // This is implicitly called by setupPaintFill and setupPaintStroke, but
104     // you may wish to call it directly sometimes if you don't want that other
105     // behavior.
106     void setupPaintCommon(SkPaint*) const;
107
108     // Sets up the paint for the current fill style.
109     void setupPaintForFilling(SkPaint*) const;
110
111     // Sets up the paint for stroking. Returns an int representing the width of
112     // the pen, or 1 if the pen's width is 0 if a non-zero length is provided,
113     // the number of dashes/dots on a dashed/dotted line will be adjusted to
114     // start and end that length with a dash/dot.
115     float setupPaintForStroking(SkPaint*, SkRect*, int length) const;
116
117     // State setting functions.
118     void setDrawLooper(SkDrawLooper*);  // Note: takes an additional ref.
119     void setMiterLimit(float);
120     void setAlpha(float);
121     void setLineCap(SkPaint::Cap);
122     void setLineJoin(SkPaint::Join);
123     void setXfermodeMode(SkXfermode::Mode);
124     void setFillColor(SkColor);
125     void setStrokeStyle(StrokeStyle);
126     void setStrokeColor(SkColor);
127     void setStrokeThickness(float thickness);
128     void setTextDrawingMode(TextDrawingModeFlags mode);
129     void setUseAntialiasing(bool enable);
130     void setDashPathEffect(SkDashPathEffect*);
131
132     SkDrawLooper* getDrawLooper() const;
133     StrokeStyle getStrokeStyle() const;
134     float getStrokeThickness() const;
135     TextDrawingModeFlags getTextDrawingMode() const;
136     float getAlpha() const;
137     int getNormalizedAlpha() const;
138     SkXfermode::Mode getXfermodeMode() const;
139
140     void canvasClipPath(const SkPath&);
141
142     // Returns the fill color. The returned color has it's alpha adjusted
143     // by the current alpha.
144     SkColor effectiveFillColor() const;
145
146     // Returns the stroke color. The returned color has it's alpha adjusted
147     // by the current alpha.
148     SkColor effectiveStrokeColor() const;
149
150     // Returns the canvas used for painting, NOT guaranteed to be non-null.
151     SkCanvas* canvas() { return m_canvas; }
152
153     InterpolationQuality interpolationQuality() const;
154     void setInterpolationQuality(InterpolationQuality interpolationQuality);
155
156     // FIXME: This should be pushed down to GraphicsContext.
157     void drawRect(SkRect rect);
158
159     // FIXME: I'm still unsure how I will serialize this call.
160     void paintSkPaint(const SkRect&, const SkPaint&);
161
162     const SkBitmap* bitmap() const;
163
164     // Returns if the context is a printing context instead of a display
165     // context. Bitmap shouldn't be resampled when printing to keep the best
166     // possible quality.
167     bool printing() const { return m_printing; }
168     void setPrinting(bool p) { m_printing = p; }
169
170     void getImageResamplingHint(IntSize* srcSize, FloatSize* dstSize) const;
171     void setImageResamplingHint(const IntSize& srcSize, const FloatSize& dstSize);
172     void clearImageResamplingHint();
173     bool hasImageResamplingHint() const;
174
175     bool isAccelerated() const { return m_gpuContext; }
176     void setGraphicsContext3D(GraphicsContext3D*);
177
178 private:
179     // Used when restoring and the state has an image clip. Only shows the pixels in
180     // m_canvas that are also in imageBuffer.
181     void applyClipFromImage(const FloatRect&, const SkBitmap&);
182     void applyAntiAliasedClipPaths(WTF::Vector<SkPath>& paths);
183
184     // common code between setupPaintFor[Filling,Stroking]
185     void setupShader(SkPaint*, Gradient*, Pattern*, SkColor) const;
186
187     // Defines drawing style.
188     struct State;
189
190     // NULL indicates painting is disabled. Never delete this object.
191     SkCanvas* m_canvas;
192     const GraphicsContext* m_gc;
193
194     // States stack. Enables local drawing state change with save()/restore()
195     // calls.
196     WTF::Vector<State> m_stateStack;
197     // Pointer to the current drawing state. This is a cached value of
198     // mStateStack.back().
199     State* m_state;
200
201     // Stores image sizes for a hint to compute image resampling modes.
202     // Values are used in ImageSkia.cpp
203     IntSize m_imageResamplingHintSrcSize;
204     FloatSize m_imageResamplingHintDstSize;
205     bool m_printing;
206     bool m_drawingToImageBuffer;
207     GraphicsContext3D* m_gpuContext;
208 };
209
210 }
211 #endif // PlatformContextSkia_h