1 /*
2  * Copyright 2013 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 GrTypesPriv_DEFINED
9 #define GrTypesPriv_DEFINED
10 
11 #include <chrono>
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/gpu/GrTypes.h"
17 #include "include/private/GrSharedEnums.h"
18 #include "include/private/SkImageInfoPriv.h"
19 #include "include/private/SkWeakRefCnt.h"
20 
21 class GrBackendFormat;
22 class GrCaps;
23 
24 // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might
25 // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently
26 // used for idle resource purging so it shouldn't cause a correctness problem.
27 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
28 using GrStdSteadyClock = std::chrono::monotonic_clock;
29 #else
30 using GrStdSteadyClock = std::chrono::steady_clock;
31 #endif
32 
33 /**
34  *  divide, rounding up
35  */
36 
GrSizeDivRoundUp(size_t x,size_t y)37 static inline constexpr size_t GrSizeDivRoundUp(size_t x, size_t y) { return (x + (y - 1)) / y; }
38 
39 /**
40  *  align up to a power of 2
41  */
GrAlignTo(size_t x,size_t alignment)42 static inline constexpr size_t GrAlignTo(size_t x, size_t alignment) {
43     SkASSERT(alignment && SkIsPow2(alignment));
44     return (x + alignment - 1) & ~(alignment - 1);
45 }
46 
47 /**
48  * Geometric primitives used for drawing.
49  */
50 enum class GrPrimitiveType : uint8_t {
51     kTriangles,
52     kTriangleStrip,
53     kPoints,
54     kLines,          // 1 pix wide only
55     kLineStrip,      // 1 pix wide only
56     kPatches,
57     kPath
58 };
59 static constexpr int kNumGrPrimitiveTypes = (int)GrPrimitiveType::kPath + 1;
60 
GrIsPrimTypeLines(GrPrimitiveType type)61 static constexpr bool GrIsPrimTypeLines(GrPrimitiveType type) {
62     return GrPrimitiveType::kLines == type || GrPrimitiveType::kLineStrip == type;
63 }
64 
GrIsPrimTypeTris(GrPrimitiveType type)65 static constexpr bool GrIsPrimTypeTris(GrPrimitiveType type) {
66     return GrPrimitiveType::kTriangles == type || GrPrimitiveType::kTriangleStrip == type;
67 }
68 
69 enum class GrPrimitiveRestart : bool {
70     kNo = false,
71     kYes = true
72 };
73 
74 /**
75  * Should a created surface be texturable?
76  */
77 enum class GrTexturable : bool {
78     kNo = false,
79     kYes = true
80 };
81 
82 /**
83  *  Formats for masks, used by the font cache. Important that these are 0-based.
84  */
85 enum GrMaskFormat {
86     kA8_GrMaskFormat,    //!< 1-byte per pixel
87     kA565_GrMaskFormat,  //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
88     kARGB_GrMaskFormat,  //!< 4-bytes per pixel, color format
89 
90     kLast_GrMaskFormat = kARGB_GrMaskFormat
91 };
92 static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
93 
94 /**
95  *  Return the number of bytes-per-pixel for the specified mask format.
96  */
GrMaskFormatBytesPerPixel(GrMaskFormat format)97 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
98     SkASSERT(format < kMaskFormatCount);
99     // kA8   (0) -> 1
100     // kA565 (1) -> 2
101     // kARGB (2) -> 4
102     static const int sBytesPerPixel[] = {1, 2, 4};
103     static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
104     static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
105     static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
106     static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
107 
108     return sBytesPerPixel[(int)format];
109 }
110 
111 /** Ownership rules for external GPU resources imported into Skia. */
112 enum GrWrapOwnership {
113     /** Skia will assume the client will keep the resource alive and Skia will not free it. */
114     kBorrow_GrWrapOwnership,
115 
116     /** Skia will assume ownership of the resource and free it. */
117     kAdopt_GrWrapOwnership,
118 };
119 
120 enum class GrWrapCacheable : bool {
121     /**
122      * The wrapped resource will be removed from the cache as soon as it becomes purgeable. It may
123      * still be assigned and found by a unique key, but the presence of the key will not be used to
124      * keep the resource alive when it has no references.
125      */
126     kNo = false,
127     /**
128      * The wrapped resource is allowed to remain in the GrResourceCache when it has no references
129      * but has a unique key. Such resources should only be given unique keys when it is known that
130      * the key will eventually be removed from the resource or invalidated via the message bus.
131      */
132     kYes = true
133 };
134 
135 enum class GrBudgetedType : uint8_t {
136     /** The resource is budgeted and is subject to purging under budget pressure. */
137     kBudgeted,
138     /**
139      * The resource is unbudgeted and is purged as soon as it has no refs regardless of whether
140      * it has a unique or scratch key.
141      */
142     kUnbudgetedUncacheable,
143     /**
144      * The resource is unbudgeted and is allowed to remain in the cache with no refs if it
145      * has a unique key. Scratch keys are ignored.
146      */
147     kUnbudgetedCacheable,
148 };
149 
150 /**
151  * Clips are composed from these objects.
152  */
153 enum GrClipType {
154     kRect_ClipType,
155     kPath_ClipType
156 };
157 
158 enum class GrScissorTest : bool {
159     kDisabled = false,
160     kEnabled = true
161 };
162 
163 struct GrMipLevel {
164     const void* fPixels = nullptr;
165     size_t fRowBytes = 0;
166 };
167 
168 /**
169  * This enum is used to specify the load operation to be used when an GrOpsTask/GrOpsRenderPass
170  * begins execution.
171  */
172 enum class GrLoadOp {
173     kLoad,
174     kClear,
175     kDiscard,
176 };
177 
178 /**
179  * This enum is used to specify the store operation to be used when an GrOpsTask/GrOpsRenderPass
180  * ends execution.
181  */
182 enum class GrStoreOp {
183     kStore,
184     kDiscard,
185 };
186 
187 /**
188  * Used to control antialiasing in draw calls.
189  */
190 enum class GrAA : bool {
191     kNo = false,
192     kYes = true
193 };
194 
195 enum class GrFillRule : bool {
196     kNonzero,
197     kEvenOdd
198 };
199 
GrFillRuleForSkPath(const SkPath & path)200 inline GrFillRule GrFillRuleForSkPath(const SkPath& path) {
201     switch (path.getFillType()) {
202         case SkPathFillType::kWinding:
203         case SkPathFillType::kInverseWinding:
204             return GrFillRule::kNonzero;
205         case SkPathFillType::kEvenOdd:
206         case SkPathFillType::kInverseEvenOdd:
207             return GrFillRule::kEvenOdd;
208     }
209     SkUNREACHABLE;
210 }
211 
212 /** This enum indicates the type of antialiasing to be performed. */
213 enum class GrAAType : unsigned {
214     /** No antialiasing */
215     kNone,
216     /** Use fragment shader code or mixed samples to blend with a fractional pixel coverage. */
217     kCoverage,
218     /** Use normal MSAA. */
219     kMSAA,
220 
221     kLast = kMSAA
222 };
223 static const int kGrAATypeCount = static_cast<int>(GrAAType::kLast) + 1;
224 
GrAATypeIsHW(GrAAType type)225 static constexpr bool GrAATypeIsHW(GrAAType type) {
226     switch (type) {
227         case GrAAType::kNone:
228             return false;
229         case GrAAType::kCoverage:
230             return false;
231         case GrAAType::kMSAA:
232             return true;
233     }
234     SkUNREACHABLE;
235 }
236 
237 /**
238  * Some pixel configs are inherently clamped to [0,1], some are allowed to go outside that range,
239  * and some are FP but manually clamped in the XP.
240  */
241 enum class GrClampType {
242     kAuto,    // Normalized, fixed-point configs
243     kManual,  // Clamped FP configs
244     kNone,    // Normal (unclamped) FP configs
245 };
246 
247 /**
248  * A number of rectangle/quadrilateral drawing APIs can control anti-aliasing on a per edge basis.
249  * These masks specify which edges are AA'ed. The intent for this is to support tiling with seamless
250  * boundaries, where the inner edges are non-AA and the outer edges are AA. Regular draws (where AA
251  * is specified by GrAA) is almost equivalent to kNone or kAll, with the exception of how MSAA is
252  * handled.
253  *
254  * When tiling and there is MSAA, mixed edge rectangles are processed with MSAA, so in order for the
255  * tiled edges to remain seamless, inner tiles with kNone must also be processed with MSAA. In
256  * regular drawing, however, kNone should disable MSAA (if it's supported) to match the expected
257  * appearance.
258  *
259  * Therefore, APIs that use per-edge AA flags also take a GrAA value so that they can differentiate
260  * between the regular and tiling use case behaviors. Tiling operations should always pass
261  * GrAA::kYes while regular options should pass GrAA based on the SkPaint's anti-alias state.
262  *
263  * These values are identical to SkCanvas::QuadAAFlags.
264  */
265 enum class GrQuadAAFlags {
266     kLeft   = 0b0001,
267     kTop    = 0b0010,
268     kRight  = 0b0100,
269     kBottom = 0b1000,
270 
271     kNone = 0b0000,
272     kAll  = 0b1111,
273 };
274 
GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)275 GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)
276 
277 static inline GrQuadAAFlags SkToGrQuadAAFlags(unsigned flags) {
278     return static_cast<GrQuadAAFlags>(flags);
279 }
280 
281 /**
282  * Types of shader-language-specific boxed variables we can create.
283  */
284 enum GrSLType {
285     kVoid_GrSLType,
286     kBool_GrSLType,
287     kByte_GrSLType,
288     kByte2_GrSLType,
289     kByte3_GrSLType,
290     kByte4_GrSLType,
291     kUByte_GrSLType,
292     kUByte2_GrSLType,
293     kUByte3_GrSLType,
294     kUByte4_GrSLType,
295     kShort_GrSLType,
296     kShort2_GrSLType,
297     kShort3_GrSLType,
298     kShort4_GrSLType,
299     kUShort_GrSLType,
300     kUShort2_GrSLType,
301     kUShort3_GrSLType,
302     kUShort4_GrSLType,
303     kFloat_GrSLType,
304     kFloat2_GrSLType,
305     kFloat3_GrSLType,
306     kFloat4_GrSLType,
307     kFloat2x2_GrSLType,
308     kFloat3x3_GrSLType,
309     kFloat4x4_GrSLType,
310     kHalf_GrSLType,
311     kHalf2_GrSLType,
312     kHalf3_GrSLType,
313     kHalf4_GrSLType,
314     kHalf2x2_GrSLType,
315     kHalf3x3_GrSLType,
316     kHalf4x4_GrSLType,
317     kInt_GrSLType,
318     kInt2_GrSLType,
319     kInt3_GrSLType,
320     kInt4_GrSLType,
321     kUint_GrSLType,
322     kUint2_GrSLType,
323     kTexture2DSampler_GrSLType,
324     kTextureExternalSampler_GrSLType,
325     kTexture2DRectSampler_GrSLType,
326     kTexture2D_GrSLType,
327     kSampler_GrSLType,
328 
329     kLast_GrSLType = kSampler_GrSLType
330 };
331 static const int kGrSLTypeCount = kLast_GrSLType + 1;
332 
333 /**
334  * The type of texture. Backends other than GL currently only use the 2D value but the type must
335  * still be known at the API-neutral layer as it used to determine whether MIP maps, renderability,
336  * and sampling parameters are legal for proxies that will be instantiated with wrapped textures.
337  */
338 enum class GrTextureType {
339     kNone,
340     k2D,
341     /* Rectangle uses unnormalized texture coordinates. */
342     kRectangle,
343     kExternal
344 };
345 
346 enum GrShaderType {
347     kVertex_GrShaderType,
348     kGeometry_GrShaderType,
349     kFragment_GrShaderType,
350 
351     kLastkFragment_GrShaderType = kFragment_GrShaderType
352 };
353 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
354 
355 enum GrShaderFlags {
356     kNone_GrShaderFlags = 0,
357     kVertex_GrShaderFlag = 1,
358     kTessControl_GrShaderFlag = 1 << 2,
359     kTessEvaluation_GrShaderFlag = 1 << 2,
360     kGeometry_GrShaderFlag = 1 << 3,
361     kFragment_GrShaderFlag = 1 << 4
362 };
GR_MAKE_BITFIELD_OPS(GrShaderFlags)363 GR_MAKE_BITFIELD_OPS(GrShaderFlags)
364 
365 /** Is the shading language type float (including vectors/matrices)? */
366 static constexpr bool GrSLTypeIsFloatType(GrSLType type) {
367     switch (type) {
368         case kFloat_GrSLType:
369         case kFloat2_GrSLType:
370         case kFloat3_GrSLType:
371         case kFloat4_GrSLType:
372         case kFloat2x2_GrSLType:
373         case kFloat3x3_GrSLType:
374         case kFloat4x4_GrSLType:
375         case kHalf_GrSLType:
376         case kHalf2_GrSLType:
377         case kHalf3_GrSLType:
378         case kHalf4_GrSLType:
379         case kHalf2x2_GrSLType:
380         case kHalf3x3_GrSLType:
381         case kHalf4x4_GrSLType:
382             return true;
383 
384         case kVoid_GrSLType:
385         case kTexture2DSampler_GrSLType:
386         case kTextureExternalSampler_GrSLType:
387         case kTexture2DRectSampler_GrSLType:
388         case kBool_GrSLType:
389         case kByte_GrSLType:
390         case kByte2_GrSLType:
391         case kByte3_GrSLType:
392         case kByte4_GrSLType:
393         case kUByte_GrSLType:
394         case kUByte2_GrSLType:
395         case kUByte3_GrSLType:
396         case kUByte4_GrSLType:
397         case kShort_GrSLType:
398         case kShort2_GrSLType:
399         case kShort3_GrSLType:
400         case kShort4_GrSLType:
401         case kUShort_GrSLType:
402         case kUShort2_GrSLType:
403         case kUShort3_GrSLType:
404         case kUShort4_GrSLType:
405         case kInt_GrSLType:
406         case kInt2_GrSLType:
407         case kInt3_GrSLType:
408         case kInt4_GrSLType:
409         case kUint_GrSLType:
410         case kUint2_GrSLType:
411         case kTexture2D_GrSLType:
412         case kSampler_GrSLType:
413             return false;
414     }
415     SkUNREACHABLE;
416 }
417 
418 /** If the type represents a single value or vector return the vector length, else -1. */
GrSLTypeVecLength(GrSLType type)419 static constexpr int GrSLTypeVecLength(GrSLType type) {
420     switch (type) {
421         case kFloat_GrSLType:
422         case kHalf_GrSLType:
423         case kBool_GrSLType:
424         case kByte_GrSLType:
425         case kUByte_GrSLType:
426         case kShort_GrSLType:
427         case kUShort_GrSLType:
428         case kInt_GrSLType:
429         case kUint_GrSLType:
430             return 1;
431 
432         case kFloat2_GrSLType:
433         case kHalf2_GrSLType:
434         case kByte2_GrSLType:
435         case kUByte2_GrSLType:
436         case kShort2_GrSLType:
437         case kUShort2_GrSLType:
438         case kInt2_GrSLType:
439         case kUint2_GrSLType:
440             return 2;
441 
442         case kFloat3_GrSLType:
443         case kHalf3_GrSLType:
444         case kByte3_GrSLType:
445         case kUByte3_GrSLType:
446         case kShort3_GrSLType:
447         case kUShort3_GrSLType:
448         case kInt3_GrSLType:
449             return 3;
450 
451         case kFloat4_GrSLType:
452         case kHalf4_GrSLType:
453         case kByte4_GrSLType:
454         case kUByte4_GrSLType:
455         case kShort4_GrSLType:
456         case kUShort4_GrSLType:
457         case kInt4_GrSLType:
458             return 4;
459 
460         case kFloat2x2_GrSLType:
461         case kFloat3x3_GrSLType:
462         case kFloat4x4_GrSLType:
463         case kHalf2x2_GrSLType:
464         case kHalf3x3_GrSLType:
465         case kHalf4x4_GrSLType:
466         case kVoid_GrSLType:
467         case kTexture2DSampler_GrSLType:
468         case kTextureExternalSampler_GrSLType:
469         case kTexture2DRectSampler_GrSLType:
470         case kTexture2D_GrSLType:
471         case kSampler_GrSLType:
472             return -1;
473     }
474     SkUNREACHABLE;
475 }
476 
GrSLCombinedSamplerTypeForTextureType(GrTextureType type)477 static inline GrSLType GrSLCombinedSamplerTypeForTextureType(GrTextureType type) {
478     switch (type) {
479         case GrTextureType::k2D:
480             return kTexture2DSampler_GrSLType;
481         case GrTextureType::kRectangle:
482             return kTexture2DRectSampler_GrSLType;
483         case GrTextureType::kExternal:
484             return kTextureExternalSampler_GrSLType;
485         default:
486             SK_ABORT("Unexpected texture type");
487     }
488 }
489 
490 /** Rectangle and external textures only support the clamp wrap mode and do not support
491  *  MIP maps.
492  */
GrTextureTypeHasRestrictedSampling(GrTextureType type)493 static inline bool GrTextureTypeHasRestrictedSampling(GrTextureType type) {
494     switch (type) {
495         case GrTextureType::k2D:
496             return false;
497         case GrTextureType::kRectangle:
498             return true;
499         case GrTextureType::kExternal:
500             return true;
501         default:
502             SK_ABORT("Unexpected texture type");
503     }
504 }
505 
GrSLTypeIsCombinedSamplerType(GrSLType type)506 static constexpr bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
507     switch (type) {
508         case kTexture2DSampler_GrSLType:
509         case kTextureExternalSampler_GrSLType:
510         case kTexture2DRectSampler_GrSLType:
511             return true;
512 
513         case kVoid_GrSLType:
514         case kFloat_GrSLType:
515         case kFloat2_GrSLType:
516         case kFloat3_GrSLType:
517         case kFloat4_GrSLType:
518         case kFloat2x2_GrSLType:
519         case kFloat3x3_GrSLType:
520         case kFloat4x4_GrSLType:
521         case kHalf_GrSLType:
522         case kHalf2_GrSLType:
523         case kHalf3_GrSLType:
524         case kHalf4_GrSLType:
525         case kHalf2x2_GrSLType:
526         case kHalf3x3_GrSLType:
527         case kHalf4x4_GrSLType:
528         case kInt_GrSLType:
529         case kInt2_GrSLType:
530         case kInt3_GrSLType:
531         case kInt4_GrSLType:
532         case kUint_GrSLType:
533         case kUint2_GrSLType:
534         case kBool_GrSLType:
535         case kByte_GrSLType:
536         case kByte2_GrSLType:
537         case kByte3_GrSLType:
538         case kByte4_GrSLType:
539         case kUByte_GrSLType:
540         case kUByte2_GrSLType:
541         case kUByte3_GrSLType:
542         case kUByte4_GrSLType:
543         case kShort_GrSLType:
544         case kShort2_GrSLType:
545         case kShort3_GrSLType:
546         case kShort4_GrSLType:
547         case kUShort_GrSLType:
548         case kUShort2_GrSLType:
549         case kUShort3_GrSLType:
550         case kUShort4_GrSLType:
551         case kTexture2D_GrSLType:
552         case kSampler_GrSLType:
553             return false;
554     }
555     SkUNREACHABLE;
556 }
557 
558 //////////////////////////////////////////////////////////////////////////////
559 
560 /**
561  * Types used to describe format of vertices in arrays.
562  */
563 enum GrVertexAttribType {
564     kFloat_GrVertexAttribType = 0,
565     kFloat2_GrVertexAttribType,
566     kFloat3_GrVertexAttribType,
567     kFloat4_GrVertexAttribType,
568     kHalf_GrVertexAttribType,
569     kHalf2_GrVertexAttribType,
570     kHalf4_GrVertexAttribType,
571 
572     kInt2_GrVertexAttribType,   // vector of 2 32-bit ints
573     kInt3_GrVertexAttribType,   // vector of 3 32-bit ints
574     kInt4_GrVertexAttribType,   // vector of 4 32-bit ints
575 
576 
577     kByte_GrVertexAttribType,  // signed byte
578     kByte2_GrVertexAttribType, // vector of 2 8-bit signed bytes
579     kByte4_GrVertexAttribType, // vector of 4 8-bit signed bytes
580     kUByte_GrVertexAttribType,  // unsigned byte
581     kUByte2_GrVertexAttribType, // vector of 2 8-bit unsigned bytes
582     kUByte4_GrVertexAttribType, // vector of 4 8-bit unsigned bytes
583 
584     kUByte_norm_GrVertexAttribType,  // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
585     kUByte4_norm_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f,
586                                      // 255 -> 1.0f.
587 
588     kShort2_GrVertexAttribType,       // vector of 2 16-bit shorts.
589     kShort4_GrVertexAttribType,       // vector of 4 16-bit shorts.
590 
591     kUShort2_GrVertexAttribType,      // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
592     kUShort2_norm_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
593 
594     kInt_GrVertexAttribType,
595     kUint_GrVertexAttribType,
596 
597     kUShort_norm_GrVertexAttribType,
598 
599     kUShort4_norm_GrVertexAttribType, // vector of 4 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
600 
601     kLast_GrVertexAttribType = kUShort4_norm_GrVertexAttribType
602 };
603 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
604 
605 //////////////////////////////////////////////////////////////////////////////
606 
607 static const int kGrClipEdgeTypeCnt = (int) GrClipEdgeType::kLast + 1;
608 
GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType)609 static constexpr bool GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType) {
610     return (GrClipEdgeType::kFillAA == edgeType || GrClipEdgeType::kFillBW == edgeType);
611 }
612 
GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType)613 static constexpr bool GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType) {
614     return (GrClipEdgeType::kInverseFillAA == edgeType ||
615             GrClipEdgeType::kInverseFillBW == edgeType);
616 }
617 
GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType)618 static constexpr bool GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType) {
619     return (GrClipEdgeType::kFillBW != edgeType &&
620             GrClipEdgeType::kInverseFillBW != edgeType);
621 }
622 
GrInvertProcessorEdgeType(const GrClipEdgeType edgeType)623 static inline GrClipEdgeType GrInvertProcessorEdgeType(const GrClipEdgeType edgeType) {
624     switch (edgeType) {
625         case GrClipEdgeType::kFillBW:
626             return GrClipEdgeType::kInverseFillBW;
627         case GrClipEdgeType::kFillAA:
628             return GrClipEdgeType::kInverseFillAA;
629         case GrClipEdgeType::kInverseFillBW:
630             return GrClipEdgeType::kFillBW;
631         case GrClipEdgeType::kInverseFillAA:
632             return GrClipEdgeType::kFillAA;
633         case GrClipEdgeType::kHairlineAA:
634             SK_ABORT("Hairline fill isn't invertible.");
635     }
636     return GrClipEdgeType::kFillAA;  // suppress warning.
637 }
638 
639 /**
640  * Indicates the type of pending IO operations that can be recorded for gpu resources.
641  */
642 enum GrIOType {
643     kRead_GrIOType,
644     kWrite_GrIOType,
645     kRW_GrIOType
646 };
647 
648 /**
649  * Indicates the type of data that a GPU buffer will be used for.
650  */
651 enum class GrGpuBufferType {
652     kVertex,
653     kIndex,
654     kXferCpuToGpu,
655     kXferGpuToCpu,
656 };
657 static const int kGrGpuBufferTypeCount = static_cast<int>(GrGpuBufferType::kXferGpuToCpu) + 1;
658 
659 /**
660  * Provides a performance hint regarding the frequency at which a data store will be accessed.
661  */
662 enum GrAccessPattern {
663     /** Data store will be respecified repeatedly and used many times. */
664     kDynamic_GrAccessPattern,
665     /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
666     kStatic_GrAccessPattern,
667     /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
668     kStream_GrAccessPattern,
669 
670     kLast_GrAccessPattern = kStream_GrAccessPattern
671 };
672 
673 // Flags shared between the GrSurface & GrSurfaceProxy class hierarchies
674 enum class GrInternalSurfaceFlags {
675     kNone                           = 0,
676 
677     // Texture-level
678 
679     // Means the pixels in the texture are read-only. Cannot also be a GrRenderTarget[Proxy].
680     kReadOnly                       = 1 << 0,
681 
682     // RT-level
683 
684     // This flag is for use with GL only. It tells us that the internal render target wraps FBO 0.
685     kGLRTFBOIDIs0                   = 1 << 1,
686 
687     // This means the render target is multisampled, and internally holds a non-msaa texture for
688     // resolving into. The render target resolves itself by blitting into this internal texture.
689     // (asTexture() might or might not return the internal texture, but if it does, we always
690     // resolve the render target before accessing this texture's data.)
691     kRequiresManualMSAAResolve      = 1 << 2,
692 
693     // This means the pixels in the render target are write-only. This is used for Dawn and Metal
694     // swap chain targets which can be rendered to, but not read or copied.
695     kFramebufferOnly                = 1 << 3,
696 };
697 
698 GR_MAKE_BITFIELD_CLASS_OPS(GrInternalSurfaceFlags)
699 
700 // 'GR_MAKE_BITFIELD_CLASS_OPS' defines the & operator on GrInternalSurfaceFlags to return bool.
701 // We want to find the bitwise & with these masks, so we declare them as ints.
702 constexpr static int kGrInternalTextureFlagsMask = static_cast<int>(
703         GrInternalSurfaceFlags::kReadOnly);
704 
705 constexpr static int kGrInternalRenderTargetFlagsMask = static_cast<int>(
706         GrInternalSurfaceFlags::kGLRTFBOIDIs0 | GrInternalSurfaceFlags::kRequiresManualMSAAResolve);
707 
708 constexpr static int kGrInternalTextureRenderTargetFlagsMask =
709         kGrInternalTextureFlagsMask | kGrInternalRenderTargetFlagsMask;
710 
711 #ifdef SK_DEBUG
712 // Takes a pointer to a GrCaps, and will suppress prints if required
713 #define GrCapsDebugf(caps, ...)  if (!(caps)->suppressPrints()) SkDebugf(__VA_ARGS__)
714 #else
715 #define GrCapsDebugf(caps, ...) do {} while (0)
716 #endif
717 
718 /**
719  * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
720  */
721 enum class GrBackendObjectOwnership : bool {
722     /** Holder does not destroy the backend object. */
723     kBorrowed = false,
724     /** Holder destroys the backend object. */
725     kOwned = true
726 };
727 
728 /*
729  * Object for CPU-GPU synchronization
730  */
731 typedef uint64_t GrFence;
732 
733 /**
734  * Used to include or exclude specific GPU path renderers for testing purposes.
735  */
736 enum class GpuPathRenderers {
737     kNone              =   0,  // Always use software masks and/or GrDefaultPathRenderer.
738     kDashLine          =   1 << 0,
739     kTessellation      =   1 << 1,
740     kStencilAndCover   =   1 << 2,
741     kCoverageCounting  =   1 << 3,
742     kAAHairline        =   1 << 4,
743     kAAConvex          =   1 << 5,
744     kAALinearizing     =   1 << 6,
745     kSmall             =   1 << 7,
746     kTriangulating     =   1 << 8,
747     kDefault           = ((1 << 9) - 1) & ~kTessellation  // All but kTessellation.
748 };
749 
750 /**
751  * Used to describe the current state of Mips on a GrTexture
752  */
753 enum class  GrMipMapsStatus {
754     kNotAllocated, // Mips have not been allocated
755     kDirty,        // Mips are allocated but the full mip tree does not have valid data
756     kValid,        // All levels fully allocated and have valid data in them
757 };
758 
759 GR_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers)
760 
761 /**
762  * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels,
763  * their type, and width. This exists so that the GPU backend can have private types that have no
764  * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend.
765  * It does not refer to a texture format and the mapping to texture formats may be many-to-many.
766  * It does not specify the sRGB encoding of the stored values. The components are listed in order of
767  * where they appear in memory. In other words the first component listed is in the low bits and
768  * the last component in the high bits.
769  */
770 enum class GrColorType {
771     kUnknown,
772     kAlpha_8,
773     kBGR_565,
774     kABGR_4444,  // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed.
775     kRGBA_8888,
776     kRGBA_8888_SRGB,
777     kRGB_888x,
778     kRG_88,
779     kBGRA_8888,
780     kRGBA_1010102,
781     kBGRA_1010102,
782     kGray_8,
783     kAlpha_F16,
784     kRGBA_F16,
785     kRGBA_F16_Clamped,
786     kRGBA_F32,
787 
788     kAlpha_16,
789     kRG_1616,
790     kRG_F16,
791     kRGBA_16161616,
792 
793     // Unusual types that come up after reading back in cases where we are reassigning the meaning
794     // of a texture format's channels to use for a particular color format but have to read back the
795     // data to a full RGBA quadruple. (e.g. using a R8 texture format as A8 color type but the API
796     // only supports reading to RGBA8.) None of these have SkColorType equivalents.
797     kAlpha_8xxx,
798     kAlpha_F32xxx,
799     kGray_8xxx,
800 
801     // Types used to initialize backend textures.
802     kRGB_888,
803     kR_8,
804     kR_16,
805     kR_F16,
806     kGray_F16,
807 
808     kLast = kGray_F16
809 };
810 
811 static const int kGrColorTypeCnt = static_cast<int>(GrColorType::kLast) + 1;
812 
GrColorTypeToSkColorType(GrColorType ct)813 static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct) {
814     switch (ct) {
815         case GrColorType::kUnknown:          return kUnknown_SkColorType;
816         case GrColorType::kAlpha_8:          return kAlpha_8_SkColorType;
817         case GrColorType::kBGR_565:          return kRGB_565_SkColorType;
818         case GrColorType::kABGR_4444:        return kARGB_4444_SkColorType;
819         case GrColorType::kRGBA_8888:        return kRGBA_8888_SkColorType;
820         // Once we add kRGBA_8888_SRGB_SkColorType we should return that here.
821         case GrColorType::kRGBA_8888_SRGB:   return kRGBA_8888_SkColorType;
822         case GrColorType::kRGB_888x:         return kRGB_888x_SkColorType;
823         case GrColorType::kRG_88:            return kR8G8_unorm_SkColorType;
824         case GrColorType::kBGRA_8888:        return kBGRA_8888_SkColorType;
825         case GrColorType::kRGBA_1010102:     return kRGBA_1010102_SkColorType;
826         case GrColorType::kBGRA_1010102:     return kBGRA_1010102_SkColorType;
827         case GrColorType::kGray_8:           return kGray_8_SkColorType;
828         case GrColorType::kAlpha_F16:        return kA16_float_SkColorType;
829         case GrColorType::kRGBA_F16:         return kRGBA_F16_SkColorType;
830         case GrColorType::kRGBA_F16_Clamped: return kRGBA_F16Norm_SkColorType;
831         case GrColorType::kRGBA_F32:         return kRGBA_F32_SkColorType;
832         case GrColorType::kAlpha_8xxx:       return kUnknown_SkColorType;
833         case GrColorType::kAlpha_F32xxx:     return kUnknown_SkColorType;
834         case GrColorType::kGray_8xxx:        return kUnknown_SkColorType;
835         case GrColorType::kAlpha_16:         return kA16_unorm_SkColorType;
836         case GrColorType::kRG_1616:          return kR16G16_unorm_SkColorType;
837         case GrColorType::kRGBA_16161616:    return kR16G16B16A16_unorm_SkColorType;
838         case GrColorType::kRG_F16:           return kR16G16_float_SkColorType;
839         case GrColorType::kRGB_888:          return kUnknown_SkColorType;
840         case GrColorType::kR_8:              return kUnknown_SkColorType;
841         case GrColorType::kR_16:             return kUnknown_SkColorType;
842         case GrColorType::kR_F16:            return kUnknown_SkColorType;
843         case GrColorType::kGray_F16:         return kUnknown_SkColorType;
844     }
845     SkUNREACHABLE;
846 }
847 
SkColorTypeToGrColorType(SkColorType ct)848 static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct) {
849     switch (ct) {
850         case kUnknown_SkColorType:            return GrColorType::kUnknown;
851         case kAlpha_8_SkColorType:            return GrColorType::kAlpha_8;
852         case kRGB_565_SkColorType:            return GrColorType::kBGR_565;
853         case kARGB_4444_SkColorType:          return GrColorType::kABGR_4444;
854         case kRGBA_8888_SkColorType:          return GrColorType::kRGBA_8888;
855         case kRGB_888x_SkColorType:           return GrColorType::kRGB_888x;
856         case kBGRA_8888_SkColorType:          return GrColorType::kBGRA_8888;
857         case kGray_8_SkColorType:             return GrColorType::kGray_8;
858         case kRGBA_F16Norm_SkColorType:       return GrColorType::kRGBA_F16_Clamped;
859         case kRGBA_F16_SkColorType:           return GrColorType::kRGBA_F16;
860         case kRGBA_1010102_SkColorType:       return GrColorType::kRGBA_1010102;
861         case kRGB_101010x_SkColorType:        return GrColorType::kUnknown;
862         case kBGRA_1010102_SkColorType:       return GrColorType::kBGRA_1010102;
863         case kBGR_101010x_SkColorType:        return GrColorType::kUnknown;
864         case kRGBA_F32_SkColorType:           return GrColorType::kRGBA_F32;
865         case kR8G8_unorm_SkColorType:         return GrColorType::kRG_88;
866         case kA16_unorm_SkColorType:          return GrColorType::kAlpha_16;
867         case kR16G16_unorm_SkColorType:       return GrColorType::kRG_1616;
868         case kA16_float_SkColorType:          return GrColorType::kAlpha_F16;
869         case kR16G16_float_SkColorType:       return GrColorType::kRG_F16;
870         case kR16G16B16A16_unorm_SkColorType: return GrColorType::kRGBA_16161616;
871     }
872     SkUNREACHABLE;
873 }
874 
875 // This is a temporary means of mapping an SkColorType and format to a
876 // GrColorType::kRGBA_8888_SRGB. Once we have an SRGB SkColorType this can go away.
877 GrColorType SkColorTypeAndFormatToGrColorType(const GrCaps* caps,
878                                               SkColorType skCT,
879                                               const GrBackendFormat& format);
880 
GrColorTypeChannelFlags(GrColorType ct)881 static constexpr uint32_t GrColorTypeChannelFlags(GrColorType ct) {
882     switch (ct) {
883         case GrColorType::kUnknown:          return 0;
884         case GrColorType::kAlpha_8:          return kAlpha_SkColorChannelFlag;
885         case GrColorType::kBGR_565:          return kRGB_SkColorChannelFlags;
886         case GrColorType::kABGR_4444:        return kRGBA_SkColorChannelFlags;
887         case GrColorType::kRGBA_8888:        return kRGBA_SkColorChannelFlags;
888         case GrColorType::kRGBA_8888_SRGB:   return kRGBA_SkColorChannelFlags;
889         case GrColorType::kRGB_888x:         return kRGB_SkColorChannelFlags;
890         case GrColorType::kRG_88:            return kRG_SkColorChannelFlags;
891         case GrColorType::kBGRA_8888:        return kRGBA_SkColorChannelFlags;
892         case GrColorType::kRGBA_1010102:     return kRGBA_SkColorChannelFlags;
893         case GrColorType::kBGRA_1010102:     return kRGBA_SkColorChannelFlags;
894         case GrColorType::kGray_8:           return kGray_SkColorChannelFlag;
895         case GrColorType::kAlpha_F16:        return kAlpha_SkColorChannelFlag;
896         case GrColorType::kRGBA_F16:         return kRGBA_SkColorChannelFlags;
897         case GrColorType::kRGBA_F16_Clamped: return kRGBA_SkColorChannelFlags;
898         case GrColorType::kRGBA_F32:         return kRGBA_SkColorChannelFlags;
899         case GrColorType::kAlpha_8xxx:       return kAlpha_SkColorChannelFlag;
900         case GrColorType::kAlpha_F32xxx:     return kAlpha_SkColorChannelFlag;
901         case GrColorType::kGray_8xxx:        return kGray_SkColorChannelFlag;
902         case GrColorType::kAlpha_16:         return kAlpha_SkColorChannelFlag;
903         case GrColorType::kRG_1616:          return kRG_SkColorChannelFlags;
904         case GrColorType::kRGBA_16161616:    return kRGBA_SkColorChannelFlags;
905         case GrColorType::kRG_F16:           return kRG_SkColorChannelFlags;
906         case GrColorType::kRGB_888:          return kRGB_SkColorChannelFlags;
907         case GrColorType::kR_8:              return kRed_SkColorChannelFlag;
908         case GrColorType::kR_16:             return kRed_SkColorChannelFlag;
909         case GrColorType::kR_F16:            return kRed_SkColorChannelFlag;
910         case GrColorType::kGray_F16:         return kGray_SkColorChannelFlag;
911     }
912     SkUNREACHABLE;
913 }
914 
915 /**
916  * Describes the encoding of channel data in a GrColorType.
917  */
918 enum class GrColorTypeEncoding {
919     kUnorm,
920     kSRGBUnorm,
921     // kSnorm,
922     kFloat,
923     // kSint
924     // kUint
925 };
926 
927 /**
928  * Describes a GrColorType by how many bits are used for each color component and how they are
929  * encoded. Currently all the non-zero channels share a single GrColorTypeEncoding. This could be
930  * expanded to store separate encodings and to indicate which bits belong to which components.
931  */
932 struct GrColorTypeDesc {
933 public:
MakeRGBAGrColorTypeDesc934     static constexpr GrColorTypeDesc MakeRGBA(int rgba, GrColorTypeEncoding e) {
935         return {rgba, rgba, rgba, rgba, 0, e};
936     }
937 
MakeRGBAGrColorTypeDesc938     static constexpr GrColorTypeDesc MakeRGBA(int rgb, int a, GrColorTypeEncoding e) {
939         return {rgb, rgb, rgb, a, 0, e};
940     }
941 
MakeRGBGrColorTypeDesc942     static constexpr GrColorTypeDesc MakeRGB(int rgb, GrColorTypeEncoding e) {
943         return {rgb, rgb, rgb, 0, 0, e};
944     }
945 
MakeRGBGrColorTypeDesc946     static constexpr GrColorTypeDesc MakeRGB(int r, int g, int b, GrColorTypeEncoding e) {
947         return {r, g, b, 0, 0, e};
948     }
949 
MakeAlphaGrColorTypeDesc950     static constexpr GrColorTypeDesc MakeAlpha(int a, GrColorTypeEncoding e) {
951         return {0, 0, 0, a, 0, e};
952     }
953 
MakeRGrColorTypeDesc954     static constexpr GrColorTypeDesc MakeR(int r, GrColorTypeEncoding e) {
955         return {r, 0, 0, 0, 0, e};
956     }
957 
MakeRGGrColorTypeDesc958     static constexpr GrColorTypeDesc MakeRG(int rg, GrColorTypeEncoding e) {
959         return {rg, rg, 0, 0, 0, e};
960     }
961 
MakeGrayGrColorTypeDesc962     static constexpr GrColorTypeDesc MakeGray(int grayBits, GrColorTypeEncoding e) {
963         return {0, 0, 0, 0, grayBits, e};
964     }
965 
MakeInvalidGrColorTypeDesc966     static constexpr GrColorTypeDesc MakeInvalid() { return {}; }
967 
rGrColorTypeDesc968     constexpr int r() const { return fRBits; }
gGrColorTypeDesc969     constexpr int g() const { return fGBits; }
bGrColorTypeDesc970     constexpr int b() const { return fBBits; }
aGrColorTypeDesc971     constexpr int a() const { return fABits; }
972     constexpr int operator[](int c) const {
973         switch (c) {
974             case 0: return this->r();
975             case 1: return this->g();
976             case 2: return this->b();
977             case 3: return this->a();
978         }
979         SkUNREACHABLE;
980     }
981 
grayGrColorTypeDesc982     constexpr int gray() const { return fGrayBits; }
983 
encodingGrColorTypeDesc984     constexpr GrColorTypeEncoding encoding() const { return fEncoding; }
985 
986 private:
987     int fRBits = 0;
988     int fGBits = 0;
989     int fBBits = 0;
990     int fABits = 0;
991     int fGrayBits = 0;
992     GrColorTypeEncoding fEncoding = GrColorTypeEncoding::kUnorm;
993 
994     constexpr GrColorTypeDesc() = default;
995 
GrColorTypeDescGrColorTypeDesc996     constexpr GrColorTypeDesc(int r, int g, int b, int a, int gray, GrColorTypeEncoding encoding)
997             : fRBits(r), fGBits(g), fBBits(b), fABits(a), fGrayBits(gray), fEncoding(encoding) {
998         SkASSERT(r >= 0 && g >= 0 && b >= 0 && a >= 0 && gray >= 0);
999         SkASSERT(!gray || (!r && !g && !b));
1000         SkASSERT(r || g || b || a || gray);
1001     }
1002 };
1003 
GrGetColorTypeDesc(GrColorType ct)1004 static constexpr GrColorTypeDesc GrGetColorTypeDesc(GrColorType ct) {
1005     switch (ct) {
1006         case GrColorType::kUnknown:
1007             return GrColorTypeDesc::MakeInvalid();
1008         case GrColorType::kAlpha_8:
1009             return GrColorTypeDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
1010         case GrColorType::kBGR_565:
1011             return GrColorTypeDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
1012         case GrColorType::kABGR_4444:
1013             return GrColorTypeDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
1014         case GrColorType::kRGBA_8888:
1015             return GrColorTypeDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
1016         case GrColorType::kRGBA_8888_SRGB:
1017             return GrColorTypeDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm);
1018         case GrColorType::kRGB_888x:
1019             return GrColorTypeDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
1020         case GrColorType::kRG_88:
1021             return GrColorTypeDesc::MakeRG(8, GrColorTypeEncoding::kUnorm);
1022         case GrColorType::kBGRA_8888:
1023             return GrColorTypeDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
1024         case GrColorType::kRGBA_1010102:
1025             return GrColorTypeDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
1026         case GrColorType::kBGRA_1010102:
1027             return GrColorTypeDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
1028         case GrColorType::kGray_8:
1029             return GrColorTypeDesc::MakeGray(8, GrColorTypeEncoding::kUnorm);
1030         case GrColorType::kAlpha_F16:
1031             return GrColorTypeDesc::MakeAlpha(16, GrColorTypeEncoding::kFloat);
1032         case GrColorType::kRGBA_F16:
1033             return GrColorTypeDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
1034         case GrColorType::kRGBA_F16_Clamped:
1035             return GrColorTypeDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
1036         case GrColorType::kRGBA_F32:
1037             return GrColorTypeDesc::MakeRGBA(32, GrColorTypeEncoding::kFloat);
1038         case GrColorType::kAlpha_8xxx:
1039             return GrColorTypeDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
1040         case GrColorType::kAlpha_F32xxx:
1041             return GrColorTypeDesc::MakeAlpha(32, GrColorTypeEncoding::kFloat);
1042         case GrColorType::kGray_8xxx:
1043             return GrColorTypeDesc::MakeGray(8, GrColorTypeEncoding::kUnorm);
1044         case GrColorType::kAlpha_16:
1045             return GrColorTypeDesc::MakeAlpha(16, GrColorTypeEncoding::kUnorm);
1046         case GrColorType::kRG_1616:
1047             return GrColorTypeDesc::MakeRG(16, GrColorTypeEncoding::kUnorm);
1048         case GrColorType::kRGBA_16161616:
1049             return GrColorTypeDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm);
1050         case GrColorType::kRG_F16:
1051             return GrColorTypeDesc::MakeRG(16, GrColorTypeEncoding::kFloat);
1052         case GrColorType::kRGB_888:
1053             return GrColorTypeDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
1054         case GrColorType::kR_8:
1055             return GrColorTypeDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
1056         case GrColorType::kR_16:
1057             return GrColorTypeDesc::MakeR(16, GrColorTypeEncoding::kUnorm);
1058         case GrColorType::kR_F16:
1059             return GrColorTypeDesc::MakeR(16, GrColorTypeEncoding::kFloat);
1060         case GrColorType::kGray_F16:
1061             return GrColorTypeDesc::MakeGray(16, GrColorTypeEncoding::kFloat);
1062     }
1063     SkUNREACHABLE;
1064 }
1065 
GrColorTypeClampType(GrColorType colorType)1066 static constexpr GrClampType GrColorTypeClampType(GrColorType colorType) {
1067     if (GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kUnorm ||
1068         GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kSRGBUnorm) {
1069         return GrClampType::kAuto;
1070     }
1071     return GrColorType::kRGBA_F16_Clamped == colorType ? GrClampType::kManual : GrClampType::kNone;
1072 }
1073 
1074 // Consider a color type "wider" than n if it has more than n bits for any its representable
1075 // channels.
GrColorTypeIsWiderThan(GrColorType colorType,int n)1076 static constexpr bool GrColorTypeIsWiderThan(GrColorType colorType, int n) {
1077     SkASSERT(n > 0);
1078     auto desc = GrGetColorTypeDesc(colorType);
1079     return (desc.r() && desc.r() > n )||
1080            (desc.g() && desc.g() > n) ||
1081            (desc.b() && desc.b() > n) ||
1082            (desc.a() && desc.a() > n) ||
1083            (desc.gray() && desc.gray() > n);
1084 }
1085 
GrColorTypeIsAlphaOnly(GrColorType ct)1086 static constexpr bool GrColorTypeIsAlphaOnly(GrColorType ct) {
1087     return GrColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag;
1088 }
1089 
GrColorTypeHasAlpha(GrColorType ct)1090 static constexpr bool GrColorTypeHasAlpha(GrColorType ct) {
1091     return GrColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag;
1092 }
1093 
GrColorTypeBytesPerPixel(GrColorType ct)1094 static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct) {
1095     switch (ct) {
1096         case GrColorType::kUnknown:          return 0;
1097         case GrColorType::kAlpha_8:          return 1;
1098         case GrColorType::kBGR_565:          return 2;
1099         case GrColorType::kABGR_4444:        return 2;
1100         case GrColorType::kRGBA_8888:        return 4;
1101         case GrColorType::kRGBA_8888_SRGB:   return 4;
1102         case GrColorType::kRGB_888x:         return 4;
1103         case GrColorType::kRG_88:            return 2;
1104         case GrColorType::kBGRA_8888:        return 4;
1105         case GrColorType::kRGBA_1010102:     return 4;
1106         case GrColorType::kBGRA_1010102:     return 4;
1107         case GrColorType::kGray_8:           return 1;
1108         case GrColorType::kAlpha_F16:        return 2;
1109         case GrColorType::kRGBA_F16:         return 8;
1110         case GrColorType::kRGBA_F16_Clamped: return 8;
1111         case GrColorType::kRGBA_F32:         return 16;
1112         case GrColorType::kAlpha_8xxx:       return 4;
1113         case GrColorType::kAlpha_F32xxx:     return 16;
1114         case GrColorType::kGray_8xxx:        return 4;
1115         case GrColorType::kAlpha_16:         return 2;
1116         case GrColorType::kRG_1616:          return 4;
1117         case GrColorType::kRGBA_16161616:    return 8;
1118         case GrColorType::kRG_F16:           return 4;
1119         case GrColorType::kRGB_888:          return 3;
1120         case GrColorType::kR_8:              return 1;
1121         case GrColorType::kR_16:             return 2;
1122         case GrColorType::kR_F16:            return 2;
1123         case GrColorType::kGray_F16:         return 2;
1124     }
1125     SkUNREACHABLE;
1126 }
1127 
1128 // In general we try to not mix CompressionType and ColorType, but currently SkImage still requires
1129 // an SkColorType even for CompressedTypes so we need some conversion.
GrCompressionTypeToSkColorType(SkImage::CompressionType compression)1130 static constexpr SkColorType GrCompressionTypeToSkColorType(SkImage::CompressionType compression) {
1131     switch (compression) {
1132         case SkImage::CompressionType::kNone:            return kUnknown_SkColorType;
1133         case SkImage::CompressionType::kETC2_RGB8_UNORM: return kRGB_888x_SkColorType;
1134         case SkImage::CompressionType::kBC1_RGB8_UNORM:  return kRGB_888x_SkColorType;
1135         case SkImage::CompressionType::kBC1_RGBA8_UNORM: return kRGBA_8888_SkColorType;
1136     }
1137 
1138     SkUNREACHABLE;
1139 }
1140 
GrMaskFormatToColorType(GrMaskFormat format)1141 static constexpr GrColorType GrMaskFormatToColorType(GrMaskFormat format) {
1142     switch (format) {
1143         case kA8_GrMaskFormat:
1144             return GrColorType::kAlpha_8;
1145         case kA565_GrMaskFormat:
1146             return GrColorType::kBGR_565;
1147         case kARGB_GrMaskFormat:
1148             return GrColorType::kRGBA_8888;
1149     }
1150     SkUNREACHABLE;
1151 }
1152 
1153 /**
1154  * Ref-counted object that calls a callback from its destructor.
1155  */
1156 class GrRefCntedCallback : public SkRefCnt {
1157 public:
1158     using Context = void*;
1159     using Callback = void (*)(Context);
1160 
GrRefCntedCallback(Callback proc,Context ctx)1161     GrRefCntedCallback(Callback proc, Context ctx) : fReleaseProc(proc), fReleaseCtx(ctx) {
1162         SkASSERT(proc);
1163     }
~GrRefCntedCallback()1164     ~GrRefCntedCallback() override { fReleaseProc ? fReleaseProc(fReleaseCtx) : void(); }
1165 
context()1166     Context context() const { return fReleaseCtx; }
1167 
1168 private:
1169     Callback fReleaseProc;
1170     Context fReleaseCtx;
1171 };
1172 
1173 #if GR_TEST_UTILS || defined(SK_ENABLE_DUMP_GPU)
GrBackendApiToStr(GrBackendApi api)1174 static constexpr const char* GrBackendApiToStr(GrBackendApi api) {
1175     switch (api) {
1176         case GrBackendApi::kOpenGL:   return "OpenGL";
1177         case GrBackendApi::kVulkan:   return "Vulkan";
1178         case GrBackendApi::kMetal:    return "Metal";
1179         case GrBackendApi::kDirect3D: return "Direct3D";
1180         case GrBackendApi::kDawn:     return "Dawn";
1181         case GrBackendApi::kMock:     return "Mock";
1182     }
1183     SkUNREACHABLE;
1184 }
1185 
GrColorTypeToStr(GrColorType ct)1186 static constexpr const char* GrColorTypeToStr(GrColorType ct) {
1187     switch (ct) {
1188         case GrColorType::kUnknown:          return "kUnknown";
1189         case GrColorType::kAlpha_8:          return "kAlpha_8";
1190         case GrColorType::kBGR_565:          return "kRGB_565";
1191         case GrColorType::kABGR_4444:        return "kABGR_4444";
1192         case GrColorType::kRGBA_8888:        return "kRGBA_8888";
1193         case GrColorType::kRGBA_8888_SRGB:   return "kRGBA_8888_SRGB";
1194         case GrColorType::kRGB_888x:         return "kRGB_888x";
1195         case GrColorType::kRG_88:            return "kRG_88";
1196         case GrColorType::kBGRA_8888:        return "kBGRA_8888";
1197         case GrColorType::kRGBA_1010102:     return "kRGBA_1010102";
1198         case GrColorType::kBGRA_1010102:     return "kBGRA_1010102";
1199         case GrColorType::kGray_8:           return "kGray_8";
1200         case GrColorType::kAlpha_F16:        return "kAlpha_F16";
1201         case GrColorType::kRGBA_F16:         return "kRGBA_F16";
1202         case GrColorType::kRGBA_F16_Clamped: return "kRGBA_F16_Clamped";
1203         case GrColorType::kRGBA_F32:         return "kRGBA_F32";
1204         case GrColorType::kAlpha_8xxx:       return "kAlpha_8xxx";
1205         case GrColorType::kAlpha_F32xxx:     return "kAlpha_F32xxx";
1206         case GrColorType::kGray_8xxx:        return "kGray_8xxx";
1207         case GrColorType::kAlpha_16:         return "kAlpha_16";
1208         case GrColorType::kRG_1616:          return "kRG_1616";
1209         case GrColorType::kRGBA_16161616:    return "kRGBA_16161616";
1210         case GrColorType::kRG_F16:           return "kRG_F16";
1211         case GrColorType::kRGB_888:          return "kRGB_888";
1212         case GrColorType::kR_8:              return "kR_8";
1213         case GrColorType::kR_16:             return "kR_16";
1214         case GrColorType::kR_F16:            return "kR_F16";
1215         case GrColorType::kGray_F16:         return "kGray_F16";
1216     }
1217     SkUNREACHABLE;
1218 }
1219 
GrCompressionTypeToStr(SkImage::CompressionType compression)1220 static constexpr const char* GrCompressionTypeToStr(SkImage::CompressionType compression) {
1221     switch (compression) {
1222         case SkImage::CompressionType::kNone:            return "kNone";
1223         case SkImage::CompressionType::kETC2_RGB8_UNORM: return "kETC2_RGB8_UNORM";
1224         case SkImage::CompressionType::kBC1_RGB8_UNORM:  return "kBC1_RGB8_UNORM";
1225         case SkImage::CompressionType::kBC1_RGBA8_UNORM: return "kBC1_RGBA8_UNORM";
1226     }
1227     SkUNREACHABLE;
1228 }
1229 #endif
1230 
1231 #endif
1232