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