blob: 67319d5ec96c925bad494023c395b7b31e0da0e5 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
8#ifndef GrContext_DEFINED
9#define GrContext_DEFINED
10
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000011#include "SkMatrix.h"
egdaniele61c4112014-06-12 10:24:21 -070012#include "SkPathEffect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000013#include "SkTypes.h"
Robert Phillips4217ea72019-01-30 13:08:28 -050014#include "../private/GrRecordingContext.h"
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040015#include "GrContextOptions.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000016
Brian Salomon79bf7ac2018-05-11 13:08:30 -040017// We shouldn't need this but currently Android is relying on this being include transitively.
18#include "SkUnPreMultiply.h"
19
Robert Phillipsc4039ea2018-03-01 11:36:45 -050020class GrAtlasManager;
Robert Phillipsfc711a22018-02-13 17:03:00 -050021class GrBackendFormat;
Greg Daniel51316782017-08-02 15:10:09 +000022class GrBackendSemaphore;
Brian Salomonc7fe0f72018-05-11 10:14:21 -040023class GrCaps;
robertphillips4fd74ae2016-08-03 14:26:53 -070024class GrContextPriv;
bsalomon41b952c2016-03-11 06:46:33 -080025class GrContextThreadSafeProxy;
robertphillips77a2e522015-10-17 07:43:27 -070026class GrDrawingManager;
joshualittb0a8a372014-09-23 09:50:21 -070027class GrFragmentProcessor;
Greg Daniel02611d92017-07-25 10:05:01 -040028struct GrGLInterface;
Herb Derby081e6f32019-01-16 13:46:02 -050029class GrStrikeCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000030class GrGpu;
Greg Daniel02611d92017-07-25 10:05:01 -040031struct GrMockOptions;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000032class GrPath;
Greg Daniel02611d92017-07-25 10:05:01 -040033class GrRenderTargetContext;
bsalomon0ea80f42015-02-11 10:49:59 -080034class GrResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -070035class GrResourceProvider;
Brian Salomon2bbdcc42017-09-07 12:36:34 -040036class GrSamplerState;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050037class GrSkSLFPFactoryCache;
Robert Phillips7ee385e2017-03-30 08:02:11 -040038class GrSurfaceProxy;
Robert Phillipsfc711a22018-02-13 17:03:00 -050039class GrSwizzle;
joshualittb7133be2015-04-08 09:08:31 -070040class GrTextBlobCache;
kkinnunenc6cb56f2014-06-24 00:12:27 -070041class GrTextContext;
Mike Reed84dd8572017-03-08 22:21:00 -050042class GrTextureProxy;
Greg Daniel02611d92017-07-25 10:05:01 -040043struct GrVkBackendContext;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000044
Mike Reed84dd8572017-03-08 22:21:00 -050045class SkImage;
46class SkSurfaceProps;
Brian Osman51279982017-08-23 10:12:00 -040047class SkTaskGroup;
Robert Phillipsfc711a22018-02-13 17:03:00 -050048class SkTraceMemoryDump;
Mike Reed84dd8572017-03-08 22:21:00 -050049
Robert Phillips4217ea72019-01-30 13:08:28 -050050class SK_API GrContext : public GrRecordingContext {
bsalomon@google.com27847de2011-02-22 20:59:41 +000051public:
52 /**
Brian Salomonc1b9c102018-04-06 09:18:00 -040053 * Creates a GrContext for a backend context. If no GrGLInterface is provided then the result of
54 * GrGLMakeNativeInterface() is used if it succeeds.
bsalomon@google.com27847de2011-02-22 20:59:41 +000055 */
Brian Salomon384fab42017-12-07 12:33:05 -050056 static sk_sp<GrContext> MakeGL(sk_sp<const GrGLInterface>, const GrContextOptions&);
57 static sk_sp<GrContext> MakeGL(sk_sp<const GrGLInterface>);
Brian Salomonc1b9c102018-04-06 09:18:00 -040058 static sk_sp<GrContext> MakeGL(const GrContextOptions&);
59 static sk_sp<GrContext> MakeGL();
60
Greg Daniel10a83da2018-06-14 09:31:11 -040061 static sk_sp<GrContext> MakeVulkan(const GrVkBackendContext&, const GrContextOptions&);
62 static sk_sp<GrContext> MakeVulkan(const GrVkBackendContext&);
Greg Daniel02611d92017-07-25 10:05:01 -040063
Greg Danielb76a72a2017-07-13 15:07:54 -040064#ifdef SK_METAL
65 /**
66 * Makes a GrContext which uses Metal as the backend. The device parameter is an MTLDevice
67 * and queue is an MTLCommandQueue which should be used by the backend. These objects must
68 * have a ref on them which can be transferred to Ganesh which will release the ref when the
69 * GrContext is destroyed.
70 */
71 static sk_sp<GrContext> MakeMetal(void* device, void* queue, const GrContextOptions& options);
Greg Daniel02611d92017-07-25 10:05:01 -040072 static sk_sp<GrContext> MakeMetal(void* device, void* queue);
Greg Danielb76a72a2017-07-13 15:07:54 -040073#endif
74
Greg Daniel02611d92017-07-25 10:05:01 -040075 static sk_sp<GrContext> MakeMock(const GrMockOptions*, const GrContextOptions&);
76 static sk_sp<GrContext> MakeMock(const GrMockOptions*);
77
Robert Phillipsbb606772019-02-04 17:50:57 -050078 ~GrContext() override;
bsalomon@google.com27847de2011-02-22 20:59:41 +000079
bungeman6bd52842016-10-27 09:30:08 -070080 sk_sp<GrContextThreadSafeProxy> threadSafeProxy();
bsalomon41b952c2016-03-11 06:46:33 -080081
bsalomon@google.com27847de2011-02-22 20:59:41 +000082 /**
83 * The GrContext normally assumes that no outsider is setting state
84 * within the underlying 3D API's context/device/whatever. This call informs
85 * the context that the state was modified and it should resend. Shouldn't
86 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000087 * The flag bits, state, is dpendent on which backend is used by the
88 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000089 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000090 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000091
bsalomon@google.com8fe72472011-03-30 21:26:44 +000092 /**
Robert Phillips88260b52018-01-19 12:56:09 -050093 * Abandons all GPU resources and assumes the underlying backend 3D API context is no longer
bsalomon6e2aad42016-04-01 11:54:31 -070094 * usable. Call this if you have lost the associated GPU context, and thus internal texture,
95 * buffer, etc. references/IDs are now invalid. Calling this ensures that the destructors of the
96 * GrContext and any of its created resource objects will not make backend 3D API calls. Content
97 * rendered but not previously flushed may be lost. After this function is called all subsequent
98 * calls on the GrContext will fail or be no-ops.
99 *
100 * The typical use case for this function is that the underlying 3D context was lost and further
101 * API calls may crash.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102 */
Robert Phillips1056eb82018-03-01 14:16:41 -0500103 virtual void abandonContext();
junov@google.com53a55842011-06-08 22:55:10 +0000104
bsalomon6e2aad42016-04-01 11:54:31 -0700105 /**
Khushalc421ca12018-06-26 14:38:34 -0700106 * Returns true if the context was abandoned.
107 */
108 bool abandoned() const;
109
110 /**
bsalomon6e2aad42016-04-01 11:54:31 -0700111 * This is similar to abandonContext() however the underlying 3D context is not yet lost and
112 * the GrContext will cleanup all allocated resources before returning. After returning it will
113 * assume that the underlying context may no longer be valid.
114 *
115 * The typical use case for this function is that the client is going to destroy the 3D context
116 * but can't guarantee that GrContext will be destroyed first (perhaps because it may be ref'ed
117 * elsewhere by either the client or Skia objects).
118 */
Robert Phillips1056eb82018-03-01 14:16:41 -0500119 virtual void releaseResourcesAndAbandonContext();
bsalomon6e2aad42016-04-01 11:54:31 -0700120
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000121 ///////////////////////////////////////////////////////////////////////////
122 // Resource Cache
123
124 /**
125 * Return the current GPU resource cache limits.
126 *
127 * @param maxResources If non-null, returns maximum number of resources that
128 * can be held in the cache.
129 * @param maxResourceBytes If non-null, returns maximum number of bytes of
130 * video memory that can be held in the cache.
131 */
132 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000133
134 /**
135 * Gets the current GPU resource cache usage.
136 *
137 * @param resourceCount If non-null, returns the number of resources that are held in the
138 * cache.
139 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
140 * in the cache.
141 */
142 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
143
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000144 /**
Derek Sollenbergeree479142017-05-24 11:41:33 -0400145 * Gets the number of bytes in the cache consumed by purgeable (e.g. unlocked) resources.
146 */
147 size_t getResourceCachePurgeableBytes() const;
148
149 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000150 * Specify the GPU resource cache limits. If the current cache exceeds either
151 * of these, it will be purged (LRU) to keep the cache within these limits.
152 *
153 * @param maxResources The maximum number of resources that can be held in
154 * the cache.
155 * @param maxResourceBytes The maximum number of bytes of video memory
156 * that can be held in the cache.
157 */
158 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000159
junov@google.com53a55842011-06-08 22:55:10 +0000160 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000161 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000162 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000163 */
Robert Phillips1056eb82018-03-01 14:16:41 -0500164 virtual void freeGpuResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000165
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000166 /**
Jim Van Verth76d917c2017-12-13 09:26:37 -0500167 * Purge GPU resources that haven't been used in the past 'msNotUsed' milliseconds or are
168 * otherwise marked for deletion, regardless of whether the context is under budget.
Brian Salomon5e150852017-03-22 14:53:13 -0400169 */
Jim Van Verth76d917c2017-12-13 09:26:37 -0500170 void performDeferredCleanup(std::chrono::milliseconds msNotUsed);
Brian Salomon5e150852017-03-22 14:53:13 -0400171
Mike Klein4b6bde72017-12-14 07:46:55 -0500172 // Temporary compatibility API for Android.
173 void purgeResourcesNotUsedInMs(std::chrono::milliseconds msNotUsed) {
174 this->performDeferredCleanup(msNotUsed);
175 }
176
Derek Sollenberger5480a182017-05-25 16:43:59 -0400177 /**
178 * Purge unlocked resources from the cache until the the provided byte count has been reached
179 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
180 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
181 * resource types.
182 *
183 * @param maxBytesToPurge the desired number of bytes to be purged.
184 * @param preferScratchResources If true scratch resources will be purged prior to other
185 * resource types.
186 */
187 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
188
Robert Phillips6eba0632018-03-28 12:25:42 -0400189 /**
190 * This entry point is intended for instances where an app has been backgrounded or
191 * suspended.
192 * If 'scratchResourcesOnly' is true all unlocked scratch resources will be purged but the
193 * unlocked resources with persistent data will remain. If 'scratchResourcesOnly' is false
194 * then all unlocked resources will be purged.
195 * In either case, after the unlocked resources are purged a separate pass will be made to
196 * ensure that resource usage is under budget (i.e., even if 'scratchResourcesOnly' is true
197 * some resources with persistent data may be purged to be under budget).
198 *
199 * @param scratchResourcesOnly If true only unlocked scratch resources will be purged prior
200 * enforcing the budget requirements.
201 */
202 void purgeUnlockedResources(bool scratchResourcesOnly);
203
Brian Salomonbdecacf2018-02-02 20:32:49 -0500204 /**
Brian Salomonf932a632018-04-05 12:46:09 -0400205 * Gets the maximum supported texture size.
206 */
207 int maxTextureSize() const;
208
209 /**
210 * Gets the maximum supported render target size.
211 */
212 int maxRenderTargetSize() const;
213
214 /**
Brian Salomonbdecacf2018-02-02 20:32:49 -0500215 * Can a SkImage be created with the given color type.
216 */
217 bool colorTypeSupportedAsImage(SkColorType) const;
218
219 /**
220 * Can a SkSurface be created with the given color type. To check whether MSAA is supported
221 * use maxSurfaceSampleCountForColorType().
222 */
223 bool colorTypeSupportedAsSurface(SkColorType colorType) const {
224 return this->maxSurfaceSampleCountForColorType(colorType) > 0;
225 }
226
227 /**
228 * Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA
229 * rendering is supported for the color type. 0 is returned if rendering to this color type
230 * is not supported at all.
231 */
232 int maxSurfaceSampleCountForColorType(SkColorType) const;
233
bsalomon@google.com27847de2011-02-22 20:59:41 +0000234 ///////////////////////////////////////////////////////////////////////////
235 // Misc.
236
237 /**
Greg Daniel51316782017-08-02 15:10:09 +0000238 * Call to ensure all drawing to the context has been issued to the underlying 3D API.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239 */
bsalomonb77a9072016-09-07 10:02:04 -0700240 void flush();
robertphillipsea461502015-05-26 11:38:03 -0700241
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000242 /**
Greg Daniel51316782017-08-02 15:10:09 +0000243 * Call to ensure all drawing to the context has been issued to the underlying 3D API. After
244 * issuing all commands, numSemaphore semaphores will be signaled by the gpu. The client passes
245 * in an array of numSemaphores GrBackendSemaphores. In general these GrBackendSemaphore's can
246 * be either initialized or not. If they are initialized, the backend uses the passed in
247 * semaphore. If it is not initialized, a new semaphore is created and the GrBackendSemaphore
248 * object is initialized with that semaphore.
249 *
250 * The client will own and be responsible for deleting the underlying semaphores that are stored
251 * and returned in initialized GrBackendSemaphore objects. The GrBackendSemaphore objects
252 * themselves can be deleted as soon as this function returns.
253 *
254 * If the backend API is OpenGL only uninitialized GrBackendSemaphores are supported.
255 * If the backend API is Vulkan either initialized or unitialized semaphores are supported.
256 * If unitialized, the semaphores which are created will be valid for use only with the VkDevice
257 * with which they were created.
258 *
259 * If this call returns GrSemaphoresSubmited::kNo, the GPU backend will not have created or
260 * added any semaphores to signal on the GPU. Thus the client should not have the GPU wait on
261 * any of the semaphores. However, any pending commands to the context will still be flushed.
262 */
263 GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
264 GrBackendSemaphore signalSemaphores[]);
265
robertphillips4fd74ae2016-08-03 14:26:53 -0700266 // Provides access to functions that aren't part of the public API.
Robert Phillips9da87e02019-02-04 13:26:26 -0500267 GrContextPriv priv();
268 const GrContextPriv priv() const;
robertphillips4fd74ae2016-08-03 14:26:53 -0700269
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500270 /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
271 // Chrome is using this!
272 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
273
Khushal3e7548c2018-05-23 15:45:01 -0700274 bool supportsDistanceFieldText() const;
275
Greg Daniela870b462019-01-08 15:49:46 -0500276 void storeVkPipelineCacheData();
277
Robert Phillips88260b52018-01-19 12:56:09 -0500278protected:
Robert Phillipsa41c6852019-02-07 10:44:10 -0500279 GrContext(GrBackendApi, const GrContextOptions&, int32_t contextID = SK_InvalidGenID);
280
281 GrContext* asDirectContext() override { return this; }
Robert Phillips88260b52018-01-19 12:56:09 -0500282
Robert Phillipsbb606772019-02-04 17:50:57 -0500283 bool init(sk_sp<const GrCaps>, sk_sp<GrSkSLFPFactoryCache>) override;
Robert Phillips1056eb82018-03-01 14:16:41 -0500284
Robert Phillips5a66efb2018-03-07 15:13:18 -0500285 virtual GrAtlasManager* onGetAtlasManager() = 0;
Robert Phillips1056eb82018-03-01 14:16:41 -0500286
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500287 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
Robert Phillips1056eb82018-03-01 14:16:41 -0500288
bsalomon@google.com27847de2011-02-22 20:59:41 +0000289private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500290 // fTaskGroup must appear before anything that uses it (e.g. fGpu), so that it is destroyed
291 // after all of its users. Clients of fTaskGroup will generally want to ensure that they call
292 // wait() on it as they are being destroyed, to avoid the possibility of pending tasks being
293 // invoked after objects they depend upon have already been destroyed.
294 std::unique_ptr<SkTaskGroup> fTaskGroup;
Brian Salomon384fab42017-12-07 12:33:05 -0500295 sk_sp<GrGpu> fGpu;
bsalomon41b952c2016-03-11 06:46:33 -0800296 GrResourceCache* fResourceCache;
Brian Osman32342f02017-03-04 08:12:46 -0500297 GrResourceProvider* fResourceProvider;
bsalomond309e7a2015-04-30 14:18:54 -0700298
Robert Phillipsd6841482019-02-08 10:29:20 -0500299 GrStrikeCache* fGlyphCache;
bungeman6bd52842016-10-27 09:30:08 -0700300 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000301
bsalomon41b952c2016-03-11 06:46:33 -0800302 bool fDidTestPMConversions;
Brian Osman28804f32017-04-20 10:24:36 -0400303 // true if the PM/UPM conversion succeeded; false otherwise
304 bool fPMUPMConversionsRoundTrip;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000305
bungeman6bd52842016-10-27 09:30:08 -0700306 std::unique_ptr<GrDrawingManager> fDrawingManager;
robertphillips77a2e522015-10-17 07:43:27 -0700307
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400308 GrContextOptions::PersistentCache* fPersistentCache;
309
Brian Osman11052242016-10-27 14:47:55 -0400310 // TODO: have the GrClipStackClip use renderTargetContexts and rm this friending
robertphillips4fd74ae2016-08-03 14:26:53 -0700311 friend class GrContextPriv;
csmartdaltonc6f411e2016-08-05 22:32:12 -0700312
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000313 /**
Brian Osman5ea96bf2018-10-02 14:58:05 -0400314 * These functions create premul <-> unpremul effects, using the specialized round-trip effects
315 * from GrConfigConversionEffect.
bsalomon@google.comadc65362013-01-28 14:26:09 +0000316 */
Brian Osman5ea96bf2018-10-02 14:58:05 -0400317 std::unique_ptr<GrFragmentProcessor> createPMToUPMEffect(std::unique_ptr<GrFragmentProcessor>);
318 std::unique_ptr<GrFragmentProcessor> createUPMToPMEffect(std::unique_ptr<GrFragmentProcessor>);
Brian Osman409e74f2017-04-17 11:48:28 -0400319
320 /**
Brian Osman5ea96bf2018-10-02 14:58:05 -0400321 * Returns true if createPMToUPMEffect and createUPMToPMEffect will succeed. In other words,
322 * did we find a pair of round-trip preserving conversion effects?
Brian Osman409e74f2017-04-17 11:48:28 -0400323 */
324 bool validPMUPMConversionExists();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000325
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000326 /**
joshualitt0db6dfa2015-04-10 07:01:30 -0700327 * A callback similar to the above for use by the TextBlobCache
328 * TODO move textblob draw calls below context so we can use the call above.
329 */
330 static void TextBlobCacheOverBudgetCB(void* data);
331
Robert Phillips4217ea72019-01-30 13:08:28 -0500332 typedef GrRecordingContext INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000333};
334
bsalomon@google.com27847de2011-02-22 20:59:41 +0000335#endif