PlatformGraphicsContext.h revision 22ddc4a8f76077593fba14c8cafffc74f9170dab
1/*
2 * Copyright 2006, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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 copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef platform_graphics_context_h
27#define platform_graphics_context_h
28
29#include "IntRect.h"
30#include "GraphicsContext.h"
31#include "RenderSkinAndroid.h"
32#include "SkCanvas.h"
33#include "SkPicture.h"
34#include "SkTDArray.h"
35#include <wtf/Vector.h>
36
37class SkCanvas;
38
39namespace WebCore {
40
41class PlatformGraphicsContext {
42public:
43    PlatformGraphicsContext();
44    virtual ~PlatformGraphicsContext();
45    virtual bool isPaintingDisabled() = 0;
46    virtual SkCanvas* getCanvas() = 0;
47
48    void setGraphicsContext(GraphicsContext* gc) { m_gc = gc; }
49    virtual bool deleteUs() const { return false; }
50
51    typedef enum { PaintingContext, RecordingContext } ContextType;
52    virtual ContextType type() = 0;
53
54    // State management
55    virtual void beginTransparencyLayer(float opacity) = 0;
56    virtual void endTransparencyLayer() = 0;
57    virtual void save();
58    virtual void restore();
59
60    // State values
61    virtual void setAlpha(float alpha);
62    int getNormalizedAlpha() const;
63    virtual void setCompositeOperation(CompositeOperator op);
64    virtual bool setFillColor(const Color& c);
65    virtual bool setFillShader(SkShader* fillShader);
66    virtual void setLineCap(LineCap cap);
67    virtual void setLineDash(const DashArray& dashes, float dashOffset);
68    virtual void setLineJoin(LineJoin join);
69    virtual void setMiterLimit(float limit);
70    virtual void setShadow(int radius, int dx, int dy, SkColor c);
71    virtual void setShouldAntialias(bool useAA);
72    virtual bool setStrokeColor(const Color& c);
73    virtual bool setStrokeShader(SkShader* strokeShader);
74    virtual void setStrokeStyle(StrokeStyle style);
75    virtual void setStrokeThickness(float f);
76
77    // FIXME: These setupPaint* should be private, but
78    //        they are used by FontAndroid currently
79    virtual void setupPaintFill(SkPaint* paint) const;
80    virtual bool setupPaintShadow(SkPaint* paint, SkPoint* offset) const;
81    // Sets up the paint for stroking. Returns true if the style is really
82    // just a dash of squares (the size of the paint's stroke-width.
83    virtual bool setupPaintStroke(SkPaint* paint, SkRect* rect, bool isHLine = false);
84
85    // Matrix operations
86    virtual void concatCTM(const AffineTransform& affine) = 0;
87    virtual void rotate(float angleInRadians) = 0;
88    virtual void scale(const FloatSize& size) = 0;
89    virtual void translate(float x, float y) = 0;
90    virtual const SkMatrix& getTotalMatrix() = 0;
91
92    // Clipping
93    virtual void addInnerRoundedRectClip(const IntRect& rect, int thickness) = 0;
94    virtual void canvasClip(const Path& path) = 0;
95    virtual bool clip(const FloatRect& rect) = 0;
96    virtual bool clip(const Path& path) = 0;
97    virtual bool clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias) = 0;
98    virtual bool clipOut(const IntRect& r) = 0;
99    virtual bool clipOut(const Path& p) = 0;
100    virtual bool clipPath(const Path& pathToClip, WindRule clipRule) = 0;
101
102    // Drawing
103    virtual void clearRect(const FloatRect& rect) = 0;
104    virtual void drawBitmapPattern(const SkBitmap& bitmap, const SkMatrix& matrix,
105                           CompositeOperator compositeOp, const FloatRect& destRect) = 0;
106    virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
107                        const SkRect& dst, CompositeOperator op) = 0;
108    virtual void drawConvexPolygon(size_t numPoints, const FloatPoint* points,
109                           bool shouldAntialias) = 0;
110    virtual void drawEllipse(const IntRect& rect) = 0;
111    virtual void drawFocusRing(const Vector<IntRect>& rects, int /* width */,
112                       int /* offset */, const Color& color) = 0;
113    virtual void drawHighlightForText(const Font& font, const TextRun& run,
114                              const FloatPoint& point, int h,
115                              const Color& backgroundColor, ColorSpace colorSpace,
116                              int from, int to, bool isActive) = 0;
117    virtual void drawLine(const IntPoint& point1, const IntPoint& point2) = 0;
118    virtual void drawLineForText(const FloatPoint& pt, float width) = 0;
119    virtual void drawLineForTextChecking(const FloatPoint& pt, float width,
120                                 GraphicsContext::TextCheckingLineStyle) = 0;
121    virtual void drawRect(const IntRect& rect) = 0;
122    virtual void fillPath(const Path& pathToFill, WindRule fillRule) = 0;
123    virtual void fillRect(const FloatRect& rect) = 0;
124    void fillRect(const FloatRect& rect, const Color& color, ColorSpace) {
125        fillRect(rect, color);
126    }
127    virtual void fillRect(const FloatRect& rect, const Color& color) = 0;
128    void fillRoundedRect(const IntRect& rect, const IntSize& topLeft,
129                         const IntSize& topRight, const IntSize& bottomLeft,
130                         const IntSize& bottomRight, const Color& color,
131                         ColorSpace) {
132        fillRoundedRect(rect, topLeft, topRight, bottomLeft, bottomRight, color);
133    }
134    virtual void fillRoundedRect(const IntRect& rect, const IntSize& topLeft,
135                         const IntSize& topRight, const IntSize& bottomLeft,
136                         const IntSize& bottomRight, const Color& color) = 0;
137    virtual void strokeArc(const IntRect& r, int startAngle, int angleSpan) = 0;
138    virtual void strokePath(const Path& pathToStroke) = 0;
139    virtual void strokeRect(const FloatRect& rect, float lineWidth) = 0;
140
141    virtual SkCanvas* recordingCanvas() = 0;
142    virtual void endRecording(int type = 0) = 0;
143
144protected:
145
146    struct ShadowRec {
147        SkScalar blur;
148        SkScalar dx;
149        SkScalar dy;
150        SkColor color;  // alpha>0 means valid shadow
151        ShadowRec(SkScalar b = 0,
152                  SkScalar x = 0,
153                  SkScalar y = 0,
154                  SkColor c = 0) // by default, alpha=0, so no shadow
155                : blur(b), dx(x), dy(y), color(c)
156            {};
157    };
158
159    class State {
160    public:
161        SkPathEffect* pathEffect;
162        float miterLimit;
163        float alpha;
164        float strokeThickness;
165        SkPaint::Cap lineCap;
166        SkPaint::Join lineJoin;
167        SkXfermode::Mode mode;
168        int dashRatio; // Ratio of the length of a dash to its width
169        ShadowRec shadow;
170        SkColor fillColor;
171        SkShader* fillShader;
172        SkColor strokeColor;
173        SkShader* strokeShader;
174        bool useAA;
175        StrokeStyle strokeStyle;
176
177        State();
178        State(const State& other);
179        ~State();
180
181        void setShadow(int radius, int dx, int dy, SkColor c);
182        bool setupShadowPaint(SkPaint* paint, SkPoint* offset,
183                              bool shadowsIgnoreTransforms);
184        SkColor applyAlpha(SkColor c) const;
185
186        State cloneInheritedProperties();
187    private:
188        // Not supported.
189        void operator=(const State&);
190
191        friend class PlatformGraphicsContextRecording;
192        friend class PlatformGraphicsContextSkia;
193    };
194
195    virtual bool shadowsIgnoreTransforms() const = 0;
196    void setupPaintCommon(SkPaint* paint) const;
197    GraphicsContext* m_gc; // Back-ptr to our parent
198
199    struct State;
200    WTF::Vector<State> m_stateStack;
201    State* m_state;
202};
203
204}
205#endif
206