1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrTypes_DEFINED
9 #define GrTypes_DEFINED
10 
11 #include "SkMath.h"
12 #include "SkTypes.h"
13 #include "GrConfig.h"
14 
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 /**
18  * Defines overloaded bitwise operators to make it easier to use an enum as a
19  * bitfield.
20  */
21 #define GR_MAKE_BITFIELD_OPS(X) \
22     inline X operator |(X a, X b) { \
23         return (X) (+a | +b); \
24     } \
25     inline X& operator |=(X& a, X b) { \
26         return (a = a | b); \
27     } \
28     inline X operator &(X a, X b) { \
29         return (X) (+a & +b); \
30     } \
31     inline X& operator &=(X& a, X b) { \
32         return (a = 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     friend X& operator |=(X& a, X b); \
46     \
47     friend X operator &(X a, X b); \
48     friend X& operator &=(X& a, X b); \
49     \
50     template <typename T> \
51     friend X operator &(T a, X b); \
52     \
53     template <typename T> \
54     friend X operator &(X a, T b); \
55 
56 /**
57  * Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of
58  * masking with type safety. Instantiated with the ~ operator.
59  */
60 template<typename TFlags> class GrTFlagsMask {
61 public:
GrTFlagsMask(TFlags value)62     constexpr explicit GrTFlagsMask(TFlags value) : GrTFlagsMask(static_cast<int>(value)) {}
GrTFlagsMask(int value)63     constexpr explicit GrTFlagsMask(int value) : fValue(value) {}
value()64     constexpr int value() const { return fValue; }
65 private:
66     const int fValue;
67 };
68 
69 // Or-ing a mask always returns another mask.
70 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(GrTFlagsMask<TFlags> a,
71                                                                    GrTFlagsMask<TFlags> b) {
72     return GrTFlagsMask<TFlags>(a.value() | b.value());
73 }
74 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(GrTFlagsMask<TFlags> a,
75                                                                    TFlags b) {
76     return GrTFlagsMask<TFlags>(a.value() | static_cast<int>(b));
77 }
78 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator|(TFlags a,
79                                                                    GrTFlagsMask<TFlags> b) {
80     return GrTFlagsMask<TFlags>(static_cast<int>(a) | b.value());
81 }
82 template<typename TFlags> inline GrTFlagsMask<TFlags>& operator|=(GrTFlagsMask<TFlags>& a,
83                                                                   GrTFlagsMask<TFlags> b) {
84     return (a = a | b);
85 }
86 
87 // And-ing two masks returns another mask; and-ing one with regular flags returns flags.
88 template<typename TFlags> constexpr GrTFlagsMask<TFlags> operator&(GrTFlagsMask<TFlags> a,
89                                                                    GrTFlagsMask<TFlags> b) {
90     return GrTFlagsMask<TFlags>(a.value() & b.value());
91 }
92 template<typename TFlags> constexpr TFlags operator&(GrTFlagsMask<TFlags> a, TFlags b) {
93     return static_cast<TFlags>(a.value() & static_cast<int>(b));
94 }
95 template<typename TFlags> constexpr TFlags operator&(TFlags a, GrTFlagsMask<TFlags> b) {
96     return static_cast<TFlags>(static_cast<int>(a) & b.value());
97 }
98 template<typename TFlags> inline TFlags& operator&=(TFlags& a, GrTFlagsMask<TFlags> b) {
99     return (a = a & b);
100 }
101 
102 /**
103  * Defines bitwise operators that make it possible to use an enum class as a
104  * basic bitfield.
105  */
106 #define GR_MAKE_BITFIELD_CLASS_OPS(X) \
107     constexpr GrTFlagsMask<X> operator~(X a) { \
108         return GrTFlagsMask<X>(~static_cast<int>(a)); \
109     } \
110     constexpr X operator|(X a, X b) { \
111         return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \
112     } \
113     inline X& operator|=(X& a, X b) { \
114         return (a = a | b); \
115     } \
116     constexpr bool operator&(X a, X b) { \
117         return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \
118     } \
119 
120 #define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
121     friend constexpr GrTFlagsMask<X> operator ~(X); \
122     friend constexpr X operator |(X, X); \
123     friend X& operator |=(X&, X); \
124     friend constexpr bool operator &(X, X);
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 
128 // compile time versions of min/max
129 #define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
130 #define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
131 
132 /**
133  *  divide, rounding up
134  */
GrIDivRoundUp(int x,int y)135 static inline int32_t GrIDivRoundUp(int x, int y) {
136     SkASSERT(y > 0);
137     return (x + (y-1)) / y;
138 }
GrUIDivRoundUp(uint32_t x,uint32_t y)139 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
140     return (x + (y-1)) / y;
141 }
GrSizeDivRoundUp(size_t x,size_t y)142 static inline size_t GrSizeDivRoundUp(size_t x, size_t y) {
143     return (x + (y-1)) / y;
144 }
145 
146 // compile time, evaluates Y multiple times
147 #define GR_CT_DIV_ROUND_UP(X, Y) (((X) + ((Y)-1)) / (Y))
148 
149 /**
150  *  align up
151  */
GrUIAlignUp(uint32_t x,uint32_t alignment)152 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
153     return GrUIDivRoundUp(x, alignment) * alignment;
154 }
GrSizeAlignUp(size_t x,size_t alignment)155 static inline size_t GrSizeAlignUp(size_t x, size_t alignment) {
156     return GrSizeDivRoundUp(x, alignment) * alignment;
157 }
158 
159 // compile time, evaluates A multiple times
160 #define GR_CT_ALIGN_UP(X, A) (GR_CT_DIV_ROUND_UP((X),(A)) * (A))
161 
162 /**
163  * amount of pad needed to align up
164  */
GrUIAlignUpPad(uint32_t x,uint32_t alignment)165 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
166     return (alignment - x % alignment) % alignment;
167 }
GrSizeAlignUpPad(size_t x,size_t alignment)168 static inline size_t GrSizeAlignUpPad(size_t x, size_t alignment) {
169     return (alignment - x % alignment) % alignment;
170 }
171 
172 /**
173  *  align down
174  */
GrUIAlignDown(uint32_t x,uint32_t alignment)175 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
176     return (x / alignment) * alignment;
177 }
GrSizeAlignDown(size_t x,uint32_t alignment)178 static inline size_t GrSizeAlignDown(size_t x, uint32_t alignment) {
179     return (x / alignment) * alignment;
180 }
181 
182 ///////////////////////////////////////////////////////////////////////////////
183 
184 /**
185  * Possible 3D APIs that may be used by Ganesh.
186  */
187 enum GrBackend {
188     kOpenGL_GrBackend,
189     kVulkan_GrBackend,
190 
191     kLast_GrBackend = kVulkan_GrBackend
192 };
193 const int kBackendCount = kLast_GrBackend + 1;
194 
195 /**
196  * Backend-specific 3D context handle
197  *      GrGLInterface* for OpenGL. If NULL will use the default GL interface.
198  *      GrVkBackendContext* for Vulkan.
199  */
200 typedef intptr_t GrBackendContext;
201 
202 ///////////////////////////////////////////////////////////////////////////////
203 
204 /**
205  * Used to control antialiasing in draw calls.
206  */
207 enum class GrAA {
208     kYes,
209     kNo
210 };
211 
GrBoolToAA(bool aa)212 static inline GrAA GrBoolToAA(bool aa) { return aa ? GrAA::kYes : GrAA::kNo; }
213 
214 ///////////////////////////////////////////////////////////////////////////////
215 
216 /**
217 * Geometric primitives used for drawing.
218 */
219 enum GrPrimitiveType {
220     kTriangles_GrPrimitiveType,
221     kTriangleStrip_GrPrimitiveType,
222     kTriangleFan_GrPrimitiveType,
223     kPoints_GrPrimitiveType,
224     kLines_GrPrimitiveType,     // 1 pix wide only
225     kLineStrip_GrPrimitiveType, // 1 pix wide only
226     kLast_GrPrimitiveType = kLineStrip_GrPrimitiveType
227 };
228 
GrIsPrimTypeLines(GrPrimitiveType type)229 static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
230     return kLines_GrPrimitiveType == type || kLineStrip_GrPrimitiveType == type;
231 }
232 
GrIsPrimTypeTris(GrPrimitiveType type)233 static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
234     return kTriangles_GrPrimitiveType == type     ||
235            kTriangleStrip_GrPrimitiveType == type ||
236            kTriangleFan_GrPrimitiveType == type;
237 }
238 
239 /**
240  *  Formats for masks, used by the font cache.
241  *  Important that these are 0-based.
242  */
243 enum GrMaskFormat {
244     kA8_GrMaskFormat,    //!< 1-byte per pixel
245     kA565_GrMaskFormat,  //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
246     kARGB_GrMaskFormat,  //!< 4-bytes per pixel, color format
247 
248     kLast_GrMaskFormat = kARGB_GrMaskFormat
249 };
250 static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
251 
252 /**
253  *  Return the number of bytes-per-pixel for the specified mask format.
254  */
GrMaskFormatBytesPerPixel(GrMaskFormat format)255 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
256     SkASSERT(format < kMaskFormatCount);
257     // kA8   (0) -> 1
258     // kA565 (1) -> 2
259     // kARGB (2) -> 4
260     static const int sBytesPerPixel[] = { 1, 2, 4 };
261     static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
262     static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
263     static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
264     static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
265 
266     return sBytesPerPixel[(int) format];
267 }
268 
269 /**
270  * Pixel configurations.
271  */
272 enum GrPixelConfig {
273     kUnknown_GrPixelConfig,
274     kAlpha_8_GrPixelConfig,
275     kGray_8_GrPixelConfig,
276     kRGB_565_GrPixelConfig,
277     /**
278      * Premultiplied
279      */
280     kRGBA_4444_GrPixelConfig,
281     /**
282      * Premultiplied. Byte order is r,g,b,a.
283      */
284     kRGBA_8888_GrPixelConfig,
285     /**
286      * Premultiplied. Byte order is b,g,r,a.
287      */
288     kBGRA_8888_GrPixelConfig,
289     /**
290      * Premultiplied and sRGB. Byte order is r,g,b,a.
291      */
292     kSRGBA_8888_GrPixelConfig,
293     /**
294      * Premultiplied and sRGB. Byte order is b,g,r,a.
295      */
296     kSBGRA_8888_GrPixelConfig,
297     /**
298      * 8 bit signed integers per-channel. Byte order is b,g,r,a.
299      */
300     kRGBA_8888_sint_GrPixelConfig,
301     /**
302      * ETC1 Compressed Data
303      */
304     kETC1_GrPixelConfig,
305     /**
306      * Byte order is r, g, b, a.  This color format is 32 bits per channel
307      */
308     kRGBA_float_GrPixelConfig,
309     /**
310      * Byte order is r, g.  This color format is 32 bits per channel
311      */
312     kRG_float_GrPixelConfig,
313 
314     /**
315      * This color format is a single 16 bit float channel
316      */
317     kAlpha_half_GrPixelConfig,
318 
319     /**
320     * Byte order is r, g, b, a.  This color format is 16 bits per channel
321     */
322     kRGBA_half_GrPixelConfig,
323 
324     kLast_GrPixelConfig = kRGBA_half_GrPixelConfig
325 };
326 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
327 
328 // Aliases for pixel configs that match skia's byte order.
329 #ifndef SK_CPU_LENDIAN
330     #error "Skia gpu currently assumes little endian"
331 #endif
332 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
333     static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
334 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
335     static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
336 #else
337     #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
338 #endif
339 
340 // Returns true if the pixel config is a GPU-specific compressed format
341 // representation.
GrPixelConfigIsCompressed(GrPixelConfig config)342 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
343     switch (config) {
344         case kETC1_GrPixelConfig:
345             return true;
346         case kUnknown_GrPixelConfig:
347         case kAlpha_8_GrPixelConfig:
348         case kGray_8_GrPixelConfig:
349         case kRGB_565_GrPixelConfig:
350         case kRGBA_4444_GrPixelConfig:
351         case kRGBA_8888_GrPixelConfig:
352         case kBGRA_8888_GrPixelConfig:
353         case kSRGBA_8888_GrPixelConfig:
354         case kSBGRA_8888_GrPixelConfig:
355         case kRGBA_8888_sint_GrPixelConfig:
356         case kRGBA_float_GrPixelConfig:
357         case kRG_float_GrPixelConfig:
358         case kAlpha_half_GrPixelConfig:
359         case kRGBA_half_GrPixelConfig:
360             return false;
361     }
362     SkFAIL("Invalid pixel config");
363     return false;
364 }
365 
366 /** If the pixel config is compressed, return an equivalent uncompressed format. */
GrMakePixelConfigUncompressed(GrPixelConfig config)367 static inline GrPixelConfig GrMakePixelConfigUncompressed(GrPixelConfig config) {
368     switch (config) {
369         case kETC1_GrPixelConfig:
370             return kRGBA_8888_GrPixelConfig;
371         case kUnknown_GrPixelConfig:
372         case kAlpha_8_GrPixelConfig:
373         case kGray_8_GrPixelConfig:
374         case kRGB_565_GrPixelConfig:
375         case kRGBA_4444_GrPixelConfig:
376         case kRGBA_8888_GrPixelConfig:
377         case kBGRA_8888_GrPixelConfig:
378         case kSRGBA_8888_GrPixelConfig:
379         case kSBGRA_8888_GrPixelConfig:
380         case kRGBA_8888_sint_GrPixelConfig:
381         case kRGBA_float_GrPixelConfig:
382         case kRG_float_GrPixelConfig:
383         case kAlpha_half_GrPixelConfig:
384         case kRGBA_half_GrPixelConfig:
385             SkASSERT(!GrPixelConfigIsCompressed(config));
386             return config;
387     }
388     SkFAIL("Invalid pixel config");
389     return config;
390 }
391 
392 // Returns true if the pixel config is 32 bits per pixel
GrPixelConfigIs8888Unorm(GrPixelConfig config)393 static inline bool GrPixelConfigIs8888Unorm(GrPixelConfig config) {
394     switch (config) {
395         case kRGBA_8888_GrPixelConfig:
396         case kBGRA_8888_GrPixelConfig:
397         case kSRGBA_8888_GrPixelConfig:
398         case kSBGRA_8888_GrPixelConfig:
399             return true;
400         case kUnknown_GrPixelConfig:
401         case kAlpha_8_GrPixelConfig:
402         case kGray_8_GrPixelConfig:
403         case kRGB_565_GrPixelConfig:
404         case kRGBA_4444_GrPixelConfig:
405         case kRGBA_8888_sint_GrPixelConfig:
406         case kETC1_GrPixelConfig:
407         case kRGBA_float_GrPixelConfig:
408         case kRG_float_GrPixelConfig:
409         case kAlpha_half_GrPixelConfig:
410         case kRGBA_half_GrPixelConfig:
411             return false;
412     }
413     SkFAIL("Invalid pixel config");
414     return false;
415 }
416 
417 // Returns true if the color (non-alpha) components represent sRGB values. It does NOT indicate that
418 // all three color components are present in the config or anything about their order.
GrPixelConfigIsSRGB(GrPixelConfig config)419 static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
420     switch (config) {
421         case kSRGBA_8888_GrPixelConfig:
422         case kSBGRA_8888_GrPixelConfig:
423             return true;
424         case kUnknown_GrPixelConfig:
425         case kAlpha_8_GrPixelConfig:
426         case kGray_8_GrPixelConfig:
427         case kRGB_565_GrPixelConfig:
428         case kRGBA_4444_GrPixelConfig:
429         case kRGBA_8888_GrPixelConfig:
430         case kBGRA_8888_GrPixelConfig:
431         case kRGBA_8888_sint_GrPixelConfig:
432         case kETC1_GrPixelConfig:
433         case kRGBA_float_GrPixelConfig:
434         case kRG_float_GrPixelConfig:
435         case kAlpha_half_GrPixelConfig:
436         case kRGBA_half_GrPixelConfig:
437             return false;
438     }
439     SkFAIL("Invalid pixel config");
440     return false;
441 }
442 
443 // Takes a config and returns the equivalent config with the R and B order
444 // swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
GrPixelConfigSwapRAndB(GrPixelConfig config)445 static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
446     switch (config) {
447         case kBGRA_8888_GrPixelConfig:
448             return kRGBA_8888_GrPixelConfig;
449         case kRGBA_8888_GrPixelConfig:
450             return kBGRA_8888_GrPixelConfig;
451         case kSBGRA_8888_GrPixelConfig:
452             return kSRGBA_8888_GrPixelConfig;
453         case kSRGBA_8888_GrPixelConfig:
454             return kSBGRA_8888_GrPixelConfig;
455         case kUnknown_GrPixelConfig:
456         case kAlpha_8_GrPixelConfig:
457         case kGray_8_GrPixelConfig:
458         case kRGB_565_GrPixelConfig:
459         case kRGBA_4444_GrPixelConfig:
460         case kRGBA_8888_sint_GrPixelConfig:
461         case kETC1_GrPixelConfig:
462         case kRGBA_float_GrPixelConfig:
463         case kRG_float_GrPixelConfig:
464         case kAlpha_half_GrPixelConfig:
465         case kRGBA_half_GrPixelConfig:
466             return kUnknown_GrPixelConfig;
467     }
468     SkFAIL("Invalid pixel config");
469     return kUnknown_GrPixelConfig;
470 }
471 
GrBytesPerPixel(GrPixelConfig config)472 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
473     SkASSERT(!GrPixelConfigIsCompressed(config));
474     switch (config) {
475         case kAlpha_8_GrPixelConfig:
476         case kGray_8_GrPixelConfig:
477             return 1;
478         case kRGB_565_GrPixelConfig:
479         case kRGBA_4444_GrPixelConfig:
480         case kAlpha_half_GrPixelConfig:
481             return 2;
482         case kRGBA_8888_GrPixelConfig:
483         case kBGRA_8888_GrPixelConfig:
484         case kSRGBA_8888_GrPixelConfig:
485         case kSBGRA_8888_GrPixelConfig:
486         case kRGBA_8888_sint_GrPixelConfig:
487             return 4;
488         case kRGBA_half_GrPixelConfig:
489             return 8;
490         case kRGBA_float_GrPixelConfig:
491             return 16;
492         case kRG_float_GrPixelConfig:
493             return 8;
494         case kUnknown_GrPixelConfig:
495         case kETC1_GrPixelConfig:
496             return 0;
497     }
498     SkFAIL("Invalid pixel config");
499     return 0;
500 }
501 
GrPixelConfigIsOpaque(GrPixelConfig config)502 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
503     switch (config) {
504         case kETC1_GrPixelConfig:
505         case kRGB_565_GrPixelConfig:
506         case kGray_8_GrPixelConfig:
507             return true;
508         case kAlpha_8_GrPixelConfig:
509         case kRGBA_4444_GrPixelConfig:
510         case kAlpha_half_GrPixelConfig:
511         case kRGBA_8888_GrPixelConfig:
512         case kBGRA_8888_GrPixelConfig:
513         case kSRGBA_8888_GrPixelConfig:
514         case kSBGRA_8888_GrPixelConfig:
515         case kRGBA_8888_sint_GrPixelConfig:
516         case kRGBA_half_GrPixelConfig:
517         case kRGBA_float_GrPixelConfig:
518         case kRG_float_GrPixelConfig:
519         case kUnknown_GrPixelConfig:
520             return false;
521     }
522     SkFAIL("Invalid pixel config");
523     return false;
524 }
525 
GrPixelConfigIsAlphaOnly(GrPixelConfig config)526 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
527     switch (config) {
528         case kAlpha_8_GrPixelConfig:
529         case kAlpha_half_GrPixelConfig:
530             return true;
531         case kUnknown_GrPixelConfig:
532         case kGray_8_GrPixelConfig:
533         case kRGB_565_GrPixelConfig:
534         case kRGBA_4444_GrPixelConfig:
535         case kRGBA_8888_GrPixelConfig:
536         case kBGRA_8888_GrPixelConfig:
537         case kSRGBA_8888_GrPixelConfig:
538         case kSBGRA_8888_GrPixelConfig:
539         case kRGBA_8888_sint_GrPixelConfig:
540         case kETC1_GrPixelConfig:
541         case kRGBA_float_GrPixelConfig:
542         case kRG_float_GrPixelConfig:
543         case kRGBA_half_GrPixelConfig:
544             return false;
545     }
546     SkFAIL("Invalid pixel config.");
547     return false;
548 }
549 
GrPixelConfigIsFloatingPoint(GrPixelConfig config)550 static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
551     switch (config) {
552         case kRGBA_float_GrPixelConfig:
553         case kRG_float_GrPixelConfig:
554         case kAlpha_half_GrPixelConfig:
555         case kRGBA_half_GrPixelConfig:
556             return true;
557         case kUnknown_GrPixelConfig:
558         case kAlpha_8_GrPixelConfig:
559         case kGray_8_GrPixelConfig:
560         case kRGB_565_GrPixelConfig:
561         case kRGBA_4444_GrPixelConfig:
562         case kRGBA_8888_GrPixelConfig:
563         case kBGRA_8888_GrPixelConfig:
564         case kSRGBA_8888_GrPixelConfig:
565         case kSBGRA_8888_GrPixelConfig:
566         case kRGBA_8888_sint_GrPixelConfig:
567         case kETC1_GrPixelConfig:
568             return false;
569     }
570     SkFAIL("Invalid pixel config");
571     return false;
572 }
573 
GrPixelConfigIsSint(GrPixelConfig config)574 static inline bool GrPixelConfigIsSint(GrPixelConfig config) {
575     return config == kRGBA_8888_sint_GrPixelConfig;
576 }
577 
578 /**
579  * Optional bitfield flags that can be set on GrSurfaceDesc (below).
580  */
581 enum GrSurfaceFlags {
582     kNone_GrSurfaceFlags            = 0x0,
583     /**
584      * Creates a texture that can be rendered to as a GrRenderTarget. Use
585      * GrTexture::asRenderTarget() to access.
586      */
587     kRenderTarget_GrSurfaceFlag     = 0x1,
588     /**
589      * Placeholder for managing zero-copy textures
590      */
591     kZeroCopy_GrSurfaceFlag         = 0x2,
592     /**
593      * Indicates that all allocations (color buffer, FBO completeness, etc)
594      * should be verified.
595      */
596     kCheckAllocation_GrSurfaceFlag  = 0x4,
597 };
598 
599 GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
600 
601 // opaque type for 3D API object handles
602 typedef intptr_t GrBackendObject;
603 
604 /**
605  * Some textures will be stored such that the upper and left edges of the content meet at the
606  * the origin (in texture coord space) and for other textures the lower and left edges meet at
607  * the origin. kDefault_GrSurfaceOrigin sets textures to TopLeft, and render targets
608  * to BottomLeft.
609  */
610 
611 enum GrSurfaceOrigin {
612     kDefault_GrSurfaceOrigin,         // DEPRECATED; to be removed
613     kTopLeft_GrSurfaceOrigin,
614     kBottomLeft_GrSurfaceOrigin,
615 };
616 
617 struct GrMipLevel {
618     const void* fPixels;
619     size_t fRowBytes;
620 };
621 
622 /**
623  * Describes a surface to be created.
624  */
625 struct GrSurfaceDesc {
GrSurfaceDescGrSurfaceDesc626     GrSurfaceDesc()
627     : fFlags(kNone_GrSurfaceFlags)
628     , fOrigin(kDefault_GrSurfaceOrigin)
629     , fWidth(0)
630     , fHeight(0)
631     , fConfig(kUnknown_GrPixelConfig)
632     , fSampleCnt(0)
633     , fIsMipMapped(false) {
634     }
635 
636     GrSurfaceFlags         fFlags;  //!< bitfield of TextureFlags
637     GrSurfaceOrigin        fOrigin; //!< origin of the texture
638     int                    fWidth;  //!< Width of the texture
639     int                    fHeight; //!< Height of the texture
640 
641     /**
642      * Format of source data of the texture. Not guaranteed to be the same as
643      * internal format used by 3D API.
644      */
645     GrPixelConfig          fConfig;
646 
647     /**
648      * The number of samples per pixel or 0 to disable full scene AA. This only
649      * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
650      * of samples may not exactly match the request. The request will be rounded
651      * up to the next supported sample count, or down if it is larger than the
652      * max supported count.
653      */
654     int                    fSampleCnt;
655     bool                   fIsMipMapped; //!< Indicates if the texture has mipmaps
656 };
657 
658 // Legacy alias
659 typedef GrSurfaceDesc GrTextureDesc;
660 
661 /**
662  * Clips are composed from these objects.
663  */
664 enum GrClipType {
665     kRect_ClipType,
666     kPath_ClipType
667 };
668 
669 ///////////////////////////////////////////////////////////////////////////////
670 
671 
672 /** Ownership rules for external GPU resources imported into Skia. */
673 enum GrWrapOwnership {
674     /** Skia will assume the client will keep the resource alive and Skia will not free it. */
675     kBorrow_GrWrapOwnership,
676 
677     /** Skia will assume ownership of the resource and free it. */
678     kAdopt_GrWrapOwnership,
679 
680     /** Skia will assume ownership of the resource, free it, and reuse it within the cache. */
681     kAdoptAndCache_GrWrapOwnership,
682 };
683 
684 /**
685  * Gr can wrap an existing texture created by the client with a GrTexture
686  * object. The client is responsible for ensuring that the texture lives at
687  * least as long as the GrTexture object wrapping it. We require the client to
688  * explicitly provide information about the texture, such as width, height,
689  * and pixel config, rather than querying the 3D APIfor these values. We expect
690  * these to be immutable even if the 3D API doesn't require this (OpenGL).
691  *
692  * Textures that are also render targets are supported as well. Gr will manage
693  * any ancillary 3D API (stencil buffer, FBO id, etc) objects necessary for
694  * Gr to draw into the render target. To access the render target object
695  * call GrTexture::asRenderTarget().
696  *
697  * If in addition to the render target flag, the caller also specifies a sample
698  * count Gr will create an MSAA buffer that resolves into the texture. Gr auto-
699  * resolves when it reads from the texture. The client can explictly resolve
700  * using the GrRenderTarget interface.
701  *
702  * Note: These flags currently form a subset of GrTexture's flags.
703  */
704 
705 enum GrBackendTextureFlags {
706     /**
707      * No flags enabled
708      */
709     kNone_GrBackendTextureFlag             = 0,
710     /**
711      * Indicates that the texture is also a render target, and thus should have
712      * a GrRenderTarget object.
713      */
714     kRenderTarget_GrBackendTextureFlag     = kRenderTarget_GrSurfaceFlag,
715 };
716 GR_MAKE_BITFIELD_OPS(GrBackendTextureFlags)
717 
718 struct GrBackendTextureDesc {
GrBackendTextureDescGrBackendTextureDesc719     GrBackendTextureDesc() { memset(this, 0, sizeof(*this)); }
720     GrBackendTextureFlags           fFlags;
721     GrSurfaceOrigin                 fOrigin;
722     int                             fWidth;         //<! width in pixels
723     int                             fHeight;        //<! height in pixels
724     GrPixelConfig                   fConfig;        //<! color format
725     /**
726      * If the render target flag is set and sample count is greater than 0
727      * then Gr will create an MSAA buffer that resolves to the texture.
728      */
729     int                             fSampleCnt;
730     /**
731      * Handle to the 3D API object.
732      * OpenGL: Texture ID.
733      * Vulkan: GrVkImageInfo*
734      */
735     GrBackendObject                 fTextureHandle;
736 };
737 
738 ///////////////////////////////////////////////////////////////////////////////
739 
740 /**
741  * Gr can wrap an existing render target created by the client in the 3D API
742  * with a GrRenderTarget object. The client is responsible for ensuring that the
743  * underlying 3D API object lives at least as long as the GrRenderTarget object
744  * wrapping it. We require the client to explicitly provide information about
745  * the target, such as width, height, and pixel config rather than querying the
746  * 3D API for these values. We expect these properties to be immutable even if
747  * the 3D API doesn't require this (OpenGL).
748  */
749 
750 struct GrBackendRenderTargetDesc {
GrBackendRenderTargetDescGrBackendRenderTargetDesc751     GrBackendRenderTargetDesc() { memset(this, 0, sizeof(*this)); }
752     int                             fWidth;         //<! width in pixels
753     int                             fHeight;        //<! height in pixels
754     GrPixelConfig                   fConfig;        //<! color format
755     GrSurfaceOrigin                 fOrigin;        //<! pixel origin
756     /**
757      * The number of samples per pixel. Gr uses this to influence decisions
758      * about applying other forms of anti-aliasing.
759      */
760     int                             fSampleCnt;
761     /**
762      * Number of bits of stencil per-pixel.
763      */
764     int                             fStencilBits;
765     /**
766      * Handle to the 3D API object.
767      * OpenGL: FBO ID
768      * Vulkan: GrVkImageInfo*
769      */
770     GrBackendObject                 fRenderTargetHandle;
771 };
772 
773 /**
774  * The GrContext's cache of backend context state can be partially invalidated.
775  * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
776  */
777 enum GrGLBackendState {
778     kRenderTarget_GrGLBackendState     = 1 << 0,
779     kTextureBinding_GrGLBackendState   = 1 << 1,
780     // View state stands for scissor and viewport
781     kView_GrGLBackendState             = 1 << 2,
782     kBlend_GrGLBackendState            = 1 << 3,
783     kMSAAEnable_GrGLBackendState       = 1 << 4,
784     kVertex_GrGLBackendState           = 1 << 5,
785     kStencil_GrGLBackendState          = 1 << 6,
786     kPixelStore_GrGLBackendState       = 1 << 7,
787     kProgram_GrGLBackendState          = 1 << 8,
788     kFixedFunction_GrGLBackendState    = 1 << 9,
789     kMisc_GrGLBackendState             = 1 << 10,
790     kPathRendering_GrGLBackendState    = 1 << 11,
791     kALL_GrGLBackendState              = 0xffff
792 };
793 
794 /**
795  * Returns the data size for the given compressed pixel config
796  */
GrCompressedFormatDataSize(GrPixelConfig config,int width,int height)797 static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
798                                                 int width, int height) {
799     SkASSERT(GrPixelConfigIsCompressed(config));
800 
801     switch (config) {
802         case kETC1_GrPixelConfig:
803             SkASSERT((width & 3) == 0);
804             SkASSERT((height & 3) == 0);
805             return (width >> 2) * (height >> 2) * 8;
806 
807         case kUnknown_GrPixelConfig:
808         case kAlpha_8_GrPixelConfig:
809         case kGray_8_GrPixelConfig:
810         case kRGB_565_GrPixelConfig:
811         case kRGBA_4444_GrPixelConfig:
812         case kRGBA_8888_GrPixelConfig:
813         case kBGRA_8888_GrPixelConfig:
814         case kSRGBA_8888_GrPixelConfig:
815         case kSBGRA_8888_GrPixelConfig:
816         case kRGBA_8888_sint_GrPixelConfig:
817         case kRGBA_float_GrPixelConfig:
818         case kRG_float_GrPixelConfig:
819         case kAlpha_half_GrPixelConfig:
820         case kRGBA_half_GrPixelConfig:
821             SkFAIL("Unknown compressed pixel config");
822             return 4 * width * height;
823     }
824 
825     SkFAIL("Invalid pixel config");
826     return 4 * width * height;
827 }
828 
829 /**
830  * This value translates to reseting all the context state for any backend.
831  */
832 static const uint32_t kAll_GrBackendState = 0xffffffff;
833 
834 #endif
835