blob: 57f35f78ffc6c3395890bb9d5fdbcd267c3f6135 [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;
Adlai Holler6d0745b2020-10-13 13:29:00 -040032class GrRenderTargetContext;
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
509 * must be sized correctly (please see SkMipmap::ComputeLevelSize and ComputeLevelCount).
510 * Note: the pixmap's alphatypes and colorspaces are ignored.
511 * For the Vulkan backend the layout of the created VkImage will be:
512 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
513 */
514 GrBackendTexture createBackendTexture(const SkPixmap srcData[], int numLevels,
515 GrRenderable, GrProtected,
516 GrGpuFinishedProc finishedProc = nullptr,
517 GrGpuFinishedContext finishedContext = nullptr);
518
519 // Helper version of above for a single level.
520 GrBackendTexture createBackendTexture(const SkPixmap& srcData,
521 GrRenderable renderable,
522 GrProtected isProtected,
523 GrGpuFinishedProc finishedProc = nullptr,
524 GrGpuFinishedContext finishedContext = nullptr) {
525 return this->createBackendTexture(&srcData, 1, renderable, isProtected, finishedProc,
526 finishedContext);
527 }
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400528
529 /**
530 * If possible, updates a backend texture to be filled to a particular color. The client should
531 * check the return value to see if the update was successful. The client can pass in a
532 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
533 * deleted. The client is required to call `submit` to send the upload work to the gpu.
534 * The finishedProc will always get called even if we failed to update the GrBackendTexture.
535 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
536 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
537 */
538 bool updateBackendTexture(const GrBackendTexture&,
539 const SkColor4f& color,
540 GrGpuFinishedProc finishedProc,
541 GrGpuFinishedContext finishedContext);
542
543 /**
544 * If possible, updates a backend texture to be filled to a particular color. The data in
545 * GrBackendTexture and passed in color is interpreted with respect to the passed in
546 * SkColorType. The client should check the return value to see if the update was successful.
547 * The client can pass in a finishedProc to be notified when the data has been uploaded by the
548 * gpu and the texture can be deleted. The client is required to call `submit` to send
549 * the upload work to the gpu. The finishedProc will always get called even if we failed to
550 * update the GrBackendTexture.
551 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
552 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
553 */
554 bool updateBackendTexture(const GrBackendTexture&,
555 SkColorType skColorType,
556 const SkColor4f& color,
557 GrGpuFinishedProc finishedProc,
558 GrGpuFinishedContext finishedContext);
559
560 /**
561 * If possible, updates a backend texture filled with the provided pixmap data. The client
562 * should check the return value to see if the update was successful. The client can pass in a
563 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
564 * deleted. The client is required to call `submit` to send the upload work to the gpu.
565 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
566 * The backend texture must be compatible with the provided pixmap(s). Compatible, in this case,
567 * means that the backend format is compatible with the base pixmap's colortype. The src data
568 * can be deleted when this call returns.
569 * If the backend texture is mip mapped, the data for all the mipmap levels must be provided.
570 * In the mipmapped case all the colortypes of the provided pixmaps must be the same.
571 * Additionally, all the miplevels must be sized correctly (please see
572 * SkMipmap::ComputeLevelSize and ComputeLevelCount).
573 * Note: the pixmap's alphatypes and colorspaces are ignored.
574 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
575 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
576 */
577 bool updateBackendTexture(const GrBackendTexture&,
578 const SkPixmap srcData[],
579 int numLevels,
580 GrGpuFinishedProc finishedProc,
581 GrGpuFinishedContext finishedContext);
582
583 /**
584 * Retrieve the GrBackendFormat for a given SkImage::CompressionType. This is
585 * guaranteed to match the backend format used by the following
586 * createCompressedBackendTexture methods that take a CompressionType.
587 * The caller should check that the returned format is valid.
588 */
589 using GrRecordingContext::compressedBackendFormat;
590
Adlai Holler64e13832020-10-13 08:21:56 -0400591 /**
592 *If possible, create a compressed backend texture initialized to a particular color. The
593 * client should ensure that the returned backend texture is valid. The client can pass in a
594 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
595 * deleted. The client is required to call `submit` to send the upload work to the gpu.
596 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
597 * For the Vulkan backend the layout of the created VkImage will be:
598 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
599 */
600 GrBackendTexture createCompressedBackendTexture(int width, int height,
601 const GrBackendFormat&,
602 const SkColor4f& color,
603 GrMipmapped,
604 GrProtected = GrProtected::kNo,
605 GrGpuFinishedProc finishedProc = nullptr,
606 GrGpuFinishedContext finishedContext = nullptr);
607
608 GrBackendTexture createCompressedBackendTexture(int width, int height,
609 SkImage::CompressionType,
610 const SkColor4f& color,
611 GrMipmapped,
612 GrProtected = GrProtected::kNo,
613 GrGpuFinishedProc finishedProc = nullptr,
614 GrGpuFinishedContext finishedContext = nullptr);
615
616 /**
617 * If possible, create a backend texture initialized with the provided raw data. The client
618 * should ensure that the returned backend texture is valid. The client can pass in a
619 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
620 * deleted. The client is required to call `submit` to send the upload work to the gpu.
621 * The finishedProc will always get called even if we failed to create the GrBackendTexture
622 * If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
623 * the data for all the mipmap levels must be provided. Additionally, all the miplevels
624 * must be sized correctly (please see SkMipmap::ComputeLevelSize and ComputeLevelCount).
625 * For the Vulkan backend the layout of the created VkImage will be:
626 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
627 */
628 GrBackendTexture createCompressedBackendTexture(int width, int height,
629 const GrBackendFormat&,
630 const void* data, size_t dataSize,
631 GrMipmapped,
632 GrProtected = GrProtected::kNo,
633 GrGpuFinishedProc finishedProc = nullptr,
634 GrGpuFinishedContext finishedContext = nullptr);
635
636 GrBackendTexture createCompressedBackendTexture(int width, int height,
637 SkImage::CompressionType,
638 const void* data, size_t dataSize,
639 GrMipmapped,
640 GrProtected = GrProtected::kNo,
641 GrGpuFinishedProc finishedProc = nullptr,
642 GrGpuFinishedContext finishedContext = nullptr);
643
644 /**
645 * If possible, updates a backend texture filled with the provided color. If the texture is
646 * mipmapped, all levels of the mip chain will be updated to have the supplied color. The client
647 * should check the return value to see if the update was successful. The client can pass in a
648 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
649 * deleted. The client is required to call `submit` to send the upload work to the gpu.
650 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
651 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
652 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
653 */
654 bool updateCompressedBackendTexture(const GrBackendTexture&,
655 const SkColor4f& color,
656 GrGpuFinishedProc finishedProc,
657 GrGpuFinishedContext finishedContext);
658
659 /**
660 * If possible, updates a backend texture filled with the provided raw data. The client
661 * should check the return value to see if the update was successful. The client can pass in a
662 * finishedProc to be notified when the data has been uploaded by the gpu and the texture can be
663 * deleted. The client is required to call `submit` to send the upload work to the gpu.
664 * The finishedProc will always get called even if we failed to create the GrBackendTexture.
665 * If a mipMapped texture is passed in, the data for all the mipmap levels must be provided.
666 * Additionally, all the miplevels must be sized correctly (please see
667 * SkMipMap::ComputeLevelSize and ComputeLevelCount).
668 * For the Vulkan backend after a successful update the layout of the created VkImage will be:
669 * VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
670 */
671 bool updateCompressedBackendTexture(const GrBackendTexture&,
672 const void* data,
673 size_t dataSize,
674 GrGpuFinishedProc finishedProc,
675 GrGpuFinishedContext finishedContext);
676
Adlai Holler6d0745b2020-10-13 13:29:00 -0400677 /**
678 * Updates the state of the GrBackendTexture/RenderTarget to have the passed in
679 * GrBackendSurfaceMutableState. All objects that wrap the backend surface (i.e. SkSurfaces and
680 * SkImages) will also be aware of this state change. This call does not submit the state change
681 * to the gpu, but requires the client to call `submit` to send it to the GPU. The work
682 * for this call is ordered linearly with all other calls that require GrContext::submit to be
683 * called (e.g updateBackendTexture and flush). If finishedProc is not null then it will be
684 * called with finishedContext after the state transition is known to have occurred on the GPU.
685 *
686 * See GrBackendSurfaceMutableState to see what state can be set via this call.
687 *
688 * If the backend API is Vulkan, the caller can set the GrBackendSurfaceMutableState's
689 * VkImageLayout to VK_IMAGE_LAYOUT_UNDEFINED or queueFamilyIndex to VK_QUEUE_FAMILY_IGNORED to
690 * tell Skia to not change those respective states.
691 *
692 * If previousState is not null and this returns true, then Skia will have filled in
693 * previousState to have the values of the state before this call.
694 */
695 bool setBackendTextureState(const GrBackendTexture&,
696 const GrBackendSurfaceMutableState&,
697 GrBackendSurfaceMutableState* previousState = nullptr,
698 GrGpuFinishedProc finishedProc = nullptr,
699 GrGpuFinishedContext finishedContext = nullptr);
700 bool setBackendRenderTargetState(const GrBackendRenderTarget&,
701 const GrBackendSurfaceMutableState&,
702 GrBackendSurfaceMutableState* previousState = nullptr,
703 GrGpuFinishedProc finishedProc = nullptr,
704 GrGpuFinishedContext finishedContext = nullptr);
705
706 void deleteBackendTexture(GrBackendTexture);
707
708 // This interface allows clients to pre-compile shaders and populate the runtime program cache.
709 // The key and data blobs should be the ones passed to the PersistentCache, in SkSL format.
710 //
711 // Steps to use this API:
712 //
Adlai Hollerb2705682020-10-20 10:11:53 -0400713 // 1) Create a GrDirectContext as normal, but set fPersistentCache on GrContextOptions to
714 // something that will save the cached shader blobs. Set fShaderCacheStrategy to kSkSL. This
715 // will ensure that the blobs are SkSL, and are suitable for pre-compilation.
Adlai Holler6d0745b2020-10-13 13:29:00 -0400716 // 2) Run your application, and save all of the key/data pairs that are fed to the cache.
717 //
718 // 3) Switch over to shipping your application. Include the key/data pairs from above.
719 // 4) At startup (or any convenient time), call precompileShader for each key/data pair.
720 // This will compile the SkSL to create a GL program, and populate the runtime cache.
721 //
722 // This is only guaranteed to work if the context/device used in step #2 are created in the
723 // same way as the one used in step #4, and the same GrContextOptions are specified.
724 // Using cached shader blobs on a different device or driver are undefined.
725 bool precompileShader(const SkData& key, const SkData& data);
726
727#ifdef SK_ENABLE_DUMP_GPU
728 /** Returns a string with detailed information about the context & GPU, in JSON format. */
729 SkString dump() const;
730#endif
731
Adlai Holler53cf44c2020-10-13 17:40:21 -0400732 // Provides access to functions that aren't part of the public API.
Adlai Hollera0693042020-10-14 11:23:11 -0400733 GrDirectContextPriv priv();
734 const GrDirectContextPriv priv() const; // NOLINT(readability-const-return-type)
Adlai Holler53cf44c2020-10-13 17:40:21 -0400735
Robert Phillipsad248452020-06-30 09:27:52 -0400736protected:
Robert Phillipsf4f80112020-07-13 16:13:31 -0400737 GrDirectContext(GrBackendApi backend, const GrContextOptions& options);
738
Robert Phillipsad248452020-06-30 09:27:52 -0400739 bool init() override;
740
Adlai Holler53cf44c2020-10-13 17:40:21 -0400741 GrAtlasManager* onGetAtlasManager() { return fAtlasManager.get(); }
742 GrSmallPathAtlasMgr* onGetSmallPathAtlasMgr();
Robert Phillipsad248452020-06-30 09:27:52 -0400743
Robert Phillips44333c52020-06-30 13:28:00 -0400744 GrDirectContext* asDirectContext() override { return this; }
745
Robert Phillipsad248452020-06-30 09:27:52 -0400746private:
Adlai Holler53cf44c2020-10-13 17:40:21 -0400747 // fTaskGroup must appear before anything that uses it (e.g. fGpu), so that it is destroyed
748 // after all of its users. Clients of fTaskGroup will generally want to ensure that they call
749 // wait() on it as they are being destroyed, to avoid the possibility of pending tasks being
750 // invoked after objects they depend upon have already been destroyed.
751 std::unique_ptr<SkTaskGroup> fTaskGroup;
752 std::unique_ptr<GrStrikeCache> fStrikeCache;
753 sk_sp<GrGpu> fGpu;
754 std::unique_ptr<GrResourceCache> fResourceCache;
755 std::unique_ptr<GrResourceProvider> fResourceProvider;
756
757 bool fDidTestPMConversions;
758 // true if the PM/UPM conversion succeeded; false otherwise
759 bool fPMUPMConversionsRoundTrip;
760
761 GrContextOptions::PersistentCache* fPersistentCache;
762 GrContextOptions::ShaderErrorHandler* fShaderErrorHandler;
763
764 std::unique_ptr<GrClientMappedBufferManager> fMappedBufferManager;
Robert Phillips3262bc82020-08-10 12:11:58 -0400765 std::unique_ptr<GrAtlasManager> fAtlasManager;
Robert Phillipsad248452020-06-30 09:27:52 -0400766
Robert Phillips079455c2020-08-11 15:18:46 -0400767 std::unique_ptr<GrSmallPathAtlasMgr> fSmallPathAtlasMgr;
Robert Phillips5edf5102020-08-10 16:30:36 -0400768
Adlai Hollera0693042020-10-14 11:23:11 -0400769 friend class GrDirectContextPriv;
Adlai Holler53cf44c2020-10-13 17:40:21 -0400770
Adlai Holler9ae860a2020-10-20 10:13:32 -0400771 using INHERITED = GrRecordingContext;
Robert Phillipsad248452020-06-30 09:27:52 -0400772};
773
774
775#endif