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 "SkImageInfo.h"
15 #include "SkRefCnt.h"
16 
17 class GrCaps;
18 
19 #ifdef MOZ_SKIA
20 #include "mozilla/TimeStamp.h"
21 
22 struct GrStdSteadyClock
23 {
24     typedef mozilla::TimeStamp time_point;
25 
nowGrStdSteadyClock26     static time_point now() {
27         return mozilla::TimeStamp::NowLoRes();
28     }
29 };
30 
31 static inline GrStdSteadyClock::time_point
32 operator-(GrStdSteadyClock::time_point t, std::chrono::milliseconds ms) {
33     return t - mozilla::TimeDuration::FromMilliseconds(ms.count());
34 }
35 
36 #else
37 
38 // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might
39 // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently
40 // used for idle resource purging so it shouldn't cause a correctness problem.
41 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
42 using GrStdSteadyClock = std::chrono::monotonic_clock;
43 #else
44 using GrStdSteadyClock = std::chrono::steady_clock;
45 #endif
46 
47 #endif
48 
49 /** This enum is used to specify the load operation to be used when an
50  *  opList/GrGpuCommandBuffer begins execution.
51  */
52 enum class GrLoadOp {
53     kLoad,
54     kClear,
55     kDiscard,
56 };
57 
58 /** This enum is used to specify the store operation to be used when an
59  *  opList/GrGpuCommandBuffer ends execution.
60  */
61 enum class GrStoreOp {
62     kStore,
63     kDiscard,
64 };
65 
66 /** This enum indicates the type of antialiasing to be performed. */
67 enum class GrAAType : unsigned {
68     /** No antialiasing */
69     kNone,
70     /** Use fragment shader code to compute a fractional pixel coverage. */
71     kCoverage,
72     /** Use normal MSAA. */
73     kMSAA,
74     /**
75      * Use "mixed samples" MSAA such that the stencil buffer is multisampled but the color buffer is
76      * not.
77      */
78     kMixedSamples
79 };
80 
GrAATypeIsHW(GrAAType type)81 static inline bool GrAATypeIsHW(GrAAType type) {
82     switch (type) {
83         case GrAAType::kNone:
84             return false;
85         case GrAAType::kCoverage:
86             return false;
87         case GrAAType::kMSAA:
88             return true;
89         case GrAAType::kMixedSamples:
90             return true;
91     }
92     SK_ABORT("Unknown AA Type");
93     return false;
94 }
95 
96 /** The type of full scene antialiasing supported by a render target. */
97 enum class GrFSAAType {
98     /** No FSAA */
99     kNone,
100     /** Regular MSAA where each attachment has the same sample count. */
101     kUnifiedMSAA,
102     /** One color sample, N stencil samples. */
103     kMixedSamples,
104 };
105 
106 /**
107  * Not all drawing code paths support using mixed samples when available and instead use
108  * coverage-based aa.
109  */
110 enum class GrAllowMixedSamples : bool { kNo = false, kYes = true };
111 
112 GrAAType GrChooseAAType(GrAA, GrFSAAType, GrAllowMixedSamples, const GrCaps&);
113 
114 /**
115  * Some pixel configs are inherently clamped to [0,1], while others can hold values outside of that
116  * range. This is important for blending - the latter category may require manual clamping.
117  */
118 enum class GrPixelConfigIsClamped : bool {
119     kNo = false,   // F16 or F32
120     kYes = true,  // Any UNORM type
121 };
122 
123 /**
124  * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
125  * but should be applicable to other shader languages.)
126  */
127 enum GrSLType {
128     kVoid_GrSLType,
129     kBool_GrSLType,
130     kShort_GrSLType,
131     kShort2_GrSLType,
132     kShort3_GrSLType,
133     kShort4_GrSLType,
134     kUShort_GrSLType,
135     kUShort2_GrSLType,
136     kUShort3_GrSLType,
137     kUShort4_GrSLType,
138     kFloat_GrSLType,
139     kFloat2_GrSLType,
140     kFloat3_GrSLType,
141     kFloat4_GrSLType,
142     kFloat2x2_GrSLType,
143     kFloat3x3_GrSLType,
144     kFloat4x4_GrSLType,
145     kHalf_GrSLType,
146     kHalf2_GrSLType,
147     kHalf3_GrSLType,
148     kHalf4_GrSLType,
149     kHalf2x2_GrSLType,
150     kHalf3x3_GrSLType,
151     kHalf4x4_GrSLType,
152     kInt_GrSLType,
153     kInt2_GrSLType,
154     kInt3_GrSLType,
155     kInt4_GrSLType,
156     kUint_GrSLType,
157     kUint2_GrSLType,
158     kTexture2DSampler_GrSLType,
159     kTextureExternalSampler_GrSLType,
160     kTexture2DRectSampler_GrSLType,
161     kBufferSampler_GrSLType,
162     kTexture2D_GrSLType,
163     kSampler_GrSLType,
164 };
165 
166 enum GrShaderType {
167     kVertex_GrShaderType,
168     kGeometry_GrShaderType,
169     kFragment_GrShaderType,
170 
171     kLastkFragment_GrShaderType = kFragment_GrShaderType
172 };
173 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
174 
175 enum GrShaderFlags {
176     kNone_GrShaderFlags = 0,
177     kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
178     kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
179     kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
180 };
181 GR_MAKE_BITFIELD_OPS(GrShaderFlags);
182 
183 /**
184  * Precisions of shader language variables. Not all shading languages support precisions or actually
185  * vary the internal precision based on the qualifiers. These currently only apply to float types (
186  * including float vectors and matrices).
187  */
188 enum GrSLPrecision {
189     kLow_GrSLPrecision,
190     kMedium_GrSLPrecision,
191     kHigh_GrSLPrecision,
192 
193     // Default precision is a special tag that means "whatever the default for the program/type
194     // combination is". In other words, it maps to the empty string in shader code. There are some
195     // scenarios where kDefault is not allowed (as the default precision for a program, or for
196     // varyings, for example).
197     kDefault_GrSLPrecision,
198 
199     // We only consider the "real" precisions here
200     kLast_GrSLPrecision = kHigh_GrSLPrecision,
201 };
202 
203 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
204 
205 /** Is the shading language type float (including vectors/matrices)? */
GrSLTypeIsFloatType(GrSLType type)206 static inline bool GrSLTypeIsFloatType(GrSLType type) {
207     switch (type) {
208         case kFloat_GrSLType:
209         case kFloat2_GrSLType:
210         case kFloat3_GrSLType:
211         case kFloat4_GrSLType:
212         case kFloat2x2_GrSLType:
213         case kFloat3x3_GrSLType:
214         case kFloat4x4_GrSLType:
215         case kHalf_GrSLType:
216         case kHalf2_GrSLType:
217         case kHalf3_GrSLType:
218         case kHalf4_GrSLType:
219         case kHalf2x2_GrSLType:
220         case kHalf3x3_GrSLType:
221         case kHalf4x4_GrSLType:
222             return true;
223 
224         case kVoid_GrSLType:
225         case kTexture2DSampler_GrSLType:
226         case kTextureExternalSampler_GrSLType:
227         case kTexture2DRectSampler_GrSLType:
228         case kBufferSampler_GrSLType:
229         case kBool_GrSLType:
230         case kShort_GrSLType:
231         case kShort2_GrSLType:
232         case kShort3_GrSLType:
233         case kShort4_GrSLType:
234         case kUShort_GrSLType:
235         case kUShort2_GrSLType:
236         case kUShort3_GrSLType:
237         case kUShort4_GrSLType:
238         case kInt_GrSLType:
239         case kInt2_GrSLType:
240         case kInt3_GrSLType:
241         case kInt4_GrSLType:
242         case kUint_GrSLType:
243         case kUint2_GrSLType:
244         case kTexture2D_GrSLType:
245         case kSampler_GrSLType:
246             return false;
247     }
248     SK_ABORT("Unexpected type");
249     return false;
250 }
251 
252 /** If the type represents a single value or vector return the vector length, else -1. */
GrSLTypeVecLength(GrSLType type)253 static inline int GrSLTypeVecLength(GrSLType type) {
254     switch (type) {
255         case kFloat_GrSLType:
256         case kHalf_GrSLType:
257         case kBool_GrSLType:
258         case kShort_GrSLType:
259         case kUShort_GrSLType:
260         case kInt_GrSLType:
261         case kUint_GrSLType:
262             return 1;
263 
264         case kFloat2_GrSLType:
265         case kHalf2_GrSLType:
266         case kShort2_GrSLType:
267         case kUShort2_GrSLType:
268         case kInt2_GrSLType:
269         case kUint2_GrSLType:
270             return 2;
271 
272         case kFloat3_GrSLType:
273         case kHalf3_GrSLType:
274         case kShort3_GrSLType:
275         case kUShort3_GrSLType:
276         case kInt3_GrSLType:
277             return 3;
278 
279         case kFloat4_GrSLType:
280         case kHalf4_GrSLType:
281         case kShort4_GrSLType:
282         case kUShort4_GrSLType:
283         case kInt4_GrSLType:
284             return 4;
285 
286         case kFloat2x2_GrSLType:
287         case kFloat3x3_GrSLType:
288         case kFloat4x4_GrSLType:
289         case kHalf2x2_GrSLType:
290         case kHalf3x3_GrSLType:
291         case kHalf4x4_GrSLType:
292         case kVoid_GrSLType:
293         case kTexture2DSampler_GrSLType:
294         case kTextureExternalSampler_GrSLType:
295         case kTexture2DRectSampler_GrSLType:
296         case kBufferSampler_GrSLType:
297         case kTexture2D_GrSLType:
298         case kSampler_GrSLType:
299             return -1;
300     }
301     SK_ABORT("Unexpected type");
302     return -1;
303 }
304 
GrSLTypeIs2DCombinedSamplerType(GrSLType type)305 static inline bool GrSLTypeIs2DCombinedSamplerType(GrSLType type) {
306     switch (type) {
307         case kTexture2DSampler_GrSLType:
308         case kTextureExternalSampler_GrSLType:
309         case kTexture2DRectSampler_GrSLType:
310             return true;
311 
312         case kVoid_GrSLType:
313         case kFloat_GrSLType:
314         case kFloat2_GrSLType:
315         case kFloat3_GrSLType:
316         case kFloat4_GrSLType:
317         case kFloat2x2_GrSLType:
318         case kFloat3x3_GrSLType:
319         case kFloat4x4_GrSLType:
320         case kHalf_GrSLType:
321         case kHalf2_GrSLType:
322         case kHalf3_GrSLType:
323         case kHalf4_GrSLType:
324         case kHalf2x2_GrSLType:
325         case kHalf3x3_GrSLType:
326         case kHalf4x4_GrSLType:
327         case kInt_GrSLType:
328         case kInt2_GrSLType:
329         case kInt3_GrSLType:
330         case kInt4_GrSLType:
331         case kUint_GrSLType:
332         case kUint2_GrSLType:
333         case kBufferSampler_GrSLType:
334         case kBool_GrSLType:
335         case kShort_GrSLType:
336         case kShort2_GrSLType:
337         case kShort3_GrSLType:
338         case kShort4_GrSLType:
339         case kUShort_GrSLType:
340         case kUShort2_GrSLType:
341         case kUShort3_GrSLType:
342         case kUShort4_GrSLType:
343         case kTexture2D_GrSLType:
344         case kSampler_GrSLType:
345             return false;
346     }
347     SK_ABORT("Unexpected type");
348     return false;
349 }
350 
GrSLTypeIsCombinedSamplerType(GrSLType type)351 static inline bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
352     switch (type) {
353         case kTexture2DSampler_GrSLType:
354         case kTextureExternalSampler_GrSLType:
355         case kTexture2DRectSampler_GrSLType:
356         case kBufferSampler_GrSLType:
357             return true;
358 
359         case kVoid_GrSLType:
360         case kFloat_GrSLType:
361         case kFloat2_GrSLType:
362         case kFloat3_GrSLType:
363         case kFloat4_GrSLType:
364         case kFloat2x2_GrSLType:
365         case kFloat3x3_GrSLType:
366         case kFloat4x4_GrSLType:
367         case kHalf_GrSLType:
368         case kHalf2_GrSLType:
369         case kHalf3_GrSLType:
370         case kHalf4_GrSLType:
371         case kHalf2x2_GrSLType:
372         case kHalf3x3_GrSLType:
373         case kHalf4x4_GrSLType:
374         case kInt_GrSLType:
375         case kInt2_GrSLType:
376         case kInt3_GrSLType:
377         case kInt4_GrSLType:
378         case kUint_GrSLType:
379         case kUint2_GrSLType:
380         case kBool_GrSLType:
381         case kShort_GrSLType:
382         case kShort2_GrSLType:
383         case kShort3_GrSLType:
384         case kShort4_GrSLType:
385         case kUShort_GrSLType:
386         case kUShort2_GrSLType:
387         case kUShort3_GrSLType:
388         case kUShort4_GrSLType:
389         case kTexture2D_GrSLType:
390         case kSampler_GrSLType:
391             return false;
392     }
393     SK_ABORT("Unexpected type");
394     return false;
395 }
396 
GrSLTypeAcceptsPrecision(GrSLType type)397 static inline bool GrSLTypeAcceptsPrecision(GrSLType type) {
398     switch (type) {
399         case kTexture2DSampler_GrSLType:
400         case kTextureExternalSampler_GrSLType:
401         case kTexture2DRectSampler_GrSLType:
402         case kBufferSampler_GrSLType:
403         case kTexture2D_GrSLType:
404         case kSampler_GrSLType:
405             return true;
406 
407         case kVoid_GrSLType:
408         case kBool_GrSLType:
409         case kShort_GrSLType:
410         case kShort2_GrSLType:
411         case kShort3_GrSLType:
412         case kShort4_GrSLType:
413         case kUShort_GrSLType:
414         case kUShort2_GrSLType:
415         case kUShort3_GrSLType:
416         case kUShort4_GrSLType:
417         case kFloat_GrSLType:
418         case kFloat2_GrSLType:
419         case kFloat3_GrSLType:
420         case kFloat4_GrSLType:
421         case kFloat2x2_GrSLType:
422         case kFloat3x3_GrSLType:
423         case kFloat4x4_GrSLType:
424         case kHalf_GrSLType:
425         case kHalf2_GrSLType:
426         case kHalf3_GrSLType:
427         case kHalf4_GrSLType:
428         case kHalf2x2_GrSLType:
429         case kHalf3x3_GrSLType:
430         case kHalf4x4_GrSLType:
431         case kInt_GrSLType:
432         case kInt2_GrSLType:
433         case kInt3_GrSLType:
434         case kInt4_GrSLType:
435         case kUint_GrSLType:
436         case kUint2_GrSLType:
437             return false;
438     }
439     SK_ABORT("Unexpected type");
440     return false;
441 }
442 
443 // temporarily accepting (but ignoring) precision modifiers on the new types; this will be killed
444 // in a future CL
GrSLTypeTemporarilyAcceptsPrecision(GrSLType type)445 static inline bool GrSLTypeTemporarilyAcceptsPrecision(GrSLType type) {
446     switch (type) {
447         case kShort_GrSLType:
448         case kUShort_GrSLType:
449         case kFloat_GrSLType:
450         case kFloat2_GrSLType:
451         case kFloat3_GrSLType:
452         case kFloat4_GrSLType:
453         case kFloat2x2_GrSLType:
454         case kFloat3x3_GrSLType:
455         case kFloat4x4_GrSLType:
456         case kHalf_GrSLType:
457         case kHalf2_GrSLType:
458         case kHalf3_GrSLType:
459         case kHalf4_GrSLType:
460         case kHalf2x2_GrSLType:
461         case kHalf3x3_GrSLType:
462         case kHalf4x4_GrSLType:
463         case kInt_GrSLType:
464         case kInt2_GrSLType:
465         case kInt3_GrSLType:
466         case kInt4_GrSLType:
467         case kUint_GrSLType:
468         case kUint2_GrSLType:
469         case kTexture2DSampler_GrSLType:
470         case kTextureExternalSampler_GrSLType:
471         case kTexture2DRectSampler_GrSLType:
472         case kBufferSampler_GrSLType:
473         case kTexture2D_GrSLType:
474         case kSampler_GrSLType:
475             return true;
476 
477         case kVoid_GrSLType:
478         case kBool_GrSLType:
479         case kShort2_GrSLType:
480         case kShort3_GrSLType:
481         case kShort4_GrSLType:
482         case kUShort2_GrSLType:
483         case kUShort3_GrSLType:
484         case kUShort4_GrSLType:
485             return false;
486     }
487     SK_ABORT("Unexpected type");
488     return false;
489 }
490 
491 //////////////////////////////////////////////////////////////////////////////
492 
493 /**
494  * Types used to describe format of vertices in arrays.
495  */
496 enum GrVertexAttribType {
497     kFloat_GrVertexAttribType = 0,
498     kFloat2_GrVertexAttribType,
499     kFloat3_GrVertexAttribType,
500     kFloat4_GrVertexAttribType,
501     kHalf_GrVertexAttribType,
502     kHalf2_GrVertexAttribType,
503     kHalf3_GrVertexAttribType,
504     kHalf4_GrVertexAttribType,
505 
506     kInt2_GrVertexAttribType,   // vector of 2 32-bit ints
507     kInt3_GrVertexAttribType,   // vector of 3 32-bit ints
508     kInt4_GrVertexAttribType,   // vector of 4 32-bit ints
509 
510     kUByte_norm_GrVertexAttribType,  // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
511     kUByte4_norm_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f,
512                                      // 255 -> 1.0f.
513 
514     kShort2_GrVertexAttribType,       // vector of 2 16-bit shorts.
515     kUShort2_GrVertexAttribType,      // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
516     kUShort2_norm_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
517 
518     kInt_GrVertexAttribType,
519     kUint_GrVertexAttribType,
520 
521     kLast_GrVertexAttribType = kUint_GrVertexAttribType
522 };
523 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
524 
525 /**
526  * Returns the size of the attrib type in bytes.
527  */
GrVertexAttribTypeSize(GrVertexAttribType type)528 static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
529     switch (type) {
530         case kFloat_GrVertexAttribType:
531             return sizeof(float);
532         case kFloat2_GrVertexAttribType:
533             return 2 * sizeof(float);
534         case kFloat3_GrVertexAttribType:
535             return 3 * sizeof(float);
536         case kFloat4_GrVertexAttribType:
537             return 4 * sizeof(float);
538         case kHalf_GrVertexAttribType:
539             return sizeof(float);
540         case kHalf2_GrVertexAttribType:
541             return 2 * sizeof(float);
542         case kHalf3_GrVertexAttribType:
543             return 3 * sizeof(float);
544         case kHalf4_GrVertexAttribType:
545             return 4 * sizeof(float);
546         case kInt2_GrVertexAttribType:
547             return 2 * sizeof(int32_t);
548         case kInt3_GrVertexAttribType:
549             return 3 * sizeof(int32_t);
550         case kInt4_GrVertexAttribType:
551             return 4 * sizeof(int32_t);
552         case kUByte_norm_GrVertexAttribType:
553             return 1 * sizeof(char);
554         case kUByte4_norm_GrVertexAttribType:
555             return 4 * sizeof(char);
556         case kShort2_GrVertexAttribType:
557             return 2 * sizeof(int16_t);
558         case kUShort2_GrVertexAttribType: // fall through
559         case kUShort2_norm_GrVertexAttribType:
560             return 2 * sizeof(uint16_t);
561         case kInt_GrVertexAttribType:
562             return sizeof(int32_t);
563         case kUint_GrVertexAttribType:
564             return sizeof(uint32_t);
565     }
566     SK_ABORT("Unexpected attribute type");
567     return 0;
568 }
569 
570 /**
571  * converts a GrVertexAttribType to a GrSLType
572  */
GrVertexAttribTypeToSLType(GrVertexAttribType type)573 static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
574     switch (type) {
575         case kShort2_GrVertexAttribType:
576             return kShort2_GrSLType;
577         case kUShort2_GrVertexAttribType:
578             return kUShort2_GrSLType;
579         case kUShort2_norm_GrVertexAttribType:
580             return kFloat2_GrSLType;
581         case kUByte_norm_GrVertexAttribType:   // fall through
582         case kFloat_GrVertexAttribType:
583             return kFloat_GrSLType;
584         case kFloat2_GrVertexAttribType:
585             return kFloat2_GrSLType;
586         case kFloat3_GrVertexAttribType:
587             return kFloat3_GrSLType;
588         case kFloat4_GrVertexAttribType:
589             return kFloat4_GrSLType;
590         case kHalf_GrVertexAttribType:
591             return kHalf_GrSLType;
592         case kHalf2_GrVertexAttribType:
593             return kHalf2_GrSLType;
594         case kHalf3_GrVertexAttribType:
595             return kHalf3_GrSLType;
596         case kHalf4_GrVertexAttribType:
597         case kUByte4_norm_GrVertexAttribType:
598             return kHalf4_GrSLType;
599         case kInt2_GrVertexAttribType:
600             return kInt2_GrSLType;
601         case kInt3_GrVertexAttribType:
602             return kInt3_GrSLType;
603         case kInt4_GrVertexAttribType:
604             return kInt4_GrSLType;
605         case kInt_GrVertexAttribType:
606             return kInt_GrSLType;
607         case kUint_GrVertexAttribType:
608             return kUint_GrSLType;
609     }
610     SK_ABORT("Unsupported type conversion");
611     return kVoid_GrSLType;
612 }
613 
614 //////////////////////////////////////////////////////////////////////////////
615 
616 static const int kGrClipEdgeTypeCnt = (int) GrClipEdgeType::kLast + 1;
617 
GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType)618 static inline bool GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType) {
619     return (GrClipEdgeType::kFillAA == edgeType || GrClipEdgeType::kFillBW == edgeType);
620 }
621 
GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType)622 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType) {
623     return (GrClipEdgeType::kInverseFillAA == edgeType ||
624             GrClipEdgeType::kInverseFillBW == edgeType);
625 }
626 
GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType)627 static inline bool GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType) {
628     return (GrClipEdgeType::kFillBW != edgeType &&
629             GrClipEdgeType::kInverseFillBW != edgeType);
630 }
631 
GrInvertProcessorEdgeType(const GrClipEdgeType edgeType)632 static inline GrClipEdgeType GrInvertProcessorEdgeType(const GrClipEdgeType edgeType) {
633     switch (edgeType) {
634         case GrClipEdgeType::kFillBW:
635             return GrClipEdgeType::kInverseFillBW;
636         case GrClipEdgeType::kFillAA:
637             return GrClipEdgeType::kInverseFillAA;
638         case GrClipEdgeType::kInverseFillBW:
639             return GrClipEdgeType::kFillBW;
640         case GrClipEdgeType::kInverseFillAA:
641             return GrClipEdgeType::kFillAA;
642         case GrClipEdgeType::kHairlineAA:
643             SK_ABORT("Hairline fill isn't invertible.");
644     }
645     return GrClipEdgeType::kFillAA;  // suppress warning.
646 }
647 
648 /**
649  * Indicates the type of pending IO operations that can be recorded for gpu resources.
650  */
651 enum GrIOType {
652     kRead_GrIOType,
653     kWrite_GrIOType,
654     kRW_GrIOType
655 };
656 
657 /**
658  * Indicates the type of data that a GPU buffer will be used for.
659  */
660 enum GrBufferType {
661     kVertex_GrBufferType,
662     kIndex_GrBufferType,
663     kTexel_GrBufferType,
664     kDrawIndirect_GrBufferType,
665     kXferCpuToGpu_GrBufferType,
666     kXferGpuToCpu_GrBufferType,
667 
668     kLast_GrBufferType = kXferGpuToCpu_GrBufferType
669 };
670 static const int kGrBufferTypeCount = kLast_GrBufferType + 1;
671 
GrBufferTypeIsVertexOrIndex(GrBufferType type)672 static inline bool GrBufferTypeIsVertexOrIndex(GrBufferType type) {
673     SkASSERT(type >= 0 && type < kGrBufferTypeCount);
674     return type <= kIndex_GrBufferType;
675 
676     GR_STATIC_ASSERT(0 == kVertex_GrBufferType);
677     GR_STATIC_ASSERT(1 == kIndex_GrBufferType);
678 }
679 
680 /**
681  * Provides a performance hint regarding the frequency at which a data store will be accessed.
682  */
683 enum GrAccessPattern {
684     /** Data store will be respecified repeatedly and used many times. */
685     kDynamic_GrAccessPattern,
686     /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
687     kStatic_GrAccessPattern,
688     /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
689     kStream_GrAccessPattern,
690 
691     kLast_GrAccessPattern = kStream_GrAccessPattern
692 };
693 
694 // Flags shared between GrRenderTarget and GrRenderTargetProxy
695 enum class GrRenderTargetFlags {
696     kNone               = 0,
697 
698     // For internal resources:
699     //    this is enabled whenever MSAA is enabled and GrCaps reports mixed samples are supported
700     // For wrapped resources:
701     //    this is disabled for FBO0
702     //    but, otherwise, is enabled whenever MSAA is enabled and GrCaps reports mixed samples
703     //        are supported
704     kMixedSampled       = 1 << 0,
705 
706     // For internal resources:
707     //    this is enabled whenever GrCaps reports window rect support
708     // For wrapped resources1
709     //    this is disabled for FBO0
710     //    but, otherwise, is enabled whenever GrCaps reports window rect support
711     kWindowRectsSupport = 1 << 1
712 };
713 GR_MAKE_BITFIELD_CLASS_OPS(GrRenderTargetFlags)
714 
715 #ifdef SK_DEBUG
716 // Takes a pointer to a GrCaps, and will suppress prints if required
717 #define GrCapsDebugf(caps, ...)      \
718     if (!(caps)->suppressPrints()) { \
719         SkDebugf(__VA_ARGS__);       \
720     }
721 #else
722 #define GrCapsDebugf(caps, ...)
723 #endif
724 
725 /**
726  * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
727  */
728 enum class GrBackendObjectOwnership : bool {
729     /** Holder does not destroy the backend object. */
730     kBorrowed = false,
731     /** Holder destroys the backend object. */
732     kOwned = true
733 };
734 
735 template <typename T>
unique_ptr_address_as_pointer_address(std::unique_ptr<T> const * up)736 T* const* unique_ptr_address_as_pointer_address(std::unique_ptr<T> const* up) {
737     static_assert(sizeof(T*) == sizeof(std::unique_ptr<T>), "unique_ptr not expected size.");
738     return reinterpret_cast<T* const*>(up);
739 }
740 
741 /*
742  * Object for CPU-GPU synchronization
743  */
744 typedef uint64_t GrFence;
745 
746 /**
747  * Used to include or exclude specific GPU path renderers for testing purposes.
748  */
749 enum class GpuPathRenderers {
750     kNone              = 0, // Always use sofware masks and/or GrDefaultPathRenderer.
751     kDashLine          = 1 << 0,
752     kStencilAndCover   = 1 << 1,
753     kMSAA              = 1 << 2,
754     kAAConvex          = 1 << 3,
755     kAALinearizing     = 1 << 4,
756     kSmall             = 1 << 5,
757     kCoverageCounting  = 1 << 6,
758     kTessellating      = 1 << 7,
759 
760     kAll               = (kTessellating | (kTessellating - 1)),
761     kDefault           = kAll
762 };
763 
764 /**
765  * Used to describe the current state of Mips on a GrTexture
766  */
767 enum class  GrMipMapsStatus {
768     kNotAllocated, // Mips have not been allocated
769     kDirty,        // Mips are allocated but the full mip tree does not have valid data
770     kValid,        // All levels fully allocated and have valid data in them
771 };
772 
773 GR_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers)
774 
775 /**
776  * We want to extend the GrPixelConfig enum to add cases for dealing with alpha_8 which is
777  * internally either alpha8 or red8. Also for Gray_8 which can be luminance_8 or red_8.
778  */
779 static constexpr GrPixelConfig kAlpha_8_as_Alpha_GrPixelConfig = kPrivateConfig1_GrPixelConfig;
780 static constexpr GrPixelConfig kAlpha_8_as_Red_GrPixelConfig = kPrivateConfig2_GrPixelConfig;
781 static constexpr GrPixelConfig kAlpha_half_as_Red_GrPixelConfig = kPrivateConfig3_GrPixelConfig;
782 static constexpr GrPixelConfig kGray_8_as_Lum_GrPixelConfig = kPrivateConfig4_GrPixelConfig;
783 static constexpr GrPixelConfig kGray_8_as_Red_GrPixelConfig = kPrivateConfig5_GrPixelConfig;
784 
785 /**
786  * Refers to the encoding of a GPU buffer as it will be interpreted by the GPU when sampling and
787  * blending.
788  */
789 enum class GrSRGBEncoded : bool { kNo = false, kYes = true };
790 
791 /**
792  * Describes whether pixel data encoding should be converted to/from linear/sRGB encoding.
793  */
794 enum class GrSRGBConversion {
795     kNone,
796     kSRGBToLinear,
797     kLinearToSRGB,
798 };
799 
800 /**
801  * Utility functions for GrPixelConfig
802  */
803 
804 // Returns whether the config's color channels are sRGB encoded.
GrPixelConfigIsSRGBEncoded(GrPixelConfig config)805 static inline GrSRGBEncoded GrPixelConfigIsSRGBEncoded(GrPixelConfig config) {
806     switch (config) {
807         case kSRGBA_8888_GrPixelConfig:
808         case kSBGRA_8888_GrPixelConfig:
809             return GrSRGBEncoded::kYes;
810         case kUnknown_GrPixelConfig:
811         case kAlpha_8_GrPixelConfig:
812         case kAlpha_8_as_Alpha_GrPixelConfig:
813         case kAlpha_8_as_Red_GrPixelConfig:
814         case kGray_8_GrPixelConfig:
815         case kGray_8_as_Lum_GrPixelConfig:
816         case kGray_8_as_Red_GrPixelConfig:
817         case kRGB_565_GrPixelConfig:
818         case kRGBA_4444_GrPixelConfig:
819         case kRGBA_8888_GrPixelConfig:
820         case kBGRA_8888_GrPixelConfig:
821         case kRGBA_float_GrPixelConfig:
822         case kRG_float_GrPixelConfig:
823         case kAlpha_half_GrPixelConfig:
824         case kAlpha_half_as_Red_GrPixelConfig:
825         case kRGBA_half_GrPixelConfig:
826             return GrSRGBEncoded::kNo;
827     }
828     SK_ABORT("Invalid pixel config");
829     return GrSRGBEncoded::kNo;
830 }
831 
GrPixelConfigIsSRGB(GrPixelConfig config)832 static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
833     return GrSRGBEncoded::kYes == GrPixelConfigIsSRGBEncoded(config);
834 }
835 // Takes a config and returns the equivalent config with the R and B order
836 // swapped if such a config exists. Otherwise, kUnknown_GrPixelConfig
GrPixelConfigSwapRAndB(GrPixelConfig config)837 static inline GrPixelConfig GrPixelConfigSwapRAndB(GrPixelConfig config) {
838     switch (config) {
839         case kBGRA_8888_GrPixelConfig:
840             return kRGBA_8888_GrPixelConfig;
841         case kRGBA_8888_GrPixelConfig:
842             return kBGRA_8888_GrPixelConfig;
843         case kSBGRA_8888_GrPixelConfig:
844             return kSRGBA_8888_GrPixelConfig;
845         case kSRGBA_8888_GrPixelConfig:
846             return kSBGRA_8888_GrPixelConfig;
847         case kUnknown_GrPixelConfig:
848         case kAlpha_8_GrPixelConfig:
849         case kAlpha_8_as_Alpha_GrPixelConfig:
850         case kAlpha_8_as_Red_GrPixelConfig:
851         case kGray_8_GrPixelConfig:
852         case kGray_8_as_Lum_GrPixelConfig:
853         case kGray_8_as_Red_GrPixelConfig:
854         case kRGB_565_GrPixelConfig:
855         case kRGBA_4444_GrPixelConfig:
856         case kRGBA_float_GrPixelConfig:
857         case kRG_float_GrPixelConfig:
858         case kAlpha_half_GrPixelConfig:
859         case kAlpha_half_as_Red_GrPixelConfig:
860         case kRGBA_half_GrPixelConfig:
861             return kUnknown_GrPixelConfig;
862     }
863     SK_ABORT("Invalid pixel config");
864     return kUnknown_GrPixelConfig;
865 }
866 
GrBytesPerPixel(GrPixelConfig config)867 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
868     switch (config) {
869         case kAlpha_8_GrPixelConfig:
870         case kAlpha_8_as_Alpha_GrPixelConfig:
871         case kAlpha_8_as_Red_GrPixelConfig:
872         case kGray_8_GrPixelConfig:
873         case kGray_8_as_Lum_GrPixelConfig:
874         case kGray_8_as_Red_GrPixelConfig:
875             return 1;
876         case kRGB_565_GrPixelConfig:
877         case kRGBA_4444_GrPixelConfig:
878         case kAlpha_half_GrPixelConfig:
879         case kAlpha_half_as_Red_GrPixelConfig:
880             return 2;
881         case kRGBA_8888_GrPixelConfig:
882         case kBGRA_8888_GrPixelConfig:
883         case kSRGBA_8888_GrPixelConfig:
884         case kSBGRA_8888_GrPixelConfig:
885             return 4;
886         case kRGBA_half_GrPixelConfig:
887             return 8;
888         case kRGBA_float_GrPixelConfig:
889             return 16;
890         case kRG_float_GrPixelConfig:
891             return 8;
892         case kUnknown_GrPixelConfig:
893             return 0;
894     }
895     SK_ABORT("Invalid pixel config");
896     return 0;
897 }
898 
GrPixelConfigIsOpaque(GrPixelConfig config)899 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
900     switch (config) {
901         case kRGB_565_GrPixelConfig:
902         case kGray_8_GrPixelConfig:
903         case kGray_8_as_Lum_GrPixelConfig:
904         case kGray_8_as_Red_GrPixelConfig:
905         case kRG_float_GrPixelConfig:
906             return true;
907         case kAlpha_8_GrPixelConfig:
908         case kAlpha_8_as_Alpha_GrPixelConfig:
909         case kAlpha_8_as_Red_GrPixelConfig:
910         case kRGBA_4444_GrPixelConfig:
911         case kAlpha_half_GrPixelConfig:
912         case kAlpha_half_as_Red_GrPixelConfig:
913         case kRGBA_8888_GrPixelConfig:
914         case kBGRA_8888_GrPixelConfig:
915         case kSRGBA_8888_GrPixelConfig:
916         case kSBGRA_8888_GrPixelConfig:
917         case kRGBA_half_GrPixelConfig:
918         case kRGBA_float_GrPixelConfig:
919         case kUnknown_GrPixelConfig:
920             return false;
921     }
922     SK_ABORT("Invalid pixel config");
923     return false;
924 }
925 
GrPixelConfigIsAlphaOnly(GrPixelConfig config)926 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
927     switch (config) {
928         case kAlpha_8_GrPixelConfig:
929         case kAlpha_8_as_Alpha_GrPixelConfig:
930         case kAlpha_8_as_Red_GrPixelConfig:
931         case kAlpha_half_GrPixelConfig:
932         case kAlpha_half_as_Red_GrPixelConfig:
933             return true;
934         case kUnknown_GrPixelConfig:
935         case kGray_8_GrPixelConfig:
936         case kGray_8_as_Lum_GrPixelConfig:
937         case kGray_8_as_Red_GrPixelConfig:
938         case kRGB_565_GrPixelConfig:
939         case kRGBA_4444_GrPixelConfig:
940         case kRGBA_8888_GrPixelConfig:
941         case kBGRA_8888_GrPixelConfig:
942         case kSRGBA_8888_GrPixelConfig:
943         case kSBGRA_8888_GrPixelConfig:
944         case kRGBA_float_GrPixelConfig:
945         case kRG_float_GrPixelConfig:
946         case kRGBA_half_GrPixelConfig:
947             return false;
948     }
949     SK_ABORT("Invalid pixel config.");
950     return false;
951 }
952 
GrPixelConfigIsFloatingPoint(GrPixelConfig config)953 static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
954     switch (config) {
955         case kRGBA_float_GrPixelConfig:
956         case kRG_float_GrPixelConfig:
957         case kAlpha_half_GrPixelConfig:
958         case kAlpha_half_as_Red_GrPixelConfig:
959         case kRGBA_half_GrPixelConfig:
960             return true;
961         case kUnknown_GrPixelConfig:
962         case kAlpha_8_GrPixelConfig:
963         case kAlpha_8_as_Alpha_GrPixelConfig:
964         case kAlpha_8_as_Red_GrPixelConfig:
965         case kGray_8_GrPixelConfig:
966         case kGray_8_as_Lum_GrPixelConfig:
967         case kGray_8_as_Red_GrPixelConfig:
968         case kRGB_565_GrPixelConfig:
969         case kRGBA_4444_GrPixelConfig:
970         case kRGBA_8888_GrPixelConfig:
971         case kBGRA_8888_GrPixelConfig:
972         case kSRGBA_8888_GrPixelConfig:
973         case kSBGRA_8888_GrPixelConfig:
974             return false;
975     }
976     SK_ABORT("Invalid pixel config");
977     return false;
978 }
979 
GrPixelConfigIsUnorm(GrPixelConfig config)980 static inline bool GrPixelConfigIsUnorm(GrPixelConfig config) {
981     switch (config) {
982         case kAlpha_8_GrPixelConfig:
983         case kAlpha_8_as_Alpha_GrPixelConfig:
984         case kAlpha_8_as_Red_GrPixelConfig:
985         case kGray_8_GrPixelConfig:
986         case kGray_8_as_Lum_GrPixelConfig:
987         case kGray_8_as_Red_GrPixelConfig:
988         case kRGB_565_GrPixelConfig:
989         case kRGBA_4444_GrPixelConfig:
990         case kRGBA_8888_GrPixelConfig:
991         case kBGRA_8888_GrPixelConfig:
992         case kSRGBA_8888_GrPixelConfig:
993         case kSBGRA_8888_GrPixelConfig:
994             return true;
995         case kUnknown_GrPixelConfig:
996         case kAlpha_half_GrPixelConfig:
997         case kAlpha_half_as_Red_GrPixelConfig:
998         case kRGBA_float_GrPixelConfig:
999         case kRG_float_GrPixelConfig:
1000         case kRGBA_half_GrPixelConfig:
1001             return false;
1002     }
1003     SK_ABORT("Invalid pixel config.");
1004     return false;
1005 }
1006 
1007 /**
1008  * Precision qualifier that should be used with a sampler.
1009  */
GrSLSamplerPrecision(GrPixelConfig config)1010 static inline GrSLPrecision GrSLSamplerPrecision(GrPixelConfig config) {
1011     switch (config) {
1012         case kUnknown_GrPixelConfig:
1013         case kAlpha_8_GrPixelConfig:
1014         case kAlpha_8_as_Alpha_GrPixelConfig:
1015         case kAlpha_8_as_Red_GrPixelConfig:
1016         case kGray_8_GrPixelConfig:
1017         case kGray_8_as_Lum_GrPixelConfig:
1018         case kGray_8_as_Red_GrPixelConfig:
1019         case kRGB_565_GrPixelConfig:
1020         case kRGBA_4444_GrPixelConfig:
1021         case kRGBA_8888_GrPixelConfig:
1022         case kBGRA_8888_GrPixelConfig:
1023         case kSRGBA_8888_GrPixelConfig:
1024         case kSBGRA_8888_GrPixelConfig:
1025             return kLow_GrSLPrecision;
1026         case kRGBA_float_GrPixelConfig:
1027         case kRG_float_GrPixelConfig:
1028             return kHigh_GrSLPrecision;
1029         case kAlpha_half_GrPixelConfig:
1030         case kAlpha_half_as_Red_GrPixelConfig:
1031         case kRGBA_half_GrPixelConfig:
1032             return kMedium_GrSLPrecision;
1033     }
1034     SK_ABORT("Unexpected type");
1035     return kHigh_GrSLPrecision;
1036 }
1037 
GrGetPixelConfigIsClamped(GrPixelConfig config)1038 static inline GrPixelConfigIsClamped GrGetPixelConfigIsClamped(GrPixelConfig config) {
1039     return GrPixelConfigIsFloatingPoint(config) ? GrPixelConfigIsClamped::kNo
1040                                                 : GrPixelConfigIsClamped::kYes;
1041 }
1042 
1043 /**
1044  * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels,
1045  * their type, and width. This exists so that the GPU backend can have private types that have no
1046  * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend.
1047  * It does not refer to a texture format and the mapping to texture formats may be many-to-many.
1048  * It does not specify the sRGB encding of the stored values.
1049  */
1050 enum class GrColorType {
1051     kUnknown,
1052     kAlpha_8,
1053     kRGB_565,
1054     kABGR_4444,  // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed.
1055     kRGBA_8888,
1056     kBGRA_8888,
1057     kGray_8,
1058     kAlpha_F16,
1059     kRGBA_F16,
1060     kRG_F32,
1061     kRGBA_F32,
1062 };
1063 
GrColorTypeToSkColorType(GrColorType ct)1064 static inline SkColorType GrColorTypeToSkColorType(GrColorType ct) {
1065     switch (ct) {
1066         case GrColorType::kUnknown:   return kUnknown_SkColorType;
1067         case GrColorType::kAlpha_8:   return kAlpha_8_SkColorType;
1068         case GrColorType::kRGB_565:   return kRGB_565_SkColorType;
1069         case GrColorType::kABGR_4444: return kARGB_4444_SkColorType;
1070         case GrColorType::kRGBA_8888: return kRGBA_8888_SkColorType;
1071         case GrColorType::kBGRA_8888: return kBGRA_8888_SkColorType;
1072         case GrColorType::kGray_8:    return kGray_8_SkColorType;
1073         case GrColorType::kAlpha_F16: return kUnknown_SkColorType;
1074         case GrColorType::kRGBA_F16:  return kRGBA_F16_SkColorType;
1075         case GrColorType::kRG_F32:    return kUnknown_SkColorType;
1076         case GrColorType::kRGBA_F32:  return kUnknown_SkColorType;
1077     }
1078     SK_ABORT("Invalid GrColorType");
1079     return kUnknown_SkColorType;
1080 }
1081 
SkColorTypeToGrColorType(SkColorType ct)1082 static inline GrColorType SkColorTypeToGrColorType(SkColorType ct) {
1083     switch (ct) {
1084         case kUnknown_SkColorType:      return GrColorType::kUnknown;
1085         case kAlpha_8_SkColorType:      return GrColorType::kAlpha_8;
1086         case kRGB_565_SkColorType:      return GrColorType::kRGB_565;
1087         case kARGB_4444_SkColorType:    return GrColorType::kABGR_4444;
1088         case kRGBA_8888_SkColorType:    return GrColorType::kRGBA_8888;
1089         case kBGRA_8888_SkColorType:    return GrColorType::kBGRA_8888;
1090         case kRGB_888x_SkColorType:     return GrColorType::kUnknown;
1091         case kGray_8_SkColorType:       return GrColorType::kGray_8;
1092         case kRGBA_F16_SkColorType:     return GrColorType::kRGBA_F16;
1093         case kRGBA_1010102_SkColorType: return GrColorType::kUnknown;
1094         case kRGB_101010x_SkColorType:  return GrColorType::kUnknown;
1095     }
1096     SK_ABORT("Invalid SkColorType");
1097     return GrColorType::kUnknown;
1098 }
1099 
GrColorTypeBytesPerPixel(GrColorType ct)1100 static inline int GrColorTypeBytesPerPixel(GrColorType ct) {
1101     switch (ct) {
1102         case GrColorType::kUnknown:   return 0;
1103         case GrColorType::kAlpha_8:   return 1;
1104         case GrColorType::kRGB_565:   return 2;
1105         case GrColorType::kABGR_4444: return 2;
1106         case GrColorType::kRGBA_8888: return 4;
1107         case GrColorType::kBGRA_8888: return 4;
1108         case GrColorType::kGray_8:    return 1;
1109         case GrColorType::kAlpha_F16: return 2;
1110         case GrColorType::kRGBA_F16:  return 8;
1111         case GrColorType::kRG_F32:    return 8;
1112         case GrColorType::kRGBA_F32:  return 16;
1113     }
1114     SK_ABORT("Invalid GrColorType");
1115     return 0;
1116 }
1117 
GrColorTypeIsAlphaOnly(GrColorType ct)1118 static inline int GrColorTypeIsAlphaOnly(GrColorType ct) {
1119     switch (ct) {
1120         case GrColorType::kUnknown:   return false;
1121         case GrColorType::kAlpha_8:   return true;
1122         case GrColorType::kRGB_565:   return false;
1123         case GrColorType::kABGR_4444: return false;
1124         case GrColorType::kRGBA_8888: return false;
1125         case GrColorType::kBGRA_8888: return false;
1126         case GrColorType::kGray_8:    return false;
1127         case GrColorType::kAlpha_F16: return true;
1128         case GrColorType::kRGBA_F16:  return false;
1129         case GrColorType::kRG_F32:    return false;
1130         case GrColorType::kRGBA_F32:  return false;
1131     }
1132     SK_ABORT("Invalid GrColorType");
1133     return false;
1134 }
1135 
GrPixelConfigToColorTypeAndEncoding(GrPixelConfig config,GrSRGBEncoded * srgbEncoded)1136 static inline GrColorType GrPixelConfigToColorTypeAndEncoding(GrPixelConfig config,
1137                                                               GrSRGBEncoded* srgbEncoded) {
1138     SkASSERT(srgbEncoded);
1139     switch (config) {
1140         case kUnknown_GrPixelConfig:
1141             return GrColorType::kUnknown;
1142         case kAlpha_8_GrPixelConfig:
1143             *srgbEncoded = GrSRGBEncoded::kNo;
1144             return GrColorType::kAlpha_8;
1145         case kGray_8_GrPixelConfig:
1146             *srgbEncoded = GrSRGBEncoded::kNo;
1147             return GrColorType::kGray_8;
1148         case kRGB_565_GrPixelConfig:
1149             *srgbEncoded = GrSRGBEncoded::kNo;
1150             return GrColorType::kRGB_565;
1151         case kRGBA_4444_GrPixelConfig:
1152             *srgbEncoded = GrSRGBEncoded::kNo;
1153             return GrColorType::kABGR_4444;
1154         case kRGBA_8888_GrPixelConfig:
1155             *srgbEncoded = GrSRGBEncoded::kNo;
1156             return GrColorType::kRGBA_8888;
1157         case kBGRA_8888_GrPixelConfig:
1158             *srgbEncoded = GrSRGBEncoded::kNo;
1159             return GrColorType::kBGRA_8888;
1160         case kSRGBA_8888_GrPixelConfig:
1161             *srgbEncoded = GrSRGBEncoded::kYes;
1162             return GrColorType::kRGBA_8888;
1163         case kSBGRA_8888_GrPixelConfig:
1164             *srgbEncoded = GrSRGBEncoded::kYes;
1165             return GrColorType::kBGRA_8888;
1166         case kRGBA_float_GrPixelConfig:
1167             *srgbEncoded = GrSRGBEncoded::kNo;
1168             return GrColorType::kRGBA_F32;
1169         case kRG_float_GrPixelConfig:
1170             *srgbEncoded = GrSRGBEncoded::kNo;
1171             return GrColorType::kRG_F32;
1172         case kAlpha_half_GrPixelConfig:
1173             *srgbEncoded = GrSRGBEncoded::kNo;
1174             return GrColorType::kAlpha_F16;
1175         case kRGBA_half_GrPixelConfig:
1176             *srgbEncoded = GrSRGBEncoded::kNo;
1177             return GrColorType::kRGBA_F16;
1178         case kAlpha_8_as_Alpha_GrPixelConfig:
1179             *srgbEncoded = GrSRGBEncoded::kNo;
1180             return GrColorType::kAlpha_8;
1181         case kAlpha_8_as_Red_GrPixelConfig:
1182             *srgbEncoded = GrSRGBEncoded::kNo;
1183             return GrColorType::kAlpha_8;
1184         case kAlpha_half_as_Red_GrPixelConfig:
1185             *srgbEncoded = GrSRGBEncoded::kNo;
1186             return GrColorType::kAlpha_F16;
1187         case kGray_8_as_Lum_GrPixelConfig:
1188             *srgbEncoded = GrSRGBEncoded::kNo;
1189             return GrColorType::kGray_8;
1190         case kGray_8_as_Red_GrPixelConfig:
1191             *srgbEncoded = GrSRGBEncoded::kNo;
1192             return GrColorType::kGray_8;
1193     }
1194     SK_ABORT("Invalid GrPixelConfig");
1195     return GrColorType::kUnknown;
1196 }
1197 
GrPixelConfigToColorType(GrPixelConfig config)1198 static inline GrColorType GrPixelConfigToColorType(GrPixelConfig config) {
1199     GrSRGBEncoded bogusEncoded;
1200     return GrPixelConfigToColorTypeAndEncoding(config, &bogusEncoded);
1201 }
1202 
GrColorTypeToPixelConfig(GrColorType config,GrSRGBEncoded srgbEncoded)1203 static inline GrPixelConfig GrColorTypeToPixelConfig(GrColorType config,
1204                                                      GrSRGBEncoded srgbEncoded) {
1205     switch (config) {
1206         case GrColorType::kUnknown:
1207             return kUnknown_GrPixelConfig;
1208         case GrColorType::kAlpha_8:
1209             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1210                                                         : kAlpha_8_GrPixelConfig;
1211 
1212         case GrColorType::kGray_8:
1213             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1214                                                         : kGray_8_GrPixelConfig;
1215 
1216         case GrColorType::kRGB_565:
1217             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1218                                                         : kRGB_565_GrPixelConfig;
1219 
1220         case GrColorType::kABGR_4444:
1221             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1222                                                         : kRGBA_4444_GrPixelConfig;
1223 
1224         case GrColorType::kRGBA_8888:
1225             return (GrSRGBEncoded::kYes == srgbEncoded) ? kSRGBA_8888_GrPixelConfig
1226                                                         : kRGBA_8888_GrPixelConfig;
1227 
1228         case GrColorType::kBGRA_8888:
1229             return (GrSRGBEncoded::kYes == srgbEncoded) ? kSBGRA_8888_GrPixelConfig
1230                                                         : kBGRA_8888_GrPixelConfig;
1231 
1232         case GrColorType::kRGBA_F32:
1233             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1234                                                         : kRGBA_float_GrPixelConfig;
1235 
1236         case GrColorType::kRG_F32:
1237             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1238                                                         : kRG_float_GrPixelConfig;
1239 
1240         case GrColorType::kAlpha_F16:
1241             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1242                                                         : kAlpha_half_GrPixelConfig;
1243 
1244         case GrColorType::kRGBA_F16:
1245             return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
1246                                                         : kRGBA_half_GrPixelConfig;
1247     }
1248     SK_ABORT("Invalid GrColorType");
1249     return kUnknown_GrPixelConfig;
1250 }
1251 
1252 class GrReleaseProcHelper : public SkRefCnt {
1253 public:
1254     // These match the definitions in SkImage, from whence they came
1255     typedef void* ReleaseCtx;
1256     typedef void (*ReleaseProc)(ReleaseCtx);
1257 
GrReleaseProcHelper(ReleaseProc proc,ReleaseCtx ctx)1258     GrReleaseProcHelper(ReleaseProc proc, ReleaseCtx ctx) : fReleaseProc(proc), fReleaseCtx(ctx) {}
~GrReleaseProcHelper()1259     ~GrReleaseProcHelper() override {
1260         fReleaseProc(fReleaseCtx);
1261     }
1262 
1263 private:
1264     ReleaseProc fReleaseProc;
1265     ReleaseCtx  fReleaseCtx;
1266 };
1267 
1268 #endif
1269