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