blob: 3b406779af5ab311656811baa21828af2852b874 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
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#ifndef ANDROID_OPENGLES_CONTEXT_H
18#define ANDROID_OPENGLES_CONTEXT_H
19
20#include <stdint.h>
21#include <stddef.h>
22#include <sys/types.h>
23#include <pthread.h>
24#ifdef HAVE_ANDROID_OS
25#include <bionic_tls.h>
26#endif
27
28#include <private/pixelflinger/ggl_context.h>
Mathias Agopianb1514c92009-04-10 14:24:30 -070029#include <hardware/copybit.h>
Mathias Agopian2ff585f2009-06-10 16:01:54 -070030#include <hardware/gralloc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031
32#include <GLES/gl.h>
33#include <GLES/glext.h>
34
35namespace android {
36
37const unsigned int OGLES_NUM_COMPRESSED_TEXTURE_FORMATS = 10;
38
39class EGLTextureObject;
40class EGLSurfaceManager;
41class EGLBufferObjectManager;
42
43namespace gl {
Mathias Agopianb1514c92009-04-10 14:24:30 -070044
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045struct ogles_context_t;
46struct matrixx_t;
47struct transform_t;
48struct buffer_t;
49
50ogles_context_t* getGlContext();
51
52template<typename T>
53static inline void swap(T& a, T& b) {
54 T t(a); a = b; b = t;
55}
56template<typename T>
57inline T max(T a, T b) {
58 return a<b ? b : a;
59}
60template<typename T>
61inline T max(T a, T b, T c) {
62 return max(a, max(b, c));
63}
64template<typename T>
65inline T min(T a, T b) {
66 return a<b ? a : b;
67}
68template<typename T>
69inline T min(T a, T b, T c) {
70 return min(a, min(b, c));
71}
72template<typename T>
73inline T min(T a, T b, T c, T d) {
74 return min(min(a,b), min(c,d));
75}
76
77// ----------------------------------------------------------------------------
78// vertices
79// ----------------------------------------------------------------------------
80
81struct vec3_t {
82 union {
83 struct { GLfixed x, y, z; };
84 struct { GLfixed r, g, b; };
85 struct { GLfixed S, T, R; };
86 GLfixed v[3];
87 };
88};
89
90struct vec4_t {
91 union {
92 struct { GLfixed x, y, z, w; };
93 struct { GLfixed r, g, b, a; };
94 struct { GLfixed S, T, R, Q; };
95 GLfixed v[4];
96 };
97};
98
99struct vertex_t {
100 enum {
Mathias Agopianb1514c92009-04-10 14:24:30 -0700101 // these constant matter for our clipping
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 CLIP_L = 0x0001, // clipping flags
103 CLIP_R = 0x0002,
104 CLIP_B = 0x0004,
105 CLIP_T = 0x0008,
106 CLIP_N = 0x0010,
107 CLIP_F = 0x0020,
108
109 EYE = 0x0040,
110 RESERVED = 0x0080,
Mathias Agopianb1514c92009-04-10 14:24:30 -0700111
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800112 USER_CLIP_0 = 0x0100, // user clipping flags
113 USER_CLIP_1 = 0x0200,
114 USER_CLIP_2 = 0x0400,
115 USER_CLIP_3 = 0x0800,
116 USER_CLIP_4 = 0x1000,
117 USER_CLIP_5 = 0x2000,
118
119 LIT = 0x4000, // lighting has been applied
120 TT = 0x8000, // texture coords transformed
121
122 FRUSTUM_CLIP_ALL= 0x003F,
123 USER_CLIP_ALL = 0x3F00,
124 CLIP_ALL = 0x3F3F,
125 };
Mathias Agopianb1514c92009-04-10 14:24:30 -0700126
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 // the fields below are arranged to minimize d-cache usage
128 // we group together, by cache-line, the fields most likely to be used
129
130 union {
131 vec4_t obj;
132 vec4_t eye;
133 };
134 vec4_t clip;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700135
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 uint32_t flags;
137 size_t index; // cache tag, and vertex index
138 GLfixed fog;
139 uint8_t locked;
140 uint8_t mru;
141 uint8_t reserved[2];
142 vec4_t window;
143
144 vec4_t color;
145 vec4_t texture[GGL_TEXTURE_UNIT_COUNT];
146 uint32_t reserved1[4];
Mathias Agopianb1514c92009-04-10 14:24:30 -0700147
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148 inline void clear() {
149 flags = index = locked = mru = 0;
150 }
151};
152
153struct point_size_t {
154 GGLcoord size;
155 GLboolean smooth;
156};
157
158struct line_width_t {
159 GGLcoord width;
160 GLboolean smooth;
161};
162
163struct polygon_offset_t {
164 GLfixed factor;
165 GLfixed units;
166 GLboolean enable;
167};
168
169// ----------------------------------------------------------------------------
170// arrays
171// ----------------------------------------------------------------------------
172
173struct array_t {
174 typedef void (*fetcher_t)(ogles_context_t*, GLfixed*, const GLvoid*);
175 fetcher_t fetch;
176 GLvoid const* physical_pointer;
177 GLint size;
178 GLsizei stride;
179 GLvoid const* pointer;
180 buffer_t const* bo;
181 uint16_t type;
182 GLboolean enable;
183 GLboolean pad;
184 GLsizei bounds;
185 void init(GLint, GLenum, GLsizei, const GLvoid *, const buffer_t*, GLsizei);
186 inline void resolve();
187 inline const GLubyte* element(GLint i) const {
188 return (const GLubyte*)physical_pointer + i * stride;
189 }
190};
191
192struct array_machine_t {
193 array_t vertex;
194 array_t normal;
195 array_t color;
196 array_t texture[GGL_TEXTURE_UNIT_COUNT];
197 uint8_t activeTexture;
198 uint8_t tmu;
199 uint16_t cull;
200 uint32_t flags;
201 GLenum indicesType;
202 buffer_t const* array_buffer;
203 buffer_t const* element_array_buffer;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700204
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 void (*compileElements)(ogles_context_t*, vertex_t*, GLint, GLsizei);
206 void (*compileElement)(ogles_context_t*, vertex_t*, GLint);
207
208 void (*mvp_transform)(transform_t const*, vec4_t*, vec4_t const*);
209 void (*mv_transform)(transform_t const*, vec4_t*, vec4_t const*);
210 void (*tex_transform[2])(transform_t const*, vec4_t*, vec4_t const*);
211 void (*perspective)(ogles_context_t*c, vertex_t* v);
212 void (*clipVertex)(ogles_context_t* c, vertex_t* nv,
213 GGLfixed t, const vertex_t* s, const vertex_t* p);
214 void (*clipEye)(ogles_context_t* c, vertex_t* nv,
215 GGLfixed t, const vertex_t* s, const vertex_t* p);
216};
217
218struct vertex_cache_t {
219 enum {
220 // must be at least 4
221 // 3 vertice for triangles
222 // or 2 + 2 for indexed triangles w/ cache contention
223 VERTEX_BUFFER_SIZE = 8,
224 // must be a power of two and at least 3
225 VERTEX_CACHE_SIZE = 64, // 8 KB
226
227 INDEX_BITS = 16,
228 INDEX_MASK = ((1LU<<INDEX_BITS)-1),
229 INDEX_SEQ = 1LU<<INDEX_BITS,
230 };
231 vertex_t* vBuffer;
232 vertex_t* vCache;
233 uint32_t sequence;
234 void* base;
235 uint32_t total;
236 uint32_t misses;
237 int64_t startTime;
238 void init();
239 void uninit();
240 void clear();
241 void dump_stats(GLenum mode);
242};
243
244// ----------------------------------------------------------------------------
245// fog
246// ----------------------------------------------------------------------------
247
248struct fog_t {
249 GLfixed density;
250 GLfixed start;
251 GLfixed end;
252 GLfixed invEndMinusStart;
253 GLenum mode;
254 GLfixed (*fog)(ogles_context_t* c, GLfixed z);
255};
256
257// ----------------------------------------------------------------------------
258// user clip planes
259// ----------------------------------------------------------------------------
260
261const unsigned int OGLES_MAX_CLIP_PLANES = 6;
262
263struct clip_plane_t {
264 vec4_t equation;
265};
266
267struct user_clip_planes_t {
268 clip_plane_t plane[OGLES_MAX_CLIP_PLANES];
269 uint32_t enable;
270};
271
272// ----------------------------------------------------------------------------
273// lighting
274// ----------------------------------------------------------------------------
275
276const unsigned int OGLES_MAX_LIGHTS = 8;
277
278struct light_t {
279 vec4_t ambient;
280 vec4_t diffuse;
281 vec4_t specular;
282 vec4_t implicitAmbient;
283 vec4_t implicitDiffuse;
284 vec4_t implicitSpecular;
285 vec4_t position; // position in eye space
286 vec4_t objPosition;
287 vec4_t normalizedObjPosition;
288 vec4_t spotDir;
289 vec4_t normalizedSpotDir;
290 GLfixed spotExp;
291 GLfixed spotCutoff;
292 GLfixed spotCutoffCosine;
293 GLfixed attenuation[3];
294 GLfixed rConstAttenuation;
295 GLboolean enable;
296};
297
298struct material_t {
299 vec4_t ambient;
300 vec4_t diffuse;
301 vec4_t specular;
302 vec4_t emission;
303 GLfixed shininess;
304};
305
306struct light_model_t {
307 vec4_t ambient;
308 GLboolean twoSide;
309};
310
311struct color_material_t {
312 GLenum face;
313 GLenum mode;
314 GLboolean enable;
315};
316
317struct lighting_t {
318 light_t lights[OGLES_MAX_LIGHTS];
319 material_t front;
320 light_model_t lightModel;
321 color_material_t colorMaterial;
322 uint32_t enabledLights;
323 GLboolean enable;
324 vec4_t implicitSceneEmissionAndAmbient;
325 GLenum shadeModel;
326 typedef void (*light_fct_t)(ogles_context_t*, vertex_t*);
327 void (*lightVertex)(ogles_context_t* c, vertex_t* v);
328 void (*lightTriangle)(ogles_context_t* c,
329 vertex_t* v0, vertex_t* v1, vertex_t* v2);
330};
331
332struct culling_t {
333 GLenum cullFace;
334 GLenum frontFace;
335 GLboolean enable;
336};
337
338// ----------------------------------------------------------------------------
339// textures
340// ----------------------------------------------------------------------------
341
342struct texture_unit_t {
343 GLuint name;
344 EGLTextureObject* texture;
345 uint8_t dirty;
346};
347
348struct texture_state_t
349{
350 texture_unit_t tmu[GGL_TEXTURE_UNIT_COUNT];
351 int active; // active tmu
352 EGLTextureObject* defaultTexture;
353 GGLContext* ggl;
354 uint8_t packAlignment;
355 uint8_t unpackAlignment;
356};
357
358// ----------------------------------------------------------------------------
359// transformation and matrices
360// ----------------------------------------------------------------------------
361
362struct matrixf_t;
363
364struct matrixx_t {
365 GLfixed m[16];
366 void load(const matrixf_t& rhs);
367};
368
369struct matrix_stack_t;
370
371
372struct matrixf_t {
373 void loadIdentity();
374 void load(const matrixf_t& rhs);
375
376 inline GLfloat* editElements() { return m; }
377 inline GLfloat const* elements() const { return m; }
378
379 void set(const GLfixed* rhs);
380 void set(const GLfloat* rhs);
381
382 static void multiply(matrixf_t& r,
383 const matrixf_t& lhs, const matrixf_t& rhs);
384
385 void dump(const char* what);
386
387private:
388 friend struct matrix_stack_t;
389 GLfloat m[16];
390 void load(const GLfixed* rhs);
391 void load(const GLfloat* rhs);
392 void multiply(const matrixf_t& rhs);
393 void translate(GLfloat x, GLfloat y, GLfloat z);
394 void scale(GLfloat x, GLfloat y, GLfloat z);
395 void rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z);
396};
397
398enum {
399 OP_IDENTITY = 0x00,
400 OP_TRANSLATE = 0x01,
401 OP_UNIFORM_SCALE = 0x02,
402 OP_SCALE = 0x05,
403 OP_ROTATE = 0x08,
404 OP_SKEW = 0x10,
405 OP_ALL = 0x1F
406};
407
408struct transform_t {
409 enum {
410 FLAGS_2D_PROJECTION = 0x1
411 };
412 matrixx_t matrix;
413 uint32_t flags;
414 uint32_t ops;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700415
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416 union {
417 struct {
418 void (*point2)(transform_t const* t, vec4_t*, vec4_t const*);
419 void (*point3)(transform_t const* t, vec4_t*, vec4_t const*);
420 void (*point4)(transform_t const* t, vec4_t*, vec4_t const*);
421 };
422 void (*pointv[3])(transform_t const* t, vec4_t*, vec4_t const*);
423 };
424
425 void loadIdentity();
426 void picker();
427 void dump(const char* what);
428};
429
430struct mvui_transform_t : public transform_t
431{
432 void picker();
433};
434
435struct matrix_stack_t {
436 enum {
437 DO_PICKER = 0x1,
438 DO_FLOAT_TO_FIXED = 0x2
439 };
440 transform_t transform;
441 uint8_t maxDepth;
442 uint8_t depth;
443 uint8_t dirty;
444 uint8_t reserved;
445 matrixf_t *stack;
446 uint8_t *ops;
447 void init(int depth);
448 void uninit();
449 void loadIdentity();
450 void load(const GLfixed* rhs);
451 void load(const GLfloat* rhs);
452 void multiply(const matrixf_t& rhs);
453 void translate(GLfloat x, GLfloat y, GLfloat z);
454 void scale(GLfloat x, GLfloat y, GLfloat z);
455 void rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z);
456 GLint push();
457 GLint pop();
458 void validate();
459 matrixf_t& top() { return stack[depth]; }
460 const matrixf_t& top() const { return stack[depth]; }
461 const uint32_t top_ops() const { return ops[depth]; }
462 inline bool isRigidBody() const {
463 return !(ops[depth] & ~(OP_TRANSLATE|OP_UNIFORM_SCALE|OP_ROTATE));
464 }
465};
466
467struct vp_transform_t {
468 transform_t transform;
469 matrixf_t matrix;
470 GLfloat zNear;
471 GLfloat zFar;
472 void loadIdentity();
473};
474
475struct transform_state_t {
476 enum {
477 MODELVIEW = 0x01,
478 PROJECTION = 0x02,
479 VIEWPORT = 0x04,
480 TEXTURE = 0x08,
481 MVUI = 0x10,
482 MVIT = 0x20,
483 MVP = 0x40,
484 };
485 matrix_stack_t *current;
486 matrix_stack_t modelview;
487 matrix_stack_t projection;
488 matrix_stack_t texture[GGL_TEXTURE_UNIT_COUNT];
489
490 // modelview * projection
491 transform_t mvp __attribute__((aligned(32)));
492 // viewport transformation
493 vp_transform_t vpt __attribute__((aligned(32)));
494 // same for 4-D vertices
495 transform_t mvp4;
496 // full modelview inverse transpose
497 transform_t mvit4;
498 // upper 3x3 of mv-inverse-transpose (for normals)
499 mvui_transform_t mvui;
500
501 GLenum matrixMode;
502 GLenum rescaleNormals;
503 uint32_t dirty;
504 void invalidate();
505 void update_mvp();
506 void update_mvit();
507 void update_mvui();
508};
509
510struct viewport_t {
511 GLint x;
512 GLint y;
513 GLsizei w;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700514 GLsizei h;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 struct {
516 GLint x;
517 GLint y;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700518 } surfaceport;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519 struct {
520 GLint x;
521 GLint y;
522 GLsizei w;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700523 GLsizei h;
524 } scissor;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525};
526
527// ----------------------------------------------------------------------------
528// Lerping
529// ----------------------------------------------------------------------------
530
531struct compute_iterators_t
532{
533 void initTriangle(
534 vertex_t const* v0,
535 vertex_t const* v1,
536 vertex_t const* v2);
537
538 void initLine(
539 vertex_t const* v0,
540 vertex_t const* v1);
541
542 inline void initLerp(vertex_t const* v0, uint32_t enables);
543
544 int iteratorsScale(int32_t it[3],
545 int32_t c0, int32_t c1, int32_t c2) const;
546
547 void iterators1616(GGLfixed it[3],
548 GGLfixed c0, GGLfixed c1, GGLfixed c2) const;
549
550 void iterators0032(int32_t it[3],
551 int32_t c0, int32_t c1, int32_t c2) const;
552
553 void iterators0032(int64_t it[3],
554 int32_t c0, int32_t c1, int32_t c2) const;
555
556 GGLcoord area() const { return m_area; }
557
558private:
559 // don't change order of members here -- used by iterators.S
560 GGLcoord m_dx01, m_dy10, m_dx20, m_dy02;
561 GGLcoord m_x0, m_y0;
562 GGLcoord m_area;
563 uint8_t m_scale;
564 uint8_t m_area_scale;
565 uint8_t m_reserved[2];
566
567};
568
569// ----------------------------------------------------------------------------
570// state
571// ----------------------------------------------------------------------------
572
573#ifdef HAVE_ANDROID_OS
574 // We have a dedicated TLS slot in bionic
575 inline void setGlThreadSpecific(ogles_context_t *value) {
576 ((uint32_t *)__get_tls())[TLS_SLOT_OPENGL] = (uint32_t)value;
577 }
578 inline ogles_context_t* getGlThreadSpecific() {
579 return (ogles_context_t *)(((unsigned *)__get_tls())[TLS_SLOT_OPENGL]);
580 }
581#else
582 extern pthread_key_t gGLKey;
583 inline void setGlThreadSpecific(ogles_context_t *value) {
584 pthread_setspecific(gGLKey, value);
585 }
586 inline ogles_context_t* getGlThreadSpecific() {
587 return static_cast<ogles_context_t*>(pthread_getspecific(gGLKey));
588 }
589#endif
590
591
592struct prims_t {
593 typedef ogles_context_t* GL;
594 void (*renderPoint)(GL, vertex_t*);
595 void (*renderLine)(GL, vertex_t*, vertex_t*);
596 void (*renderTriangle)(GL, vertex_t*, vertex_t*, vertex_t*);
597};
598
Mathias Agopianb1514c92009-04-10 14:24:30 -0700599struct copybits_context_t {
600 // A handle to the blit engine, if it exists, else NULL.
601 copybit_device_t* blitEngine;
602 int32_t minScale;
603 int32_t maxScale;
Mathias Agopian2ff585f2009-06-10 16:01:54 -0700604 buffer_handle_t drawSurfaceBuffer;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700605};
606
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607struct ogles_context_t {
608 context_t rasterizer;
609 array_machine_t arrays __attribute__((aligned(32)));
610 texture_state_t textures;
611 transform_state_t transforms;
612 vertex_cache_t vc;
613 prims_t prims;
614 culling_t cull;
615 lighting_t lighting;
616 user_clip_planes_t clipPlanes;
617 compute_iterators_t lerp; __attribute__((aligned(32)));
618 vertex_t current;
619 vec4_t currentColorClamped;
620 vec3_t currentNormal;
621 viewport_t viewport;
622 point_size_t point;
623 line_width_t line;
624 polygon_offset_t polygonOffset;
625 fog_t fog;
626 uint32_t perspective : 1;
627 uint32_t transformTextures : 1;
628 EGLSurfaceManager* surfaceManager;
629 EGLBufferObjectManager* bufferObjectManager;
Mathias Agopianb1514c92009-04-10 14:24:30 -0700630
631 // copybits is only used if LIBAGL_USE_GRALLOC_COPYBITS is
632 // defined, but it is always present because ogles_context_t is a public
633 // struct that is used by clients of libagl. We want the size and offsets
634 // to stay the same, whether or not LIBAGL_USE_GRALLOC_COPYBITS is defined.
635
636 copybits_context_t copybits;
637
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638 GLenum error;
639
640 static inline ogles_context_t* get() {
641 return getGlThreadSpecific();
642 }
643
644};
645
646}; // namespace gl
647}; // namespace android
648
649#endif // ANDROID_OPENGLES_CONTEXT_H
650