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