Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / SkGpuDevice.h
1
2 /*
3  * Copyright 2010 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10
11 #ifndef SkGpuDevice_DEFINED
12 #define SkGpuDevice_DEFINED
13
14 #include "SkGr.h"
15 #include "SkBitmap.h"
16 #include "SkBitmapDevice.h"
17 #include "SkRegion.h"
18 #include "GrContext.h"
19
20 struct SkDrawProcs;
21 struct GrSkDrawProcs;
22
23 class GrTextContext;
24
25 /**
26  *  Subclass of SkBitmapDevice, which directs all drawing to the GrGpu owned by the
27  *  canvas.
28  */
29 class SK_API SkGpuDevice : public SkBitmapDevice {
30 public:
31
32     /**
33      * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render
34      * target. The caller owns a ref on the returned device.
35      */
36     static SkGpuDevice* Create(GrSurface* surface);
37
38     /**
39      *  New device that will create an offscreen renderTarget based on the
40      *  config, width, height, and sampleCount. The device's storage will not
41      *  count against the GrContext's texture cache budget. The device's pixels
42      *  will be uninitialized. TODO: This can fail, replace with a factory function.
43      */
44     SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height, int sampleCount = 0);
45
46     /**
47      *  DEPRECATED -- need to make this private, call Create(surface)
48      *  New device that will render to the specified renderTarget.
49      */
50     SkGpuDevice(GrContext*, GrRenderTarget*);
51
52     /**
53      *  DEPRECATED -- need to make this private, call Create(surface)
54      *  New device that will render to the texture (as a rendertarget).
55      *  The GrTexture's asRenderTarget() must be non-NULL or device will not
56      *  function.
57      */
58     SkGpuDevice(GrContext*, GrTexture*);
59
60     virtual ~SkGpuDevice();
61
62     GrContext* context() const { return fContext; }
63
64     virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
65
66     // overrides from SkBaseDevice
67     virtual uint32_t getDeviceCapabilities() SK_OVERRIDE {
68         return 0;
69     }
70     virtual int width() const SK_OVERRIDE {
71         return NULL == fRenderTarget ? 0 : fRenderTarget->width();
72     }
73     virtual int height() const SK_OVERRIDE {
74         return NULL == fRenderTarget ? 0 : fRenderTarget->height();
75     }
76     virtual bool isOpaque() const SK_OVERRIDE {
77         return NULL == fRenderTarget ? false
78                                      : kRGB_565_GrPixelConfig == fRenderTarget->config();
79     }
80     virtual SkBitmap::Config config() const SK_OVERRIDE;
81
82     virtual void clear(SkColor color) SK_OVERRIDE;
83     virtual void writePixels(const SkBitmap& bitmap, int x, int y,
84                              SkCanvas::Config8888 config8888) SK_OVERRIDE;
85
86     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
87     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
88                             const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
89     virtual void drawRect(const SkDraw&, const SkRect& r,
90                           const SkPaint& paint) SK_OVERRIDE;
91     virtual void drawRRect(const SkDraw&, const SkRRect& r,
92                            const SkPaint& paint) SK_OVERRIDE;
93     virtual void drawOval(const SkDraw&, const SkRect& oval,
94                           const SkPaint& paint) SK_OVERRIDE;
95     virtual void drawPath(const SkDraw&, const SkPath& path,
96                           const SkPaint& paint, const SkMatrix* prePathMatrix,
97                           bool pathIsMutable) SK_OVERRIDE;
98     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
99                             const SkMatrix&, const SkPaint&) SK_OVERRIDE;
100     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
101                                 const SkRect* srcOrNull, const SkRect& dst,
102                                 const SkPaint& paint,
103                                 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
104     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
105                             int x, int y, const SkPaint& paint);
106     virtual void drawText(const SkDraw&, const void* text, size_t len,
107                           SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
108     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
109                              const SkScalar pos[], SkScalar constY,
110                              int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
111     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
112                                 const SkPath& path, const SkMatrix* matrix,
113                                 const SkPaint&) SK_OVERRIDE;
114     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
115                               const SkPoint verts[], const SkPoint texs[],
116                               const SkColor colors[], SkXfermode* xmode,
117                               const uint16_t indices[], int indexCount,
118                               const SkPaint&) SK_OVERRIDE;
119     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
120                             const SkPaint&) SK_OVERRIDE;
121     virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
122
123     virtual void flush() SK_OVERRIDE;
124
125     virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
126     virtual void onDetachFromCanvas() SK_OVERRIDE;
127
128     /**
129      * Make's this device's rendertarget current in the underlying 3D API.
130      * Also implicitly flushes.
131      */
132     virtual void makeRenderTargetCurrent();
133
134     virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE;
135     virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkMatrix&,
136                              SkBitmap*, SkIPoint*) SK_OVERRIDE;
137
138     class SkAutoCachedTexture; // used internally
139
140 protected:
141     // overrides from SkBaseDevice
142     virtual bool onReadPixels(const SkBitmap& bitmap,
143                               int x, int y,
144                               SkCanvas::Config8888 config8888) SK_OVERRIDE;
145
146 private:
147     GrContext*      fContext;
148
149     GrSkDrawProcs*  fDrawProcs;
150
151     GrClipData      fClipData;
152
153     GrTextContext*  fMainTextContext;
154     GrTextContext*  fFallbackTextContext;
155
156     // state for our render-target
157     GrRenderTarget*     fRenderTarget;
158     bool                fNeedClear;
159
160     // called from rt and tex cons
161     void initFromRenderTarget(GrContext*, GrRenderTarget*, bool cached);
162
163     // used by createCompatibleDevice
164     SkGpuDevice(GrContext*, GrTexture* texture, bool needClear);
165
166     // override from SkBaseDevice
167     virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
168                                                    int width, int height,
169                                                    bool isOpaque,
170                                                    Usage usage) SK_OVERRIDE;
171
172     virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
173
174     // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
175     // SkDraw's matrix and draw in device coords.
176     void prepareDraw(const SkDraw&, bool forceIdentity);
177
178     /**
179      * Implementation for both drawBitmap and drawBitmapRect.
180      */
181     void drawBitmapCommon(const SkDraw&,
182                           const SkBitmap& bitmap,
183                           const SkRect* srcRectPtr,
184                           const SkSize* dstSizePtr,      // ignored iff srcRectPtr == NULL
185                           const SkPaint&,
186                           SkCanvas::DrawBitmapRectFlags flags);
187
188     /**
189      * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
190      * matrix, clip, and the device's render target has already been set on GrContext.
191      */
192
193     // The tileSize and clippedSrcRect will be valid only if true is returned.
194     bool shouldTileBitmap(const SkBitmap& bitmap,
195                           const GrTextureParams& sampler,
196                           const SkRect* srcRectPtr,
197                           int maxTileSize,
198                           int* tileSize,
199                           SkIRect* clippedSrcRect) const;
200     void internalDrawBitmap(const SkBitmap&,
201                             const SkRect&,
202                             const GrTextureParams& params,
203                             const SkPaint& paint,
204                             SkCanvas::DrawBitmapRectFlags flags,
205                             bool bicubic);
206     void drawTiledBitmap(const SkBitmap& bitmap,
207                          const SkRect& srcRect,
208                          const SkIRect& clippedSrcRect,
209                          const GrTextureParams& params,
210                          const SkPaint& paint,
211                          SkCanvas::DrawBitmapRectFlags flags,
212                          int tileSize,
213                          bool bicubic);
214
215     typedef SkBitmapDevice INHERITED;
216 };
217
218 #endif