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