GrTypes.h revision 5bc34f04fe70cdde702ac3bff1fea0ccb275d4a5
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 17//////////////////////////////////////////////////////////////////////////////// 18 19/** 20 * Defines overloaded bitwise operators to make it easier to use an enum as a 21 * bitfield. 22 */ 23#define GR_MAKE_BITFIELD_OPS(X) \ 24 inline X operator | (X a, X b) { \ 25 return (X) (+a | +b); \ 26 } \ 27 \ 28 inline X operator & (X a, X b) { \ 29 return (X) (+a & +b); \ 30 } \ 31 template <typename T> \ 32 inline X operator & (T a, X b) { \ 33 return (X) (+a & +b); \ 34 } \ 35 template <typename T> \ 36 inline X operator & (X a, T b) { \ 37 return (X) (+a & +b); \ 38 } \ 39 40#define GR_DECL_BITFIELD_OPS_FRIENDS(X) \ 41 friend X operator | (X a, X b); \ 42 \ 43 friend X operator & (X a, X b); \ 44 \ 45 template <typename T> \ 46 friend X operator & (T a, X b); \ 47 \ 48 template <typename T> \ 49 friend X operator & (X a, T b); \ 50//////////////////////////////////////////////////////////////////////////////// 51 52 53/** 54 * Macro to round n up to the next multiple of 4, or return it unchanged if 55 * n is already a multiple of 4 56 */ 57#define GrALIGN4(n) SkAlign4(n) 58#define GrIsALIGN4(n) SkIsAlign4(n) 59 60template <typename T> const T& GrMin(const T& a, const T& b) { 61 return (a < b) ? a : b; 62} 63 64template <typename T> const T& GrMax(const T& a, const T& b) { 65 return (b < a) ? a : b; 66} 67 68// compile time versions of min/max 69#define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b)) 70#define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a)) 71 72/** 73 * divide, rounding up 74 */ 75static inline int32_t GrIDivRoundUp(int x, int y) { 76 GrAssert(y > 0); 77 return (x + (y-1)) / y; 78} 79static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) { 80 return (x + (y-1)) / y; 81} 82static inline size_t GrSizeDivRoundUp(size_t x, uint32_t y) { 83 return (x + (y-1)) / y; 84} 85 86/** 87 * align up 88 */ 89static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) { 90 return GrUIDivRoundUp(x, alignment) * alignment; 91} 92static inline uint32_t GrSizeAlignUp(size_t x, uint32_t alignment) { 93 return GrSizeDivRoundUp(x, alignment) * alignment; 94} 95 96/** 97 * amount of pad needed to align up 98 */ 99static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) { 100 return (alignment - x % alignment) % alignment; 101} 102static inline size_t GrSizeAlignUpPad(size_t x, uint32_t alignment) { 103 return (alignment - x % alignment) % alignment; 104} 105 106/** 107 * align down 108 */ 109static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) { 110 return (x / alignment) * alignment; 111} 112static inline uint32_t GrSizeAlignDown(size_t x, uint32_t alignment) { 113 return (x / alignment) * alignment; 114} 115 116/** 117 * Count elements in an array 118 */ 119#define GR_ARRAY_COUNT(array) SK_ARRAY_COUNT(array) 120 121//!< allocate a block of memory, will never return NULL 122extern void* GrMalloc(size_t bytes); 123 124//!< free block allocated by GrMalloc. ptr may be NULL 125extern void GrFree(void* ptr); 126 127static inline void Gr_bzero(void* dst, size_t size) { 128 memset(dst, 0, size); 129} 130 131/////////////////////////////////////////////////////////////////////////////// 132 133/** 134 * Return the number of leading zeros in n 135 */ 136extern int Gr_clz(uint32_t n); 137 138/** 139 * Return true if n is a power of 2 140 */ 141static inline bool GrIsPow2(unsigned n) { 142 return n && 0 == (n & (n - 1)); 143} 144 145/** 146 * Return the next power of 2 >= n. 147 */ 148static inline uint32_t GrNextPow2(uint32_t n) { 149 return n ? (1 << (32 - Gr_clz(n - 1))) : 1; 150} 151 152static inline int GrNextPow2(int n) { 153 GrAssert(n >= 0); // this impl only works for non-neg. 154 return n ? (1 << (32 - Gr_clz(n - 1))) : 1; 155} 156 157/////////////////////////////////////////////////////////////////////////////// 158 159/** 160 * 16.16 fixed point type 161 */ 162typedef int32_t GrFixed; 163 164#if GR_DEBUG 165 166static inline int16_t GrToS16(intptr_t x) { 167 GrAssert((int16_t)x == x); 168 return (int16_t)x; 169} 170 171#else 172 173#define GrToS16(x) x 174 175#endif 176 177 178/////////////////////////////////////////////////////////////////////////////// 179 180/** 181 * Possible 3D APIs that may be used by Ganesh. 182 */ 183enum GrEngine { 184 kOpenGL_Shaders_GrEngine, 185 kOpenGL_Fixed_GrEngine, 186}; 187 188/** 189 * Engine-specific 3D context handle 190 * GrGLInterface* for OpenGL. If NULL will use the default GL interface. 191 */ 192typedef intptr_t GrPlatform3DContext; 193 194/////////////////////////////////////////////////////////////////////////////// 195 196/** 197 * Type used to describe format of vertices in arrays 198 * Values are defined in GrDrawTarget 199 */ 200typedef int GrVertexLayout; 201 202/** 203* Geometric primitives used for drawing. 204*/ 205enum GrPrimitiveType { 206 kTriangles_PrimitiveType, 207 kTriangleStrip_PrimitiveType, 208 kTriangleFan_PrimitiveType, 209 kPoints_PrimitiveType, 210 kLines_PrimitiveType, // 1 pix wide only 211 kLineStrip_PrimitiveType // 1 pix wide only 212}; 213 214static inline bool GrIsPrimTypeLines(GrPrimitiveType type) { 215 return kLines_PrimitiveType == type || kLineStrip_PrimitiveType == type; 216} 217 218static inline bool GrIsPrimTypeTris(GrPrimitiveType type) { 219 return kTriangles_PrimitiveType == type || 220 kTriangleStrip_PrimitiveType == type || 221 kTriangleFan_PrimitiveType == type; 222} 223 224/** 225 * Coeffecients for alpha-blending. 226 */ 227enum GrBlendCoeff { 228 kZero_BlendCoeff, //<! 0 229 kOne_BlendCoeff, //<! 1 230 kSC_BlendCoeff, //<! src color 231 kISC_BlendCoeff, //<! one minus src color 232 kDC_BlendCoeff, //<! dst color 233 kIDC_BlendCoeff, //<! one minus dst color 234 kSA_BlendCoeff, //<! src alpha 235 kISA_BlendCoeff, //<! one minus src alpha 236 kDA_BlendCoeff, //<! dst alpha 237 kIDA_BlendCoeff, //<! one minus dst alpha 238 kConstC_BlendCoeff, //<! constant color 239 kIConstC_BlendCoeff, //<! one minus constant color 240 kConstA_BlendCoeff, //<! constant color alpha 241 kIConstA_BlendCoeff, //<! one minus constant color alpha 242 243 kPublicBlendCoeffCount 244}; 245 246/** 247 * Formats for masks, used by the font cache. 248 * Important that these are 0-based. 249 */ 250enum GrMaskFormat { 251 kA8_GrMaskFormat, //!< 1-byte per pixel 252 kA565_GrMaskFormat, //!< 2-bytes per pixel 253 kA888_GrMaskFormat, //!< 4-bytes per pixel 254 255 kCount_GrMaskFormats //!< used to allocate arrays sized for mask formats 256}; 257 258/** 259 * Return the number of bytes-per-pixel for the specified mask format. 260 */ 261static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) { 262 GrAssert((unsigned)format <= 2); 263 // kA8 (0) -> 1 264 // kA565 (1) -> 2 265 // kA888 (2) -> 4 266 return 1 << (int)format; 267} 268 269/** 270 * Pixel configurations. 271 * 272 * Unpremultiplied configs are intended for converting pixel data in and out 273 * from skia. Surfaces with these configs have limited support. As an input 274 * (GrPaint texture) the corresponding GrSamplerState must have its filter set 275 * to kNearest_Filter. Otherwise, the draw will fail. When the render target 276 * has an unpremultiplied config draws must use blend coeffs 1,0 (AKA src-mode). 277 * Other coeffs will cause the draw to fail. 278 */ 279enum GrPixelConfig { 280 kUnknown_GrPixelConfig, 281 kAlpha_8_GrPixelConfig, 282 kIndex_8_GrPixelConfig, 283 kRGB_565_GrPixelConfig, 284 /** 285 * Premultiplied 286 */ 287 kRGBA_4444_GrPixelConfig, 288 /** 289 * Premultiplied. Byte order is r,g,b,a 290 */ 291 kRGBA_8888_PM_GrPixelConfig, 292 /** 293 * Unpremultiplied. Byte order is r,g,b,a 294 */ 295 kRGBA_8888_UPM_GrPixelConfig, 296 /** 297 * Premultiplied. Byte order is b,g,r,a 298 */ 299 kBGRA_8888_PM_GrPixelConfig, 300 /** 301 * Unpremultiplied. Byte order is b,g,r,a 302 */ 303 kBGRA_8888_UPM_GrPixelConfig, 304}; 305 306// Aliases for pixel configs that match skia's byte order 307#ifndef SK_CPU_LENDIAN 308 #error "Skia gpu currently assumes little endian" 309#endif 310#if 24 == SK_A32_SHIFT && 16 == SK_R32_SHIFT && \ 311 8 == SK_G32_SHIFT && 0 == SK_B32_SHIFT 312 static const GrPixelConfig kSkia8888_PM_GrPixelConfig = kBGRA_8888_PM_GrPixelConfig; 313 static const GrPixelConfig kSkia8888_UPM_GrPixelConfig = kBGRA_8888_UPM_GrPixelConfig; 314#elif 24 == SK_A32_SHIFT && 16 == SK_B32_SHIFT && \ 315 8 == SK_G32_SHIFT && 0 == SK_R32_SHIFT 316 static const GrPixelConfig kSkia8888_PM_GrPixelConfig = kRGBA_8888_PM_GrPixelConfig; 317 static const GrPixelConfig kSkia8888_UPM_GrPixelConfig = kRGBA_8888_UPM_GrPixelConfig; 318#else 319 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format." 320#endif 321 322// WebKit is relying on this old name for the native skia PM config. This will 323// be deleted ASAP because it is so similar to kRGBA_PM_8888_GrPixelConfig but 324// has a different interpretation when skia is compiled BGRA. 325static const GrPixelConfig kRGBA_8888_GrPixelConfig = kSkia8888_PM_GrPixelConfig; 326 327// Returns true if the pixel config has 8bit r,g,b,a components in that byte 328// order 329static inline bool GrPixelConfigIsRGBA8888(GrPixelConfig config) { 330 switch (config) { 331 case kRGBA_8888_PM_GrPixelConfig: 332 case kRGBA_8888_UPM_GrPixelConfig: 333 return true; 334 default: 335 return false; 336 } 337} 338 339// Returns true if the pixel config has 8bit b,g,r,a components in that byte 340// order 341static inline bool GrPixelConfigIsBGRA8888(GrPixelConfig config) { 342 switch (config) { 343 case kBGRA_8888_PM_GrPixelConfig: 344 case kBGRA_8888_UPM_GrPixelConfig: 345 return true; 346 default: 347 return false; 348 } 349} 350 351// Returns true if the pixel config is 32 bits per pixel 352static inline bool GrPixelConfigIs32Bit(GrPixelConfig config) { 353 switch (config) { 354 case kRGBA_8888_PM_GrPixelConfig: 355 case kRGBA_8888_UPM_GrPixelConfig: 356 case kBGRA_8888_PM_GrPixelConfig: 357 case kBGRA_8888_UPM_GrPixelConfig: 358 return true; 359 default: 360 return false; 361 } 362} 363 364// Takes a config and returns the equivalent config with the R and B order 365// swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig 366static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) { 367 switch (config) { 368 case kBGRA_8888_PM_GrPixelConfig: 369 return kRGBA_8888_PM_GrPixelConfig; 370 case kBGRA_8888_UPM_GrPixelConfig: 371 return kRGBA_8888_UPM_GrPixelConfig; 372 case kRGBA_8888_PM_GrPixelConfig: 373 return kBGRA_8888_PM_GrPixelConfig; 374 case kRGBA_8888_UPM_GrPixelConfig: 375 return kBGRA_8888_UPM_GrPixelConfig; 376 default: 377 return kUnknown_GrPixelConfig; 378 } 379} 380 381static inline size_t GrBytesPerPixel(GrPixelConfig config) { 382 switch (config) { 383 case kAlpha_8_GrPixelConfig: 384 case kIndex_8_GrPixelConfig: 385 return 1; 386 case kRGB_565_GrPixelConfig: 387 case kRGBA_4444_GrPixelConfig: 388 return 2; 389 case kRGBA_8888_PM_GrPixelConfig: 390 case kRGBA_8888_UPM_GrPixelConfig: 391 case kBGRA_8888_PM_GrPixelConfig: 392 case kBGRA_8888_UPM_GrPixelConfig: 393 return 4; 394 default: 395 return 0; 396 } 397} 398 399static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) { 400 switch (config) { 401 case kRGB_565_GrPixelConfig: 402 return true; 403 default: 404 return false; 405 } 406} 407 408/** 409 * Premultiplied alpha is the usual for skia. Therefore, configs that are 410 * ambiguous (alpha-only or color-only) are considered premultiplied. 411 */ 412static inline bool GrPixelConfigIsUnpremultiplied(GrPixelConfig config) { 413 switch (config) { 414 case kRGBA_8888_UPM_GrPixelConfig: 415 case kBGRA_8888_UPM_GrPixelConfig: 416 return true; 417 default: 418 return false; 419 } 420} 421 422static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) { 423 switch (config) { 424 case kAlpha_8_GrPixelConfig: 425 return true; 426 default: 427 return false; 428 } 429} 430 431/** 432 * Used to control the level of antialiasing available for a rendertarget. 433 * Anti-alias quality levels depend on the underlying API/GPU capabilities. 434 */ 435enum GrAALevels { 436 kNone_GrAALevel, //<! No antialiasing available. 437 kLow_GrAALevel, //<! Low quality antialiased rendering. Actual 438 // interpretation is platform-dependent. 439 kMed_GrAALevel, //<! Medium quality antialiased rendering. Actual 440 // interpretation is platform-dependent. 441 kHigh_GrAALevel, //<! High quality antialiased rendering. Actual 442 // interpretation is platform-dependent. 443}; 444 445/** 446 * Optional bitfield flags that can be passed to createTexture. 447 */ 448enum GrTextureFlags { 449 kNone_GrTextureFlags = 0x0, 450 /** 451 * Creates a texture that can be rendered to as a GrRenderTarget. Use 452 * GrTexture::asRenderTarget() to access. 453 */ 454 kRenderTarget_GrTextureFlagBit = 0x1, 455 /** 456 * By default all render targets have an associated stencil buffer that 457 * may be required for path filling. This flag overrides stencil buffer 458 * creation. 459 * MAKE THIS PRIVATE? 460 */ 461 kNoStencil_GrTextureFlagBit = 0x2, 462 /** 463 * Hint that the CPU may modify this texture after creation. 464 */ 465 kDynamicUpdate_GrTextureFlagBit = 0x4, 466}; 467 468GR_MAKE_BITFIELD_OPS(GrTextureFlags) 469 470enum { 471 /** 472 * For Index8 pixel config, the colortable must be 256 entries 473 */ 474 kGrColorTableSize = 256 * 4 //sizeof(GrColor) 475}; 476 477/** 478 * Describes a texture to be created. 479 */ 480struct GrTextureDesc { 481 GrTextureFlags fFlags; //!< bitfield of TextureFlags 482 /** 483 * The level of antialiasing available for a rendertarget texture. Only used 484 * fFlags contains kRenderTarget_GrTextureFlag. 485 */ 486 GrAALevels fAALevel; 487 int fWidth; //!< Width of the texture 488 int fHeight; //!< Height of the texture 489 /** 490 * Format of source data of the texture. Not guaraunteed to be the same as 491 * internal format used by 3D API. 492 */ 493 GrPixelConfig fConfig; 494}; 495 496/** 497 * Set Operations used to construct clips. 498 */ 499enum GrSetOp { 500 kReplace_SetOp, 501 kIntersect_SetOp, 502 kUnion_SetOp, 503 kXor_SetOp, 504 kDifference_SetOp, 505 kReverseDifference_SetOp, 506}; 507 508/** 509 * Clips are composed from these objects. 510 */ 511enum GrClipType { 512 kRect_ClipType, 513 kPath_ClipType 514}; 515 516/** 517 * Commands used to describe a path. Each command 518 * is accompanied by some number of points. 519 */ 520enum GrPathCmd { 521 kMove_PathCmd, //!< Starts a new subpath at 522 // at the returned point 523 // 1 point 524 kLine_PathCmd, //!< Adds a line segment 525 // 2 points 526 kQuadratic_PathCmd, //!< Adds a quadratic segment 527 // 3 points 528 kCubic_PathCmd, //!< Adds a cubic segment 529 // 4 points 530 kClose_PathCmd, //!< Closes the current subpath 531 // by connecting a line to the 532 // starting point. 533 // 0 points 534 kEnd_PathCmd //!< Indicates the end of the last subpath 535 // when iterating 536 // 0 points. 537}; 538 539/** 540 * Gets the number of points associated with a path command. 541 */ 542static int inline NumPathCmdPoints(GrPathCmd cmd) { 543 static const int gNumPoints[] = { 544 1, 2, 3, 4, 0, 0 545 }; 546 return gNumPoints[cmd]; 547} 548 549/** 550 * Path filling rules 551 */ 552enum GrPathFill { 553 kWinding_PathFill, 554 kEvenOdd_PathFill, 555 kInverseWinding_PathFill, 556 kInverseEvenOdd_PathFill, 557 kHairLine_PathFill, 558 559 kPathFillCount 560}; 561 562static inline GrPathFill GrNonInvertedFill(GrPathFill fill) { 563 static const GrPathFill gNonInvertedFills[] = { 564 kWinding_PathFill, // kWinding_PathFill 565 kEvenOdd_PathFill, // kEvenOdd_PathFill 566 kWinding_PathFill, // kInverseWinding_PathFill 567 kEvenOdd_PathFill, // kInverseEvenOdd_PathFill 568 kHairLine_PathFill,// kHairLine_PathFill 569 }; 570 GR_STATIC_ASSERT(0 == kWinding_PathFill); 571 GR_STATIC_ASSERT(1 == kEvenOdd_PathFill); 572 GR_STATIC_ASSERT(2 == kInverseWinding_PathFill); 573 GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill); 574 GR_STATIC_ASSERT(4 == kHairLine_PathFill); 575 GR_STATIC_ASSERT(5 == kPathFillCount); 576 return gNonInvertedFills[fill]; 577} 578 579static inline bool GrIsFillInverted(GrPathFill fill) { 580 static const bool gIsFillInverted[] = { 581 false, // kWinding_PathFill 582 false, // kEvenOdd_PathFill 583 true, // kInverseWinding_PathFill 584 true, // kInverseEvenOdd_PathFill 585 false, // kHairLine_PathFill 586 }; 587 GR_STATIC_ASSERT(0 == kWinding_PathFill); 588 GR_STATIC_ASSERT(1 == kEvenOdd_PathFill); 589 GR_STATIC_ASSERT(2 == kInverseWinding_PathFill); 590 GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill); 591 GR_STATIC_ASSERT(4 == kHairLine_PathFill); 592 GR_STATIC_ASSERT(5 == kPathFillCount); 593 return gIsFillInverted[fill]; 594} 595 596/** 597 * Hints provided about a path's convexity (or lack thereof). 598 */ 599enum GrConvexHint { 600 kNone_ConvexHint, //<! No hint about convexity 601 // of the path 602 kConvex_ConvexHint, //<! Path is one convex piece 603 kNonOverlappingConvexPieces_ConvexHint, //<! Multiple convex pieces, 604 // pieces are known to be 605 // disjoint 606 kSameWindingConvexPieces_ConvexHint, //<! Multiple convex pieces, 607 // may or may not intersect, 608 // either all wind cw or all 609 // wind ccw. 610 kConcave_ConvexHint //<! Path is known to be 611 // concave 612}; 613 614/////////////////////////////////////////////////////////////////////////////// 615 616// opaque type for 3D API object handles 617typedef intptr_t GrPlatform3DObject; 618 619/** 620 * Gr can wrap an existing texture created by the client with a GrTexture 621 * object. The client is responsible for ensuring that the texture lives at 622 * least as long as the GrTexture object wrapping it. We require the client to 623 * explicitly provide information about the texture, such as width, height, 624 * and pixel config, rather than querying the 3D APIfor these values. We expect 625 * these to be immutable even if the 3D API doesn't require this (OpenGL). 626 * 627 * Textures that are also render targets are supported as well. Gr will manage 628 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for 629 * Gr to draw into the render target. To access the render target object 630 * call GrTexture::asRenderTarget(). 631 * 632 * If in addition to the render target flag, the caller also specifies a sample 633 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto- 634 * resolves when it reads from the texture. The client can explictly resolve 635 * using the GrRenderTarget interface. 636 */ 637 638enum GrPlatformTextureFlags { 639 /** 640 * No flags enabled 641 */ 642 kNone_GrPlatformTextureFlag = 0x0, 643 /** 644 * Indicates that the texture is also a render target, and thus should have 645 * a GrRenderTarget object. 646 * 647 * D3D (future): client must have created the texture with flags that allow 648 * it to be used as a render target. 649 */ 650 kRenderTarget_GrPlatformTextureFlag = 0x1, 651}; 652GR_MAKE_BITFIELD_OPS(GrPlatformTextureFlags) 653 654struct GrPlatformTextureDesc { 655 GrPlatformTextureDesc() { memset(this, 0, sizeof(*this)); } 656 GrPlatformTextureFlags fFlags; 657 int fWidth; //<! width in pixels 658 int fHeight; //<! height in pixels 659 GrPixelConfig fConfig; //<! color format 660 /** 661 * If the render target flag is set and sample count is greater than 0 662 * then Gr will create an MSAA buffer that resolves to the texture. 663 */ 664 int fSampleCnt; 665 /** 666 * Handle to the 3D API object. 667 * OpenGL: Texture ID. 668 */ 669 GrPlatform3DObject fTextureHandle; 670}; 671 672/////////////////////////////////////////////////////////////////////////////// 673 674/** 675 * Gr can wrap an existing render target created by the client in the 3D API 676 * with a GrRenderTarget object. The client is responsible for ensuring that the 677 * underlying 3D API object lives at least as long as the GrRenderTarget object 678 * wrapping it. We require the client to explicitly provide information about 679 * the target, such as width, height, and pixel config rather than querying the 680 * 3D API for these values. We expect these properties to be immutable even if 681 * the 3D API doesn't require this (OpenGL). 682 */ 683 684struct GrPlatformRenderTargetDesc { 685 GrPlatformRenderTargetDesc() { memset(this, 0, sizeof(*this)); } 686 int fWidth; //<! width in pixels 687 int fHeight; //<! height in pixels 688 GrPixelConfig fConfig; //<! color format 689 /** 690 * The number of samples per pixel. Gr uses this to influence decisions 691 * about applying other forms of antialiasing. 692 */ 693 int fSampleCnt; 694 /** 695 * Number of bits of stencil per-pixel. 696 */ 697 int fStencilBits; 698 /** 699 * Handle to the 3D API object. 700 * OpenGL: FBO ID 701 */ 702 GrPlatform3DObject fRenderTargetHandle; 703}; 704 705/////////////////////////////////////////////////////////////////////////////// 706// DEPRECATED. createPlatformSurface is replaced by createPlatformTexture 707// and createPlatformRenderTarget. These enums and structs will be removed. 708 709enum GrPlatformSurfaceType { 710 /** 711 * Specifies that the object being created is a render target. 712 */ 713 kRenderTarget_GrPlatformSurfaceType, 714 /** 715 * Specifies that the object being created is a texture. 716 */ 717 kTexture_GrPlatformSurfaceType, 718 /** 719 * Specifies that the object being created is a texture and a render 720 * target. 721 */ 722 kTextureRenderTarget_GrPlatformSurfaceType, 723}; 724 725enum GrPlatformRenderTargetFlags { 726 kNone_GrPlatformRenderTargetFlagBit = 0x0, 727 728 /** 729 * Gives permission to Gr to perform the downsample-resolve of a 730 * multisampled render target. If this is not set then read pixel 731 * operations may fail. If the object is both a texture and render target 732 * then this *must* be set. Otherwise, if the client wants do its own 733 * resolves it must create separate GrRenderTarget and GrTexture objects 734 * and insert appropriate flushes and resolves betweeen data hazards. 735 * GrRenderTarget has a flagForResolve() 736 */ 737 kGrCanResolve_GrPlatformRenderTargetFlagBit = 0x2, 738}; 739 740GR_MAKE_BITFIELD_OPS(GrPlatformRenderTargetFlags) 741 742struct GrPlatformSurfaceDesc { 743 GrPlatformSurfaceType fSurfaceType; // type of surface to create 744 /** 745 * Flags for kRenderTarget and kTextureRenderTarget surface types 746 */ 747 GrPlatformRenderTargetFlags fRenderTargetFlags; 748 749 int fWidth; // width in pixels 750 int fHeight; // height in pixels 751 GrPixelConfig fConfig; // color format 752 /** 753 * Number of per sample stencil buffer. Only relevant if kIsRenderTarget is 754 * set in fFlags. 755 */ 756 int fStencilBits; 757 758 /** 759 * Number of samples per-pixel. Only relevant if kIsRenderTarget is set in 760 * fFlags. 761 */ 762 int fSampleCnt; 763 764 /** 765 * Texture object in 3D API. Only relevant if fSurfaceType is kTexture or 766 * kTextureRenderTarget. 767 * GL: this is a texture object (glGenTextures) 768 */ 769 GrPlatform3DObject fPlatformTexture; 770 /** 771 * Render target object in 3D API. Only relevant if fSurfaceType is 772 * kRenderTarget or kTextureRenderTarget 773 * GL: this is a FBO object (glGenFramebuffers) 774 */ 775 GrPlatform3DObject fPlatformRenderTarget; 776 /** 777 * 3D API object used as destination of resolve. Only relevant if 778 * fSurfaceType is kRenderTarget or kTextureRenderTarget and 779 * kGrCanResolve is set in fRenderTargetFlags. 780 * fFlags. 781 * GL: this is a FBO object (glGenFramebuffers) 782 */ 783 GrPlatform3DObject fPlatformResolveDestination; 784 785 void reset() { memset(this, 0, sizeof(GrPlatformSurfaceDesc)); } 786}; 787 788/** 789 * Example of how to wrap render-to-texture-with-MSAA GL objects with a GrPlatformSurace 790 * 791 * GLint colorBufferID; 792 * glGenRenderbuffers(1, &colorID); 793 * glBindRenderbuffer(GL_RENDERBUFFER, colorBufferID); 794 * glRenderbufferStorageMultisample(GL_RENDERBUFFER, S, GL_RGBA, W, H); 795 * 796 * GLint stencilBufferID; 797 * glGenRenderBuffers(1, &stencilBufferID); 798 * glBindRenderbuffer(GL_RENDERBUFFER, stencilBufferID); 799 * glRenderbufferStorageMultisample(GL_RENDERBUFFER, S, GL_STENCIL_INDEX8, W, H); 800 * 801 * GLint drawFBOID; 802 * glGenFramebuffers(1, &drawFBOID); 803 * glBindFramebuffer(GL_FRAMEBUFFER, drawFBOID); 804 * glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferID); 805 * glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencilBufferID); 806 * 807 * GLint textureID; 808 * glGenTextures(1, &textureID); 809 * glBindTexture(GL_TEXTURE_2D, textureID); 810 * glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, H, ...); 811 * 812 * GLint readFBOID; 813 * glGenFramebuffers(1, &readFBOID); 814 * glBindFramebuffer(GL_FRAMEBUFFER, readFBOID); 815 * glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureID, 0); 816 * 817 * GrPlatformSurfaceDesc renderTargetTextureDesc; 818 * renderTargetTextureDesc.fSurfaceType = kTextureRenderTarget_GrPlatformSurfaceType; 819 * renderTargetTextureDesc.fRenderTargetFlags = kGrCanResolve_GrPlatformRenderTargetFlagBit; 820 * renderTargetTextureDesc.fWidth = W; 821 * renderTargetTextureDesc.fHeight = H; 822 * renderTargetTextureDesc.fConfig = kSkia8888_PM_GrPixelConfig 823 * renderTargetTextureDesc.fStencilBits = 8; 824 * renderTargetTextureDesc.fSampleCnt = S; 825 * renderTargetTextureDesc.fPlatformTexture = textureID; 826 * renderTargetTextureDesc.fPlatformRenderTarget = drawFBOID; 827 * renderTargetTextureDesc.fPlatformResolveDestination = readFBOID; 828 * 829 * GrTexture* texture = static_cast<GrTexture*>(grContext->createPlatrformSurface(renderTargetTextureDesc)); 830 */ 831 832 833/////////////////////////////////////////////////////////////////////////////// 834 835// this is included only to make it easy to use this debugging facility 836#include "GrInstanceCounter.h" 837 838#endif 839