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 * 16.16 fixed point type
75 typedef int32_t GrFixed;
79 static inline int16_t GrToS16(intptr_t x) {
80 SkASSERT((int16_t)x == x);
92 // compile time versions of min/max
93 #define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
94 #define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
99 static inline int32_t GrIDivRoundUp(int x, int y) {
101 return (x + (y-1)) / y;
103 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
104 return (x + (y-1)) / y;
106 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
107 return (x + (y-1)) / y;
110 // compile time, evaluates Y multiple times
111 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
116 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
117 return GrUIDivRoundUp(x, alignment) * alignment;
119 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
120 return GrSizeDivRoundUp(x, alignment) * alignment;
123 // compile time, evaluates A multiple times
124 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
127 * amount of pad needed to align up
129 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
130 return (alignment - x % alignment) % alignment;
132 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
133 return (alignment - x % alignment) % alignment;
139 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
140 return (x / alignment) * alignment;
142 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
143 return (x / alignment) * alignment;
146 ///////////////////////////////////////////////////////////////////////////////
149 * Return the next power of 2 >= n.
151 static inline uint32_t GrNextPow2(uint32_t n) {
152 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
155 static inline int GrNextPow2(int n) {
156 SkASSERT(n >= 0); // this impl only works for non-neg.
157 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
160 ///////////////////////////////////////////////////////////////////////////////
163 * Possible 3D APIs that may be used by Ganesh.
170 * Backend-specific 3D context handle
171 * GrGLInterface* for OpenGL. If NULL will use the default GL interface.
173 typedef intptr_t GrBackendContext;
175 ///////////////////////////////////////////////////////////////////////////////
178 * Geometric primitives used for drawing.
180 enum GrPrimitiveType {
181 kTriangles_GrPrimitiveType,
182 kTriangleStrip_GrPrimitiveType,
183 kTriangleFan_GrPrimitiveType,
184 kPoints_GrPrimitiveType,
185 kLines_GrPrimitiveType, // 1 pix wide only
186 kLineStrip_GrPrimitiveType // 1 pix wide only
189 static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
190 return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
193 static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
194 return kTriangles_GrPrimitiveType == type ||
195 kTriangleStrip_GrPrimitiveType == type ||
196 kTriangleFan_GrPrimitiveType == type;
200 * Coeffecients for alpha-blending.
203 kInvalid_GrBlendCoeff = -1,
205 kZero_GrBlendCoeff, //<! 0
206 kOne_GrBlendCoeff, //<! 1
207 kSC_GrBlendCoeff, //<! src color
208 kISC_GrBlendCoeff, //<! one minus src color
209 kDC_GrBlendCoeff, //<! dst color
210 kIDC_GrBlendCoeff, //<! one minus dst color
211 kSA_GrBlendCoeff, //<! src alpha
212 kISA_GrBlendCoeff, //<! one minus src alpha
213 kDA_GrBlendCoeff, //<! dst alpha
214 kIDA_GrBlendCoeff, //<! one minus dst alpha
215 kConstC_GrBlendCoeff, //<! constant color
216 kIConstC_GrBlendCoeff, //<! one minus constant color
217 kConstA_GrBlendCoeff, //<! constant color alpha
218 kIConstA_GrBlendCoeff, //<! one minus constant color alpha
220 kFirstPublicGrBlendCoeff = kZero_GrBlendCoeff,
221 kLastPublicGrBlendCoeff = kIConstA_GrBlendCoeff,
223 static const int kPublicGrBlendCoeffCount = kLastPublicGrBlendCoeff + 1;
226 * Formats for masks, used by the font cache.
227 * Important that these are 0-based.
230 kA8_GrMaskFormat, //!< 1-byte per pixel
231 kA565_GrMaskFormat, //!< 2-bytes per pixel
232 kA888_GrMaskFormat, //!< 4-bytes per pixel
233 kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format
235 kLast_GrMaskFormat = kARGB_GrMaskFormat
237 static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
240 * Return the number of bytes-per-pixel for the specified mask format.
242 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
243 SkASSERT((unsigned)format <= 3);
248 static const int sBytesPerPixel[] = { 1, 2, 4, 4 };
249 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, array_size_mismatch);
251 return sBytesPerPixel[(int) format];
255 * Pixel configurations.
258 kUnknown_GrPixelConfig,
259 kAlpha_8_GrPixelConfig,
260 kIndex_8_GrPixelConfig,
261 kRGB_565_GrPixelConfig,
265 kRGBA_4444_GrPixelConfig,
267 * Premultiplied. Byte order is r,g,b,a.
269 kRGBA_8888_GrPixelConfig,
271 * Premultiplied. Byte order is b,g,r,a.
273 kBGRA_8888_GrPixelConfig,
275 * ETC1 Compressed Data
279 * LATC/RGTC/3Dc/BC4 Compressed Data
283 * R11 EAC Compressed Data
284 * (Corresponds to section C.3.5 of the OpenGL 4.4 core profile spec)
286 kR11_EAC_GrPixelConfig,
289 * 12x12 ASTC Compressed Data
290 * ASTC stands for Adaptive Scalable Texture Compression. It is a technique
291 * that allows for a lot of customization in the compressed representataion
292 * of a block. The only thing fixed in the representation is the block size,
293 * which means that a texture that contains ASTC data must be treated as
294 * having RGBA values. However, there are single-channel encodings which set
295 * the alpha to opaque and all three RGB channels equal effectively making the
296 * compression format a single channel such as R11 EAC and LATC.
298 kASTC_12x12_GrPixelConfig,
301 * Byte order is r, g, b, a. This color format is 32 bits per channel
303 kRGBA_float_GrPixelConfig,
304 kLast_GrPixelConfig = kRGBA_float_GrPixelConfig
306 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
308 // Aliases for pixel configs that match skia's byte order.
309 #ifndef SK_CPU_LENDIAN
310 #error "Skia gpu currently assumes little endian"
312 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
313 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
314 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
315 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
317 #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
320 // Returns true if the pixel config is a GPU-specific compressed format
322 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
324 case kIndex_8_GrPixelConfig:
325 case kETC1_GrPixelConfig:
326 case kLATC_GrPixelConfig:
327 case kR11_EAC_GrPixelConfig:
328 case kASTC_12x12_GrPixelConfig:
335 // Returns true if the pixel config is 32 bits per pixel
336 static inline bool GrPixelConfigIs8888(GrPixelConfig config) {
338 case kRGBA_8888_GrPixelConfig:
339 case kBGRA_8888_GrPixelConfig:
346 // Takes a config and returns the equivalent config with the R and B order
347 // swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
348 static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
350 case kBGRA_8888_GrPixelConfig:
351 return kRGBA_8888_GrPixelConfig;
352 case kRGBA_8888_GrPixelConfig:
353 return kBGRA_8888_GrPixelConfig;
355 return kUnknown_GrPixelConfig;
359 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
360 SkASSERT(!GrPixelConfigIsCompressed(config));
362 case kAlpha_8_GrPixelConfig:
364 case kRGB_565_GrPixelConfig:
365 case kRGBA_4444_GrPixelConfig:
367 case kRGBA_8888_GrPixelConfig:
368 case kBGRA_8888_GrPixelConfig:
370 case kRGBA_float_GrPixelConfig:
377 static inline size_t GrUnpackAlignment(GrPixelConfig config) {
378 SkASSERT(!GrPixelConfigIsCompressed(config));
380 case kAlpha_8_GrPixelConfig:
382 case kRGB_565_GrPixelConfig:
383 case kRGBA_4444_GrPixelConfig:
385 case kRGBA_8888_GrPixelConfig:
386 case kBGRA_8888_GrPixelConfig:
387 case kRGBA_float_GrPixelConfig:
394 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
396 case kETC1_GrPixelConfig:
397 case kRGB_565_GrPixelConfig:
404 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
406 case kR11_EAC_GrPixelConfig:
407 case kLATC_GrPixelConfig:
408 case kASTC_12x12_GrPixelConfig:
409 case kAlpha_8_GrPixelConfig:
417 * Optional bitfield flags that can be set on GrSurfaceDesc (below).
419 enum GrSurfaceFlags {
420 kNone_GrSurfaceFlags = 0x0,
422 * Creates a texture that can be rendered to as a GrRenderTarget. Use
423 * GrTexture::asRenderTarget() to access.
425 kRenderTarget_GrSurfaceFlag = 0x1,
427 * By default all render targets have an associated stencil buffer that
428 * may be required for path filling. This flag overrides stencil buffer
432 kNoStencil_GrSurfaceFlag = 0x2,
434 * Indicates that all allocations (color buffer, FBO completeness, etc)
435 * should be verified.
437 kCheckAllocation_GrSurfaceFlag = 0x4,
440 GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
443 typedef GrSurfaceFlags GrTextureFlags;
444 static const GrSurfaceFlags kNone_GrTextureFlags = kNone_GrSurfaceFlags;
445 static const GrSurfaceFlags kRenderTarget_GrTextureFlagBit = kRenderTarget_GrSurfaceFlag;
446 static const GrSurfaceFlags kNoStencil_GrTextureFlagBit = kNoStencil_GrSurfaceFlag;
447 static const GrSurfaceFlags kCheckAllocation_GrTextureFlagBit = kCheckAllocation_GrSurfaceFlag;
450 * Some textures will be stored such that the upper and left edges of the content meet at the
451 * the origin (in texture coord space) and for other textures the lower and left edges meet at
452 * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
456 enum GrSurfaceOrigin {
457 kDefault_GrSurfaceOrigin, // DEPRECATED; to be removed
458 kTopLeft_GrSurfaceOrigin,
459 kBottomLeft_GrSurfaceOrigin,
463 * Describes a surface to be created.
465 struct GrSurfaceDesc {
467 : fFlags(kNone_GrSurfaceFlags)
468 , fOrigin(kDefault_GrSurfaceOrigin)
471 , fConfig(kUnknown_GrPixelConfig)
475 GrSurfaceFlags fFlags; //!< bitfield of TextureFlags
476 GrSurfaceOrigin fOrigin; //!< origin of the texture
477 int fWidth; //!< Width of the texture
478 int fHeight; //!< Height of the texture
481 * Format of source data of the texture. Not guaranteed to be the same as
482 * internal format used by 3D API.
484 GrPixelConfig fConfig;
487 * The number of samples per pixel or 0 to disable full scene AA. This only
488 * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
489 * of samples may not exactly match the request. The request will be rounded
490 * up to the next supported sample count, or down if it is larger than the
491 * max supported count.
497 typedef GrSurfaceDesc GrTextureDesc;
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 = 0,
591 * Indicates that the texture is also a render target, and thus should have
592 * a GrRenderTarget object.
594 kRenderTarget_GrBackendTextureFlag = kRenderTarget_GrSurfaceFlag,
596 GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
598 struct GrBackendTextureDesc {
599 GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
600 GrBackendTextureFlags fFlags;
601 GrSurfaceOrigin fOrigin;
602 int fWidth; //<! width in pixels
603 int fHeight; //<! height in pixels
604 GrPixelConfig fConfig; //<! color format
606 * If the render target flag is set and sample count is greater than 0
607 * then Gr will create an MSAA buffer that resolves to the texture.
611 * Handle to the 3D API object.
612 * OpenGL: Texture ID.
614 GrBackendObject fTextureHandle;
617 ///////////////////////////////////////////////////////////////////////////////
620 * Gr can wrap an existing render target created by the client in the 3D API
621 * with a GrRenderTarget object. The client is responsible for ensuring that the
622 * underlying 3D API object lives at least as long as the GrRenderTarget object
623 * wrapping it. We require the client to explicitly provide information about
624 * the target, such as width, height, and pixel config rather than querying the
625 * 3D API for these values. We expect these properties to be immutable even if
626 * the 3D API doesn't require this (OpenGL).
629 struct GrBackendRenderTargetDesc {
630 GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
631 int fWidth; //<! width in pixels
632 int fHeight; //<! height in pixels
633 GrPixelConfig fConfig; //<! color format
634 GrSurfaceOrigin fOrigin; //<! pixel origin
636 * The number of samples per pixel. Gr uses this to influence decisions
637 * about applying other forms of anti-aliasing.
641 * Number of bits of stencil per-pixel.
645 * Handle to the 3D API object.
648 GrBackendObject fRenderTargetHandle;
652 * The GrContext's cache of backend context state can be partially invalidated.
653 * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
655 enum GrGLBackendState {
656 kRenderTarget_GrGLBackendState = 1 << 0,
657 kTextureBinding_GrGLBackendState = 1 << 1,
658 // View state stands for scissor and viewport
659 kView_GrGLBackendState = 1 << 2,
660 kBlend_GrGLBackendState = 1 << 3,
661 kMSAAEnable_GrGLBackendState = 1 << 4,
662 kVertex_GrGLBackendState = 1 << 5,
663 kStencil_GrGLBackendState = 1 << 6,
664 kPixelStore_GrGLBackendState = 1 << 7,
665 kProgram_GrGLBackendState = 1 << 8,
666 kFixedFunction_GrGLBackendState = 1 << 9,
667 kMisc_GrGLBackendState = 1 << 10,
668 kPathRendering_GrGLBackendState = 1 << 11,
669 kALL_GrGLBackendState = 0xffff
673 * Returns the data size for the given compressed pixel config
675 static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
676 int width, int height) {
677 SkASSERT(GrPixelConfigIsCompressed(config));
678 static const int kGrIndex8TableSize = 256 * 4; // 4 == sizeof(GrColor)
681 case kIndex_8_GrPixelConfig:
682 return width * height + kGrIndex8TableSize;
683 case kR11_EAC_GrPixelConfig:
684 case kLATC_GrPixelConfig:
685 case kETC1_GrPixelConfig:
686 SkASSERT((width & 3) == 0);
687 SkASSERT((height & 3) == 0);
688 return (width >> 2) * (height >> 2) * 8;
690 case kASTC_12x12_GrPixelConfig:
691 SkASSERT((width % 12) == 0);
692 SkASSERT((height % 12) == 0);
693 return (width / 12) * (height / 12) * 16;
696 SkFAIL("Unknown compressed pixel config");
697 return 4 * width * height;
702 * This value translates to reseting all the context state for any backend.
704 static const uint32_t kAll_GrBackendState = 0xffffffff;
706 ///////////////////////////////////////////////////////////////////////////////
708 #if GR_ALWAYS_ALLOCATE_ON_HEAP
709 #define GrAutoMallocBaseType SkAutoMalloc
711 #define GrAutoMallocBaseType SkAutoSMalloc<S>
714 template <size_t S> class GrAutoMalloc : public GrAutoMallocBaseType {
716 GrAutoMalloc() : INHERITED() {}
717 explicit GrAutoMalloc(size_t size) : INHERITED(size) {}
718 virtual ~GrAutoMalloc() {}
720 typedef GrAutoMallocBaseType INHERITED;
723 #undef GrAutoMallocBaseType