• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 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 GrTypes_DEFINED
9 #define GrTypes_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkTo.h" // IWYU pragma: keep
13 
14 #include <cstddef>
15 #include <cstdint>
16 class GrBackendSemaphore;
17 
18 namespace skgpu {
19 enum class Protected : bool;
20 enum class Renderable : bool;
21 }
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 
25 /**
26  * Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of
27  * masking with type safety. Instantiated with the ~ operator.
28  */
29 template<typename TFlags> class GrTFlagsMask {
30 public:
GrTFlagsMask(TFlags value)31     constexpr explicit GrTFlagsMask(TFlags value) : GrTFlagsMask(static_cast<int>(value)) {}
GrTFlagsMask(int value)32     constexpr explicit GrTFlagsMask(int value) : fValue(value) {}
value()33     constexpr int value() const { return fValue; }
34 private:
35     const int fValue;
36 };
37 
38 /**
39  * Defines bitwise operators that make it possible to use an enum class as a
40  * basic bitfield.
41  */
42 #define GR_MAKE_BITFIELD_CLASS_OPS(X) \
43     [[maybe_unused]] constexpr GrTFlagsMask<X> operator~(X a) { \
44         return GrTFlagsMask<X>(~static_cast<int>(a)); \
45     } \
46     [[maybe_unused]] constexpr X operator|(X a, X b) { \
47         return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \
48     } \
49     [[maybe_unused]] inline X& operator|=(X& a, X b) { \
50         return (a = a | b); \
51     } \
52     [[maybe_unused]] constexpr bool operator&(X a, X b) { \
53         return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \
54     } \
55     [[maybe_unused]] constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X> a, GrTFlagsMask<X> b) { \
56         return GrTFlagsMask<X>(a.value() | b.value()); \
57     } \
58     [[maybe_unused]] constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X> a, X b) { \
59         return GrTFlagsMask<X>(a.value() | static_cast<int>(b)); \
60     } \
61     [[maybe_unused]] constexpr GrTFlagsMask<X> operator|(X a, GrTFlagsMask<X> b) { \
62         return GrTFlagsMask<X>(static_cast<int>(a) | b.value()); \
63     } \
64     [[maybe_unused]] constexpr X operator&(GrTFlagsMask<X> a, GrTFlagsMask<X> b) { \
65         return static_cast<X>(a.value() & b.value()); \
66     } \
67     [[maybe_unused]] constexpr X operator&(GrTFlagsMask<X> a, X b) { \
68         return static_cast<X>(a.value() & static_cast<int>(b)); \
69     } \
70     [[maybe_unused]] constexpr X operator&(X a, GrTFlagsMask<X> b) { \
71         return static_cast<X>(static_cast<int>(a) & b.value()); \
72     } \
73     [[maybe_unused]] inline X& operator&=(X& a, GrTFlagsMask<X> b) { \
74         return (a = a & b); \
75     } \
76 
77 #define GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \
78     friend constexpr GrTFlagsMask<X> operator ~(X); \
79     friend constexpr X operator |(X, X); \
80     friend X& operator |=(X&, X); \
81     friend constexpr bool operator &(X, X); \
82     friend constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X>, GrTFlagsMask<X>); \
83     friend constexpr GrTFlagsMask<X> operator|(GrTFlagsMask<X>, X); \
84     friend constexpr GrTFlagsMask<X> operator|(X, GrTFlagsMask<X>); \
85     friend constexpr X operator&(GrTFlagsMask<X>, GrTFlagsMask<X>); \
86     friend constexpr X operator&(GrTFlagsMask<X>, X); \
87     friend constexpr X operator&(X, GrTFlagsMask<X>); \
88     friend X& operator &=(X&, GrTFlagsMask<X>)
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 
92 /**
93  * Possible 3D APIs that may be used by Ganesh.
94  */
95 enum class GrBackendApi : unsigned {
96     kOpenGL,
97     kVulkan,
98     kMetal,
99     kDirect3D,
100 
101     /**
102      * Mock is a backend that does not draw anything. It is used for unit tests
103      * and to measure CPU overhead.
104      */
105     kMock,
106 
107     /**
108      * Ganesh doesn't support some context types (e.g. Dawn) and will return Unsupported.
109      */
110     kUnsupported,
111 
112     /**
113      * Added here to support the legacy GrBackend enum value and clients who referenced it using
114      * GrBackend::kOpenGL_GrBackend.
115      */
116     kOpenGL_GrBackend = kOpenGL,
117 };
118 
119 /**
120  * Previously the above enum was not an enum class but a normal enum. To support the legacy use of
121  * the enum values we define them below so that no clients break.
122  */
123 typedef GrBackendApi GrBackend;
124 
125 static constexpr GrBackendApi kMetal_GrBackend = GrBackendApi::kMetal;
126 static constexpr GrBackendApi kVulkan_GrBackend = GrBackendApi::kVulkan;
127 static constexpr GrBackendApi kMock_GrBackend = GrBackendApi::kMock;
128 
129 ///////////////////////////////////////////////////////////////////////////////
130 
131 /*
132  * Can a GrBackendObject be rendered to?
133  */
134 using GrRenderable = skgpu::Renderable;
135 
136 /*
137  * Used to say whether texture is backed by protected memory.
138  */
139 using GrProtected = skgpu::Protected;
140 
141 ///////////////////////////////////////////////////////////////////////////////
142 
143 /**
144  * GPU SkImage and SkSurfaces can be stored such that (0, 0) in texture space may correspond to
145  * either the top-left or bottom-left content pixel.
146  */
147 enum GrSurfaceOrigin : int {
148     kTopLeft_GrSurfaceOrigin,
149     kBottomLeft_GrSurfaceOrigin,
150 };
151 
152 /**
153  * A GrContext's cache of backend context state can be partially invalidated.
154  * These enums are specific to the GL backend and we'd add a new set for an alternative backend.
155  */
156 enum GrGLBackendState {
157     kRenderTarget_GrGLBackendState     = 1 << 0,
158     // Also includes samplers bound to texture units.
159     kTextureBinding_GrGLBackendState   = 1 << 1,
160     // View state stands for scissor and viewport
161     kView_GrGLBackendState             = 1 << 2,
162     kBlend_GrGLBackendState            = 1 << 3,
163     kMSAAEnable_GrGLBackendState       = 1 << 4,
164     kVertex_GrGLBackendState           = 1 << 5,
165     kStencil_GrGLBackendState          = 1 << 6,
166     kPixelStore_GrGLBackendState       = 1 << 7,
167     kProgram_GrGLBackendState          = 1 << 8,
168     kFixedFunction_GrGLBackendState    = 1 << 9,
169     kMisc_GrGLBackendState             = 1 << 10,
170     kALL_GrGLBackendState              = 0xffff
171 };
172 
173 /**
174  * This value translates to reseting all the context state for any backend.
175  */
176 static const uint32_t kAll_GrBackendState = 0xffffffff;
177 
178 typedef void* GrGpuFinishedContext;
179 typedef void (*GrGpuFinishedProc)(GrGpuFinishedContext finishedContext);
180 
181 typedef void* GrGpuSubmittedContext;
182 typedef void (*GrGpuSubmittedProc)(GrGpuSubmittedContext submittedContext, bool success);
183 
184 typedef void* GrDirectContextDestroyedContext;
185 typedef void (*GrDirectContextDestroyedProc)(GrDirectContextDestroyedContext destroyedContext);
186 
187 /**
188  * Struct to supply options to flush calls.
189  *
190  * After issuing all commands, fNumSemaphore semaphores will be signaled by the gpu. The client
191  * passes in an array of fNumSemaphores GrBackendSemaphores. In general these GrBackendSemaphore's
192  * can be either initialized or not. If they are initialized, the backend uses the passed in
193  * semaphore. If it is not initialized, a new semaphore is created and the GrBackendSemaphore
194  * object is initialized with that semaphore. The semaphores are not sent to the GPU until the next
195  * GrContext::submit call is made. See the GrContext::submit for more information.
196  *
197  * The client will own and be responsible for deleting the underlying semaphores that are stored
198  * and returned in initialized GrBackendSemaphore objects. The GrBackendSemaphore objects
199  * themselves can be deleted as soon as this function returns.
200  *
201  * If a finishedProc is provided, the finishedProc will be called when all work submitted to the gpu
202  * from this flush call and all previous flush calls has finished on the GPU. If the flush call
203  * fails due to an error and nothing ends up getting sent to the GPU, the finished proc is called
204  * immediately.
205  *
206  * If a submittedProc is provided, the submittedProc will be called when all work from this flush
207  * call is submitted to the GPU. If the flush call fails due to an error and nothing will get sent
208  * to the GPU, the submitted proc is called immediately. It is possibly that when work is finally
209  * submitted, that the submission actual fails. In this case we will not reattempt to do the
210  * submission. Skia notifies the client of these via the success bool passed into the submittedProc.
211  * The submittedProc is useful to the client to know when semaphores that were sent with the flush
212  * have actually been submitted to the GPU so that they can be waited on (or deleted if the submit
213  * fails).
214  * GrBackendSemaphores are not supported for the GL backend and will be ignored if set.
215  */
216 struct GrFlushInfo {
217     size_t fNumSemaphores = 0;
218     GrBackendSemaphore* fSignalSemaphores = nullptr;
219     GrGpuFinishedProc fFinishedProc = nullptr;
220     GrGpuFinishedContext fFinishedContext = nullptr;
221     GrGpuSubmittedProc fSubmittedProc = nullptr;
222     GrGpuSubmittedContext fSubmittedContext = nullptr;
223 };
224 
225 /**
226  * Enum used as return value when flush with semaphores so the client knows whether the valid
227  * semaphores will be submitted on the next GrContext::submit call.
228  */
229 enum class GrSemaphoresSubmitted : bool {
230     kNo = false,
231     kYes = true
232 };
233 
234 enum class GrPurgeResourceOptions {
235     kAllResources,
236     kScratchResourcesOnly,
237 };
238 
239 enum class GrSyncCpu : bool {
240     kNo = false,
241     kYes = true,
242 };
243 
244 #endif
245