blob: e42e99709dcbd56f7b7e71bda5e6e0ab266e4130 [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;
26class SkGpuCanvas;
27struct GrSkDrawProcs;
28class GrTextContext;
29
30/**
31 * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the
32 * canvas.
33 */
34class SkGpuDevice : public SkDevice {
35public:
36 SkGpuDevice(SkGpuCanvas*, const SkBitmap& bitmap, bool isLayer);
37 virtual ~SkGpuDevice();
38
39 GrContext* context() const { return fContext; }
40
41 /**
42 * If this device was built for rendering as a layer (i.e. offscreen),
43 * then this will return the platform-specific handle to that GPU resource.
44 * For example, in OpenGL, this will return the FBO's texture ID.
45 * If this device was not built for rendering as a layer, then 0
46 * is returned.
47 */
48 intptr_t getLayerTextureHandle() const;
49
50 /**
51 * Attaches the device to a rendering surface. This device will then render
52 * to the surface.
53 * For example, in OpenGL, the device will interpret handle as an FBO ID
54 * and drawing to the device would direct GL to the FBO.
55 */
56 void bindDeviceToTargetHandle(intptr_t handle);
57
58 // call to set the clip to the specified rect
59 void scissor(const SkIRect&);
60
61 /**
62 * Override from SkGpuDevice, so we can set our FBO to be the render target
63 * The canvas parameter must be a SkGpuCanvas
64 */
65 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&);
66
67 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
68
69 // overrides from SkDevice
70
71 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
72 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
73
74 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip);
75
76 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
77 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
78 const SkPoint[], const SkPaint& paint);
79 virtual void drawRect(const SkDraw&, const SkRect& r,
80 const SkPaint& paint);
81 virtual void drawPath(const SkDraw&, const SkPath& path,
82 const SkPaint& paint, const SkMatrix* prePathMatrix,
83 bool pathIsMutable);
84 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
85 const SkIRect* srcRectOrNull,
86 const SkMatrix& matrix, const SkPaint& paint);
87 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
88 int x, int y, const SkPaint& paint);
89 virtual void drawText(const SkDraw&, const void* text, size_t len,
90 SkScalar x, SkScalar y, const SkPaint& paint);
91 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
92 const SkScalar pos[], SkScalar constY,
93 int scalarsPerPos, const SkPaint& paint);
94 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
95 const SkPath& path, const SkMatrix* matrix,
96 const SkPaint& paint);
97 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
98 const SkPoint verts[], const SkPoint texs[],
99 const SkColor colors[], SkXfermode* xmode,
100 const uint16_t indices[], int indexCount,
101 const SkPaint& paint);
102 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
103 const SkPaint&);
104
105 virtual void flush() { fContext->flush(false); }
106
107 /**
108 * Make's this device's rendertarget current in the underlying 3D API.
109 * Also implicitly flushes.
110 */
111 virtual void makeRenderTargetCurrent();
112
113protected:
114 class TexCache;
115 TexCache* lockCachedTexture(const SkBitmap& bitmap,
116 const GrSamplerState& sampler,
117 GrTexture** texture,
118 bool forDeviceRenderTarget = false);
119 void unlockCachedTexture(TexCache*);
120
121 class SkAutoCachedTexture {
122 public:
123 SkAutoCachedTexture();
124 SkAutoCachedTexture(SkGpuDevice* device,
125 const SkBitmap& bitmap,
126 const GrSamplerState& sampler,
127 GrTexture** texture);
128 ~SkAutoCachedTexture();
129
130 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
131
132 private:
133 SkGpuDevice* fDevice;
134 TexCache* fTex;
135 };
136 friend class SkAutoTexCache;
137
138private:
139 GrContext* fContext;
140 GrSkDrawProcs* fDrawProcs;
141
142 // state for our offscreen render-target
143 TexCache* fCache;
144 GrTexture* fTexture;
145 GrRenderTarget* fRenderTarget;
146 bool fNeedClear;
147 bool fNeedPrepareRenderTarget;
148
149 SkDrawProcs* initDrawForText(const SkPaint&, GrTextContext*);
150 bool bindDeviceAsTexture(SkPoint* max);
151
152 void prepareRenderTarget(const SkDraw&);
153 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
154 const SkIRect&, const SkMatrix&, const SkPaint&);
155
156 class AutoPaintShader {
157 public:
158 AutoPaintShader();
159 AutoPaintShader(SkGpuDevice*, const SkPaint& paint, const SkMatrix&);
160
161 void init(SkGpuDevice*, const SkPaint& paint, const SkMatrix&);
162
163 bool failed() const { return !fSuccess; }
164 bool useTex() const { return fTexture != 0; }
165 private:
166 GrTexture* fTexture;
167 SkAutoCachedTexture fCachedTexture;
168 bool fSuccess;
169 };
170 friend class AutoPaintShader;
171
172 typedef SkDevice INHERITED;
173};
174
175#endif
176