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