2 * Copyright (c) 2008, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef PlatformContextSkia_h
32 #define PlatformContextSkia_h
34 #include "GraphicsContext.h"
35 #include "Noncopyable.h"
37 #include "SkDashPathEffect.h"
38 #include "SkDrawLooper.h"
41 #include "skia/ext/platform_canvas.h"
43 #include <wtf/Vector.h>
47 enum CompositeOperator;
50 class GraphicsContext3D;
53 // This class holds the platform-specific state for GraphicsContext. We put
54 // most of our Skia wrappers on this class. In theory, a lot of this stuff could
55 // be moved to GraphicsContext directly, except that some code external to this
56 // would like to poke at our graphics layer as well (like the Image and Font
57 // stuff, which needs some amount of our wrappers and state around SkCanvas).
59 // So in general, this class uses just Skia types except when there's no easy
60 // conversion. GraphicsContext is responsible for converting the WebKit types to
61 // Skia types and setting up the eventual call to the Skia functions.
63 // This class then keeps track of all the current Skia state. WebKit expects
64 // that the graphics state that is pushed and popped by save() and restore()
65 // includes things like colors and pen styles. Skia does this differently, where
66 // push and pop only includes transforms and bitmaps, and the application is
67 // responsible for managing the painting state which is store in separate
68 // SkPaint objects. This class provides the adaptor that allows the painting
69 // state to be pushed and popped along with the bitmap.
70 class PlatformContextSkia {
71 WTF_MAKE_NONCOPYABLE(PlatformContextSkia);
73 // For printing, there shouldn't be any canvas. canvas can be NULL. If you
74 // supply a NULL canvas, you can also call setCanvas later.
75 PlatformContextSkia(skia::PlatformCanvas*);
76 ~PlatformContextSkia();
78 // Sets the canvas associated with this context. Use when supplying NULL
79 // to the constructor.
80 void setCanvas(skia::PlatformCanvas*);
82 // If false we're rendering to a GraphicsContext for a web page, if false
83 // we're not (as is the case when rendering to a canvas object).
84 // If this is true the contents have not been marked up with the magic
85 // color and all text drawing needs to go to a layer so that the alpha is
87 void setDrawingToImageBuffer(bool);
88 bool isDrawingToImageBuffer() const;
93 // Begins a layer that is clipped to the image |imageBuffer| at the location
94 // |rect|. This layer is implicitly restored when the next restore is
96 // NOTE: |imageBuffer| may be deleted before the |restore| is invoked.
97 void beginLayerClippedToImage(const FloatRect&, const ImageBuffer*);
98 void clipPathAntiAliased(const SkPath&);
100 // Sets up the common flags on a paint for antialiasing, effects, etc.
101 // This is implicitly called by setupPaintFill and setupPaintStroke, but
102 // you may wish to call it directly sometimes if you don't want that other
104 void setupPaintCommon(SkPaint*) const;
106 // Sets up the paint for the current fill style.
107 void setupPaintForFilling(SkPaint*) const;
109 // Sets up the paint for stroking. Returns an int representing the width of
110 // the pen, or 1 if the pen's width is 0 if a non-zero length is provided,
111 // the number of dashes/dots on a dashed/dotted line will be adjusted to
112 // start and end that length with a dash/dot.
113 float setupPaintForStroking(SkPaint*, SkRect*, int length) const;
115 // State setting functions.
116 void setDrawLooper(SkDrawLooper*); // Note: takes an additional ref.
117 void setMiterLimit(float);
118 void setAlpha(float);
119 void setLineCap(SkPaint::Cap);
120 void setLineJoin(SkPaint::Join);
121 void setXfermodeMode(SkXfermode::Mode);
122 void setFillColor(SkColor);
123 void setFillShader(SkShader*);
124 void setStrokeStyle(StrokeStyle);
125 void setStrokeColor(SkColor);
126 void setStrokeThickness(float thickness);
127 void setStrokeShader(SkShader*);
128 void setTextDrawingMode(TextDrawingModeFlags mode);
129 void setUseAntialiasing(bool enable);
130 void setDashPathEffect(SkDashPathEffect*);
132 SkDrawLooper* getDrawLooper() const;
133 StrokeStyle getStrokeStyle() const;
134 float getStrokeThickness() const;
135 TextDrawingModeFlags getTextDrawingMode() const;
136 float getAlpha() const;
137 int getNormalizedAlpha() const;
139 void canvasClipPath(const SkPath&);
141 // Returns the fill color. The returned color has it's alpha adjusted
142 // by the current alpha.
143 SkColor effectiveFillColor() const;
145 // Returns the stroke color. The returned color has it's alpha adjusted
146 // by the current alpha.
147 SkColor effectiveStrokeColor() const;
149 skia::PlatformCanvas* canvas() { return m_canvas; }
151 InterpolationQuality interpolationQuality() const;
152 void setInterpolationQuality(InterpolationQuality interpolationQuality);
154 // FIXME: This should be pushed down to GraphicsContext.
155 void drawRect(SkRect rect);
157 // FIXME: I'm still unsure how I will serialize this call.
158 void paintSkPaint(const SkRect&, const SkPaint&);
160 const SkBitmap* bitmap() const;
162 // Returns the canvas used for painting, NOT guaranteed to be non-NULL.
164 // Warning: This function is deprecated so the users are reminded that they
165 // should use this layer of indirection instead of using the canvas
166 // directly. This is to help with the eventual serialization.
167 skia::PlatformCanvas* canvas() const;
169 // Returns if the context is a printing context instead of a display
170 // context. Bitmap shouldn't be resampled when printing to keep the best
174 void getImageResamplingHint(IntSize* srcSize, FloatSize* dstSize) const;
175 void setImageResamplingHint(const IntSize& srcSize, const FloatSize& dstSize);
176 void clearImageResamplingHint();
177 bool hasImageResamplingHint() const;
179 bool canAccelerate() const;
180 bool canvasClipApplied() const;
181 bool useGPU() { return m_useGPU; }
182 void setSharedGraphicsContext3D(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&);
183 #if ENABLE(ACCELERATED_2D_CANVAS)
184 GLES2Canvas* gpuCanvas() const { return m_gpuCanvas.get(); }
186 GLES2Canvas* gpuCanvas() const { return 0; }
188 // Call these before making a call that manipulates the underlying
189 // skia::PlatformCanvas or WebCore::GLES2Canvas
190 void prepareForSoftwareDraw() const;
191 void prepareForHardwareDraw() const;
192 // Call to force the skia::PlatformCanvas to contain all rendering results.
193 void syncSoftwareCanvas() const;
194 void markDirtyRect(const IntRect& rect);
197 // Used when restoring and the state has an image clip. Only shows the pixels in
198 // m_canvas that are also in imageBuffer.
199 void applyClipFromImage(const FloatRect&, const SkBitmap&);
200 void applyAntiAliasedClipPaths(WTF::Vector<SkPath>& paths);
202 void uploadSoftwareToHardware(CompositeOperator) const;
203 void readbackHardwareToSoftware() const;
205 // Defines drawing style.
208 // NULL indicates painting is disabled. Never delete this object.
209 skia::PlatformCanvas* m_canvas;
211 // States stack. Enables local drawing state change with save()/restore()
213 WTF::Vector<State> m_stateStack;
214 // Pointer to the current drawing state. This is a cached value of
215 // mStateStack.back().
218 // Stores image sizes for a hint to compute image resampling modes.
219 // Values are used in ImageSkia.cpp
220 IntSize m_imageResamplingHintSrcSize;
221 FloatSize m_imageResamplingHintDstSize;
222 bool m_drawingToImageBuffer;
224 #if ENABLE(ACCELERATED_2D_CANVAS)
225 OwnPtr<GLES2Canvas> m_gpuCanvas;
226 mutable RefPtr<Texture> m_uploadTexture;
228 mutable enum { None, Software, Mixed, Hardware } m_backingStoreState;
229 mutable IntRect m_softwareDirtyRect;
233 #endif // PlatformContextSkia_h