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