blob: 15def87942f37efbba49e3dd5dce950f5e5b5022 [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
38 * lifetime.
39 */
40 SkGpuDevice(GrContext*,
41 const SkBitmap& bitmap,
42 GrRenderTarget* renderTargetOrNull);
43
44 /**
45 * Magic value that can be passed to constructor. Causes
46 * the device to infer rendertarget from underlying 3D API (e.g. GL or D3D).
47 * This isn't a valid pointer, don't attempt to dereference.
48 */
49 static GrRenderTarget* Current3DApiRenderTarget();
50
reed@google.comac10a2d2010-12-22 21:39:39 +000051 virtual ~SkGpuDevice();
reed@google.com7b201d22011-01-11 18:59:23 +000052
reed@google.comac10a2d2010-12-22 21:39:39 +000053 GrContext* context() const { return fContext; }
54
55 /**
56 * If this device was built for rendering as a layer (i.e. offscreen),
57 * then this will return the platform-specific handle to that GPU resource.
58 * For example, in OpenGL, this will return the FBO's texture ID.
59 * If this device was not built for rendering as a layer, then 0
60 * is returned.
61 */
62 intptr_t getLayerTextureHandle() const;
reed@google.com7b201d22011-01-11 18:59:23 +000063
reed@google.comac10a2d2010-12-22 21:39:39 +000064 // call to set the clip to the specified rect
65 void scissor(const SkIRect&);
66
67 /**
68 * Override from SkGpuDevice, so we can set our FBO to be the render target
69 * The canvas parameter must be a SkGpuCanvas
70 */
bsalomon@google.comd302f142011-03-03 13:54:13 +000071 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&,
72 const SkClipStack& clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +000073
74 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
75
76 // overrides from SkDevice
77
bsalomon@google.com398109c2011-04-14 18:40:27 +000078 virtual void clear(SkColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +000079 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
80 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000081
reed@google.com46799cd2011-02-22 20:56:26 +000082 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
83 const SkClipStack&);
reed@google.comac10a2d2010-12-22 21:39:39 +000084
85 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
86 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
87 const SkPoint[], const SkPaint& paint);
88 virtual void drawRect(const SkDraw&, const SkRect& r,
89 const SkPaint& paint);
90 virtual void drawPath(const SkDraw&, const SkPath& path,
91 const SkPaint& paint, const SkMatrix* prePathMatrix,
92 bool pathIsMutable);
93 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
94 const SkIRect* srcRectOrNull,
95 const SkMatrix& matrix, const SkPaint& paint);
96 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
97 int x, int y, const SkPaint& paint);
98 virtual void drawText(const SkDraw&, const void* text, size_t len,
99 SkScalar x, SkScalar y, const SkPaint& paint);
100 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
101 const SkScalar pos[], SkScalar constY,
102 int scalarsPerPos, const SkPaint& paint);
103 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
104 const SkPath& path, const SkMatrix* matrix,
105 const SkPaint& paint);
106 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
107 const SkPoint verts[], const SkPoint texs[],
108 const SkColor colors[], SkXfermode* xmode,
109 const uint16_t indices[], int indexCount,
110 const SkPaint& paint);
111 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
112 const SkPaint&);
reed@google.comf67e4cf2011-03-15 20:56:58 +0000113 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*);
reed@google.com7b201d22011-01-11 18:59:23 +0000114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000116
117 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 * Make's this device's rendertarget current in the underlying 3D API.
119 * Also implicitly flushes.
120 */
121 virtual void makeRenderTargetCurrent();
122
123protected:
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000124 // override
125 virtual SkDeviceFactory* onNewDeviceFactory();
126
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 class TexCache;
128 TexCache* lockCachedTexture(const SkBitmap& bitmap,
129 const GrSamplerState& sampler,
130 GrTexture** texture,
131 bool forDeviceRenderTarget = false);
132 void unlockCachedTexture(TexCache*);
133
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;
147 TexCache* fTex;
148 };
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
157 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.com5782d712011-01-21 21:03:59 +0000163 // doesn't set the texture/sampler/matrix state
164 // caller needs to null out GrPaint's texture if
165 // non-textured drawing is desired.
166 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
167 bool justAlpha,
168 GrPaint* grPaint);
169
170 // uses the SkShader to setup paint, act used to
171 // hold lock on cached texture and free it when
172 // destroyed.
173 bool skPaint2GrPaintShader(const SkPaint& skPaint,
174 SkAutoCachedTexture* act,
175 const SkMatrix& ctm,
176 GrPaint* grPaint);
177
178 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000179 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000180
181 void prepareRenderTarget(const SkDraw&);
182 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000183 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
185 typedef SkDevice INHERITED;
186};
187
188#endif
189