1 /* 2 * Copyright (C) 2006 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 17 #ifndef ANDROID_GGL_CONTEXT_H 18 #define ANDROID_GGL_CONTEXT_H 19 20 #include <stdint.h> 21 #include <stddef.h> 22 #include <string.h> 23 #include <sys/types.h> 24 #include <endian.h> 25 26 #include <pixelflinger/pixelflinger.h> 27 #include <private/pixelflinger/ggl_fixed.h> 28 29 namespace android { 30 31 // ---------------------------------------------------------------------------- 32 33 #if BYTE_ORDER == LITTLE_ENDIAN 34 35 inline uint32_t GGL_RGBA_TO_HOST(uint32_t v) { 36 return v; 37 } 38 inline uint32_t GGL_HOST_TO_RGBA(uint32_t v) { 39 return v; 40 } 41 42 #else 43 44 inline uint32_t GGL_RGBA_TO_HOST(uint32_t v) { 45 #if defined(__mips__) && __mips_isa_rev>=2 46 uint32_t r; 47 __asm__("wsbh %0, %1;" 48 "rotr %0, %0, 16" 49 : "=r" (r) 50 : "r" (v) 51 ); 52 return r; 53 #else 54 return (v<<24) | (v>>24) | ((v<<8)&0xff0000) | ((v>>8)&0xff00); 55 #endif 56 } 57 inline uint32_t GGL_HOST_TO_RGBA(uint32_t v) { 58 #if defined(__mips__) && __mips_isa_rev>=2 59 uint32_t r; 60 __asm__("wsbh %0, %1;" 61 "rotr %0, %0, 16" 62 : "=r" (r) 63 : "r" (v) 64 ); 65 return r; 66 #else 67 return (v<<24) | (v>>24) | ((v<<8)&0xff0000) | ((v>>8)&0xff00); 68 #endif 69 } 70 71 #endif 72 73 // ---------------------------------------------------------------------------- 74 75 const int GGL_DITHER_BITS = 6; // dither weights stored on 6 bits 76 const int GGL_DITHER_ORDER_SHIFT= 3; 77 const int GGL_DITHER_ORDER = (1<<GGL_DITHER_ORDER_SHIFT); 78 const int GGL_DITHER_SIZE = GGL_DITHER_ORDER * GGL_DITHER_ORDER; 79 const int GGL_DITHER_MASK = GGL_DITHER_ORDER-1; 80 81 // ---------------------------------------------------------------------------- 82 83 const int GGL_SUBPIXEL_BITS = 4; 84 85 // TRI_FRACTION_BITS defines the number of bits we want to use 86 // for the sub-pixel coordinates during the edge stepping, the 87 // value shouldn't be more than 7, or bad things are going to 88 // happen when drawing large triangles (8 doesn't work because 89 // 32 bit muls will loose the sign bit) 90 91 #define TRI_FRACTION_BITS (GGL_SUBPIXEL_BITS) 92 #define TRI_ONE (1 << TRI_FRACTION_BITS) 93 #define TRI_HALF (1 << (TRI_FRACTION_BITS-1)) 94 #define TRI_FROM_INT(x) ((x) << TRI_FRACTION_BITS) 95 #define TRI_FRAC(x) ((x) & (TRI_ONE-1)) 96 #define TRI_FLOOR(x) ((x) & ~(TRI_ONE-1)) 97 #define TRI_CEIL(x) (((x) + (TRI_ONE-1)) & ~(TRI_ONE-1)) 98 #define TRI_ROUND(x) (((x) + TRI_HALF ) & ~(TRI_ONE-1)) 99 100 #define TRI_ROUDNING (1 << (16 - TRI_FRACTION_BITS - 1)) 101 #define TRI_FROM_FIXED(x) (((x)+TRI_ROUDNING) >> (16-TRI_FRACTION_BITS)) 102 103 #define TRI_SNAP_NEXT_HALF(x) (TRI_CEIL((x)+TRI_HALF) - TRI_HALF) 104 #define TRI_SNAP_PREV_HALF(x) (TRI_CEIL((x)-TRI_HALF) - TRI_HALF) 105 106 // ---------------------------------------------------------------------------- 107 108 const int GGL_COLOR_BITS = 24; 109 110 // To maintain 8-bits color chanels, with a maximum GGLSurface 111 // size of 4096 and GGL_SUBPIXEL_BITS=4, we need 8 + 12 + 4 = 24 bits 112 // for encoding the color iterators 113 114 inline GGLcolor gglFixedToIteratedColor(GGLfixed c) { 115 return (c << 8) - c; 116 } 117 118 // ---------------------------------------------------------------------------- 119 120 template<bool> struct CTA; 121 template<> struct CTA<true> { }; 122 123 #define GGL_CONTEXT(con, c) context_t *con = static_cast<context_t *>(c) 124 #define GGL_OFFSETOF(field) uintptr_t(&(((context_t*)0)->field)) 125 #define GGL_INIT_PROC(p, f) p.f = ggl_ ## f; 126 #define GGL_BETWEEN(x, L, H) (uint32_t((x)-(L)) <= ((H)-(L))) 127 128 #define ggl_likely(x) __builtin_expect(!!(x), 1) 129 #define ggl_unlikely(x) __builtin_expect(!!(x), 0) 130 131 const int GGL_TEXTURE_UNIT_COUNT = 2; 132 const int GGL_TMU_STATE = 0x00000001; 133 const int GGL_CB_STATE = 0x00000002; 134 const int GGL_PIXEL_PIPELINE_STATE = 0x00000004; 135 136 // ---------------------------------------------------------------------------- 137 138 #define GGL_RESERVE_NEEDS(name, l, s) \ 139 const uint32_t GGL_NEEDS_##name##_MASK = (((1LU<<(s))-1)<<l); \ 140 const uint32_t GGL_NEEDS_##name##_SHIFT = (l); 141 142 #define GGL_BUILD_NEEDS(val, name) \ 143 (((val)<<(GGL_NEEDS_##name##_SHIFT)) & GGL_NEEDS_##name##_MASK) 144 145 #define GGL_READ_NEEDS(name, n) \ 146 (uint32_t(n & GGL_NEEDS_##name##_MASK) >> GGL_NEEDS_##name##_SHIFT) 147 148 #define GGL_NEED_MASK(name) (uint32_t(GGL_NEEDS_##name##_MASK)) 149 #define GGL_NEED(name, val) GGL_BUILD_NEEDS(val, name) 150 151 GGL_RESERVE_NEEDS( CB_FORMAT, 0, 6 ) 152 GGL_RESERVE_NEEDS( SHADE, 6, 1 ) 153 GGL_RESERVE_NEEDS( W, 7, 1 ) 154 GGL_RESERVE_NEEDS( BLEND_SRC, 8, 4 ) 155 GGL_RESERVE_NEEDS( BLEND_DST, 12, 4 ) 156 GGL_RESERVE_NEEDS( BLEND_SRCA, 16, 4 ) 157 GGL_RESERVE_NEEDS( BLEND_DSTA, 20, 4 ) 158 GGL_RESERVE_NEEDS( LOGIC_OP, 24, 4 ) 159 GGL_RESERVE_NEEDS( MASK_ARGB, 28, 4 ) 160 161 GGL_RESERVE_NEEDS( P_ALPHA_TEST, 0, 3 ) 162 GGL_RESERVE_NEEDS( P_AA, 3, 1 ) 163 GGL_RESERVE_NEEDS( P_DEPTH_TEST, 4, 3 ) 164 GGL_RESERVE_NEEDS( P_MASK_Z, 7, 1 ) 165 GGL_RESERVE_NEEDS( P_DITHER, 8, 1 ) 166 GGL_RESERVE_NEEDS( P_FOG, 9, 1 ) 167 GGL_RESERVE_NEEDS( P_RESERVED1, 10,22 ) 168 169 GGL_RESERVE_NEEDS( T_FORMAT, 0, 6 ) 170 GGL_RESERVE_NEEDS( T_RESERVED0, 6, 1 ) 171 GGL_RESERVE_NEEDS( T_POT, 7, 1 ) 172 GGL_RESERVE_NEEDS( T_S_WRAP, 8, 2 ) 173 GGL_RESERVE_NEEDS( T_T_WRAP, 10, 2 ) 174 GGL_RESERVE_NEEDS( T_ENV, 12, 3 ) 175 GGL_RESERVE_NEEDS( T_LINEAR, 15, 1 ) 176 177 const int GGL_NEEDS_WRAP_CLAMP_TO_EDGE = 0; 178 const int GGL_NEEDS_WRAP_REPEAT = 1; 179 const int GGL_NEEDS_WRAP_11 = 2; 180 181 inline uint32_t ggl_wrap_to_needs(uint32_t e) { 182 switch (e) { 183 case GGL_CLAMP: return GGL_NEEDS_WRAP_CLAMP_TO_EDGE; 184 case GGL_REPEAT: return GGL_NEEDS_WRAP_REPEAT; 185 } 186 return 0; 187 } 188 189 inline uint32_t ggl_blendfactor_to_needs(uint32_t b) { 190 if (b <= 1) return b; 191 return (b & 0xF)+2; 192 } 193 194 inline uint32_t ggl_needs_to_blendfactor(uint32_t n) { 195 if (n <= 1) return n; 196 return (n - 2) + 0x300; 197 } 198 199 inline uint32_t ggl_env_to_needs(uint32_t e) { 200 switch (e) { 201 case GGL_REPLACE: return 0; 202 case GGL_MODULATE: return 1; 203 case GGL_DECAL: return 2; 204 case GGL_BLEND: return 3; 205 case GGL_ADD: return 4; 206 } 207 return 0; 208 } 209 210 inline uint32_t ggl_needs_to_env(uint32_t n) { 211 const uint32_t envs[] = { GGL_REPLACE, GGL_MODULATE, 212 GGL_DECAL, GGL_BLEND, GGL_ADD }; 213 return envs[n]; 214 215 } 216 217 // ---------------------------------------------------------------------------- 218 219 enum { 220 GGL_ENABLE_BLENDING = 0x00000001, 221 GGL_ENABLE_SMOOTH = 0x00000002, 222 GGL_ENABLE_AA = 0x00000004, 223 GGL_ENABLE_LOGIC_OP = 0x00000008, 224 GGL_ENABLE_ALPHA_TEST = 0x00000010, 225 GGL_ENABLE_SCISSOR_TEST = 0x00000020, 226 GGL_ENABLE_TMUS = 0x00000040, 227 GGL_ENABLE_DEPTH_TEST = 0x00000080, 228 GGL_ENABLE_STENCIL_TEST = 0x00000100, 229 GGL_ENABLE_W = 0x00000200, 230 GGL_ENABLE_DITHER = 0x00000400, 231 GGL_ENABLE_FOG = 0x00000800, 232 GGL_ENABLE_POINT_AA_NICE= 0x00001000 233 }; 234 235 // ---------------------------------------------------------------------------- 236 237 struct needs_filter_t; 238 struct needs_t { 239 inline int match(const needs_filter_t& filter); 240 inline bool operator == (const needs_t& rhs) const { 241 return (n==rhs.n) && 242 (p==rhs.p) && 243 (t[0]==rhs.t[0]) && 244 (t[1]==rhs.t[1]); 245 } 246 inline bool operator != (const needs_t& rhs) const { 247 return !operator == (rhs); 248 } 249 uint32_t n; 250 uint32_t p; 251 uint32_t t[GGL_TEXTURE_UNIT_COUNT]; 252 }; 253 254 inline int compare_type(const needs_t& lhs, const needs_t& rhs) { 255 return memcmp(&lhs, &rhs, sizeof(needs_t)); 256 } 257 258 struct needs_filter_t { 259 needs_t value; 260 needs_t mask; 261 }; 262 263 int needs_t::match(const needs_filter_t& filter) { 264 uint32_t result = 265 ((filter.value.n ^ n) & filter.mask.n) | 266 ((filter.value.p ^ p) & filter.mask.p) | 267 ((filter.value.t[0] ^ t[0]) & filter.mask.t[0]) | 268 ((filter.value.t[1] ^ t[1]) & filter.mask.t[1]); 269 return (result == 0); 270 } 271 272 // ---------------------------------------------------------------------------- 273 274 struct context_t; 275 class Assembly; 276 277 struct blend_state_t { 278 uint32_t src; 279 uint32_t dst; 280 uint32_t src_alpha; 281 uint32_t dst_alpha; 282 uint8_t reserved; 283 uint8_t alpha_separate; 284 uint8_t operation; 285 uint8_t equation; 286 }; 287 288 struct mask_state_t { 289 uint8_t color; 290 uint8_t depth; 291 uint32_t stencil; 292 }; 293 294 struct clear_state_t { 295 GGLclampx r; 296 GGLclampx g; 297 GGLclampx b; 298 GGLclampx a; 299 GGLclampx depth; 300 GGLint stencil; 301 uint32_t colorPacked; 302 uint32_t depthPacked; 303 uint32_t stencilPacked; 304 uint32_t dirty; 305 }; 306 307 struct fog_state_t { 308 uint8_t color[4]; 309 }; 310 311 struct logic_op_state_t { 312 uint16_t opcode; 313 }; 314 315 struct alpha_test_state_t { 316 uint16_t func; 317 GGLcolor ref; 318 }; 319 320 struct depth_test_state_t { 321 uint16_t func; 322 GGLclampx clearValue; 323 }; 324 325 struct scissor_t { 326 uint32_t user_left; 327 uint32_t user_right; 328 uint32_t user_top; 329 uint32_t user_bottom; 330 uint32_t left; 331 uint32_t right; 332 uint32_t top; 333 uint32_t bottom; 334 }; 335 336 struct pixel_t { 337 uint32_t c[4]; 338 uint8_t s[4]; 339 }; 340 341 struct surface_t { 342 union { 343 GGLSurface s; 344 // Keep the following struct field types in line with the corresponding 345 // GGLSurface fields to avoid mismatches leading to errors. 346 struct { 347 GGLsizei reserved; 348 GGLuint width; 349 GGLuint height; 350 GGLint stride; 351 GGLubyte* data; 352 GGLubyte format; 353 GGLubyte dirty; 354 GGLubyte pad[2]; 355 }; 356 }; 357 void (*read) (const surface_t* s, context_t* c, 358 uint32_t x, uint32_t y, pixel_t* pixel); 359 void (*write)(const surface_t* s, context_t* c, 360 uint32_t x, uint32_t y, const pixel_t* pixel); 361 }; 362 363 // ---------------------------------------------------------------------------- 364 365 struct texture_shade_t { 366 union { 367 struct { 368 int32_t is0; 369 int32_t idsdx; 370 int32_t idsdy; 371 int sscale; 372 int32_t it0; 373 int32_t idtdx; 374 int32_t idtdy; 375 int tscale; 376 }; 377 struct { 378 int32_t v; 379 int32_t dx; 380 int32_t dy; 381 int scale; 382 } st[2]; 383 }; 384 }; 385 386 struct texture_iterators_t { 387 // these are not encoded in the same way than in the 388 // texture_shade_t structure 389 union { 390 struct { 391 GGLfixed ydsdy; 392 GGLfixed dsdx; 393 GGLfixed dsdy; 394 int sscale; 395 GGLfixed ydtdy; 396 GGLfixed dtdx; 397 GGLfixed dtdy; 398 int tscale; 399 }; 400 struct { 401 GGLfixed ydvdy; 402 GGLfixed dvdx; 403 GGLfixed dvdy; 404 int scale; 405 } st[2]; 406 }; 407 }; 408 409 struct texture_t { 410 surface_t surface; 411 texture_iterators_t iterators; 412 texture_shade_t shade; 413 uint32_t s_coord; 414 uint32_t t_coord; 415 uint16_t s_wrap; 416 uint16_t t_wrap; 417 uint16_t min_filter; 418 uint16_t mag_filter; 419 uint16_t env; 420 uint8_t env_color[4]; 421 uint8_t enable; 422 uint8_t dirty; 423 }; 424 425 struct raster_t { 426 GGLfixed x; 427 GGLfixed y; 428 }; 429 430 struct framebuffer_t { 431 surface_t color; 432 surface_t read; 433 surface_t depth; 434 surface_t stencil; 435 int16_t *coverage; 436 size_t coverageBufferSize; 437 }; 438 439 // ---------------------------------------------------------------------------- 440 441 struct iterators_t { 442 int32_t xl; 443 int32_t xr; 444 int32_t y; 445 GGLcolor ydady; 446 GGLcolor ydrdy; 447 GGLcolor ydgdy; 448 GGLcolor ydbdy; 449 GGLfixed ydzdy; 450 GGLfixed ydwdy; 451 GGLfixed ydfdy; 452 }; 453 454 struct shade_t { 455 GGLcolor a0; 456 GGLcolor dadx; 457 GGLcolor dady; 458 GGLcolor r0; 459 GGLcolor drdx; 460 GGLcolor drdy; 461 GGLcolor g0; 462 GGLcolor dgdx; 463 GGLcolor dgdy; 464 GGLcolor b0; 465 GGLcolor dbdx; 466 GGLcolor dbdy; 467 uint32_t z0; 468 GGLfixed32 dzdx; 469 GGLfixed32 dzdy; 470 GGLfixed w0; 471 GGLfixed dwdx; 472 GGLfixed dwdy; 473 uint32_t f0; 474 GGLfixed dfdx; 475 GGLfixed dfdy; 476 }; 477 478 // these are used in the generated code 479 // we use this mirror structure to improve 480 // data locality in the pixel pipeline 481 struct generated_tex_vars_t { 482 uint32_t width; 483 uint32_t height; 484 uint32_t stride; 485 uintptr_t data; 486 int32_t dsdx; 487 int32_t dtdx; 488 int32_t spill[2]; 489 }; 490 491 struct generated_vars_t { 492 struct { 493 int32_t c; 494 int32_t dx; 495 } argb[4]; 496 int32_t aref; 497 int32_t dzdx; 498 int32_t zbase; 499 int32_t f; 500 int32_t dfdx; 501 int32_t spill[3]; 502 generated_tex_vars_t texture[GGL_TEXTURE_UNIT_COUNT]; 503 int32_t rt; 504 int32_t lb; 505 }; 506 507 // ---------------------------------------------------------------------------- 508 509 struct state_t { 510 framebuffer_t buffers; 511 texture_t texture[GGL_TEXTURE_UNIT_COUNT]; 512 scissor_t scissor; 513 raster_t raster; 514 blend_state_t blend; 515 alpha_test_state_t alpha_test; 516 depth_test_state_t depth_test; 517 mask_state_t mask; 518 clear_state_t clear; 519 fog_state_t fog; 520 logic_op_state_t logic_op; 521 uint32_t enables; 522 uint32_t enabled_tmu; 523 needs_t needs; 524 }; 525 526 // ---------------------------------------------------------------------------- 527 528 struct context_t { 529 GGLContext procs; 530 state_t state; 531 shade_t shade; 532 iterators_t iterators; 533 generated_vars_t generated_vars __attribute__((aligned(32))); 534 uint8_t ditherMatrix[GGL_DITHER_SIZE] __attribute__((aligned(32))); 535 uint32_t packed; 536 uint32_t packed8888; 537 const GGLFormat* formats; 538 uint32_t dirty; 539 texture_t* activeTMU; 540 uint32_t activeTMUIndex; 541 542 void (*init_y)(context_t* c, int32_t y); 543 void (*step_y)(context_t* c); 544 void (*scanline)(context_t* c); 545 void (*span)(context_t* c); 546 void (*rect)(context_t* c, size_t yc); 547 548 void* base; 549 Assembly* scanline_as; 550 GGLenum error; 551 }; 552 553 // ---------------------------------------------------------------------------- 554 555 void ggl_init_context(context_t* context); 556 void ggl_uninit_context(context_t* context); 557 void ggl_error(context_t* c, GGLenum error); 558 int64_t ggl_system_time(); 559 560 // ---------------------------------------------------------------------------- 561 562 }; 563 564 #endif // ANDROID_GGL_CONTEXT_H 565 566