blob: 96afa082ff47bf8736b727c1456efee1face3a96 [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"
22#include "SkDevice.h"
23#include "SkRegion.h"
24
25struct SkDrawProcs;
reed@google.comac10a2d2010-12-22 21:39:39 +000026struct GrSkDrawProcs;
27class GrTextContext;
28
29/**
30 * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the
31 * canvas.
32 */
bsalomon@google.com91826102011-03-21 19:51:57 +000033class SK_API SkGpuDevice : public SkDevice {
reed@google.comac10a2d2010-12-22 21:39:39 +000034public:
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000035 /**
36 * The SkGpuDevice will render to the GrRenderTarget, or if the paremeter is
37 * null it will create its own render target and manage that target's
bsalomon@google.comb5b31682011-06-16 18:05:35 +000038 * lifetime. Setting isSaveLayer to true is for internal use and may cause
39 * problems when using the device's bitmap as a src if used externally.
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000040 */
41 SkGpuDevice(GrContext*,
42 const SkBitmap& bitmap,
bsalomon@google.comb5b31682011-06-16 18:05:35 +000043 GrRenderTarget* renderTargetOrNull,
44 bool isSaveLayer = false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000045
46 /**
47 * Magic value that can be passed to constructor. Causes
48 * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D).
49 * This isn't a valid pointer, don't attempt to dereference.
50 */
51 static GrRenderTarget* Current3DApiRenderTarget();
52
reed@google.comac10a2d2010-12-22 21:39:39 +000053 virtual ~SkGpuDevice();
reed@google.com7b201d22011-01-11 18:59:23 +000054
reed@google.comac10a2d2010-12-22 21:39:39 +000055 GrContext* context() const { return fContext; }
56
57 /**
58 * If this device was built for rendering as a layer (i.e. offscreen),
59 * then this will return the platform-specific handle to that GPU resource.
60 * For example, in OpenGL, this will return the FBO's texture ID.
61 * If this device was not built for rendering as a layer, then 0
62 * is returned.
63 */
64 intptr_t getLayerTextureHandle() const;
reed@google.com7b201d22011-01-11 18:59:23 +000065
reed@google.comac10a2d2010-12-22 21:39:39 +000066 // call to set the clip to the specified rect
67 void scissor(const SkIRect&);
68
69 /**
70 * Override from SkGpuDevice, so we can set our FBO to be the render target
71 * The canvas parameter must be a SkGpuCanvas
72 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000073 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
74 const SkClipStack& clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +000075
76 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
77
78 // overrides from SkDevice
79
bsalomon@google.com398109c2011-04-14 18:40:27 +000080 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000081 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
82 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000083
reed@google.com46799cd2011-02-22 20:56:26 +000084 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
85 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000086
87 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
88 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
89 const SkPoint[], const SkPaint& paint);
90 virtual void drawRect(const SkDraw&, const SkRect& r,
91 const SkPaint& paint);
92 virtual void drawPath(const SkDraw&, const SkPath& path,
93 const SkPaint& paint, const SkMatrix* prePathMatrix,
94 bool pathIsMutable);
95 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
96 const SkIRect* srcRectOrNull,
97 const SkMatrix& matrix, const SkPaint& paint);
98 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
99 int x, int y, const SkPaint& paint);
100 virtual void drawText(const SkDraw&, const void* text, size_t len,
101 SkScalar x, SkScalar y, const SkPaint& paint);
102 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
103 const SkScalar pos[], SkScalar constY,
104 int scalarsPerPos, const SkPaint& paint);
105 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
106 const SkPath& path, const SkMatrix* matrix,
107 const SkPaint& paint);
108 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
109 const SkPoint verts[], const SkPoint texs[],
110 const SkColor colors[], SkXfermode* xmode,
111 const uint16_t indices[], int indexCount,
112 const SkPaint& paint);
113 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
114 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000115 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000118
119 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 * Make's this device's rendertarget current in the underlying 3D API.
121 * Also implicitly flushes.
122 */
123 virtual void makeRenderTargetCurrent();
124
125protected:
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000126 // override
127 virtual SkDeviceFactory* onNewDeviceFactory();
128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 class TexCache;
130 TexCache* lockCachedTexture(const SkBitmap& bitmap,
131 const GrSamplerState& sampler,
132 GrTexture** texture,
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000133 bool forDeviceRenderTarget = false,
134 bool isSaveLayer = false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 void unlockCachedTexture(TexCache*);
136
137 class SkAutoCachedTexture {
138 public:
139 SkAutoCachedTexture();
140 SkAutoCachedTexture(SkGpuDevice* device,
141 const SkBitmap& bitmap,
142 const GrSamplerState& sampler,
143 GrTexture** texture);
144 ~SkAutoCachedTexture();
145
146 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
147
148 private:
149 SkGpuDevice* fDevice;
150 TexCache* fTex;
151 };
152 friend class SkAutoTexCache;
153
154private:
155 GrContext* fContext;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000156
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 GrSkDrawProcs* fDrawProcs;
158
159 // state for our offscreen render-target
160 TexCache* fCache;
161 GrTexture* fTexture;
162 GrRenderTarget* fRenderTarget;
163 bool fNeedClear;
164 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000165
bsalomon@google.com5782d712011-01-21 21:03:59 +0000166 // doesn't set the texture/sampler/matrix state
167 // caller needs to null out GrPaint's texture if
168 // non-textured drawing is desired.
Scroggod757df22011-05-16 13:11:16 +0000169 // Set constantColor to true if a constant color
170 // will be used. This is an optimization, and can
171 // always be set to false. constantColor should
172 // never be true if justAlpha is true.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000173 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
174 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000175 GrPaint* grPaint,
176 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000177
178 // uses the SkShader to setup paint, act used to
179 // hold lock on cached texture and free it when
180 // destroyed.
Scroggod757df22011-05-16 13:11:16 +0000181 // If there is no shader, constantColor will
182 // be passed to skPaint2GrPaintNoShader. Otherwise
183 // it is ignored.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000184 bool skPaint2GrPaintShader(const SkPaint& skPaint,
185 SkAutoCachedTexture* act,
186 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000187 GrPaint* grPaint,
188 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000189
190 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000191 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
193 void prepareRenderTarget(const SkDraw&);
194 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000195 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000196
197 typedef SkDevice INHERITED;
198};
199
200#endif
201