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