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