blob: e902ff7a37ff55e0902436fd87cbcf4d2bb0c76e [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 Phillips7e90be92019-02-15 12:22:59 -050039 bool explicitlyAllocateGPUResources() const {
40 return fContext->explicitlyAllocateGPUResources();
41 }
42
Robert Phillipsbb606772019-02-04 17:50:57 -050043 const GrCaps* caps() const { return fContext->caps(); }
Robert Phillipsa41c6852019-02-07 10:44:10 -050044 sk_sp<const GrCaps> refCaps() const;
Robert Phillipsbb606772019-02-04 17:50:57 -050045
46 sk_sp<GrSkSLFPFactoryCache> fpFactoryCache();
47
Robert Phillipsa41c6852019-02-07 10:44:10 -050048 GrImageContext* asImageContext() { return fContext->asImageContext(); }
49 GrRecordingContext* asRecordingContext() { return fContext->asRecordingContext(); }
50 GrContext* asDirectContext() { return fContext->asDirectContext(); }
51
Robert Phillips4217ea72019-01-30 13:08:28 -050052 // from GrImageContext
Robert Phillipsa41c6852019-02-07 10:44:10 -050053 GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
54 const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
55
Robert Phillips6a6de562019-02-15 15:19:15 -050056 bool abandoned() const { return fContext->abandoned(); }
57
Robert Phillipsa41c6852019-02-07 10:44:10 -050058 /** This is only useful for debug purposes */
59 SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
Robert Phillips4217ea72019-01-30 13:08:28 -050060
61 // from GrRecordingContext
Robert Phillips6f0e02f2019-02-13 11:02:28 -050062
63 // CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real
64 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
65
Robert Phillipsd6841482019-02-08 10:29:20 -050066 sk_sp<GrOpMemoryPool> refOpMemoryPool();
67 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
68
Robert Phillipsc5058a62019-02-15 12:52:59 -050069 /**
70 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
71 *
72 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
73 * ensure its lifetime is tied to that of the context.
74 */
75 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
76
Robert Phillips292a6b22019-02-14 14:49:02 -050077 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
78 sk_sp<SkColorSpace> = nullptr,
79 const SkSurfaceProps* = nullptr);
80
81 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
82 const GrSurfaceDesc&,
83 GrSurfaceOrigin,
84 GrMipMapped,
85 SkBackingFit,
86 SkBudgeted,
87 sk_sp<SkColorSpace> colorSpace = nullptr,
88 const SkSurfaceProps* = nullptr);
89
Robert Phillipsb97da532019-02-12 15:24:12 -050090 /*
91 * Create a new render target context backed by a deferred-style
92 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
93 * renderTargetContexts created via this entry point.
94 */
95 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
96 const GrBackendFormat& format,
97 SkBackingFit fit,
98 int width, int height,
99 GrPixelConfig config,
100 sk_sp<SkColorSpace> colorSpace,
101 int sampleCnt = 1,
102 GrMipMapped = GrMipMapped::kNo,
103 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
104 const SkSurfaceProps* surfaceProps = nullptr,
105 SkBudgeted = SkBudgeted::kYes);
106
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500107 /*
108 * This method will attempt to create a renderTargetContext that has, at least, the number of
109 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
110 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
111 * SRGB-ness will be preserved.
112 */
113 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
114 const GrBackendFormat& format,
115 SkBackingFit fit,
116 int width, int height,
117 GrPixelConfig config,
118 sk_sp<SkColorSpace> colorSpace,
119 int sampleCnt = 1,
120 GrMipMapped = GrMipMapped::kNo,
121 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
122 const SkSurfaceProps* surfaceProps = nullptr,
123 SkBudgeted budgeted = SkBudgeted::kYes);
124
Robert Phillipsd6841482019-02-08 10:29:20 -0500125 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
Robert Phillips4217ea72019-01-30 13:08:28 -0500126
Robert Phillipse42edcc2017-12-13 11:50:22 -0500127 /**
128 * Create a GrContext without a resource cache
129 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400130 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -0500131
Brian Salomond17f6582017-07-19 18:28:58 -0400132 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000133 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500134 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500135
Greg Daniel8ce79912019-02-05 10:08:43 -0500136 // These match the definitions in SkSurface & GrSurface.h, for whence they came
137 typedef void* ReleaseContext;
138 typedef void (*ReleaseProc)(ReleaseContext);
139
Brian Osman11052242016-10-27 14:47:55 -0400140 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000141 const GrBackendTexture& tex,
142 GrSurfaceOrigin origin,
143 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400144 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500145 const SkSurfaceProps* = nullptr,
146 ReleaseProc = nullptr,
147 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700148
Brian Osman11052242016-10-27 14:47:55 -0400149 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000150 const GrBackendRenderTarget&,
151 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -0400152 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500153 const SkSurfaceProps* = nullptr,
154 ReleaseProc = nullptr,
155 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700156
Brian Osman11052242016-10-27 14:47:55 -0400157 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000158 const GrBackendTexture& tex,
159 GrSurfaceOrigin origin,
160 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700161 sk_sp<SkColorSpace> colorSpace,
162 const SkSurfaceProps* = nullptr);
163
Greg Danielb46add82019-01-02 14:51:29 -0500164 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
165 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
166
Robert Phillips7ee385e2017-03-30 08:02:11 -0400167 /**
168 * Call to ensure all drawing to the context has been issued to the
169 * underlying 3D API.
170 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
171 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
172 * provided then all current work will be flushed.
173 */
174 void flush(GrSurfaceProxy*);
175
Chris Daltonfe199b72017-05-05 11:26:15 -0400176 /**
Robert Phillips7ee385e2017-03-30 08:02:11 -0400177 * After this returns any pending writes to the surface will have been issued to the
178 * backend 3D API.
179 */
180 void flushSurfaceWrites(GrSurfaceProxy*);
181
182 /**
183 * After this returns any pending reads or writes to the surface will have been issued to the
184 * backend 3D API.
185 */
186 void flushSurfaceIO(GrSurfaceProxy*);
187
188 /**
189 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
190 * if necessary.
191 *
192 * It is not necessary to call this before reading the render target via Skia/GrContext.
193 * GrContext will detect when it must perform a resolve before reading pixels back from the
194 * surface or using it as a texture.
195 */
196 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
197
Robert Phillipse78b7252017-04-06 07:59:41 -0400198 /**
199 * These flags can be used with the read/write pixels functions below.
200 */
201 enum PixelOpsFlags {
202 /** The GrContext will not be flushed before the surface read or write. This means that
203 the read or write may occur before previous draws have executed. */
204 kDontFlush_PixelOpsFlag = 0x1,
205 /** Any surface writes should be flushed to the backend 3D API after the surface operation
206 is complete */
207 kFlushWrites_PixelOp = 0x2,
208 /** The src for write or dst read is unpremultiplied. This is only respected if both the
209 config src and dst configs are an RGBA/BGRA 8888 format. */
210 kUnpremul_PixelOpsFlag = 0x4,
211 };
212
213 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400214 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400215 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400216 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400217 * @param left left edge of the rectangle to read (inclusive)
218 * @param top top edge of the rectangle to read (inclusive)
219 * @param width width of rectangle to read in pixels.
220 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400221 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400222 * @param dstColorSpace color space of the destination buffer
223 * @param buffer memory to read the rectangle into.
224 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
225 * packed.
226 * @param pixelOpsFlags see PixelOpsFlags enum above.
227 *
228 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
229 * pixel configs
230 */
Brian Salomonc320b152018-02-20 14:05:36 -0500231 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
232 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
233 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400234
235 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400236 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500237 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400238 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400239 * @param left left edge of the rectangle to write (inclusive)
240 * @param top top edge of the rectangle to write (inclusive)
241 * @param width width of rectangle to write in pixels.
242 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500243 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400244 * @param srcColorSpace color space of the source buffer
245 * @param buffer memory to read pixels from
246 * @param rowBytes number of bytes between consecutive rows. Zero
247 * means rows are tightly packed.
248 * @param pixelOpsFlags see PixelOpsFlags enum above.
249 * @return true if the write succeeded, false if not. The write can fail because of an
250 * unsupported combination of surface and src configs.
251 */
Brian Salomonc320b152018-02-20 14:05:36 -0500252 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
253 GrColorType srcColorType, SkColorSpace* srcColorSpace,
254 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400255
Brian Osman51279982017-08-23 10:12:00 -0400256 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
257
Robert Phillips6be756b2018-01-16 15:07:54 -0500258 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
259 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
260
261 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
262
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500263 GrGpu* getGpu() { return fContext->fGpu.get(); }
264 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
265
Herb Derby081e6f32019-01-16 13:46:02 -0500266 GrStrikeCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500267 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500268
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500269 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500270 GrAtlasManager* getAtlasManager() {
271 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500272 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500273
Robert Phillips62000362018-02-01 09:10:04 -0500274 void moveOpListsToDDL(SkDeferredDisplayList*);
275 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
276
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500277 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
278
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500279#ifdef SK_ENABLE_DUMP_GPU
280 /** Returns a string with detailed information about the context & GPU, in JSON format. */
281 SkString dump() const;
282#endif
283
284#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500285 /** Reset GPU stats */
286 void resetGpuStats() const ;
287
288 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
289 void dumpCacheStats(SkString*) const;
290 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
291 void printCacheStats() const;
292
293 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
294 void dumpGpuStats(SkString*) const;
295 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
296 void printGpuStats() const;
297
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500298 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
299 this is for testing only */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500300 void testingOnly_setTextBlobCacheLimit(size_t bytes);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500301
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500302 /** Get pointer to atlas texture for given mask format. Note that this wraps an
303 actively mutating texture in an SkImage. This could yield unexpected results
304 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500305 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500306
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500307 /**
308 * Purge all the unlocked resources from the cache.
309 * This entry point is mainly meant for timing texture uploads
310 * and is not defined in normal builds of Skia.
311 */
312 void testingOnly_purgeAllUnlockedResources();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500313
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500314 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
315#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500316
robertphillips4fd74ae2016-08-03 14:26:53 -0700317private:
318 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400319 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700320 GrContextPriv& operator=(const GrContextPriv&); // unimpl
321
322 // No taking addresses of this type.
323 const GrContextPriv* operator&() const;
324 GrContextPriv* operator&();
325
326 GrContext* fContext;
327
328 friend class GrContext; // to construct/copy this type.
329};
330
Robert Phillips9da87e02019-02-04 13:26:26 -0500331inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700332
Robert Phillips9da87e02019-02-04 13:26:26 -0500333inline const GrContextPriv GrContext::priv() const {
robertphillips4fd74ae2016-08-03 14:26:53 -0700334 return GrContextPriv(const_cast<GrContext*>(this));
335}
336
337#endif