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