blob: 4d0efeb511598c211e231572bc79d2b33def28a4 [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 *
40 * isForSaveLayer is a special flag that should only be set by SkCanvas
41 * internally.
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000042 */
reed@google.comaf951c92011-06-16 19:10:39 +000043 SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height,
44 bool isForSaveLayer = false);
45
46 /**
47 * New device that will render to the specified renderTarget.
48 */
49 SkGpuDevice(GrContext*, GrRenderTarget*);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000050
51 /**
52 * Magic value that can be passed to constructor. Causes
53 * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D).
54 * This isn't a valid pointer, don't attempt to dereference.
55 */
56 static GrRenderTarget* Current3DApiRenderTarget();
57
reed@google.comac10a2d2010-12-22 21:39:39 +000058 virtual ~SkGpuDevice();
reed@google.com7b201d22011-01-11 18:59:23 +000059
reed@google.comac10a2d2010-12-22 21:39:39 +000060 GrContext* context() const { return fContext; }
61
62 /**
63 * If this device was built for rendering as a layer (i.e. offscreen),
64 * then this will return the platform-specific handle to that GPU resource.
65 * For example, in OpenGL, this will return the FBO's texture ID.
66 * If this device was not built for rendering as a layer, then 0
67 * is returned.
68 */
69 intptr_t getLayerTextureHandle() const;
reed@google.com7b201d22011-01-11 18:59:23 +000070
reed@google.comac10a2d2010-12-22 21:39:39 +000071 // call to set the clip to the specified rect
72 void scissor(const SkIRect&);
73
74 /**
75 * Override from SkGpuDevice, so we can set our FBO to be the render target
76 * The canvas parameter must be a SkGpuCanvas
77 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000078 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
79 const SkClipStack& clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +000080
81 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
82
83 // overrides from SkDevice
84
bsalomon@google.com398109c2011-04-14 18:40:27 +000085 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000086 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
87 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000088
reed@google.com46799cd2011-02-22 20:56:26 +000089 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
90 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000091
92 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
93 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
94 const SkPoint[], const SkPaint& paint);
95 virtual void drawRect(const SkDraw&, const SkRect& r,
96 const SkPaint& paint);
97 virtual void drawPath(const SkDraw&, const SkPath& path,
98 const SkPaint& paint, const SkMatrix* prePathMatrix,
99 bool pathIsMutable);
100 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
101 const SkIRect* srcRectOrNull,
102 const SkMatrix& matrix, const SkPaint& paint);
103 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
104 int x, int y, const SkPaint& paint);
105 virtual void drawText(const SkDraw&, const void* text, size_t len,
106 SkScalar x, SkScalar y, const SkPaint& paint);
107 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
108 const SkScalar pos[], SkScalar constY,
109 int scalarsPerPos, const SkPaint& paint);
110 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
111 const SkPath& path, const SkMatrix* matrix,
112 const SkPaint& paint);
113 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
114 const SkPoint verts[], const SkPoint texs[],
115 const SkColor colors[], SkXfermode* xmode,
116 const uint16_t indices[], int indexCount,
117 const SkPaint& paint);
118 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
119 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000120 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000123
124 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 * Make's this device's rendertarget current in the underlying 3D API.
126 * Also implicitly flushes.
127 */
128 virtual void makeRenderTargetCurrent();
129
130protected:
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000131 // override
132 virtual SkDeviceFactory* onNewDeviceFactory();
133
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 class TexCache;
135 TexCache* lockCachedTexture(const SkBitmap& bitmap,
136 const GrSamplerState& sampler,
137 GrTexture** texture,
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000138 bool forDeviceRenderTarget = false,
139 bool isSaveLayer = false);
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.com5782d712011-01-21 21:03:59 +0000171 // doesn't set the texture/sampler/matrix state
172 // caller needs to null out GrPaint's texture if
173 // non-textured drawing is desired.
Scroggod757df22011-05-16 13:11:16 +0000174 // Set constantColor to true if a constant color
175 // will be used. This is an optimization, and can
176 // always be set to false. constantColor should
177 // never be true if justAlpha is true.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000178 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
179 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000180 GrPaint* grPaint,
181 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000182
183 // uses the SkShader to setup paint, act used to
184 // hold lock on cached texture and free it when
185 // destroyed.
Scroggod757df22011-05-16 13:11:16 +0000186 // If there is no shader, constantColor will
187 // be passed to skPaint2GrPaintNoShader. Otherwise
188 // it is ignored.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000189 bool skPaint2GrPaintShader(const SkPaint& skPaint,
190 SkAutoCachedTexture* act,
191 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000192 GrPaint* grPaint,
193 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000194
195 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000196 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000197
198 void prepareRenderTarget(const SkDraw&);
199 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000200 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000201
202 typedef SkDevice INHERITED;
203};
204
205#endif
206