blob: d2bc62b70fb69e515db34339d41598cf096cfee9 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#ifndef SkGL_DEFINED
2#define SkGL_DEFINED
3
4#ifdef SK_BUILD_FOR_MAC
5 #include <OpenGL/gl.h>
6 #include <OpenGL/glext.h>
7 #include <AGL/agl.h>
8 // use FBOs for devices
9 #define SK_GL_DEVICE_FBO
10#elif defined(ANDROID)
11 #include <GLES/gl.h>
reed@android.com45bcb222009-01-26 23:54:06 +000012 #include <EGL/egl.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#endif
14
15#include "SkColor.h"
16#include "SkMatrix.h"
17#include "SkShader.h"
18
19class SkPaint;
20class SkPath;
21
22class SkGLClipIter;
23
24//#define TRACE_TEXTURE_CREATE
25
26///////////////////////////////////////////////////////////////////////////////
27
28#if GL_OES_compressed_paletted_texture
29 #define SK_GL_SUPPORT_COMPRESSEDTEXIMAGE2D
30#endif
31
32#if GL_OES_fixed_point && defined(SK_SCALAR_IS_FIXED)
33 #define SK_GLType GL_FIXED
34#else
35 #define SK_GLType GL_FLOAT
36#endif
37
38#if SK_GLType == GL_FIXED
39 typedef SkFixed SkGLScalar;
40
41 #define SkIntToGL(n) SkIntToFixed(n)
42 #define SkScalarToGL(x) SkScalarToFixed(x)
43 #define SK_GLScalar1 SK_Fixed1
44 #define SkGLScalarMul(a, b) SkFixedMul(a, b)
45 #define MAKE_GL(name) name ## x
46
47 #ifdef SK_SCALAR_IS_FIXED
48 #define GLSCALAR_IS_SCALAR 1
49 #define SkPerspToGL(x) SkFractToFixed(x)
50 #else
51 #define GLSCALAR_IS_SCALAR 0
52 #define SkPerspToGL(x) SkFractToFloat(x)
53 #endif
54#else
55 typedef float SkGLScalar;
56
57 #define SkIntToGL(n) (n)
58 #define SkScalarToGL(x) SkScalarToFloat(x)
59 #define SK_GLScalar1 (1.f)
60 #define SkGLScalarMul(a, b) ((a) * (b))
61 #define MAKE_GL(name) name ## f
62
63 #ifdef SK_SCALAR_IS_FLOAT
64 #define GLSCALAR_IS_SCALAR 1
65 #define SkPerspToGL(x) (x)
66 #else
67 #define GLSCALAR_IS_SCALAR 0
68 #define SkPerspToGL(x) SkFractToFloat(x)
69 #endif
70#endif
71
72#if GL_OES_fixed_point
73 typedef SkFixed SkGLTextScalar;
74 #define SK_TextGLType GL_FIXED
75
76 #define SkIntToTextGL(n) SkIntToFixed(n)
77 #define SkFixedToTextGL(x) (x)
78
79 #define SK_glTexParameteri(target, pname, param) \
80 glTexParameterx(target, pname, param)
81#else
82 typedef float SkGLTextScalar;
83 #define SK_TextGLType SK_GLType
84 #define SK_GL_HAS_COLOR4UB
85
86 #define SkIntToTextGL(n) SkIntToGL(n)
87 #define SkFixedToTextGL(x) SkFixedToFloat(x)
88
89
90 #define SK_glTexParameteri(target, pname, param) \
91 glTexParameteri(target, pname, param)
92#endif
93
94///////////////////////////////////////////////////////////////////////////////
95
96// text has its own vertex class, since it may want to be in fixed point (given)
97// that it starts with all integers) even when the default vertices are floats
98struct SkGLTextVertex {
99 SkGLTextScalar fX;
100 SkGLTextScalar fY;
101
102 void setI(int x, int y) {
103 fX = SkIntToTextGL(x);
104 fY = SkIntToTextGL(y);
105 }
106
107 void setX(SkFixed x, SkFixed y) {
108 fX = SkFixedToTextGL(x);
109 fY = SkFixedToTextGL(y);
110 }
111
112 // counter-clockwise fan
113 void setIRectFan(int l, int t, int r, int b) {
114 SkGLTextVertex* SK_RESTRICT v = this;
115 v[0].setI(l, t);
116 v[1].setI(l, b);
117 v[2].setI(r, b);
118 v[3].setI(r, t);
119 }
120
121 // counter-clockwise fan
122 void setXRectFan(SkFixed l, SkFixed t, SkFixed r, SkFixed b) {
123 SkGLTextVertex* SK_RESTRICT v = this;
124 v[0].setX(l, t);
125 v[1].setX(l, b);
126 v[2].setX(r, b);
127 v[3].setX(r, t);
128 }
129};
130
131struct SkGLVertex {
132 SkGLScalar fX;
133 SkGLScalar fY;
134
135 void setGL(SkGLScalar x, SkGLScalar y) {
136 fX = x;
137 fY = y;
138 }
139
140 void setScalars(SkScalar x, SkScalar y) {
141 fX = SkScalarToGL(x);
142 fY = SkScalarToGL(y);
143 }
144
145 void setPoint(const SkPoint& pt) {
146 fX = SkScalarToGL(pt.fX);
147 fY = SkScalarToGL(pt.fY);
148 }
149
150 void setPoints(const SkPoint* SK_RESTRICT pts, int count) {
151 const SkScalar* SK_RESTRICT src = (const SkScalar*)pts;
152 SkGLScalar* SK_RESTRICT dst = (SkGLScalar*)this;
153 for (int i = 0; i < count; i++) {
154 *dst++ = SkScalarToGL(*src++);
155 *dst++ = SkScalarToGL(*src++);
156 }
157 }
158
159 // counter-clockwise fan
160 void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
161 SkGLVertex* v = this;
162 v[0].setScalars(l, t);
163 v[1].setScalars(l, b);
164 v[2].setScalars(r, b);
165 v[3].setScalars(r, t);
166 }
167
168 // counter-clockwise fan
169 void setIRectFan(int l, int t, int r, int b) {
170 SkGLVertex* v = this;
171 v[0].setGL(SkIntToGL(l), SkIntToGL(t));
172 v[1].setGL(SkIntToGL(l), SkIntToGL(b));
173 v[2].setGL(SkIntToGL(r), SkIntToGL(b));
174 v[3].setGL(SkIntToGL(r), SkIntToGL(t));
175 }
176
177 // counter-clockwise fan
178 void setRectFan(const SkRect& r) {
179 this->setRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom);
180 }
181
182 // counter-clockwise fan
183 void setIRectFan(const SkIRect& r) {
184 this->setIRectFan(r.fLeft, r.fTop, r.fRight, r.fBottom);
185 }
186};
187
188struct SkGLMatrix {
189 SkGLScalar fMat[16];
190
191 void reset() {
192 bzero(fMat, sizeof(fMat));
193 fMat[0] = fMat[5] = fMat[10] = fMat[15] = SK_GLScalar1;
194 }
195
196 void set(const SkMatrix& m) {
197 bzero(fMat, sizeof(fMat));
198 fMat[0] = SkScalarToGL(m[SkMatrix::kMScaleX]);
199 fMat[4] = SkScalarToGL(m[SkMatrix::kMSkewX]);
200 fMat[12] = SkScalarToGL(m[SkMatrix::kMTransX]);
201
202 fMat[1] = SkScalarToGL(m[SkMatrix::kMSkewY]);
203 fMat[5] = SkScalarToGL(m[SkMatrix::kMScaleY]);
204 fMat[13] = SkScalarToGL(m[SkMatrix::kMTransY]);
205
206 fMat[3] = SkPerspToGL(m[SkMatrix::kMPersp0]);
207 fMat[7] = SkPerspToGL(m[SkMatrix::kMPersp1]);
208 fMat[15] = SkPerspToGL(m[SkMatrix::kMPersp2]);
209
210 fMat[10] = SK_GLScalar1; // z-scale
211 }
212};
213
214class SkGL {
215public:
216 static void SetColor(SkColor c);
217 static void SetAlpha(U8CPU alpha);
218 static void SetPaint(const SkPaint&, bool isPremul = true,
219 bool justAlpha = false);
220 static void SetPaintAlpha(const SkPaint& paint, bool isPremul = true) {
221 SetPaint(paint, isPremul, true);
222 }
223
224 static void SetRGBA(uint8_t rgba[], const SkColor src[], int count);
225 static void DumpError(const char caller[]);
226
227 static void Ortho(float left, float right, float bottom, float top,
228 float near, float far);
229
230 static inline void Translate(SkScalar dx, SkScalar dy) {
231 MAKE_GL(glTranslate)(SkScalarToGL(dx), SkScalarToGL(dy), 0);
232 }
233
234 static inline void Scale(SkScalar sx, SkScalar sy) {
235 MAKE_GL(glScale)(SkScalarToGL(sx), SkScalarToGL(sy), SK_GLScalar1);
236 }
237
238 static inline void Rotate(SkScalar angle) {
239 MAKE_GL(glRotate)(SkScalarToGL(angle), 0, 0, SK_GLScalar1);
240 }
241
242 static inline void MultMatrix(const SkMatrix& m) {
243 SkGLMatrix glm;
244 glm.set(m);
245 MAKE_GL(glMultMatrix)(glm.fMat);
246 }
247
248 static inline void LoadMatrix(const SkMatrix& m) {
249 SkGLMatrix glm;
250 glm.set(m);
251 MAKE_GL(glLoadMatrix)(glm.fMat);
252 }
253
254 static void Scissor(const SkIRect&, int viewportHeight);
255
256 // return the byte size for the associated texture memory. This doesn't
257 // always == bitmap.getSize(), since on a given port we may have to change
258 // the format when the bitmap's pixels are copied over to GL
259 static size_t ComputeTextureMemorySize(const SkBitmap&);
260 // return 0 on failure
261 static GLuint BindNewTexture(const SkBitmap&, SkPoint* dimension);
262
263 static void SetTexParams(bool filter,
264 SkShader::TileMode tx, SkShader::TileMode ty);
265 static void SetTexParamsClamp(bool filter);
266
267 static void DrawVertices(int count, GLenum mode,
268 const SkGLVertex* SK_RESTRICT vertex,
269 const SkGLVertex* SK_RESTRICT texCoords,
270 const uint8_t* SK_RESTRICT colorArray,
271 const uint16_t* SK_RESTRICT indexArray,
272 SkGLClipIter*);
273
274 static void PrepareForFillPath(SkPaint* paint);
275 static void FillPath(const SkPath& path, const SkPaint& paint, bool useTex,
276 SkGLClipIter*);
277 static void DrawPath(const SkPath& path, bool useTex, SkGLClipIter*);
278};
279
280#include "SkRegion.h"
281
282class SkGLClipIter : public SkRegion::Iterator {
283public:
284 SkGLClipIter(int viewportHeight) : fViewportHeight(viewportHeight) {}
285
286 // call rewind only if this is non-null
287 void safeRewind() {
288 if (this) {
289 this->rewind();
290 }
291 }
292
293 void scissor() {
294 SkASSERT(!this->done());
295 SkGL::Scissor(this->rect(), fViewportHeight);
296 }
297
298private:
299 const int fViewportHeight;
300};
301
302#endif
303