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