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