blob: 77540251b9802a67816e4fb2966a4a4101ce421b [file] [log] [blame]
Robert Phillipsad248452020-06-30 09:27:52 -04001/*
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 Holler9ae860a2020-10-20 10:13:32 -040011#include "include/gpu/GrRecordingContext.h"
Robert Phillipsad248452020-06-30 09:27:52 -040012
Adlai Holler6d0745b2020-10-13 13:29:00 -040013#include "include/gpu/GrBackendSurface.h"
14
15// We shouldn't need this but currently Android is relying on this being include transitively.
16#include "include/core/SkUnPreMultiply.h"
17
Adlai Holler53cf44c2020-10-13 17:40:21 -040018class GrAtlasManager;
Adlai Holler6d0745b2020-10-13 13:29:00 -040019class GrBackendSemaphore;
Adlai Holler53cf44c2020-10-13 17:40:21 -040020class GrClientMappedBufferManager;
Adlai Hollera0693042020-10-14 11:23:11 -040021class GrDirectContextPriv;
Adlai Holler6d0745b2020-10-13 13:29:00 -040022class GrContextThreadSafeProxy;
23struct GrD3DBackendContext;
24class GrFragmentProcessor;
Adlai Holler53cf44c2020-10-13 17:40:21 -040025class GrGpu;
Adlai Holler6d0745b2020-10-13 13:29:00 -040026struct GrGLInterface;
Jim Van Verth351c9b52020-11-12 15:21:11 -050027struct GrMtlBackendContext;
Adlai Holler6d0745b2020-10-13 13:29:00 -040028struct GrMockOptions;
29class GrPath;
Adlai Holler53cf44c2020-10-13 17:40:21 -040030class GrResourceCache;
31class GrSmallPathAtlasMgr;
Brian Salomoneebe7352020-12-09 16:37:04 -050032class GrSurfaceDrawContext;
Adlai Holler53cf44c2020-10-13 17:40:21 -040033class GrResourceProvider;
34class GrStrikeCache;
Adlai Holler6d0745b2020-10-13 13:29:00 -040035class GrSurfaceProxy;
36class GrSwizzle;
37class GrTextureProxy;
38struct GrVkBackendContext;
39
40class SkImage;
41class SkString;
42class SkSurfaceCharacterization;
43class SkSurfaceProps;
Adlai Holler53cf44c2020-10-13 17:40:21 -040044class SkTaskGroup;
Adlai Holler6d0745b2020-10-13 13:29:00 -040045class SkTraceMemoryDump;
Robert Phillipsad248452020-06-30 09:27:52 -040046
Adlai Holler9ae860a2020-10-20 10:13:32 -040047class SK_API GrDirectContext : public GrRecordingContext {
Robert Phillipsad248452020-06-30 09:27:52 -040048public:
Robert Phillipsf4f80112020-07-13 16:13:31 -040049#ifdef SK_GL
50 /**
51 * Creates a GrDirectContext for a backend context. If no GrGLInterface is provided then the
52 * result of GrGLMakeNativeInterface() is used if it succeeds.
53 */
54 static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>, const GrContextOptions&);
55 static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>);
56 static sk_sp<GrDirectContext> MakeGL(const GrContextOptions&);
57 static sk_sp<GrDirectContext> MakeGL();
58#endif
59
60#ifdef SK_VULKAN
61 /**
62 * The Vulkan context (VkQueue, VkDevice, VkInstance) must be kept alive until the returned
63 * GrDirectContext is destroyed. This also means that any objects created with this
64 * GrDirectContext (e.g. SkSurfaces, SkImages, etc.) must also be released as they may hold
65 * refs on the GrDirectContext. Once all these objects and the GrDirectContext are released,
66 * then it is safe to delete the vulkan objects.
67 */
68 static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&, const GrContextOptions&);
69 static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&);
70#endif
71
72#ifdef SK_METAL
73 /**
Jim Van Verth351c9b52020-11-12 15:21:11 -050074 * Makes a GrDirectContext which uses Metal as the backend. The GrMtlBackendContext contains a
75 * MTLDevice and MTLCommandQueue which should be used by the backend. These objects must
76 * have their own ref which will be released when the GrMtlBackendContext is destroyed.
77 * Ganesh will take its own ref on the objects which will be released when the GrDirectContext
78 * is destroyed.
79 */
80 static sk_sp<GrDirectContext> MakeMetal(const GrMtlBackendContext&, const GrContextOptions&);
81 static sk_sp<GrDirectContext> MakeMetal(const GrMtlBackendContext&);
82 /**
83 * Deprecated.
84 *
Robert Phillipsf4f80112020-07-13 16:13:31 -040085 * Makes a GrDirectContext which uses Metal as the backend. The device parameter is an
86 * MTLDevice and queue is an MTLCommandQueue which should be used by the backend. These objects
Jim Van Verth351c9b52020-11-12 15:21:11 -050087 * must have a ref on them that can be transferred to Ganesh, which will release the ref
Robert Phillipsf4f80112020-07-13 16:13:31 -040088 * when the GrDirectContext is destroyed.
89 */
90 static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue, const GrContextOptions&);
91 static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue);
92#endif
93
94#ifdef SK_DIRECT3D
95 /**
96 * Makes a GrDirectContext which uses Direct3D as the backend. The Direct3D context
97 * must be kept alive until the returned GrDirectContext is first destroyed or abandoned.
98 */
99 static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&, const GrContextOptions&);
100 static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&);
101#endif
102
103#ifdef SK_DAWN
104 static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&,
105 const GrContextOptions&);
106 static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&);
107#endif
108
109 static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*, const GrContextOptions&);
110 static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*);
Robert Phillipsad248452020-06-30 09:27:52 -0400111
112 ~GrDirectContext() override;
113
Adlai Hollera7a40442020-10-09 09:49:42 -0400114 /**
115 * The context normally assumes that no outsider is setting state
116 * within the underlying 3D API's context/device/whatever. This call informs
117 * the context that the state was modified and it should resend. Shouldn't
118 * be called frequently for good performance.
119 * The flag bits, state, is dependent on which backend is used by the
120 * context, either GL or D3D (possible in future).
121 */
122 void resetContext(uint32_t state = kAll_GrBackendState);
123
124 /**
125 * If the backend is GrBackendApi::kOpenGL, then all texture unit/target combinations for which
126 * the context has modified the bound texture will have texture id 0 bound. This does not
127 * flush the context. Calling resetContext() does not change the set that will be bound
128 * to texture id 0 on the next call to resetGLTextureBindings(). After this is called
129 * all unit/target combinations are considered to have unmodified bindings until the context
130 * subsequently modifies them (meaning if this is called twice in a row with no intervening
131 * context usage then the second call is a no-op.)
132 */
133 void resetGLTextureBindings();
134
135 /**
136 * Abandons all GPU resources and assumes the underlying backend 3D API context is no longer
137 * usable. Call this if you have lost the associated GPU context, and thus internal texture,
138 * buffer, etc. references/IDs are now invalid. Calling this ensures that the destructors of the
Adlai Holler98dd0042020-10-13 10:04:00 -0400139 * context and any of its created resource objects will not make backend 3D API calls. Content
Adlai Hollera7a40442020-10-09 09:49:42 -0400140 * rendered but not previously flushed may be lost. After this function is called all subsequent
Adlai Holler98dd0042020-10-13 10:04:00 -0400141 * calls on the context will fail or be no-ops.
Adlai Hollera7a40442020-10-09 09:49:42 -0400142 *
143 * The typical use case for this function is that the underlying 3D context was lost and further
144 * API calls may crash.
145 *
146 * For Vulkan, even if the device becomes lost, the VkQueue, VkDevice, or VkInstance used to
147 * create the context must be kept alive even after abandoning the context. Those objects must
148 * live for the lifetime of the context object itself. The reason for this is so that
149 * we can continue to delete any outstanding GrBackendTextures/RenderTargets which must be
150 * cleaned up even in a device lost state.
151 */
Robert Phillipsad248452020-06-30 09:27:52 -0400152 void abandonContext() override;
153
Adlai Hollera7a40442020-10-09 09:49:42 -0400154 /**
155 * Returns true if the context was abandoned or if the if the backend specific context has
156 * gotten into an unrecoverarble, lost state (e.g. in Vulkan backend if we've gotten a
Adlai Holler64e13832020-10-13 08:21:56 -0400157 * VK_ERROR_DEVICE_LOST). If the backend context is lost, this call will also abandon this
158 * context.
Adlai Hollera7a40442020-10-09 09:49:42 -0400159 */
160 bool abandoned() override;
161
Adlai Holler61a591c2020-10-12 12:38:33 -0400162 // TODO: Remove this from public after migrating Chrome.
163 sk_sp<GrContextThreadSafeProxy> threadSafeProxy();
164
165 /**
166 * Checks if the underlying 3D API reported an out-of-memory error. If this returns true it is
167 * reset and will return false until another out-of-memory error is reported by the 3D API. If
168 * the context is abandoned then this will report false.
169 *
170 * Currently this is implemented for:
171 *
172 * OpenGL [ES] - Note that client calls to glGetError() may swallow GL_OUT_OF_MEMORY errors and
173 * therefore hide the error from Skia. Also, it is not advised to use this in combination with
174 * enabling GrContextOptions::fSkipGLErrorChecks. That option may prevent the context from ever
175 * checking the GL context for OOM.
176 *
177 * Vulkan - Reports true if VK_ERROR_OUT_OF_HOST_MEMORY or VK_ERROR_OUT_OF_DEVICE_MEMORY has
178 * occurred.
179 */
180 bool oomed();
181
182 /**
183 * This is similar to abandonContext() however the underlying 3D context is not yet lost and
184 * the context will cleanup all allocated resources before returning. After returning it will
185 * assume that the underlying context may no longer be valid.
186 *
187 * The typical use case for this function is that the client is going to destroy the 3D context
188 * but can't guarantee that context will be destroyed first (perhaps because it may be ref'ed
189 * elsewhere by either the client or Skia objects).
190 *
191 * For Vulkan, even if the device becomes lost, the VkQueue, VkDevice, or VkInstance used to
192 * create the context must be alive before calling releaseResourcesAndAbandonContext.
193 */
194 void releaseResourcesAndAbandonContext();
Robert Phillipsad248452020-06-30 09:27:52 -0400195
Adlai Holler3a508e92020-10-12 13:58:01 -0400196 ///////////////////////////////////////////////////////////////////////////
197 // Resource Cache
198
199 /** DEPRECATED
200 * Return the current GPU resource cache limits.
201 *
202 * @param maxResources If non-null, will be set to -1.
203 * @param maxResourceBytes If non-null, returns maximum number of bytes of
204 * video memory that can be held in the cache.
205 */
206 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
207
208 /**
209 * Return the current GPU resource cache limit in bytes.
210 */
211 size_t getResourceCacheLimit() const;
212
213 /**
214 * Gets the current GPU resource cache usage.
215 *
216 * @param resourceCount If non-null, returns the number of resources that are held in the
217 * cache.
218 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
219 * in the cache.
220 */
221 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
222
223 /**
224 * Gets the number of bytes in the cache consumed by purgeable (e.g. unlocked) resources.
225 */
226 size_t getResourceCachePurgeableBytes() const;
227
228 /** DEPRECATED
229 * Specify the GPU resource cache limits. If the current cache exceeds the maxResourceBytes
230 * limit, it will be purged (LRU) to keep the cache within the limit.
231 *
232 * @param maxResources Unused.
233 * @param maxResourceBytes The maximum number of bytes of video memory
234 * that can be held in the cache.
235 */
236 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
237
238 /**
239 * Specify the GPU resource cache limit. If the cache currently exceeds this limit,
240 * it will be purged (LRU) to keep the cache within the limit.
241 *
242 * @param maxResourceBytes The maximum number of bytes of video memory
243 * that can be held in the cache.
244 */
245 void setResourceCacheLimit(size_t maxResourceBytes);
246
Adlai Holler4aa4c602020-10-12 13:58:52 -0400247 /**
248 * Frees GPU created by the context. Can be called to reduce GPU memory
249 * pressure.
250 */
251 void freeGpuResources();
252
253 /**
254 * Purge GPU resources that haven't been used in the past 'msNotUsed' milliseconds or are
255 * otherwise marked for deletion, regardless of whether the context is under budget.
256 */
257 void performDeferredCleanup(std::chrono::milliseconds msNotUsed);
258
259 // Temporary compatibility API for Android.
260 void purgeResourcesNotUsedInMs(std::chrono::milliseconds msNotUsed) {
261 this->performDeferredCleanup(msNotUsed);
262 }
263
264 /**
265 * Purge unlocked resources from the cache until the the provided byte count has been reached
266 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
267 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
268 * resource types.
269 *
270 * @param maxBytesToPurge the desired number of bytes to be purged.
271 * @param preferScratchResources If true scratch resources will be purged prior to other
272 * resource types.
273 */
274 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
275
276 /**
277 * This entry point is intended for instances where an app has been backgrounded or
278 * suspended.
279 * If 'scratchResourcesOnly' is true all unlocked scratch resources will be purged but the
280 * unlocked resources with persistent data will remain. If 'scratchResourcesOnly' is false
281 * then all unlocked resources will be purged.
282 * In either case, after the unlocked resources are purged a separate pass will be made to
283 * ensure that resource usage is under budget (i.e., even if 'scratchResourcesOnly' is true
284 * some resources with persistent data may be purged to be under budget).
285 *
286 * @param scratchResourcesOnly If true only unlocked scratch resources will be purged prior
287 * enforcing the budget requirements.
288 */
289 void purgeUnlockedResources(bool scratchResourcesOnly);
290
291 /**
292 * Gets the maximum supported texture size.
293 */
294 using GrRecordingContext::maxTextureSize;
295
296 /**
297 * Gets the maximum supported render target size.
298 */
299 using GrRecordingContext::maxRenderTargetSize;
300
301 /**
302 * Can a SkImage be created with the given color type.
303 */
304 using GrRecordingContext::colorTypeSupportedAsImage;
305
306 /**
307 * Can a SkSurface be created with the given color type. To check whether MSAA is supported
308 * use maxSurfaceSampleCountForColorType().
309 */
310 using GrRecordingContext::colorTypeSupportedAsSurface;
311
312 /**
313 * Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA
314 * rendering is supported for the color type. 0 is returned if rendering to this color type
315 * is not supported at all.
316 */
317 using GrRecordingContext::maxSurfaceSampleCountForColorType;
Robert Phillipsad248452020-06-30 09:27:52 -0400318
Adlai Holler3acc69a2020-10-13 08:20:51 -0400319 ///////////////////////////////////////////////////////////////////////////
320 // Misc.
321
322 /**
323 * Inserts a list of GPU semaphores that the current GPU-backed API must wait on before
324 * executing any more commands on the GPU. If this call returns false, then the GPU back-end
325 * will not wait on any passed in semaphores, and the client will still own the semaphores,
326 * regardless of the value of deleteSemaphoresAfterWait.
327 *
328 * If deleteSemaphoresAfterWait is false then Skia will not delete the semaphores. In this case
329 * it is the client's responsibility to not destroy or attempt to reuse the semaphores until it
330 * knows that Skia has finished waiting on them. This can be done by using finishedProcs on
331 * flush calls.
332 */
333 bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
334 bool deleteSemaphoresAfterWait = true);
335
336 /**
337 * Call to ensure all drawing to the context has been flushed and submitted to the underlying 3D
338 * API. This is equivalent to calling GrContext::flush with a default GrFlushInfo followed by
339 * GrContext::submit(syncCpu).
340 */
341 void flushAndSubmit(bool syncCpu = false) {
342 this->flush(GrFlushInfo());
343 this->submit(syncCpu);
344 }
345
346 /**
347 * Call to ensure all drawing to the context has been flushed to underlying 3D API specific
348 * objects. A call to `submit` is always required to ensure work is actually sent to
349 * the gpu. Some specific API details:
350 * GL: Commands are actually sent to the driver, but glFlush is never called. Thus some
351 * sync objects from the flush will not be valid until a submission occurs.
352 *
353 * Vulkan/Metal/D3D/Dawn: Commands are recorded to the backend APIs corresponding command
354 * buffer or encoder objects. However, these objects are not sent to the gpu until a
355 * submission occurs.
356 *
357 * If the return is GrSemaphoresSubmitted::kYes, only initialized GrBackendSemaphores will be
358 * submitted to the gpu during the next submit call (it is possible Skia failed to create a
359 * subset of the semaphores). The client should not wait on these semaphores until after submit
360 * has been called, and must keep them alive until then. If this call returns
361 * GrSemaphoresSubmitted::kNo, the GPU backend will not submit any semaphores to be signaled on
362 * the GPU. Thus the client should not have the GPU wait on any of the semaphores passed in with
363 * the GrFlushInfo. Regardless of whether semaphores were submitted to the GPU or not, the
364 * client is still responsible for deleting any initialized semaphores.
365 * Regardleess of semaphore submission the context will still be flushed. It should be
366 * emphasized that a return value of GrSemaphoresSubmitted::kNo does not mean the flush did not
367 * happen. It simply means there were no semaphores submitted to the GPU. A caller should only
368 * take this as a failure if they passed in semaphores to be submitted.
369 */
370 GrSemaphoresSubmitted flush(const GrFlushInfo& info);
371
372 void flush() { this->flush({}); }
373
374 /**
375 * Submit outstanding work to the gpu from all previously un-submitted flushes. The return
376 * value of the submit will indicate whether or not the submission to the GPU was successful.
377 *
378 * If the call returns true, all previously passed in semaphores in flush calls will have been
379 * submitted to the GPU and they can safely be waited on. The caller should wait on those
380 * semaphores or perform some other global synchronization before deleting the semaphores.
381 *
382 * If it returns false, then those same semaphores will not have been submitted and we will not
383 * try to submit them again. The caller is free to delete the semaphores at any time.
384 *
385 * If the syncCpu flag is true this function will return once the gpu has finished with all
386 * submitted work.
387 */
388 bool submit(bool syncCpu = false);
389
390 /**
391 * Checks whether any asynchronous work is complete and if so calls related callbacks.
392 */
393 void checkAsyncWorkCompletion();
394
395 /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
396 // Chrome is using this!
397 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
398
399 bool supportsDistanceFieldText() const;
400
401 void storeVkPipelineCacheData();
402
403 // Returns the gpu memory size of the the texture that backs the passed in SkImage. Returns 0 if
404 // the SkImage is not texture backed. For external format textures this will also return 0 as we
405 // cannot determine the correct size.
406 static size_t ComputeImageSize(sk_sp<SkImage> image, GrMipmapped, bool useNextPow2 = false);
407
408 /**
409 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
410 * It is guaranteed that this backend format will be the one used by the following
411 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
412 *
413 * The caller should check that the returned format is valid.
414 */
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400415 using GrRecordingContext::defaultBackendFormat;
Adlai Holler98dd0042020-10-13 10:04:00 -0400416
417 /**
418 * The explicitly allocated backend texture API allows clients to use Skia to create backend
419 * objects outside of Skia proper (i.e., Skia's caching system will not know about them.)
420 *
421 * It is the client's responsibility to delete all these objects (using deleteBackendTexture)
422 * before deleting the context used to create them. If the backend is Vulkan, the textures must
423 * be deleted before abandoning the context as well. Additionally, clients should only delete
424 * these objects on the thread for which that context is active.
425 *
426 * The client is responsible for ensuring synchronization between different uses
427 * of the backend object (i.e., wrapping it in a surface, rendering to it, deleting the
428 * surface, rewrapping it in a image and drawing the image will require explicit
429 * synchronization on the client's part).
430 */
431
432 /**
433 * If possible, create an uninitialized backend texture. The client should ensure that the
434 * returned backend texture is valid.
435 * For the Vulkan backend the layout of the created VkImage will be:
436 * VK_IMAGE_LAYOUT_UNDEFINED.
437 */
438 GrBackendTexture createBackendTexture(int width, int height,
439 const GrBackendFormat&,
440 GrMipmapped,
441 GrRenderable,
442 GrProtected = GrProtected::kNo);
443
444 /**
445 * If possible, create an uninitialized backend texture. The client should ensure that the
446 * returned backend texture is valid.
447 * If successful, the created backend texture will be compatible with the provided
448 * SkColorType.
449 * For the Vulkan backend the layout of the created VkImage will be:
450 * VK_IMAGE_LAYOUT_UNDEFINED.
451 */
452 GrBackendTexture createBackendTexture(int width, int height,
453 SkColorType,
454 GrMipmapped,
455 GrRenderable,
456 GrProtected = GrProtected::kNo);
457
458 /**
459 * If possible, create a backend texture initialized to a particular color. The client should
460 * ensure that the returned backend texture is valid. The client can pass in a finishedProc
461 * to be notified when the data has been uploaded by the gpu and the texture can be deleted. The
462 * client is required to call `submit` to send the upload work to the gpu. The
463 * finishedProc will always get called even if we failed to create the GrBackendTexture.
464 * For the Vulkan backend the layout of the created VkImage will be:
465 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
466 */
467 GrBackendTexture createBackendTexture(int width, int height,
468 const GrBackendFormat&,
469 const SkColor4f& color,
470 GrMipmapped,
471 GrRenderable,
472 GrProtected = GrProtected::kNo,
473 GrGpuFinishedProc finishedProc = nullptr,
474 GrGpuFinishedContext finishedContext = nullptr);
475
476 /**
477 * If possible, create a backend texture initialized to a particular color. The client should
478 * ensure that the returned backend texture is valid. The client can pass in a finishedProc
479 * to be notified when the data has been uploaded by the gpu and the texture can be deleted. The
480 * client is required to call `submit` to send the upload work to the gpu. The
481 * finishedProc will always get called even if we failed to create the GrBackendTexture.
482 * If successful, the created backend texture will be compatible with the provided
483 * SkColorType.
484 * For the Vulkan backend the layout of the created VkImage will be:
485 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
486 */
487 GrBackendTexture createBackendTexture(int width, int height,
488 SkColorType,
489 const SkColor4f& color,
490 GrMipmapped,
491 GrRenderable,
492 GrProtected = GrProtected::kNo,
493 GrGpuFinishedProc finishedProc = nullptr,
494 GrGpuFinishedContext finishedContext = nullptr);
495
496 /**
497 * If possible, create a backend texture initialized with the provided pixmap data. The client
498 * should ensure that the returned backend texture is valid. The client can pass in a
499 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
500 * deleted. The client is required to call `submit` to send the upload work to the gpu.
501 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
502 * If successful, the created backend texture will be compatible with the provided
503 * pixmap(s). Compatible, in this case, means that the backend format will be the result
504 * of calling defaultBackendFormat on the base pixmap's colortype. The src data can be deleted
505 * when this call returns.
506 * If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
507 * the data for all the mipmap levels must be provided. In the mipmapped case all the
508 * colortypes of the provided pixmaps must be the same. Additionally, all the miplevels
Brian Salomonb5f880a2020-12-07 11:30:16 -0500509 * must be sized correctly (please see SkMipmap::ComputeLevelSize and ComputeLevelCount). The
510 * GrSurfaceOrigin controls whether the pixmap data is vertically flipped in the texture.
Adlai Holler98dd0042020-10-13 10:04:00 -0400511 * Note: the pixmap's alphatypes and colorspaces are ignored.
512 * For the Vulkan backend the layout of the created VkImage will be:
513 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
514 */
Brian Salomonb5f880a2020-12-07 11:30:16 -0500515 GrBackendTexture createBackendTexture(const SkPixmap srcData[],
516 int numLevels,
517 GrSurfaceOrigin,
518 GrRenderable,
519 GrProtected,
Adlai Holler98dd0042020-10-13 10:04:00 -0400520 GrGpuFinishedProc finishedProc = nullptr,
521 GrGpuFinishedContext finishedContext = nullptr);
522
Brian Salomonb5f880a2020-12-07 11:30:16 -0500523 /**
524 * Convenience version createBackendTexture() that takes just a base level pixmap.
525 */
Adlai Holler98dd0042020-10-13 10:04:00 -0400526 GrBackendTexture createBackendTexture(const SkPixmap& srcData,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500527 GrSurfaceOrigin textureOrigin,
Adlai Holler98dd0042020-10-13 10:04:00 -0400528 GrRenderable renderable,
529 GrProtected isProtected,
530 GrGpuFinishedProc finishedProc = nullptr,
531 GrGpuFinishedContext finishedContext = nullptr) {
Brian Salomonb5f880a2020-12-07 11:30:16 -0500532 return this->createBackendTexture(&srcData, 1, textureOrigin, renderable, isProtected,
533 finishedProc, finishedContext);
Adlai Holler98dd0042020-10-13 10:04:00 -0400534 }
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400535
Brian Salomonb5f880a2020-12-07 11:30:16 -0500536 // Deprecated versions that do not take origin and assume top-left.
537 GrBackendTexture createBackendTexture(const SkPixmap srcData[],
538 int numLevels,
539 GrRenderable renderable,
540 GrProtected isProtected,
541 GrGpuFinishedProc finishedProc = nullptr,
542 GrGpuFinishedContext finishedContext = nullptr) {
543 return this->createBackendTexture(srcData,
544 numLevels,
545 kTopLeft_GrSurfaceOrigin,
546 renderable,
547 isProtected,
548 finishedProc,
549 finishedContext);
550 }
551 GrBackendTexture createBackendTexture(const SkPixmap& srcData,
552 GrRenderable renderable,
553 GrProtected isProtected,
554 GrGpuFinishedProc finishedProc = nullptr,
555 GrGpuFinishedContext finishedContext = nullptr) {
556 return this->createBackendTexture(&srcData,
557 1,
558 renderable,
559 isProtected,
560 finishedProc,
561 finishedContext);
562 }
563
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400564 /**
565 * If possible, updates a backend texture to be filled to a particular color. The client should
566 * check the return value to see if the update was successful. The client can pass in a
567 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
568 * deleted. The client is required to call `submit` to send the upload work to the gpu.
569 * The finishedProc will always get called even if we failed to update the GrBackendTexture.
570 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
571 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
572 */
573 bool updateBackendTexture(const GrBackendTexture&,
574 const SkColor4f& color,
575 GrGpuFinishedProc finishedProc,
576 GrGpuFinishedContext finishedContext);
577
578 /**
579 * If possible, updates a backend texture to be filled to a particular color. The data in
580 * GrBackendTexture and passed in color is interpreted with respect to the passed in
581 * SkColorType. The client should check the return value to see if the update was successful.
582 * The client can pass in a finishedProc to be notified when the data has been uploaded by the
583 * gpu and the texture can be deleted. The client is required to call `submit` to send
584 * the upload work to the gpu. The finishedProc will always get called even if we failed to
585 * update the GrBackendTexture.
586 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
587 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
588 */
589 bool updateBackendTexture(const GrBackendTexture&,
590 SkColorType skColorType,
591 const SkColor4f& color,
592 GrGpuFinishedProc finishedProc,
593 GrGpuFinishedContext finishedContext);
594
595 /**
596 * If possible, updates a backend texture filled with the provided pixmap data. The client
597 * should check the return value to see if the update was successful. The client can pass in a
598 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
599 * deleted. The client is required to call `submit` to send the upload work to the gpu.
600 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
601 * The backend texture must be compatible with the provided pixmap(s). Compatible, in this case,
602 * means that the backend format is compatible with the base pixmap's colortype. The src data
603 * can be deleted when this call returns.
604 * If the backend texture is mip mapped, the data for all the mipmap levels must be provided.
605 * In the mipmapped case all the colortypes of the provided pixmaps must be the same.
606 * Additionally, all the miplevels must be sized correctly (please see
Brian Salomonb5f880a2020-12-07 11:30:16 -0500607 * SkMipmap::ComputeLevelSize and ComputeLevelCount). The GrSurfaceOrigin controls whether the
608 * pixmap data is vertically flipped in the texture.
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400609 * Note: the pixmap's alphatypes and colorspaces are ignored.
610 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
611 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
612 */
613 bool updateBackendTexture(const GrBackendTexture&,
614 const SkPixmap srcData[],
615 int numLevels,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500616 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
617 GrGpuFinishedProc finishedProc = nullptr,
618 GrGpuFinishedContext finishedContext = nullptr);
619
620 /**
621 * Convenience version of updateBackendTexture that takes just a base level pixmap.
622 */
623 bool updateBackendTexture(const GrBackendTexture& texture,
624 const SkPixmap& srcData,
625 GrSurfaceOrigin textureOrigin = kTopLeft_GrSurfaceOrigin,
626 GrGpuFinishedProc finishedProc = nullptr,
627 GrGpuFinishedContext finishedContext = nullptr) {
628 return this->updateBackendTexture(texture,
629 &srcData,
630 1,
631 textureOrigin,
632 finishedProc,
633 finishedContext);
634 }
635
636 // Deprecated version that does not take origin and assumes top-left.
637 bool updateBackendTexture(const GrBackendTexture& texture,
638 const SkPixmap srcData[],
639 int numLevels,
640 GrGpuFinishedProc finishedProc,
641 GrGpuFinishedContext finishedContext) {
642 return this->updateBackendTexture(texture,
643 srcData,
644 numLevels,
645 kTopLeft_GrSurfaceOrigin,
646 finishedProc,
647 finishedContext);
648 }
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400649
650 /**
651 * Retrieve the GrBackendFormat for a given SkImage::CompressionType. This is
652 * guaranteed to match the backend format used by the following
653 * createCompressedBackendTexture methods that take a CompressionType.
654 * The caller should check that the returned format is valid.
655 */
656 using GrRecordingContext::compressedBackendFormat;
657
Adlai Holler64e13832020-10-13 08:21:56 -0400658 /**
659 *If possible, create a compressed backend texture initialized to a particular color. The
660 * client should ensure that the returned backend texture is valid. The client can pass in a
661 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
662 * deleted. The client is required to call `submit` to send the upload work to the gpu.
663 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
664 * For the Vulkan backend the layout of the created VkImage will be:
665 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
666 */
667 GrBackendTexture createCompressedBackendTexture(int width, int height,
668 const GrBackendFormat&,
669 const SkColor4f& color,
670 GrMipmapped,
671 GrProtected = GrProtected::kNo,
672 GrGpuFinishedProc finishedProc = nullptr,
673 GrGpuFinishedContext finishedContext = nullptr);
674
675 GrBackendTexture createCompressedBackendTexture(int width, int height,
676 SkImage::CompressionType,
677 const SkColor4f& color,
678 GrMipmapped,
679 GrProtected = GrProtected::kNo,
680 GrGpuFinishedProc finishedProc = nullptr,
681 GrGpuFinishedContext finishedContext = nullptr);
682
683 /**
684 * If possible, create a backend texture initialized with the provided raw data. The client
685 * should ensure that the returned backend texture is valid. The client can pass in a
686 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
687 * deleted. The client is required to call `submit` to send the upload work to the gpu.
688 * The finishedProc will always get called even if we failed to create the GrBackendTexture
689 * If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
690 * the data for all the mipmap levels must be provided. Additionally, all the miplevels
691 * must be sized correctly (please see SkMipmap::ComputeLevelSize and ComputeLevelCount).
692 * For the Vulkan backend the layout of the created VkImage will be:
693 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
694 */
695 GrBackendTexture createCompressedBackendTexture(int width, int height,
696 const GrBackendFormat&,
697 const void* data, size_t dataSize,
698 GrMipmapped,
699 GrProtected = GrProtected::kNo,
700 GrGpuFinishedProc finishedProc = nullptr,
701 GrGpuFinishedContext finishedContext = nullptr);
702
703 GrBackendTexture createCompressedBackendTexture(int width, int height,
704 SkImage::CompressionType,
705 const void* data, size_t dataSize,
706 GrMipmapped,
707 GrProtected = GrProtected::kNo,
708 GrGpuFinishedProc finishedProc = nullptr,
709 GrGpuFinishedContext finishedContext = nullptr);
710
711 /**
712 * If possible, updates a backend texture filled with the provided color. If the texture is
713 * mipmapped, all levels of the mip chain will be updated to have the supplied color. The client
714 * should check the return value to see if the update was successful. The client can pass in a
715 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
716 * deleted. The client is required to call `submit` to send the upload work to the gpu.
717 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
718 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
719 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
720 */
721 bool updateCompressedBackendTexture(const GrBackendTexture&,
722 const SkColor4f& color,
723 GrGpuFinishedProc finishedProc,
724 GrGpuFinishedContext finishedContext);
725
726 /**
727 * If possible, updates a backend texture filled with the provided raw data. The client
728 * should check the return value to see if the update was successful. The client can pass in a
729 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
730 * deleted. The client is required to call `submit` to send the upload work to the gpu.
731 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
732 * If a mipMapped texture is passed in, the data for all the mipmap levels must be provided.
733 * Additionally, all the miplevels must be sized correctly (please see
734 * SkMipMap::ComputeLevelSize and ComputeLevelCount).
735 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
736 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
737 */
738 bool updateCompressedBackendTexture(const GrBackendTexture&,
739 const void* data,
740 size_t dataSize,
741 GrGpuFinishedProc finishedProc,
742 GrGpuFinishedContext finishedContext);
743
Adlai Holler6d0745b2020-10-13 13:29:00 -0400744 /**
745 * Updates the state of the GrBackendTexture/RenderTarget to have the passed in
746 * GrBackendSurfaceMutableState. All objects that wrap the backend surface (i.e. SkSurfaces and
747 * SkImages) will also be aware of this state change. This call does not submit the state change
748 * to the gpu, but requires the client to call `submit` to send it to the GPU. The work
749 * for this call is ordered linearly with all other calls that require GrContext::submit to be
750 * called (e.g updateBackendTexture and flush). If finishedProc is not null then it will be
751 * called with finishedContext after the state transition is known to have occurred on the GPU.
752 *
753 * See GrBackendSurfaceMutableState to see what state can be set via this call.
754 *
755 * If the backend API is Vulkan, the caller can set the GrBackendSurfaceMutableState's
756 * VkImageLayout to VK_IMAGE_LAYOUT_UNDEFINED or queueFamilyIndex to VK_QUEUE_FAMILY_IGNORED to
757 * tell Skia to not change those respective states.
758 *
759 * If previousState is not null and this returns true, then Skia will have filled in
760 * previousState to have the values of the state before this call.
761 */
762 bool setBackendTextureState(const GrBackendTexture&,
763 const GrBackendSurfaceMutableState&,
764 GrBackendSurfaceMutableState* previousState = nullptr,
765 GrGpuFinishedProc finishedProc = nullptr,
766 GrGpuFinishedContext finishedContext = nullptr);
767 bool setBackendRenderTargetState(const GrBackendRenderTarget&,
768 const GrBackendSurfaceMutableState&,
769 GrBackendSurfaceMutableState* previousState = nullptr,
770 GrGpuFinishedProc finishedProc = nullptr,
771 GrGpuFinishedContext finishedContext = nullptr);
772
773 void deleteBackendTexture(GrBackendTexture);
774
775 // This interface allows clients to pre-compile shaders and populate the runtime program cache.
776 // The key and data blobs should be the ones passed to the PersistentCache, in SkSL format.
777 //
778 // Steps to use this API:
779 //
Adlai Hollerb2705682020-10-20 10:11:53 -0400780 // 1) Create a GrDirectContext as normal, but set fPersistentCache on GrContextOptions to
781 // something that will save the cached shader blobs. Set fShaderCacheStrategy to kSkSL. This
782 // will ensure that the blobs are SkSL, and are suitable for pre-compilation.
Adlai Holler6d0745b2020-10-13 13:29:00 -0400783 // 2) Run your application, and save all of the key/data pairs that are fed to the cache.
784 //
785 // 3) Switch over to shipping your application. Include the key/data pairs from above.
786 // 4) At startup (or any convenient time), call precompileShader for each key/data pair.
787 // This will compile the SkSL to create a GL program, and populate the runtime cache.
788 //
789 // This is only guaranteed to work if the context/device used in step #2 are created in the
790 // same way as the one used in step #4, and the same GrContextOptions are specified.
791 // Using cached shader blobs on a different device or driver are undefined.
792 bool precompileShader(const SkData& key, const SkData& data);
793
794#ifdef SK_ENABLE_DUMP_GPU
795 /** Returns a string with detailed information about the context & GPU, in JSON format. */
796 SkString dump() const;
797#endif
798
Adlai Holler53cf44c2020-10-13 17:40:21 -0400799 // Provides access to functions that aren't part of the public API.
Adlai Hollera0693042020-10-14 11:23:11 -0400800 GrDirectContextPriv priv();
801 const GrDirectContextPriv priv() const; // NOLINT(readability-const-return-type)
Adlai Holler53cf44c2020-10-13 17:40:21 -0400802
Robert Phillipsad248452020-06-30 09:27:52 -0400803protected:
Robert Phillipsf4f80112020-07-13 16:13:31 -0400804 GrDirectContext(GrBackendApi backend, const GrContextOptions& options);
805
Robert Phillipsad248452020-06-30 09:27:52 -0400806 bool init() override;
807
Adlai Holler53cf44c2020-10-13 17:40:21 -0400808 GrAtlasManager* onGetAtlasManager() { return fAtlasManager.get(); }
809 GrSmallPathAtlasMgr* onGetSmallPathAtlasMgr();
Robert Phillipsad248452020-06-30 09:27:52 -0400810
Robert Phillips44333c52020-06-30 13:28:00 -0400811 GrDirectContext* asDirectContext() override { return this; }
812
Robert Phillipsad248452020-06-30 09:27:52 -0400813private:
Greg Daniela89b4302021-01-29 10:48:40 -0500814 // This call will make sure out work on the GPU is finished and will execute any outstanding
815 // asynchronous work (e.g. calling finished procs, freeing resources, etc.) related to the
816 // outstanding work on the gpu. The main use currently for this function is when tearing down or
817 // abandoning the context.
818 //
819 // When we finish up work on the GPU it could trigger callbacks to the client. In the case we
820 // are abandoning the context we don't want the client to be able to use the GrDirectContext to
821 // issue more commands during the callback. Thus before calling this function we set the
822 // GrDirectContext's state to be abandoned. However, we need to be able to get by the abaonded
823 // check in the call to know that it is safe to execute this. The shouldExecuteWhileAbandoned
824 // bool is used for this signal.
825 void syncAllOutstandingGpuWork(bool shouldExecuteWhileAbandoned);
826
Adlai Holler53cf44c2020-10-13 17:40:21 -0400827 // fTaskGroup must appear before anything that uses it (e.g. fGpu), so that it is destroyed
828 // after all of its users. Clients of fTaskGroup will generally want to ensure that they call
829 // wait() on it as they are being destroyed, to avoid the possibility of pending tasks being
830 // invoked after objects they depend upon have already been destroyed.
831 std::unique_ptr<SkTaskGroup> fTaskGroup;
832 std::unique_ptr<GrStrikeCache> fStrikeCache;
833 sk_sp<GrGpu> fGpu;
834 std::unique_ptr<GrResourceCache> fResourceCache;
835 std::unique_ptr<GrResourceProvider> fResourceProvider;
836
837 bool fDidTestPMConversions;
838 // true if the PM/UPM conversion succeeded; false otherwise
839 bool fPMUPMConversionsRoundTrip;
840
841 GrContextOptions::PersistentCache* fPersistentCache;
842 GrContextOptions::ShaderErrorHandler* fShaderErrorHandler;
843
844 std::unique_ptr<GrClientMappedBufferManager> fMappedBufferManager;
Robert Phillips3262bc82020-08-10 12:11:58 -0400845 std::unique_ptr<GrAtlasManager> fAtlasManager;
Robert Phillipsad248452020-06-30 09:27:52 -0400846
Robert Phillips079455c2020-08-11 15:18:46 -0400847 std::unique_ptr<GrSmallPathAtlasMgr> fSmallPathAtlasMgr;
Robert Phillips5edf5102020-08-10 16:30:36 -0400848
Adlai Hollera0693042020-10-14 11:23:11 -0400849 friend class GrDirectContextPriv;
Adlai Holler53cf44c2020-10-13 17:40:21 -0400850
Adlai Holler9ae860a2020-10-20 10:13:32 -0400851 using INHERITED = GrRecordingContext;
Robert Phillipsad248452020-06-30 09:27:52 -0400852};
853
854
855#endif