• 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     kGray_8_GrPixelConfig,
41     kRGB_565_GrPixelConfig,
42     kRGBA_4444_GrPixelConfig,
43     kRGBA_8888_GrPixelConfig,
44     kRGB_888_GrPixelConfig,
45     kRG_88_GrPixelConfig,
46     kBGRA_8888_GrPixelConfig,
47     kSRGBA_8888_GrPixelConfig,
48     kSBGRA_8888_GrPixelConfig,
49     kRGBA_1010102_GrPixelConfig,
50     kRGBA_float_GrPixelConfig,
51     kRG_float_GrPixelConfig,
52     kAlpha_half_GrPixelConfig,
53     kRGBA_half_GrPixelConfig,
54     kRGB_ETC1_GrPixelConfig,
55 
56     /** For internal usage. */
57     kPrivateConfig1_GrPixelConfig,
58     kPrivateConfig2_GrPixelConfig,
59     kPrivateConfig3_GrPixelConfig,
60     kPrivateConfig4_GrPixelConfig,
61     kPrivateConfig5_GrPixelConfig,
62 
63     kLast_GrPixelConfig = kPrivateConfig5_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;
245     size_t fRowBytes;
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 enum class GrQuadAAFlags {
324     kLeft   = SkCanvas::kLeft_QuadAAFlag,
325     kTop    = SkCanvas::kTop_QuadAAFlag,
326     kRight  = SkCanvas::kRight_QuadAAFlag,
327     kBottom = SkCanvas::kBottom_QuadAAFlag,
328 
329     kNone = SkCanvas::kNone_QuadAAFlags,
330     kAll  = SkCanvas::kAll_QuadAAFlags
331 };
332 
GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)333 GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)
334 
335 static inline GrQuadAAFlags SkToGrQuadAAFlags(unsigned flags) {
336     return static_cast<GrQuadAAFlags>(flags);
337 }
338 
339 /**
340  * Types of shader-language-specific boxed variables we can create.
341  */
342 enum GrSLType {
343     kVoid_GrSLType,
344     kBool_GrSLType,
345     kByte_GrSLType,
346     kByte2_GrSLType,
347     kByte3_GrSLType,
348     kByte4_GrSLType,
349     kUByte_GrSLType,
350     kUByte2_GrSLType,
351     kUByte3_GrSLType,
352     kUByte4_GrSLType,
353     kShort_GrSLType,
354     kShort2_GrSLType,
355     kShort3_GrSLType,
356     kShort4_GrSLType,
357     kUShort_GrSLType,
358     kUShort2_GrSLType,
359     kUShort3_GrSLType,
360     kUShort4_GrSLType,
361     kFloat_GrSLType,
362     kFloat2_GrSLType,
363     kFloat3_GrSLType,
364     kFloat4_GrSLType,
365     kFloat2x2_GrSLType,
366     kFloat3x3_GrSLType,
367     kFloat4x4_GrSLType,
368     kHalf_GrSLType,
369     kHalf2_GrSLType,
370     kHalf3_GrSLType,
371     kHalf4_GrSLType,
372     kHalf2x2_GrSLType,
373     kHalf3x3_GrSLType,
374     kHalf4x4_GrSLType,
375     kInt_GrSLType,
376     kInt2_GrSLType,
377     kInt3_GrSLType,
378     kInt4_GrSLType,
379     kUint_GrSLType,
380     kUint2_GrSLType,
381     kTexture2DSampler_GrSLType,
382     kTextureExternalSampler_GrSLType,
383     kTexture2DRectSampler_GrSLType,
384 
385     kLast_GrSLType = kTexture2DRectSampler_GrSLType
386 };
387 static const int kGrSLTypeCount = kLast_GrSLType + 1;
388 
389 /**
390  * The type of texture. Backends other than GL currently only use the 2D value but the type must
391  * still be known at the API-neutral layer as it used to determine whether MIP maps, renderability,
392  * and sampling parameters are legal for proxies that will be instantiated with wrapped textures.
393  */
394 enum class GrTextureType {
395     k2D,
396     /* Rectangle uses unnormalized texture coordinates. */
397     kRectangle,
398     kExternal
399 };
400 
401 enum GrShaderType {
402     kVertex_GrShaderType,
403     kGeometry_GrShaderType,
404     kFragment_GrShaderType,
405 
406     kLastkFragment_GrShaderType = kFragment_GrShaderType
407 };
408 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
409 
410 enum GrShaderFlags {
411     kNone_GrShaderFlags = 0,
412     kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
413     kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
414     kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
415 };
416 GR_MAKE_BITFIELD_OPS(GrShaderFlags);
417 
418 /**
419  * Precisions of shader language variables. Not all shading languages support precisions or actually
420  * vary the internal precision based on the qualifiers. These currently only apply to float types (
421  * including float vectors and matrices).
422  */
423 enum GrSLPrecision : int {
424     kLow_GrSLPrecision,
425     kMedium_GrSLPrecision,
426     kHigh_GrSLPrecision,
427 
428     // Default precision is a special tag that means "whatever the default for the program/type
429     // combination is". In other words, it maps to the empty string in shader code. There are some
430     // scenarios where kDefault is not allowed (as the default precision for a program, or for
431     // varyings, for example).
432     kDefault_GrSLPrecision,
433 
434     // We only consider the "real" precisions here
435     kLast_GrSLPrecision = kHigh_GrSLPrecision,
436 };
437 
438 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
439 
440 /** Is the shading language type float (including vectors/matrices)? */
GrSLTypeIsFloatType(GrSLType type)441 static inline bool GrSLTypeIsFloatType(GrSLType type) {
442     switch (type) {
443         case kFloat_GrSLType:
444         case kFloat2_GrSLType:
445         case kFloat3_GrSLType:
446         case kFloat4_GrSLType:
447         case kFloat2x2_GrSLType:
448         case kFloat3x3_GrSLType:
449         case kFloat4x4_GrSLType:
450         case kHalf_GrSLType:
451         case kHalf2_GrSLType:
452         case kHalf3_GrSLType:
453         case kHalf4_GrSLType:
454         case kHalf2x2_GrSLType:
455         case kHalf3x3_GrSLType:
456         case kHalf4x4_GrSLType:
457             return true;
458 
459         case kVoid_GrSLType:
460         case kTexture2DSampler_GrSLType:
461         case kTextureExternalSampler_GrSLType:
462         case kTexture2DRectSampler_GrSLType:
463         case kBool_GrSLType:
464         case kByte_GrSLType:
465         case kByte2_GrSLType:
466         case kByte3_GrSLType:
467         case kByte4_GrSLType:
468         case kUByte_GrSLType:
469         case kUByte2_GrSLType:
470         case kUByte3_GrSLType:
471         case kUByte4_GrSLType:
472         case kShort_GrSLType:
473         case kShort2_GrSLType:
474         case kShort3_GrSLType:
475         case kShort4_GrSLType:
476         case kUShort_GrSLType:
477         case kUShort2_GrSLType:
478         case kUShort3_GrSLType:
479         case kUShort4_GrSLType:
480         case kInt_GrSLType:
481         case kInt2_GrSLType:
482         case kInt3_GrSLType:
483         case kInt4_GrSLType:
484         case kUint_GrSLType:
485         case kUint2_GrSLType:
486             return false;
487     }
488     SK_ABORT("Unexpected type");
489     return false;
490 }
491 
492 /** If the type represents a single value or vector return the vector length, else -1. */
GrSLTypeVecLength(GrSLType type)493 static inline int GrSLTypeVecLength(GrSLType type) {
494     switch (type) {
495         case kFloat_GrSLType:
496         case kHalf_GrSLType:
497         case kBool_GrSLType:
498         case kByte_GrSLType:
499         case kUByte_GrSLType:
500         case kShort_GrSLType:
501         case kUShort_GrSLType:
502         case kInt_GrSLType:
503         case kUint_GrSLType:
504             return 1;
505 
506         case kFloat2_GrSLType:
507         case kHalf2_GrSLType:
508         case kByte2_GrSLType:
509         case kUByte2_GrSLType:
510         case kShort2_GrSLType:
511         case kUShort2_GrSLType:
512         case kInt2_GrSLType:
513         case kUint2_GrSLType:
514             return 2;
515 
516         case kFloat3_GrSLType:
517         case kHalf3_GrSLType:
518         case kByte3_GrSLType:
519         case kUByte3_GrSLType:
520         case kShort3_GrSLType:
521         case kUShort3_GrSLType:
522         case kInt3_GrSLType:
523             return 3;
524 
525         case kFloat4_GrSLType:
526         case kHalf4_GrSLType:
527         case kByte4_GrSLType:
528         case kUByte4_GrSLType:
529         case kShort4_GrSLType:
530         case kUShort4_GrSLType:
531         case kInt4_GrSLType:
532             return 4;
533 
534         case kFloat2x2_GrSLType:
535         case kFloat3x3_GrSLType:
536         case kFloat4x4_GrSLType:
537         case kHalf2x2_GrSLType:
538         case kHalf3x3_GrSLType:
539         case kHalf4x4_GrSLType:
540         case kVoid_GrSLType:
541         case kTexture2DSampler_GrSLType:
542         case kTextureExternalSampler_GrSLType:
543         case kTexture2DRectSampler_GrSLType:
544             return -1;
545     }
546     SK_ABORT("Unexpected type");
547     return -1;
548 }
549 
GrSLCombinedSamplerTypeForTextureType(GrTextureType type)550 static inline GrSLType GrSLCombinedSamplerTypeForTextureType(GrTextureType type) {
551     switch (type) {
552         case GrTextureType::k2D:
553             return kTexture2DSampler_GrSLType;
554         case GrTextureType::kRectangle:
555             return kTexture2DRectSampler_GrSLType;
556         case GrTextureType::kExternal:
557             return kTextureExternalSampler_GrSLType;
558     }
559     SK_ABORT("Unexpected texture type");
560     return kTexture2DSampler_GrSLType;
561 }
562 
563 /** Rectangle and external textures ony support the clamp wrap mode and do not support MIP maps. */
GrTextureTypeHasRestrictedSampling(GrTextureType type)564 static inline bool GrTextureTypeHasRestrictedSampling(GrTextureType type) {
565     switch (type) {
566         case GrTextureType::k2D:
567             return false;
568         case GrTextureType::kRectangle:
569             return true;
570         case GrTextureType::kExternal:
571             return true;
572     }
573     SK_ABORT("Unexpected texture type");
574     return false;
575 }
576 
GrSLTypeIsCombinedSamplerType(GrSLType type)577 static inline bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
578     switch (type) {
579         case kTexture2DSampler_GrSLType:
580         case kTextureExternalSampler_GrSLType:
581         case kTexture2DRectSampler_GrSLType:
582             return true;
583 
584         case kVoid_GrSLType:
585         case kFloat_GrSLType:
586         case kFloat2_GrSLType:
587         case kFloat3_GrSLType:
588         case kFloat4_GrSLType:
589         case kFloat2x2_GrSLType:
590         case kFloat3x3_GrSLType:
591         case kFloat4x4_GrSLType:
592         case kHalf_GrSLType:
593         case kHalf2_GrSLType:
594         case kHalf3_GrSLType:
595         case kHalf4_GrSLType:
596         case kHalf2x2_GrSLType:
597         case kHalf3x3_GrSLType:
598         case kHalf4x4_GrSLType:
599         case kInt_GrSLType:
600         case kInt2_GrSLType:
601         case kInt3_GrSLType:
602         case kInt4_GrSLType:
603         case kUint_GrSLType:
604         case kUint2_GrSLType:
605         case kBool_GrSLType:
606         case kByte_GrSLType:
607         case kByte2_GrSLType:
608         case kByte3_GrSLType:
609         case kByte4_GrSLType:
610         case kUByte_GrSLType:
611         case kUByte2_GrSLType:
612         case kUByte3_GrSLType:
613         case kUByte4_GrSLType:
614         case kShort_GrSLType:
615         case kShort2_GrSLType:
616         case kShort3_GrSLType:
617         case kShort4_GrSLType:
618         case kUShort_GrSLType:
619         case kUShort2_GrSLType:
620         case kUShort3_GrSLType:
621         case kUShort4_GrSLType:
622             return false;
623     }
624     SK_ABORT("Unexpected type");
625     return false;
626 }
627 
GrSLTypeAcceptsPrecision(GrSLType type)628 static inline bool GrSLTypeAcceptsPrecision(GrSLType type) {
629     switch (type) {
630         case kTexture2DSampler_GrSLType:
631         case kTextureExternalSampler_GrSLType:
632         case kTexture2DRectSampler_GrSLType:
633             return true;
634 
635         case kVoid_GrSLType:
636         case kBool_GrSLType:
637         case kByte_GrSLType:
638         case kByte2_GrSLType:
639         case kByte3_GrSLType:
640         case kByte4_GrSLType:
641         case kUByte_GrSLType:
642         case kUByte2_GrSLType:
643         case kUByte3_GrSLType:
644         case kUByte4_GrSLType:
645         case kShort_GrSLType:
646         case kShort2_GrSLType:
647         case kShort3_GrSLType:
648         case kShort4_GrSLType:
649         case kUShort_GrSLType:
650         case kUShort2_GrSLType:
651         case kUShort3_GrSLType:
652         case kUShort4_GrSLType:
653         case kFloat_GrSLType:
654         case kFloat2_GrSLType:
655         case kFloat3_GrSLType:
656         case kFloat4_GrSLType:
657         case kFloat2x2_GrSLType:
658         case kFloat3x3_GrSLType:
659         case kFloat4x4_GrSLType:
660         case kHalf_GrSLType:
661         case kHalf2_GrSLType:
662         case kHalf3_GrSLType:
663         case kHalf4_GrSLType:
664         case kHalf2x2_GrSLType:
665         case kHalf3x3_GrSLType:
666         case kHalf4x4_GrSLType:
667         case kInt_GrSLType:
668         case kInt2_GrSLType:
669         case kInt3_GrSLType:
670         case kInt4_GrSLType:
671         case kUint_GrSLType:
672         case kUint2_GrSLType:
673             return false;
674     }
675     SK_ABORT("Unexpected type");
676     return false;
677 }
678 
679 // temporarily accepting (but ignoring) precision modifiers on the new types; this will be killed
680 // in a future CL
GrSLTypeTemporarilyAcceptsPrecision(GrSLType type)681 static inline bool GrSLTypeTemporarilyAcceptsPrecision(GrSLType type) {
682     switch (type) {
683         case kShort_GrSLType:
684         case kUShort_GrSLType:
685         case kFloat_GrSLType:
686         case kFloat2_GrSLType:
687         case kFloat3_GrSLType:
688         case kFloat4_GrSLType:
689         case kFloat2x2_GrSLType:
690         case kFloat3x3_GrSLType:
691         case kFloat4x4_GrSLType:
692         case kHalf_GrSLType:
693         case kHalf2_GrSLType:
694         case kHalf3_GrSLType:
695         case kHalf4_GrSLType:
696         case kHalf2x2_GrSLType:
697         case kHalf3x3_GrSLType:
698         case kHalf4x4_GrSLType:
699         case kInt_GrSLType:
700         case kInt2_GrSLType:
701         case kInt3_GrSLType:
702         case kInt4_GrSLType:
703         case kUint_GrSLType:
704         case kUint2_GrSLType:
705         case kTexture2DSampler_GrSLType:
706         case kTextureExternalSampler_GrSLType:
707         case kTexture2DRectSampler_GrSLType:
708             return true;
709 
710         case kVoid_GrSLType:
711         case kBool_GrSLType:
712         case kByte_GrSLType:
713         case kByte2_GrSLType:
714         case kByte3_GrSLType:
715         case kByte4_GrSLType:
716         case kUByte_GrSLType:
717         case kUByte2_GrSLType:
718         case kUByte3_GrSLType:
719         case kUByte4_GrSLType:
720         case kShort2_GrSLType:
721         case kShort3_GrSLType:
722         case kShort4_GrSLType:
723         case kUShort2_GrSLType:
724         case kUShort3_GrSLType:
725         case kUShort4_GrSLType:
726             return false;
727     }
728     SK_ABORT("Unexpected type");
729     return false;
730 }
731 
732 //////////////////////////////////////////////////////////////////////////////
733 
734 /**
735  * Types used to describe format of vertices in arrays.
736  */
737 enum GrVertexAttribType {
738     kFloat_GrVertexAttribType = 0,
739     kFloat2_GrVertexAttribType,
740     kFloat3_GrVertexAttribType,
741     kFloat4_GrVertexAttribType,
742     kHalf_GrVertexAttribType,
743     kHalf2_GrVertexAttribType,
744     kHalf3_GrVertexAttribType,
745     kHalf4_GrVertexAttribType,
746 
747     kInt2_GrVertexAttribType,   // vector of 2 32-bit ints
748     kInt3_GrVertexAttribType,   // vector of 3 32-bit ints
749     kInt4_GrVertexAttribType,   // vector of 4 32-bit ints
750 
751 
752     kByte_GrVertexAttribType,  // signed byte
753     kByte2_GrVertexAttribType, // vector of 2 8-bit signed bytes
754     kByte3_GrVertexAttribType, // vector of 3 8-bit signed bytes
755     kByte4_GrVertexAttribType, // vector of 4 8-bit signed bytes
756     kUByte_GrVertexAttribType,  // unsigned byte
757     kUByte2_GrVertexAttribType, // vector of 2 8-bit unsigned bytes
758     kUByte3_GrVertexAttribType, // vector of 3 8-bit unsigned bytes
759     kUByte4_GrVertexAttribType, // vector of 4 8-bit unsigned bytes
760 
761     kUByte_norm_GrVertexAttribType,  // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
762     kUByte4_norm_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f,
763                                      // 255 -> 1.0f.
764 
765     kShort2_GrVertexAttribType,       // vector of 2 16-bit shorts.
766     kShort4_GrVertexAttribType,       // vector of 4 16-bit shorts.
767 
768     kUShort2_GrVertexAttribType,      // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
769     kUShort2_norm_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
770 
771     kInt_GrVertexAttribType,
772     kUint_GrVertexAttribType,
773 
774     kLast_GrVertexAttribType = kUint_GrVertexAttribType
775 };
776 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
777 
778 //////////////////////////////////////////////////////////////////////////////
779 
780 static const int kGrClipEdgeTypeCnt = (int) GrClipEdgeType::kLast + 1;
781 
GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType)782 static inline bool GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType) {
783     return (GrClipEdgeType::kFillAA == edgeType || GrClipEdgeType::kFillBW == edgeType);
784 }
785 
GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType)786 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType) {
787     return (GrClipEdgeType::kInverseFillAA == edgeType ||
788             GrClipEdgeType::kInverseFillBW == edgeType);
789 }
790 
GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType)791 static inline bool GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType) {
792     return (GrClipEdgeType::kFillBW != edgeType &&
793             GrClipEdgeType::kInverseFillBW != edgeType);
794 }
795 
GrInvertProcessorEdgeType(const GrClipEdgeType edgeType)796 static inline GrClipEdgeType GrInvertProcessorEdgeType(const GrClipEdgeType edgeType) {
797     switch (edgeType) {
798         case GrClipEdgeType::kFillBW:
799             return GrClipEdgeType::kInverseFillBW;
800         case GrClipEdgeType::kFillAA:
801             return GrClipEdgeType::kInverseFillAA;
802         case GrClipEdgeType::kInverseFillBW:
803             return GrClipEdgeType::kFillBW;
804         case GrClipEdgeType::kInverseFillAA:
805             return GrClipEdgeType::kFillAA;
806         case GrClipEdgeType::kHairlineAA:
807             SK_ABORT("Hairline fill isn't invertible.");
808     }
809     return GrClipEdgeType::kFillAA;  // suppress warning.
810 }
811 
812 /**
813  * Indicates the type of pending IO operations that can be recorded for gpu resources.
814  */
815 enum GrIOType {
816     kRead_GrIOType,
817     kWrite_GrIOType,
818     kRW_GrIOType
819 };
820 
821 /**
822  * Indicates the type of data that a GPU buffer will be used for.
823  */
824 enum GrBufferType {
825     kVertex_GrBufferType,
826     kIndex_GrBufferType,
827     kTexel_GrBufferType,
828     kDrawIndirect_GrBufferType,
829     kXferCpuToGpu_GrBufferType,
830     kXferGpuToCpu_GrBufferType,
831 
832     kLast_GrBufferType = kXferGpuToCpu_GrBufferType
833 };
834 static const int kGrBufferTypeCount = kLast_GrBufferType + 1;
835 
GrBufferTypeIsVertexOrIndex(GrBufferType type)836 static inline bool GrBufferTypeIsVertexOrIndex(GrBufferType type) {
837     SkASSERT(type >= 0 && type < kGrBufferTypeCount);
838     return type <= kIndex_GrBufferType;
839 
840     GR_STATIC_ASSERT(0 == kVertex_GrBufferType);
841     GR_STATIC_ASSERT(1 == kIndex_GrBufferType);
842 }
843 
844 /**
845  * Provides a performance hint regarding the frequency at which a data store will be accessed.
846  */
847 enum GrAccessPattern {
848     /** Data store will be respecified repeatedly and used many times. */
849     kDynamic_GrAccessPattern,
850     /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
851     kStatic_GrAccessPattern,
852     /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
853     kStream_GrAccessPattern,
854 
855     kLast_GrAccessPattern = kStream_GrAccessPattern
856 };
857 
858 // Flags shared between the GrSurface & GrSurfaceProxy class hierarchies
859 enum class GrInternalSurfaceFlags {
860     kNone                           = 0,
861 
862     // Surface-level
863 
864     kNoPendingIO                    = 1 << 0,
865 
866     kSurfaceMask                    = kNoPendingIO,
867 
868     // Texture-level
869 
870     // Means the pixels in the texture are read-only. Cannot also be a GrRenderTarget[Proxy].
871     kReadOnly                       = 1 << 1,
872 
873     kTextureMask                    = kReadOnly,
874 
875     // RT-level
876 
877     // For internal resources:
878     //    this is enabled whenever MSAA is enabled and GrCaps reports mixed samples are supported
879     // For wrapped resources:
880     //    this is disabled for FBO0
881     //    but, otherwise, is enabled whenever MSAA is enabled and GrCaps reports mixed samples
882     //        are supported
883     kMixedSampled                   = 1 << 2,
884 
885     // This flag is for use with GL only. It tells us that the internal render target wraps FBO 0.
886     kGLRTFBOIDIs0                   = 1 << 3,
887 
888     kRenderTargetMask               = kMixedSampled | kGLRTFBOIDIs0,
889 };
890 GR_MAKE_BITFIELD_CLASS_OPS(GrInternalSurfaceFlags)
891 
892 #ifdef SK_DEBUG
893 // Takes a pointer to a GrCaps, and will suppress prints if required
894 #define GrCapsDebugf(caps, ...)  if (!(caps)->suppressPrints()) SkDebugf(__VA_ARGS__)
895 #else
896 #define GrCapsDebugf(caps, ...) do {} while (0)
897 #endif
898 
899 /**
900  * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
901  */
902 enum class GrBackendObjectOwnership : bool {
903     /** Holder does not destroy the backend object. */
904     kBorrowed = false,
905     /** Holder destroys the backend object. */
906     kOwned = true
907 };
908 
909 template <typename T>
unique_ptr_address_as_pointer_address(std::unique_ptr<T> const * up)910 T* const* unique_ptr_address_as_pointer_address(std::unique_ptr<T> const* up) {
911     static_assert(sizeof(T*) == sizeof(std::unique_ptr<T>), "unique_ptr not expected size.");
912     return reinterpret_cast<T* const*>(up);
913 }
914 
915 /*
916  * Object for CPU-GPU synchronization
917  */
918 typedef uint64_t GrFence;
919 
920 /**
921  * Used to include or exclude specific GPU path renderers for testing purposes.
922  */
923 enum class GpuPathRenderers {
924     kNone              = 0, // Always use sofware masks and/or GrDefaultPathRenderer.
925     kDashLine          = 1 << 0,
926     kStencilAndCover   = 1 << 1,
927     kCoverageCounting  = 1 << 2,
928     kAAHairline        = 1 << 3,
929     kAAConvex          = 1 << 4,
930     kAALinearizing     = 1 << 5,
931     kSmall             = 1 << 6,
932     kTessellating      = 1 << 7,
933 
934     kAll               = (kTessellating | (kTessellating - 1))
935 };
936 
937 /**
938  * Used to describe the current state of Mips on a GrTexture
939  */
940 enum class  GrMipMapsStatus {
941     kNotAllocated, // Mips have not been allocated
942     kDirty,        // Mips are allocated but the full mip tree does not have valid data
943     kValid,        // All levels fully allocated and have valid data in them
944 };
945 
946 GR_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers)
947 
948 /**
949  * We want to extend the GrPixelConfig enum to add cases for dealing with alpha_8 which is
950  * internally either alpha8 or red8. Also for Gray_8 which can be luminance_8 or red_8.
951  */
952 static constexpr GrPixelConfig kAlpha_8_as_Alpha_GrPixelConfig = kPrivateConfig1_GrPixelConfig;
953 static constexpr GrPixelConfig kAlpha_8_as_Red_GrPixelConfig = kPrivateConfig2_GrPixelConfig;
954 static constexpr GrPixelConfig kAlpha_half_as_Red_GrPixelConfig = kPrivateConfig3_GrPixelConfig;
955 static constexpr GrPixelConfig kGray_8_as_Lum_GrPixelConfig = kPrivateConfig4_GrPixelConfig;
956 static constexpr GrPixelConfig kGray_8_as_Red_GrPixelConfig = kPrivateConfig5_GrPixelConfig;
957 
958 /**
959  * Refers to the encoding of a GPU buffer as it will be interpreted by the GPU when sampling and
960  * blending.
961  */
962 enum class GrSRGBEncoded : bool { kNo = false, kYes = true };
963 
964 /**
965  * Describes whether pixel data encoding should be converted to/from linear/sRGB encoding.
966  */
967 enum class GrSRGBConversion {
968     kNone,
969     kSRGBToLinear,
970     kLinearToSRGB,
971 };
972 
973 /**
974  * Utility functions for GrPixelConfig
975  */
976 
977 // Returns whether the config's color channels are sRGB encoded.
GrPixelConfigIsSRGBEncoded(GrPixelConfig config)978 static inline GrSRGBEncoded GrPixelConfigIsSRGBEncoded(GrPixelConfig config) {
979     switch (config) {
980         case kSRGBA_8888_GrPixelConfig:
981         case kSBGRA_8888_GrPixelConfig:
982             return GrSRGBEncoded::kYes;
983         case kUnknown_GrPixelConfig:
984         case kAlpha_8_GrPixelConfig:
985         case kAlpha_8_as_Alpha_GrPixelConfig:
986         case kAlpha_8_as_Red_GrPixelConfig:
987         case kGray_8_GrPixelConfig:
988         case kGray_8_as_Lum_GrPixelConfig:
989         case kGray_8_as_Red_GrPixelConfig:
990         case kRGB_565_GrPixelConfig:
991         case kRGBA_4444_GrPixelConfig:
992         case kRGB_888_GrPixelConfig:
993         case kRG_88_GrPixelConfig:
994         case kRGBA_8888_GrPixelConfig:
995         case kBGRA_8888_GrPixelConfig:
996         case kRGBA_1010102_GrPixelConfig:
997         case kRGBA_float_GrPixelConfig:
998         case kRG_float_GrPixelConfig:
999         case kAlpha_half_GrPixelConfig:
1000         case kAlpha_half_as_Red_GrPixelConfig:
1001         case kRGBA_half_GrPixelConfig:
1002         case kRGB_ETC1_GrPixelConfig:
1003             return GrSRGBEncoded::kNo;
1004     }
1005     SK_ABORT("Invalid pixel config");
1006     return GrSRGBEncoded::kNo;
1007 }
1008 
GrPixelConfigIsSRGB(GrPixelConfig config)1009 static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
1010     return GrSRGBEncoded::kYes == GrPixelConfigIsSRGBEncoded(config);
1011 }
1012 
GrBytesPerPixel(GrPixelConfig config)1013 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
1014     switch (config) {
1015         case kAlpha_8_GrPixelConfig:
1016         case kAlpha_8_as_Alpha_GrPixelConfig:
1017         case kAlpha_8_as_Red_GrPixelConfig:
1018         case kGray_8_GrPixelConfig:
1019         case kGray_8_as_Lum_GrPixelConfig:
1020         case kGray_8_as_Red_GrPixelConfig:
1021             return 1;
1022         case kRGB_565_GrPixelConfig:
1023         case kRGBA_4444_GrPixelConfig:
1024         case kRG_88_GrPixelConfig:
1025         case kAlpha_half_GrPixelConfig:
1026         case kAlpha_half_as_Red_GrPixelConfig:
1027             return 2;
1028         case kRGBA_8888_GrPixelConfig:
1029         case kRGB_888_GrPixelConfig:  // Assuming GPUs store this 4-byte aligned.
1030         case kBGRA_8888_GrPixelConfig:
1031         case kSRGBA_8888_GrPixelConfig:
1032         case kSBGRA_8888_GrPixelConfig:
1033         case kRGBA_1010102_GrPixelConfig:
1034             return 4;
1035         case kRGBA_half_GrPixelConfig:
1036             return 8;
1037         case kRGBA_float_GrPixelConfig:
1038             return 16;
1039         case kRG_float_GrPixelConfig:
1040             return 8;
1041         case kUnknown_GrPixelConfig:
1042         case kRGB_ETC1_GrPixelConfig:
1043             return 0;
1044     }
1045     SK_ABORT("Invalid pixel config");
1046     return 0;
1047 }
1048 
GrPixelConfigIsOpaque(GrPixelConfig config)1049 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
1050     switch (config) {
1051         case kRGB_565_GrPixelConfig:
1052         case kRGB_888_GrPixelConfig:
1053         case kRG_88_GrPixelConfig:
1054         case kGray_8_GrPixelConfig:
1055         case kGray_8_as_Lum_GrPixelConfig:
1056         case kGray_8_as_Red_GrPixelConfig:
1057         case kRG_float_GrPixelConfig:
1058         case kRGB_ETC1_GrPixelConfig:
1059             return true;
1060         case kAlpha_8_GrPixelConfig:
1061         case kAlpha_8_as_Alpha_GrPixelConfig:
1062         case kAlpha_8_as_Red_GrPixelConfig:
1063         case kRGBA_4444_GrPixelConfig:
1064         case kAlpha_half_GrPixelConfig:
1065         case kAlpha_half_as_Red_GrPixelConfig:
1066         case kRGBA_8888_GrPixelConfig:
1067         case kBGRA_8888_GrPixelConfig:
1068         case kSRGBA_8888_GrPixelConfig:
1069         case kSBGRA_8888_GrPixelConfig:
1070         case kRGBA_1010102_GrPixelConfig:
1071         case kRGBA_half_GrPixelConfig:
1072         case kRGBA_float_GrPixelConfig:
1073         case kUnknown_GrPixelConfig:
1074             return false;
1075     }
1076     SK_ABORT("Invalid pixel config");
1077     return false;
1078 }
1079 
GrPixelConfigIsAlphaOnly(GrPixelConfig config)1080 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
1081     switch (config) {
1082         case kAlpha_8_GrPixelConfig:
1083         case kAlpha_8_as_Alpha_GrPixelConfig:
1084         case kAlpha_8_as_Red_GrPixelConfig:
1085         case kAlpha_half_GrPixelConfig:
1086         case kAlpha_half_as_Red_GrPixelConfig:
1087             return true;
1088         case kUnknown_GrPixelConfig:
1089         case kGray_8_GrPixelConfig:
1090         case kGray_8_as_Lum_GrPixelConfig:
1091         case kGray_8_as_Red_GrPixelConfig:
1092         case kRGB_565_GrPixelConfig:
1093         case kRGBA_4444_GrPixelConfig:
1094         case kRGBA_8888_GrPixelConfig:
1095         case kRGB_888_GrPixelConfig:
1096         case kRG_88_GrPixelConfig:
1097         case kBGRA_8888_GrPixelConfig:
1098         case kSRGBA_8888_GrPixelConfig:
1099         case kSBGRA_8888_GrPixelConfig:
1100         case kRGBA_1010102_GrPixelConfig:
1101         case kRGBA_float_GrPixelConfig:
1102         case kRG_float_GrPixelConfig:
1103         case kRGBA_half_GrPixelConfig:
1104         case kRGB_ETC1_GrPixelConfig:
1105             return false;
1106     }
1107     SK_ABORT("Invalid pixel config.");
1108     return false;
1109 }
1110 
GrPixelConfigIsFloatingPoint(GrPixelConfig config)1111 static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
1112     switch (config) {
1113         case kUnknown_GrPixelConfig:
1114         case kAlpha_8_GrPixelConfig:
1115         case kAlpha_8_as_Alpha_GrPixelConfig:
1116         case kAlpha_8_as_Red_GrPixelConfig:
1117         case kGray_8_GrPixelConfig:
1118         case kGray_8_as_Lum_GrPixelConfig:
1119         case kGray_8_as_Red_GrPixelConfig:
1120         case kRGB_565_GrPixelConfig:
1121         case kRGBA_4444_GrPixelConfig:
1122         case kRGB_888_GrPixelConfig:
1123         case kRG_88_GrPixelConfig:
1124         case kRGBA_8888_GrPixelConfig:
1125         case kBGRA_8888_GrPixelConfig:
1126         case kSRGBA_8888_GrPixelConfig:
1127         case kSBGRA_8888_GrPixelConfig:
1128         case kRGBA_1010102_GrPixelConfig:
1129         case kRGB_ETC1_GrPixelConfig:
1130             return false;
1131         case kRGBA_float_GrPixelConfig:
1132         case kRG_float_GrPixelConfig:
1133         case kAlpha_half_GrPixelConfig:
1134         case kAlpha_half_as_Red_GrPixelConfig:
1135         case kRGBA_half_GrPixelConfig:
1136             return true;
1137     }
1138     SK_ABORT("Invalid pixel config.");
1139     return false;
1140 }
1141 
1142 /**
1143  * Returns true if the pixel config is a GPU-specific compressed format
1144  * representation.
1145  */
GrPixelConfigIsCompressed(GrPixelConfig config)1146 static inline bool GrPixelConfigIsCompressed(GrPixelConfig config) {
1147     switch (config) {
1148         case kRGB_ETC1_GrPixelConfig:
1149             return true;
1150         case kUnknown_GrPixelConfig:
1151         case kAlpha_8_GrPixelConfig:
1152         case kAlpha_8_as_Alpha_GrPixelConfig:
1153         case kAlpha_8_as_Red_GrPixelConfig:
1154         case kGray_8_GrPixelConfig:
1155         case kGray_8_as_Lum_GrPixelConfig:
1156         case kGray_8_as_Red_GrPixelConfig:
1157         case kRGB_565_GrPixelConfig:
1158         case kRGBA_4444_GrPixelConfig:
1159         case kRGB_888_GrPixelConfig:
1160         case kRG_88_GrPixelConfig:
1161         case kRGBA_8888_GrPixelConfig:
1162         case kBGRA_8888_GrPixelConfig:
1163         case kSRGBA_8888_GrPixelConfig:
1164         case kSBGRA_8888_GrPixelConfig:
1165         case kRGBA_1010102_GrPixelConfig:
1166         case kRGBA_float_GrPixelConfig:
1167         case kRG_float_GrPixelConfig:
1168         case kAlpha_half_GrPixelConfig:
1169         case kAlpha_half_as_Red_GrPixelConfig:
1170         case kRGBA_half_GrPixelConfig:
1171             return false;
1172     }
1173     SK_ABORT("Invalid pixel config");
1174     return false;
1175 }
1176 
1177 /**
1178  * If the pixel config is compressed, return an equivalent uncompressed format.
1179  */
GrMakePixelConfigUncompressed(GrPixelConfig config)1180 static inline GrPixelConfig GrMakePixelConfigUncompressed(GrPixelConfig config) {
1181     switch (config) {
1182         case kRGB_ETC1_GrPixelConfig:
1183             return kRGBA_8888_GrPixelConfig;
1184         case kUnknown_GrPixelConfig:
1185         case kAlpha_8_GrPixelConfig:
1186         case kAlpha_8_as_Alpha_GrPixelConfig:
1187         case kAlpha_8_as_Red_GrPixelConfig:
1188         case kGray_8_GrPixelConfig:
1189         case kGray_8_as_Lum_GrPixelConfig:
1190         case kGray_8_as_Red_GrPixelConfig:
1191         case kRGB_565_GrPixelConfig:
1192         case kRGBA_4444_GrPixelConfig:
1193         case kRGB_888_GrPixelConfig:
1194         case kRG_88_GrPixelConfig:
1195         case kRGBA_8888_GrPixelConfig:
1196         case kBGRA_8888_GrPixelConfig:
1197         case kSRGBA_8888_GrPixelConfig:
1198         case kSBGRA_8888_GrPixelConfig:
1199         case kRGBA_1010102_GrPixelConfig:
1200         case kRGBA_float_GrPixelConfig:
1201         case kRG_float_GrPixelConfig:
1202         case kAlpha_half_GrPixelConfig:
1203         case kAlpha_half_as_Red_GrPixelConfig:
1204         case kRGBA_half_GrPixelConfig:
1205             return config;
1206         }
1207     SK_ABORT("Invalid pixel config");
1208     return config;
1209 }
1210 
1211 /**
1212  * Returns the data size for the given compressed pixel config
1213  */
GrCompressedFormatDataSize(GrPixelConfig config,int width,int height)1214 static inline size_t GrCompressedFormatDataSize(GrPixelConfig config,
1215                                                 int width, int height) {
1216     SkASSERT(GrPixelConfigIsCompressed(config));
1217 
1218     switch (config) {
1219         case kRGB_ETC1_GrPixelConfig:
1220             SkASSERT((width & 3) == 0);
1221             SkASSERT((height & 3) == 0);
1222             return (width >> 2) * (height >> 2) * 8;
1223 
1224         case kUnknown_GrPixelConfig:
1225         case kAlpha_8_GrPixelConfig:
1226         case kAlpha_8_as_Alpha_GrPixelConfig:
1227         case kAlpha_8_as_Red_GrPixelConfig:
1228         case kGray_8_GrPixelConfig:
1229         case kGray_8_as_Lum_GrPixelConfig:
1230         case kGray_8_as_Red_GrPixelConfig:
1231         case kRGB_565_GrPixelConfig:
1232         case kRGBA_4444_GrPixelConfig:
1233         case kRGB_888_GrPixelConfig:
1234         case kRG_88_GrPixelConfig:
1235         case kRGBA_8888_GrPixelConfig:
1236         case kBGRA_8888_GrPixelConfig:
1237         case kSRGBA_8888_GrPixelConfig:
1238         case kSBGRA_8888_GrPixelConfig:
1239         case kRGBA_1010102_GrPixelConfig:
1240         case kRGBA_float_GrPixelConfig:
1241         case kRG_float_GrPixelConfig:
1242         case kAlpha_half_GrPixelConfig:
1243         case kAlpha_half_as_Red_GrPixelConfig:
1244         case kRGBA_half_GrPixelConfig:
1245             SK_ABORT("Unknown compressed pixel config");
1246             return 4 * width * height;
1247     }
1248 
1249     SK_ABORT("Invalid pixel config");
1250     return 4 * width * height;
1251 }
1252 
1253 /**
1254  * Precision qualifier that should be used with a sampler.
1255  */
GrSLSamplerPrecision(GrPixelConfig config)1256 static inline GrSLPrecision GrSLSamplerPrecision(GrPixelConfig config) {
1257     switch (config) {
1258         case kUnknown_GrPixelConfig:
1259         case kAlpha_8_GrPixelConfig:
1260         case kAlpha_8_as_Alpha_GrPixelConfig:
1261         case kAlpha_8_as_Red_GrPixelConfig:
1262         case kGray_8_GrPixelConfig:
1263         case kGray_8_as_Lum_GrPixelConfig:
1264         case kGray_8_as_Red_GrPixelConfig:
1265         case kRGB_565_GrPixelConfig:
1266         case kRGBA_4444_GrPixelConfig:
1267         case kRGBA_8888_GrPixelConfig:
1268         case kRGB_888_GrPixelConfig:
1269         case kRG_88_GrPixelConfig:
1270         case kBGRA_8888_GrPixelConfig:
1271         case kSRGBA_8888_GrPixelConfig:
1272         case kSBGRA_8888_GrPixelConfig:
1273         case kRGB_ETC1_GrPixelConfig:
1274             return kLow_GrSLPrecision;
1275         case kRGBA_float_GrPixelConfig:
1276         case kRG_float_GrPixelConfig:
1277             return kHigh_GrSLPrecision;
1278         case kAlpha_half_GrPixelConfig:
1279         case kAlpha_half_as_Red_GrPixelConfig:
1280         case kRGBA_half_GrPixelConfig:
1281         case kRGBA_1010102_GrPixelConfig:
1282             return kMedium_GrSLPrecision;
1283     }
1284     SK_ABORT("Unexpected type");
1285     return kHigh_GrSLPrecision;
1286 }
1287 
1288 /**
1289  * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels,
1290  * their type, and width. This exists so that the GPU backend can have private types that have no
1291  * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend.
1292  * It does not refer to a texture format and the mapping to texture formats may be many-to-many.
1293  * It does not specify the sRGB encoding of the stored values.
1294  */
1295 enum class GrColorType {
1296     kUnknown,
1297     kAlpha_8,
1298     kRGB_565,
1299     kABGR_4444,  // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed.
1300     kRGBA_8888,
1301     kRGB_888x,
1302     kRG_88,
1303     kBGRA_8888,
1304     kRGBA_1010102,
1305     kGray_8,
1306     kAlpha_F16,
1307     kRGBA_F16,
1308     kRG_F32,
1309     kRGBA_F32,
1310     kRGB_ETC1,   // This type doesn't appear in SkColorType at all.
1311 };
1312 
GrColorTypeToSkColorType(GrColorType ct)1313 static inline SkColorType GrColorTypeToSkColorType(GrColorType ct) {
1314     switch (ct) {
1315         case GrColorType::kUnknown:      return kUnknown_SkColorType;
1316         case GrColorType::kAlpha_8:      return kAlpha_8_SkColorType;
1317         case GrColorType::kRGB_565:      return kRGB_565_SkColorType;
1318         case GrColorType::kABGR_4444:    return kARGB_4444_SkColorType;
1319         case GrColorType::kRGBA_8888:    return kRGBA_8888_SkColorType;
1320         case GrColorType::kRGB_888x:     return kRGB_888x_SkColorType;
1321         case GrColorType::kRG_88:        return kUnknown_SkColorType;
1322         case GrColorType::kBGRA_8888:    return kBGRA_8888_SkColorType;
1323         case GrColorType::kRGBA_1010102: return kRGBA_1010102_SkColorType;
1324         case GrColorType::kGray_8:       return kGray_8_SkColorType;
1325         case GrColorType::kAlpha_F16:    return kUnknown_SkColorType;
1326         case GrColorType::kRGBA_F16:     return kRGBA_F16_SkColorType;
1327         case GrColorType::kRG_F32:       return kUnknown_SkColorType;
1328         case GrColorType::kRGBA_F32:     return kRGBA_F32_SkColorType;
1329         case GrColorType::kRGB_ETC1:     return kUnknown_SkColorType;
1330     }
1331     SK_ABORT("Invalid GrColorType");
1332     return kUnknown_SkColorType;
1333 }
1334 
SkColorTypeToGrColorType(SkColorType ct)1335 static inline GrColorType SkColorTypeToGrColorType(SkColorType ct) {
1336     switch (ct) {
1337         case kUnknown_SkColorType:      return GrColorType::kUnknown;
1338         case kAlpha_8_SkColorType:      return GrColorType::kAlpha_8;
1339         case kRGB_565_SkColorType:      return GrColorType::kRGB_565;
1340         case kARGB_4444_SkColorType:    return GrColorType::kABGR_4444;
1341         case kRGBA_8888_SkColorType:    return GrColorType::kRGBA_8888;
1342         case kRGB_888x_SkColorType:     return GrColorType::kRGB_888x;
1343         case kBGRA_8888_SkColorType:    return GrColorType::kBGRA_8888;
1344         case kGray_8_SkColorType:       return GrColorType::kGray_8;
1345         case kRGBA_F16_SkColorType:     return GrColorType::kRGBA_F16;
1346         case kRGBA_1010102_SkColorType: return GrColorType::kRGBA_1010102;
1347         case kRGB_101010x_SkColorType:  return GrColorType::kUnknown;
1348         case kRGBA_F32_SkColorType:     return GrColorType::kRGBA_F32;
1349     }
1350     SK_ABORT("Invalid SkColorType");
1351     return GrColorType::kUnknown;
1352 }
1353 
GrColorTypeComponentFlags(GrColorType ct)1354 static inline uint32_t GrColorTypeComponentFlags(GrColorType ct) {
1355     switch (ct) {
1356         case GrColorType::kUnknown:      return 0;
1357         case GrColorType::kAlpha_8:      return kAlpha_SkColorTypeComponentFlag;
1358         case GrColorType::kRGB_565:      return kRGB_SkColorTypeComponentFlags;
1359         case GrColorType::kABGR_4444:    return kRGBA_SkColorTypeComponentFlags;
1360         case GrColorType::kRGBA_8888:    return kRGBA_SkColorTypeComponentFlags;
1361         case GrColorType::kRGB_888x:     return kRGB_SkColorTypeComponentFlags;
1362         case GrColorType::kRG_88:        return kRed_SkColorTypeComponentFlag |
1363                                                 kGreen_SkColorTypeComponentFlag;
1364         case GrColorType::kBGRA_8888:    return kRGBA_SkColorTypeComponentFlags;
1365         case GrColorType::kRGBA_1010102: return kRGBA_SkColorTypeComponentFlags;
1366         case GrColorType::kGray_8:       return kGray_SkColorTypeComponentFlag;
1367         case GrColorType::kAlpha_F16:    return kAlpha_SkColorTypeComponentFlag;
1368         case GrColorType::kRGBA_F16:     return kRGBA_SkColorTypeComponentFlags;
1369         case GrColorType::kRG_F32:       return kRed_SkColorTypeComponentFlag |
1370                                                 kGreen_SkColorTypeComponentFlag;
1371         case GrColorType::kRGBA_F32:     return kRGBA_SkColorTypeComponentFlags;
1372         case GrColorType::kRGB_ETC1:     return kRGB_SkColorTypeComponentFlags;
1373     }
1374     SK_ABORT("Invalid GrColorType");
1375     return kUnknown_SkColorType;
1376 }
1377 
GrColorTypeIsAlphaOnly(GrColorType ct)1378 static inline bool GrColorTypeIsAlphaOnly(GrColorType ct) {
1379     return kAlpha_SkColorTypeComponentFlag == GrColorTypeComponentFlags(ct);
1380 }
1381 
GrColorTypeHasAlpha(GrColorType ct)1382 static inline bool GrColorTypeHasAlpha(GrColorType ct) {
1383     return kAlpha_SkColorTypeComponentFlag & GrColorTypeComponentFlags(ct);
1384 }
1385 
GrColorTypeBytesPerPixel(GrColorType ct)1386 static inline int GrColorTypeBytesPerPixel(GrColorType ct) {
1387     switch (ct) {
1388         case GrColorType::kUnknown:      return 0;
1389         case GrColorType::kRGB_ETC1:     return 0;
1390         case GrColorType::kAlpha_8:      return 1;
1391         case GrColorType::kRGB_565:      return 2;
1392         case GrColorType::kABGR_4444:    return 2;
1393         case GrColorType::kRGBA_8888:    return 4;
1394         case GrColorType::kRGB_888x:     return 4;
1395         case GrColorType::kRG_88:        return 2;
1396         case GrColorType::kBGRA_8888:    return 4;
1397         case GrColorType::kRGBA_1010102: return 4;
1398         case GrColorType::kGray_8:       return 1;
1399         case GrColorType::kAlpha_F16:    return 2;
1400         case GrColorType::kRGBA_F16:     return 8;
1401         case GrColorType::kRG_F32:       return 8;
1402         case GrColorType::kRGBA_F32:     return 16;
1403     }
1404     SK_ABORT("Invalid GrColorType");
1405     return 0;
1406 }
1407 
GrPixelConfigToColorTypeAndEncoding(GrPixelConfig config,GrSRGBEncoded * srgbEncoded)1408 static inline GrColorType GrPixelConfigToColorTypeAndEncoding(GrPixelConfig config,
1409                                                               GrSRGBEncoded* srgbEncoded) {
1410     SkASSERT(srgbEncoded);
1411     switch (config) {
1412         case kUnknown_GrPixelConfig:
1413             return GrColorType::kUnknown;
1414         case kAlpha_8_GrPixelConfig:
1415             *srgbEncoded = GrSRGBEncoded::kNo;
1416             return GrColorType::kAlpha_8;
1417         case kGray_8_GrPixelConfig:
1418             *srgbEncoded = GrSRGBEncoded::kNo;
1419             return GrColorType::kGray_8;
1420         case kRGB_565_GrPixelConfig:
1421             *srgbEncoded = GrSRGBEncoded::kNo;
1422             return GrColorType::kRGB_565;
1423         case kRGBA_4444_GrPixelConfig:
1424             *srgbEncoded = GrSRGBEncoded::kNo;
1425             return GrColorType::kABGR_4444;
1426         case kRGBA_8888_GrPixelConfig:
1427             *srgbEncoded = GrSRGBEncoded::kNo;
1428             return GrColorType::kRGBA_8888;
1429         case kRGB_888_GrPixelConfig:
1430             *srgbEncoded = GrSRGBEncoded::kNo;
1431             return GrColorType::kRGB_888x;
1432         case kRG_88_GrPixelConfig:
1433             *srgbEncoded = GrSRGBEncoded::kNo;
1434             return GrColorType::kRG_88;
1435         case kBGRA_8888_GrPixelConfig:
1436             *srgbEncoded = GrSRGBEncoded::kNo;
1437             return GrColorType::kBGRA_8888;
1438         case kSRGBA_8888_GrPixelConfig:
1439             *srgbEncoded = GrSRGBEncoded::kYes;
1440             return GrColorType::kRGBA_8888;
1441         case kSBGRA_8888_GrPixelConfig:
1442             *srgbEncoded = GrSRGBEncoded::kYes;
1443             return GrColorType::kBGRA_8888;
1444         case kRGBA_1010102_GrPixelConfig:
1445             *srgbEncoded = GrSRGBEncoded::kNo;
1446             return GrColorType::kRGBA_1010102;
1447         case kRGBA_float_GrPixelConfig:
1448             *srgbEncoded = GrSRGBEncoded::kNo;
1449             return GrColorType::kRGBA_F32;
1450         case kRG_float_GrPixelConfig:
1451             *srgbEncoded = GrSRGBEncoded::kNo;
1452             return GrColorType::kRG_F32;
1453         case kAlpha_half_GrPixelConfig:
1454             *srgbEncoded = GrSRGBEncoded::kNo;
1455             return GrColorType::kAlpha_F16;
1456         case kRGBA_half_GrPixelConfig:
1457             *srgbEncoded = GrSRGBEncoded::kNo;
1458             return GrColorType::kRGBA_F16;
1459         case kRGB_ETC1_GrPixelConfig:
1460             *srgbEncoded = GrSRGBEncoded::kNo;
1461             return GrColorType::kRGB_ETC1;
1462         case kAlpha_8_as_Alpha_GrPixelConfig:
1463             *srgbEncoded = GrSRGBEncoded::kNo;
1464             return GrColorType::kAlpha_8;
1465         case kAlpha_8_as_Red_GrPixelConfig:
1466             *srgbEncoded = GrSRGBEncoded::kNo;
1467             return GrColorType::kAlpha_8;
1468         case kAlpha_half_as_Red_GrPixelConfig:
1469             *srgbEncoded = GrSRGBEncoded::kNo;
1470             return GrColorType::kAlpha_F16;
1471         case kGray_8_as_Lum_GrPixelConfig:
1472             *srgbEncoded = GrSRGBEncoded::kNo;
1473             return GrColorType::kGray_8;
1474         case kGray_8_as_Red_GrPixelConfig:
1475             *srgbEncoded = GrSRGBEncoded::kNo;
1476             return GrColorType::kGray_8;
1477     }
1478     SK_ABORT("Invalid GrPixelConfig");
1479     return GrColorType::kUnknown;
1480 }
1481 
GrPixelConfigToColorType(GrPixelConfig config)1482 static inline GrColorType GrPixelConfigToColorType(GrPixelConfig config) {
1483     GrSRGBEncoded bogusEncoded;
1484     return GrPixelConfigToColorTypeAndEncoding(config, &bogusEncoded);
1485 }
1486 
GrColorTypeToPixelConfig(GrColorType config,GrSRGBEncoded srgbEncoded)1487 static inline GrPixelConfig GrColorTypeToPixelConfig(GrColorType config,
1488                                                      GrSRGBEncoded srgbEncoded) {
1489     switch (config) {
1490         case GrColorType::kUnknown:
1491             return kUnknown_GrPixelConfig;
1492         case GrColorType::kAlpha_8:
1493             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1494                                                         : kAlpha_8_GrPixelConfig;
1495 
1496         case GrColorType::kGray_8:
1497             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1498                                                         : kGray_8_GrPixelConfig;
1499 
1500         case GrColorType::kRGB_565:
1501             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1502                                                         : kRGB_565_GrPixelConfig;
1503 
1504         case GrColorType::kABGR_4444:
1505             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1506                                                         : kRGBA_4444_GrPixelConfig;
1507 
1508         case GrColorType::kRGBA_8888:
1509             return (GrSRGBEncoded::kYes == srgbEncoded) ? kSRGBA_8888_GrPixelConfig
1510                                                         : kRGBA_8888_GrPixelConfig;
1511 
1512         case GrColorType::kRGB_888x:
1513             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1514                                                         : kRGB_888_GrPixelConfig;
1515         case GrColorType::kRG_88:
1516             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1517                                                         : kRG_88_GrPixelConfig;
1518 
1519         case GrColorType::kBGRA_8888:
1520             return (GrSRGBEncoded::kYes == srgbEncoded) ? kSBGRA_8888_GrPixelConfig
1521                                                         : kBGRA_8888_GrPixelConfig;
1522 
1523         case GrColorType::kRGBA_1010102:
1524             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1525                                                         : kRGBA_1010102_GrPixelConfig;
1526 
1527         case GrColorType::kRGBA_F32:
1528             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1529                                                         : kRGBA_float_GrPixelConfig;
1530 
1531         case GrColorType::kRG_F32:
1532             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1533                                                         : kRG_float_GrPixelConfig;
1534 
1535         case GrColorType::kAlpha_F16:
1536             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1537                                                         : kAlpha_half_GrPixelConfig;
1538 
1539         case GrColorType::kRGBA_F16:
1540             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1541                                                         : kRGBA_half_GrPixelConfig;
1542 
1543         case GrColorType::kRGB_ETC1:
1544             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1545                                                         : kRGB_ETC1_GrPixelConfig;
1546     }
1547     SK_ABORT("Invalid GrColorType");
1548     return kUnknown_GrPixelConfig;
1549 }
1550 
1551 class GrReleaseProcHelper : public SkRefCnt {
1552 public:
1553     // These match the definitions in SkImage, from whence they came
1554     typedef void* ReleaseCtx;
1555     typedef void (*ReleaseProc)(ReleaseCtx);
1556 
GrReleaseProcHelper(ReleaseProc proc,ReleaseCtx ctx)1557     GrReleaseProcHelper(ReleaseProc proc, ReleaseCtx ctx) : fReleaseProc(proc), fReleaseCtx(ctx) {
1558         SkASSERT(proc);
1559     }
~GrReleaseProcHelper()1560     ~GrReleaseProcHelper() override { fReleaseProc(fReleaseCtx); }
1561 
1562 private:
1563     ReleaseProc fReleaseProc;
1564     ReleaseCtx  fReleaseCtx;
1565 };
1566 
1567 #endif
1568