blob: ede9ea06164965df0d20fd87773da381abab7380 [file] [log] [blame]
robertphillips4fd74ae2016-08-03 14:26:53 -07001/*
2 * Copyright 2016 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 GrContextPriv_DEFINED
9#define GrContextPriv_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContext.h"
12#include "src/gpu/GrSurfaceContext.h"
13#include "src/gpu/text/GrAtlasManager.h"
robertphillips4fd74ae2016-08-03 14:26:53 -070014
Greg Daniel4065d452018-11-16 15:43:41 -050015class GrBackendFormat;
Greg Danielbcf612b2017-05-01 13:50:58 +000016class GrBackendRenderTarget;
Robert Phillipsc994a932018-06-19 13:09:54 -040017class GrOpMemoryPool;
Brian Salomond17f6582017-07-19 18:28:58 -040018class GrOnFlushCallbackObject;
Greg Danield85f97d2017-03-07 13:37:21 -050019class GrSemaphore;
Ethan Nicholas00543112018-07-31 09:44:36 -040020class GrSkSLFPFactory;
Robert Phillipsbc429442019-02-20 08:26:03 -050021class GrSkSLFPFactoryCache;
Robert Phillipse2f7d182016-12-15 09:23:05 -050022class GrSurfaceProxy;
Brian Salomond17f6582017-07-19 18:28:58 -040023class GrTextureContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -050024
Robert Phillips62000362018-02-01 09:10:04 -050025class SkDeferredDisplayList;
Robert Phillipsbc429442019-02-20 08:26:03 -050026class SkTaskGroup;
Robert Phillips62000362018-02-01 09:10:04 -050027
robertphillips4fd74ae2016-08-03 14:26:53 -070028/** Class that adds methods to GrContext that are only intended for use internal to Skia.
29 This class is purely a privileged window into GrContext. It should never have additional
30 data members or virtual methods. */
31class GrContextPriv {
32public:
Robert Phillips4217ea72019-01-30 13:08:28 -050033
34 // from GrContext_Base
Robert Phillipsfd0d9702019-02-01 10:19:42 -050035 uint32_t contextID() const { return fContext->contextID(); }
Robert Phillips4217ea72019-01-30 13:08:28 -050036
Robert Phillipsfe0963c2019-02-07 13:25:07 -050037 bool matches(GrContext_Base* candidate) const { return fContext->matches(candidate); }
38
Robert Phillipsc1541ae2019-02-04 12:05:37 -050039 const GrContextOptions& options() const { return fContext->options(); }
40
Robert Phillipsbb606772019-02-04 17:50:57 -050041 const GrCaps* caps() const { return fContext->caps(); }
Robert Phillipsa41c6852019-02-07 10:44:10 -050042 sk_sp<const GrCaps> refCaps() const;
Robert Phillipsbb606772019-02-04 17:50:57 -050043
44 sk_sp<GrSkSLFPFactoryCache> fpFactoryCache();
45
Robert Phillipsa41c6852019-02-07 10:44:10 -050046 GrImageContext* asImageContext() { return fContext->asImageContext(); }
47 GrRecordingContext* asRecordingContext() { return fContext->asRecordingContext(); }
48 GrContext* asDirectContext() { return fContext->asDirectContext(); }
49
Robert Phillips4217ea72019-01-30 13:08:28 -050050 // from GrImageContext
Robert Phillipsa41c6852019-02-07 10:44:10 -050051 GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
52 const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
53
Robert Phillips6a6de562019-02-15 15:19:15 -050054 bool abandoned() const { return fContext->abandoned(); }
55
Robert Phillipsa41c6852019-02-07 10:44:10 -050056 /** This is only useful for debug purposes */
57 SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
Robert Phillips4217ea72019-01-30 13:08:28 -050058
59 // from GrRecordingContext
Robert Phillips6f0e02f2019-02-13 11:02:28 -050060 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
61
Robert Phillipsd6841482019-02-08 10:29:20 -050062 sk_sp<GrOpMemoryPool> refOpMemoryPool();
63 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
64
Herb Derbya00da612019-03-04 17:10:01 -050065 GrStrikeCache* getGrStrikeCache() { return fContext->getGrStrikeCache(); }
Robert Phillips2184fb72019-02-21 16:11:41 -050066 GrTextBlobCache* getTextBlobCache() { return fContext->getTextBlobCache(); }
67
Robert Phillipsc5058a62019-02-15 12:52:59 -050068 /**
69 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
70 *
71 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
72 * ensure its lifetime is tied to that of the context.
73 */
74 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
75
Brian Salomonbf6b9792019-08-21 09:38:10 -040076 std::unique_ptr<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
77 GrColorType,
78 SkAlphaType,
79 sk_sp<SkColorSpace> = nullptr,
80 const SkSurfaceProps* = nullptr);
Robert Phillips292a6b22019-02-14 14:49:02 -050081
Brian Salomon947efe22019-07-16 15:36:11 -040082 /** Create a new texture context backed by a deferred-style GrTextureProxy. */
Brian Salomonbf6b9792019-08-21 09:38:10 -040083 std::unique_ptr<GrTextureContext> makeDeferredTextureContext(
Brian Salomon947efe22019-07-16 15:36:11 -040084 SkBackingFit,
85 int width,
86 int height,
87 GrColorType,
88 SkAlphaType,
89 sk_sp<SkColorSpace>,
90 GrMipMapped = GrMipMapped::kNo,
91 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
92 SkBudgeted = SkBudgeted::kYes,
93 GrProtected = GrProtected::kNo);
Robert Phillips292a6b22019-02-14 14:49:02 -050094
Robert Phillipsb97da532019-02-12 15:24:12 -050095 /*
96 * Create a new render target context backed by a deferred-style
97 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
98 * renderTargetContexts created via this entry point.
99 */
Brian Salomonbf6b9792019-08-21 09:38:10 -0400100 std::unique_ptr<GrRenderTargetContext> makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400101 SkBackingFit fit,
102 int width,
103 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400104 GrColorType,
105 sk_sp<SkColorSpace> colorSpace,
106 int sampleCnt = 1,
107 GrMipMapped = GrMipMapped::kNo,
108 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
109 const SkSurfaceProps* surfaceProps = nullptr,
110 SkBudgeted = SkBudgeted::kYes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400111 GrProtected isProtected = GrProtected::kNo);
Robert Phillipsb97da532019-02-12 15:24:12 -0500112
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500113 /*
114 * This method will attempt to create a renderTargetContext that has, at least, the number of
115 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
116 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
117 * SRGB-ness will be preserved.
118 */
Brian Salomonbf6b9792019-08-21 09:38:10 -0400119 std::unique_ptr<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
Brian Salomond6287472019-06-24 15:50:07 -0400120 SkBackingFit fit,
121 int width,
122 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400123 GrColorType,
124 sk_sp<SkColorSpace> colorSpace,
125 int sampleCnt = 1,
126 GrMipMapped = GrMipMapped::kNo,
127 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
128 const SkSurfaceProps* surfaceProps = nullptr,
Brian Osmanc2486592019-09-20 13:32:51 -0400129 SkBudgeted budgeted = SkBudgeted::kYes,
130 GrProtected isProtected = GrProtected::kNo);
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500131
Robert Phillipsd6841482019-02-08 10:29:20 -0500132 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
Robert Phillips4217ea72019-01-30 13:08:28 -0500133
Robert Phillipse42edcc2017-12-13 11:50:22 -0500134 /**
135 * Create a GrContext without a resource cache
136 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400137 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -0500138
Brian Salomonbf6b9792019-08-21 09:38:10 -0400139 std::unique_ptr<GrTextureContext> makeBackendTextureContext(const GrBackendTexture&,
140 GrSurfaceOrigin,
141 GrColorType,
142 SkAlphaType,
143 sk_sp<SkColorSpace>);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500144
Greg Daniel8ce79912019-02-05 10:08:43 -0500145 // These match the definitions in SkSurface & GrSurface.h, for whence they came
146 typedef void* ReleaseContext;
147 typedef void (*ReleaseProc)(ReleaseContext);
148
Brian Salomonbf6b9792019-08-21 09:38:10 -0400149 std::unique_ptr<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400150 const GrBackendTexture& tex,
151 GrSurfaceOrigin origin,
152 int sampleCnt,
153 GrColorType,
154 sk_sp<SkColorSpace> colorSpace,
155 const SkSurfaceProps* = nullptr,
156 ReleaseProc = nullptr,
157 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700158
Brian Salomonbf6b9792019-08-21 09:38:10 -0400159 std::unique_ptr<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400160 const GrBackendRenderTarget&,
161 GrSurfaceOrigin origin,
162 GrColorType,
163 sk_sp<SkColorSpace> colorSpace,
164 const SkSurfaceProps* = nullptr,
165 ReleaseProc = nullptr,
166 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700167
Brian Salomonbf6b9792019-08-21 09:38:10 -0400168 std::unique_ptr<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400169 const GrBackendTexture& tex,
170 GrSurfaceOrigin origin,
171 int sampleCnt,
172 GrColorType,
173 sk_sp<SkColorSpace> colorSpace,
174 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700175
Brian Salomonbf6b9792019-08-21 09:38:10 -0400176 std::unique_ptr<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
Greg Danielb46add82019-01-02 14:51:29 -0500177 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
178
Robert Phillips7ee385e2017-03-30 08:02:11 -0400179 /**
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400180 * Finalizes all pending reads and writes to the surfaces and also performs an MSAA resolves
181 * if necessary. The GrSurfaceProxy array is treated as a hint. If it is supplied the context
182 * will guarantee that the draws required for those proxies are flushed but it could do more.
183 * If no array is provided then all current work will be flushed.
Robert Phillips7ee385e2017-03-30 08:02:11 -0400184 *
185 * It is not necessary to call this before reading the render target via Skia/GrContext.
186 * GrContext will detect when it must perform a resolve before reading pixels back from the
187 * surface or using it as a texture.
188 */
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400189 GrSemaphoresSubmitted flushSurfaces(GrSurfaceProxy*[], int numProxies, const GrFlushInfo&);
190
191 /** Version of above that flushes for a single proxy and uses a default GrFlushInfo. Null is
192 * allowed. */
Greg Daniel4aa13e72019-04-15 14:42:20 -0400193 void flushSurface(GrSurfaceProxy*);
Robert Phillips7ee385e2017-03-30 08:02:11 -0400194
Greg Daniel6eb8c242019-06-05 10:22:24 -0400195 /**
196 * Returns true if createPMToUPMEffect and createUPMToPMEffect will succeed. In other words,
197 * did we find a pair of round-trip preserving conversion effects?
198 */
199 bool validPMUPMConversionExists();
Robert Phillipse78b7252017-04-06 07:59:41 -0400200
201 /**
Greg Daniel6eb8c242019-06-05 10:22:24 -0400202 * These functions create premul <-> unpremul effects, using the specialized round-trip effects
203 * from GrConfigConversionEffect.
Robert Phillipse78b7252017-04-06 07:59:41 -0400204 */
Greg Daniel6eb8c242019-06-05 10:22:24 -0400205 std::unique_ptr<GrFragmentProcessor> createPMToUPMEffect(std::unique_ptr<GrFragmentProcessor>);
206 std::unique_ptr<GrFragmentProcessor> createUPMToPMEffect(std::unique_ptr<GrFragmentProcessor>);
Robert Phillipse78b7252017-04-06 07:59:41 -0400207
Brian Osman51279982017-08-23 10:12:00 -0400208 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
209
Robert Phillips6be756b2018-01-16 15:07:54 -0500210 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
211 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
212
213 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
214
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500215 GrGpu* getGpu() { return fContext->fGpu.get(); }
216 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
217
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500218 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500219 GrAtlasManager* getAtlasManager() {
220 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500221 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500222
Chris Dalton6b498102019-08-01 14:14:52 -0600223 void moveRenderTasksToDDL(SkDeferredDisplayList*);
224 void copyRenderTasksFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
Robert Phillips62000362018-02-01 09:10:04 -0500225
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500226 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400227 GrContextOptions::ShaderErrorHandler* getShaderErrorHandler() const {
228 return fContext->fShaderErrorHandler;
229 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500230
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500231#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500232 /** Reset GPU stats */
233 void resetGpuStats() const ;
234
235 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
236 void dumpCacheStats(SkString*) const;
237 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
238 void printCacheStats() const;
239
240 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
241 void dumpGpuStats(SkString*) const;
242 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
243 void printGpuStats() const;
244
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500245 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
246 this is for testing only */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500247 void testingOnly_setTextBlobCacheLimit(size_t bytes);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500248
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500249 /** Get pointer to atlas texture for given mask format. Note that this wraps an
250 actively mutating texture in an SkImage. This could yield unexpected results
251 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500252 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500253
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500254 /**
255 * Purge all the unlocked resources from the cache.
256 * This entry point is mainly meant for timing texture uploads
257 * and is not defined in normal builds of Skia.
258 */
259 void testingOnly_purgeAllUnlockedResources();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500260
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500261 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
262#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500263
Robert Phillipscb1adb42019-06-10 15:09:34 -0400264 // If possible, create a backend texture initialized with the provided pixmap data. The client
265 // should ensure that the returned backend texture is valid.
266 // If successful, the created backend texture will be compatible with the provided
267 // pixmap(s).
268 // If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
269 // the data for all the mipmap levels must be provided.
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400270 GrBackendTexture createBackendTexture(const SkPixmap srcData[], int numLevels,
271 GrRenderable, GrProtected);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400272
robertphillips4fd74ae2016-08-03 14:26:53 -0700273private:
274 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400275 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700276 GrContextPriv& operator=(const GrContextPriv&); // unimpl
277
278 // No taking addresses of this type.
279 const GrContextPriv* operator&() const;
280 GrContextPriv* operator&();
281
282 GrContext* fContext;
283
284 friend class GrContext; // to construct/copy this type.
285};
286
Robert Phillips9da87e02019-02-04 13:26:26 -0500287inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700288
Robert Phillips9da87e02019-02-04 13:26:26 -0500289inline const GrContextPriv GrContext::priv() const {
robertphillips4fd74ae2016-08-03 14:26:53 -0700290 return GrContextPriv(const_cast<GrContext*>(this));
291}
292
293#endif