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/SkColor.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/base/SkMacros.h"
19 #include "include/private/base/SkTypeTraits.h"
20
21 class GrBackendFormat;
22 class GrCaps;
23 class GrSurfaceProxy;
24
25 // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might
26 // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently
27 // used for idle resource purging so it shouldn't cause a correctness problem.
28 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
29 using GrStdSteadyClock = std::chrono::monotonic_clock;
30 #else
31 using GrStdSteadyClock = std::chrono::steady_clock;
32 #endif
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 kLastkFragment_GrShaderType = kFragment_GrShaderType
281 };
282 static const int kGrShaderTypeCount = kLastkFragment_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 SkSurfaceCharacterization 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 * Object for CPU-GPU synchronization
505 */
506 typedef uint64_t GrFence;
507
508 /**
509 * Used to include or exclude specific GPU path renderers for testing purposes.
510 */
511 enum class GpuPathRenderers {
512 kNone = 0, // Always use software masks and/or DefaultPathRenderer.
513 kDashLine = 1 << 0,
514 kAtlas = 1 << 1,
515 kTessellation = 1 << 2,
516 kCoverageCounting = 1 << 3,
517 kAAHairline = 1 << 4,
518 kAAConvex = 1 << 5,
519 kAALinearizing = 1 << 6,
520 kSmall = 1 << 7,
521 kTriangulating = 1 << 8,
522 kDefault = ((1 << 9) - 1) // All path renderers.
523 };
524
525 /**
526 * Used to describe the current state of Mips on a GrTexture
527 */
528 enum class GrMipmapStatus {
529 kNotAllocated, // Mips have not been allocated
530 kDirty, // Mips are allocated but the full mip tree does not have valid data
531 kValid, // All levels fully allocated and have valid data in them
532 };
533
534 GR_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers)
535
536 /**
537 * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels,
538 * their type, and width. This exists so that the GPU backend can have private types that have no
539 * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend.
540 * It does not refer to a texture format and the mapping to texture formats may be many-to-many.
541 * It does not specify the sRGB encoding of the stored values. The components are listed in order of
542 * where they appear in memory. In other words the first component listed is in the low bits and
543 * the last component in the high bits.
544 */
545 enum class GrColorType {
546 kUnknown,
547 kAlpha_8,
548 kBGR_565,
549 kABGR_4444, // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed.
550 kRGBA_8888,
551 kRGBA_8888_SRGB,
552 kRGB_888x,
553 kRG_88,
554 kBGRA_8888,
555 kRGBA_1010102,
556 kBGRA_1010102,
557 kGray_8,
558 kGrayAlpha_88,
559 kAlpha_F16,
560 kRGBA_F16,
561 kRGBA_F16_Clamped,
562 kRGBA_F32,
563
564 kAlpha_16,
565 kRG_1616,
566 kRG_F16,
567 kRGBA_16161616,
568
569 // Unusual types that come up after reading back in cases where we are reassigning the meaning
570 // of a texture format's channels to use for a particular color format but have to read back the
571 // data to a full RGBA quadruple. (e.g. using a R8 texture format as A8 color type but the API
572 // only supports reading to RGBA8.) None of these have SkColorType equivalents.
573 kAlpha_8xxx,
574 kAlpha_F32xxx,
575 kGray_8xxx,
576 kR_8xxx,
577
578 // Types used to initialize backend textures.
579 kRGB_888,
580 kR_8,
581 kR_16,
582 kR_F16,
583 kGray_F16,
584 kBGRA_4444,
585 kARGB_4444,
586
587 kLast = kARGB_4444
588 };
589
590 static const int kGrColorTypeCnt = static_cast<int>(GrColorType::kLast) + 1;
591
GrColorTypeToSkColorType(GrColorType ct)592 static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct) {
593 switch (ct) {
594 case GrColorType::kUnknown: return kUnknown_SkColorType;
595 case GrColorType::kAlpha_8: return kAlpha_8_SkColorType;
596 case GrColorType::kBGR_565: return kRGB_565_SkColorType;
597 case GrColorType::kABGR_4444: return kARGB_4444_SkColorType;
598 case GrColorType::kRGBA_8888: return kRGBA_8888_SkColorType;
599 case GrColorType::kRGBA_8888_SRGB: return kSRGBA_8888_SkColorType;
600 case GrColorType::kRGB_888x: return kRGB_888x_SkColorType;
601 case GrColorType::kRG_88: return kR8G8_unorm_SkColorType;
602 case GrColorType::kBGRA_8888: return kBGRA_8888_SkColorType;
603 case GrColorType::kRGBA_1010102: return kRGBA_1010102_SkColorType;
604 case GrColorType::kBGRA_1010102: return kBGRA_1010102_SkColorType;
605 case GrColorType::kGray_8: return kGray_8_SkColorType;
606 case GrColorType::kGrayAlpha_88: return kUnknown_SkColorType;
607 case GrColorType::kAlpha_F16: return kA16_float_SkColorType;
608 case GrColorType::kRGBA_F16: return kRGBA_F16_SkColorType;
609 case GrColorType::kRGBA_F16_Clamped: return kRGBA_F16Norm_SkColorType;
610 case GrColorType::kRGBA_F32: return kRGBA_F32_SkColorType;
611 case GrColorType::kAlpha_8xxx: return kUnknown_SkColorType;
612 case GrColorType::kAlpha_F32xxx: return kUnknown_SkColorType;
613 case GrColorType::kGray_8xxx: return kUnknown_SkColorType;
614 case GrColorType::kR_8xxx: return kUnknown_SkColorType;
615 case GrColorType::kAlpha_16: return kA16_unorm_SkColorType;
616 case GrColorType::kRG_1616: return kR16G16_unorm_SkColorType;
617 case GrColorType::kRGBA_16161616: return kR16G16B16A16_unorm_SkColorType;
618 case GrColorType::kRG_F16: return kR16G16_float_SkColorType;
619 case GrColorType::kRGB_888: return kUnknown_SkColorType;
620 case GrColorType::kR_8: return kR8_unorm_SkColorType;
621 case GrColorType::kR_16: return kUnknown_SkColorType;
622 case GrColorType::kR_F16: return kUnknown_SkColorType;
623 case GrColorType::kGray_F16: return kUnknown_SkColorType;
624 case GrColorType::kARGB_4444: return kUnknown_SkColorType;
625 case GrColorType::kBGRA_4444: return kUnknown_SkColorType;
626 }
627 SkUNREACHABLE;
628 }
629
SkColorTypeToGrColorType(SkColorType ct)630 static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct) {
631 switch (ct) {
632 case kUnknown_SkColorType: return GrColorType::kUnknown;
633 case kAlpha_8_SkColorType: return GrColorType::kAlpha_8;
634 case kRGB_565_SkColorType: return GrColorType::kBGR_565;
635 case kARGB_4444_SkColorType: return GrColorType::kABGR_4444;
636 case kRGBA_8888_SkColorType: return GrColorType::kRGBA_8888;
637 case kSRGBA_8888_SkColorType: return GrColorType::kRGBA_8888_SRGB;
638 case kRGB_888x_SkColorType: return GrColorType::kRGB_888x;
639 case kBGRA_8888_SkColorType: return GrColorType::kBGRA_8888;
640 case kGray_8_SkColorType: return GrColorType::kGray_8;
641 case kRGBA_F16Norm_SkColorType: return GrColorType::kRGBA_F16_Clamped;
642 case kRGBA_F16_SkColorType: return GrColorType::kRGBA_F16;
643 case kRGBA_1010102_SkColorType: return GrColorType::kRGBA_1010102;
644 case kRGB_101010x_SkColorType: return GrColorType::kUnknown;
645 case kBGRA_1010102_SkColorType: return GrColorType::kBGRA_1010102;
646 case kBGR_101010x_SkColorType: return GrColorType::kUnknown;
647 case kBGR_101010x_XR_SkColorType: return GrColorType::kUnknown;
648 case kRGBA_F32_SkColorType: return GrColorType::kRGBA_F32;
649 case kR8G8_unorm_SkColorType: return GrColorType::kRG_88;
650 case kA16_unorm_SkColorType: return GrColorType::kAlpha_16;
651 case kR16G16_unorm_SkColorType: return GrColorType::kRG_1616;
652 case kA16_float_SkColorType: return GrColorType::kAlpha_F16;
653 case kR16G16_float_SkColorType: return GrColorType::kRG_F16;
654 case kR16G16B16A16_unorm_SkColorType: return GrColorType::kRGBA_16161616;
655 case kR8_unorm_SkColorType: return GrColorType::kR_8;
656 }
657 SkUNREACHABLE;
658 }
659
GrColorTypeChannelFlags(GrColorType ct)660 static constexpr uint32_t GrColorTypeChannelFlags(GrColorType ct) {
661 switch (ct) {
662 case GrColorType::kUnknown: return 0;
663 case GrColorType::kAlpha_8: return kAlpha_SkColorChannelFlag;
664 case GrColorType::kBGR_565: return kRGB_SkColorChannelFlags;
665 case GrColorType::kABGR_4444: return kRGBA_SkColorChannelFlags;
666 case GrColorType::kRGBA_8888: return kRGBA_SkColorChannelFlags;
667 case GrColorType::kRGBA_8888_SRGB: return kRGBA_SkColorChannelFlags;
668 case GrColorType::kRGB_888x: return kRGB_SkColorChannelFlags;
669 case GrColorType::kRG_88: return kRG_SkColorChannelFlags;
670 case GrColorType::kBGRA_8888: return kRGBA_SkColorChannelFlags;
671 case GrColorType::kRGBA_1010102: return kRGBA_SkColorChannelFlags;
672 case GrColorType::kBGRA_1010102: return kRGBA_SkColorChannelFlags;
673 case GrColorType::kGray_8: return kGray_SkColorChannelFlag;
674 case GrColorType::kGrayAlpha_88: return kGrayAlpha_SkColorChannelFlags;
675 case GrColorType::kAlpha_F16: return kAlpha_SkColorChannelFlag;
676 case GrColorType::kRGBA_F16: return kRGBA_SkColorChannelFlags;
677 case GrColorType::kRGBA_F16_Clamped: return kRGBA_SkColorChannelFlags;
678 case GrColorType::kRGBA_F32: return kRGBA_SkColorChannelFlags;
679 case GrColorType::kAlpha_8xxx: return kAlpha_SkColorChannelFlag;
680 case GrColorType::kAlpha_F32xxx: return kAlpha_SkColorChannelFlag;
681 case GrColorType::kGray_8xxx: return kGray_SkColorChannelFlag;
682 case GrColorType::kR_8xxx: return kRed_SkColorChannelFlag;
683 case GrColorType::kAlpha_16: return kAlpha_SkColorChannelFlag;
684 case GrColorType::kRG_1616: return kRG_SkColorChannelFlags;
685 case GrColorType::kRGBA_16161616: return kRGBA_SkColorChannelFlags;
686 case GrColorType::kRG_F16: return kRG_SkColorChannelFlags;
687 case GrColorType::kRGB_888: return kRGB_SkColorChannelFlags;
688 case GrColorType::kR_8: return kRed_SkColorChannelFlag;
689 case GrColorType::kR_16: return kRed_SkColorChannelFlag;
690 case GrColorType::kR_F16: return kRed_SkColorChannelFlag;
691 case GrColorType::kGray_F16: return kGray_SkColorChannelFlag;
692 case GrColorType::kARGB_4444: return kRGBA_SkColorChannelFlags;
693 case GrColorType::kBGRA_4444: return kRGBA_SkColorChannelFlags;
694 }
695 SkUNREACHABLE;
696 }
697
698 /**
699 * Describes the encoding of channel data in a GrColorType.
700 */
701 enum class GrColorTypeEncoding {
702 kUnorm,
703 kSRGBUnorm,
704 // kSnorm,
705 kFloat,
706 // kSint
707 // kUint
708 };
709
710 /**
711 * Describes a GrColorType by how many bits are used for each color component and how they are
712 * encoded. Currently all the non-zero channels share a single GrColorTypeEncoding. This could be
713 * expanded to store separate encodings and to indicate which bits belong to which components.
714 */
715 class GrColorFormatDesc {
716 public:
MakeRGBA(int rgba,GrColorTypeEncoding e)717 static constexpr GrColorFormatDesc MakeRGBA(int rgba, GrColorTypeEncoding e) {
718 return {rgba, rgba, rgba, rgba, 0, e};
719 }
720
MakeRGBA(int rgb,int a,GrColorTypeEncoding e)721 static constexpr GrColorFormatDesc MakeRGBA(int rgb, int a, GrColorTypeEncoding e) {
722 return {rgb, rgb, rgb, a, 0, e};
723 }
724
MakeRGB(int rgb,GrColorTypeEncoding e)725 static constexpr GrColorFormatDesc MakeRGB(int rgb, GrColorTypeEncoding e) {
726 return {rgb, rgb, rgb, 0, 0, e};
727 }
728
MakeRGB(int r,int g,int b,GrColorTypeEncoding e)729 static constexpr GrColorFormatDesc MakeRGB(int r, int g, int b, GrColorTypeEncoding e) {
730 return {r, g, b, 0, 0, e};
731 }
732
MakeAlpha(int a,GrColorTypeEncoding e)733 static constexpr GrColorFormatDesc MakeAlpha(int a, GrColorTypeEncoding e) {
734 return {0, 0, 0, a, 0, e};
735 }
736
MakeR(int r,GrColorTypeEncoding e)737 static constexpr GrColorFormatDesc MakeR(int r, GrColorTypeEncoding e) {
738 return {r, 0, 0, 0, 0, e};
739 }
740
MakeRG(int rg,GrColorTypeEncoding e)741 static constexpr GrColorFormatDesc MakeRG(int rg, GrColorTypeEncoding e) {
742 return {rg, rg, 0, 0, 0, e};
743 }
744
MakeGray(int grayBits,GrColorTypeEncoding e)745 static constexpr GrColorFormatDesc MakeGray(int grayBits, GrColorTypeEncoding e) {
746 return {0, 0, 0, 0, grayBits, e};
747 }
748
MakeGrayAlpha(int grayAlpha,GrColorTypeEncoding e)749 static constexpr GrColorFormatDesc MakeGrayAlpha(int grayAlpha, GrColorTypeEncoding e) {
750 return {0, 0, 0, 0, grayAlpha, e};
751 }
752
MakeInvalid()753 static constexpr GrColorFormatDesc MakeInvalid() { return {}; }
754
r()755 constexpr int r() const { return fRBits; }
g()756 constexpr int g() const { return fGBits; }
b()757 constexpr int b() const { return fBBits; }
a()758 constexpr int a() const { return fABits; }
759 constexpr int operator[](int c) const {
760 switch (c) {
761 case 0: return this->r();
762 case 1: return this->g();
763 case 2: return this->b();
764 case 3: return this->a();
765 }
766 SkUNREACHABLE;
767 }
768
gray()769 constexpr int gray() const { return fGrayBits; }
770
encoding()771 constexpr GrColorTypeEncoding encoding() const { return fEncoding; }
772
773 private:
774 int fRBits = 0;
775 int fGBits = 0;
776 int fBBits = 0;
777 int fABits = 0;
778 int fGrayBits = 0;
779 GrColorTypeEncoding fEncoding = GrColorTypeEncoding::kUnorm;
780
781 constexpr GrColorFormatDesc() = default;
782
GrColorFormatDesc(int r,int g,int b,int a,int gray,GrColorTypeEncoding encoding)783 constexpr GrColorFormatDesc(int r, int g, int b, int a, int gray, GrColorTypeEncoding encoding)
784 : fRBits(r), fGBits(g), fBBits(b), fABits(a), fGrayBits(gray), fEncoding(encoding) {
785 SkASSERT(r >= 0 && g >= 0 && b >= 0 && a >= 0 && gray >= 0);
786 SkASSERT(!gray || (!r && !g && !b));
787 SkASSERT(r || g || b || a || gray);
788 }
789 };
790
GrGetColorTypeDesc(GrColorType ct)791 static constexpr GrColorFormatDesc GrGetColorTypeDesc(GrColorType ct) {
792 switch (ct) {
793 case GrColorType::kUnknown:
794 return GrColorFormatDesc::MakeInvalid();
795 case GrColorType::kAlpha_8:
796 return GrColorFormatDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
797 case GrColorType::kBGR_565:
798 return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
799 case GrColorType::kABGR_4444:
800 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
801 case GrColorType::kRGBA_8888:
802 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
803 case GrColorType::kRGBA_8888_SRGB:
804 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm);
805 case GrColorType::kRGB_888x:
806 return GrColorFormatDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
807 case GrColorType::kRG_88:
808 return GrColorFormatDesc::MakeRG(8, GrColorTypeEncoding::kUnorm);
809 case GrColorType::kBGRA_8888:
810 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
811 case GrColorType::kRGBA_1010102:
812 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
813 case GrColorType::kBGRA_1010102:
814 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
815 case GrColorType::kGray_8:
816 return GrColorFormatDesc::MakeGray(8, GrColorTypeEncoding::kUnorm);
817 case GrColorType::kGrayAlpha_88:
818 return GrColorFormatDesc::MakeGrayAlpha(8, GrColorTypeEncoding::kUnorm);
819 case GrColorType::kAlpha_F16:
820 return GrColorFormatDesc::MakeAlpha(16, GrColorTypeEncoding::kFloat);
821 case GrColorType::kRGBA_F16:
822 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
823 case GrColorType::kRGBA_F16_Clamped:
824 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
825 case GrColorType::kRGBA_F32:
826 return GrColorFormatDesc::MakeRGBA(32, GrColorTypeEncoding::kFloat);
827 case GrColorType::kAlpha_8xxx:
828 return GrColorFormatDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
829 case GrColorType::kAlpha_F32xxx:
830 return GrColorFormatDesc::MakeAlpha(32, GrColorTypeEncoding::kFloat);
831 case GrColorType::kGray_8xxx:
832 return GrColorFormatDesc::MakeGray(8, GrColorTypeEncoding::kUnorm);
833 case GrColorType::kR_8xxx:
834 return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
835 case GrColorType::kAlpha_16:
836 return GrColorFormatDesc::MakeAlpha(16, GrColorTypeEncoding::kUnorm);
837 case GrColorType::kRG_1616:
838 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kUnorm);
839 case GrColorType::kRGBA_16161616:
840 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm);
841 case GrColorType::kRG_F16:
842 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kFloat);
843 case GrColorType::kRGB_888:
844 return GrColorFormatDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
845 case GrColorType::kR_8:
846 return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
847 case GrColorType::kR_16:
848 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kUnorm);
849 case GrColorType::kR_F16:
850 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kFloat);
851 case GrColorType::kGray_F16:
852 return GrColorFormatDesc::MakeGray(16, GrColorTypeEncoding::kFloat);
853 case GrColorType::kARGB_4444:
854 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
855 case GrColorType::kBGRA_4444:
856 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
857 }
858 SkUNREACHABLE;
859 }
860
GrColorTypeClampType(GrColorType colorType)861 static constexpr GrClampType GrColorTypeClampType(GrColorType colorType) {
862 if (GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kUnorm ||
863 GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kSRGBUnorm) {
864 return GrClampType::kAuto;
865 }
866 return GrColorType::kRGBA_F16_Clamped == colorType ? GrClampType::kManual : GrClampType::kNone;
867 }
868
869 // Consider a color type "wider" than n if it has more than n bits for any its representable
870 // channels.
GrColorTypeIsWiderThan(GrColorType colorType,int n)871 static constexpr bool GrColorTypeIsWiderThan(GrColorType colorType, int n) {
872 SkASSERT(n > 0);
873 auto desc = GrGetColorTypeDesc(colorType);
874 return (desc.r() && desc.r() > n )||
875 (desc.g() && desc.g() > n) ||
876 (desc.b() && desc.b() > n) ||
877 (desc.a() && desc.a() > n) ||
878 (desc.gray() && desc.gray() > n);
879 }
880
GrColorTypeIsAlphaOnly(GrColorType ct)881 static constexpr bool GrColorTypeIsAlphaOnly(GrColorType ct) {
882 return GrColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag;
883 }
884
GrColorTypeHasAlpha(GrColorType ct)885 static constexpr bool GrColorTypeHasAlpha(GrColorType ct) {
886 return GrColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag;
887 }
888
GrColorTypeBytesPerPixel(GrColorType ct)889 static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct) {
890 switch (ct) {
891 case GrColorType::kUnknown: return 0;
892 case GrColorType::kAlpha_8: return 1;
893 case GrColorType::kBGR_565: return 2;
894 case GrColorType::kABGR_4444: return 2;
895 case GrColorType::kRGBA_8888: return 4;
896 case GrColorType::kRGBA_8888_SRGB: return 4;
897 case GrColorType::kRGB_888x: return 4;
898 case GrColorType::kRG_88: return 2;
899 case GrColorType::kBGRA_8888: return 4;
900 case GrColorType::kRGBA_1010102: return 4;
901 case GrColorType::kBGRA_1010102: return 4;
902 case GrColorType::kGray_8: return 1;
903 case GrColorType::kGrayAlpha_88: return 2;
904 case GrColorType::kAlpha_F16: return 2;
905 case GrColorType::kRGBA_F16: return 8;
906 case GrColorType::kRGBA_F16_Clamped: return 8;
907 case GrColorType::kRGBA_F32: return 16;
908 case GrColorType::kAlpha_8xxx: return 4;
909 case GrColorType::kAlpha_F32xxx: return 16;
910 case GrColorType::kGray_8xxx: return 4;
911 case GrColorType::kR_8xxx: return 4;
912 case GrColorType::kAlpha_16: return 2;
913 case GrColorType::kRG_1616: return 4;
914 case GrColorType::kRGBA_16161616: return 8;
915 case GrColorType::kRG_F16: return 4;
916 case GrColorType::kRGB_888: return 3;
917 case GrColorType::kR_8: return 1;
918 case GrColorType::kR_16: return 2;
919 case GrColorType::kR_F16: return 2;
920 case GrColorType::kGray_F16: return 2;
921 case GrColorType::kARGB_4444: return 2;
922 case GrColorType::kBGRA_4444: return 2;
923 }
924 SkUNREACHABLE;
925 }
926
927 // In general we try to not mix CompressionType and ColorType, but currently SkImage still requires
928 // an SkColorType even for CompressedTypes so we need some conversion.
GrCompressionTypeToSkColorType(SkImage::CompressionType compression)929 static constexpr SkColorType GrCompressionTypeToSkColorType(SkImage::CompressionType compression) {
930 switch (compression) {
931 case SkImage::CompressionType::kNone: return kUnknown_SkColorType;
932 case SkImage::CompressionType::kETC2_RGB8_UNORM: return kRGB_888x_SkColorType;
933 case SkImage::CompressionType::kBC1_RGB8_UNORM: return kRGB_888x_SkColorType;
934 case SkImage::CompressionType::kBC1_RGBA8_UNORM: return kRGBA_8888_SkColorType;
935 }
936
937 SkUNREACHABLE;
938 }
939
940 enum class GrDstSampleFlags {
941 kNone = 0,
942 kRequiresTextureBarrier = 1 << 0,
943 kAsInputAttachment = 1 << 1,
944 };
945 GR_MAKE_BITFIELD_CLASS_OPS(GrDstSampleFlags)
946
947 using GrVisitProxyFunc = std::function<void(GrSurfaceProxy*, GrMipmapped)>;
948
949 #if defined(SK_DEBUG) || GR_TEST_UTILS || defined(SK_ENABLE_DUMP_GPU)
GrBackendApiToStr(GrBackendApi api)950 static constexpr const char* GrBackendApiToStr(GrBackendApi api) {
951 switch (api) {
952 case GrBackendApi::kOpenGL: return "OpenGL";
953 case GrBackendApi::kVulkan: return "Vulkan";
954 case GrBackendApi::kMetal: return "Metal";
955 case GrBackendApi::kDirect3D: return "Direct3D";
956 case GrBackendApi::kDawn: return "Dawn";
957 case GrBackendApi::kMock: return "Mock";
958 }
959 SkUNREACHABLE;
960 }
961
GrColorTypeToStr(GrColorType ct)962 static constexpr const char* GrColorTypeToStr(GrColorType ct) {
963 switch (ct) {
964 case GrColorType::kUnknown: return "kUnknown";
965 case GrColorType::kAlpha_8: return "kAlpha_8";
966 case GrColorType::kBGR_565: return "kRGB_565";
967 case GrColorType::kABGR_4444: return "kABGR_4444";
968 case GrColorType::kRGBA_8888: return "kRGBA_8888";
969 case GrColorType::kRGBA_8888_SRGB: return "kRGBA_8888_SRGB";
970 case GrColorType::kRGB_888x: return "kRGB_888x";
971 case GrColorType::kRG_88: return "kRG_88";
972 case GrColorType::kBGRA_8888: return "kBGRA_8888";
973 case GrColorType::kRGBA_1010102: return "kRGBA_1010102";
974 case GrColorType::kBGRA_1010102: return "kBGRA_1010102";
975 case GrColorType::kGray_8: return "kGray_8";
976 case GrColorType::kGrayAlpha_88: return "kGrayAlpha_88";
977 case GrColorType::kAlpha_F16: return "kAlpha_F16";
978 case GrColorType::kRGBA_F16: return "kRGBA_F16";
979 case GrColorType::kRGBA_F16_Clamped: return "kRGBA_F16_Clamped";
980 case GrColorType::kRGBA_F32: return "kRGBA_F32";
981 case GrColorType::kAlpha_8xxx: return "kAlpha_8xxx";
982 case GrColorType::kAlpha_F32xxx: return "kAlpha_F32xxx";
983 case GrColorType::kGray_8xxx: return "kGray_8xxx";
984 case GrColorType::kR_8xxx: return "kR_8xxx";
985 case GrColorType::kAlpha_16: return "kAlpha_16";
986 case GrColorType::kRG_1616: return "kRG_1616";
987 case GrColorType::kRGBA_16161616: return "kRGBA_16161616";
988 case GrColorType::kRG_F16: return "kRG_F16";
989 case GrColorType::kRGB_888: return "kRGB_888";
990 case GrColorType::kR_8: return "kR_8";
991 case GrColorType::kR_16: return "kR_16";
992 case GrColorType::kR_F16: return "kR_F16";
993 case GrColorType::kGray_F16: return "kGray_F16";
994 case GrColorType::kARGB_4444: return "kARGB_4444";
995 case GrColorType::kBGRA_4444: return "kBGRA_4444";
996 }
997 SkUNREACHABLE;
998 }
999
GrCompressionTypeToStr(SkImage::CompressionType compression)1000 static constexpr const char* GrCompressionTypeToStr(SkImage::CompressionType compression) {
1001 switch (compression) {
1002 case SkImage::CompressionType::kNone: return "kNone";
1003 case SkImage::CompressionType::kETC2_RGB8_UNORM: return "kETC2_RGB8_UNORM";
1004 case SkImage::CompressionType::kBC1_RGB8_UNORM: return "kBC1_RGB8_UNORM";
1005 case SkImage::CompressionType::kBC1_RGBA8_UNORM: return "kBC1_RGBA8_UNORM";
1006 }
1007 SkUNREACHABLE;
1008 }
1009
GrSurfaceOriginToStr(GrSurfaceOrigin origin)1010 static constexpr const char* GrSurfaceOriginToStr(GrSurfaceOrigin origin) {
1011 switch (origin) {
1012 case kTopLeft_GrSurfaceOrigin: return "kTopLeft";
1013 case kBottomLeft_GrSurfaceOrigin: return "kBottomLeft";
1014 }
1015 SkUNREACHABLE;
1016 }
1017 #endif
1018
1019 #endif
1020