Composers.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2012 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 <GLES2/gl2.h>
  17. #include <GLES2/gl2ext.h>
  18. #include "Flatland.h"
  19. #include "GLHelper.h"
  20. namespace android {
  21. class Blitter {
  22. public:
  23. bool setUp(GLHelper* helper) {
  24. bool result;
  25. result = helper->getShaderProgram("Blit", &mBlitPgm);
  26. if (!result) {
  27. return false;
  28. }
  29. mPosAttribLoc = glGetAttribLocation(mBlitPgm, "position");
  30. mUVAttribLoc = glGetAttribLocation(mBlitPgm, "uv");
  31. mUVToTexUniformLoc = glGetUniformLocation(mBlitPgm, "uvToTex");
  32. mObjToNdcUniformLoc = glGetUniformLocation(mBlitPgm, "objToNdc");
  33. mBlitSrcSamplerLoc = glGetUniformLocation(mBlitPgm, "blitSrc");
  34. mModColorUniformLoc = glGetUniformLocation(mBlitPgm, "modColor");
  35. return true;
  36. }
  37. bool blit(GLuint texName, const float* texMatrix,
  38. int32_t x, int32_t y, uint32_t w, uint32_t h) {
  39. float modColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  40. return modBlit(texName, texMatrix, modColor, x, y, w, h);
  41. }
  42. bool modBlit(GLuint texName, const float* texMatrix, float* modColor,
  43. int32_t x, int32_t y, uint32_t w, uint32_t h) {
  44. glUseProgram(mBlitPgm);
  45. GLint vp[4];
  46. glGetIntegerv(GL_VIEWPORT, vp);
  47. float screenToNdc[16] = {
  48. 2.0f/float(vp[2]), 0.0f, 0.0f, 0.0f,
  49. 0.0f, -2.0f/float(vp[3]), 0.0f, 0.0f,
  50. 0.0f, 0.0f, 1.0f, 0.0f,
  51. -1.0f, 1.0f, 0.0f, 1.0f,
  52. };
  53. const float pos[] = {
  54. float(x), float(y),
  55. float(x+w), float(y),
  56. float(x), float(y+h),
  57. float(x+w), float(y+h),
  58. };
  59. const float uv[] = {
  60. 0.0f, 0.0f,
  61. 1.0f, 0.0f,
  62. 0.0f, 1.0f,
  63. 1.0f, 1.0f,
  64. };
  65. glVertexAttribPointer(mPosAttribLoc, 2, GL_FLOAT, GL_FALSE, 0, pos);
  66. glVertexAttribPointer(mUVAttribLoc, 2, GL_FLOAT, GL_FALSE, 0, uv);
  67. glEnableVertexAttribArray(mPosAttribLoc);
  68. glEnableVertexAttribArray(mUVAttribLoc);
  69. glUniformMatrix4fv(mObjToNdcUniformLoc, 1, GL_FALSE, screenToNdc);
  70. glUniformMatrix4fv(mUVToTexUniformLoc, 1, GL_FALSE, texMatrix);
  71. glUniform4fv(mModColorUniformLoc, 1, modColor);
  72. glActiveTexture(GL_TEXTURE0);
  73. glBindTexture(GL_TEXTURE_EXTERNAL_OES, texName);
  74. glUniform1i(mBlitSrcSamplerLoc, 0);
  75. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  76. glDisableVertexAttribArray(mPosAttribLoc);
  77. glDisableVertexAttribArray(mUVAttribLoc);
  78. if (glGetError() != GL_NO_ERROR) {
  79. fprintf(stderr, "GL error!\n");
  80. }
  81. return true;
  82. }
  83. private:
  84. GLuint mBlitPgm;
  85. GLint mPosAttribLoc;
  86. GLint mUVAttribLoc;
  87. GLint mUVToTexUniformLoc;
  88. GLint mObjToNdcUniformLoc;
  89. GLint mBlitSrcSamplerLoc;
  90. GLint mModColorUniformLoc;
  91. };
  92. class ComposerBase : public Composer {
  93. public:
  94. virtual ~ComposerBase() {}
  95. virtual bool setUp(const LayerDesc& desc,
  96. GLHelper* helper) {
  97. mLayerDesc = desc;
  98. return setUp(helper);
  99. }
  100. virtual void tearDown() {
  101. }
  102. virtual bool compose(GLuint /*texName*/, const sp<GLConsumer>& /*glc*/) {
  103. return true;
  104. }
  105. protected:
  106. virtual bool setUp(GLHelper* /*helper*/) {
  107. return true;
  108. }
  109. LayerDesc mLayerDesc;
  110. };
  111. Composer* nocomp() {
  112. class NoComp : public ComposerBase {
  113. };
  114. return new NoComp();
  115. }
  116. Composer* opaque() {
  117. class OpaqueComp : public ComposerBase {
  118. virtual bool setUp(GLHelper* helper) {
  119. return mBlitter.setUp(helper);
  120. }
  121. virtual bool compose(GLuint texName, const sp<GLConsumer>& glc) {
  122. float texMatrix[16];
  123. glc->getTransformMatrix(texMatrix);
  124. int32_t x = mLayerDesc.x;
  125. int32_t y = mLayerDesc.y;
  126. int32_t w = mLayerDesc.width;
  127. int32_t h = mLayerDesc.height;
  128. return mBlitter.blit(texName, texMatrix, x, y, w, h);
  129. }
  130. Blitter mBlitter;
  131. };
  132. return new OpaqueComp();
  133. }
  134. Composer* opaqueShrink() {
  135. class OpaqueComp : public ComposerBase {
  136. virtual bool setUp(GLHelper* helper) {
  137. mParity = false;
  138. return mBlitter.setUp(helper);
  139. }
  140. virtual bool compose(GLuint texName, const sp<GLConsumer>& glc) {
  141. float texMatrix[16];
  142. glc->getTransformMatrix(texMatrix);
  143. int32_t x = mLayerDesc.x;
  144. int32_t y = mLayerDesc.y;
  145. int32_t w = mLayerDesc.width;
  146. int32_t h = mLayerDesc.height;
  147. mParity = !mParity;
  148. if (mParity) {
  149. x += w / 128;
  150. y += h / 128;
  151. w -= w / 64;
  152. h -= h / 64;
  153. }
  154. return mBlitter.blit(texName, texMatrix, x, y, w, h);
  155. }
  156. Blitter mBlitter;
  157. bool mParity;
  158. };
  159. return new OpaqueComp();
  160. }
  161. Composer* blend() {
  162. class BlendComp : public ComposerBase {
  163. virtual bool setUp(GLHelper* helper) {
  164. return mBlitter.setUp(helper);
  165. }
  166. virtual bool compose(GLuint texName, const sp<GLConsumer>& glc) {
  167. bool result;
  168. float texMatrix[16];
  169. glc->getTransformMatrix(texMatrix);
  170. float modColor[4] = { .75f, .75f, .75f, .75f };
  171. int32_t x = mLayerDesc.x;
  172. int32_t y = mLayerDesc.y;
  173. int32_t w = mLayerDesc.width;
  174. int32_t h = mLayerDesc.height;
  175. glEnable(GL_BLEND);
  176. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  177. result = mBlitter.modBlit(texName, texMatrix, modColor,
  178. x, y, w, h);
  179. if (!result) {
  180. return false;
  181. }
  182. glDisable(GL_BLEND);
  183. return true;
  184. }
  185. Blitter mBlitter;
  186. };
  187. return new BlendComp();
  188. }
  189. Composer* blendShrink() {
  190. class BlendShrinkComp : public ComposerBase {
  191. virtual bool setUp(GLHelper* helper) {
  192. mParity = false;
  193. return mBlitter.setUp(helper);
  194. }
  195. virtual bool compose(GLuint texName, const sp<GLConsumer>& glc) {
  196. bool result;
  197. float texMatrix[16];
  198. glc->getTransformMatrix(texMatrix);
  199. float modColor[4] = { .75f, .75f, .75f, .75f };
  200. int32_t x = mLayerDesc.x;
  201. int32_t y = mLayerDesc.y;
  202. int32_t w = mLayerDesc.width;
  203. int32_t h = mLayerDesc.height;
  204. mParity = !mParity;
  205. if (mParity) {
  206. x += w / 128;
  207. y += h / 128;
  208. w -= w / 64;
  209. h -= h / 64;
  210. }
  211. glEnable(GL_BLEND);
  212. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  213. result = mBlitter.modBlit(texName, texMatrix, modColor,
  214. x, y, w, h);
  215. if (!result) {
  216. return false;
  217. }
  218. glDisable(GL_BLEND);
  219. return true;
  220. }
  221. Blitter mBlitter;
  222. bool mParity;
  223. };
  224. return new BlendShrinkComp();
  225. }
  226. } // namespace android