xref: /aosp_15_r20/external/skia/include/private/gpu/ganesh/GrTypesPriv.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkPathTypes.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/private/base/SkAssert.h"
18 #include "include/private/base/SkDebug.h"
19 #include "include/private/base/SkMacros.h"
20 #include "include/private/base/SkTypeTraits.h"
21 
22 #include <cstddef>
23 #include <cstdint>
24 #include <functional>
25 #include <type_traits>
26 
27 class GrSurfaceProxy;
28 
29 namespace skgpu {
30 enum class Mipmapped : bool;
31 }
32 
33 /**
34  *  divide, rounding up
35  */
36 
GrSizeDivRoundUp(size_t x,size_t y)37 static inline constexpr size_t GrSizeDivRoundUp(size_t x, size_t y) { return (x + (y - 1)) / y; }
38 
39 /**
40  * Geometric primitives used for drawing.
41  */
42 enum class GrPrimitiveType : uint8_t {
43     kTriangles,
44     kTriangleStrip,
45     kPoints,
46     kLines,          // 1 pix wide only
47     kLineStrip,      // 1 pix wide only
48 };
49 static constexpr int kNumGrPrimitiveTypes = (int)GrPrimitiveType::kLineStrip + 1;
50 
GrIsPrimTypeLines(GrPrimitiveType type)51 static constexpr bool GrIsPrimTypeLines(GrPrimitiveType type) {
52     return GrPrimitiveType::kLines == type || GrPrimitiveType::kLineStrip == type;
53 }
54 
55 enum class GrPrimitiveRestart : bool {
56     kNo = false,
57     kYes = true
58 };
59 
60 /**
61  * Should a created surface be texturable?
62  */
63 enum class GrTexturable : bool {
64     kNo = false,
65     kYes = true
66 };
67 
68 // A DDL recorder has its own proxy provider and proxy cache. This enum indicates if
69 // a given proxy provider is one of these special ones.
70 enum class GrDDLProvider : bool {
71     kNo = false,
72     kYes = true
73 };
74 
75 /** Ownership rules for external GPU resources imported into Skia. */
76 enum GrWrapOwnership {
77     /** Skia will assume the client will keep the resource alive and Skia will not free it. */
78     kBorrow_GrWrapOwnership,
79 
80     /** Skia will assume ownership of the resource and free it. */
81     kAdopt_GrWrapOwnership,
82 };
83 
84 enum class GrWrapCacheable : bool {
85     /**
86      * The wrapped resource will be removed from the cache as soon as it becomes purgeable. It may
87      * still be assigned and found by a unique key, but the presence of the key will not be used to
88      * keep the resource alive when it has no references.
89      */
90     kNo = false,
91     /**
92      * The wrapped resource is allowed to remain in the GrResourceCache when it has no references
93      * but has a unique key. Such resources should only be given unique keys when it is known that
94      * the key will eventually be removed from the resource or invalidated via the message bus.
95      */
96     kYes = true
97 };
98 
99 enum class GrBudgetedType : uint8_t {
100     /** The resource is budgeted and is subject to purging under budget pressure. */
101     kBudgeted,
102     /**
103      * The resource is unbudgeted and is purged as soon as it has no refs regardless of whether
104      * it has a unique or scratch key.
105      */
106     kUnbudgetedUncacheable,
107     /**
108      * The resource is unbudgeted and is allowed to remain in the cache with no refs if it
109      * has a unique key. Scratch keys are ignored.
110      */
111     kUnbudgetedCacheable,
112 };
113 
114 enum class GrScissorTest : bool {
115     kDisabled = false,
116     kEnabled = true
117 };
118 
119 /*
120  * Used to say whether texture is backed by memory.
121  */
122 enum class GrMemoryless : bool {
123     /**
124      * The texture will be allocated normally and will affect memory budgets.
125      */
126     kNo = false,
127     /**
128      * The texture will be not use GPU memory and will not affect memory budgets.
129      */
130     kYes = true
131 };
132 
133 struct GrMipLevel {
134     const void* fPixels = nullptr;
135     size_t fRowBytes = 0;
136     // This may be used to keep fPixels from being freed while a GrMipLevel exists.
137     sk_sp<SkData> fOptionalStorage;
138 
139     static_assert(::sk_is_trivially_relocatable<decltype(fPixels)>::value);
140     static_assert(::sk_is_trivially_relocatable<decltype(fOptionalStorage)>::value);
141 
142     using sk_is_trivially_relocatable = std::true_type;
143 };
144 
145 enum class GrSemaphoreWrapType {
146     kWillSignal,
147     kWillWait,
148 };
149 
150 /**
151  * This enum is used to specify the load operation to be used when an OpsTask/GrOpsRenderPass
152  * begins execution.
153  */
154 enum class GrLoadOp {
155     kLoad,
156     kClear,
157     kDiscard,
158 };
159 
160 /**
161  * This enum is used to specify the store operation to be used when an OpsTask/GrOpsRenderPass
162  * ends execution.
163  */
164 enum class GrStoreOp {
165     kStore,
166     kDiscard,
167 };
168 
169 /**
170  * Used to control antialiasing in draw calls.
171  */
172 enum class GrAA : bool {
173     kNo = false,
174     kYes = true
175 };
176 
177 enum class GrFillRule : bool {
178     kNonzero,
179     kEvenOdd
180 };
181 
GrFillRuleForPathFillType(SkPathFillType fillType)182 inline GrFillRule GrFillRuleForPathFillType(SkPathFillType fillType) {
183     switch (fillType) {
184         case SkPathFillType::kWinding:
185         case SkPathFillType::kInverseWinding:
186             return GrFillRule::kNonzero;
187         case SkPathFillType::kEvenOdd:
188         case SkPathFillType::kInverseEvenOdd:
189             return GrFillRule::kEvenOdd;
190     }
191     SkUNREACHABLE;
192 }
193 
GrFillRuleForSkPath(const SkPath & path)194 inline GrFillRule GrFillRuleForSkPath(const SkPath& path) {
195     return GrFillRuleForPathFillType(path.getFillType());
196 }
197 
198 /** This enum indicates the type of antialiasing to be performed. */
199 enum class GrAAType : unsigned {
200     /** No antialiasing */
201     kNone,
202     /** Use fragment shader code to blend with a fractional pixel coverage. */
203     kCoverage,
204     /** Use normal MSAA. */
205     kMSAA,
206 
207     kLast = kMSAA
208 };
209 static const int kGrAATypeCount = static_cast<int>(GrAAType::kLast) + 1;
210 
GrAATypeIsHW(GrAAType type)211 static constexpr bool GrAATypeIsHW(GrAAType type) {
212     switch (type) {
213         case GrAAType::kNone:
214             return false;
215         case GrAAType::kCoverage:
216             return false;
217         case GrAAType::kMSAA:
218             return true;
219     }
220     SkUNREACHABLE;
221 }
222 
223 /**
224  * Some pixel configs are inherently clamped to [0,1], some are allowed to go outside that range,
225  * and some are FP but manually clamped in the XP.
226  */
227 enum class GrClampType {
228     kAuto,    // Normalized, fixed-point configs
229     kManual,  // Clamped FP configs
230     kNone,    // Normal (unclamped) FP configs
231 };
232 
233 /**
234  * A number of rectangle/quadrilateral drawing APIs can control anti-aliasing on a per edge basis.
235  * These masks specify which edges are AA'ed. The intent for this is to support tiling with seamless
236  * boundaries, where the inner edges are non-AA and the outer edges are AA. Regular rectangle draws
237  * simply use kAll or kNone depending on if they want anti-aliasing or not.
238  *
239  * In APIs that support per-edge AA, GrQuadAAFlags is the only AA-control parameter that is
240  * provided (compared to the typical GrAA parameter). kNone is equivalent to GrAA::kNo, and any
241  * other set of edge flags would require GrAA::kYes (with rendering output dependent on how that
242  * maps to GrAAType for a given SurfaceDrawContext).
243  *
244  * These values are identical to SkCanvas::QuadAAFlags.
245  */
246 enum class GrQuadAAFlags {
247     kLeft   = 0b0001,
248     kTop    = 0b0010,
249     kRight  = 0b0100,
250     kBottom = 0b1000,
251 
252     kNone = 0b0000,
253     kAll  = 0b1111,
254 };
255 
SK_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)256 SK_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)
257 
258 static inline GrQuadAAFlags SkToGrQuadAAFlags(unsigned flags) {
259     return static_cast<GrQuadAAFlags>(flags);
260 }
261 
262 /**
263  * The type of texture. Backends other than GL currently only use the 2D value but the type must
264  * still be known at the API-neutral layer as it used to determine whether MIP maps, renderability,
265  * and sampling parameters are legal for proxies that will be instantiated with wrapped textures.
266  */
267 enum class GrTextureType {
268     kNone,
269     k2D,
270     /* Rectangle uses unnormalized texture coordinates. */
271     kRectangle,
272     kExternal
273 };
274 
275 enum GrShaderType {
276     kVertex_GrShaderType,
277     kFragment_GrShaderType,
278 
279     kLast_GrShaderType = kFragment_GrShaderType
280 };
281 static const int kGrShaderTypeCount = kLast_GrShaderType + 1;
282 
283 enum GrShaderFlags {
284     kNone_GrShaderFlags          = 0,
285     kVertex_GrShaderFlag         = 1 << 0,
286     kFragment_GrShaderFlag       = 1 << 1
287 };
SK_MAKE_BITFIELD_OPS(GrShaderFlags)288 SK_MAKE_BITFIELD_OPS(GrShaderFlags)
289 
290 /** Rectangle and external textures only support the clamp wrap mode and do not support
291  *  MIP maps.
292  */
293 static inline bool GrTextureTypeHasRestrictedSampling(GrTextureType type) {
294     switch (type) {
295         case GrTextureType::k2D:
296             return false;
297         case GrTextureType::kRectangle:
298             return true;
299         case GrTextureType::kExternal:
300             return true;
301         default:
302             SK_ABORT("Unexpected texture type");
303     }
304 }
305 
306 //////////////////////////////////////////////////////////////////////////////
307 
308 /**
309  * Types used to describe format of vertices in arrays.
310  */
311 enum GrVertexAttribType {
312     kFloat_GrVertexAttribType = 0,
313     kFloat2_GrVertexAttribType,
314     kFloat3_GrVertexAttribType,
315     kFloat4_GrVertexAttribType,
316     kHalf_GrVertexAttribType,
317     kHalf2_GrVertexAttribType,
318     kHalf4_GrVertexAttribType,
319 
320     kInt2_GrVertexAttribType,   // vector of 2 32-bit ints
321     kInt3_GrVertexAttribType,   // vector of 3 32-bit ints
322     kInt4_GrVertexAttribType,   // vector of 4 32-bit ints
323 
324 
325     kByte_GrVertexAttribType,  // signed byte
326     kByte2_GrVertexAttribType, // vector of 2 8-bit signed bytes
327     kByte4_GrVertexAttribType, // vector of 4 8-bit signed bytes
328     kUByte_GrVertexAttribType,  // unsigned byte
329     kUByte2_GrVertexAttribType, // vector of 2 8-bit unsigned bytes
330     kUByte4_GrVertexAttribType, // vector of 4 8-bit unsigned bytes
331 
332     kUByte_norm_GrVertexAttribType,  // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
333     kUByte4_norm_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f,
334                                      // 255 -> 1.0f.
335 
336     kShort2_GrVertexAttribType,       // vector of 2 16-bit shorts.
337     kShort4_GrVertexAttribType,       // vector of 4 16-bit shorts.
338 
339     kUShort2_GrVertexAttribType,      // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
340     kUShort2_norm_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
341 
342     kInt_GrVertexAttribType,
343     kUInt_GrVertexAttribType,
344 
345     kUShort_norm_GrVertexAttribType,
346 
347     kUShort4_norm_GrVertexAttribType, // vector of 4 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
348 
349     kLast_GrVertexAttribType = kUShort4_norm_GrVertexAttribType
350 };
351 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
352 
353 //////////////////////////////////////////////////////////////////////////////
354 
355 /**
356  * We have coverage effects that clip rendering to the edge of some geometric primitive.
357  * This enum specifies how that clipping is performed. Not all factories that take a
358  * GrClipEdgeType will succeed with all values and it is up to the caller to verify success.
359  */
360 enum class GrClipEdgeType {
361     kFillBW,
362     kFillAA,
363     kInverseFillBW,
364     kInverseFillAA,
365 
366     kLast = kInverseFillAA
367 };
368 static const int kGrClipEdgeTypeCnt = (int) GrClipEdgeType::kLast + 1;
369 
GrClipEdgeTypeIsFill(const GrClipEdgeType edgeType)370 static constexpr bool GrClipEdgeTypeIsFill(const GrClipEdgeType edgeType) {
371     return (GrClipEdgeType::kFillAA == edgeType || GrClipEdgeType::kFillBW == edgeType);
372 }
373 
GrClipEdgeTypeIsInverseFill(const GrClipEdgeType edgeType)374 static constexpr bool GrClipEdgeTypeIsInverseFill(const GrClipEdgeType edgeType) {
375     return (GrClipEdgeType::kInverseFillAA == edgeType ||
376             GrClipEdgeType::kInverseFillBW == edgeType);
377 }
378 
GrClipEdgeTypeIsAA(const GrClipEdgeType edgeType)379 static constexpr bool GrClipEdgeTypeIsAA(const GrClipEdgeType edgeType) {
380     return (GrClipEdgeType::kFillBW != edgeType &&
381             GrClipEdgeType::kInverseFillBW != edgeType);
382 }
383 
GrInvertClipEdgeType(const GrClipEdgeType edgeType)384 static inline GrClipEdgeType GrInvertClipEdgeType(const GrClipEdgeType edgeType) {
385     switch (edgeType) {
386         case GrClipEdgeType::kFillBW:
387             return GrClipEdgeType::kInverseFillBW;
388         case GrClipEdgeType::kFillAA:
389             return GrClipEdgeType::kInverseFillAA;
390         case GrClipEdgeType::kInverseFillBW:
391             return GrClipEdgeType::kFillBW;
392         case GrClipEdgeType::kInverseFillAA:
393             return GrClipEdgeType::kFillAA;
394     }
395     SkUNREACHABLE;
396 }
397 
398 /**
399  * Indicates the type of pending IO operations that can be recorded for gpu resources.
400  */
401 enum GrIOType {
402     kRead_GrIOType,
403     kWrite_GrIOType,
404     kRW_GrIOType
405 };
406 
407 /**
408  * Indicates the type of data that a GPU buffer will be used for.
409  */
410 enum class GrGpuBufferType {
411     kVertex,
412     kIndex,
413     kDrawIndirect,
414     kXferCpuToGpu,
415     kXferGpuToCpu,
416     kUniform,
417 };
418 static const constexpr int kGrGpuBufferTypeCount = static_cast<int>(GrGpuBufferType::kUniform) + 1;
419 
420 /**
421  * Provides a performance hint regarding the frequency at which a data store will be accessed.
422  */
423 enum GrAccessPattern {
424     /** Data store will be respecified repeatedly and used many times. */
425     kDynamic_GrAccessPattern,
426     /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
427     kStatic_GrAccessPattern,
428     /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
429     kStream_GrAccessPattern,
430 
431     kLast_GrAccessPattern = kStream_GrAccessPattern
432 };
433 
434 // Flags shared between the GrSurface & GrSurfaceProxy class hierarchies
435 enum class GrInternalSurfaceFlags {
436     kNone                           = 0,
437 
438     // Texture-level
439 
440     // Means the pixels in the texture are read-only. Cannot also be a GrRenderTarget[Proxy].
441     kReadOnly                       = 1 << 0,
442 
443     // RT-level
444 
445     // This flag is for use with GL only. It tells us that the internal render target wraps FBO 0.
446     kGLRTFBOIDIs0                   = 1 << 1,
447 
448     // This means the render target is multisampled, and internally holds a non-msaa texture for
449     // resolving into. The render target resolves itself by blitting into this internal texture.
450     // (asTexture() might or might not return the internal texture, but if it does, we always
451     // resolve the render target before accessing this texture's data.)
452     kRequiresManualMSAAResolve      = 1 << 2,
453 
454     // This means the pixels in the render target are write-only. This is used for Dawn and Metal
455     // swap chain targets which can be rendered to, but not read or copied.
456     kFramebufferOnly                = 1 << 3,
457 
458     // This is a Vulkan only flag. If set the surface can be used as an input attachment in a
459     // shader. This is used for doing in shader blending where we want to sample from the same
460     // image we are drawing to.
461     kVkRTSupportsInputAttachment    = 1 << 4,
462 };
463 
464 SK_MAKE_BITFIELD_CLASS_OPS(GrInternalSurfaceFlags)
465 
466 // 'SK_MAKE_BITFIELD_CLASS_OPS' defines the & operator on GrInternalSurfaceFlags to return bool.
467 // We want to find the bitwise & with these masks, so we declare them as ints.
468 constexpr static int kGrInternalTextureFlagsMask = static_cast<int>(
469         GrInternalSurfaceFlags::kReadOnly);
470 
471 // We don't include kVkRTSupportsInputAttachment in this mask since we check it manually. We don't
472 // require that both the surface and proxy have matching values for this flag. Instead we require
473 // if the proxy has it set then the surface must also have it set. All other flags listed here must
474 // match on the proxy and surface.
475 // TODO: Add back kFramebufferOnly flag here once we update GrSurfaceCharacterization to take it
476 // as a flag. skbug.com/10672
477 constexpr static int kGrInternalRenderTargetFlagsMask = static_cast<int>(
478         GrInternalSurfaceFlags::kGLRTFBOIDIs0 |
479         GrInternalSurfaceFlags::kRequiresManualMSAAResolve/* |
480         GrInternalSurfaceFlags::kFramebufferOnly*/);
481 
482 constexpr static int kGrInternalTextureRenderTargetFlagsMask =
483         kGrInternalTextureFlagsMask | kGrInternalRenderTargetFlagsMask;
484 
485 #ifdef SK_DEBUG
486 // Takes a pointer to a GrCaps, and will suppress prints if required
487 #define GrCapsDebugf(caps, ...)  if (!(caps)->suppressPrints()) SkDebugf(__VA_ARGS__)
488 #else
489 #define GrCapsDebugf(caps, ...) do {} while (0)
490 #endif
491 
492 /**
493  * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
494  */
495 enum class GrBackendObjectOwnership : bool {
496     /** Holder does not destroy the backend object. */
497     kBorrowed = false,
498     /** Holder destroys the backend object. */
499     kOwned = true
500 };
501 
502 /**
503  * Used to include or exclude specific GPU path renderers for testing purposes.
504  */
505 enum class GpuPathRenderers {
506     kNone              =   0,  // Always use software masks and/or DefaultPathRenderer.
507     kDashLine          =   1 << 0,
508     kAtlas             =   1 << 1,
509     kTessellation      =   1 << 2,
510     kCoverageCounting  =   1 << 3,
511     kAAHairline        =   1 << 4,
512     kAAConvex          =   1 << 5,
513     kAALinearizing     =   1 << 6,
514     kSmall             =   1 << 7,
515     kTriangulating     =   1 << 8,
516     kDefault           = ((1 << 9) - 1)  // All path renderers.
517 };
518 
519 /**
520  * Used to describe the current state of Mips on a GrTexture
521  */
522 enum class GrMipmapStatus {
523     kNotAllocated, // Mips have not been allocated
524     kDirty,        // Mips are allocated but the full mip tree does not have valid data
525     kValid,        // All levels fully allocated and have valid data in them
526 };
527 
528 SK_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers)
529 
530 /**
531  * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels,
532  * their type, and width. This exists so that the GPU backend can have private types that have no
533  * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend.
534  * It does not refer to a texture format and the mapping to texture formats may be many-to-many.
535  * It does not specify the sRGB encoding of the stored values. The components are listed in order of
536  * where they appear in memory. In other words the first component listed is in the low bits and
537  * the last component in the high bits.
538  */
539 enum class GrColorType {
540     kUnknown,
541     kAlpha_8,
542     kBGR_565,
543     kRGB_565,
544     kABGR_4444,  // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed.
545     kRGBA_8888,
546     kRGBA_8888_SRGB,
547     kRGB_888x,
548     kRG_88,
549     kBGRA_8888,
550     kRGBA_1010102,
551     kBGRA_1010102,
552     kRGB_101010x,
553     kRGBA_10x6,
554     kGray_8,
555     kGrayAlpha_88,
556     kAlpha_F16,
557     kRGBA_F16,
558     kRGB_F16F16F16x,
559     kRGBA_F16_Clamped,
560     kRGBA_F32,
561 
562     kAlpha_16,
563     kRG_1616,
564     kRG_F16,
565     kRGBA_16161616,
566 
567     // Unusual types that come up after reading back in cases where we are reassigning the meaning
568     // of a texture format's channels to use for a particular color format but have to read back the
569     // data to a full RGBA quadruple. (e.g. using a R8 texture format as A8 color type but the API
570     // only supports reading to RGBA8.) None of these have SkColorType equivalents.
571     kAlpha_8xxx,
572     kAlpha_F32xxx,
573     kGray_8xxx,
574     kR_8xxx,
575 
576     // Types used to initialize backend textures.
577     kRGB_888,
578     kR_8,
579     kR_16,
580     kR_F16,
581     kGray_F16,
582     kBGRA_4444,
583     kARGB_4444,
584 
585     kLast = kARGB_4444
586 };
587 
588 static const int kGrColorTypeCnt = static_cast<int>(GrColorType::kLast) + 1;
589 
GrColorTypeToSkColorType(GrColorType ct)590 static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct) {
591     switch (ct) {
592         case GrColorType::kUnknown:          return kUnknown_SkColorType;
593         case GrColorType::kAlpha_8:          return kAlpha_8_SkColorType;
594         case GrColorType::kBGR_565:          return kRGB_565_SkColorType;
595         case GrColorType::kRGB_565:          return kUnknown_SkColorType;
596         case GrColorType::kABGR_4444:        return kARGB_4444_SkColorType;
597         case GrColorType::kRGBA_8888:        return kRGBA_8888_SkColorType;
598         case GrColorType::kRGBA_8888_SRGB:   return kSRGBA_8888_SkColorType;
599         case GrColorType::kRGB_888x:         return kRGB_888x_SkColorType;
600         case GrColorType::kRG_88:            return kR8G8_unorm_SkColorType;
601         case GrColorType::kBGRA_8888:        return kBGRA_8888_SkColorType;
602         case GrColorType::kRGBA_1010102:     return kRGBA_1010102_SkColorType;
603         case GrColorType::kBGRA_1010102:     return kBGRA_1010102_SkColorType;
604         case GrColorType::kRGB_101010x:      return kRGB_101010x_SkColorType;
605         case GrColorType::kRGBA_10x6:        return kRGBA_10x6_SkColorType;
606         case GrColorType::kGray_8:           return kGray_8_SkColorType;
607         case GrColorType::kGrayAlpha_88:     return kUnknown_SkColorType;
608         case GrColorType::kAlpha_F16:        return kA16_float_SkColorType;
609         case GrColorType::kRGBA_F16:         return kRGBA_F16_SkColorType;
610         case GrColorType::kRGBA_F16_Clamped: return kRGBA_F16Norm_SkColorType;
611         case GrColorType::kRGB_F16F16F16x:   return kRGB_F16F16F16x_SkColorType;
612         case GrColorType::kRGBA_F32:         return kRGBA_F32_SkColorType;
613         case GrColorType::kAlpha_8xxx:       return kUnknown_SkColorType;
614         case GrColorType::kAlpha_F32xxx:     return kUnknown_SkColorType;
615         case GrColorType::kGray_8xxx:        return kUnknown_SkColorType;
616         case GrColorType::kR_8xxx:           return kUnknown_SkColorType;
617         case GrColorType::kAlpha_16:         return kA16_unorm_SkColorType;
618         case GrColorType::kRG_1616:          return kR16G16_unorm_SkColorType;
619         case GrColorType::kRGBA_16161616:    return kR16G16B16A16_unorm_SkColorType;
620         case GrColorType::kRG_F16:           return kR16G16_float_SkColorType;
621         case GrColorType::kRGB_888:          return kUnknown_SkColorType;
622         case GrColorType::kR_8:              return kR8_unorm_SkColorType;
623         case GrColorType::kR_16:             return kUnknown_SkColorType;
624         case GrColorType::kR_F16:            return kUnknown_SkColorType;
625         case GrColorType::kGray_F16:         return kUnknown_SkColorType;
626         case GrColorType::kARGB_4444:        return kUnknown_SkColorType;
627         case GrColorType::kBGRA_4444:        return kUnknown_SkColorType;
628     }
629     SkUNREACHABLE;
630 }
631 
SkColorTypeToGrColorType(SkColorType ct)632 static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct) {
633     switch (ct) {
634         case kUnknown_SkColorType:            return GrColorType::kUnknown;
635         case kAlpha_8_SkColorType:            return GrColorType::kAlpha_8;
636         case kRGB_565_SkColorType:            return GrColorType::kBGR_565;
637         case kARGB_4444_SkColorType:          return GrColorType::kABGR_4444;
638         case kRGBA_8888_SkColorType:          return GrColorType::kRGBA_8888;
639         case kSRGBA_8888_SkColorType:         return GrColorType::kRGBA_8888_SRGB;
640         case kRGB_888x_SkColorType:           return GrColorType::kRGB_888x;
641         case kBGRA_8888_SkColorType:          return GrColorType::kBGRA_8888;
642         case kGray_8_SkColorType:             return GrColorType::kGray_8;
643         case kRGBA_F16Norm_SkColorType:       return GrColorType::kRGBA_F16_Clamped;
644         case kRGBA_F16_SkColorType:           return GrColorType::kRGBA_F16;
645         case kRGB_F16F16F16x_SkColorType:     return GrColorType::kRGB_F16F16F16x;
646         case kRGBA_1010102_SkColorType:       return GrColorType::kRGBA_1010102;
647         case kRGB_101010x_SkColorType:        return GrColorType::kRGB_101010x;
648         case kBGRA_1010102_SkColorType:       return GrColorType::kBGRA_1010102;
649         case kBGR_101010x_SkColorType:        return GrColorType::kUnknown;
650         case kBGR_101010x_XR_SkColorType:     return GrColorType::kUnknown;
651         case kBGRA_10101010_XR_SkColorType:   return GrColorType::kUnknown;
652         case kRGBA_10x6_SkColorType:          return GrColorType::kRGBA_10x6;
653         case kRGBA_F32_SkColorType:           return GrColorType::kRGBA_F32;
654         case kR8G8_unorm_SkColorType:         return GrColorType::kRG_88;
655         case kA16_unorm_SkColorType:          return GrColorType::kAlpha_16;
656         case kR16G16_unorm_SkColorType:       return GrColorType::kRG_1616;
657         case kA16_float_SkColorType:          return GrColorType::kAlpha_F16;
658         case kR16G16_float_SkColorType:       return GrColorType::kRG_F16;
659         case kR16G16B16A16_unorm_SkColorType: return GrColorType::kRGBA_16161616;
660         case kR8_unorm_SkColorType:           return GrColorType::kR_8;
661     }
662     SkUNREACHABLE;
663 }
664 
GrColorTypeChannelFlags(GrColorType ct)665 static constexpr uint32_t GrColorTypeChannelFlags(GrColorType ct) {
666     switch (ct) {
667         case GrColorType::kUnknown:          return 0;
668         case GrColorType::kAlpha_8:          return kAlpha_SkColorChannelFlag;
669         case GrColorType::kBGR_565:          return kRGB_SkColorChannelFlags;
670         case GrColorType::kRGB_565:          return kRGB_SkColorChannelFlags;
671         case GrColorType::kABGR_4444:        return kRGBA_SkColorChannelFlags;
672         case GrColorType::kRGBA_8888:        return kRGBA_SkColorChannelFlags;
673         case GrColorType::kRGBA_8888_SRGB:   return kRGBA_SkColorChannelFlags;
674         case GrColorType::kRGB_888x:         return kRGB_SkColorChannelFlags;
675         case GrColorType::kRG_88:            return kRG_SkColorChannelFlags;
676         case GrColorType::kBGRA_8888:        return kRGBA_SkColorChannelFlags;
677         case GrColorType::kRGBA_1010102:     return kRGBA_SkColorChannelFlags;
678         case GrColorType::kBGRA_1010102:     return kRGBA_SkColorChannelFlags;
679         case GrColorType::kRGB_101010x:      return kRGB_SkColorChannelFlags;
680         case GrColorType::kRGBA_10x6:        return kRGBA_SkColorChannelFlags;
681         case GrColorType::kGray_8:           return kGray_SkColorChannelFlag;
682         case GrColorType::kGrayAlpha_88:     return kGrayAlpha_SkColorChannelFlags;
683         case GrColorType::kAlpha_F16:        return kAlpha_SkColorChannelFlag;
684         case GrColorType::kRGBA_F16:         return kRGBA_SkColorChannelFlags;
685         case GrColorType::kRGBA_F16_Clamped: return kRGBA_SkColorChannelFlags;
686         case GrColorType::kRGB_F16F16F16x:   return kRGB_SkColorChannelFlags;
687         case GrColorType::kRGBA_F32:         return kRGBA_SkColorChannelFlags;
688         case GrColorType::kAlpha_8xxx:       return kAlpha_SkColorChannelFlag;
689         case GrColorType::kAlpha_F32xxx:     return kAlpha_SkColorChannelFlag;
690         case GrColorType::kGray_8xxx:        return kGray_SkColorChannelFlag;
691         case GrColorType::kR_8xxx:           return kRed_SkColorChannelFlag;
692         case GrColorType::kAlpha_16:         return kAlpha_SkColorChannelFlag;
693         case GrColorType::kRG_1616:          return kRG_SkColorChannelFlags;
694         case GrColorType::kRGBA_16161616:    return kRGBA_SkColorChannelFlags;
695         case GrColorType::kRG_F16:           return kRG_SkColorChannelFlags;
696         case GrColorType::kRGB_888:          return kRGB_SkColorChannelFlags;
697         case GrColorType::kR_8:              return kRed_SkColorChannelFlag;
698         case GrColorType::kR_16:             return kRed_SkColorChannelFlag;
699         case GrColorType::kR_F16:            return kRed_SkColorChannelFlag;
700         case GrColorType::kGray_F16:         return kGray_SkColorChannelFlag;
701         case GrColorType::kARGB_4444:        return kRGBA_SkColorChannelFlags;
702         case GrColorType::kBGRA_4444:        return kRGBA_SkColorChannelFlags;
703     }
704     SkUNREACHABLE;
705 }
706 
707 /**
708  * Describes the encoding of channel data in a GrColorType.
709  */
710 enum class GrColorTypeEncoding {
711     kUnorm,
712     kSRGBUnorm,
713     // kSnorm,
714     kFloat,
715     // kSint
716     // kUint
717 };
718 
719 /**
720  * Describes a GrColorType by how many bits are used for each color component and how they are
721  * encoded. Currently all the non-zero channels share a single GrColorTypeEncoding. This could be
722  * expanded to store separate encodings and to indicate which bits belong to which components.
723  */
724 class GrColorFormatDesc {
725 public:
MakeRGBA(int rgba,GrColorTypeEncoding e)726     static constexpr GrColorFormatDesc MakeRGBA(int rgba, GrColorTypeEncoding e) {
727         return {rgba, rgba, rgba, rgba, 0, e};
728     }
729 
MakeRGBA(int rgb,int a,GrColorTypeEncoding e)730     static constexpr GrColorFormatDesc MakeRGBA(int rgb, int a, GrColorTypeEncoding e) {
731         return {rgb, rgb, rgb, a, 0, e};
732     }
733 
MakeRGB(int rgb,GrColorTypeEncoding e)734     static constexpr GrColorFormatDesc MakeRGB(int rgb, GrColorTypeEncoding e) {
735         return {rgb, rgb, rgb, 0, 0, e};
736     }
737 
MakeRGB(int r,int g,int b,GrColorTypeEncoding e)738     static constexpr GrColorFormatDesc MakeRGB(int r, int g, int b, GrColorTypeEncoding e) {
739         return {r, g, b, 0, 0, e};
740     }
741 
MakeAlpha(int a,GrColorTypeEncoding e)742     static constexpr GrColorFormatDesc MakeAlpha(int a, GrColorTypeEncoding e) {
743         return {0, 0, 0, a, 0, e};
744     }
745 
MakeR(int r,GrColorTypeEncoding e)746     static constexpr GrColorFormatDesc MakeR(int r, GrColorTypeEncoding e) {
747         return {r, 0, 0, 0, 0, e};
748     }
749 
MakeRG(int rg,GrColorTypeEncoding e)750     static constexpr GrColorFormatDesc MakeRG(int rg, GrColorTypeEncoding e) {
751         return {rg, rg, 0, 0, 0, e};
752     }
753 
MakeGray(int grayBits,GrColorTypeEncoding e)754     static constexpr GrColorFormatDesc MakeGray(int grayBits, GrColorTypeEncoding e) {
755         return {0, 0, 0, 0, grayBits, e};
756     }
757 
MakeGrayAlpha(int grayAlpha,GrColorTypeEncoding e)758     static constexpr GrColorFormatDesc MakeGrayAlpha(int grayAlpha, GrColorTypeEncoding e) {
759         return {0, 0, 0, 0, grayAlpha, e};
760     }
761 
MakeInvalid()762     static constexpr GrColorFormatDesc MakeInvalid() { return {}; }
763 
r()764     constexpr int r() const { return fRBits; }
g()765     constexpr int g() const { return fGBits; }
b()766     constexpr int b() const { return fBBits; }
a()767     constexpr int a() const { return fABits; }
768     constexpr int operator[](int c) const {
769         switch (c) {
770             case 0: return this->r();
771             case 1: return this->g();
772             case 2: return this->b();
773             case 3: return this->a();
774         }
775         SkUNREACHABLE;
776     }
777 
gray()778     constexpr int gray() const { return fGrayBits; }
779 
encoding()780     constexpr GrColorTypeEncoding encoding() const { return fEncoding; }
781 
782 private:
783     int fRBits = 0;
784     int fGBits = 0;
785     int fBBits = 0;
786     int fABits = 0;
787     int fGrayBits = 0;
788     GrColorTypeEncoding fEncoding = GrColorTypeEncoding::kUnorm;
789 
790     constexpr GrColorFormatDesc() = default;
791 
GrColorFormatDesc(int r,int g,int b,int a,int gray,GrColorTypeEncoding encoding)792     constexpr GrColorFormatDesc(int r, int g, int b, int a, int gray, GrColorTypeEncoding encoding)
793             : fRBits(r), fGBits(g), fBBits(b), fABits(a), fGrayBits(gray), fEncoding(encoding) {
794         SkASSERT(r >= 0 && g >= 0 && b >= 0 && a >= 0 && gray >= 0);
795         SkASSERT(!gray || (!r && !g && !b));
796         SkASSERT(r || g || b || a || gray);
797     }
798 };
799 
GrGetColorTypeDesc(GrColorType ct)800 static constexpr GrColorFormatDesc GrGetColorTypeDesc(GrColorType ct) {
801     switch (ct) {
802         case GrColorType::kUnknown:
803             return GrColorFormatDesc::MakeInvalid();
804         case GrColorType::kAlpha_8:
805             return GrColorFormatDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
806         case GrColorType::kBGR_565:
807             return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
808         case GrColorType::kRGB_565:
809             return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
810         case GrColorType::kABGR_4444:
811             return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
812         case GrColorType::kRGBA_8888:
813             return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
814         case GrColorType::kRGBA_8888_SRGB:
815             return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm);
816         case GrColorType::kRGB_888x:
817             return GrColorFormatDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
818         case GrColorType::kRG_88:
819             return GrColorFormatDesc::MakeRG(8, GrColorTypeEncoding::kUnorm);
820         case GrColorType::kBGRA_8888:
821             return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
822         case GrColorType::kRGBA_1010102:
823             return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
824         case GrColorType::kBGRA_1010102:
825             return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
826         case GrColorType::kRGB_101010x:
827             return GrColorFormatDesc::MakeRGB(10, GrColorTypeEncoding::kUnorm);
828         case GrColorType::kRGBA_10x6:
829             return GrColorFormatDesc::MakeRGBA(10, GrColorTypeEncoding::kUnorm);
830         case GrColorType::kGray_8:
831             return GrColorFormatDesc::MakeGray(8, GrColorTypeEncoding::kUnorm);
832         case GrColorType::kGrayAlpha_88:
833             return GrColorFormatDesc::MakeGrayAlpha(8, GrColorTypeEncoding::kUnorm);
834         case GrColorType::kAlpha_F16:
835             return GrColorFormatDesc::MakeAlpha(16, GrColorTypeEncoding::kFloat);
836         case GrColorType::kRGBA_F16:
837             return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
838         case GrColorType::kRGB_F16F16F16x:
839             return GrColorFormatDesc::MakeRGB(16, GrColorTypeEncoding::kFloat);
840         case GrColorType::kRGBA_F16_Clamped:
841             return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
842         case GrColorType::kRGBA_F32:
843             return GrColorFormatDesc::MakeRGBA(32, GrColorTypeEncoding::kFloat);
844         case GrColorType::kAlpha_8xxx:
845             return GrColorFormatDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
846         case GrColorType::kAlpha_F32xxx:
847             return GrColorFormatDesc::MakeAlpha(32, GrColorTypeEncoding::kFloat);
848         case GrColorType::kGray_8xxx:
849             return GrColorFormatDesc::MakeGray(8, GrColorTypeEncoding::kUnorm);
850         case GrColorType::kR_8xxx:
851             return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
852         case GrColorType::kAlpha_16:
853             return GrColorFormatDesc::MakeAlpha(16, GrColorTypeEncoding::kUnorm);
854         case GrColorType::kRG_1616:
855             return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kUnorm);
856         case GrColorType::kRGBA_16161616:
857             return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm);
858         case GrColorType::kRG_F16:
859             return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kFloat);
860         case GrColorType::kRGB_888:
861             return GrColorFormatDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
862         case GrColorType::kR_8:
863             return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
864         case GrColorType::kR_16:
865             return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kUnorm);
866         case GrColorType::kR_F16:
867             return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kFloat);
868         case GrColorType::kGray_F16:
869             return GrColorFormatDesc::MakeGray(16, GrColorTypeEncoding::kFloat);
870         case GrColorType::kARGB_4444:
871             return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
872         case GrColorType::kBGRA_4444:
873             return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
874     }
875     SkUNREACHABLE;
876 }
877 
GrColorTypeClampType(GrColorType colorType)878 static constexpr GrClampType GrColorTypeClampType(GrColorType colorType) {
879     if (GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kUnorm ||
880         GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kSRGBUnorm) {
881         return GrClampType::kAuto;
882     }
883     return GrColorType::kRGBA_F16_Clamped == colorType ? GrClampType::kManual : GrClampType::kNone;
884 }
885 
886 // Consider a color type "wider" than n if it has more than n bits for any its representable
887 // channels.
GrColorTypeIsWiderThan(GrColorType colorType,int n)888 static constexpr bool GrColorTypeIsWiderThan(GrColorType colorType, int n) {
889     SkASSERT(n > 0);
890     auto desc = GrGetColorTypeDesc(colorType);
891     return (desc.r() && desc.r() > n )||
892            (desc.g() && desc.g() > n) ||
893            (desc.b() && desc.b() > n) ||
894            (desc.a() && desc.a() > n) ||
895            (desc.gray() && desc.gray() > n);
896 }
897 
GrColorTypeIsAlphaOnly(GrColorType ct)898 static constexpr bool GrColorTypeIsAlphaOnly(GrColorType ct) {
899     return GrColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag;
900 }
901 
GrColorTypeHasAlpha(GrColorType ct)902 static constexpr bool GrColorTypeHasAlpha(GrColorType ct) {
903     return GrColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag;
904 }
905 
GrColorTypeBytesPerPixel(GrColorType ct)906 static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct) {
907     switch (ct) {
908         case GrColorType::kUnknown:          return 0;
909         case GrColorType::kAlpha_8:          return 1;
910         case GrColorType::kBGR_565:          return 2;
911         case GrColorType::kRGB_565:          return 2;
912         case GrColorType::kABGR_4444:        return 2;
913         case GrColorType::kRGBA_8888:        return 4;
914         case GrColorType::kRGBA_8888_SRGB:   return 4;
915         case GrColorType::kRGB_888x:         return 4;
916         case GrColorType::kRG_88:            return 2;
917         case GrColorType::kBGRA_8888:        return 4;
918         case GrColorType::kRGBA_1010102:     return 4;
919         case GrColorType::kBGRA_1010102:     return 4;
920         case GrColorType::kRGB_101010x:      return 4;
921         case GrColorType::kRGBA_10x6:        return 8;
922         case GrColorType::kGray_8:           return 1;
923         case GrColorType::kGrayAlpha_88:     return 2;
924         case GrColorType::kAlpha_F16:        return 2;
925         case GrColorType::kRGBA_F16:         return 8;
926         case GrColorType::kRGBA_F16_Clamped: return 8;
927         case GrColorType::kRGB_F16F16F16x:   return 8;
928         case GrColorType::kRGBA_F32:         return 16;
929         case GrColorType::kAlpha_8xxx:       return 4;
930         case GrColorType::kAlpha_F32xxx:     return 16;
931         case GrColorType::kGray_8xxx:        return 4;
932         case GrColorType::kR_8xxx:           return 4;
933         case GrColorType::kAlpha_16:         return 2;
934         case GrColorType::kRG_1616:          return 4;
935         case GrColorType::kRGBA_16161616:    return 8;
936         case GrColorType::kRG_F16:           return 4;
937         case GrColorType::kRGB_888:          return 3;
938         case GrColorType::kR_8:              return 1;
939         case GrColorType::kR_16:             return 2;
940         case GrColorType::kR_F16:            return 2;
941         case GrColorType::kGray_F16:         return 2;
942         case GrColorType::kARGB_4444:        return 2;
943         case GrColorType::kBGRA_4444:        return 2;
944     }
945     SkUNREACHABLE;
946 }
947 
948 enum class GrDstSampleFlags {
949     kNone = 0,
950     kRequiresTextureBarrier =   1 << 0,
951     kAsInputAttachment = 1 << 1,
952 };
953 SK_MAKE_BITFIELD_CLASS_OPS(GrDstSampleFlags)
954 
955 using GrVisitProxyFunc = std::function<void(GrSurfaceProxy*, skgpu::Mipmapped)>;
956 
957 #endif
958