1 2 /* 3 * Copyright 2010 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 11 #ifndef GrTypes_DEFINED 12 #define GrTypes_DEFINED 13 14 #include "SkTypes.h" 15 #include "GrConfig.h" 16 #include "SkMath.h" 17 18 //#define SK_SUPPORT_LEGACY_GRTYPES 19 20 //////////////////////////////////////////////////////////////////////////////// 21 22 /** 23 * Defines overloaded bitwise operators to make it easier to use an enum as a 24 * bitfield. 25 */ 26 #define GR_MAKE_BITFIELD_OPS(X) \ 27 inline X operator | (X a, X b) { \ 28 return (X) (+a | +b); \ 29 } \ 30 \ 31 inline X operator & (X a, X b) { \ 32 return (X) (+a & +b); \ 33 } \ 34 template <typename T> \ 35 inline X operator & (T a, X b) { \ 36 return (X) (+a & +b); \ 37 } \ 38 template <typename T> \ 39 inline X operator & (X a, T b) { \ 40 return (X) (+a & +b); \ 41 } \ 42 43 #define GR_DECL_BITFIELD_OPS_FRIENDS(X) \ 44 friend X operator | (X a, X b); \ 45 \ 46 friend X operator & (X a, X b); \ 47 \ 48 template <typename T> \ 49 friend X operator & (T a, X b); \ 50 \ 51 template <typename T> \ 52 friend X operator & (X a, T b); \ 53 //////////////////////////////////////////////////////////////////////////////// 54 55 #ifdef SK_SUPPORT_LEGACY_GRTYPES 56 57 /** 58 * Macro to round n up to the next multiple of 4, or return it unchanged if 59 * n is already a multiple of 4 60 */ 61 #define GrALIGN4(n) SkAlign4(n) 62 #define GrIsALIGN4(n) SkIsAlign4(n) 63 64 template <typename T> const T& GrMin(const T& a, const T& b) { 65 return (a < b) ? a : b; 66 } 67 68 template <typename T> const T& GrMax(const T& a, const T& b) { 69 return (b < a) ? a : b; 70 } 71 72 /** 73 * Count elements in an array 74 */ 75 #define GR_ARRAY_COUNT(array) SK_ARRAY_COUNT(array) 76 77 /** 78 * 16.16 fixed point type 79 */ 80 typedef int32_t GrFixed; 81 82 #ifdef SK_DEBUG 83 84 static inline int16_t GrToS16(intptr_t x) { 85 SkASSERT((int16_t)x == x); 86 return (int16_t)x; 87 } 88 89 #else 90 91 #define GrToS16(x) x 92 93 #endif 94 95 #endif 96 97 // compile time versions of min/max 98 #define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b)) 99 #define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a)) 100 101 /** 102 * divide, rounding up 103 */ 104 static inline int32_t GrIDivRoundUp(int x, int y) { 105 SkASSERT(y > 0); 106 return (x + (y-1)) / y; 107 } 108 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) { 109 return (x + (y-1)) / y; 110 } 111 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) { 112 return (x + (y-1)) / y; 113 } 114 115 // compile time, evaluates Y multiple times 116 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y)) 117 118 /** 119 * align up 120 */ 121 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) { 122 return GrUIDivRoundUp(x, alignment) * alignment; 123 } 124 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) { 125 return GrSizeDivRoundUp(x, alignment) * alignment; 126 } 127 128 // compile time, evaluates A multiple times 129 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A)) 130 131 /** 132 * amount of pad needed to align up 133 */ 134 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) { 135 return (alignment - x % alignment) % alignment; 136 } 137 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) { 138 return (alignment - x % alignment) % alignment; 139 } 140 141 /** 142 * align down 143 */ 144 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) { 145 return (x / alignment) * alignment; 146 } 147 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) { 148 return (x / alignment) * alignment; 149 } 150 151 /////////////////////////////////////////////////////////////////////////////// 152 153 /** 154 * Return the next power of 2 >= n. 155 */ 156 static inline uint32_t GrNextPow2(uint32_t n) { 157 return n ? (1 << (32 - SkCLZ(n - 1))) : 1; 158 } 159 160 static inline int GrNextPow2(int n) { 161 SkASSERT(n >= 0); // this impl only works for non-neg. 162 return n ? (1 << (32 - SkCLZ(n - 1))) : 1; 163 } 164 165 /////////////////////////////////////////////////////////////////////////////// 166 167 /** 168 * Possible 3D APIs that may be used by Ganesh. 169 */ 170 enum GrBackend { 171 kOpenGL_GrBackend, 172 }; 173 174 /** 175 * Backend-specific 3D context handle 176 * GrGLInterface* for OpenGL. If NULL will use the default GL interface. 177 */ 178 typedef intptr_t GrBackendContext; 179 180 /////////////////////////////////////////////////////////////////////////////// 181 182 /** 183 * Geometric primitives used for drawing. 184 */ 185 enum GrPrimitiveType { 186 kTriangles_GrPrimitiveType, 187 kTriangleStrip_GrPrimitiveType, 188 kTriangleFan_GrPrimitiveType, 189 kPoints_GrPrimitiveType, 190 kLines_GrPrimitiveType, // 1 pix wide only 191 kLineStrip_GrPrimitiveType // 1 pix wide only 192 }; 193 194 static inline bool GrIsPrimTypeLines(GrPrimitiveType type) { 195 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type; 196 } 197 198 static inline bool GrIsPrimTypeTris(GrPrimitiveType type) { 199 return kTriangles_GrPrimitiveType == type || 200 kTriangleStrip_GrPrimitiveType == type || 201 kTriangleFan_GrPrimitiveType == type; 202 } 203 204 /** 205 * Coeffecients for alpha-blending. 206 */ 207 enum GrBlendCoeff { 208 kInvalid_GrBlendCoeff = -1, 209 210 kZero_GrBlendCoeff, //<! 0 211 kOne_GrBlendCoeff, //<! 1 212 kSC_GrBlendCoeff, //<! src color 213 kISC_GrBlendCoeff, //<! one minus src color 214 kDC_GrBlendCoeff, //<! dst color 215 kIDC_GrBlendCoeff, //<! one minus dst color 216 kSA_GrBlendCoeff, //<! src alpha 217 kISA_GrBlendCoeff, //<! one minus src alpha 218 kDA_GrBlendCoeff, //<! dst alpha 219 kIDA_GrBlendCoeff, //<! one minus dst alpha 220 kConstC_GrBlendCoeff, //<! constant color 221 kIConstC_GrBlendCoeff, //<! one minus constant color 222 kConstA_GrBlendCoeff, //<! constant color alpha 223 kIConstA_GrBlendCoeff, //<! one minus constant color alpha 224 225 kPublicGrBlendCoeffCount 226 }; 227 228 /** 229 * Formats for masks, used by the font cache. 230 * Important that these are 0-based. 231 */ 232 enum GrMaskFormat { 233 kA8_GrMaskFormat, //!< 1-byte per pixel 234 kA565_GrMaskFormat, //!< 2-bytes per pixel 235 kA888_GrMaskFormat, //!< 4-bytes per pixel 236 kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format 237 238 kLast_GrMaskFormat = kARGB_GrMaskFormat 239 }; 240 static const int kMaskFormatCount = kLast_GrMaskFormat + 1; 241 242 /** 243 * Return the number of bytes-per-pixel for the specified mask format. 244 */ 245 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) { 246 SkASSERT((unsigned)format <= 3); 247 // kA8 (0) -> 1 248 // kA565 (1) -> 2 249 // kA888 (2) -> 4 250 // kARGB (3) -> 4 251 static const int sBytesPerPixel[] = { 1, 2, 4, 4 }; 252 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, array_size_mismatch); 253 254 return sBytesPerPixel[(int) format]; 255 } 256 257 /** 258 * Pixel configurations. 259 */ 260 enum GrPixelConfig { 261 kUnknown_GrPixelConfig, 262 kAlpha_8_GrPixelConfig, 263 kIndex_8_GrPixelConfig, 264 kRGB_565_GrPixelConfig, 265 /** 266 * Premultiplied 267 */ 268 kRGBA_4444_GrPixelConfig, 269 /** 270 * Premultiplied. Byte order is r,g,b,a. 271 */ 272 kRGBA_8888_GrPixelConfig, 273 /** 274 * Premultiplied. Byte order is b,g,r,a. 275 */ 276 kBGRA_8888_GrPixelConfig, 277 /** 278 * ETC1 Compressed Data 279 */ 280 kETC1_GrPixelConfig, 281 /** 282 * LATC/RGTC/3Dc/BC4 Compressed Data 283 */ 284 kLATC_GrPixelConfig, 285 286 kLast_GrPixelConfig = kLATC_GrPixelConfig 287 }; 288 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1; 289 290 // Aliases for pixel configs that match skia's byte order. 291 #ifndef SK_CPU_LENDIAN 292 #error "Skia gpu currently assumes little endian" 293 #endif 294 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) 295 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig; 296 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) 297 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig; 298 #else 299 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format." 300 #endif 301 302 // Returns true if the pixel config is a GPU-specific compressed format 303 // representation. 304 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) { 305 switch (config) { 306 case kETC1_GrPixelConfig: 307 case kLATC_GrPixelConfig: 308 return true; 309 default: 310 return false; 311 } 312 } 313 314 // Returns true if the pixel config is 32 bits per pixel 315 static inline bool GrPixelConfigIs8888(GrPixelConfig config) { 316 switch (config) { 317 case kRGBA_8888_GrPixelConfig: 318 case kBGRA_8888_GrPixelConfig: 319 return true; 320 default: 321 return false; 322 } 323 } 324 325 // Takes a config and returns the equivalent config with the R and B order 326 // swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig 327 static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) { 328 switch (config) { 329 case kBGRA_8888_GrPixelConfig: 330 return kRGBA_8888_GrPixelConfig; 331 case kRGBA_8888_GrPixelConfig: 332 return kBGRA_8888_GrPixelConfig; 333 default: 334 return kUnknown_GrPixelConfig; 335 } 336 } 337 338 static inline size_t GrBytesPerPixel(GrPixelConfig config) { 339 switch (config) { 340 case kAlpha_8_GrPixelConfig: 341 case kIndex_8_GrPixelConfig: 342 return 1; 343 case kRGB_565_GrPixelConfig: 344 case kRGBA_4444_GrPixelConfig: 345 return 2; 346 case kRGBA_8888_GrPixelConfig: 347 case kBGRA_8888_GrPixelConfig: 348 return 4; 349 default: 350 return 0; 351 } 352 } 353 354 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) { 355 switch (config) { 356 case kETC1_GrPixelConfig: 357 case kRGB_565_GrPixelConfig: 358 return true; 359 default: 360 return false; 361 } 362 } 363 364 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) { 365 switch (config) { 366 case kLATC_GrPixelConfig: 367 case kAlpha_8_GrPixelConfig: 368 return true; 369 default: 370 return false; 371 } 372 } 373 374 /** 375 * Optional bitfield flags that can be passed to createTexture. 376 */ 377 enum GrTextureFlags { 378 kNone_GrTextureFlags = 0x0, 379 /** 380 * Creates a texture that can be rendered to as a GrRenderTarget. Use 381 * GrTexture::asRenderTarget() to access. 382 */ 383 kRenderTarget_GrTextureFlagBit = 0x1, 384 /** 385 * By default all render targets have an associated stencil buffer that 386 * may be required for path filling. This flag overrides stencil buffer 387 * creation. 388 * MAKE THIS PRIVATE? 389 */ 390 kNoStencil_GrTextureFlagBit = 0x2, 391 /** 392 * Hint that the CPU may modify this texture after creation. 393 */ 394 kDynamicUpdate_GrTextureFlagBit = 0x4, 395 /** 396 * Indicates that all allocations (color buffer, FBO completeness, etc) 397 * should be verified. 398 */ 399 kCheckAllocation_GrTextureFlagBit = 0x8, 400 401 kDummy_GrTextureFlagBit, 402 kLastPublic_GrTextureFlagBit = kDummy_GrTextureFlagBit-1, 403 }; 404 405 GR_MAKE_BITFIELD_OPS(GrTextureFlags) 406 407 enum { 408 /** 409 * For Index8 pixel config, the colortable must be 256 entries 410 */ 411 kGrColorTableSize = 256 * 4 //sizeof(GrColor) 412 }; 413 414 /** 415 * Some textures will be stored such that the upper and left edges of the content meet at the 416 * the origin (in texture coord space) and for other textures the lower and left edges meet at 417 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets 418 * to BottomLeft. 419 */ 420 421 enum GrSurfaceOrigin { 422 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed 423 kTopLeft_GrSurfaceOrigin, 424 kBottomLeft_GrSurfaceOrigin, 425 }; 426 427 /** 428 * Describes a texture to be created. 429 */ 430 struct GrTextureDesc { 431 GrTextureDesc() 432 : fFlags(kNone_GrTextureFlags) 433 , fOrigin(kDefault_GrSurfaceOrigin) 434 , fWidth(0) 435 , fHeight(0) 436 , fConfig(kUnknown_GrPixelConfig) 437 , fSampleCnt(0) { 438 } 439 440 GrTextureFlags fFlags; //!< bitfield of TextureFlags 441 GrSurfaceOrigin fOrigin; //!< origin of the texture 442 int fWidth; //!< Width of the texture 443 int fHeight; //!< Height of the texture 444 445 /** 446 * Format of source data of the texture. Not guaranteed to be the same as 447 * internal format used by 3D API. 448 */ 449 GrPixelConfig fConfig; 450 451 /** 452 * The number of samples per pixel or 0 to disable full scene AA. This only 453 * applies if the kRenderTarget_GrTextureFlagBit is set. The actual number 454 * of samples may not exactly match the request. The request will be rounded 455 * up to the next supported sample count, or down if it is larger than the 456 * max supported count. 457 */ 458 int fSampleCnt; 459 }; 460 461 /** 462 * GrCacheID is used create and find cached GrResources (e.g. GrTextures). The ID has two parts: 463 * the domain and the key. Domains simply allow multiple clients to use 0-based indices as their 464 * cache key without colliding. The key uniquely identifies a GrResource within the domain. 465 * Users of the cache must obtain a domain via GenerateDomain(). 466 */ 467 struct GrCacheID { 468 public: 469 typedef uint8_t Domain; 470 471 struct Key { 472 union { 473 uint8_t fData8[16]; 474 uint32_t fData32[4]; 475 uint64_t fData64[2]; 476 }; 477 }; 478 479 /** 480 * A default cache ID is invalid; a set method must be called before the object is used. 481 */ 482 GrCacheID() { fDomain = kInvalid_Domain; } 483 484 /** 485 * Initialize the cache ID to a domain and key. 486 */ 487 GrCacheID(Domain domain, const Key& key) { 488 SkASSERT(kInvalid_Domain != domain); 489 this->reset(domain, key); 490 } 491 492 void reset(Domain domain, const Key& key) { 493 fDomain = domain; 494 memcpy(&fKey, &key, sizeof(Key)); 495 } 496 497 /** Has this been initialized to a valid domain */ 498 bool isValid() const { return kInvalid_Domain != fDomain; } 499 500 const Key& getKey() const { SkASSERT(this->isValid()); return fKey; } 501 Domain getDomain() const { SkASSERT(this->isValid()); return fDomain; } 502 503 /** Creates a new unique ID domain. */ 504 static Domain GenerateDomain(); 505 506 private: 507 Key fKey; 508 Domain fDomain; 509 510 static const Domain kInvalid_Domain = 0; 511 }; 512 513 /** 514 * Clips are composed from these objects. 515 */ 516 enum GrClipType { 517 kRect_ClipType, 518 kPath_ClipType 519 }; 520 521 /////////////////////////////////////////////////////////////////////////////// 522 523 // opaque type for 3D API object handles 524 typedef intptr_t GrBackendObject; 525 526 /** 527 * Gr can wrap an existing texture created by the client with a GrTexture 528 * object. The client is responsible for ensuring that the texture lives at 529 * least as long as the GrTexture object wrapping it. We require the client to 530 * explicitly provide information about the texture, such as width, height, 531 * and pixel config, rather than querying the 3D APIfor these values. We expect 532 * these to be immutable even if the 3D API doesn't require this (OpenGL). 533 * 534 * Textures that are also render targets are supported as well. Gr will manage 535 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for 536 * Gr to draw into the render target. To access the render target object 537 * call GrTexture::asRenderTarget(). 538 * 539 * If in addition to the render target flag, the caller also specifies a sample 540 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto- 541 * resolves when it reads from the texture. The client can explictly resolve 542 * using the GrRenderTarget interface. 543 * 544 * Note: These flags currently form a subset of GrTexture's flags. 545 */ 546 547 enum GrBackendTextureFlags { 548 /** 549 * No flags enabled 550 */ 551 kNone_GrBackendTextureFlag = kNone_GrTextureFlags, 552 /** 553 * Indicates that the texture is also a render target, and thus should have 554 * a GrRenderTarget object. 555 * 556 * D3D (future): client must have created the texture with flags that allow 557 * it to be used as a render target. 558 */ 559 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrTextureFlagBit, 560 }; 561 GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags) 562 563 struct GrBackendTextureDesc { 564 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); } 565 GrBackendTextureFlags fFlags; 566 GrSurfaceOrigin fOrigin; 567 int fWidth; //<! width in pixels 568 int fHeight; //<! height in pixels 569 GrPixelConfig fConfig; //<! color format 570 /** 571 * If the render target flag is set and sample count is greater than 0 572 * then Gr will create an MSAA buffer that resolves to the texture. 573 */ 574 int fSampleCnt; 575 /** 576 * Handle to the 3D API object. 577 * OpenGL: Texture ID. 578 */ 579 GrBackendObject fTextureHandle; 580 }; 581 582 /////////////////////////////////////////////////////////////////////////////// 583 584 /** 585 * Gr can wrap an existing render target created by the client in the 3D API 586 * with a GrRenderTarget object. The client is responsible for ensuring that the 587 * underlying 3D API object lives at least as long as the GrRenderTarget object 588 * wrapping it. We require the client to explicitly provide information about 589 * the target, such as width, height, and pixel config rather than querying the 590 * 3D API for these values. We expect these properties to be immutable even if 591 * the 3D API doesn't require this (OpenGL). 592 */ 593 594 struct GrBackendRenderTargetDesc { 595 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); } 596 int fWidth; //<! width in pixels 597 int fHeight; //<! height in pixels 598 GrPixelConfig fConfig; //<! color format 599 GrSurfaceOrigin fOrigin; //<! pixel origin 600 /** 601 * The number of samples per pixel. Gr uses this to influence decisions 602 * about applying other forms of anti-aliasing. 603 */ 604 int fSampleCnt; 605 /** 606 * Number of bits of stencil per-pixel. 607 */ 608 int fStencilBits; 609 /** 610 * Handle to the 3D API object. 611 * OpenGL: FBO ID 612 */ 613 GrBackendObject fRenderTargetHandle; 614 }; 615 616 /** 617 * The GrContext's cache of backend context state can be partially invalidated. 618 * These enums are specific to the GL backend and we'd add a new set for an alternative backend. 619 */ 620 enum GrGLBackendState { 621 kRenderTarget_GrGLBackendState = 1 << 0, 622 kTextureBinding_GrGLBackendState = 1 << 1, 623 // View state stands for scissor and viewport 624 kView_GrGLBackendState = 1 << 2, 625 kBlend_GrGLBackendState = 1 << 3, 626 kAA_GrGLBackendState = 1 << 4, 627 kVertex_GrGLBackendState = 1 << 5, 628 kStencil_GrGLBackendState = 1 << 6, 629 kPixelStore_GrGLBackendState = 1 << 7, 630 kProgram_GrGLBackendState = 1 << 8, 631 kFixedFunction_GrGLBackendState = 1 << 9, 632 kMisc_GrGLBackendState = 1 << 10, 633 kPathRendering_GrGLBackendState = 1 << 11, 634 kALL_GrGLBackendState = 0xffff 635 }; 636 637 /** 638 * Returns the data size for the given compressed pixel config 639 */ 640 static inline size_t GrCompressedFormatDataSize(GrPixelConfig config, 641 int width, int height) { 642 SkASSERT(GrPixelConfigIsCompressed(config)); 643 644 switch (config) { 645 case kLATC_GrPixelConfig: 646 case kETC1_GrPixelConfig: 647 SkASSERT((width & 3) == 0); 648 SkASSERT((height & 3) == 0); 649 return (width >> 2) * (height >> 2) * 8; 650 651 default: 652 SkFAIL("Unknown compressed pixel config"); 653 return 4 * width * height; 654 } 655 } 656 657 /** 658 * This value translates to reseting all the context state for any backend. 659 */ 660 static const uint32_t kAll_GrBackendState = 0xffffffff; 661 662 /////////////////////////////////////////////////////////////////////////////// 663 664 #endif 665