blob: 2bea0bf28cde29e465da6fbba78ee91acb68f633 [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
77 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
78
79 // overrides from SkDevice
80
bsalomon@google.com398109c2011-04-14 18:40:27 +000081 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000082 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
83 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000084
reed@google.com46799cd2011-02-22 20:56:26 +000085 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
86 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000087
88 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
89 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
90 const SkPoint[], const SkPaint& paint);
91 virtual void drawRect(const SkDraw&, const SkRect& r,
92 const SkPaint& paint);
93 virtual void drawPath(const SkDraw&, const SkPath& path,
94 const SkPaint& paint, const SkMatrix* prePathMatrix,
95 bool pathIsMutable);
96 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
97 const SkIRect* srcRectOrNull,
98 const SkMatrix& matrix, const SkPaint& paint);
99 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
100 int x, int y, const SkPaint& paint);
101 virtual void drawText(const SkDraw&, const void* text, size_t len,
102 SkScalar x, SkScalar y, const SkPaint& paint);
103 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
104 const SkScalar pos[], SkScalar constY,
105 int scalarsPerPos, const SkPaint& paint);
106 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
107 const SkPath& path, const SkMatrix* matrix,
108 const SkPaint& paint);
109 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
110 const SkPoint verts[], const SkPoint texs[],
111 const SkColor colors[], SkXfermode* xmode,
112 const uint16_t indices[], int indexCount,
113 const SkPaint& paint);
114 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
115 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000116 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000117
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000119
120 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 * Make's this device's rendertarget current in the underlying 3D API.
122 * Also implicitly flushes.
123 */
124 virtual void makeRenderTargetCurrent();
125
126protected:
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 class TexCache;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000129 enum TexType {
130 kBitmap_TexType,
131 kDeviceRenderTarget_TexType,
132 kSaveLayerDeviceRenderTarget_TexType
133 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 TexCache* lockCachedTexture(const SkBitmap& bitmap,
135 const GrSamplerState& sampler,
136 GrTexture** texture,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000137 TexType type = kBitmap_TexType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000138 void unlockCachedTexture(TexCache*);
139
140 class SkAutoCachedTexture {
141 public:
142 SkAutoCachedTexture();
143 SkAutoCachedTexture(SkGpuDevice* device,
144 const SkBitmap& bitmap,
145 const GrSamplerState& sampler,
146 GrTexture** texture);
147 ~SkAutoCachedTexture();
148
149 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
150
151 private:
152 SkGpuDevice* fDevice;
153 TexCache* fTex;
154 };
155 friend class SkAutoTexCache;
156
157private:
158 GrContext* fContext;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000159
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 GrSkDrawProcs* fDrawProcs;
161
162 // state for our offscreen render-target
163 TexCache* fCache;
164 GrTexture* fTexture;
165 GrRenderTarget* fRenderTarget;
166 bool fNeedClear;
167 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000168
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169 // called from rt and tex cons
170 void initFromRenderTarget(GrContext*, GrRenderTarget*);
171
bsalomon@google.com5782d712011-01-21 21:03:59 +0000172 // doesn't set the texture/sampler/matrix state
173 // caller needs to null out GrPaint's texture if
174 // non-textured drawing is desired.
Scroggod757df22011-05-16 13:11:16 +0000175 // Set constantColor to true if a constant color
176 // will be used. This is an optimization, and can
177 // always be set to false. constantColor should
178 // never be true if justAlpha is true.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000179 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
180 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000181 GrPaint* grPaint,
182 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000183
184 // uses the SkShader to setup paint, act used to
185 // hold lock on cached texture and free it when
186 // destroyed.
Scroggod757df22011-05-16 13:11:16 +0000187 // If there is no shader, constantColor will
188 // be passed to skPaint2GrPaintNoShader. Otherwise
189 // it is ignored.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000190 bool skPaint2GrPaintShader(const SkPaint& skPaint,
191 SkAutoCachedTexture* act,
192 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000193 GrPaint* grPaint,
194 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000195
bsalomon@google.come97f0852011-06-17 13:10:25 +0000196 // override from SkDevice
197 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
198 int width, int height,
199 bool isOpaque,
200 Usage usage);
201
bsalomon@google.com5782d712011-01-21 21:03:59 +0000202 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000203 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
205 void prepareRenderTarget(const SkDraw&);
206 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000207 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000208
209 typedef SkDevice INHERITED;
210};
211
212#endif
213