blob: 6c4285ed535a888edf12fbae484705eb9289ecbf [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
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 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000058 * Override from SkGpuDevice, so we can set our FBO to be the render target
59 * The canvas parameter must be a SkGpuCanvas
60 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000061 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
reed@google.com75d939b2011-12-07 15:07:23 +000062 const SkClipStack& clipStack) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000063
reed@google.com75d939b2011-12-07 15:07:23 +000064 virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000065
66 // overrides from SkDevice
67
reed@google.com75d939b2011-12-07 15:07:23 +000068 virtual void clear(SkColor color) SK_OVERRIDE;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000069 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
70 SkCanvas::Config8888 config8888) SK_OVERRIDE;
reed@google.com7b201d22011-01-11 18:59:23 +000071
tomhudson@google.com8a0b0292011-09-13 14:41:06 +000072 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
reed@google.com75d939b2011-12-07 15:07:23 +000073 const SkClipStack&) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000074
reed@google.com75d939b2011-12-07 15:07:23 +000075 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000076 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
reed@google.com75d939b2011-12-07 15:07:23 +000077 const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000078 virtual void drawRect(const SkDraw&, const SkRect& r,
reed@google.com75d939b2011-12-07 15:07:23 +000079 const SkPaint& paint) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000080 virtual void drawPath(const SkDraw&, const SkPath& path,
81 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.com75d939b2011-12-07 15:07:23 +000082 bool pathIsMutable) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000083 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
84 const SkIRect* srcRectOrNull,
reed@google.com75d939b2011-12-07 15:07:23 +000085 const SkMatrix&, const SkPaint&) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000086 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
87 int x, int y, const SkPaint& paint);
88 virtual void drawText(const SkDraw&, const void* text, size_t len,
reed@google.com75d939b2011-12-07 15:07:23 +000089 SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000090 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
91 const SkScalar pos[], SkScalar constY,
reed@google.com75d939b2011-12-07 15:07:23 +000092 int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000093 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
94 const SkPath& path, const SkMatrix* matrix,
reed@google.com75d939b2011-12-07 15:07:23 +000095 const SkPaint&) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000096 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
97 const SkPoint verts[], const SkPoint texs[],
98 const SkColor colors[], SkXfermode* xmode,
99 const uint16_t indices[], int indexCount,
reed@google.com75d939b2011-12-07 15:07:23 +0000100 const SkPaint&) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
reed@google.com75d939b2011-12-07 15:07:23 +0000102 const SkPaint&) SK_OVERRIDE;
103 virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
reed@google.com7b201d22011-01-11 18:59:23 +0000104
reed@google.com75d939b2011-12-07 15:07:23 +0000105 virtual void flush();
reed@google.com7b201d22011-01-11 18:59:23 +0000106
107 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 * Make's this device's rendertarget current in the underlying 3D API.
109 * Also implicitly flushes.
110 */
111 virtual void makeRenderTargetCurrent();
112
reed@google.com76dd2772012-01-05 21:15:07 +0000113 virtual bool filterImage(SkImageFilter*, const SkBitmap& src,
114 const SkMatrix& ctm,
115 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE;
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117protected:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000118 typedef GrContext::TextureCacheEntry TexCache;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000119 enum TexType {
120 kBitmap_TexType,
121 kDeviceRenderTarget_TexType,
122 kSaveLayerDeviceRenderTarget_TexType
123 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000124 TexCache lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000125 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000126 TexType type = kBitmap_TexType);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000127 bool isBitmapInTextureCache(const SkBitmap& bitmap,
128 const GrSamplerState& sampler) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000129 void unlockCachedTexture(TexCache);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
131 class SkAutoCachedTexture {
132 public:
133 SkAutoCachedTexture();
134 SkAutoCachedTexture(SkGpuDevice* device,
135 const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000136 const GrSamplerState* sampler,
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 GrTexture** texture);
138 ~SkAutoCachedTexture();
139
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000140 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState*);
reed@google.comac10a2d2010-12-22 21:39:39 +0000141
142 private:
143 SkGpuDevice* fDevice;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000144 TexCache fTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 };
146 friend class SkAutoTexCache;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000147
148 // overrides from SkDevice
bsalomon@google.com910267d2011-11-02 20:06:25 +0000149 virtual bool onReadPixels(const SkBitmap& bitmap,
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000150 int x, int y,
151 SkCanvas::Config8888 config8888) SK_OVERRIDE;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000152
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
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
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000160 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.comf9046fe2011-06-17 15:10:21 +0000166 // called from rt and tex cons
167 void initFromRenderTarget(GrContext*, GrRenderTarget*);
168
bsalomon@google.com5782d712011-01-21 21:03:59 +0000169 // doesn't set the texture/sampler/matrix state
170 // caller needs to null out GrPaint's texture if
171 // non-textured drawing is desired.
Scroggod757df22011-05-16 13:11:16 +0000172 // Set constantColor to true if a constant color
173 // will be used. This is an optimization, and can
174 // always be set to false. constantColor should
175 // never be true if justAlpha is true.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000176 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
177 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000178 GrPaint* grPaint,
179 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000180
181 // uses the SkShader to setup paint, act used to
182 // hold lock on cached texture and free it when
183 // destroyed.
Scroggod757df22011-05-16 13:11:16 +0000184 // If there is no shader, constantColor will
185 // be passed to skPaint2GrPaintNoShader. Otherwise
186 // it is ignored.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000187 bool skPaint2GrPaintShader(const SkPaint& skPaint,
188 SkAutoCachedTexture* act,
189 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000190 GrPaint* grPaint,
191 bool constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000192
bsalomon@google.come97f0852011-06-17 13:10:25 +0000193 // override from SkDevice
194 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
195 int width, int height,
196 bool isOpaque,
197 Usage usage);
198
bsalomon@google.com5782d712011-01-21 21:03:59 +0000199 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000200 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000201
202 void prepareRenderTarget(const SkDraw&);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000203 bool shouldTileBitmap(const SkBitmap& bitmap,
204 const GrSamplerState& sampler,
205 const SkIRect* srcRectPtr,
206 int* tileSize) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000208 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000209
210 typedef SkDevice INHERITED;
211};
212
213#endif
214