blob: 141ed96423a2b988639e5720c745bc284b1968fb [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 */
33class SkGpuDevice : public SkDevice {
34public:
reed@google.com7b201d22011-01-11 18:59:23 +000035 SkGpuDevice(GrContext*, const SkBitmap& bitmap, bool isLayer);
reed@google.comac10a2d2010-12-22 21:39:39 +000036 virtual ~SkGpuDevice();
reed@google.com7b201d22011-01-11 18:59:23 +000037
reed@google.comac10a2d2010-12-22 21:39:39 +000038 GrContext* context() const { return fContext; }
39
40 /**
41 * If this device was built for rendering as a layer (i.e. offscreen),
42 * then this will return the platform-specific handle to that GPU resource.
43 * For example, in OpenGL, this will return the FBO's texture ID.
44 * If this device was not built for rendering as a layer, then 0
45 * is returned.
46 */
47 intptr_t getLayerTextureHandle() const;
reed@google.com7b201d22011-01-11 18:59:23 +000048
reed@google.comac10a2d2010-12-22 21:39:39 +000049 /**
50 * Attaches the device to a rendering surface. This device will then render
51 * to the surface.
52 * For example, in OpenGL, the device will interpret handle as an FBO ID
53 * and drawing to the device would direct GL to the FBO.
54 */
55 void bindDeviceToTargetHandle(intptr_t handle);
56
57 // call to set the clip to the specified rect
58 void scissor(const SkIRect&);
59
60 /**
61 * Override from SkGpuDevice, so we can set our FBO to be the render target
62 * The canvas parameter must be a SkGpuCanvas
63 */
64 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&);
65
66 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
67
68 // overrides from SkDevice
69
70 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
71 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000072
reed@google.comac10a2d2010-12-22 21:39:39 +000073 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip);
74
75 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
76 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
77 const SkPoint[], const SkPaint& paint);
78 virtual void drawRect(const SkDraw&, const SkRect& r,
79 const SkPaint& paint);
80 virtual void drawPath(const SkDraw&, const SkPath& path,
81 const SkPaint& paint, const SkMatrix* prePathMatrix,
82 bool pathIsMutable);
83 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
84 const SkIRect* srcRectOrNull,
85 const SkMatrix& matrix, const SkPaint& paint);
86 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,
89 SkScalar x, SkScalar y, const SkPaint& paint);
90 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
91 const SkScalar pos[], SkScalar constY,
92 int scalarsPerPos, const SkPaint& paint);
93 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
94 const SkPath& path, const SkMatrix* matrix,
95 const SkPaint& paint);
96 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,
100 const SkPaint& paint);
101 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
102 const SkPaint&);
reed@google.com7b201d22011-01-11 18:59:23 +0000103
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000105
106 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 * Make's this device's rendertarget current in the underlying 3D API.
108 * Also implicitly flushes.
109 */
110 virtual void makeRenderTargetCurrent();
111
112protected:
113 class TexCache;
114 TexCache* lockCachedTexture(const SkBitmap& bitmap,
115 const GrSamplerState& sampler,
116 GrTexture** texture,
117 bool forDeviceRenderTarget = false);
118 void unlockCachedTexture(TexCache*);
119
120 class SkAutoCachedTexture {
121 public:
122 SkAutoCachedTexture();
123 SkAutoCachedTexture(SkGpuDevice* device,
124 const SkBitmap& bitmap,
125 const GrSamplerState& sampler,
126 GrTexture** texture);
127 ~SkAutoCachedTexture();
128
129 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
130
131 private:
132 SkGpuDevice* fDevice;
133 TexCache* fTex;
134 };
135 friend class SkAutoTexCache;
136
137private:
138 GrContext* fContext;
139 GrSkDrawProcs* fDrawProcs;
140
141 // state for our offscreen render-target
142 TexCache* fCache;
143 GrTexture* fTexture;
144 GrRenderTarget* fRenderTarget;
145 bool fNeedClear;
146 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000147
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 SkDrawProcs* initDrawForText(const SkPaint&, GrTextContext*);
149 bool bindDeviceAsTexture(SkPoint* max);
150
151 void prepareRenderTarget(const SkDraw&);
152 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
153 const SkIRect&, const SkMatrix&, const SkPaint&);
reed@google.com7b201d22011-01-11 18:59:23 +0000154
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 class AutoPaintShader {
156 public:
157 AutoPaintShader();
158 AutoPaintShader(SkGpuDevice*, const SkPaint& paint, const SkMatrix&);
159
160 void init(SkGpuDevice*, const SkPaint& paint, const SkMatrix&);
161
162 bool failed() const { return !fSuccess; }
163 bool useTex() const { return fTexture != 0; }
164 private:
165 GrTexture* fTexture;
166 SkAutoCachedTexture fCachedTexture;
167 bool fSuccess;
168 };
169 friend class AutoPaintShader;
170
171 typedef SkDevice INHERITED;
172};
173
174#endif
175