buffer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* libs/pixelflinger/buffer.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 <assert.h>
  18. #include <android-base/macros.h>
  19. #include "buffer.h"
  20. namespace android {
  21. // ----------------------------------------------------------------------------
  22. static void read_pixel(const surface_t* s, context_t* c,
  23. uint32_t x, uint32_t y, pixel_t* pixel);
  24. static void write_pixel(const surface_t* s, context_t* c,
  25. uint32_t x, uint32_t y, const pixel_t* pixel);
  26. static void readRGB565(const surface_t* s, context_t* c,
  27. uint32_t x, uint32_t y, pixel_t* pixel);
  28. static void readABGR8888(const surface_t* s, context_t* c,
  29. uint32_t x, uint32_t y, pixel_t* pixel);
  30. static uint32_t logic_op(int op, uint32_t s, uint32_t d);
  31. static uint32_t extract(uint32_t v, int h, int l, int bits);
  32. static uint32_t expand(uint32_t v, int sbits, int dbits);
  33. static uint32_t downshift_component(uint32_t in, uint32_t v,
  34. int sh, int sl, int dh, int dl, int ch, int cl, int dither);
  35. // ----------------------------------------------------------------------------
  36. void ggl_init_texture(context_t* c)
  37. {
  38. for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; i++) {
  39. texture_t& t = c->state.texture[i];
  40. t.s_coord = GGL_ONE_TO_ONE;
  41. t.t_coord = GGL_ONE_TO_ONE;
  42. t.s_wrap = GGL_REPEAT;
  43. t.t_wrap = GGL_REPEAT;
  44. t.min_filter = GGL_NEAREST;
  45. t.mag_filter = GGL_NEAREST;
  46. t.env = GGL_MODULATE;
  47. }
  48. c->activeTMU = &(c->state.texture[0]);
  49. }
  50. void ggl_set_surface(context_t* c, surface_t* dst, const GGLSurface* src)
  51. {
  52. dst->width = src->width;
  53. dst->height = src->height;
  54. dst->stride = src->stride;
  55. dst->data = src->data;
  56. dst->format = src->format;
  57. dst->dirty = 1;
  58. if (__builtin_expect(dst->stride < 0, false)) {
  59. const GGLFormat& pixelFormat(c->formats[dst->format]);
  60. const int32_t bpr = -dst->stride * pixelFormat.size;
  61. dst->data += bpr * (dst->height-1);
  62. }
  63. }
  64. static void pick_read_write(surface_t* s)
  65. {
  66. // Choose best reader/writers.
  67. switch (s->format) {
  68. case GGL_PIXEL_FORMAT_RGBA_8888: s->read = readABGR8888; break;
  69. case GGL_PIXEL_FORMAT_RGB_565: s->read = readRGB565; break;
  70. default: s->read = read_pixel; break;
  71. }
  72. s->write = write_pixel;
  73. }
  74. void ggl_pick_texture(context_t* c)
  75. {
  76. for (int i=0 ; i<GGL_TEXTURE_UNIT_COUNT ; ++i) {
  77. surface_t& s = c->state.texture[i].surface;
  78. if ((!c->state.texture[i].enable) || (!s.dirty))
  79. continue;
  80. s.dirty = 0;
  81. pick_read_write(&s);
  82. generated_tex_vars_t& gen = c->generated_vars.texture[i];
  83. gen.width = s.width;
  84. gen.height = s.height;
  85. gen.stride = s.stride;
  86. gen.data = uintptr_t(s.data);
  87. }
  88. }
  89. void ggl_pick_cb(context_t* c)
  90. {
  91. surface_t& s = c->state.buffers.color;
  92. if (s.dirty) {
  93. s.dirty = 0;
  94. pick_read_write(&s);
  95. }
  96. }
  97. // ----------------------------------------------------------------------------
  98. void read_pixel(const surface_t* s, context_t* c,
  99. uint32_t x, uint32_t y, pixel_t* pixel)
  100. {
  101. assert((x < s->width) && (y < s->height));
  102. const GGLFormat* f = &(c->formats[s->format]);
  103. int32_t index = x + (s->stride * y);
  104. uint8_t* const data = s->data + index * f->size;
  105. uint32_t v = 0;
  106. switch (f->size) {
  107. case 1: v = *data; break;
  108. case 2: v = *(uint16_t*)data; break;
  109. case 3: v = (data[2]<<16)|(data[1]<<8)|data[0]; break;
  110. case 4: v = GGL_RGBA_TO_HOST(*(uint32_t*)data); break;
  111. }
  112. for (int i=0 ; i<4 ; i++) {
  113. pixel->s[i] = f->c[i].h - f->c[i].l;
  114. if (pixel->s[i])
  115. pixel->c[i] = extract(v, f->c[i].h, f->c[i].l, f->size*8);
  116. }
  117. }
  118. void readRGB565(const surface_t* s, context_t* /*c*/,
  119. uint32_t x, uint32_t y, pixel_t* pixel)
  120. {
  121. uint16_t v = *(reinterpret_cast<uint16_t*>(s->data) + (x + (s->stride * y)));
  122. pixel->c[0] = 0;
  123. pixel->c[1] = v>>11;
  124. pixel->c[2] = (v>>5)&0x3F;
  125. pixel->c[3] = v&0x1F;
  126. pixel->s[0] = 0;
  127. pixel->s[1] = 5;
  128. pixel->s[2] = 6;
  129. pixel->s[3] = 5;
  130. }
  131. void readABGR8888(const surface_t* s, context_t* /*c*/,
  132. uint32_t x, uint32_t y, pixel_t* pixel)
  133. {
  134. uint32_t v = *(reinterpret_cast<uint32_t*>(s->data) + (x + (s->stride * y)));
  135. v = GGL_RGBA_TO_HOST(v);
  136. pixel->c[0] = v>>24; // A
  137. pixel->c[1] = v&0xFF; // R
  138. pixel->c[2] = (v>>8)&0xFF; // G
  139. pixel->c[3] = (v>>16)&0xFF; // B
  140. pixel->s[0] =
  141. pixel->s[1] =
  142. pixel->s[2] =
  143. pixel->s[3] = 8;
  144. }
  145. void write_pixel(const surface_t* s, context_t* c,
  146. uint32_t x, uint32_t y, const pixel_t* pixel)
  147. {
  148. assert((x < s->width) && (y < s->height));
  149. int dither = -1;
  150. if (c->state.enables & GGL_ENABLE_DITHER) {
  151. dither = c->ditherMatrix[ (x & GGL_DITHER_MASK) +
  152. ((y & GGL_DITHER_MASK)<<GGL_DITHER_ORDER_SHIFT) ];
  153. }
  154. const GGLFormat* f = &(c->formats[s->format]);
  155. int32_t index = x + (s->stride * y);
  156. uint8_t* const data = s->data + index * f->size;
  157. uint32_t mask = 0;
  158. uint32_t v = 0;
  159. for (int i=0 ; i<4 ; i++) {
  160. const int component_mask = 1 << i;
  161. if (f->components>=GGL_LUMINANCE &&
  162. (i==GGLFormat::GREEN || i==GGLFormat::BLUE)) {
  163. // destinations L formats don't have G or B
  164. continue;
  165. }
  166. const int l = f->c[i].l;
  167. const int h = f->c[i].h;
  168. if (h && (c->state.mask.color & component_mask)) {
  169. mask |= (((1<<(h-l))-1)<<l);
  170. uint32_t u = pixel->c[i];
  171. int32_t pixelSize = pixel->s[i];
  172. if (pixelSize < (h-l)) {
  173. u = expand(u, pixelSize, h-l);
  174. pixelSize = h-l;
  175. }
  176. v = downshift_component(v, u, pixelSize, 0, h, l, 0, 0, dither);
  177. }
  178. }
  179. if ((c->state.mask.color != 0xF) ||
  180. (c->state.enables & GGL_ENABLE_LOGIC_OP)) {
  181. uint32_t d = 0;
  182. switch (f->size) {
  183. case 1: d = *data; break;
  184. case 2: d = *(uint16_t*)data; break;
  185. case 3: d = (data[2]<<16)|(data[1]<<8)|data[0]; break;
  186. case 4: d = GGL_RGBA_TO_HOST(*(uint32_t*)data); break;
  187. }
  188. if (c->state.enables & GGL_ENABLE_LOGIC_OP) {
  189. v = logic_op(c->state.logic_op.opcode, v, d);
  190. v &= mask;
  191. }
  192. v |= (d & ~mask);
  193. }
  194. switch (f->size) {
  195. case 1: *data = v; break;
  196. case 2: *(uint16_t*)data = v; break;
  197. case 3:
  198. data[0] = v;
  199. data[1] = v>>8;
  200. data[2] = v>>16;
  201. break;
  202. case 4: *(uint32_t*)data = GGL_HOST_TO_RGBA(v); break;
  203. }
  204. }
  205. static uint32_t logic_op(int op, uint32_t s, uint32_t d)
  206. {
  207. switch(op) {
  208. case GGL_CLEAR: return 0;
  209. case GGL_AND: return s & d;
  210. case GGL_AND_REVERSE: return s & ~d;
  211. case GGL_COPY: return s;
  212. case GGL_AND_INVERTED: return ~s & d;
  213. case GGL_NOOP: return d;
  214. case GGL_XOR: return s ^ d;
  215. case GGL_OR: return s | d;
  216. case GGL_NOR: return ~(s | d);
  217. case GGL_EQUIV: return ~(s ^ d);
  218. case GGL_INVERT: return ~d;
  219. case GGL_OR_REVERSE: return s | ~d;
  220. case GGL_COPY_INVERTED: return ~s;
  221. case GGL_OR_INVERTED: return ~s | d;
  222. case GGL_NAND: return ~(s & d);
  223. case GGL_SET: return ~0;
  224. };
  225. return s;
  226. }
  227. uint32_t ggl_expand(uint32_t v, int sbits, int dbits)
  228. {
  229. return expand(v, sbits, dbits);
  230. }
  231. uint32_t ggl_pack_color(context_t* c, int32_t format,
  232. GGLcolor r, GGLcolor g, GGLcolor b, GGLcolor a)
  233. {
  234. const GGLFormat* f = &(c->formats[format]);
  235. uint32_t p = 0;
  236. const int32_t hbits = GGL_COLOR_BITS;
  237. const int32_t lbits = GGL_COLOR_BITS - 8;
  238. p = downshift_component(p, r, hbits, lbits, f->rh, f->rl, 0, 1, -1);
  239. p = downshift_component(p, g, hbits, lbits, f->gh, f->gl, 0, 1, -1);
  240. p = downshift_component(p, b, hbits, lbits, f->bh, f->bl, 0, 1, -1);
  241. p = downshift_component(p, a, hbits, lbits, f->ah, f->al, 0, 1, -1);
  242. switch (f->size) {
  243. case 1:
  244. p |= p << 8;
  245. FALLTHROUGH_INTENDED;
  246. case 2:
  247. p |= p << 16;
  248. }
  249. return p;
  250. }
  251. // ----------------------------------------------------------------------------
  252. // extract a component from a word
  253. uint32_t extract(uint32_t v, int h, int l, int bits)
  254. {
  255. assert(h);
  256. if (l) {
  257. v >>= l;
  258. }
  259. if (h != bits) {
  260. v &= (1<<(h-l))-1;
  261. }
  262. return v;
  263. }
  264. // expand a component from sbits to dbits
  265. uint32_t expand(uint32_t v, int sbits, int dbits)
  266. {
  267. if (dbits > sbits) {
  268. assert(sbits);
  269. if (sbits==1) {
  270. v = (v<<dbits) - v;
  271. } else {
  272. if (dbits % sbits) {
  273. v <<= (dbits-sbits);
  274. dbits -= sbits;
  275. do {
  276. v |= v>>sbits;
  277. dbits -= sbits;
  278. sbits *= 2;
  279. } while (dbits>0);
  280. } else {
  281. dbits -= sbits;
  282. do {
  283. v |= v<<sbits;
  284. dbits -= sbits;
  285. if (sbits*2 < dbits) {
  286. sbits *= 2;
  287. }
  288. } while (dbits > 0);
  289. }
  290. }
  291. }
  292. return v;
  293. }
  294. // downsample a component from sbits to dbits
  295. // and shift / construct the pixel
  296. uint32_t downshift_component( uint32_t in, uint32_t v,
  297. int sh, int sl, // src
  298. int dh, int dl, // dst
  299. int ch, int cl, // clear
  300. int dither)
  301. {
  302. const int sbits = sh-sl;
  303. const int dbits = dh-dl;
  304. assert(sbits>=dbits);
  305. if (sbits>dbits) {
  306. if (dither>=0) {
  307. v -= (v>>dbits); // fix up
  308. const int shift = (GGL_DITHER_BITS - (sbits-dbits));
  309. if (shift >= 0) v += (dither >> shift) << sl;
  310. else v += (dither << (-shift)) << sl;
  311. } else {
  312. // don't do that right now, so we can reproduce the same
  313. // artifacts we get on ARM (Where we don't do this)
  314. // -> this is not really needed if we don't dither
  315. //if (dBits > 1) { // result already OK if dBits==1
  316. // v -= (v>>dbits); // fix up
  317. // v += 1 << ((sbits-dbits)-1); // rounding
  318. //}
  319. }
  320. }
  321. // we need to clear the high bits of the source
  322. if (ch) {
  323. v <<= 32-sh;
  324. sl += 32-sh;
  325. sh = 32;
  326. }
  327. if (dl) {
  328. if (cl || (sbits>dbits)) {
  329. v >>= sh-dbits;
  330. sl = 0;
  331. sh = dbits;
  332. in |= v<<dl;
  333. } else {
  334. // sbits==dbits and we don't need to clean the lower bits
  335. // so we just have to shift the component to the right location
  336. int shift = dh-sh;
  337. in |= v<<shift;
  338. }
  339. } else {
  340. // destination starts at bit 0
  341. // ie: sh-dh == sh-dbits
  342. int shift = sh-dh;
  343. if (shift > 0) in |= v>>shift;
  344. else if (shift < 0) in |= v<<shift;
  345. else in |= v;
  346. }
  347. return in;
  348. }
  349. // ----------------------------------------------------------------------------
  350. }; // namespace android