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