blob: 301a2b33dbcb9acb230f5b8c46b74d75ab117861 [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
11#include "GrContext.h"
Brian Osman45580d32016-11-23 09:37:01 -050012#include "GrSurfaceContext.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050013#include "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 Phillipse2f7d182016-12-15 09:23:05 -050021class GrSurfaceProxy;
Brian Salomond17f6582017-07-19 18:28:58 -040022class GrTextureContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -050023
Robert Phillips62000362018-02-01 09:10:04 -050024class SkDeferredDisplayList;
25
robertphillips4fd74ae2016-08-03 14:26:53 -070026/** Class that adds methods to GrContext that are only intended for use internal to Skia.
27 This class is purely a privileged window into GrContext. It should never have additional
28 data members or virtual methods. */
29class GrContextPriv {
30public:
Robert Phillips4217ea72019-01-30 13:08:28 -050031
32 // from GrContext_Base
Robert Phillipsfd0d9702019-02-01 10:19:42 -050033 uint32_t contextID() const { return fContext->contextID(); }
Robert Phillips4217ea72019-01-30 13:08:28 -050034
Robert Phillipsfe0963c2019-02-07 13:25:07 -050035 bool matches(GrContext_Base* candidate) const { return fContext->matches(candidate); }
36
Robert Phillipsc1541ae2019-02-04 12:05:37 -050037 const GrContextOptions& options() const { return fContext->options(); }
38
Robert Phillipsbb606772019-02-04 17:50:57 -050039 const GrCaps* caps() const { return fContext->caps(); }
Robert Phillipsa41c6852019-02-07 10:44:10 -050040 sk_sp<const GrCaps> refCaps() const;
Robert Phillipsbb606772019-02-04 17:50:57 -050041
42 sk_sp<GrSkSLFPFactoryCache> fpFactoryCache();
43
Robert Phillipsa41c6852019-02-07 10:44:10 -050044 GrImageContext* asImageContext() { return fContext->asImageContext(); }
45 GrRecordingContext* asRecordingContext() { return fContext->asRecordingContext(); }
46 GrContext* asDirectContext() { return fContext->asDirectContext(); }
47
Robert Phillips4217ea72019-01-30 13:08:28 -050048 // from GrImageContext
Robert Phillipsa41c6852019-02-07 10:44:10 -050049 GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
50 const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
51
52 /** This is only useful for debug purposes */
53 SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
Robert Phillips4217ea72019-01-30 13:08:28 -050054
55 // from GrRecordingContext
Robert Phillipsd6841482019-02-08 10:29:20 -050056 sk_sp<GrOpMemoryPool> refOpMemoryPool();
57 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
58
Robert Phillipsb97da532019-02-12 15:24:12 -050059 /*
60 * Create a new render target context backed by a deferred-style
61 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
62 * renderTargetContexts created via this entry point.
63 */
64 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
65 const GrBackendFormat& format,
66 SkBackingFit fit,
67 int width, int height,
68 GrPixelConfig config,
69 sk_sp<SkColorSpace> colorSpace,
70 int sampleCnt = 1,
71 GrMipMapped = GrMipMapped::kNo,
72 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
73 const SkSurfaceProps* surfaceProps = nullptr,
74 SkBudgeted = SkBudgeted::kYes);
75
Robert Phillipsd6841482019-02-08 10:29:20 -050076 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
Robert Phillips4217ea72019-01-30 13:08:28 -050077
Robert Phillipse42edcc2017-12-13 11:50:22 -050078 /**
79 * Create a GrContext without a resource cache
80 */
Kevin Lubickb5502b22018-03-12 10:17:06 -040081 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -050082
bungeman6bd52842016-10-27 09:30:08 -070083 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070084
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050085 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
86 sk_sp<SkColorSpace> = nullptr,
87 const SkSurfaceProps* = nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -050088
Greg Daniel4065d452018-11-16 15:43:41 -050089 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
90 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -050091 GrSurfaceOrigin,
Greg Daniel65c7f662017-10-30 13:39:09 -040092 GrMipMapped,
Robert Phillipsbf25d432017-04-07 10:08:53 -040093 SkBackingFit,
Brian Salomon366093f2018-02-13 09:25:22 -050094 SkBudgeted,
95 sk_sp<SkColorSpace> colorSpace = nullptr,
96 const SkSurfaceProps* = nullptr);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050097
Brian Salomond17f6582017-07-19 18:28:58 -040098 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +000099 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500100 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500101
Greg Daniel8ce79912019-02-05 10:08:43 -0500102 // These match the definitions in SkSurface & GrSurface.h, for whence they came
103 typedef void* ReleaseContext;
104 typedef void (*ReleaseProc)(ReleaseContext);
105
Brian Osman11052242016-10-27 14:47:55 -0400106 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000107 const GrBackendTexture& tex,
108 GrSurfaceOrigin origin,
109 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400110 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500111 const SkSurfaceProps* = nullptr,
112 ReleaseProc = nullptr,
113 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700114
Brian Osman11052242016-10-27 14:47:55 -0400115 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000116 const GrBackendRenderTarget&,
117 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -0400118 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500119 const SkSurfaceProps* = nullptr,
120 ReleaseProc = nullptr,
121 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700122
Brian Osman11052242016-10-27 14:47:55 -0400123 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000124 const GrBackendTexture& tex,
125 GrSurfaceOrigin origin,
126 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700127 sk_sp<SkColorSpace> colorSpace,
128 const SkSurfaceProps* = nullptr);
129
Greg Danielb46add82019-01-02 14:51:29 -0500130 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
131 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
132
Robert Phillips7ee385e2017-03-30 08:02:11 -0400133 /**
134 * Call to ensure all drawing to the context has been issued to the
135 * underlying 3D API.
136 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
137 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
138 * provided then all current work will be flushed.
139 */
140 void flush(GrSurfaceProxy*);
141
Chris Daltonfe199b72017-05-05 11:26:15 -0400142 /**
143 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
144 *
145 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
146 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400147 */
Chris Daltonfe199b72017-05-05 11:26:15 -0400148 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
149
Robert Phillips7ee385e2017-03-30 08:02:11 -0400150 /**
151 * After this returns any pending writes to the surface will have been issued to the
152 * backend 3D API.
153 */
154 void flushSurfaceWrites(GrSurfaceProxy*);
155
156 /**
157 * After this returns any pending reads or writes to the surface will have been issued to the
158 * backend 3D API.
159 */
160 void flushSurfaceIO(GrSurfaceProxy*);
161
162 /**
163 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
164 * if necessary.
165 *
166 * It is not necessary to call this before reading the render target via Skia/GrContext.
167 * GrContext will detect when it must perform a resolve before reading pixels back from the
168 * surface or using it as a texture.
169 */
170 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
171
Robert Phillipse78b7252017-04-06 07:59:41 -0400172 /**
173 * These flags can be used with the read/write pixels functions below.
174 */
175 enum PixelOpsFlags {
176 /** The GrContext will not be flushed before the surface read or write. This means that
177 the read or write may occur before previous draws have executed. */
178 kDontFlush_PixelOpsFlag = 0x1,
179 /** Any surface writes should be flushed to the backend 3D API after the surface operation
180 is complete */
181 kFlushWrites_PixelOp = 0x2,
182 /** The src for write or dst read is unpremultiplied. This is only respected if both the
183 config src and dst configs are an RGBA/BGRA 8888 format. */
184 kUnpremul_PixelOpsFlag = 0x4,
185 };
186
187 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400188 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400189 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400190 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400191 * @param left left edge of the rectangle to read (inclusive)
192 * @param top top edge of the rectangle to read (inclusive)
193 * @param width width of rectangle to read in pixels.
194 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400195 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400196 * @param dstColorSpace color space of the destination buffer
197 * @param buffer memory to read the rectangle into.
198 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
199 * packed.
200 * @param pixelOpsFlags see PixelOpsFlags enum above.
201 *
202 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
203 * pixel configs
204 */
Brian Salomonc320b152018-02-20 14:05:36 -0500205 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
206 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
207 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400208
209 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400210 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500211 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400212 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400213 * @param left left edge of the rectangle to write (inclusive)
214 * @param top top edge of the rectangle to write (inclusive)
215 * @param width width of rectangle to write in pixels.
216 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500217 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400218 * @param srcColorSpace color space of the source buffer
219 * @param buffer memory to read pixels from
220 * @param rowBytes number of bytes between consecutive rows. Zero
221 * means rows are tightly packed.
222 * @param pixelOpsFlags see PixelOpsFlags enum above.
223 * @return true if the write succeeded, false if not. The write can fail because of an
224 * unsupported combination of surface and src configs.
225 */
Brian Salomonc320b152018-02-20 14:05:36 -0500226 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
227 GrColorType srcColorType, SkColorSpace* srcColorSpace,
228 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400229
Brian Osman51279982017-08-23 10:12:00 -0400230 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
231
Robert Phillips6be756b2018-01-16 15:07:54 -0500232 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
233 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
234
235 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
236
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500237 GrGpu* getGpu() { return fContext->fGpu.get(); }
238 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
239
Herb Derby081e6f32019-01-16 13:46:02 -0500240 GrStrikeCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500241 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500242
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500243 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500244 GrAtlasManager* getAtlasManager() {
245 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500246 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500247
Robert Phillips62000362018-02-01 09:10:04 -0500248 void moveOpListsToDDL(SkDeferredDisplayList*);
249 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
250
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500251 /*
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500252 * This method will attempt to create a renderTargetContext that has, at least, the number of
253 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
254 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
255 * SRGB-ness will be preserved.
256 */
257 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500258 const GrBackendFormat& format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500259 SkBackingFit fit,
260 int width, int height,
261 GrPixelConfig config,
262 sk_sp<SkColorSpace> colorSpace,
263 int sampleCnt = 1,
264 GrMipMapped = GrMipMapped::kNo,
265 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
266 const SkSurfaceProps* surfaceProps = nullptr,
267 SkBudgeted budgeted = SkBudgeted::kYes);
268
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500269 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
270
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500271#ifdef SK_ENABLE_DUMP_GPU
272 /** Returns a string with detailed information about the context & GPU, in JSON format. */
273 SkString dump() const;
274#endif
275
276#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500277 /** Reset GPU stats */
278 void resetGpuStats() const ;
279
280 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
281 void dumpCacheStats(SkString*) const;
282 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
283 void printCacheStats() const;
284
285 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
286 void dumpGpuStats(SkString*) const;
287 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
288 void printGpuStats() const;
289
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500290 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
291 this is for testing only */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500292 void testingOnly_setTextBlobCacheLimit(size_t bytes);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500293
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500294 /** Get pointer to atlas texture for given mask format. Note that this wraps an
295 actively mutating texture in an SkImage. This could yield unexpected results
296 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500297 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500298
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500299 /**
300 * Purge all the unlocked resources from the cache.
301 * This entry point is mainly meant for timing texture uploads
302 * and is not defined in normal builds of Skia.
303 */
304 void testingOnly_purgeAllUnlockedResources();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500305
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500306 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
307#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500308
robertphillips4fd74ae2016-08-03 14:26:53 -0700309private:
310 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400311 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700312 GrContextPriv& operator=(const GrContextPriv&); // unimpl
313
314 // No taking addresses of this type.
315 const GrContextPriv* operator&() const;
316 GrContextPriv* operator&();
317
318 GrContext* fContext;
319
320 friend class GrContext; // to construct/copy this type.
321};
322
Robert Phillips9da87e02019-02-04 13:26:26 -0500323inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700324
Robert Phillips9da87e02019-02-04 13:26:26 -0500325inline const GrContextPriv GrContext::priv() const {
robertphillips4fd74ae2016-08-03 14:26:53 -0700326 return GrContextPriv(const_cast<GrContext*>(this));
327}
328
329#endif