3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #ifndef GrTypes_DEFINED
12 #define GrTypes_DEFINED
18 //#define SK_SUPPORT_LEGACY_GRTYPES
20 ////////////////////////////////////////////////////////////////////////////////
23 * Defines overloaded bitwise operators to make it easier to use an enum as a
26 #define GR_MAKE_BITFIELD_OPS(X) \
27 inline X operator | (X a, X b) { \
28 return (X) (+a | +b); \
31 inline X operator & (X a, X b) { \
32 return (X) (+a & +b); \
34 template <typename T> \
35 inline X operator & (T a, X b) { \
36 return (X) (+a & +b); \
38 template <typename T> \
39 inline X operator & (X a, T b) { \
40 return (X) (+a & +b); \
43 #define GR_DECL_BITFIELD_OPS_FRIENDS(X) \
44 friend X operator | (X a, X b); \
46 friend X operator & (X a, X b); \
48 template <typename T> \
49 friend X operator & (T a, X b); \
51 template <typename T> \
52 friend X operator & (X a, T b); \
53 ////////////////////////////////////////////////////////////////////////////////
55 #ifdef SK_SUPPORT_LEGACY_GRTYPES
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
61 #define GrALIGN4(n) SkAlign4(n)
62 #define GrIsALIGN4(n) SkIsAlign4(n)
64 template <typename T> const T& GrMin(const T& a, const T& b) {
65 return (a < b) ? a : b;
68 template <typename T> const T& GrMax(const T& a, const T& b) {
69 return (b < a) ? a : b;
73 * Count elements in an array
75 #define GR_ARRAY_COUNT(array) SK_ARRAY_COUNT(array)
78 * 16.16 fixed point type
80 typedef int32_t GrFixed;
84 static inline int16_t GrToS16(intptr_t x) {
85 SkASSERT((int16_t)x == x);
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))
102 * divide, rounding up
104 static inline int32_t GrIDivRoundUp(int x, int y) {
106 return (x + (y-1)) / y;
108 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
109 return (x + (y-1)) / y;
111 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
112 return (x + (y-1)) / y;
115 // compile time, evaluates Y multiple times
116 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
121 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
122 return GrUIDivRoundUp(x, alignment) * alignment;
124 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
125 return GrSizeDivRoundUp(x, alignment) * alignment;
128 // compile time, evaluates A multiple times
129 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
132 * amount of pad needed to align up
134 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
135 return (alignment - x % alignment) % alignment;
137 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
138 return (alignment - x % alignment) % alignment;
144 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
145 return (x / alignment) * alignment;
147 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
148 return (x / alignment) * alignment;
151 ///////////////////////////////////////////////////////////////////////////////
154 * Return the next power of 2 >= n.
156 static inline uint32_t GrNextPow2(uint32_t n) {
157 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
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;
165 ///////////////////////////////////////////////////////////////////////////////
168 * Possible 3D APIs that may be used by Ganesh.
175 * Backend-specific 3D context handle
176 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
178 typedef intptr_t GrBackendContext;
180 ///////////////////////////////////////////////////////////////////////////////
183 * Geometric primitives used for drawing.
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
194 static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
195 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
198 static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
199 return kTriangles_GrPrimitiveType == type ||
200 kTriangleStrip_GrPrimitiveType == type ||
201 kTriangleFan_GrPrimitiveType == type;
205 * Coeffecients for alpha-blending.
208 kInvalid_GrBlendCoeff = -1,
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
225 kPublicGrBlendCoeffCount
229 * Formats for masks, used by the font cache.
230 * Important that these are 0-based.
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
238 kLast_GrMaskFormat = kARGB_GrMaskFormat
240 static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
243 * Return the number of bytes-per-pixel for the specified mask format.
245 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
246 SkASSERT((unsigned)format <= 3);
251 static const int sBytesPerPixel[] = { 1, 2, 4, 4 };
252 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, array_size_mismatch);
254 return sBytesPerPixel[(int) format];
258 * Pixel configurations.
261 kUnknown_GrPixelConfig,
262 kAlpha_8_GrPixelConfig,
263 kIndex_8_GrPixelConfig,
264 kRGB_565_GrPixelConfig,
268 kRGBA_4444_GrPixelConfig,
270 * Premultiplied. Byte order is r,g,b,a.
272 kRGBA_8888_GrPixelConfig,
274 * Premultiplied. Byte order is b,g,r,a.
276 kBGRA_8888_GrPixelConfig,
278 * ETC1 Compressed Data
282 * LATC/RGTC/3Dc/BC4 Compressed Data
286 * R11 EAC Compressed Data
287 * (Corresponds to section C.3.5 of the OpenGL 4.4 core profile spec)
289 kR11_EAC_GrPixelConfig,
292 * 12x12 ASTC Compressed Data
293 * ASTC stands for Adaptive Scalable Texture Compression. It is a technique
294 * that allows for a lot of customization in the compressed representataion
295 * of a block. The only thing fixed in the representation is the block size,
296 * which means that a texture that contains ASTC data must be treated as
297 * having RGBA values. However, there are single-channel encodings which set
298 * the alpha to opaque and all three RGB channels equal effectively making the
299 * compression format a single channel such as R11 EAC and LATC.
301 kASTC_12x12_GrPixelConfig,
304 * Byte order is r, g, b, a. This color format is 32 bits per channel
306 kRGBA_float_GrPixelConfig,
307 kLast_GrPixelConfig = kRGBA_float_GrPixelConfig
309 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
311 // Aliases for pixel configs that match skia's byte order.
312 #ifndef SK_CPU_LENDIAN
313 #error "Skia gpu currently assumes little endian"
315 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
316 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
317 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
318 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
320 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
323 // Returns true if the pixel config is a GPU-specific compressed format
325 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
327 case kIndex_8_GrPixelConfig:
328 case kETC1_GrPixelConfig:
329 case kLATC_GrPixelConfig:
330 case kR11_EAC_GrPixelConfig:
331 case kASTC_12x12_GrPixelConfig:
338 // Returns true if the pixel config is 32 bits per pixel
339 static inline bool GrPixelConfigIs8888(GrPixelConfig config) {
341 case kRGBA_8888_GrPixelConfig:
342 case kBGRA_8888_GrPixelConfig:
349 // Takes a config and returns the equivalent config with the R and B order
350 // swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
351 static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
353 case kBGRA_8888_GrPixelConfig:
354 return kRGBA_8888_GrPixelConfig;
355 case kRGBA_8888_GrPixelConfig:
356 return kBGRA_8888_GrPixelConfig;
358 return kUnknown_GrPixelConfig;
362 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
363 SkASSERT(!GrPixelConfigIsCompressed(config));
365 case kAlpha_8_GrPixelConfig:
367 case kRGB_565_GrPixelConfig:
368 case kRGBA_4444_GrPixelConfig:
370 case kRGBA_8888_GrPixelConfig:
371 case kBGRA_8888_GrPixelConfig:
373 case kRGBA_float_GrPixelConfig:
380 static inline size_t GrUnpackAlignment(GrPixelConfig config) {
381 SkASSERT(!GrPixelConfigIsCompressed(config));
383 case kAlpha_8_GrPixelConfig:
385 case kRGB_565_GrPixelConfig:
386 case kRGBA_4444_GrPixelConfig:
388 case kRGBA_8888_GrPixelConfig:
389 case kBGRA_8888_GrPixelConfig:
390 case kRGBA_float_GrPixelConfig:
397 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
399 case kETC1_GrPixelConfig:
400 case kRGB_565_GrPixelConfig:
407 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
409 case kR11_EAC_GrPixelConfig:
410 case kLATC_GrPixelConfig:
411 case kASTC_12x12_GrPixelConfig:
412 case kAlpha_8_GrPixelConfig:
420 * Optional bitfield flags that can be passed to createTexture.
422 enum GrTextureFlags {
423 kNone_GrTextureFlags = 0x0,
425 * Creates a texture that can be rendered to as a GrRenderTarget. Use
426 * GrTexture::asRenderTarget() to access.
428 kRenderTarget_GrTextureFlagBit = 0x1,
430 * By default all render targets have an associated stencil buffer that
431 * may be required for path filling. This flag overrides stencil buffer
435 kNoStencil_GrTextureFlagBit = 0x2,
437 * Hint that the CPU may modify this texture after creation.
439 kDynamicUpdate_GrTextureFlagBit = 0x4,
441 * Indicates that all allocations (color buffer, FBO completeness, etc)
442 * should be verified.
444 kCheckAllocation_GrTextureFlagBit = 0x8,
446 kDummy_GrTextureFlagBit,
447 kLastPublic_GrTextureFlagBit = kDummy_GrTextureFlagBit-1,
450 GR_MAKE_BITFIELD_OPS(GrTextureFlags)
453 * Some textures will be stored such that the upper and left edges of the content meet at the
454 * the origin (in texture coord space) and for other textures the lower and left edges meet at
455 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
459 enum GrSurfaceOrigin {
460 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
461 kTopLeft_GrSurfaceOrigin,
462 kBottomLeft_GrSurfaceOrigin,
466 * Describes a texture to be created.
468 struct GrTextureDesc {
470 : fFlags(kNone_GrTextureFlags)
471 , fOrigin(kDefault_GrSurfaceOrigin)
474 , fConfig(kUnknown_GrPixelConfig)
478 GrTextureFlags fFlags; //!< bitfield of TextureFlags
479 GrSurfaceOrigin fOrigin; //!< origin of the texture
480 int fWidth; //!< Width of the texture
481 int fHeight; //!< Height of the texture
484 * Format of source data of the texture. Not guaranteed to be the same as
485 * internal format used by 3D API.
487 GrPixelConfig fConfig;
490 * The number of samples per pixel or 0 to disable full scene AA. This only
491 * applies if the kRenderTarget_GrTextureFlagBit is set. The actual number
492 * of samples may not exactly match the request. The request will be rounded
493 * up to the next supported sample count, or down if it is larger than the
494 * max supported count.
500 * GrCacheID is used create and find cached GrResources (e.g. GrTextures). The ID has two parts:
501 * the domain and the key. Domains simply allow multiple clients to use 0-based indices as their
502 * cache key without colliding. The key uniquely identifies a GrResource within the domain.
503 * Users of the cache must obtain a domain via GenerateDomain().
507 typedef uint8_t Domain;
518 * A default cache ID is invalid; a set method must be called before the object is used.
520 GrCacheID() { fDomain = kInvalid_Domain; }
523 * Initialize the cache ID to a domain and key.
525 GrCacheID(Domain domain, const Key& key) {
526 SkASSERT(kInvalid_Domain != domain);
527 this->reset(domain, key);
530 void reset(Domain domain, const Key& key) {
532 memcpy(&fKey, &key, sizeof(Key));
535 /** Has this been initialized to a valid domain */
536 bool isValid() const { return kInvalid_Domain != fDomain; }
538 const Key& getKey() const { SkASSERT(this->isValid()); return fKey; }
539 Domain getDomain() const { SkASSERT(this->isValid()); return fDomain; }
541 /** Creates a new unique ID domain. */
542 static Domain GenerateDomain();
548 static const Domain kInvalid_Domain = 0;
552 * Clips are composed from these objects.
559 ///////////////////////////////////////////////////////////////////////////////
561 // opaque type for 3D API object handles
562 typedef intptr_t GrBackendObject;
565 * Gr can wrap an existing texture created by the client with a GrTexture
566 * object. The client is responsible for ensuring that the texture lives at
567 * least as long as the GrTexture object wrapping it. We require the client to
568 * explicitly provide information about the texture, such as width, height,
569 * and pixel config, rather than querying the 3D APIfor these values. We expect
570 * these to be immutable even if the 3D API doesn't require this (OpenGL).
572 * Textures that are also render targets are supported as well. Gr will manage
573 * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for
574 * Gr to draw into the render target. To access the render target object
575 * call GrTexture::asRenderTarget().
577 * If in addition to the render target flag, the caller also specifies a sample
578 * count Gr will create an MSAA buffer that resolves into the texture. Gr auto-
579 * resolves when it reads from the texture. The client can explictly resolve
580 * using the GrRenderTarget interface.
582 * Note: These flags currently form a subset of GrTexture's flags.
585 enum GrBackendTextureFlags {
589 kNone_GrBackendTextureFlag = kNone_GrTextureFlags,
591 * Indicates that the texture is also a render target, and thus should have
592 * a GrRenderTarget object.
594 * D3D (future): client must have created the texture with flags that allow
595 * it to be used as a render target.
597 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrTextureFlagBit,
599 GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
601 struct GrBackendTextureDesc {
602 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
603 GrBackendTextureFlags fFlags;
604 GrSurfaceOrigin fOrigin;
605 int fWidth; //<! width in pixels
606 int fHeight; //<! height in pixels
607 GrPixelConfig fConfig; //<! color format
609 * If the render target flag is set and sample count is greater than 0
610 * then Gr will create an MSAA buffer that resolves to the texture.
614 * Handle to the 3D API object.
615 * OpenGL: Texture ID.
617 GrBackendObject fTextureHandle;
620 ///////////////////////////////////////////////////////////////////////////////
623 * Gr can wrap an existing render target created by the client in the 3D API
624 * with a GrRenderTarget object. The client is responsible for ensuring that the
625 * underlying 3D API object lives at least as long as the GrRenderTarget object
626 * wrapping it. We require the client to explicitly provide information about
627 * the target, such as width, height, and pixel config rather than querying the
628 * 3D API for these values. We expect these properties to be immutable even if
629 * the 3D API doesn't require this (OpenGL).
632 struct GrBackendRenderTargetDesc {
633 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
634 int fWidth; //<! width in pixels
635 int fHeight; //<! height in pixels
636 GrPixelConfig fConfig; //<! color format
637 GrSurfaceOrigin fOrigin; //<! pixel origin
639 * The number of samples per pixel. Gr uses this to influence decisions
640 * about applying other forms of anti-aliasing.
644 * Number of bits of stencil per-pixel.
648 * Handle to the 3D API object.
651 GrBackendObject fRenderTargetHandle;
655 * The GrContext's cache of backend context state can be partially invalidated.
656 * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
658 enum GrGLBackendState {
659 kRenderTarget_GrGLBackendState = 1 << 0,
660 kTextureBinding_GrGLBackendState = 1 << 1,
661 // View state stands for scissor and viewport
662 kView_GrGLBackendState = 1 << 2,
663 kBlend_GrGLBackendState = 1 << 3,
664 kMSAAEnable_GrGLBackendState = 1 << 4,
665 kVertex_GrGLBackendState = 1 << 5,
666 kStencil_GrGLBackendState = 1 << 6,
667 kPixelStore_GrGLBackendState = 1 << 7,
668 kProgram_GrGLBackendState = 1 << 8,
669 kFixedFunction_GrGLBackendState = 1 << 9,
670 kMisc_GrGLBackendState = 1 << 10,
671 kPathRendering_GrGLBackendState = 1 << 11,
672 kALL_GrGLBackendState = 0xffff
676 * Returns the data size for the given compressed pixel config
678 static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
679 int width, int height) {
680 SkASSERT(GrPixelConfigIsCompressed(config));
681 static const int kGrIndex8TableSize = 256 * 4; // 4 == sizeof(GrColor)
684 case kIndex_8_GrPixelConfig:
685 return width * height + kGrIndex8TableSize;
686 case kR11_EAC_GrPixelConfig:
687 case kLATC_GrPixelConfig:
688 case kETC1_GrPixelConfig:
689 SkASSERT((width & 3) == 0);
690 SkASSERT((height & 3) == 0);
691 return (width >> 2) * (height >> 2) * 8;
693 case kASTC_12x12_GrPixelConfig:
694 SkASSERT((width % 12) == 0);
695 SkASSERT((height % 12) == 0);
696 return (width / 12) * (height / 12) * 16;
699 SkFAIL("Unknown compressed pixel config");
700 return 4 * width * height;
705 * This value translates to reseting all the context state for any backend.
707 static const uint32_t kAll_GrBackendState = 0xffffffff;
709 ///////////////////////////////////////////////////////////////////////////////
711 #if GR_ALWAYS_ALLOCATE_ON_HEAP
712 #define GrAutoMallocBaseType SkAutoMalloc
714 #define GrAutoMallocBaseType SkAutoSMalloc<S>
717 template <size_t S> class GrAutoMalloc : public GrAutoMallocBaseType {
719 GrAutoMalloc() : INHERITED() {}
720 explicit GrAutoMalloc(size_t size) : INHERITED(size) {}
721 virtual ~GrAutoMalloc() {}
723 typedef GrAutoMallocBaseType INHERITED;
726 #undef GrAutoMallocBaseType