blob: a46314e355a194c7c5a23cc1a51fcc6acf693a00 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef SkGpuDevice_DEFINED
12#define SkGpuDevice_DEFINED
13
14#include "SkGr.h"
reed@google.comaf951c92011-06-16 19:10:39 +000015#include "SkBitmap.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "SkDevice.h"
17#include "SkRegion.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000018#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
20struct SkDrawProcs;
reed@google.comac10a2d2010-12-22 21:39:39 +000021struct GrSkDrawProcs;
22class GrTextContext;
23
24/**
25 * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the
26 * canvas.
27 */
bsalomon@google.com91826102011-03-21 19:51:57 +000028class SK_API SkGpuDevice : public SkDevice {
reed@google.comac10a2d2010-12-22 21:39:39 +000029public:
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000030 /**
reed@google.comaf951c92011-06-16 19:10:39 +000031 * New device that will create an offscreen renderTarget based on the
32 * config, width, height.
33 *
bsalomon@google.come97f0852011-06-17 13:10:25 +000034 * usage is a special flag that should only be set by SkCanvas
reed@google.comaf951c92011-06-16 19:10:39 +000035 * internally.
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000036 */
bsalomon@google.come97f0852011-06-17 13:10:25 +000037 SkGpuDevice(GrContext*, SkBitmap::Config,
38 int width, int height,
39 SkDevice::Usage usage = SkDevice::kGeneral_Usage);
reed@google.comaf951c92011-06-16 19:10:39 +000040
41 /**
42 * New device that will render to the specified renderTarget.
43 */
44 SkGpuDevice(GrContext*, GrRenderTarget*);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000045
46 /**
bsalomon@google.comf9046fe2011-06-17 15:10:21 +000047 * New device that will render to the texture (as a rendertarget).
48 * The GrTexture's asRenderTarget() must be non-NULL or device will not
49 * function.
50 */
51 SkGpuDevice(GrContext*, GrTexture*);
52
53 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000054 * Magic value that can be passed to constructor. Causes
55 * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D).
56 * This isn't a valid pointer, don't attempt to dereference.
57 */
58 static GrRenderTarget* Current3DApiRenderTarget();
59
reed@google.comac10a2d2010-12-22 21:39:39 +000060 virtual ~SkGpuDevice();
reed@google.com7b201d22011-01-11 18:59:23 +000061
reed@google.comac10a2d2010-12-22 21:39:39 +000062 GrContext* context() const { return fContext; }
63
64 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000065 * Override from SkGpuDevice, so we can set our FBO to be the render target
66 * The canvas parameter must be a SkGpuCanvas
67 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000068 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
69 const SkClipStack& clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +000070
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000071 virtual SkGpuRenderTarget* accessRenderTarget() {
72 return (SkGpuRenderTarget*)fRenderTarget;
73 }
reed@google.comac10a2d2010-12-22 21:39:39 +000074
75 // overrides from SkDevice
76
bsalomon@google.com398109c2011-04-14 18:40:27 +000077 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000078 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
79 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000080
reed@google.com46799cd2011-02-22 20:56:26 +000081 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
82 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000083
84 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
85 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
86 const SkPoint[], const SkPaint& paint);
87 virtual void drawRect(const SkDraw&, const SkRect& r,
88 const SkPaint& paint);
89 virtual void drawPath(const SkDraw&, const SkPath& path,
90 const SkPaint& paint, const SkMatrix* prePathMatrix,
91 bool pathIsMutable);
92 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
93 const SkIRect* srcRectOrNull,
94 const SkMatrix& matrix, const SkPaint& paint);
95 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
96 int x, int y, const SkPaint& paint);
97 virtual void drawText(const SkDraw&, const void* text, size_t len,
98 SkScalar x, SkScalar y, const SkPaint& paint);
99 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
100 const SkScalar pos[], SkScalar constY,
101 int scalarsPerPos, const SkPaint& paint);
102 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
103 const SkPath& path, const SkMatrix* matrix,
104 const SkPaint& paint);
105 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
106 const SkPoint verts[], const SkPoint texs[],
107 const SkColor colors[], SkXfermode* xmode,
108 const uint16_t indices[], int indexCount,
109 const SkPaint& paint);
110 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
111 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000112 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000115
116 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 * Make's this device's rendertarget current in the underlying 3D API.
118 * Also implicitly flushes.
119 */
120 virtual void makeRenderTargetCurrent();
121
122protected:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000123 typedef GrContext::TextureCacheEntry TexCache;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000124 enum TexType {
125 kBitmap_TexType,
126 kDeviceRenderTarget_TexType,
127 kSaveLayerDeviceRenderTarget_TexType
128 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000129 TexCache lockCachedTexture(const SkBitmap& bitmap,
130 const GrSamplerState& sampler,
131 TexType type = kBitmap_TexType);
132 void unlockCachedTexture(TexCache);
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
134 class SkAutoCachedTexture {
135 public:
136 SkAutoCachedTexture();
137 SkAutoCachedTexture(SkGpuDevice* device,
138 const SkBitmap& bitmap,
139 const GrSamplerState& sampler,
140 GrTexture** texture);
141 ~SkAutoCachedTexture();
142
143 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
144
145 private:
146 SkGpuDevice* fDevice;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000147 TexCache fTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 };
149 friend class SkAutoTexCache;
150
151private:
152 GrContext* fContext;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000153
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 GrSkDrawProcs* fDrawProcs;
155
156 // state for our offscreen render-target
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000157 TexCache fCache;
158 GrTexture* fTexture;
159 GrRenderTarget* fRenderTarget;
160 bool fNeedClear;
161 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000162
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000163 // called from rt and tex cons
164 void initFromRenderTarget(GrContext*, GrRenderTarget*);
165
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
bsalomon@google.come97f0852011-06-17 13:10:25 +0000190 // override from SkDevice
191 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
192 int width, int height,
193 bool isOpaque,
194 Usage usage);
195
bsalomon@google.com5782d712011-01-21 21:03:59 +0000196 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000197 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000198
199 void prepareRenderTarget(const SkDraw&);
200 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000201 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
203 typedef SkDevice INHERITED;
204};
205
206#endif
207