blob: 3a675be24a9f2d79826acc220754823ebc0d9608 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef SkGpuDevice_DEFINED
19#define SkGpuDevice_DEFINED
20
21#include "SkGr.h"
reed@google.comaf951c92011-06-16 19:10:39 +000022#include "SkBitmap.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023#include "SkDevice.h"
24#include "SkRegion.h"
25
26struct SkDrawProcs;
reed@google.comac10a2d2010-12-22 21:39:39 +000027struct GrSkDrawProcs;
28class GrTextContext;
29
30/**
31 * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the
32 * canvas.
33 */
bsalomon@google.com91826102011-03-21 19:51:57 +000034class SK_API SkGpuDevice : public SkDevice {
reed@google.comac10a2d2010-12-22 21:39:39 +000035public:
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000036 /**
reed@google.comaf951c92011-06-16 19:10:39 +000037 * New device that will create an offscreen renderTarget based on the
38 * config, width, height.
39 *
bsalomon@google.come97f0852011-06-17 13:10:25 +000040 * usage is a special flag that should only be set by SkCanvas
reed@google.comaf951c92011-06-16 19:10:39 +000041 * internally.
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000042 */
bsalomon@google.come97f0852011-06-17 13:10:25 +000043 SkGpuDevice(GrContext*, SkBitmap::Config,
44 int width, int height,
45 SkDevice::Usage usage = SkDevice::kGeneral_Usage);
reed@google.comaf951c92011-06-16 19:10:39 +000046
47 /**
48 * New device that will render to the specified renderTarget.
49 */
50 SkGpuDevice(GrContext*, GrRenderTarget*);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000051
52 /**
53 * Magic value that can be passed to constructor. Causes
54 * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D).
55 * This isn't a valid pointer, don't attempt to dereference.
56 */
57 static GrRenderTarget* Current3DApiRenderTarget();
58
reed@google.comac10a2d2010-12-22 21:39:39 +000059 virtual ~SkGpuDevice();
reed@google.com7b201d22011-01-11 18:59:23 +000060
reed@google.comac10a2d2010-12-22 21:39:39 +000061 GrContext* context() const { return fContext; }
62
63 /**
64 * If this device was built for rendering as a layer (i.e. offscreen),
65 * then this will return the platform-specific handle to that GPU resource.
66 * For example, in OpenGL, this will return the FBO's texture ID.
67 * If this device was not built for rendering as a layer, then 0
68 * is returned.
69 */
70 intptr_t getLayerTextureHandle() const;
reed@google.com7b201d22011-01-11 18:59:23 +000071
reed@google.comac10a2d2010-12-22 21:39:39 +000072 // call to set the clip to the specified rect
73 void scissor(const SkIRect&);
74
75 /**
76 * Override from SkGpuDevice, so we can set our FBO to be the render target
77 * The canvas parameter must be a SkGpuCanvas
78 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000079 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
80 const SkClipStack& clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +000081
82 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
83
84 // overrides from SkDevice
85
bsalomon@google.com398109c2011-04-14 18:40:27 +000086 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000087 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
88 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000089
reed@google.com46799cd2011-02-22 20:56:26 +000090 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
91 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000092
93 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
94 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
95 const SkPoint[], const SkPaint& paint);
96 virtual void drawRect(const SkDraw&, const SkRect& r,
97 const SkPaint& paint);
98 virtual void drawPath(const SkDraw&, const SkPath& path,
99 const SkPaint& paint, const SkMatrix* prePathMatrix,
100 bool pathIsMutable);
101 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
102 const SkIRect* srcRectOrNull,
103 const SkMatrix& matrix, const SkPaint& paint);
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& paint);
108 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
109 const SkScalar pos[], SkScalar constY,
110 int scalarsPerPos, const SkPaint& paint);
111 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
112 const SkPath& path, const SkMatrix* matrix,
113 const SkPaint& paint);
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& paint);
119 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
120 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000121 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000122
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000124
125 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 * Make's this device's rendertarget current in the underlying 3D API.
127 * Also implicitly flushes.
128 */
129 virtual void makeRenderTargetCurrent();
130
131protected:
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000132 // override
133 virtual SkDeviceFactory* onNewDeviceFactory();
134
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 class TexCache;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000136 enum TexType {
137 kBitmap_TexType,
138 kDeviceRenderTarget_TexType,
139 kSaveLayerDeviceRenderTarget_TexType
140 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 TexCache* lockCachedTexture(const SkBitmap& bitmap,
142 const GrSamplerState& sampler,
143 GrTexture** texture,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000144 TexType type = kBitmap_TexType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 void unlockCachedTexture(TexCache*);
146
147 class SkAutoCachedTexture {
148 public:
149 SkAutoCachedTexture();
150 SkAutoCachedTexture(SkGpuDevice* device,
151 const SkBitmap& bitmap,
152 const GrSamplerState& sampler,
153 GrTexture** texture);
154 ~SkAutoCachedTexture();
155
156 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
157
158 private:
159 SkGpuDevice* fDevice;
160 TexCache* fTex;
161 };
162 friend class SkAutoTexCache;
163
164private:
165 GrContext* fContext;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000166
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 GrSkDrawProcs* fDrawProcs;
168
169 // state for our offscreen render-target
170 TexCache* fCache;
171 GrTexture* fTexture;
172 GrRenderTarget* fRenderTarget;
173 bool fNeedClear;
174 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000175
bsalomon@google.com5782d712011-01-21 21:03:59 +0000176 // doesn't set the texture/sampler/matrix state
177 // caller needs to null out GrPaint's texture if
178 // non-textured drawing is desired.
Scroggod757df22011-05-16 13:11:16 +0000179 // Set constantColor to true if a constant color
180 // will be used. This is an optimization, and can
181 // always be set to false. constantColor should
182 // never be true if justAlpha is true.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000183 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
184 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000185 GrPaint* grPaint,
186 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000187
188 // uses the SkShader to setup paint, act used to
189 // hold lock on cached texture and free it when
190 // destroyed.
Scroggod757df22011-05-16 13:11:16 +0000191 // If there is no shader, constantColor will
192 // be passed to skPaint2GrPaintNoShader. Otherwise
193 // it is ignored.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000194 bool skPaint2GrPaintShader(const SkPaint& skPaint,
195 SkAutoCachedTexture* act,
196 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000197 GrPaint* grPaint,
198 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000199
bsalomon@google.come97f0852011-06-17 13:10:25 +0000200 // override from SkDevice
201 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
202 int width, int height,
203 bool isOpaque,
204 Usage usage);
205
bsalomon@google.com5782d712011-01-21 21:03:59 +0000206 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000207 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000208
209 void prepareRenderTarget(const SkDraw&);
210 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000211 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000212
213 typedef SkDevice INHERITED;
214};
215
216#endif
217