GLES11RenderEngine.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 2013 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. #include <GLES/gl.h>
  17. #include <GLES/glext.h>
  18. #include <ui/Rect.h>
  19. #include <utils/String8.h>
  20. #include <cutils/compiler.h>
  21. #include <gui/ISurfaceComposer.h>
  22. #include "GLES11RenderEngine.h"
  23. #include "Mesh.h"
  24. #include "Texture.h"
  25. // ---------------------------------------------------------------------------
  26. namespace android {
  27. // ---------------------------------------------------------------------------
  28. GLES11RenderEngine::GLES11RenderEngine() {
  29. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
  30. glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
  31. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  32. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  33. glEnableClientState(GL_VERTEX_ARRAY);
  34. glShadeModel(GL_FLAT);
  35. glDisable(GL_DITHER);
  36. glDisable(GL_CULL_FACE);
  37. const uint16_t protTexData[] = { 0 };
  38. glGenTextures(1, &mProtectedTexName);
  39. glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
  40. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  41. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  42. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  43. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  44. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
  45. GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
  46. }
  47. GLES11RenderEngine::~GLES11RenderEngine() {
  48. }
  49. size_t GLES11RenderEngine::getMaxTextureSize() const {
  50. return mMaxTextureSize;
  51. }
  52. size_t GLES11RenderEngine::getMaxViewportDims() const {
  53. return
  54. mMaxViewportDims[0] < mMaxViewportDims[1] ?
  55. mMaxViewportDims[0] : mMaxViewportDims[1];
  56. }
  57. void GLES11RenderEngine::setViewportAndProjection(
  58. size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, bool yswap,
  59. Transform::orientation_flags rotation) {
  60. glViewport(0, 0, vpw, vph);
  61. glMatrixMode(GL_PROJECTION);
  62. glLoadIdentity();
  63. size_t l = sourceCrop.left;
  64. size_t r = sourceCrop.right;
  65. // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
  66. size_t t = hwh - sourceCrop.top;
  67. size_t b = hwh - sourceCrop.bottom;
  68. if (yswap) {
  69. glOrthof(l, r, t, b, 0, 1);
  70. } else {
  71. glOrthof(l, r, b, t, 0, 1);
  72. }
  73. switch (rotation) {
  74. case Transform::ROT_0:
  75. break;
  76. case Transform::ROT_90:
  77. {
  78. float x1 = (l+r)/2;
  79. float y1 = (t+b)/2;
  80. glTranslatef(x1-y1, x1+y1, 0);
  81. glRotatef(270, 0, 0, 1);
  82. break;
  83. }
  84. case Transform::ROT_180:
  85. {
  86. float x1 = (l+r)/2;
  87. float y1 = (t+b)/2;
  88. glTranslatef(x1*2, y1*2, 0);
  89. glRotatef(180, 0, 0, 1);
  90. break;
  91. }
  92. case Transform::ROT_270:
  93. {
  94. float x1 = (l+r)/2;
  95. float y1 = (t+b)/2;
  96. glTranslatef(x1+y1, y1-x1, 0);
  97. glRotatef(90, 0, 0, 1);
  98. break;
  99. }
  100. default:
  101. break;
  102. }
  103. glMatrixMode(GL_MODELVIEW);
  104. }
  105. void GLES11RenderEngine::setupLayerBlending(
  106. bool premultipliedAlpha, bool opaque, int alpha) {
  107. GLenum combineRGB;
  108. GLenum combineAlpha;
  109. GLenum src0Alpha;
  110. GLfloat envColor[4];
  111. if (CC_UNLIKELY(alpha < 0xFF)) {
  112. // Cv = premultiplied ? Cs*alpha : Cs
  113. // Av = !opaque ? As*alpha : As
  114. combineRGB = premultipliedAlpha ? GL_MODULATE : GL_REPLACE;
  115. combineAlpha = !opaque ? GL_MODULATE : GL_REPLACE;
  116. src0Alpha = GL_CONSTANT;
  117. envColor[0] = alpha * (1.0f / 255.0f);
  118. } else {
  119. // Cv = Cs
  120. // Av = opaque ? 1.0 : As
  121. combineRGB = GL_REPLACE;
  122. combineAlpha = GL_REPLACE;
  123. src0Alpha = opaque ? GL_CONSTANT : GL_TEXTURE;
  124. envColor[0] = 1.0f;
  125. }
  126. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
  127. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, combineRGB);
  128. glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
  129. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
  130. if (combineRGB == GL_MODULATE) {
  131. glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
  132. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
  133. }
  134. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, combineAlpha);
  135. glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, src0Alpha);
  136. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
  137. if (combineAlpha == GL_MODULATE) {
  138. glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE);
  139. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
  140. }
  141. if (combineRGB == GL_MODULATE || src0Alpha == GL_CONSTANT) {
  142. envColor[1] = envColor[0];
  143. envColor[2] = envColor[0];
  144. envColor[3] = envColor[0];
  145. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColor);
  146. }
  147. if (alpha < 0xFF || !opaque) {
  148. glEnable(GL_BLEND);
  149. glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA,
  150. GL_ONE_MINUS_SRC_ALPHA);
  151. } else {
  152. glDisable(GL_BLEND);
  153. }
  154. }
  155. void GLES11RenderEngine::setupDimLayerBlending(int alpha) {
  156. glDisable(GL_TEXTURE_EXTERNAL_OES);
  157. glDisable(GL_TEXTURE_2D);
  158. if (alpha == 0xFF) {
  159. glDisable(GL_BLEND);
  160. } else {
  161. glEnable(GL_BLEND);
  162. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  163. }
  164. glColor4f(0, 0, 0, alpha/255.0f);
  165. }
  166. void GLES11RenderEngine::setupLayerTexturing(const Texture& texture) {
  167. GLuint target = texture.getTextureTarget();
  168. glBindTexture(target, texture.getTextureName());
  169. GLenum filter = GL_NEAREST;
  170. if (texture.getFiltering()) {
  171. filter = GL_LINEAR;
  172. }
  173. glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  174. glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  175. glTexParameterx(target, GL_TEXTURE_MAG_FILTER, filter);
  176. glTexParameterx(target, GL_TEXTURE_MIN_FILTER, filter);
  177. glMatrixMode(GL_TEXTURE);
  178. glLoadMatrixf(texture.getMatrix().asArray());
  179. glMatrixMode(GL_MODELVIEW);
  180. glDisable(GL_TEXTURE_2D);
  181. glEnable(GL_TEXTURE_EXTERNAL_OES);
  182. }
  183. void GLES11RenderEngine::setupLayerBlackedOut() {
  184. glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
  185. glMatrixMode(GL_TEXTURE);
  186. glLoadIdentity();
  187. glMatrixMode(GL_MODELVIEW);
  188. glDisable(GL_TEXTURE_EXTERNAL_OES);
  189. glEnable(GL_TEXTURE_2D);
  190. }
  191. void GLES11RenderEngine::disableTexturing() {
  192. glDisable(GL_TEXTURE_EXTERNAL_OES);
  193. glDisable(GL_TEXTURE_2D);
  194. }
  195. void GLES11RenderEngine::disableBlending() {
  196. glDisable(GL_BLEND);
  197. }
  198. void GLES11RenderEngine::bindImageAsFramebuffer(EGLImageKHR image,
  199. uint32_t* texName, uint32_t* fbName, uint32_t* status,
  200. bool useReadPixels, int reqWidth, int reqHeight) {
  201. GLuint tname, name;
  202. if (!useReadPixels) {
  203. // turn our EGLImage into a texture
  204. glGenTextures(1, &tname);
  205. glBindTexture(GL_TEXTURE_2D, tname);
  206. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
  207. // create a Framebuffer Object to render into
  208. glGenFramebuffersOES(1, &name);
  209. glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
  210. glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
  211. GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
  212. } else {
  213. // since we're going to use glReadPixels() anyways,
  214. // use an intermediate renderbuffer instead
  215. glGenRenderbuffersOES(1, &tname);
  216. glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
  217. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
  218. // create a FBO to render into
  219. glGenFramebuffersOES(1, &name);
  220. glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
  221. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
  222. GL_RENDERBUFFER_OES, tname);
  223. }
  224. *status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
  225. *texName = tname;
  226. *fbName = name;
  227. }
  228. void GLES11RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName,
  229. bool useReadPixels) {
  230. glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
  231. glDeleteFramebuffersOES(1, &fbName);
  232. if (!useReadPixels)
  233. glDeleteTextures(1, &texName);
  234. else
  235. glDeleteRenderbuffersOES(1, &texName);
  236. }
  237. void GLES11RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
  238. glColor4f(r, g, b, a);
  239. glDisable(GL_TEXTURE_EXTERNAL_OES);
  240. glDisable(GL_TEXTURE_2D);
  241. glDisable(GL_BLEND);
  242. }
  243. void GLES11RenderEngine::drawMesh(const Mesh& mesh) {
  244. if (mesh.getTexCoordsSize()) {
  245. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  246. glTexCoordPointer(mesh.getTexCoordsSize(),
  247. GL_FLOAT,
  248. mesh.getByteStride(),
  249. mesh.getTexCoords());
  250. }
  251. glVertexPointer(mesh.getVertexSize(),
  252. GL_FLOAT,
  253. mesh.getByteStride(),
  254. mesh.getPositions());
  255. glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
  256. if (mesh.getTexCoordsSize()) {
  257. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  258. }
  259. }
  260. void GLES11RenderEngine::dump(String8& result) {
  261. RenderEngine::dump(result);
  262. }
  263. // ---------------------------------------------------------------------------
  264. }; // namespace android
  265. // ---------------------------------------------------------------------------
  266. #if defined(__gl2_h_)
  267. #error "don't include gl2/gl2.h in this file"
  268. #endif