• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrTypesPriv_DEFINED
9 #define GrTypesPriv_DEFINED
10 
11 #include <chrono>
12 #include "GrSharedEnums.h"
13 #include "GrTypes.h"
14 #include "SkCanvas.h"
15 #include "SkImageInfo.h"
16 #include "SkImageInfoPriv.h"
17 #include "SkRefCnt.h"
18 #include "SkWeakRefCnt.h"
19 
20 class GrCaps;
21 
22 // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might
23 // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently
24 // used for idle resource purging so it shouldn't cause a correctness problem.
25 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
26 using GrStdSteadyClock = std::chrono::monotonic_clock;
27 #else
28 using GrStdSteadyClock = std::chrono::steady_clock;
29 #endif
30 
31 /**
32  * Pixel configurations. This type conflates texture formats, CPU pixel formats, and
33  * premultipliedness. We are moving away from it towards SkColorType and backend API (GL, Vulkan)
34  * texture formats in the public API. Right now this mostly refers to texture formats as we're
35  * migrating.
36  */
37 enum GrPixelConfig {
38     kUnknown_GrPixelConfig,
39     kAlpha_8_GrPixelConfig,
40     kAlpha_8_as_Alpha_GrPixelConfig,
41     kAlpha_8_as_Red_GrPixelConfig,
42     kGray_8_GrPixelConfig,
43     kGray_8_as_Lum_GrPixelConfig,
44     kGray_8_as_Red_GrPixelConfig,
45     kRGB_565_GrPixelConfig,
46     kRGBA_4444_GrPixelConfig,
47     kRGBA_8888_GrPixelConfig,
48     kRGB_888_GrPixelConfig,
49     kRGB_888X_GrPixelConfig,
50     kRG_88_GrPixelConfig,
51     kBGRA_8888_GrPixelConfig,
52     kSRGBA_8888_GrPixelConfig,
53     kSBGRA_8888_GrPixelConfig,
54     kRGBA_1010102_GrPixelConfig,
55     kRGBA_float_GrPixelConfig,
56     kRG_float_GrPixelConfig,
57     kAlpha_half_GrPixelConfig,
58     kAlpha_half_as_Red_GrPixelConfig,
59     kRGBA_half_GrPixelConfig,
60     kRGBA_half_Clamped_GrPixelConfig,
61     kRGB_ETC1_GrPixelConfig,
62 
63     kLast_GrPixelConfig = kRGB_ETC1_GrPixelConfig
64 };
65 static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
66 
67 // Aliases for pixel configs that match skia's byte order.
68 #ifndef SK_CPU_LENDIAN
69 #error "Skia gpu currently assumes little endian"
70 #endif
71 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
72 static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
73 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
74 static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
75 #else
76     #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
77 #endif
78 
79 /**
80  * Geometric primitives used for drawing.
81  */
82 enum class GrPrimitiveType {
83     kTriangles,
84     kTriangleStrip,
85     kPoints,
86     kLines,          // 1 pix wide only
87     kLineStrip,      // 1 pix wide only
88     kLinesAdjacency  // requires geometry shader support.
89 };
90 static constexpr int kNumGrPrimitiveTypes = (int)GrPrimitiveType::kLinesAdjacency + 1;
91 
GrIsPrimTypeLines(GrPrimitiveType type)92 static constexpr bool GrIsPrimTypeLines(GrPrimitiveType type) {
93     return GrPrimitiveType::kLines == type ||
94            GrPrimitiveType::kLineStrip == type ||
95            GrPrimitiveType::kLinesAdjacency == type;
96 }
97 
GrIsPrimTypeTris(GrPrimitiveType type)98 static constexpr bool GrIsPrimTypeTris(GrPrimitiveType type) {
99     return GrPrimitiveType::kTriangles == type || GrPrimitiveType::kTriangleStrip == type;
100 }
101 
GrPrimTypeRequiresGeometryShaderSupport(GrPrimitiveType type)102 static constexpr bool GrPrimTypeRequiresGeometryShaderSupport(GrPrimitiveType type) {
103     return GrPrimitiveType::kLinesAdjacency == type;
104 }
105 
106 enum class GrPrimitiveRestart : bool {
107     kNo = false,
108     kYes = true
109 };
110 
111 /**
112  *  Formats for masks, used by the font cache. Important that these are 0-based.
113  */
114 enum GrMaskFormat {
115     kA8_GrMaskFormat,    //!< 1-byte per pixel
116     kA565_GrMaskFormat,  //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
117     kARGB_GrMaskFormat,  //!< 4-bytes per pixel, color format
118 
119     kLast_GrMaskFormat = kARGB_GrMaskFormat
120 };
121 static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
122 
123 /**
124  *  Return the number of bytes-per-pixel for the specified mask format.
125  */
GrMaskFormatBytesPerPixel(GrMaskFormat format)126 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
127     SkASSERT(format < kMaskFormatCount);
128     // kA8   (0) -> 1
129     // kA565 (1) -> 2
130     // kARGB (2) -> 4
131     static const int sBytesPerPixel[] = {1, 2, 4};
132     static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
133     static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
134     static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
135     static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
136 
137     return sBytesPerPixel[(int)format];
138 }
139 
140 /**
141  * Optional bitfield flags that can be set on GrSurfaceDesc (below).
142  */
143 enum GrSurfaceFlags {
144     kNone_GrSurfaceFlags = 0x0,
145     /**
146      * Creates a texture that can be rendered to as a GrRenderTarget. Use
147      * GrTexture::asRenderTarget() to access.
148      */
149     kRenderTarget_GrSurfaceFlag = 0x1,
150     /**
151      * Clears to zero on creation. It will cause creation failure if initial data is supplied to the
152      * texture. This only affects the base level if the texture is created with MIP levels.
153      */
154     kPerformInitialClear_GrSurfaceFlag = 0x2
155 };
156 GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
157 
158 typedef GrSurfaceFlags GrSurfaceDescFlags;
159 
160 /**
161  * Describes a surface to be created.
162  */
163 struct GrSurfaceDesc {
GrSurfaceDescGrSurfaceDesc164     GrSurfaceDesc()
165             : fFlags(kNone_GrSurfaceFlags)
166             , fWidth(0)
167             , fHeight(0)
168             , fConfig(kUnknown_GrPixelConfig)
169             , fSampleCnt(1) {}
170 
171     GrSurfaceDescFlags     fFlags;  //!< bitfield of TextureFlags
172     int                    fWidth;  //!< Width of the texture
173     int                    fHeight; //!< Height of the texture
174 
175     /**
176      * Format of source data of the texture. Not guaranteed to be the same as
177      * internal format used by 3D API.
178      */
179     GrPixelConfig          fConfig;
180 
181     /**
182      * The number of samples per pixel. Zero is treated equivalently to 1. This only
183      * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
184      * of samples may not exactly match the request. The request will be rounded
185      * up to the next supported sample count. A value larger than the largest
186      * supported sample count will fail.
187      */
188     int                    fSampleCnt;
189 };
190 
191 /** Ownership rules for external GPU resources imported into Skia. */
192 enum GrWrapOwnership {
193     /** Skia will assume the client will keep the resource alive and Skia will not free it. */
194     kBorrow_GrWrapOwnership,
195 
196     /** Skia will assume ownership of the resource and free it. */
197     kAdopt_GrWrapOwnership,
198 };
199 
200 enum class GrWrapCacheable : bool {
201     /**
202      * The wrapped resource will be removed from the cache as soon as it becomes purgeable. It may
203      * still be assigned and found by a unique key, but the presence of the key will not be used to
204      * keep the resource alive when it has no references.
205      */
206     kNo = false,
207     /**
208      * The wrapped resource is allowed to remain in the GrResourceCache when it has no references
209      * but has a unique key. Such resources should only be given unique keys when it is known that
210      * the key will eventually be removed from the resource or invalidated via the message bus.
211      */
212     kYes = true
213 };
214 
215 enum class GrBudgetedType : uint8_t {
216     /** The resource is budgeted and is subject to purging under budget pressure. */
217     kBudgeted,
218     /**
219      * The resource is unbudgeted and is purged as soon as it has no refs regardless of whether
220      * it has a unique or scratch key.
221      */
222     kUnbudgetedUncacheable,
223     /**
224      * The resource is unbudgeted and is allowed to remain in the cache with no refs if it
225      * has a unique key. Scratch keys are ignored.
226      */
227     kUnbudgetedCacheable,
228 };
229 
230 /**
231  * Clips are composed from these objects.
232  */
233 enum GrClipType {
234     kRect_ClipType,
235     kPath_ClipType
236 };
237 
238 enum class GrScissorTest : bool {
239     kDisabled = false,
240     kEnabled = true
241 };
242 
243 struct GrMipLevel {
244     const void* fPixels = nullptr;
245     size_t fRowBytes = 0;
246 };
247 
248 /**
249  * This enum is used to specify the load operation to be used when an opList/GrGpuCommandBuffer
250  * begins execution.
251  */
252 enum class GrLoadOp {
253     kLoad,
254     kClear,
255     kDiscard,
256 };
257 
258 /**
259  * This enum is used to specify the store operation to be used when an opList/GrGpuCommandBuffer
260  * ends execution.
261  */
262 enum class GrStoreOp {
263     kStore,
264     kDiscard,
265 };
266 
267 /**
268  * Used to control antialiasing in draw calls.
269  */
270 enum class GrAA : bool {
271     kNo = false,
272     kYes = true
273 };
274 
275 /** This enum indicates the type of antialiasing to be performed. */
276 enum class GrAAType : unsigned {
277     /** No antialiasing */
278     kNone,
279     /** Use fragment shader code to compute a fractional pixel coverage. */
280     kCoverage,
281     /** Use normal MSAA. */
282     kMSAA,
283     /**
284      * Use "mixed samples" MSAA such that the stencil buffer is multisampled but the color buffer is
285      * not.
286      */
287     kMixedSamples
288 };
289 
GrAATypeIsHW(GrAAType type)290 static inline bool GrAATypeIsHW(GrAAType type) {
291     switch (type) {
292         case GrAAType::kNone:
293             return false;
294         case GrAAType::kCoverage:
295             return false;
296         case GrAAType::kMSAA:
297             return true;
298         case GrAAType::kMixedSamples:
299             return true;
300     }
301     SK_ABORT("Unknown AA Type");
302     return false;
303 }
304 
305 /** The type of full scene antialiasing supported by a render target. */
306 enum class GrFSAAType {
307     /** No FSAA */
308     kNone,
309     /** Regular MSAA where each attachment has the same sample count. */
310     kUnifiedMSAA,
311     /** One color sample, N stencil samples. */
312     kMixedSamples,
313 };
314 
315 /**
316  * Not all drawing code paths support using mixed samples when available and instead use
317  * coverage-based aa.
318  */
319 enum class GrAllowMixedSamples : bool { kNo = false, kYes = true };
320 
321 GrAAType GrChooseAAType(GrAA, GrFSAAType, GrAllowMixedSamples, const GrCaps&);
322 
323 /*
324  * Some pixel configs are inherently clamped to [0,1], some are allowed to go outside that range,
325  * and some are FP but manually clamped in the XP.
326  */
327 enum class GrClampType {
328     kAuto,    // Normalized, fixed-point configs
329     kManual,  // Clamped FP configs
330     kNone,    // Normal (unclamped) FP configs
331 };
332 
333 /**
334  * A number of rectangle/quadrilateral drawing APIs can control anti-aliasing on a per edge basis.
335  * These masks specify which edges are AA'ed. The intent for this is to support tiling with seamless
336  * boundaries, where the inner edges are non-AA and the outer edges are AA. Regular draws (where AA
337  * is specified by GrAA) is almost equivalent to kNone or kAll, with the exception of how MSAA is
338  * handled.
339  *
340  * When tiling and there is MSAA, mixed edge rectangles are processed with MSAA, so in order for the
341  * tiled edges to remain seamless, inner tiles with kNone must also be processed with MSAA. In
342  * regular drawing, however, kNone should disable MSAA (if it's supported) to match the expected
343  * appearance.
344  *
345  * Therefore, APIs that use per-edge AA flags also take a GrAA value so that they can differentiate
346  * between the regular and tiling use case behaviors. Tiling operations should always pass
347  * GrAA::kYes while regular options should pass GrAA based on the SkPaint's anti-alias state.
348  */
349 enum class GrQuadAAFlags {
350     kLeft   = SkCanvas::kLeft_QuadAAFlag,
351     kTop    = SkCanvas::kTop_QuadAAFlag,
352     kRight  = SkCanvas::kRight_QuadAAFlag,
353     kBottom = SkCanvas::kBottom_QuadAAFlag,
354 
355     kNone = SkCanvas::kNone_QuadAAFlags,
356     kAll  = SkCanvas::kAll_QuadAAFlags
357 };
358 
GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)359 GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)
360 
361 static inline GrQuadAAFlags SkToGrQuadAAFlags(unsigned flags) {
362     return static_cast<GrQuadAAFlags>(flags);
363 }
364 
365 /**
366  * Types of shader-language-specific boxed variables we can create.
367  */
368 enum GrSLType {
369     kVoid_GrSLType,
370     kBool_GrSLType,
371     kByte_GrSLType,
372     kByte2_GrSLType,
373     kByte3_GrSLType,
374     kByte4_GrSLType,
375     kUByte_GrSLType,
376     kUByte2_GrSLType,
377     kUByte3_GrSLType,
378     kUByte4_GrSLType,
379     kShort_GrSLType,
380     kShort2_GrSLType,
381     kShort3_GrSLType,
382     kShort4_GrSLType,
383     kUShort_GrSLType,
384     kUShort2_GrSLType,
385     kUShort3_GrSLType,
386     kUShort4_GrSLType,
387     kFloat_GrSLType,
388     kFloat2_GrSLType,
389     kFloat3_GrSLType,
390     kFloat4_GrSLType,
391     kFloat2x2_GrSLType,
392     kFloat3x3_GrSLType,
393     kFloat4x4_GrSLType,
394     kHalf_GrSLType,
395     kHalf2_GrSLType,
396     kHalf3_GrSLType,
397     kHalf4_GrSLType,
398     kHalf2x2_GrSLType,
399     kHalf3x3_GrSLType,
400     kHalf4x4_GrSLType,
401     kInt_GrSLType,
402     kInt2_GrSLType,
403     kInt3_GrSLType,
404     kInt4_GrSLType,
405     kUint_GrSLType,
406     kUint2_GrSLType,
407     kTexture2DSampler_GrSLType,
408     kTextureExternalSampler_GrSLType,
409     kTexture2DRectSampler_GrSLType,
410 
411     kLast_GrSLType = kTexture2DRectSampler_GrSLType
412 };
413 static const int kGrSLTypeCount = kLast_GrSLType + 1;
414 
415 /**
416  * The type of texture. Backends other than GL currently only use the 2D value but the type must
417  * still be known at the API-neutral layer as it used to determine whether MIP maps, renderability,
418  * and sampling parameters are legal for proxies that will be instantiated with wrapped textures.
419  */
420 enum class GrTextureType {
421     k2D,
422     /* Rectangle uses unnormalized texture coordinates. */
423     kRectangle,
424     kExternal
425 };
426 
427 enum GrShaderType {
428     kVertex_GrShaderType,
429     kGeometry_GrShaderType,
430     kFragment_GrShaderType,
431 
432     kLastkFragment_GrShaderType = kFragment_GrShaderType
433 };
434 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
435 
436 enum GrShaderFlags {
437     kNone_GrShaderFlags = 0,
438     kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
439     kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
440     kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
441 };
442 GR_MAKE_BITFIELD_OPS(GrShaderFlags)
443 
444 /**
445  * Precisions of shader language variables. Not all shading languages support precisions or actually
446  * vary the internal precision based on the qualifiers. These currently only apply to float types (
447  * including float vectors and matrices).
448  */
449 enum GrSLPrecision : int {
450     kLow_GrSLPrecision,
451     kMedium_GrSLPrecision,
452     kHigh_GrSLPrecision,
453 
454     // Default precision is a special tag that means "whatever the default for the program/type
455     // combination is". In other words, it maps to the empty string in shader code. There are some
456     // scenarios where kDefault is not allowed (as the default precision for a program, or for
457     // varyings, for example).
458     kDefault_GrSLPrecision,
459 
460     // We only consider the "real" precisions here
461     kLast_GrSLPrecision = kHigh_GrSLPrecision,
462 };
463 
464 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
465 
466 /** Is the shading language type float (including vectors/matrices)? */
GrSLTypeIsFloatType(GrSLType type)467 static inline bool GrSLTypeIsFloatType(GrSLType type) {
468     switch (type) {
469         case kFloat_GrSLType:
470         case kFloat2_GrSLType:
471         case kFloat3_GrSLType:
472         case kFloat4_GrSLType:
473         case kFloat2x2_GrSLType:
474         case kFloat3x3_GrSLType:
475         case kFloat4x4_GrSLType:
476         case kHalf_GrSLType:
477         case kHalf2_GrSLType:
478         case kHalf3_GrSLType:
479         case kHalf4_GrSLType:
480         case kHalf2x2_GrSLType:
481         case kHalf3x3_GrSLType:
482         case kHalf4x4_GrSLType:
483             return true;
484 
485         case kVoid_GrSLType:
486         case kTexture2DSampler_GrSLType:
487         case kTextureExternalSampler_GrSLType:
488         case kTexture2DRectSampler_GrSLType:
489         case kBool_GrSLType:
490         case kByte_GrSLType:
491         case kByte2_GrSLType:
492         case kByte3_GrSLType:
493         case kByte4_GrSLType:
494         case kUByte_GrSLType:
495         case kUByte2_GrSLType:
496         case kUByte3_GrSLType:
497         case kUByte4_GrSLType:
498         case kShort_GrSLType:
499         case kShort2_GrSLType:
500         case kShort3_GrSLType:
501         case kShort4_GrSLType:
502         case kUShort_GrSLType:
503         case kUShort2_GrSLType:
504         case kUShort3_GrSLType:
505         case kUShort4_GrSLType:
506         case kInt_GrSLType:
507         case kInt2_GrSLType:
508         case kInt3_GrSLType:
509         case kInt4_GrSLType:
510         case kUint_GrSLType:
511         case kUint2_GrSLType:
512             return false;
513     }
514     SK_ABORT("Unexpected type");
515     return false;
516 }
517 
518 /** If the type represents a single value or vector return the vector length, else -1. */
GrSLTypeVecLength(GrSLType type)519 static inline int GrSLTypeVecLength(GrSLType type) {
520     switch (type) {
521         case kFloat_GrSLType:
522         case kHalf_GrSLType:
523         case kBool_GrSLType:
524         case kByte_GrSLType:
525         case kUByte_GrSLType:
526         case kShort_GrSLType:
527         case kUShort_GrSLType:
528         case kInt_GrSLType:
529         case kUint_GrSLType:
530             return 1;
531 
532         case kFloat2_GrSLType:
533         case kHalf2_GrSLType:
534         case kByte2_GrSLType:
535         case kUByte2_GrSLType:
536         case kShort2_GrSLType:
537         case kUShort2_GrSLType:
538         case kInt2_GrSLType:
539         case kUint2_GrSLType:
540             return 2;
541 
542         case kFloat3_GrSLType:
543         case kHalf3_GrSLType:
544         case kByte3_GrSLType:
545         case kUByte3_GrSLType:
546         case kShort3_GrSLType:
547         case kUShort3_GrSLType:
548         case kInt3_GrSLType:
549             return 3;
550 
551         case kFloat4_GrSLType:
552         case kHalf4_GrSLType:
553         case kByte4_GrSLType:
554         case kUByte4_GrSLType:
555         case kShort4_GrSLType:
556         case kUShort4_GrSLType:
557         case kInt4_GrSLType:
558             return 4;
559 
560         case kFloat2x2_GrSLType:
561         case kFloat3x3_GrSLType:
562         case kFloat4x4_GrSLType:
563         case kHalf2x2_GrSLType:
564         case kHalf3x3_GrSLType:
565         case kHalf4x4_GrSLType:
566         case kVoid_GrSLType:
567         case kTexture2DSampler_GrSLType:
568         case kTextureExternalSampler_GrSLType:
569         case kTexture2DRectSampler_GrSLType:
570             return -1;
571     }
572     SK_ABORT("Unexpected type");
573     return -1;
574 }
575 
GrSLCombinedSamplerTypeForTextureType(GrTextureType type)576 static inline GrSLType GrSLCombinedSamplerTypeForTextureType(GrTextureType type) {
577     switch (type) {
578         case GrTextureType::k2D:
579             return kTexture2DSampler_GrSLType;
580         case GrTextureType::kRectangle:
581             return kTexture2DRectSampler_GrSLType;
582         case GrTextureType::kExternal:
583             return kTextureExternalSampler_GrSLType;
584     }
585     SK_ABORT("Unexpected texture type");
586     return kTexture2DSampler_GrSLType;
587 }
588 
589 /** Rectangle and external textures ony support the clamp wrap mode and do not support MIP maps. */
GrTextureTypeHasRestrictedSampling(GrTextureType type)590 static inline bool GrTextureTypeHasRestrictedSampling(GrTextureType type) {
591     switch (type) {
592         case GrTextureType::k2D:
593             return false;
594         case GrTextureType::kRectangle:
595             return true;
596         case GrTextureType::kExternal:
597             return true;
598     }
599     SK_ABORT("Unexpected texture type");
600     return false;
601 }
602 
GrSLTypeIsCombinedSamplerType(GrSLType type)603 static inline bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
604     switch (type) {
605         case kTexture2DSampler_GrSLType:
606         case kTextureExternalSampler_GrSLType:
607         case kTexture2DRectSampler_GrSLType:
608             return true;
609 
610         case kVoid_GrSLType:
611         case kFloat_GrSLType:
612         case kFloat2_GrSLType:
613         case kFloat3_GrSLType:
614         case kFloat4_GrSLType:
615         case kFloat2x2_GrSLType:
616         case kFloat3x3_GrSLType:
617         case kFloat4x4_GrSLType:
618         case kHalf_GrSLType:
619         case kHalf2_GrSLType:
620         case kHalf3_GrSLType:
621         case kHalf4_GrSLType:
622         case kHalf2x2_GrSLType:
623         case kHalf3x3_GrSLType:
624         case kHalf4x4_GrSLType:
625         case kInt_GrSLType:
626         case kInt2_GrSLType:
627         case kInt3_GrSLType:
628         case kInt4_GrSLType:
629         case kUint_GrSLType:
630         case kUint2_GrSLType:
631         case kBool_GrSLType:
632         case kByte_GrSLType:
633         case kByte2_GrSLType:
634         case kByte3_GrSLType:
635         case kByte4_GrSLType:
636         case kUByte_GrSLType:
637         case kUByte2_GrSLType:
638         case kUByte3_GrSLType:
639         case kUByte4_GrSLType:
640         case kShort_GrSLType:
641         case kShort2_GrSLType:
642         case kShort3_GrSLType:
643         case kShort4_GrSLType:
644         case kUShort_GrSLType:
645         case kUShort2_GrSLType:
646         case kUShort3_GrSLType:
647         case kUShort4_GrSLType:
648             return false;
649     }
650     SK_ABORT("Unexpected type");
651     return false;
652 }
653 
654 //////////////////////////////////////////////////////////////////////////////
655 
656 /**
657  * Types used to describe format of vertices in arrays.
658  */
659 enum GrVertexAttribType {
660     kFloat_GrVertexAttribType = 0,
661     kFloat2_GrVertexAttribType,
662     kFloat3_GrVertexAttribType,
663     kFloat4_GrVertexAttribType,
664     kHalf_GrVertexAttribType,
665     kHalf2_GrVertexAttribType,
666     kHalf3_GrVertexAttribType,
667     kHalf4_GrVertexAttribType,
668 
669     kInt2_GrVertexAttribType,   // vector of 2 32-bit ints
670     kInt3_GrVertexAttribType,   // vector of 3 32-bit ints
671     kInt4_GrVertexAttribType,   // vector of 4 32-bit ints
672 
673 
674     kByte_GrVertexAttribType,  // signed byte
675     kByte2_GrVertexAttribType, // vector of 2 8-bit signed bytes
676     kByte3_GrVertexAttribType, // vector of 3 8-bit signed bytes
677     kByte4_GrVertexAttribType, // vector of 4 8-bit signed bytes
678     kUByte_GrVertexAttribType,  // unsigned byte
679     kUByte2_GrVertexAttribType, // vector of 2 8-bit unsigned bytes
680     kUByte3_GrVertexAttribType, // vector of 3 8-bit unsigned bytes
681     kUByte4_GrVertexAttribType, // vector of 4 8-bit unsigned bytes
682 
683     kUByte_norm_GrVertexAttribType,  // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
684     kUByte4_norm_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f,
685                                      // 255 -> 1.0f.
686 
687     kShort2_GrVertexAttribType,       // vector of 2 16-bit shorts.
688     kShort4_GrVertexAttribType,       // vector of 4 16-bit shorts.
689 
690     kUShort2_GrVertexAttribType,      // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
691     kUShort2_norm_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
692 
693     kInt_GrVertexAttribType,
694     kUint_GrVertexAttribType,
695 
696     kLast_GrVertexAttribType = kUint_GrVertexAttribType
697 };
698 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
699 
700 //////////////////////////////////////////////////////////////////////////////
701 
702 static const int kGrClipEdgeTypeCnt = (int) GrClipEdgeType::kLast + 1;
703 
GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType)704 static inline bool GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType) {
705     return (GrClipEdgeType::kFillAA == edgeType || GrClipEdgeType::kFillBW == edgeType);
706 }
707 
GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType)708 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType) {
709     return (GrClipEdgeType::kInverseFillAA == edgeType ||
710             GrClipEdgeType::kInverseFillBW == edgeType);
711 }
712 
GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType)713 static inline bool GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType) {
714     return (GrClipEdgeType::kFillBW != edgeType &&
715             GrClipEdgeType::kInverseFillBW != edgeType);
716 }
717 
GrInvertProcessorEdgeType(const GrClipEdgeType edgeType)718 static inline GrClipEdgeType GrInvertProcessorEdgeType(const GrClipEdgeType edgeType) {
719     switch (edgeType) {
720         case GrClipEdgeType::kFillBW:
721             return GrClipEdgeType::kInverseFillBW;
722         case GrClipEdgeType::kFillAA:
723             return GrClipEdgeType::kInverseFillAA;
724         case GrClipEdgeType::kInverseFillBW:
725             return GrClipEdgeType::kFillBW;
726         case GrClipEdgeType::kInverseFillAA:
727             return GrClipEdgeType::kFillAA;
728         case GrClipEdgeType::kHairlineAA:
729             SK_ABORT("Hairline fill isn't invertible.");
730     }
731     return GrClipEdgeType::kFillAA;  // suppress warning.
732 }
733 
734 /**
735  * Indicates the type of pending IO operations that can be recorded for gpu resources.
736  */
737 enum GrIOType {
738     kRead_GrIOType,
739     kWrite_GrIOType,
740     kRW_GrIOType
741 };
742 
743 /**
744  * Indicates the type of data that a GPU buffer will be used for.
745  */
746 enum class GrGpuBufferType {
747     kVertex,
748     kIndex,
749     kXferCpuToGpu,
750     kXferGpuToCpu,
751 };
752 static const int kGrGpuBufferTypeCount = static_cast<int>(GrGpuBufferType::kXferGpuToCpu) + 1;
753 
754 /**
755  * Provides a performance hint regarding the frequency at which a data store will be accessed.
756  */
757 enum GrAccessPattern {
758     /** Data store will be respecified repeatedly and used many times. */
759     kDynamic_GrAccessPattern,
760     /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
761     kStatic_GrAccessPattern,
762     /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
763     kStream_GrAccessPattern,
764 
765     kLast_GrAccessPattern = kStream_GrAccessPattern
766 };
767 
768 // Flags shared between the GrSurface & GrSurfaceProxy class hierarchies
769 enum class GrInternalSurfaceFlags {
770     kNone                           = 0,
771 
772     // Surface-level
773 
774     kNoPendingIO                    = 1 << 0,
775 
776     kSurfaceMask                    = kNoPendingIO,
777 
778     // Texture-level
779 
780     // Means the pixels in the texture are read-only. Cannot also be a GrRenderTarget[Proxy].
781     kReadOnly                       = 1 << 1,
782 
783     kTextureMask                    = kReadOnly,
784 
785     // RT-level
786 
787     // For internal resources:
788     //    this is enabled whenever MSAA is enabled and GrCaps reports mixed samples are supported
789     // For wrapped resources:
790     //    this is disabled for FBO0
791     //    but, otherwise, is enabled whenever MSAA is enabled and GrCaps reports mixed samples
792     //        are supported
793     kMixedSampled                   = 1 << 2,
794 
795     // This flag is for use with GL only. It tells us that the internal render target wraps FBO 0.
796     kGLRTFBOIDIs0                   = 1 << 3,
797 
798     kRenderTargetMask               = kMixedSampled | kGLRTFBOIDIs0,
799 };
800 GR_MAKE_BITFIELD_CLASS_OPS(GrInternalSurfaceFlags)
801 
802 #ifdef SK_DEBUG
803 // Takes a pointer to a GrCaps, and will suppress prints if required
804 #define GrCapsDebugf(caps, ...)  if (!(caps)->suppressPrints()) SkDebugf(__VA_ARGS__)
805 #else
806 #define GrCapsDebugf(caps, ...) do {} while (0)
807 #endif
808 
809 /**
810  * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
811  */
812 enum class GrBackendObjectOwnership : bool {
813     /** Holder does not destroy the backend object. */
814     kBorrowed = false,
815     /** Holder destroys the backend object. */
816     kOwned = true
817 };
818 
819 template <typename T>
unique_ptr_address_as_pointer_address(std::unique_ptr<T> const * up)820 T* const* unique_ptr_address_as_pointer_address(std::unique_ptr<T> const* up) {
821     static_assert(sizeof(T*) == sizeof(std::unique_ptr<T>), "unique_ptr not expected size.");
822     return reinterpret_cast<T* const*>(up);
823 }
824 
825 /*
826  * Object for CPU-GPU synchronization
827  */
828 typedef uint64_t GrFence;
829 
830 /**
831  * Used to include or exclude specific GPU path renderers for testing purposes.
832  */
833 enum class GpuPathRenderers {
834     kNone              = 0, // Always use sofware masks and/or GrDefaultPathRenderer.
835     kDashLine          = 1 << 0,
836     kStencilAndCover   = 1 << 1,
837     kCoverageCounting  = 1 << 2,
838     kAAHairline        = 1 << 3,
839     kAAConvex          = 1 << 4,
840     kAALinearizing     = 1 << 5,
841     kSmall             = 1 << 6,
842     kTessellating      = 1 << 7,
843 
844     kAll               = (kTessellating | (kTessellating - 1))
845 };
846 
847 /**
848  * Used to describe the current state of Mips on a GrTexture
849  */
850 enum class  GrMipMapsStatus {
851     kNotAllocated, // Mips have not been allocated
852     kDirty,        // Mips are allocated but the full mip tree does not have valid data
853     kValid,        // All levels fully allocated and have valid data in them
854 };
855 
856 GR_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers)
857 
858 /**
859  * Refers to the encoding of a GPU buffer as it will be interpreted by the GPU when sampling and
860  * blending.
861  */
862 enum class GrSRGBEncoded : bool { kNo = false, kYes = true };
863 
864 /**
865  * Describes whether pixel data encoding should be converted to/from linear/sRGB encoding.
866  */
867 enum class GrSRGBConversion {
868     kNone,
869     kSRGBToLinear,
870     kLinearToSRGB,
871 };
872 
873 /**
874  * Utility functions for GrPixelConfig
875  */
876 
877 // Returns whether the config's color channels are sRGB encoded.
GrPixelConfigIsSRGBEncoded(GrPixelConfig config)878 static inline GrSRGBEncoded GrPixelConfigIsSRGBEncoded(GrPixelConfig config) {
879     switch (config) {
880         case kSRGBA_8888_GrPixelConfig:
881         case kSBGRA_8888_GrPixelConfig:
882             return GrSRGBEncoded::kYes;
883         case kUnknown_GrPixelConfig:
884         case kAlpha_8_GrPixelConfig:
885         case kAlpha_8_as_Alpha_GrPixelConfig:
886         case kAlpha_8_as_Red_GrPixelConfig:
887         case kGray_8_GrPixelConfig:
888         case kGray_8_as_Lum_GrPixelConfig:
889         case kGray_8_as_Red_GrPixelConfig:
890         case kRGB_565_GrPixelConfig:
891         case kRGBA_4444_GrPixelConfig:
892         case kRGB_888_GrPixelConfig:
893         case kRGB_888X_GrPixelConfig:
894         case kRG_88_GrPixelConfig:
895         case kRGBA_8888_GrPixelConfig:
896         case kBGRA_8888_GrPixelConfig:
897         case kRGBA_1010102_GrPixelConfig:
898         case kRGBA_float_GrPixelConfig:
899         case kRG_float_GrPixelConfig:
900         case kAlpha_half_GrPixelConfig:
901         case kAlpha_half_as_Red_GrPixelConfig:
902         case kRGBA_half_GrPixelConfig:
903         case kRGBA_half_Clamped_GrPixelConfig:
904         case kRGB_ETC1_GrPixelConfig:
905             return GrSRGBEncoded::kNo;
906     }
907     SK_ABORT("Invalid pixel config");
908     return GrSRGBEncoded::kNo;
909 }
910 
GrPixelConfigIsSRGB(GrPixelConfig config)911 static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
912     return GrSRGBEncoded::kYes == GrPixelConfigIsSRGBEncoded(config);
913 }
914 
GrBytesPerPixel(GrPixelConfig config)915 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
916     switch (config) {
917         case kAlpha_8_GrPixelConfig:
918         case kAlpha_8_as_Alpha_GrPixelConfig:
919         case kAlpha_8_as_Red_GrPixelConfig:
920         case kGray_8_GrPixelConfig:
921         case kGray_8_as_Lum_GrPixelConfig:
922         case kGray_8_as_Red_GrPixelConfig:
923             return 1;
924         case kRGB_565_GrPixelConfig:
925         case kRGBA_4444_GrPixelConfig:
926         case kRG_88_GrPixelConfig:
927         case kAlpha_half_GrPixelConfig:
928         case kAlpha_half_as_Red_GrPixelConfig:
929             return 2;
930         case kRGBA_8888_GrPixelConfig:
931         case kRGB_888_GrPixelConfig:  // Assuming GPUs store this 4-byte aligned.
932         case kRGB_888X_GrPixelConfig:
933         case kBGRA_8888_GrPixelConfig:
934         case kSRGBA_8888_GrPixelConfig:
935         case kSBGRA_8888_GrPixelConfig:
936         case kRGBA_1010102_GrPixelConfig:
937             return 4;
938         case kRGBA_half_GrPixelConfig:
939         case kRGBA_half_Clamped_GrPixelConfig:
940             return 8;
941         case kRGBA_float_GrPixelConfig:
942             return 16;
943         case kRG_float_GrPixelConfig:
944             return 8;
945         case kUnknown_GrPixelConfig:
946         case kRGB_ETC1_GrPixelConfig:
947             return 0;
948     }
949     SK_ABORT("Invalid pixel config");
950     return 0;
951 }
952 
GrPixelConfigIsOpaque(GrPixelConfig config)953 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
954     switch (config) {
955         case kRGB_565_GrPixelConfig:
956         case kRGB_888_GrPixelConfig:
957         case kRGB_888X_GrPixelConfig:
958         case kRG_88_GrPixelConfig:
959         case kGray_8_GrPixelConfig:
960         case kGray_8_as_Lum_GrPixelConfig:
961         case kGray_8_as_Red_GrPixelConfig:
962         case kRG_float_GrPixelConfig:
963         case kRGB_ETC1_GrPixelConfig:
964             return true;
965         case kAlpha_8_GrPixelConfig:
966         case kAlpha_8_as_Alpha_GrPixelConfig:
967         case kAlpha_8_as_Red_GrPixelConfig:
968         case kRGBA_4444_GrPixelConfig:
969         case kAlpha_half_GrPixelConfig:
970         case kAlpha_half_as_Red_GrPixelConfig:
971         case kRGBA_8888_GrPixelConfig:
972         case kBGRA_8888_GrPixelConfig:
973         case kSRGBA_8888_GrPixelConfig:
974         case kSBGRA_8888_GrPixelConfig:
975         case kRGBA_1010102_GrPixelConfig:
976         case kRGBA_half_GrPixelConfig:
977         case kRGBA_half_Clamped_GrPixelConfig:
978         case kRGBA_float_GrPixelConfig:
979         case kUnknown_GrPixelConfig:
980             return false;
981     }
982     SK_ABORT("Invalid pixel config");
983     return false;
984 }
985 
GrPixelConfigIsAlphaOnly(GrPixelConfig config)986 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
987     switch (config) {
988         case kAlpha_8_GrPixelConfig:
989         case kAlpha_8_as_Alpha_GrPixelConfig:
990         case kAlpha_8_as_Red_GrPixelConfig:
991         case kAlpha_half_GrPixelConfig:
992         case kAlpha_half_as_Red_GrPixelConfig:
993             return true;
994         case kUnknown_GrPixelConfig:
995         case kGray_8_GrPixelConfig:
996         case kGray_8_as_Lum_GrPixelConfig:
997         case kGray_8_as_Red_GrPixelConfig:
998         case kRGB_565_GrPixelConfig:
999         case kRGBA_4444_GrPixelConfig:
1000         case kRGBA_8888_GrPixelConfig:
1001         case kRGB_888_GrPixelConfig:
1002         case kRGB_888X_GrPixelConfig:
1003         case kRG_88_GrPixelConfig:
1004         case kBGRA_8888_GrPixelConfig:
1005         case kSRGBA_8888_GrPixelConfig:
1006         case kSBGRA_8888_GrPixelConfig:
1007         case kRGBA_1010102_GrPixelConfig:
1008         case kRGBA_float_GrPixelConfig:
1009         case kRG_float_GrPixelConfig:
1010         case kRGBA_half_GrPixelConfig:
1011         case kRGBA_half_Clamped_GrPixelConfig:
1012         case kRGB_ETC1_GrPixelConfig:
1013             return false;
1014     }
1015     SK_ABORT("Invalid pixel config.");
1016     return false;
1017 }
1018 
GrPixelConfigIsFloatingPoint(GrPixelConfig config)1019 static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
1020     switch (config) {
1021         case kUnknown_GrPixelConfig:
1022         case kAlpha_8_GrPixelConfig:
1023         case kAlpha_8_as_Alpha_GrPixelConfig:
1024         case kAlpha_8_as_Red_GrPixelConfig:
1025         case kGray_8_GrPixelConfig:
1026         case kGray_8_as_Lum_GrPixelConfig:
1027         case kGray_8_as_Red_GrPixelConfig:
1028         case kRGB_565_GrPixelConfig:
1029         case kRGBA_4444_GrPixelConfig:
1030         case kRGB_888_GrPixelConfig:
1031         case kRGB_888X_GrPixelConfig:
1032         case kRG_88_GrPixelConfig:
1033         case kRGBA_8888_GrPixelConfig:
1034         case kBGRA_8888_GrPixelConfig:
1035         case kSRGBA_8888_GrPixelConfig:
1036         case kSBGRA_8888_GrPixelConfig:
1037         case kRGBA_1010102_GrPixelConfig:
1038         case kRGB_ETC1_GrPixelConfig:
1039             return false;
1040         case kRGBA_float_GrPixelConfig:
1041         case kRG_float_GrPixelConfig:
1042         case kAlpha_half_GrPixelConfig:
1043         case kAlpha_half_as_Red_GrPixelConfig:
1044         case kRGBA_half_GrPixelConfig:
1045         case kRGBA_half_Clamped_GrPixelConfig:
1046             return true;
1047     }
1048     SK_ABORT("Invalid pixel config.");
1049     return false;
1050 }
1051 
GrPixelConfigClampType(GrPixelConfig config)1052 static inline GrClampType GrPixelConfigClampType(GrPixelConfig config) {
1053     if (!GrPixelConfigIsFloatingPoint(config)) {
1054         return GrClampType::kAuto;
1055     }
1056     return kRGBA_half_Clamped_GrPixelConfig == config ? GrClampType::kManual : GrClampType::kNone;
1057 }
1058 
1059 /**
1060  * Returns true if the pixel config is a GPU-specific compressed format
1061  * representation.
1062  */
GrPixelConfigIsCompressed(GrPixelConfig config)1063 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
1064     switch (config) {
1065         case kRGB_ETC1_GrPixelConfig:
1066             return true;
1067         case kUnknown_GrPixelConfig:
1068         case kAlpha_8_GrPixelConfig:
1069         case kAlpha_8_as_Alpha_GrPixelConfig:
1070         case kAlpha_8_as_Red_GrPixelConfig:
1071         case kGray_8_GrPixelConfig:
1072         case kGray_8_as_Lum_GrPixelConfig:
1073         case kGray_8_as_Red_GrPixelConfig:
1074         case kRGB_565_GrPixelConfig:
1075         case kRGBA_4444_GrPixelConfig:
1076         case kRGB_888_GrPixelConfig:
1077         case kRGB_888X_GrPixelConfig:
1078         case kRG_88_GrPixelConfig:
1079         case kRGBA_8888_GrPixelConfig:
1080         case kBGRA_8888_GrPixelConfig:
1081         case kSRGBA_8888_GrPixelConfig:
1082         case kSBGRA_8888_GrPixelConfig:
1083         case kRGBA_1010102_GrPixelConfig:
1084         case kRGBA_float_GrPixelConfig:
1085         case kRG_float_GrPixelConfig:
1086         case kAlpha_half_GrPixelConfig:
1087         case kAlpha_half_as_Red_GrPixelConfig:
1088         case kRGBA_half_GrPixelConfig:
1089         case kRGBA_half_Clamped_GrPixelConfig:
1090             return false;
1091     }
1092     SK_ABORT("Invalid pixel config");
1093     return false;
1094 }
1095 
1096 /**
1097  * If the pixel config is compressed, return an equivalent uncompressed format.
1098  */
GrMakePixelConfigUncompressed(GrPixelConfig config)1099 static inline GrPixelConfig GrMakePixelConfigUncompressed(GrPixelConfig config) {
1100     switch (config) {
1101         case kRGB_ETC1_GrPixelConfig:
1102             return kRGBA_8888_GrPixelConfig;
1103         case kUnknown_GrPixelConfig:
1104         case kAlpha_8_GrPixelConfig:
1105         case kAlpha_8_as_Alpha_GrPixelConfig:
1106         case kAlpha_8_as_Red_GrPixelConfig:
1107         case kGray_8_GrPixelConfig:
1108         case kGray_8_as_Lum_GrPixelConfig:
1109         case kGray_8_as_Red_GrPixelConfig:
1110         case kRGB_565_GrPixelConfig:
1111         case kRGBA_4444_GrPixelConfig:
1112         case kRGB_888_GrPixelConfig:
1113         case kRGB_888X_GrPixelConfig:
1114         case kRG_88_GrPixelConfig:
1115         case kRGBA_8888_GrPixelConfig:
1116         case kBGRA_8888_GrPixelConfig:
1117         case kSRGBA_8888_GrPixelConfig:
1118         case kSBGRA_8888_GrPixelConfig:
1119         case kRGBA_1010102_GrPixelConfig:
1120         case kRGBA_float_GrPixelConfig:
1121         case kRG_float_GrPixelConfig:
1122         case kAlpha_half_GrPixelConfig:
1123         case kAlpha_half_as_Red_GrPixelConfig:
1124         case kRGBA_half_GrPixelConfig:
1125         case kRGBA_half_Clamped_GrPixelConfig:
1126             return config;
1127         }
1128     SK_ABORT("Invalid pixel config");
1129     return config;
1130 }
1131 
1132 /**
1133  * Returns the data size for the given compressed pixel config
1134  */
GrCompressedFormatDataSize(GrPixelConfig config,int width,int height)1135 static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
1136                                                 int width, int height) {
1137     SkASSERT(GrPixelConfigIsCompressed(config));
1138 
1139     switch (config) {
1140         case kRGB_ETC1_GrPixelConfig:
1141             SkASSERT((width & 3) == 0);
1142             SkASSERT((height & 3) == 0);
1143             return (width >> 2) * (height >> 2) * 8;
1144 
1145         case kUnknown_GrPixelConfig:
1146         case kAlpha_8_GrPixelConfig:
1147         case kAlpha_8_as_Alpha_GrPixelConfig:
1148         case kAlpha_8_as_Red_GrPixelConfig:
1149         case kGray_8_GrPixelConfig:
1150         case kGray_8_as_Lum_GrPixelConfig:
1151         case kGray_8_as_Red_GrPixelConfig:
1152         case kRGB_565_GrPixelConfig:
1153         case kRGBA_4444_GrPixelConfig:
1154         case kRGB_888_GrPixelConfig:
1155         case kRGB_888X_GrPixelConfig:
1156         case kRG_88_GrPixelConfig:
1157         case kRGBA_8888_GrPixelConfig:
1158         case kBGRA_8888_GrPixelConfig:
1159         case kSRGBA_8888_GrPixelConfig:
1160         case kSBGRA_8888_GrPixelConfig:
1161         case kRGBA_1010102_GrPixelConfig:
1162         case kRGBA_float_GrPixelConfig:
1163         case kRG_float_GrPixelConfig:
1164         case kAlpha_half_GrPixelConfig:
1165         case kAlpha_half_as_Red_GrPixelConfig:
1166         case kRGBA_half_GrPixelConfig:
1167         case kRGBA_half_Clamped_GrPixelConfig:
1168             SK_ABORT("Unknown compressed pixel config");
1169             return 4 * width * height;
1170     }
1171 
1172     SK_ABORT("Invalid pixel config");
1173     return 4 * width * height;
1174 }
1175 
1176 /**
1177  * Precision qualifier that should be used with a sampler.
1178  */
GrSLSamplerPrecision(GrPixelConfig config)1179 static inline GrSLPrecision GrSLSamplerPrecision(GrPixelConfig config) {
1180     switch (config) {
1181         case kUnknown_GrPixelConfig:
1182         case kAlpha_8_GrPixelConfig:
1183         case kAlpha_8_as_Alpha_GrPixelConfig:
1184         case kAlpha_8_as_Red_GrPixelConfig:
1185         case kGray_8_GrPixelConfig:
1186         case kGray_8_as_Lum_GrPixelConfig:
1187         case kGray_8_as_Red_GrPixelConfig:
1188         case kRGB_565_GrPixelConfig:
1189         case kRGBA_4444_GrPixelConfig:
1190         case kRGBA_8888_GrPixelConfig:
1191         case kRGB_888_GrPixelConfig:
1192         case kRGB_888X_GrPixelConfig:
1193         case kRG_88_GrPixelConfig:
1194         case kBGRA_8888_GrPixelConfig:
1195         case kSRGBA_8888_GrPixelConfig:
1196         case kSBGRA_8888_GrPixelConfig:
1197         case kRGB_ETC1_GrPixelConfig:
1198             return kLow_GrSLPrecision;
1199         case kRGBA_float_GrPixelConfig:
1200         case kRG_float_GrPixelConfig:
1201             return kHigh_GrSLPrecision;
1202         case kAlpha_half_GrPixelConfig:
1203         case kAlpha_half_as_Red_GrPixelConfig:
1204         case kRGBA_half_GrPixelConfig:
1205         case kRGBA_half_Clamped_GrPixelConfig:
1206         case kRGBA_1010102_GrPixelConfig:
1207             return kMedium_GrSLPrecision;
1208     }
1209     SK_ABORT("Unexpected type");
1210     return kHigh_GrSLPrecision;
1211 }
1212 
1213 /**
1214  * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels,
1215  * their type, and width. This exists so that the GPU backend can have private types that have no
1216  * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend.
1217  * It does not refer to a texture format and the mapping to texture formats may be many-to-many.
1218  * It does not specify the sRGB encoding of the stored values.
1219  */
1220 enum class GrColorType {
1221     kUnknown,
1222     kAlpha_8,
1223     kRGB_565,
1224     kABGR_4444,  // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed.
1225     kRGBA_8888,
1226     kRGB_888x,
1227     kRG_88,
1228     kBGRA_8888,
1229     kRGBA_1010102,
1230     kGray_8,
1231     kAlpha_F16,
1232     kRGBA_F16,
1233     kRGBA_F16_Clamped,
1234     kRG_F32,
1235     kRGBA_F32,
1236     kRGB_ETC1,   // This type doesn't appear in SkColorType at all.
1237 };
1238 
GrColorTypeToSkColorType(GrColorType ct)1239 static inline SkColorType GrColorTypeToSkColorType(GrColorType ct) {
1240     switch (ct) {
1241         case GrColorType::kUnknown:          return kUnknown_SkColorType;
1242         case GrColorType::kAlpha_8:          return kAlpha_8_SkColorType;
1243         case GrColorType::kRGB_565:          return kRGB_565_SkColorType;
1244         case GrColorType::kABGR_4444:        return kARGB_4444_SkColorType;
1245         case GrColorType::kRGBA_8888:        return kRGBA_8888_SkColorType;
1246         case GrColorType::kRGB_888x:         return kRGB_888x_SkColorType;
1247         case GrColorType::kRG_88:            return kUnknown_SkColorType;
1248         case GrColorType::kBGRA_8888:        return kBGRA_8888_SkColorType;
1249         case GrColorType::kRGBA_1010102:     return kRGBA_1010102_SkColorType;
1250         case GrColorType::kGray_8:           return kGray_8_SkColorType;
1251         case GrColorType::kAlpha_F16:        return kUnknown_SkColorType;
1252         case GrColorType::kRGBA_F16:         return kRGBA_F16_SkColorType;
1253         case GrColorType::kRGBA_F16_Clamped: return kRGBA_F16Norm_SkColorType;
1254         case GrColorType::kRG_F32:           return kUnknown_SkColorType;
1255         case GrColorType::kRGBA_F32:         return kRGBA_F32_SkColorType;
1256         case GrColorType::kRGB_ETC1:         return kUnknown_SkColorType;
1257     }
1258     SK_ABORT("Invalid GrColorType");
1259     return kUnknown_SkColorType;
1260 }
1261 
SkColorTypeToGrColorType(SkColorType ct)1262 static inline GrColorType SkColorTypeToGrColorType(SkColorType ct) {
1263     switch (ct) {
1264         case kUnknown_SkColorType:      return GrColorType::kUnknown;
1265         case kAlpha_8_SkColorType:      return GrColorType::kAlpha_8;
1266         case kRGB_565_SkColorType:      return GrColorType::kRGB_565;
1267         case kARGB_4444_SkColorType:    return GrColorType::kABGR_4444;
1268         case kRGBA_8888_SkColorType:    return GrColorType::kRGBA_8888;
1269         case kRGB_888x_SkColorType:     return GrColorType::kRGB_888x;
1270         case kBGRA_8888_SkColorType:    return GrColorType::kBGRA_8888;
1271         case kGray_8_SkColorType:       return GrColorType::kGray_8;
1272         case kRGBA_F16Norm_SkColorType: return GrColorType::kRGBA_F16_Clamped;
1273         case kRGBA_F16_SkColorType:     return GrColorType::kRGBA_F16;
1274         case kRGBA_1010102_SkColorType: return GrColorType::kRGBA_1010102;
1275         case kRGB_101010x_SkColorType:  return GrColorType::kUnknown;
1276         case kRGBA_F32_SkColorType:     return GrColorType::kRGBA_F32;
1277     }
1278     SK_ABORT("Invalid SkColorType");
1279     return GrColorType::kUnknown;
1280 }
1281 
GrColorTypeComponentFlags(GrColorType ct)1282 static inline uint32_t GrColorTypeComponentFlags(GrColorType ct) {
1283     switch (ct) {
1284         case GrColorType::kUnknown:          return 0;
1285         case GrColorType::kAlpha_8:          return kAlpha_SkColorTypeComponentFlag;
1286         case GrColorType::kRGB_565:          return kRGB_SkColorTypeComponentFlags;
1287         case GrColorType::kABGR_4444:        return kRGBA_SkColorTypeComponentFlags;
1288         case GrColorType::kRGBA_8888:        return kRGBA_SkColorTypeComponentFlags;
1289         case GrColorType::kRGB_888x:         return kRGB_SkColorTypeComponentFlags;
1290         case GrColorType::kRG_88:            return kRed_SkColorTypeComponentFlag |
1291                                                     kGreen_SkColorTypeComponentFlag;
1292         case GrColorType::kBGRA_8888:        return kRGBA_SkColorTypeComponentFlags;
1293         case GrColorType::kRGBA_1010102:     return kRGBA_SkColorTypeComponentFlags;
1294         case GrColorType::kGray_8:           return kGray_SkColorTypeComponentFlag;
1295         case GrColorType::kAlpha_F16:        return kAlpha_SkColorTypeComponentFlag;
1296         case GrColorType::kRGBA_F16:         return kRGBA_SkColorTypeComponentFlags;
1297         case GrColorType::kRGBA_F16_Clamped: return kRGBA_SkColorTypeComponentFlags;
1298         case GrColorType::kRG_F32:           return kRed_SkColorTypeComponentFlag |
1299                                                     kGreen_SkColorTypeComponentFlag;
1300         case GrColorType::kRGBA_F32:         return kRGBA_SkColorTypeComponentFlags;
1301         case GrColorType::kRGB_ETC1:         return kRGB_SkColorTypeComponentFlags;
1302     }
1303     SK_ABORT("Invalid GrColorType");
1304     return kUnknown_SkColorType;
1305 }
1306 
GrColorTypeIsAlphaOnly(GrColorType ct)1307 static inline bool GrColorTypeIsAlphaOnly(GrColorType ct) {
1308     return kAlpha_SkColorTypeComponentFlag == GrColorTypeComponentFlags(ct);
1309 }
1310 
GrColorTypeHasAlpha(GrColorType ct)1311 static inline bool GrColorTypeHasAlpha(GrColorType ct) {
1312     return kAlpha_SkColorTypeComponentFlag & GrColorTypeComponentFlags(ct);
1313 }
1314 
GrColorTypeBytesPerPixel(GrColorType ct)1315 static inline int GrColorTypeBytesPerPixel(GrColorType ct) {
1316     switch (ct) {
1317         case GrColorType::kUnknown:          return 0;
1318         case GrColorType::kRGB_ETC1:         return 0;
1319         case GrColorType::kAlpha_8:          return 1;
1320         case GrColorType::kRGB_565:          return 2;
1321         case GrColorType::kABGR_4444:        return 2;
1322         case GrColorType::kRGBA_8888:        return 4;
1323         case GrColorType::kRGB_888x:         return 4;
1324         case GrColorType::kRG_88:            return 2;
1325         case GrColorType::kBGRA_8888:        return 4;
1326         case GrColorType::kRGBA_1010102:     return 4;
1327         case GrColorType::kGray_8:           return 1;
1328         case GrColorType::kAlpha_F16:        return 2;
1329         case GrColorType::kRGBA_F16:         return 8;
1330         case GrColorType::kRGBA_F16_Clamped: return 8;
1331         case GrColorType::kRG_F32:           return 8;
1332         case GrColorType::kRGBA_F32:         return 16;
1333     }
1334     SK_ABORT("Invalid GrColorType");
1335     return 0;
1336 }
1337 
GrPixelConfigToColorTypeAndEncoding(GrPixelConfig config,GrSRGBEncoded * srgbEncoded)1338 static inline GrColorType GrPixelConfigToColorTypeAndEncoding(GrPixelConfig config,
1339                                                               GrSRGBEncoded* srgbEncoded) {
1340     SkASSERT(srgbEncoded);
1341     switch (config) {
1342         case kUnknown_GrPixelConfig:
1343             return GrColorType::kUnknown;
1344         case kAlpha_8_GrPixelConfig:
1345             *srgbEncoded = GrSRGBEncoded::kNo;
1346             return GrColorType::kAlpha_8;
1347         case kGray_8_GrPixelConfig:
1348             *srgbEncoded = GrSRGBEncoded::kNo;
1349             return GrColorType::kGray_8;
1350         case kRGB_565_GrPixelConfig:
1351             *srgbEncoded = GrSRGBEncoded::kNo;
1352             return GrColorType::kRGB_565;
1353         case kRGBA_4444_GrPixelConfig:
1354             *srgbEncoded = GrSRGBEncoded::kNo;
1355             return GrColorType::kABGR_4444;
1356         case kRGBA_8888_GrPixelConfig:
1357             *srgbEncoded = GrSRGBEncoded::kNo;
1358             return GrColorType::kRGBA_8888;
1359         case kRGB_888_GrPixelConfig:
1360             *srgbEncoded = GrSRGBEncoded::kNo;
1361             return GrColorType::kRGB_888x;
1362         case kRGB_888X_GrPixelConfig:
1363             *srgbEncoded = GrSRGBEncoded::kNo;
1364             return GrColorType::kRGB_888x;
1365         case kRG_88_GrPixelConfig:
1366             *srgbEncoded = GrSRGBEncoded::kNo;
1367             return GrColorType::kRG_88;
1368         case kBGRA_8888_GrPixelConfig:
1369             *srgbEncoded = GrSRGBEncoded::kNo;
1370             return GrColorType::kBGRA_8888;
1371         case kSRGBA_8888_GrPixelConfig:
1372             *srgbEncoded = GrSRGBEncoded::kYes;
1373             return GrColorType::kRGBA_8888;
1374         case kSBGRA_8888_GrPixelConfig:
1375             *srgbEncoded = GrSRGBEncoded::kYes;
1376             return GrColorType::kBGRA_8888;
1377         case kRGBA_1010102_GrPixelConfig:
1378             *srgbEncoded = GrSRGBEncoded::kNo;
1379             return GrColorType::kRGBA_1010102;
1380         case kRGBA_float_GrPixelConfig:
1381             *srgbEncoded = GrSRGBEncoded::kNo;
1382             return GrColorType::kRGBA_F32;
1383         case kRG_float_GrPixelConfig:
1384             *srgbEncoded = GrSRGBEncoded::kNo;
1385             return GrColorType::kRG_F32;
1386         case kAlpha_half_GrPixelConfig:
1387             *srgbEncoded = GrSRGBEncoded::kNo;
1388             return GrColorType::kAlpha_F16;
1389         case kRGBA_half_GrPixelConfig:
1390             *srgbEncoded = GrSRGBEncoded::kNo;
1391             return GrColorType::kRGBA_F16;
1392         case kRGBA_half_Clamped_GrPixelConfig:
1393             *srgbEncoded = GrSRGBEncoded::kNo;
1394             return GrColorType::kRGBA_F16_Clamped;
1395         case kRGB_ETC1_GrPixelConfig:
1396             *srgbEncoded = GrSRGBEncoded::kNo;
1397             return GrColorType::kRGB_ETC1;
1398         case kAlpha_8_as_Alpha_GrPixelConfig:
1399             *srgbEncoded = GrSRGBEncoded::kNo;
1400             return GrColorType::kAlpha_8;
1401         case kAlpha_8_as_Red_GrPixelConfig:
1402             *srgbEncoded = GrSRGBEncoded::kNo;
1403             return GrColorType::kAlpha_8;
1404         case kAlpha_half_as_Red_GrPixelConfig:
1405             *srgbEncoded = GrSRGBEncoded::kNo;
1406             return GrColorType::kAlpha_F16;
1407         case kGray_8_as_Lum_GrPixelConfig:
1408             *srgbEncoded = GrSRGBEncoded::kNo;
1409             return GrColorType::kGray_8;
1410         case kGray_8_as_Red_GrPixelConfig:
1411             *srgbEncoded = GrSRGBEncoded::kNo;
1412             return GrColorType::kGray_8;
1413     }
1414     SK_ABORT("Invalid GrPixelConfig");
1415     return GrColorType::kUnknown;
1416 }
1417 
GrPixelConfigToColorType(GrPixelConfig config)1418 static inline GrColorType GrPixelConfigToColorType(GrPixelConfig config) {
1419     GrSRGBEncoded bogusEncoded;
1420     return GrPixelConfigToColorTypeAndEncoding(config, &bogusEncoded);
1421 }
1422 
GrColorTypeToPixelConfig(GrColorType config,GrSRGBEncoded srgbEncoded)1423 static inline GrPixelConfig GrColorTypeToPixelConfig(GrColorType config,
1424                                                      GrSRGBEncoded srgbEncoded) {
1425     switch (config) {
1426         case GrColorType::kUnknown:
1427             return kUnknown_GrPixelConfig;
1428         case GrColorType::kAlpha_8:
1429             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1430                                                         : kAlpha_8_GrPixelConfig;
1431 
1432         case GrColorType::kGray_8:
1433             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1434                                                         : kGray_8_GrPixelConfig;
1435 
1436         case GrColorType::kRGB_565:
1437             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1438                                                         : kRGB_565_GrPixelConfig;
1439 
1440         case GrColorType::kABGR_4444:
1441             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1442                                                         : kRGBA_4444_GrPixelConfig;
1443 
1444         case GrColorType::kRGBA_8888:
1445             return (GrSRGBEncoded::kYes == srgbEncoded) ? kSRGBA_8888_GrPixelConfig
1446                                                         : kRGBA_8888_GrPixelConfig;
1447 
1448         case GrColorType::kRGB_888x:
1449             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1450                                                         : kRGB_888_GrPixelConfig;
1451         case GrColorType::kRG_88:
1452             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1453                                                         : kRG_88_GrPixelConfig;
1454 
1455         case GrColorType::kBGRA_8888:
1456             return (GrSRGBEncoded::kYes == srgbEncoded) ? kSBGRA_8888_GrPixelConfig
1457                                                         : kBGRA_8888_GrPixelConfig;
1458 
1459         case GrColorType::kRGBA_1010102:
1460             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1461                                                         : kRGBA_1010102_GrPixelConfig;
1462 
1463         case GrColorType::kRGBA_F32:
1464             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1465                                                         : kRGBA_float_GrPixelConfig;
1466 
1467         case GrColorType::kRG_F32:
1468             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1469                                                         : kRG_float_GrPixelConfig;
1470 
1471         case GrColorType::kAlpha_F16:
1472             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1473                                                         : kAlpha_half_GrPixelConfig;
1474 
1475         case GrColorType::kRGBA_F16:
1476             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1477                                                         : kRGBA_half_GrPixelConfig;
1478 
1479         case GrColorType::kRGBA_F16_Clamped:
1480             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1481                                                         : kRGBA_half_Clamped_GrPixelConfig;
1482 
1483         case GrColorType::kRGB_ETC1:
1484             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1485                                                         : kRGB_ETC1_GrPixelConfig;
1486     }
1487     SK_ABORT("Invalid GrColorType");
1488     return kUnknown_GrPixelConfig;
1489 }
1490 
1491 /**
1492  * Ref-counted object that calls a callback from its destructor. These can be chained together. Any
1493  * owner can cancel calling the callback via abandon().
1494  */
1495 class GrRefCntedCallback : public SkRefCnt {
1496 public:
1497     using Context = void*;
1498     using Callback = void (*)(Context);
1499 
GrRefCntedCallback(Callback proc,Context ctx)1500     GrRefCntedCallback(Callback proc, Context ctx) : fReleaseProc(proc), fReleaseCtx(ctx) {
1501         SkASSERT(proc);
1502     }
~GrRefCntedCallback()1503     ~GrRefCntedCallback() override { fReleaseProc ? fReleaseProc(fReleaseCtx) : void(); }
1504 
1505     /**
1506      * After abandon is called the release proc will no longer be called in the destructor. This
1507      * does not recurse on child release procs or unref them.
1508      */
abandon()1509     void abandon() {
1510         fReleaseProc = nullptr;
1511         fReleaseCtx = nullptr;
1512     }
1513 
1514     /** Adds another GrRefCntedCallback that this will unref in its destructor. */
addChild(sk_sp<GrRefCntedCallback> next)1515     void addChild(sk_sp<GrRefCntedCallback> next) {
1516         if (!fNext) {
1517             fNext = std::move(next);
1518             return;
1519         }
1520         fNext->addChild(std::move(next));
1521     }
1522 
context()1523     Context context() const { return fReleaseCtx; }
1524 
1525 private:
1526     sk_sp<GrRefCntedCallback> fNext;
1527     Callback fReleaseProc;
1528     Context fReleaseCtx;
1529 };
1530 
1531 #endif
1532