context.h 17 KB

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