Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / gpu / GrTypes.h
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  *  16.16 fixed point type
74  */
75 typedef int32_t GrFixed;
76
77 #ifdef SK_DEBUG
78
79 static inline int16_t GrToS16(intptr_t x) {
80     SkASSERT((int16_t)x == x);
81     return (int16_t)x;
82 }
83
84 #else
85
86 #define GrToS16(x)  x
87
88 #endif
89
90 #endif
91
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))
95
96 /**
97  *  divide, rounding up
98  */
99 static inline int32_t GrIDivRoundUp(int x, int y) {
100     SkASSERT(y > 0);
101     return (x + (y-1)) / y;
102 }
103 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
104     return (x + (y-1)) / y;
105 }
106 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
107     return (x + (y-1)) / y;
108 }
109
110 // compile time, evaluates Y multiple times
111 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
112
113 /**
114  *  align up
115  */
116 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
117     return GrUIDivRoundUp(x, alignment) * alignment;
118 }
119 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
120     return GrSizeDivRoundUp(x, alignment) * alignment;
121 }
122
123 // compile time, evaluates A multiple times
124 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
125
126 /**
127  * amount of pad needed to align up
128  */
129 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
130     return (alignment - x % alignment) % alignment;
131 }
132 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
133     return (alignment - x % alignment) % alignment;
134 }
135
136 /**
137  *  align down
138  */
139 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
140     return (x / alignment) * alignment;
141 }
142 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
143     return (x / alignment) * alignment;
144 }
145
146 ///////////////////////////////////////////////////////////////////////////////
147
148 /**
149  *  Return the next power of 2 >= n.
150  */
151 static inline uint32_t GrNextPow2(uint32_t n) {
152     return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
153 }
154
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;
158 }
159
160 ///////////////////////////////////////////////////////////////////////////////
161
162 /**
163  * Possible 3D APIs that may be used by Ganesh.
164  */
165 enum GrBackend {
166     kOpenGL_GrBackend,
167 };
168
169 /**
170  * Backend-specific 3D context handle
171  *      GrGLInterface* for OpenGL. If NULL will use the default GL interface.
172  */
173 typedef intptr_t GrBackendContext;
174
175 ///////////////////////////////////////////////////////////////////////////////
176
177 /**
178 * Geometric primitives used for drawing.
179 */
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
187 };
188
189 static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
190     return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
191 }
192
193 static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
194     return kTriangles_GrPrimitiveType == type     ||
195            kTriangleStrip_GrPrimitiveType == type ||
196            kTriangleFan_GrPrimitiveType == type;
197 }
198
199 /**
200  * Coeffecients for alpha-blending.
201  */
202 enum GrBlendCoeff {
203     kInvalid_GrBlendCoeff = -1,
204
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
219
220     kFirstPublicGrBlendCoeff = kZero_GrBlendCoeff,
221     kLastPublicGrBlendCoeff = kIConstA_GrBlendCoeff,
222 };
223 static const int kPublicGrBlendCoeffCount = kLastPublicGrBlendCoeff + 1;
224
225 /**
226  *  Formats for masks, used by the font cache.
227  *  Important that these are 0-based.
228  */
229 enum GrMaskFormat {
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
234
235     kLast_GrMaskFormat = kARGB_GrMaskFormat
236 };
237 static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
238
239 /**
240  *  Return the number of bytes-per-pixel for the specified mask format.
241  */
242 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
243     SkASSERT((unsigned)format <= 3);
244     // kA8   (0) -> 1
245     // kA565 (1) -> 2
246     // kA888 (2) -> 4
247     // kARGB (3) -> 4
248     static const int sBytesPerPixel[] = { 1, 2, 4, 4 };
249     SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, array_size_mismatch);
250
251     return sBytesPerPixel[(int) format];
252 }
253
254 /**
255  * Pixel configurations.
256  */
257 enum GrPixelConfig {
258     kUnknown_GrPixelConfig,
259     kAlpha_8_GrPixelConfig,
260     kIndex_8_GrPixelConfig,
261     kRGB_565_GrPixelConfig,
262     /**
263      * Premultiplied
264      */
265     kRGBA_4444_GrPixelConfig,
266     /**
267      * Premultiplied. Byte order is r,g,b,a.
268      */
269     kRGBA_8888_GrPixelConfig,
270     /**
271      * Premultiplied. Byte order is b,g,r,a.
272      */
273     kBGRA_8888_GrPixelConfig,
274     /**
275      * ETC1 Compressed Data
276      */
277     kETC1_GrPixelConfig,
278     /**
279      * LATC/RGTC/3Dc/BC4 Compressed Data
280      */
281     kLATC_GrPixelConfig,
282     /**
283      * R11 EAC Compressed Data
284      * (Corresponds to section C.3.5 of the OpenGL 4.4 core profile spec)
285      */
286     kR11_EAC_GrPixelConfig,
287
288     /**
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.
297      */
298     kASTC_12x12_GrPixelConfig,
299
300     /**
301      * Byte order is r, g, b, a.  This color format is 32 bits per channel
302      */
303     kRGBA_float_GrPixelConfig,
304     kLast_GrPixelConfig = kRGBA_float_GrPixelConfig
305 };
306 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
307
308 // Aliases for pixel configs that match skia's byte order.
309 #ifndef SK_CPU_LENDIAN
310     #error "Skia gpu currently assumes little endian"
311 #endif
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;
316 #else
317     #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
318 #endif
319
320 // Returns true if the pixel config is a GPU-specific compressed format
321 // representation.
322 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
323     switch (config) {
324         case kIndex_8_GrPixelConfig:
325         case kETC1_GrPixelConfig:
326         case kLATC_GrPixelConfig:
327         case kR11_EAC_GrPixelConfig:
328         case kASTC_12x12_GrPixelConfig:
329             return true;
330         default:
331             return false;
332     }
333 }
334
335 // Returns true if the pixel config is 32 bits per pixel
336 static inline bool GrPixelConfigIs8888(GrPixelConfig config) {
337     switch (config) {
338         case kRGBA_8888_GrPixelConfig:
339         case kBGRA_8888_GrPixelConfig:
340             return true;
341         default:
342             return false;
343     }
344 }
345
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) {
349     switch (config) {
350         case kBGRA_8888_GrPixelConfig:
351             return kRGBA_8888_GrPixelConfig;
352         case kRGBA_8888_GrPixelConfig:
353             return kBGRA_8888_GrPixelConfig;
354         default:
355             return kUnknown_GrPixelConfig;
356     }
357 }
358
359 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
360     SkASSERT(!GrPixelConfigIsCompressed(config));
361     switch (config) {
362         case kAlpha_8_GrPixelConfig:
363             return 1;
364         case kRGB_565_GrPixelConfig:
365         case kRGBA_4444_GrPixelConfig:
366             return 2;
367         case kRGBA_8888_GrPixelConfig:
368         case kBGRA_8888_GrPixelConfig:
369             return 4;
370         case kRGBA_float_GrPixelConfig:
371             return 16;
372         default:
373             return 0;
374     }
375 }
376
377 static inline size_t GrUnpackAlignment(GrPixelConfig config) {
378     SkASSERT(!GrPixelConfigIsCompressed(config));
379     switch (config) {
380         case kAlpha_8_GrPixelConfig:
381             return 1;
382         case kRGB_565_GrPixelConfig:
383         case kRGBA_4444_GrPixelConfig:
384             return 2;
385         case kRGBA_8888_GrPixelConfig:
386         case kBGRA_8888_GrPixelConfig:
387         case kRGBA_float_GrPixelConfig:
388             return 4;
389         default:
390             return 0;
391     }
392 }
393
394 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
395     switch (config) {
396         case kETC1_GrPixelConfig:
397         case kRGB_565_GrPixelConfig:
398             return true;
399         default:
400             return false;
401     }
402 }
403
404 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
405     switch (config) {
406         case kR11_EAC_GrPixelConfig:
407         case kLATC_GrPixelConfig:
408         case kASTC_12x12_GrPixelConfig:
409         case kAlpha_8_GrPixelConfig:
410             return true;
411         default:
412             return false;
413     }
414 }
415
416 /**
417  * Optional bitfield flags that can be set on GrSurfaceDesc (below).
418  */
419 enum GrSurfaceFlags {
420     kNone_GrSurfaceFlags            = 0x0,
421     /**
422      * Creates a texture that can be rendered to as a GrRenderTarget. Use
423      * GrTexture::asRenderTarget() to access.
424      */
425     kRenderTarget_GrSurfaceFlag     = 0x1,
426     /**
427      * By default all render targets have an associated stencil buffer that
428      * may be required for path filling. This flag overrides stencil buffer
429      * creation.
430      * MAKE THIS PRIVATE?
431      */
432     kNoStencil_GrSurfaceFlag        = 0x2,
433     /**
434      * Indicates that all allocations (color buffer, FBO completeness, etc)
435      * should be verified.
436      */
437     kCheckAllocation_GrSurfaceFlag  = 0x4,
438 };
439
440 GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
441
442 // Legacy aliases
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;
448
449 /**
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
453  * to BottomLeft.
454  */
455
456 enum GrSurfaceOrigin {
457     kDefault_GrSurfaceOrigin,         // DEPRECATED; to be removed
458     kTopLeft_GrSurfaceOrigin,
459     kBottomLeft_GrSurfaceOrigin,
460 };
461
462 /**
463  * Describes a surface to be created.
464  */
465 struct GrSurfaceDesc {
466     GrSurfaceDesc()
467     : fFlags(kNone_GrSurfaceFlags)
468     , fOrigin(kDefault_GrSurfaceOrigin)
469     , fWidth(0)
470     , fHeight(0)
471     , fConfig(kUnknown_GrPixelConfig)
472     , fSampleCnt(0) {
473     }
474
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
479
480     /**
481      * Format of source data of the texture. Not guaranteed to be the same as
482      * internal format used by 3D API.
483      */
484     GrPixelConfig          fConfig;
485
486     /**
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.
492      */
493     int                    fSampleCnt;
494 };
495
496 // Legacy alias
497 typedef GrSurfaceDesc GrTextureDesc;
498
499 /**
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().
504  */
505 struct GrCacheID {
506 public:
507     typedef uint8_t  Domain;
508
509     struct Key {
510         union {
511             uint8_t  fData8[16];
512             uint32_t fData32[4];
513             uint64_t fData64[2];
514         };
515     };
516
517     /**
518      * A default cache ID is invalid; a set method must be called before the object is used.
519      */
520     GrCacheID() { fDomain = kInvalid_Domain; }
521
522     /**
523      * Initialize the cache ID to a domain and key.
524      */
525     GrCacheID(Domain domain, const Key& key) {
526         SkASSERT(kInvalid_Domain != domain);
527         this->reset(domain, key);
528     }
529
530     void reset(Domain domain, const Key& key) {
531         fDomain = domain;
532         memcpy(&fKey, &key, sizeof(Key));
533     }
534
535     /** Has this been initialized to a valid domain */
536     bool isValid() const { return kInvalid_Domain != fDomain; }
537
538     const Key& getKey() const { SkASSERT(this->isValid()); return fKey; }
539     Domain getDomain() const { SkASSERT(this->isValid()); return fDomain; }
540
541     /** Creates a new unique ID domain. */
542     static Domain GenerateDomain();
543
544 private:
545     Key             fKey;
546     Domain          fDomain;
547
548     static const Domain kInvalid_Domain = 0;
549 };
550
551 /**
552  * Clips are composed from these objects.
553  */
554 enum GrClipType {
555     kRect_ClipType,
556     kPath_ClipType
557 };
558
559 ///////////////////////////////////////////////////////////////////////////////
560
561 // opaque type for 3D API object handles
562 typedef intptr_t GrBackendObject;
563
564 /**
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).
571  *
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().
576  *
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.
581  *
582  * Note: These flags currently form a subset of GrTexture's flags.
583  */
584
585 enum GrBackendTextureFlags {
586     /**
587      * No flags enabled
588      */
589     kNone_GrBackendTextureFlag             = 0,
590     /**
591      * Indicates that the texture is also a render target, and thus should have
592      * a GrRenderTarget object.
593      */
594     kRenderTarget_GrBackendTextureFlag     = kRenderTarget_GrSurfaceFlag,
595 };
596 GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
597
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
605     /**
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.
608      */
609     int                             fSampleCnt;
610     /**
611      * Handle to the 3D API object.
612      * OpenGL: Texture ID.
613      */
614     GrBackendObject                 fTextureHandle;
615 };
616
617 ///////////////////////////////////////////////////////////////////////////////
618
619 /**
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).
627  */
628
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
635     /**
636      * The number of samples per pixel. Gr uses this to influence decisions
637      * about applying other forms of anti-aliasing.
638      */
639     int                             fSampleCnt;
640     /**
641      * Number of bits of stencil per-pixel.
642      */
643     int                             fStencilBits;
644     /**
645      * Handle to the 3D API object.
646      * OpenGL: FBO ID
647      */
648     GrBackendObject                 fRenderTargetHandle;
649 };
650
651 /**
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.
654  */
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
670 };
671
672 /**
673  * Returns the data size for the given compressed pixel config
674  */
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)
679
680     switch (config) {
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;
689
690         case kASTC_12x12_GrPixelConfig:
691             SkASSERT((width % 12) == 0);
692             SkASSERT((height % 12) == 0);
693             return (width / 12) * (height / 12) * 16;
694
695         default:
696             SkFAIL("Unknown compressed pixel config");
697             return 4 * width * height;
698     }
699 }
700
701 /**
702  * This value translates to reseting all the context state for any backend.
703  */
704 static const uint32_t kAll_GrBackendState = 0xffffffff;
705
706 ///////////////////////////////////////////////////////////////////////////////
707
708 #if GR_ALWAYS_ALLOCATE_ON_HEAP
709     #define GrAutoMallocBaseType SkAutoMalloc
710 #else
711     #define GrAutoMallocBaseType SkAutoSMalloc<S>
712 #endif
713
714 template <size_t S> class GrAutoMalloc : public GrAutoMallocBaseType {
715 public:
716     GrAutoMalloc() : INHERITED() {}
717     explicit GrAutoMalloc(size_t size) : INHERITED(size) {}
718     virtual ~GrAutoMalloc() {}
719 private:
720     typedef GrAutoMallocBaseType INHERITED;
721 };
722
723 #undef GrAutoMallocBaseType
724 #endif