blob: 982656e912ded01ab014ce7ab3e849e28bf4399a [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
59 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
Robert Phillips4217ea72019-01-30 13:08:28 -050060
Robert Phillipse42edcc2017-12-13 11:50:22 -050061 /**
62 * Create a GrContext without a resource cache
63 */
Kevin Lubickb5502b22018-03-12 10:17:06 -040064 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -050065
bungeman6bd52842016-10-27 09:30:08 -070066 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070067
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050068 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
69 sk_sp<SkColorSpace> = nullptr,
70 const SkSurfaceProps* = nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -050071
Greg Daniel4065d452018-11-16 15:43:41 -050072 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
73 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -050074 GrSurfaceOrigin,
Greg Daniel65c7f662017-10-30 13:39:09 -040075 GrMipMapped,
Robert Phillipsbf25d432017-04-07 10:08:53 -040076 SkBackingFit,
Brian Salomon366093f2018-02-13 09:25:22 -050077 SkBudgeted,
78 sk_sp<SkColorSpace> colorSpace = nullptr,
79 const SkSurfaceProps* = nullptr);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050080
Brian Salomond17f6582017-07-19 18:28:58 -040081 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +000082 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -050083 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -050084
Greg Daniel8ce79912019-02-05 10:08:43 -050085 // These match the definitions in SkSurface & GrSurface.h, for whence they came
86 typedef void* ReleaseContext;
87 typedef void (*ReleaseProc)(ReleaseContext);
88
Brian Osman11052242016-10-27 14:47:55 -040089 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000090 const GrBackendTexture& tex,
91 GrSurfaceOrigin origin,
92 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -040093 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -050094 const SkSurfaceProps* = nullptr,
95 ReleaseProc = nullptr,
96 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070097
Brian Osman11052242016-10-27 14:47:55 -040098 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +000099 const GrBackendRenderTarget&,
100 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -0400101 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500102 const SkSurfaceProps* = nullptr,
103 ReleaseProc = nullptr,
104 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700105
Brian Osman11052242016-10-27 14:47:55 -0400106 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000107 const GrBackendTexture& tex,
108 GrSurfaceOrigin origin,
109 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700110 sk_sp<SkColorSpace> colorSpace,
111 const SkSurfaceProps* = nullptr);
112
Greg Danielb46add82019-01-02 14:51:29 -0500113 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
114 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
115
Robert Phillips7ee385e2017-03-30 08:02:11 -0400116 /**
117 * Call to ensure all drawing to the context has been issued to the
118 * underlying 3D API.
119 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
120 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
121 * provided then all current work will be flushed.
122 */
123 void flush(GrSurfaceProxy*);
124
Chris Daltonfe199b72017-05-05 11:26:15 -0400125 /**
126 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
127 *
128 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
129 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400130 */
Chris Daltonfe199b72017-05-05 11:26:15 -0400131 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
132
Robert Phillips7ee385e2017-03-30 08:02:11 -0400133 /**
134 * After this returns any pending writes to the surface will have been issued to the
135 * backend 3D API.
136 */
137 void flushSurfaceWrites(GrSurfaceProxy*);
138
139 /**
140 * After this returns any pending reads or writes to the surface will have been issued to the
141 * backend 3D API.
142 */
143 void flushSurfaceIO(GrSurfaceProxy*);
144
145 /**
146 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
147 * if necessary.
148 *
149 * It is not necessary to call this before reading the render target via Skia/GrContext.
150 * GrContext will detect when it must perform a resolve before reading pixels back from the
151 * surface or using it as a texture.
152 */
153 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
154
Robert Phillipse78b7252017-04-06 07:59:41 -0400155 /**
156 * These flags can be used with the read/write pixels functions below.
157 */
158 enum PixelOpsFlags {
159 /** The GrContext will not be flushed before the surface read or write. This means that
160 the read or write may occur before previous draws have executed. */
161 kDontFlush_PixelOpsFlag = 0x1,
162 /** Any surface writes should be flushed to the backend 3D API after the surface operation
163 is complete */
164 kFlushWrites_PixelOp = 0x2,
165 /** The src for write or dst read is unpremultiplied. This is only respected if both the
166 config src and dst configs are an RGBA/BGRA 8888 format. */
167 kUnpremul_PixelOpsFlag = 0x4,
168 };
169
170 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400171 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400172 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400173 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400174 * @param left left edge of the rectangle to read (inclusive)
175 * @param top top edge of the rectangle to read (inclusive)
176 * @param width width of rectangle to read in pixels.
177 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400178 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400179 * @param dstColorSpace color space of the destination buffer
180 * @param buffer memory to read the rectangle into.
181 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
182 * packed.
183 * @param pixelOpsFlags see PixelOpsFlags enum above.
184 *
185 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
186 * pixel configs
187 */
Brian Salomonc320b152018-02-20 14:05:36 -0500188 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
189 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
190 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400191
192 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400193 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500194 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400195 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400196 * @param left left edge of the rectangle to write (inclusive)
197 * @param top top edge of the rectangle to write (inclusive)
198 * @param width width of rectangle to write in pixels.
199 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500200 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400201 * @param srcColorSpace color space of the source buffer
202 * @param buffer memory to read pixels from
203 * @param rowBytes number of bytes between consecutive rows. Zero
204 * means rows are tightly packed.
205 * @param pixelOpsFlags see PixelOpsFlags enum above.
206 * @return true if the write succeeded, false if not. The write can fail because of an
207 * unsupported combination of surface and src configs.
208 */
Brian Salomonc320b152018-02-20 14:05:36 -0500209 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
210 GrColorType srcColorType, SkColorSpace* srcColorSpace,
211 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400212
Brian Osman51279982017-08-23 10:12:00 -0400213 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
214
Robert Phillips6be756b2018-01-16 15:07:54 -0500215 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
216 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
217
218 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
219
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500220 GrGpu* getGpu() { return fContext->fGpu.get(); }
221 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
222
Herb Derby081e6f32019-01-16 13:46:02 -0500223 GrStrikeCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500224 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500225
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500226 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500227 GrAtlasManager* getAtlasManager() {
228 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500229 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500230
Robert Phillips62000362018-02-01 09:10:04 -0500231 void moveOpListsToDDL(SkDeferredDisplayList*);
232 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
233
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500234 /*
235 * Create a new render target context backed by a deferred-style
236 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
237 * renderTargetContexts created via this entry point.
238 */
239 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500240 const GrBackendFormat& format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500241 SkBackingFit fit,
242 int width, int height,
243 GrPixelConfig config,
244 sk_sp<SkColorSpace> colorSpace,
245 int sampleCnt = 1,
246 GrMipMapped = GrMipMapped::kNo,
247 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
248 const SkSurfaceProps* surfaceProps = nullptr,
249 SkBudgeted = SkBudgeted::kYes);
250 /*
251 * This method will attempt to create a renderTargetContext that has, at least, the number of
252 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
253 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
254 * SRGB-ness will be preserved.
255 */
256 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500257 const GrBackendFormat& format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500258 SkBackingFit fit,
259 int width, int height,
260 GrPixelConfig config,
261 sk_sp<SkColorSpace> colorSpace,
262 int sampleCnt = 1,
263 GrMipMapped = GrMipMapped::kNo,
264 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
265 const SkSurfaceProps* surfaceProps = nullptr,
266 SkBudgeted budgeted = SkBudgeted::kYes);
267
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500268 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
269
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500270#ifdef SK_ENABLE_DUMP_GPU
271 /** Returns a string with detailed information about the context & GPU, in JSON format. */
272 SkString dump() const;
273#endif
274
275#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500276 /** Reset GPU stats */
277 void resetGpuStats() const ;
278
279 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
280 void dumpCacheStats(SkString*) const;
281 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
282 void printCacheStats() const;
283
284 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
285 void dumpGpuStats(SkString*) const;
286 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
287 void printGpuStats() const;
288
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500289 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
290 this is for testing only */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500291 void testingOnly_setTextBlobCacheLimit(size_t bytes);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500292
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500293 /** Get pointer to atlas texture for given mask format. Note that this wraps an
294 actively mutating texture in an SkImage. This could yield unexpected results
295 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500296 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500297
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500298 /**
299 * Purge all the unlocked resources from the cache.
300 * This entry point is mainly meant for timing texture uploads
301 * and is not defined in normal builds of Skia.
302 */
303 void testingOnly_purgeAllUnlockedResources();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500304
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500305 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
306#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500307
robertphillips4fd74ae2016-08-03 14:26:53 -0700308private:
309 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400310 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700311 GrContextPriv& operator=(const GrContextPriv&); // unimpl
312
313 // No taking addresses of this type.
314 const GrContextPriv* operator&() const;
315 GrContextPriv* operator&();
316
317 GrContext* fContext;
318
319 friend class GrContext; // to construct/copy this type.
320};
321
Robert Phillips9da87e02019-02-04 13:26:26 -0500322inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700323
Robert Phillips9da87e02019-02-04 13:26:26 -0500324inline const GrContextPriv GrContext::priv() const {
robertphillips4fd74ae2016-08-03 14:26:53 -0700325 return GrContextPriv(const_cast<GrContext*>(this));
326}
327
328#endif