blob: bddf24ec3aa5391fa1ef3e2e7940276c61c407c5 [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 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000071 * Override from SkGpuDevice, so we can set our FBO to be the render target
72 * The canvas parameter must be a SkGpuCanvas
73 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000074 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
75 const SkClipStack& clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +000076
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000077 virtual SkGpuRenderTarget* accessRenderTarget() {
78 return (SkGpuRenderTarget*)fRenderTarget;
79 }
reed@google.comac10a2d2010-12-22 21:39:39 +000080
81 // overrides from SkDevice
82
bsalomon@google.com398109c2011-04-14 18:40:27 +000083 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000084 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
85 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000086
reed@google.com46799cd2011-02-22 20:56:26 +000087 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
88 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000089
90 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
91 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
92 const SkPoint[], const SkPaint& paint);
93 virtual void drawRect(const SkDraw&, const SkRect& r,
94 const SkPaint& paint);
95 virtual void drawPath(const SkDraw&, const SkPath& path,
96 const SkPaint& paint, const SkMatrix* prePathMatrix,
97 bool pathIsMutable);
98 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
99 const SkIRect* srcRectOrNull,
100 const SkMatrix& matrix, const SkPaint& paint);
101 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
102 int x, int y, const SkPaint& paint);
103 virtual void drawText(const SkDraw&, const void* text, size_t len,
104 SkScalar x, SkScalar y, const SkPaint& paint);
105 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
106 const SkScalar pos[], SkScalar constY,
107 int scalarsPerPos, const SkPaint& paint);
108 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
109 const SkPath& path, const SkMatrix* matrix,
110 const SkPaint& paint);
111 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
112 const SkPoint verts[], const SkPoint texs[],
113 const SkColor colors[], SkXfermode* xmode,
114 const uint16_t indices[], int indexCount,
115 const SkPaint& paint);
116 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
117 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000118 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000119
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000121
122 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 * Make's this device's rendertarget current in the underlying 3D API.
124 * Also implicitly flushes.
125 */
126 virtual void makeRenderTargetCurrent();
127
128protected:
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000129
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 class TexCache;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000131 enum TexType {
132 kBitmap_TexType,
133 kDeviceRenderTarget_TexType,
134 kSaveLayerDeviceRenderTarget_TexType
135 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 TexCache* lockCachedTexture(const SkBitmap& bitmap,
137 const GrSamplerState& sampler,
138 GrTexture** texture,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000139 TexType type = kBitmap_TexType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 void unlockCachedTexture(TexCache*);
141
142 class SkAutoCachedTexture {
143 public:
144 SkAutoCachedTexture();
145 SkAutoCachedTexture(SkGpuDevice* device,
146 const SkBitmap& bitmap,
147 const GrSamplerState& sampler,
148 GrTexture** texture);
149 ~SkAutoCachedTexture();
150
151 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
152
153 private:
154 SkGpuDevice* fDevice;
155 TexCache* fTex;
156 };
157 friend class SkAutoTexCache;
158
159private:
160 GrContext* fContext;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000161
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 GrSkDrawProcs* fDrawProcs;
163
164 // state for our offscreen render-target
165 TexCache* fCache;
166 GrTexture* fTexture;
167 GrRenderTarget* fRenderTarget;
168 bool fNeedClear;
169 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000170
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000171 // called from rt and tex cons
172 void initFromRenderTarget(GrContext*, GrRenderTarget*);
173
bsalomon@google.com5782d712011-01-21 21:03:59 +0000174 // doesn't set the texture/sampler/matrix state
175 // caller needs to null out GrPaint's texture if
176 // non-textured drawing is desired.
Scroggod757df22011-05-16 13:11:16 +0000177 // Set constantColor to true if a constant color
178 // will be used. This is an optimization, and can
179 // always be set to false. constantColor should
180 // never be true if justAlpha is true.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000181 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
182 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000183 GrPaint* grPaint,
184 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000185
186 // uses the SkShader to setup paint, act used to
187 // hold lock on cached texture and free it when
188 // destroyed.
Scroggod757df22011-05-16 13:11:16 +0000189 // If there is no shader, constantColor will
190 // be passed to skPaint2GrPaintNoShader. Otherwise
191 // it is ignored.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000192 bool skPaint2GrPaintShader(const SkPaint& skPaint,
193 SkAutoCachedTexture* act,
194 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000195 GrPaint* grPaint,
196 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000197
bsalomon@google.come97f0852011-06-17 13:10:25 +0000198 // override from SkDevice
199 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
200 int width, int height,
201 bool isOpaque,
202 Usage usage);
203
bsalomon@google.com5782d712011-01-21 21:03:59 +0000204 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000205 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
207 void prepareRenderTarget(const SkDraw&);
208 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000209 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000210
211 typedef SkDevice INHERITED;
212};
213
214#endif
215