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