blob: db65e0503297d80ef997065583a26f08e67aeeb0 [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:
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 */
71 virtual void gainFocus(SkCanvas*, const SkMatrix&, const SkRegion&);
72
73 virtual SkGpuTexture* accessTexture() { return (SkGpuTexture*)fTexture; }
74
75 // overrides from SkDevice
76
77 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
78 virtual void writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com7b201d22011-01-11 18:59:23 +000079
reed@google.comac10a2d2010-12-22 21:39:39 +000080 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip);
81
82 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
83 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
84 const SkPoint[], const SkPaint& paint);
85 virtual void drawRect(const SkDraw&, const SkRect& r,
86 const SkPaint& paint);
87 virtual void drawPath(const SkDraw&, const SkPath& path,
88 const SkPaint& paint, const SkMatrix* prePathMatrix,
89 bool pathIsMutable);
90 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
91 const SkIRect* srcRectOrNull,
92 const SkMatrix& matrix, const SkPaint& paint);
93 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
94 int x, int y, const SkPaint& paint);
95 virtual void drawText(const SkDraw&, const void* text, size_t len,
96 SkScalar x, SkScalar y, const SkPaint& paint);
97 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
98 const SkScalar pos[], SkScalar constY,
99 int scalarsPerPos, const SkPaint& paint);
100 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
101 const SkPath& path, const SkMatrix* matrix,
102 const SkPaint& paint);
103 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
104 const SkPoint verts[], const SkPoint texs[],
105 const SkColor colors[], SkXfermode* xmode,
106 const uint16_t indices[], int indexCount,
107 const SkPaint& paint);
108 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
109 const SkPaint&);
reed@google.com7b201d22011-01-11 18:59:23 +0000110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 virtual void flush() { fContext->flush(false); }
reed@google.com7b201d22011-01-11 18:59:23 +0000112
113 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 * Make's this device's rendertarget current in the underlying 3D API.
115 * Also implicitly flushes.
116 */
117 virtual void makeRenderTargetCurrent();
118
119protected:
120 class TexCache;
121 TexCache* lockCachedTexture(const SkBitmap& bitmap,
122 const GrSamplerState& sampler,
123 GrTexture** texture,
124 bool forDeviceRenderTarget = false);
125 void unlockCachedTexture(TexCache*);
126
127 class SkAutoCachedTexture {
128 public:
129 SkAutoCachedTexture();
130 SkAutoCachedTexture(SkGpuDevice* device,
131 const SkBitmap& bitmap,
132 const GrSamplerState& sampler,
133 GrTexture** texture);
134 ~SkAutoCachedTexture();
135
136 GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState&);
137
138 private:
139 SkGpuDevice* fDevice;
140 TexCache* fTex;
141 };
142 friend class SkAutoTexCache;
143
144private:
145 GrContext* fContext;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000146
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 GrSkDrawProcs* fDrawProcs;
148
149 // state for our offscreen render-target
150 TexCache* fCache;
151 GrTexture* fTexture;
152 GrRenderTarget* fRenderTarget;
153 bool fNeedClear;
154 bool fNeedPrepareRenderTarget;
reed@google.com7b201d22011-01-11 18:59:23 +0000155
bsalomon@google.com5782d712011-01-21 21:03:59 +0000156 // doesn't set the texture/sampler/matrix state
157 // caller needs to null out GrPaint's texture if
158 // non-textured drawing is desired.
159 bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
160 bool justAlpha,
161 GrPaint* grPaint);
162
163 // uses the SkShader to setup paint, act used to
164 // hold lock on cached texture and free it when
165 // destroyed.
166 bool skPaint2GrPaintShader(const SkPaint& skPaint,
167 SkAutoCachedTexture* act,
168 const SkMatrix& ctm,
169 GrPaint* grPaint);
170
171 SkDrawProcs* initDrawForText(GrTextContext*);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000172 bool bindDeviceAsTexture(GrPaint* paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000173
174 void prepareRenderTarget(const SkDraw&);
175 void internalDrawBitmap(const SkDraw&, const SkBitmap&,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000176 const SkIRect&, const SkMatrix&, GrPaint* grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000177
178 typedef SkDevice INHERITED;
179};
180
181#endif
182