rsScriptC_LibGL.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "rsContext.h"
  17. #include "rsScriptC.h"
  18. #include "rsMatrix4x4.h"
  19. #include "rsMatrix3x3.h"
  20. #include "rsMatrix2x2.h"
  21. #include "rsMesh.h"
  22. #include "rsgApiStructs.h"
  23. #include "driver/rsdVertexArray.h"
  24. #include "driver/rsdShaderCache.h"
  25. #include "driver/rsdCore.h"
  26. #define GL_GLEXT_PROTOTYPES
  27. #include <GLES/gl.h>
  28. #include <GLES/glext.h>
  29. #include <GLES2/gl2.h>
  30. #include <GLES2/gl2ext.h>
  31. #include <time.h>
  32. namespace android {
  33. namespace renderscript {
  34. //////////////////////////////////////////////////////////////////////////////
  35. // Context
  36. //////////////////////////////////////////////////////////////////////////////
  37. void rsrBindTexture(Context *rsc, ProgramFragment *pf, uint32_t slot, Allocation *a) {
  38. CHECK_OBJ_OR_NULL(a);
  39. CHECK_OBJ(pf);
  40. pf->bindTexture(rsc, slot, a);
  41. }
  42. void rsrBindConstant(Context *rsc, ProgramFragment *pf, uint32_t slot, Allocation *a) {
  43. CHECK_OBJ_OR_NULL(a);
  44. CHECK_OBJ(pf);
  45. pf->bindAllocation(rsc, a, slot);
  46. }
  47. void rsrBindConstant(Context *rsc, ProgramVertex *pv, uint32_t slot, Allocation *a) {
  48. CHECK_OBJ_OR_NULL(a);
  49. CHECK_OBJ(pv);
  50. pv->bindAllocation(rsc, a, slot);
  51. }
  52. void rsrBindSampler(Context *rsc, ProgramFragment *pf, uint32_t slot, Sampler *s) {
  53. CHECK_OBJ_OR_NULL(vs);
  54. CHECK_OBJ(vpf);
  55. pf->bindSampler(rsc, slot, s);
  56. }
  57. void rsrBindProgramStore(Context *rsc, ProgramStore *ps) {
  58. CHECK_OBJ_OR_NULL(ps);
  59. rsc->setProgramStore(ps);
  60. }
  61. void rsrBindProgramFragment(Context *rsc, ProgramFragment *pf) {
  62. CHECK_OBJ_OR_NULL(pf);
  63. rsc->setProgramFragment(pf);
  64. }
  65. void rsrBindProgramVertex(Context *rsc, ProgramVertex *pv) {
  66. CHECK_OBJ_OR_NULL(pv);
  67. rsc->setProgramVertex(pv);
  68. }
  69. void rsrBindProgramRaster(Context *rsc, ProgramRaster *pr) {
  70. CHECK_OBJ_OR_NULL(pr);
  71. rsc->setProgramRaster(pr);
  72. }
  73. void rsrBindFrameBufferObjectColorTarget(Context *rsc, Allocation *a, uint32_t slot) {
  74. CHECK_OBJ(va);
  75. rsc->mFBOCache.bindColorTarget(rsc, a, slot);
  76. rsc->mStateVertex.updateSize(rsc);
  77. }
  78. void rsrBindFrameBufferObjectDepthTarget(Context *rsc, Allocation *a) {
  79. CHECK_OBJ(va);
  80. rsc->mFBOCache.bindDepthTarget(rsc, a);
  81. rsc->mStateVertex.updateSize(rsc);
  82. }
  83. void rsrClearFrameBufferObjectColorTarget(Context *rsc, uint32_t slot) {
  84. rsc->mFBOCache.bindColorTarget(rsc, nullptr, slot);
  85. rsc->mStateVertex.updateSize(rsc);
  86. }
  87. void rsrClearFrameBufferObjectDepthTarget(Context *rsc) {
  88. rsc->mFBOCache.bindDepthTarget(rsc, nullptr);
  89. rsc->mStateVertex.updateSize(rsc);
  90. }
  91. void rsrClearFrameBufferObjectTargets(Context *rsc) {
  92. rsc->mFBOCache.resetAll(rsc);
  93. rsc->mStateVertex.updateSize(rsc);
  94. }
  95. //////////////////////////////////////////////////////////////////////////////
  96. // VP
  97. //////////////////////////////////////////////////////////////////////////////
  98. void rsrVpLoadProjectionMatrix(Context *rsc, const rsc_Matrix *m) {
  99. rsc->getProgramVertex()->setProjectionMatrix(rsc, m);
  100. }
  101. void rsrVpLoadModelMatrix(Context *rsc, const rsc_Matrix *m) {
  102. rsc->getProgramVertex()->setModelviewMatrix(rsc, m);
  103. }
  104. void rsrVpLoadTextureMatrix(Context *rsc, const rsc_Matrix *m) {
  105. rsc->getProgramVertex()->setTextureMatrix(rsc, m);
  106. }
  107. void rsrPfConstantColor(Context *rsc, ProgramFragment *pf,
  108. float r, float g, float b, float a) {
  109. CHECK_OBJ(pf);
  110. pf->setConstantColor(rsc, r, g, b, a);
  111. }
  112. void rsrVpGetProjectionMatrix(Context *rsc, rsc_Matrix *m) {
  113. rsc->getProgramVertex()->getProjectionMatrix(rsc, m);
  114. }
  115. //////////////////////////////////////////////////////////////////////////////
  116. // Drawing
  117. //////////////////////////////////////////////////////////////////////////////
  118. void rsrDrawPath(Context *rsc, Path *sm) {
  119. // Never shipped in a working state
  120. }
  121. void rsrDrawMesh(Context *rsc, Mesh *sm) {
  122. CHECK_OBJ(sm);
  123. if (!rsc->setupCheck()) {
  124. return;
  125. }
  126. sm->render(rsc);
  127. }
  128. void rsrDrawMeshPrimitive(Context *rsc, Mesh *sm, uint32_t primIndex) {
  129. CHECK_OBJ(sm);
  130. if (!rsc->setupCheck()) {
  131. return;
  132. }
  133. sm->renderPrimitive(rsc, primIndex);
  134. }
  135. void rsrDrawMeshPrimitiveRange(Context *rsc, Mesh *sm, uint32_t primIndex,
  136. uint32_t start, uint32_t len) {
  137. CHECK_OBJ(sm);
  138. if (!rsc->setupCheck()) {
  139. return;
  140. }
  141. sm->renderPrimitiveRange(rsc, primIndex, start, len);
  142. }
  143. void rsrMeshComputeBoundingBox(Context *rsc, Mesh *sm,
  144. float *minX, float *minY, float *minZ,
  145. float *maxX, float *maxY, float *maxZ) {
  146. CHECK_OBJ(sm);
  147. sm->computeBBox(rsc);
  148. *minX = sm->mBBoxMin[0];
  149. *minY = sm->mBBoxMin[1];
  150. *minZ = sm->mBBoxMin[2];
  151. *maxX = sm->mBBoxMax[0];
  152. *maxY = sm->mBBoxMax[1];
  153. *maxZ = sm->mBBoxMax[2];
  154. }
  155. //////////////////////////////////////////////////////////////////////////////
  156. //
  157. //////////////////////////////////////////////////////////////////////////////
  158. void rsrColor(Context *rsc, float r, float g, float b, float a) {
  159. ProgramFragment *pf = rsc->getProgramFragment();
  160. pf->setConstantColor(rsc, r, g, b, a);
  161. }
  162. void rsrPrepareClear(Context *rsc) {
  163. rsc->mFBOCache.setup(rsc);
  164. rsc->setupProgramStore();
  165. }
  166. uint32_t rsrGetWidth(Context *rsc) {
  167. return rsc->getWidth();
  168. }
  169. uint32_t rsrGetHeight(Context *rsc) {
  170. return rsc->getHeight();
  171. }
  172. void rsrDrawTextAlloc(Context *rsc, Allocation *a, int x, int y) {
  173. const char *text = (const char *)rsc->mHal.funcs.allocation.lock1D(rsc, a);
  174. size_t allocSize = a->getType()->getPackedSizeBytes();
  175. rsc->mStateFont.renderText(text, allocSize, x, y);
  176. rsc->mHal.funcs.allocation.unlock1D(rsc, a);
  177. }
  178. void rsrDrawText(Context *rsc, const char *text, int x, int y) {
  179. size_t textLen = strlen(text);
  180. rsc->mStateFont.renderText(text, textLen, x, y);
  181. }
  182. static void SetMetrics(Font::Rect *metrics,
  183. int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
  184. if (left) {
  185. *left = metrics->left;
  186. }
  187. if (right) {
  188. *right = metrics->right;
  189. }
  190. if (top) {
  191. *top = metrics->top;
  192. }
  193. if (bottom) {
  194. *bottom = metrics->bottom;
  195. }
  196. }
  197. void rsrMeasureTextAlloc(Context *rsc, Allocation *a,
  198. int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
  199. CHECK_OBJ(a);
  200. const char *text = (const char *)rsc->mHal.funcs.allocation.lock1D(rsc, a);
  201. size_t textLen = a->getType()->getPackedSizeBytes();
  202. Font::Rect metrics;
  203. rsc->mStateFont.measureText(text, textLen, &metrics);
  204. SetMetrics(&metrics, left, right, top, bottom);
  205. rsc->mHal.funcs.allocation.unlock1D(rsc, a);
  206. }
  207. void rsrMeasureText(Context *rsc, const char *text,
  208. int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
  209. size_t textLen = strlen(text);
  210. Font::Rect metrics;
  211. rsc->mStateFont.measureText(text, textLen, &metrics);
  212. SetMetrics(&metrics, left, right, top, bottom);
  213. }
  214. void rsrBindFont(Context *rsc, Font *font) {
  215. CHECK_OBJ(font);
  216. rsi_ContextBindFont(rsc, font);
  217. }
  218. void rsrFontColor(Context *rsc, float r, float g, float b, float a) {
  219. rsc->mStateFont.setFontColor(r, g, b, a);
  220. }
  221. } // namespace renderscript
  222. } // namespace android