blob: a939b07f760dfba859321670dc889e90eba835df [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#ifndef SkGLDevice_DEFINED
2#define SkGLDevice_DEFINED
3
4#include "SkDevice.h"
5#include "SkGL.h"
6#include "SkRegion.h"
7
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +00008#ifdef SK_BUILD_FOR_MAC
9 #include <OpenGL/gl.h>
10#elif defined(ANDROID)
11 #include <GLES/gl.h>
12#endif
13
14class SkGLDeviceFactory : public SkDeviceFactory {
15public:
16 virtual SkDevice* newDevice(SkBitmap::Config config, int width, int height,
17 bool isOpaque, bool isForLayer);
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +000018};
19
reed@android.com8a1c16f2008-12-17 15:59:43 +000020struct SkGLDrawProcs;
21
22class SkGLDevice : public SkDevice {
23public:
24 SkGLDevice(const SkBitmap& bitmap, bool offscreen);
25 virtual ~SkGLDevice();
26
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +000027 virtual SkDeviceFactory* getDeviceFactory() {
28 return SkNEW(SkGLDeviceFactory);
29 }
30
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000031 virtual uint32_t getDeviceCapabilities() { return kGL_Capability; }
32
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 // used to identify GLTextCache data in the glyphcache
34 static void GlyphCacheAuxProc(void* data);
35
36 enum TexOrientation {
37 kNo_TexOrientation,
38 kTopToBottom_TexOrientation,
39 kBottomToTop_TexOrientation
40 };
41
42 /** Called when this device is no longer a candidate for a render target,
43 but will instead be used as a texture to be drawn. Be sure to call
44 the base impl if you override, as it will compute size and max.
45 */
46 virtual TexOrientation bindDeviceAsTexture();
47
48 // returns true if complex
49 SkGLClipIter* updateMatrixClip();
50 // call to set the clip to the specified rect
51 void scissor(const SkIRect&);
52
53 // overrides from SkDevice
54 virtual void gainFocus(SkCanvas*);
55 virtual void setMatrixClip(const SkMatrix& matrix, const SkRegion& clip);
56
57 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
58 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
59 const SkPoint[], const SkPaint& paint);
60 virtual void drawRect(const SkDraw&, const SkRect& r,
61 const SkPaint& paint);
62 virtual void drawPath(const SkDraw&, const SkPath& path,
63 const SkPaint& paint);
64 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
65 const SkMatrix& matrix, const SkPaint& paint);
66 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
67 int x, int y, const SkPaint& paint);
68 virtual void drawText(const SkDraw&, const void* text, size_t len,
69 SkScalar x, SkScalar y, const SkPaint& paint);
70 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
71 const SkScalar pos[], SkScalar constY,
72 int scalarsPerPos, const SkPaint& paint);
73 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
74 const SkPath& path, const SkMatrix* matrix,
75 const SkPaint& paint);
76 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
77 const SkPoint verts[], const SkPoint texs[],
78 const SkColor colors[], SkXfermode* xmode,
79 const uint16_t indices[], int indexCount,
80 const SkPaint& paint);
81 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
82 const SkPaint&);
83
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +000084 // settings for the global texture cache
85
86 static size_t GetTextureCacheMaxCount();
87 static void SetTextureCacheMaxCount(size_t count);
88
89 static size_t GetTextureCacheMaxSize();
90 static void SetTextureCacheMaxSize(size_t size);
91
92 /** Call glDeleteTextures for all textures (including those for text)
93 This should be called while the gl-context is still valid. Its purpose
94 is to free up gl resources. Note that if a bitmap or text is drawn after
95 this call, new caches will be created.
96 */
97 static void DeleteAllTextures();
98
99 /** Forget all textures without calling delete (including those for text).
100 This should be called if the gl-context has changed, and the texture
101 IDs that have been cached are no longer valid.
102 */
103 static void AbandonAllTextures();
104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105protected:
106 /** Return the current glmatrix, from a previous call to setMatrixClip */
107 const SkMatrix& matrix() const { return fMatrix; }
108 /** Return the current clip, from a previous call to setMatrixClip */
109 const SkRegion& clip() const { return fClip; }
110
111private:
112 SkGLMatrix fGLMatrix;
113 SkMatrix fMatrix;
114 SkRegion fClip;
115 bool fDirty;
116
117 SkGLClipIter fClipIter;
118 SkGLDrawProcs* fDrawProcs;
119
120 void setupForText(SkDraw* draw, const SkPaint& paint);
121
122 // global texture cache methods
123 class TexCache;
124 static TexCache* LockTexCache(const SkBitmap&, GLuint* name,
125 SkPoint* size);
126 static void UnlockTexCache(TexCache*);
127 class SkAutoLockTexCache {
128 public:
129 SkAutoLockTexCache(const SkBitmap& bitmap, GLuint* name,
130 SkPoint* size) {
131 fTex = SkGLDevice::LockTexCache(bitmap, name, size);
132 }
133 ~SkAutoLockTexCache() {
134 if (fTex) {
135 SkGLDevice::UnlockTexCache(fTex);
136 }
137 }
138 TexCache* get() const { return fTex; }
139 private:
140 TexCache* fTex;
141 };
142 friend class SkAutoTexCache;
143
144 // returns cache if the texture is bound for the shader
145 TexCache* setupGLPaintShader(const SkPaint& paint);
146
147 class AutoPaintShader {
148 public:
149 AutoPaintShader(SkGLDevice*, const SkPaint& paint);
150 ~AutoPaintShader();
151
152 bool useTex() const { return fTexCache != 0; }
153 private:
154 SkGLDevice* fDevice;
155 TexCache* fTexCache;
156 };
157 friend class AutoPaintShader;
158
159 typedef SkDevice INHERITED;
160};
161
162#endif
163