raster.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* libs/pixelflinger/raster.cpp
  2. **
  3. ** Copyright 2006, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include <string.h>
  18. #include "raster.h"
  19. #include "trap.h"
  20. namespace android {
  21. static void ggl_rasterPos2x(void* con, GGLfixed x, GGLfixed y);
  22. static void ggl_rasterPos2i(void* con, GGLint x, GGLint y);
  23. static void ggl_copyPixels(void* con, GGLint xs, GGLint ys,
  24. GGLsizei width, GGLsizei height, GGLenum type);
  25. void ggl_init_raster(context_t* c)
  26. {
  27. GGLContext& procs = *(GGLContext*)c;
  28. GGL_INIT_PROC(procs, copyPixels);
  29. GGL_INIT_PROC(procs, rasterPos2x);
  30. GGL_INIT_PROC(procs, rasterPos2i);
  31. }
  32. void ggl_rasterPos2x(void* con, GGLfixed x, GGLfixed y)
  33. {
  34. GGL_CONTEXT(c, con);
  35. // raster pos should be processed just like glVertex
  36. c->state.raster.x = x;
  37. c->state.raster.y = y;
  38. }
  39. void ggl_rasterPos2i(void* con, GGLint x, GGLint y)
  40. {
  41. ggl_rasterPos2x(con, gglIntToFixed(x), gglIntToFixed(y));
  42. }
  43. void ggl_copyPixels(void* con, GGLint xs, GGLint ys,
  44. GGLsizei width, GGLsizei height, GGLenum /*type*/)
  45. {
  46. GGL_CONTEXT(c, con);
  47. // color-buffer
  48. surface_t* cb = &(c->state.buffers.color);
  49. // undefined behaviour if we try to copy from outside the surface
  50. if (uint32_t(xs) > cb->width)
  51. return;
  52. if (uint32_t(ys) > cb->height)
  53. return;
  54. if (uint32_t(xs + width) > cb->width)
  55. return;
  56. if (uint32_t(ys + height) > cb->height)
  57. return;
  58. // copy to current raster position
  59. GGLint xd = gglFixedToIntRound(c->state.raster.x);
  60. GGLint yd = gglFixedToIntRound(c->state.raster.y);
  61. // clip to scissor
  62. if (xd < GGLint(c->state.scissor.left)) {
  63. GGLint offset = GGLint(c->state.scissor.left) - xd;
  64. xd = GGLint(c->state.scissor.left);
  65. xs += offset;
  66. width -= offset;
  67. }
  68. if (yd < GGLint(c->state.scissor.top)) {
  69. GGLint offset = GGLint(c->state.scissor.top) - yd;
  70. yd = GGLint(c->state.scissor.top);
  71. ys += offset;
  72. height -= offset;
  73. }
  74. if ((xd + width) > GGLint(c->state.scissor.right)) {
  75. width = GGLint(c->state.scissor.right) - xd;
  76. }
  77. if ((yd + height) > GGLint(c->state.scissor.bottom)) {
  78. height = GGLint(c->state.scissor.bottom) - yd;
  79. }
  80. if (width<=0 || height<=0) {
  81. return; // nothing to copy
  82. }
  83. if (xs==xd && ys==yd) {
  84. // nothing to do, but be careful, this might not be true when we support
  85. // gglPixelTransfer, gglPixelMap and gglPixelZoom
  86. return;
  87. }
  88. const GGLFormat* fp = &(c->formats[cb->format]);
  89. uint8_t* src = reinterpret_cast<uint8_t*>(cb->data)
  90. + (xs + (cb->stride * ys)) * fp->size;
  91. uint8_t* dst = reinterpret_cast<uint8_t*>(cb->data)
  92. + (xd + (cb->stride * yd)) * fp->size;
  93. const size_t bpr = cb->stride * fp->size;
  94. const size_t rowsize = width * fp->size;
  95. size_t yc = height;
  96. if (ys < yd) {
  97. // bottom to top
  98. src += height * bpr;
  99. dst += height * bpr;
  100. do {
  101. dst -= bpr;
  102. src -= bpr;
  103. memcpy(dst, src, rowsize);
  104. } while (--yc);
  105. } else {
  106. if (ys == yd) {
  107. // might be right to left
  108. do {
  109. memmove(dst, src, rowsize);
  110. dst += bpr;
  111. src += bpr;
  112. } while (--yc);
  113. } else {
  114. // top to bottom
  115. do {
  116. memcpy(dst, src, rowsize);
  117. dst += bpr;
  118. src += bpr;
  119. } while (--yc);
  120. }
  121. }
  122. }
  123. }; // namespace android
  124. using namespace android;
  125. GGLint gglBitBlit(GGLContext* con, int tmu, GGLint crop[4], GGLint where[4])
  126. {
  127. GGL_CONTEXT(c, (void*)con);
  128. GGLint x = where[0];
  129. GGLint y = where[1];
  130. GGLint w = where[2];
  131. GGLint h = where[3];
  132. // exclsively enable this tmu
  133. c->procs.activeTexture(c, tmu);
  134. c->procs.disable(c, GGL_W_LERP);
  135. uint32_t tmus = 1UL<<tmu;
  136. if (c->state.enabled_tmu != tmus) {
  137. c->activeTMU->enable = 1;
  138. c->state.enabled_tmu = tmus;
  139. c->state.enables |= GGL_ENABLE_TMUS;
  140. ggl_state_changed(c, GGL_TMU_STATE);
  141. }
  142. const GGLint Wcr = crop[2];
  143. const GGLint Hcr = crop[3];
  144. if ((w == Wcr) && (h == Hcr)) {
  145. c->procs.texGeni(c, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
  146. c->procs.texGeni(c, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
  147. const GGLint Ucr = crop[0];
  148. const GGLint Vcr = crop[1];
  149. const GGLint s0 = Ucr - x;
  150. const GGLint t0 = Vcr - y;
  151. c->procs.texCoord2i(c, s0, t0);
  152. c->procs.recti(c, x, y, x+w, y+h);
  153. } else {
  154. int32_t texcoords[8];
  155. x = gglIntToFixed(x);
  156. y = gglIntToFixed(y);
  157. // we CLAMP here, which works with premultiplied (s,t)
  158. c->procs.texParameteri(c, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_S, GGL_CLAMP);
  159. c->procs.texParameteri(c, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_T, GGL_CLAMP);
  160. c->procs.texGeni(c, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
  161. c->procs.texGeni(c, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC);
  162. const GGLint Ucr = crop[0] << 16;
  163. const GGLint Vcr = crop[1] << 16;
  164. const GGLint Wcr = crop[2] << 16;
  165. const GGLint Hcr = crop[3] << 16;
  166. // computes texture coordinates (pre-multiplied)
  167. int32_t dsdx = Wcr / w; // dsdx = ((Wcr/w)/Wt)*Wt
  168. int32_t dtdy = Hcr / h; // dtdy = ((Hcr/h)/Ht)*Ht
  169. int32_t s0 = Ucr - gglMulx(dsdx, x); // s0 = Ucr - x * dsdx
  170. int32_t t0 = Vcr - gglMulx(dtdy, y); // t0 = Vcr - y * dtdy
  171. texcoords[0] = s0;
  172. texcoords[1] = dsdx;
  173. texcoords[2] = 0;
  174. texcoords[3] = t0;
  175. texcoords[4] = 0;
  176. texcoords[5] = dtdy;
  177. texcoords[6] = 0;
  178. texcoords[7] = 0;
  179. c->procs.texCoordGradScale8xv(c, tmu, texcoords);
  180. c->procs.recti(c,
  181. gglFixedToIntRound(x),
  182. gglFixedToIntRound(y),
  183. gglFixedToIntRound(x)+w,
  184. gglFixedToIntRound(y)+h);
  185. }
  186. return 0;
  187. }