rsdProgramStore.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (C) 2011 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 "rsdCore.h"
  17. #include "rsdProgramStore.h"
  18. #include "rsContext.h"
  19. #include "rsProgramStore.h"
  20. #include <GLES/gl.h>
  21. #include <GLES/glext.h>
  22. using android::renderscript::Context;
  23. using android::renderscript::ProgramStore;
  24. struct DrvProgramStore {
  25. GLenum blendSrc;
  26. GLenum blendDst;
  27. bool blendEnable;
  28. GLenum depthFunc;
  29. bool depthTestEnable;
  30. };
  31. bool rsdProgramStoreInit(const Context *rsc, const ProgramStore *ps) {
  32. DrvProgramStore *drv = (DrvProgramStore *)calloc(1, sizeof(DrvProgramStore));
  33. if (drv == nullptr) {
  34. return false;
  35. }
  36. ps->mHal.drv = drv;
  37. drv->depthTestEnable = true;
  38. switch (ps->mHal.state.depthFunc) {
  39. case RS_DEPTH_FUNC_ALWAYS:
  40. drv->depthTestEnable = false;
  41. drv->depthFunc = GL_ALWAYS;
  42. break;
  43. case RS_DEPTH_FUNC_LESS:
  44. drv->depthFunc = GL_LESS;
  45. break;
  46. case RS_DEPTH_FUNC_LEQUAL:
  47. drv->depthFunc = GL_LEQUAL;
  48. break;
  49. case RS_DEPTH_FUNC_GREATER:
  50. drv->depthFunc = GL_GREATER;
  51. break;
  52. case RS_DEPTH_FUNC_GEQUAL:
  53. drv->depthFunc = GL_GEQUAL;
  54. break;
  55. case RS_DEPTH_FUNC_EQUAL:
  56. drv->depthFunc = GL_EQUAL;
  57. break;
  58. case RS_DEPTH_FUNC_NOTEQUAL:
  59. drv->depthFunc = GL_NOTEQUAL;
  60. break;
  61. default:
  62. ALOGE("Unknown depth function.");
  63. goto error;
  64. }
  65. drv->blendEnable = true;
  66. if ((ps->mHal.state.blendSrc == RS_BLEND_SRC_ONE) &&
  67. (ps->mHal.state.blendDst == RS_BLEND_DST_ZERO)) {
  68. drv->blendEnable = false;
  69. }
  70. switch (ps->mHal.state.blendSrc) {
  71. case RS_BLEND_SRC_ZERO:
  72. drv->blendSrc = GL_ZERO;
  73. break;
  74. case RS_BLEND_SRC_ONE:
  75. drv->blendSrc = GL_ONE;
  76. break;
  77. case RS_BLEND_SRC_DST_COLOR:
  78. drv->blendSrc = GL_DST_COLOR;
  79. break;
  80. case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
  81. drv->blendSrc = GL_ONE_MINUS_DST_COLOR;
  82. break;
  83. case RS_BLEND_SRC_SRC_ALPHA:
  84. drv->blendSrc = GL_SRC_ALPHA;
  85. break;
  86. case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
  87. drv->blendSrc = GL_ONE_MINUS_SRC_ALPHA;
  88. break;
  89. case RS_BLEND_SRC_DST_ALPHA:
  90. drv->blendSrc = GL_DST_ALPHA;
  91. break;
  92. case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
  93. drv->blendSrc = GL_ONE_MINUS_DST_ALPHA;
  94. break;
  95. case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
  96. drv->blendSrc = GL_SRC_ALPHA_SATURATE;
  97. break;
  98. default:
  99. rsc->setError(RS_ERROR_FATAL_DRIVER, "Unknown blend src mode.");
  100. goto error;
  101. }
  102. switch (ps->mHal.state.blendDst) {
  103. case RS_BLEND_DST_ZERO:
  104. drv->blendDst = GL_ZERO;
  105. break;
  106. case RS_BLEND_DST_ONE:
  107. drv->blendDst = GL_ONE;
  108. break;
  109. case RS_BLEND_DST_SRC_COLOR:
  110. drv->blendDst = GL_SRC_COLOR;
  111. break;
  112. case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
  113. drv->blendDst = GL_ONE_MINUS_SRC_COLOR;
  114. break;
  115. case RS_BLEND_DST_SRC_ALPHA:
  116. drv->blendDst = GL_SRC_ALPHA;
  117. break;
  118. case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
  119. drv->blendDst = GL_ONE_MINUS_SRC_ALPHA;
  120. break;
  121. case RS_BLEND_DST_DST_ALPHA:
  122. drv->blendDst = GL_DST_ALPHA;
  123. break;
  124. case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
  125. drv->blendDst = GL_ONE_MINUS_DST_ALPHA;
  126. break;
  127. default:
  128. rsc->setError(RS_ERROR_FATAL_DRIVER, "Unknown blend dst mode.");
  129. goto error;
  130. }
  131. return true;
  132. error:
  133. free(drv);
  134. ps->mHal.drv = nullptr;
  135. return false;
  136. }
  137. void rsdProgramStoreSetActive(const Context *rsc, const ProgramStore *ps) {
  138. DrvProgramStore *drv = (DrvProgramStore *)ps->mHal.drv;
  139. RSD_CALL_GL(glColorMask, ps->mHal.state.colorRWriteEnable,
  140. ps->mHal.state.colorGWriteEnable,
  141. ps->mHal.state.colorBWriteEnable,
  142. ps->mHal.state.colorAWriteEnable);
  143. if (drv->blendEnable) {
  144. RSD_CALL_GL(glEnable, GL_BLEND);
  145. RSD_CALL_GL(glBlendFunc, drv->blendSrc, drv->blendDst);
  146. } else {
  147. RSD_CALL_GL(glDisable, GL_BLEND);
  148. }
  149. if (rsc->mUserSurfaceConfig.depthMin > 0) {
  150. RSD_CALL_GL(glDepthMask, ps->mHal.state.depthWriteEnable);
  151. if (drv->depthTestEnable || ps->mHal.state.depthWriteEnable) {
  152. RSD_CALL_GL(glEnable, GL_DEPTH_TEST);
  153. RSD_CALL_GL(glDepthFunc, drv->depthFunc);
  154. } else {
  155. RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
  156. }
  157. } else {
  158. RSD_CALL_GL(glDepthMask, false);
  159. RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
  160. }
  161. /*
  162. if (rsc->mUserSurfaceConfig.stencilMin > 0) {
  163. } else {
  164. glStencilMask(0);
  165. glDisable(GL_STENCIL_TEST);
  166. }
  167. */
  168. if (ps->mHal.state.ditherEnable) {
  169. RSD_CALL_GL(glEnable, GL_DITHER);
  170. } else {
  171. RSD_CALL_GL(glDisable, GL_DITHER);
  172. }
  173. }
  174. void rsdProgramStoreDestroy(const Context *rsc, const ProgramStore *ps) {
  175. free(ps->mHal.drv);
  176. ps->mHal.drv = nullptr;
  177. }