Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 GrDirectContext_DEFINED |
| 9 | #define GrDirectContext_DEFINED |
| 10 | |
Adlai Holler | 0ce2c54 | 2020-10-06 14:04:35 -0400 | [diff] [blame] | 11 | #include "include/private/GrContext.h" |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 12 | |
| 13 | class GrAtlasManager; |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 14 | class GrSmallPathAtlasMgr; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 15 | |
Robert Phillips | c7228c6 | 2020-07-14 12:57:39 -0400 | [diff] [blame] | 16 | class SK_API GrDirectContext : public GrContext { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 17 | public: |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 18 | #ifdef SK_GL |
| 19 | /** |
| 20 | * Creates a GrDirectContext for a backend context. If no GrGLInterface is provided then the |
| 21 | * result of GrGLMakeNativeInterface() is used if it succeeds. |
| 22 | */ |
| 23 | static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>, const GrContextOptions&); |
| 24 | static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>); |
| 25 | static sk_sp<GrDirectContext> MakeGL(const GrContextOptions&); |
| 26 | static sk_sp<GrDirectContext> MakeGL(); |
| 27 | #endif |
| 28 | |
| 29 | #ifdef SK_VULKAN |
| 30 | /** |
| 31 | * The Vulkan context (VkQueue, VkDevice, VkInstance) must be kept alive until the returned |
| 32 | * GrDirectContext is destroyed. This also means that any objects created with this |
| 33 | * GrDirectContext (e.g. SkSurfaces, SkImages, etc.) must also be released as they may hold |
| 34 | * refs on the GrDirectContext. Once all these objects and the GrDirectContext are released, |
| 35 | * then it is safe to delete the vulkan objects. |
| 36 | */ |
| 37 | static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&, const GrContextOptions&); |
| 38 | static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&); |
| 39 | #endif |
| 40 | |
| 41 | #ifdef SK_METAL |
| 42 | /** |
| 43 | * Makes a GrDirectContext which uses Metal as the backend. The device parameter is an |
| 44 | * MTLDevice and queue is an MTLCommandQueue which should be used by the backend. These objects |
| 45 | * must have a ref on them which can be transferred to Ganesh which will release the ref |
| 46 | * when the GrDirectContext is destroyed. |
| 47 | */ |
| 48 | static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue, const GrContextOptions&); |
| 49 | static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue); |
| 50 | #endif |
| 51 | |
| 52 | #ifdef SK_DIRECT3D |
| 53 | /** |
| 54 | * Makes a GrDirectContext which uses Direct3D as the backend. The Direct3D context |
| 55 | * must be kept alive until the returned GrDirectContext is first destroyed or abandoned. |
| 56 | */ |
| 57 | static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&, const GrContextOptions&); |
| 58 | static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&); |
| 59 | #endif |
| 60 | |
| 61 | #ifdef SK_DAWN |
| 62 | static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&, |
| 63 | const GrContextOptions&); |
| 64 | static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&); |
| 65 | #endif |
| 66 | |
| 67 | static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*, const GrContextOptions&); |
| 68 | static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 69 | |
| 70 | ~GrDirectContext() override; |
| 71 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 72 | /** |
| 73 | * The context normally assumes that no outsider is setting state |
| 74 | * within the underlying 3D API's context/device/whatever. This call informs |
| 75 | * the context that the state was modified and it should resend. Shouldn't |
| 76 | * be called frequently for good performance. |
| 77 | * The flag bits, state, is dependent on which backend is used by the |
| 78 | * context, either GL or D3D (possible in future). |
| 79 | */ |
| 80 | void resetContext(uint32_t state = kAll_GrBackendState); |
| 81 | |
| 82 | /** |
| 83 | * If the backend is GrBackendApi::kOpenGL, then all texture unit/target combinations for which |
| 84 | * the context has modified the bound texture will have texture id 0 bound. This does not |
| 85 | * flush the context. Calling resetContext() does not change the set that will be bound |
| 86 | * to texture id 0 on the next call to resetGLTextureBindings(). After this is called |
| 87 | * all unit/target combinations are considered to have unmodified bindings until the context |
| 88 | * subsequently modifies them (meaning if this is called twice in a row with no intervening |
| 89 | * context usage then the second call is a no-op.) |
| 90 | */ |
| 91 | void resetGLTextureBindings(); |
| 92 | |
| 93 | /** |
| 94 | * Abandons all GPU resources and assumes the underlying backend 3D API context is no longer |
| 95 | * usable. Call this if you have lost the associated GPU context, and thus internal texture, |
| 96 | * buffer, etc. references/IDs are now invalid. Calling this ensures that the destructors of the |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 97 | * context and any of its created resource objects will not make backend 3D API calls. Content |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 98 | * rendered but not previously flushed may be lost. After this function is called all subsequent |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 99 | * calls on the context will fail or be no-ops. |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 100 | * |
| 101 | * The typical use case for this function is that the underlying 3D context was lost and further |
| 102 | * API calls may crash. |
| 103 | * |
| 104 | * For Vulkan, even if the device becomes lost, the VkQueue, VkDevice, or VkInstance used to |
| 105 | * create the context must be kept alive even after abandoning the context. Those objects must |
| 106 | * live for the lifetime of the context object itself. The reason for this is so that |
| 107 | * we can continue to delete any outstanding GrBackendTextures/RenderTargets which must be |
| 108 | * cleaned up even in a device lost state. |
| 109 | */ |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 110 | void abandonContext() override; |
| 111 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 112 | /** |
| 113 | * Returns true if the context was abandoned or if the if the backend specific context has |
| 114 | * gotten into an unrecoverarble, lost state (e.g. in Vulkan backend if we've gotten a |
| 115 | * VK_ERROR_DEVICE_LOST). If the backend context is lost, this call will also abandon the |
| 116 | * GrContext. |
| 117 | */ |
| 118 | bool abandoned() override; |
| 119 | |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 120 | // TODO: Remove this from public after migrating Chrome. |
| 121 | sk_sp<GrContextThreadSafeProxy> threadSafeProxy(); |
| 122 | |
| 123 | /** |
| 124 | * Checks if the underlying 3D API reported an out-of-memory error. If this returns true it is |
| 125 | * reset and will return false until another out-of-memory error is reported by the 3D API. If |
| 126 | * the context is abandoned then this will report false. |
| 127 | * |
| 128 | * Currently this is implemented for: |
| 129 | * |
| 130 | * OpenGL [ES] - Note that client calls to glGetError() may swallow GL_OUT_OF_MEMORY errors and |
| 131 | * therefore hide the error from Skia. Also, it is not advised to use this in combination with |
| 132 | * enabling GrContextOptions::fSkipGLErrorChecks. That option may prevent the context from ever |
| 133 | * checking the GL context for OOM. |
| 134 | * |
| 135 | * Vulkan - Reports true if VK_ERROR_OUT_OF_HOST_MEMORY or VK_ERROR_OUT_OF_DEVICE_MEMORY has |
| 136 | * occurred. |
| 137 | */ |
| 138 | bool oomed(); |
| 139 | |
| 140 | /** |
| 141 | * This is similar to abandonContext() however the underlying 3D context is not yet lost and |
| 142 | * the context will cleanup all allocated resources before returning. After returning it will |
| 143 | * assume that the underlying context may no longer be valid. |
| 144 | * |
| 145 | * The typical use case for this function is that the client is going to destroy the 3D context |
| 146 | * but can't guarantee that context will be destroyed first (perhaps because it may be ref'ed |
| 147 | * elsewhere by either the client or Skia objects). |
| 148 | * |
| 149 | * For Vulkan, even if the device becomes lost, the VkQueue, VkDevice, or VkInstance used to |
| 150 | * create the context must be alive before calling releaseResourcesAndAbandonContext. |
| 151 | */ |
| 152 | void releaseResourcesAndAbandonContext(); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 153 | |
Adlai Holler | 3a508e9 | 2020-10-12 13:58:01 -0400 | [diff] [blame] | 154 | /////////////////////////////////////////////////////////////////////////// |
| 155 | // Resource Cache |
| 156 | |
| 157 | /** DEPRECATED |
| 158 | * Return the current GPU resource cache limits. |
| 159 | * |
| 160 | * @param maxResources If non-null, will be set to -1. |
| 161 | * @param maxResourceBytes If non-null, returns maximum number of bytes of |
| 162 | * video memory that can be held in the cache. |
| 163 | */ |
| 164 | void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const; |
| 165 | |
| 166 | /** |
| 167 | * Return the current GPU resource cache limit in bytes. |
| 168 | */ |
| 169 | size_t getResourceCacheLimit() const; |
| 170 | |
| 171 | /** |
| 172 | * Gets the current GPU resource cache usage. |
| 173 | * |
| 174 | * @param resourceCount If non-null, returns the number of resources that are held in the |
| 175 | * cache. |
| 176 | * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held |
| 177 | * in the cache. |
| 178 | */ |
| 179 | void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const; |
| 180 | |
| 181 | /** |
| 182 | * Gets the number of bytes in the cache consumed by purgeable (e.g. unlocked) resources. |
| 183 | */ |
| 184 | size_t getResourceCachePurgeableBytes() const; |
| 185 | |
| 186 | /** DEPRECATED |
| 187 | * Specify the GPU resource cache limits. If the current cache exceeds the maxResourceBytes |
| 188 | * limit, it will be purged (LRU) to keep the cache within the limit. |
| 189 | * |
| 190 | * @param maxResources Unused. |
| 191 | * @param maxResourceBytes The maximum number of bytes of video memory |
| 192 | * that can be held in the cache. |
| 193 | */ |
| 194 | void setResourceCacheLimits(int maxResources, size_t maxResourceBytes); |
| 195 | |
| 196 | /** |
| 197 | * Specify the GPU resource cache limit. If the cache currently exceeds this limit, |
| 198 | * it will be purged (LRU) to keep the cache within the limit. |
| 199 | * |
| 200 | * @param maxResourceBytes The maximum number of bytes of video memory |
| 201 | * that can be held in the cache. |
| 202 | */ |
| 203 | void setResourceCacheLimit(size_t maxResourceBytes); |
| 204 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 205 | /** |
| 206 | * Frees GPU created by the context. Can be called to reduce GPU memory |
| 207 | * pressure. |
| 208 | */ |
| 209 | void freeGpuResources(); |
| 210 | |
| 211 | /** |
| 212 | * Purge GPU resources that haven't been used in the past 'msNotUsed' milliseconds or are |
| 213 | * otherwise marked for deletion, regardless of whether the context is under budget. |
| 214 | */ |
| 215 | void performDeferredCleanup(std::chrono::milliseconds msNotUsed); |
| 216 | |
| 217 | // Temporary compatibility API for Android. |
| 218 | void purgeResourcesNotUsedInMs(std::chrono::milliseconds msNotUsed) { |
| 219 | this->performDeferredCleanup(msNotUsed); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Purge unlocked resources from the cache until the the provided byte count has been reached |
| 224 | * or we have purged all unlocked resources. The default policy is to purge in LRU order, but |
| 225 | * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other |
| 226 | * resource types. |
| 227 | * |
| 228 | * @param maxBytesToPurge the desired number of bytes to be purged. |
| 229 | * @param preferScratchResources If true scratch resources will be purged prior to other |
| 230 | * resource types. |
| 231 | */ |
| 232 | void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources); |
| 233 | |
| 234 | /** |
| 235 | * This entry point is intended for instances where an app has been backgrounded or |
| 236 | * suspended. |
| 237 | * If 'scratchResourcesOnly' is true all unlocked scratch resources will be purged but the |
| 238 | * unlocked resources with persistent data will remain. If 'scratchResourcesOnly' is false |
| 239 | * then all unlocked resources will be purged. |
| 240 | * In either case, after the unlocked resources are purged a separate pass will be made to |
| 241 | * ensure that resource usage is under budget (i.e., even if 'scratchResourcesOnly' is true |
| 242 | * some resources with persistent data may be purged to be under budget). |
| 243 | * |
| 244 | * @param scratchResourcesOnly If true only unlocked scratch resources will be purged prior |
| 245 | * enforcing the budget requirements. |
| 246 | */ |
| 247 | void purgeUnlockedResources(bool scratchResourcesOnly); |
| 248 | |
| 249 | /** |
| 250 | * Gets the maximum supported texture size. |
| 251 | */ |
| 252 | using GrRecordingContext::maxTextureSize; |
| 253 | |
| 254 | /** |
| 255 | * Gets the maximum supported render target size. |
| 256 | */ |
| 257 | using GrRecordingContext::maxRenderTargetSize; |
| 258 | |
| 259 | /** |
| 260 | * Can a SkImage be created with the given color type. |
| 261 | */ |
| 262 | using GrRecordingContext::colorTypeSupportedAsImage; |
| 263 | |
| 264 | /** |
| 265 | * Can a SkSurface be created with the given color type. To check whether MSAA is supported |
| 266 | * use maxSurfaceSampleCountForColorType(). |
| 267 | */ |
| 268 | using GrRecordingContext::colorTypeSupportedAsSurface; |
| 269 | |
| 270 | /** |
| 271 | * Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA |
| 272 | * rendering is supported for the color type. 0 is returned if rendering to this color type |
| 273 | * is not supported at all. |
| 274 | */ |
| 275 | using GrRecordingContext::maxSurfaceSampleCountForColorType; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 276 | |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 277 | /////////////////////////////////////////////////////////////////////////// |
| 278 | // Misc. |
| 279 | |
| 280 | /** |
| 281 | * Inserts a list of GPU semaphores that the current GPU-backed API must wait on before |
| 282 | * executing any more commands on the GPU. If this call returns false, then the GPU back-end |
| 283 | * will not wait on any passed in semaphores, and the client will still own the semaphores, |
| 284 | * regardless of the value of deleteSemaphoresAfterWait. |
| 285 | * |
| 286 | * If deleteSemaphoresAfterWait is false then Skia will not delete the semaphores. In this case |
| 287 | * it is the client's responsibility to not destroy or attempt to reuse the semaphores until it |
| 288 | * knows that Skia has finished waiting on them. This can be done by using finishedProcs on |
| 289 | * flush calls. |
| 290 | */ |
| 291 | bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores, |
| 292 | bool deleteSemaphoresAfterWait = true); |
| 293 | |
| 294 | /** |
| 295 | * Call to ensure all drawing to the context has been flushed and submitted to the underlying 3D |
| 296 | * API. This is equivalent to calling GrContext::flush with a default GrFlushInfo followed by |
| 297 | * GrContext::submit(syncCpu). |
| 298 | */ |
| 299 | void flushAndSubmit(bool syncCpu = false) { |
| 300 | this->flush(GrFlushInfo()); |
| 301 | this->submit(syncCpu); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Call to ensure all drawing to the context has been flushed to underlying 3D API specific |
| 306 | * objects. A call to `submit` is always required to ensure work is actually sent to |
| 307 | * the gpu. Some specific API details: |
| 308 | * GL: Commands are actually sent to the driver, but glFlush is never called. Thus some |
| 309 | * sync objects from the flush will not be valid until a submission occurs. |
| 310 | * |
| 311 | * Vulkan/Metal/D3D/Dawn: Commands are recorded to the backend APIs corresponding command |
| 312 | * buffer or encoder objects. However, these objects are not sent to the gpu until a |
| 313 | * submission occurs. |
| 314 | * |
| 315 | * If the return is GrSemaphoresSubmitted::kYes, only initialized GrBackendSemaphores will be |
| 316 | * submitted to the gpu during the next submit call (it is possible Skia failed to create a |
| 317 | * subset of the semaphores). The client should not wait on these semaphores until after submit |
| 318 | * has been called, and must keep them alive until then. If this call returns |
| 319 | * GrSemaphoresSubmitted::kNo, the GPU backend will not submit any semaphores to be signaled on |
| 320 | * the GPU. Thus the client should not have the GPU wait on any of the semaphores passed in with |
| 321 | * the GrFlushInfo. Regardless of whether semaphores were submitted to the GPU or not, the |
| 322 | * client is still responsible for deleting any initialized semaphores. |
| 323 | * Regardleess of semaphore submission the context will still be flushed. It should be |
| 324 | * emphasized that a return value of GrSemaphoresSubmitted::kNo does not mean the flush did not |
| 325 | * happen. It simply means there were no semaphores submitted to the GPU. A caller should only |
| 326 | * take this as a failure if they passed in semaphores to be submitted. |
| 327 | */ |
| 328 | GrSemaphoresSubmitted flush(const GrFlushInfo& info); |
| 329 | |
| 330 | void flush() { this->flush({}); } |
| 331 | |
| 332 | /** |
| 333 | * Submit outstanding work to the gpu from all previously un-submitted flushes. The return |
| 334 | * value of the submit will indicate whether or not the submission to the GPU was successful. |
| 335 | * |
| 336 | * If the call returns true, all previously passed in semaphores in flush calls will have been |
| 337 | * submitted to the GPU and they can safely be waited on. The caller should wait on those |
| 338 | * semaphores or perform some other global synchronization before deleting the semaphores. |
| 339 | * |
| 340 | * If it returns false, then those same semaphores will not have been submitted and we will not |
| 341 | * try to submit them again. The caller is free to delete the semaphores at any time. |
| 342 | * |
| 343 | * If the syncCpu flag is true this function will return once the gpu has finished with all |
| 344 | * submitted work. |
| 345 | */ |
| 346 | bool submit(bool syncCpu = false); |
| 347 | |
| 348 | /** |
| 349 | * Checks whether any asynchronous work is complete and if so calls related callbacks. |
| 350 | */ |
| 351 | void checkAsyncWorkCompletion(); |
| 352 | |
| 353 | /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */ |
| 354 | // Chrome is using this! |
| 355 | void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 356 | |
| 357 | bool supportsDistanceFieldText() const; |
| 358 | |
| 359 | void storeVkPipelineCacheData(); |
| 360 | |
| 361 | // Returns the gpu memory size of the the texture that backs the passed in SkImage. Returns 0 if |
| 362 | // the SkImage is not texture backed. For external format textures this will also return 0 as we |
| 363 | // cannot determine the correct size. |
| 364 | static size_t ComputeImageSize(sk_sp<SkImage> image, GrMipmapped, bool useNextPow2 = false); |
| 365 | |
| 366 | /** |
| 367 | * Retrieve the default GrBackendFormat for a given SkColorType and renderability. |
| 368 | * It is guaranteed that this backend format will be the one used by the following |
| 369 | * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods. |
| 370 | * |
| 371 | * The caller should check that the returned format is valid. |
| 372 | */ |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 373 | using GrRecordingContext::defaultBackendFormat; |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * The explicitly allocated backend texture API allows clients to use Skia to create backend |
| 377 | * objects outside of Skia proper (i.e., Skia's caching system will not know about them.) |
| 378 | * |
| 379 | * It is the client's responsibility to delete all these objects (using deleteBackendTexture) |
| 380 | * before deleting the context used to create them. If the backend is Vulkan, the textures must |
| 381 | * be deleted before abandoning the context as well. Additionally, clients should only delete |
| 382 | * these objects on the thread for which that context is active. |
| 383 | * |
| 384 | * The client is responsible for ensuring synchronization between different uses |
| 385 | * of the backend object (i.e., wrapping it in a surface, rendering to it, deleting the |
| 386 | * surface, rewrapping it in a image and drawing the image will require explicit |
| 387 | * synchronization on the client's part). |
| 388 | */ |
| 389 | |
| 390 | /** |
| 391 | * If possible, create an uninitialized backend texture. The client should ensure that the |
| 392 | * returned backend texture is valid. |
| 393 | * For the Vulkan backend the layout of the created VkImage will be: |
| 394 | * VK_IMAGE_LAYOUT_UNDEFINED. |
| 395 | */ |
| 396 | GrBackendTexture createBackendTexture(int width, int height, |
| 397 | const GrBackendFormat&, |
| 398 | GrMipmapped, |
| 399 | GrRenderable, |
| 400 | GrProtected = GrProtected::kNo); |
| 401 | |
| 402 | /** |
| 403 | * If possible, create an uninitialized backend texture. The client should ensure that the |
| 404 | * returned backend texture is valid. |
| 405 | * If successful, the created backend texture will be compatible with the provided |
| 406 | * SkColorType. |
| 407 | * For the Vulkan backend the layout of the created VkImage will be: |
| 408 | * VK_IMAGE_LAYOUT_UNDEFINED. |
| 409 | */ |
| 410 | GrBackendTexture createBackendTexture(int width, int height, |
| 411 | SkColorType, |
| 412 | GrMipmapped, |
| 413 | GrRenderable, |
| 414 | GrProtected = GrProtected::kNo); |
| 415 | |
| 416 | /** |
| 417 | * If possible, create a backend texture initialized to a particular color. The client should |
| 418 | * ensure that the returned backend texture is valid. The client can pass in a finishedProc |
| 419 | * to be notified when the data has been uploaded by the gpu and the texture can be deleted. The |
| 420 | * client is required to call `submit` to send the upload work to the gpu. The |
| 421 | * finishedProc will always get called even if we failed to create the GrBackendTexture. |
| 422 | * For the Vulkan backend the layout of the created VkImage will be: |
| 423 | * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 424 | */ |
| 425 | GrBackendTexture createBackendTexture(int width, int height, |
| 426 | const GrBackendFormat&, |
| 427 | const SkColor4f& color, |
| 428 | GrMipmapped, |
| 429 | GrRenderable, |
| 430 | GrProtected = GrProtected::kNo, |
| 431 | GrGpuFinishedProc finishedProc = nullptr, |
| 432 | GrGpuFinishedContext finishedContext = nullptr); |
| 433 | |
| 434 | /** |
| 435 | * If possible, create a backend texture initialized to a particular color. The client should |
| 436 | * ensure that the returned backend texture is valid. The client can pass in a finishedProc |
| 437 | * to be notified when the data has been uploaded by the gpu and the texture can be deleted. The |
| 438 | * client is required to call `submit` to send the upload work to the gpu. The |
| 439 | * finishedProc will always get called even if we failed to create the GrBackendTexture. |
| 440 | * If successful, the created backend texture will be compatible with the provided |
| 441 | * SkColorType. |
| 442 | * For the Vulkan backend the layout of the created VkImage will be: |
| 443 | * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 444 | */ |
| 445 | GrBackendTexture createBackendTexture(int width, int height, |
| 446 | SkColorType, |
| 447 | const SkColor4f& color, |
| 448 | GrMipmapped, |
| 449 | GrRenderable, |
| 450 | GrProtected = GrProtected::kNo, |
| 451 | GrGpuFinishedProc finishedProc = nullptr, |
| 452 | GrGpuFinishedContext finishedContext = nullptr); |
| 453 | |
| 454 | /** |
| 455 | * If possible, create a backend texture initialized with the provided pixmap data. The client |
| 456 | * should ensure that the returned backend texture is valid. The client can pass in a |
| 457 | * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be |
| 458 | * deleted. The client is required to call `submit` to send the upload work to the gpu. |
| 459 | * The finishedProc will always get called even if we failed to create the GrBackendTexture. |
| 460 | * If successful, the created backend texture will be compatible with the provided |
| 461 | * pixmap(s). Compatible, in this case, means that the backend format will be the result |
| 462 | * of calling defaultBackendFormat on the base pixmap's colortype. The src data can be deleted |
| 463 | * when this call returns. |
| 464 | * If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired |
| 465 | * the data for all the mipmap levels must be provided. In the mipmapped case all the |
| 466 | * colortypes of the provided pixmaps must be the same. Additionally, all the miplevels |
| 467 | * must be sized correctly (please see SkMipmap::ComputeLevelSize and ComputeLevelCount). |
| 468 | * Note: the pixmap's alphatypes and colorspaces are ignored. |
| 469 | * For the Vulkan backend the layout of the created VkImage will be: |
| 470 | * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 471 | */ |
| 472 | GrBackendTexture createBackendTexture(const SkPixmap srcData[], int numLevels, |
| 473 | GrRenderable, GrProtected, |
| 474 | GrGpuFinishedProc finishedProc = nullptr, |
| 475 | GrGpuFinishedContext finishedContext = nullptr); |
| 476 | |
| 477 | // Helper version of above for a single level. |
| 478 | GrBackendTexture createBackendTexture(const SkPixmap& srcData, |
| 479 | GrRenderable renderable, |
| 480 | GrProtected isProtected, |
| 481 | GrGpuFinishedProc finishedProc = nullptr, |
| 482 | GrGpuFinishedContext finishedContext = nullptr) { |
| 483 | return this->createBackendTexture(&srcData, 1, renderable, isProtected, finishedProc, |
| 484 | finishedContext); |
| 485 | } |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 486 | |
| 487 | /** |
| 488 | * If possible, updates a backend texture to be filled to a particular color. The client should |
| 489 | * check the return value to see if the update was successful. The client can pass in a |
| 490 | * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be |
| 491 | * deleted. The client is required to call `submit` to send the upload work to the gpu. |
| 492 | * The finishedProc will always get called even if we failed to update the GrBackendTexture. |
| 493 | * For the Vulkan backend after a successful update the layout of the created VkImage will be: |
| 494 | * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 495 | */ |
| 496 | bool updateBackendTexture(const GrBackendTexture&, |
| 497 | const SkColor4f& color, |
| 498 | GrGpuFinishedProc finishedProc, |
| 499 | GrGpuFinishedContext finishedContext); |
| 500 | |
| 501 | /** |
| 502 | * If possible, updates a backend texture to be filled to a particular color. The data in |
| 503 | * GrBackendTexture and passed in color is interpreted with respect to the passed in |
| 504 | * SkColorType. The client should check the return value to see if the update was successful. |
| 505 | * The client can pass in a finishedProc to be notified when the data has been uploaded by the |
| 506 | * gpu and the texture can be deleted. The client is required to call `submit` to send |
| 507 | * the upload work to the gpu. The finishedProc will always get called even if we failed to |
| 508 | * update the GrBackendTexture. |
| 509 | * For the Vulkan backend after a successful update the layout of the created VkImage will be: |
| 510 | * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 511 | */ |
| 512 | bool updateBackendTexture(const GrBackendTexture&, |
| 513 | SkColorType skColorType, |
| 514 | const SkColor4f& color, |
| 515 | GrGpuFinishedProc finishedProc, |
| 516 | GrGpuFinishedContext finishedContext); |
| 517 | |
| 518 | /** |
| 519 | * If possible, updates a backend texture filled with the provided pixmap data. The client |
| 520 | * should check the return value to see if the update was successful. The client can pass in a |
| 521 | * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be |
| 522 | * deleted. The client is required to call `submit` to send the upload work to the gpu. |
| 523 | * The finishedProc will always get called even if we failed to create the GrBackendTexture. |
| 524 | * The backend texture must be compatible with the provided pixmap(s). Compatible, in this case, |
| 525 | * means that the backend format is compatible with the base pixmap's colortype. The src data |
| 526 | * can be deleted when this call returns. |
| 527 | * If the backend texture is mip mapped, the data for all the mipmap levels must be provided. |
| 528 | * In the mipmapped case all the colortypes of the provided pixmaps must be the same. |
| 529 | * Additionally, all the miplevels must be sized correctly (please see |
| 530 | * SkMipmap::ComputeLevelSize and ComputeLevelCount). |
| 531 | * Note: the pixmap's alphatypes and colorspaces are ignored. |
| 532 | * For the Vulkan backend after a successful update the layout of the created VkImage will be: |
| 533 | * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL |
| 534 | */ |
| 535 | bool updateBackendTexture(const GrBackendTexture&, |
| 536 | const SkPixmap srcData[], |
| 537 | int numLevels, |
| 538 | GrGpuFinishedProc finishedProc, |
| 539 | GrGpuFinishedContext finishedContext); |
| 540 | |
| 541 | /** |
| 542 | * Retrieve the GrBackendFormat for a given SkImage::CompressionType. This is |
| 543 | * guaranteed to match the backend format used by the following |
| 544 | * createCompressedBackendTexture methods that take a CompressionType. |
| 545 | * The caller should check that the returned format is valid. |
| 546 | */ |
| 547 | using GrRecordingContext::compressedBackendFormat; |
| 548 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 549 | protected: |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 550 | GrDirectContext(GrBackendApi backend, const GrContextOptions& options); |
| 551 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 552 | bool init() override; |
| 553 | |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 554 | GrAtlasManager* onGetAtlasManager() override { return fAtlasManager.get(); } |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 555 | GrSmallPathAtlasMgr* onGetSmallPathAtlasMgr() override; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 556 | |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 557 | GrDirectContext* asDirectContext() override { return this; } |
| 558 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 559 | private: |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 560 | std::unique_ptr<GrAtlasManager> fAtlasManager; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 561 | |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 562 | std::unique_ptr<GrSmallPathAtlasMgr> fSmallPathAtlasMgr; |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 563 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 564 | using INHERITED = GrContext; |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 565 | }; |
| 566 | |
| 567 | |
| 568 | #endif |