blob: afb14594bb85361053819e1dd2173c9deabcc452 [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 /**
bsalomon@google.comf9046fe2011-06-17 15:10:21 +000053 * New device that will render to the texture (as a rendertarget).
54 * The GrTexture's asRenderTarget() must be non-NULL or device will not
55 * function.
56 */
57 SkGpuDevice(GrContext*, GrTexture*);
58
59 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000060 * Magic value that can be passed to constructor. Causes
61 * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D).
62 * This isn't a valid pointer, don't attempt to dereference.
63 */
64 static GrRenderTarget* Current3DApiRenderTarget();
65
reed@google.comac10a2d2010-12-22 21:39:39 +000066 virtual ~SkGpuDevice();
reed@google.com7b201d22011-01-11 18:59:23 +000067
reed@google.comac10a2d2010-12-22 21:39:39 +000068 GrContext* context() const { return fContext; }
69
70 /**
71 * If this device was built for rendering as a layer (i.e. offscreen),
72 * then this will return the platform-specific handle to that GPU resource.
73 * For example, in OpenGL, this will return the FBO's texture ID.
74 * If this device was not built for rendering as a layer, then 0
75 * is returned.
76 */
77 intptr_t getLayerTextureHandle() const;
reed@google.com7b201d22011-01-11 18:59:23 +000078
reed@google.comac10a2d2010-12-22 21:39:39 +000079 // call to set the clip to the specified rect
80 void scissor(const SkIRect&);
81
82 /**
83 * Override from SkGpuDevice, so we can set our FBO to be the render target
84 * The canvas parameter must be a SkGpuCanvas
85 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000086 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
87 const SkClipStack& clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +000088
89 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
90
91 // overrides from SkDevice
92
bsalomon@google.com398109c2011-04-14 18:40:27 +000093 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000094 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
95 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000096
reed@google.com46799cd2011-02-22 20:56:26 +000097 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
98 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000099
100 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
101 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
102 const SkPoint[], const SkPaint& paint);
103 virtual void drawRect(const SkDraw&, const SkRect& r,
104 const SkPaint& paint);
105 virtual void drawPath(const SkDraw&, const SkPath& path,
106 const SkPaint& paint, const SkMatrix* prePathMatrix,
107 bool pathIsMutable);
108 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
109 const SkIRect* srcRectOrNull,
110 const SkMatrix& matrix, const SkPaint& paint);
111 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
112 int x, int y, const SkPaint& paint);
113 virtual void drawText(const SkDraw&, const void* text, size_t len,
114 SkScalar x, SkScalar y, const SkPaint& paint);
115 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
116 const SkScalar pos[], SkScalar constY,
117 int scalarsPerPos, const SkPaint& paint);
118 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
119 const SkPath& path, const SkMatrix* matrix,
120 const SkPaint& paint);
121 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
122 const SkPoint verts[], const SkPoint texs[],
123 const SkColor colors[], SkXfermode* xmode,
124 const uint16_t indices[], int indexCount,
125 const SkPaint& paint);
126 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
127 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000128 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000129
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000131
132 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 * Make's this device's rendertarget current in the underlying 3D API.
134 * Also implicitly flushes.
135 */
136 virtual void makeRenderTargetCurrent();
137
138protected:
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000139
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 class TexCache;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000141 enum TexType {
142 kBitmap_TexType,
143 kDeviceRenderTarget_TexType,
144 kSaveLayerDeviceRenderTarget_TexType
145 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 TexCache* lockCachedTexture(const SkBitmap& bitmap,
147 const GrSamplerState& sampler,
148 GrTexture** texture,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000149 TexType type = kBitmap_TexType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 void unlockCachedTexture(TexCache*);
151
152 class SkAutoCachedTexture {
153 public:
154 SkAutoCachedTexture();
155 SkAutoCachedTexture(SkGpuDevice* device,
156 const SkBitmap& bitmap,
157 const GrSamplerState& sampler,
158 GrTexture** texture);
159 ~SkAutoCachedTexture();
160
161 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
162
163 private:
164 SkGpuDevice* fDevice;
165 TexCache* fTex;
166 };
167 friend class SkAutoTexCache;
168
169private:
170 GrContext* fContext;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000171
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 GrSkDrawProcs* fDrawProcs;
173
174 // state for our offscreen render-target
175 TexCache* fCache;
176 GrTexture* fTexture;
177 GrRenderTarget* fRenderTarget;
178 bool fNeedClear;
179 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000180
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000181 // called from rt and tex cons
182 void initFromRenderTarget(GrContext*, GrRenderTarget*);
183
bsalomon@google.com5782d712011-01-21 21:03:59 +0000184 // doesn't set the texture/sampler/matrix state
185 // caller needs to null out GrPaint's texture if
186 // non-textured drawing is desired.
Scroggod757df22011-05-16 13:11:16 +0000187 // Set constantColor to true if a constant color
188 // will be used. This is an optimization, and can
189 // always be set to false. constantColor should
190 // never be true if justAlpha is true.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000191 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
192 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000193 GrPaint* grPaint,
194 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000195
196 // uses the SkShader to setup paint, act used to
197 // hold lock on cached texture and free it when
198 // destroyed.
Scroggod757df22011-05-16 13:11:16 +0000199 // If there is no shader, constantColor will
200 // be passed to skPaint2GrPaintNoShader. Otherwise
201 // it is ignored.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000202 bool skPaint2GrPaintShader(const SkPaint& skPaint,
203 SkAutoCachedTexture* act,
204 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000205 GrPaint* grPaint,
206 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000207
bsalomon@google.come97f0852011-06-17 13:10:25 +0000208 // override from SkDevice
209 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
210 int width, int height,
211 bool isOpaque,
212 Usage usage);
213
bsalomon@google.com5782d712011-01-21 21:03:59 +0000214 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000215 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000216
217 void prepareRenderTarget(const SkDraw&);
218 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
221 typedef SkDevice INHERITED;
222};
223
224#endif
225