blob: ddf257f3939959260314baae1e8ad7eca49fdadb [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
56 /** 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
61 // CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real
62 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
63
Robert Phillipsd6841482019-02-08 10:29:20 -050064 sk_sp<GrOpMemoryPool> refOpMemoryPool();
65 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
66
Robert Phillipsc5058a62019-02-15 12:52:59 -050067 /**
68 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
69 *
70 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
71 * ensure its lifetime is tied to that of the context.
72 */
73 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
74
Robert Phillips292a6b22019-02-14 14:49:02 -050075 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
76 sk_sp<SkColorSpace> = nullptr,
77 const SkSurfaceProps* = nullptr);
78
79 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
80 const GrSurfaceDesc&,
81 GrSurfaceOrigin,
82 GrMipMapped,
83 SkBackingFit,
84 SkBudgeted,
85 sk_sp<SkColorSpace> colorSpace = nullptr,
86 const SkSurfaceProps* = nullptr);
87
Robert Phillipsb97da532019-02-12 15:24:12 -050088 /*
89 * Create a new render target context backed by a deferred-style
90 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
91 * renderTargetContexts created via this entry point.
92 */
93 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
94 const GrBackendFormat& format,
95 SkBackingFit fit,
96 int width, int height,
97 GrPixelConfig config,
98 sk_sp<SkColorSpace> colorSpace,
99 int sampleCnt = 1,
100 GrMipMapped = GrMipMapped::kNo,
101 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
102 const SkSurfaceProps* surfaceProps = nullptr,
103 SkBudgeted = SkBudgeted::kYes);
104
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500105 /*
106 * This method will attempt to create a renderTargetContext that has, at least, the number of
107 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
108 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
109 * SRGB-ness will be preserved.
110 */
111 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
112 const GrBackendFormat& format,
113 SkBackingFit fit,
114 int width, int height,
115 GrPixelConfig config,
116 sk_sp<SkColorSpace> colorSpace,
117 int sampleCnt = 1,
118 GrMipMapped = GrMipMapped::kNo,
119 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
120 const SkSurfaceProps* surfaceProps = nullptr,
121 SkBudgeted budgeted = SkBudgeted::kYes);
122
Robert Phillipsd6841482019-02-08 10:29:20 -0500123 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
Robert Phillips4217ea72019-01-30 13:08:28 -0500124
Robert Phillipse42edcc2017-12-13 11:50:22 -0500125 /**
126 * Create a GrContext without a resource cache
127 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400128 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -0500129
Brian Salomond17f6582017-07-19 18:28:58 -0400130 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000131 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500132 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500133
Greg Daniel8ce79912019-02-05 10:08:43 -0500134 // These match the definitions in SkSurface & GrSurface.h, for whence they came
135 typedef void* ReleaseContext;
136 typedef void (*ReleaseProc)(ReleaseContext);
137
Brian Osman11052242016-10-27 14:47:55 -0400138 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000139 const GrBackendTexture& tex,
140 GrSurfaceOrigin origin,
141 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400142 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500143 const SkSurfaceProps* = nullptr,
144 ReleaseProc = nullptr,
145 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700146
Brian Osman11052242016-10-27 14:47:55 -0400147 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000148 const GrBackendRenderTarget&,
149 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -0400150 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500151 const SkSurfaceProps* = nullptr,
152 ReleaseProc = nullptr,
153 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700154
Brian Osman11052242016-10-27 14:47:55 -0400155 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000156 const GrBackendTexture& tex,
157 GrSurfaceOrigin origin,
158 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700159 sk_sp<SkColorSpace> colorSpace,
160 const SkSurfaceProps* = nullptr);
161
Greg Danielb46add82019-01-02 14:51:29 -0500162 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
163 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
164
Robert Phillips7ee385e2017-03-30 08:02:11 -0400165 /**
166 * Call to ensure all drawing to the context has been issued to the
167 * underlying 3D API.
168 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
169 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
170 * provided then all current work will be flushed.
171 */
172 void flush(GrSurfaceProxy*);
173
Chris Daltonfe199b72017-05-05 11:26:15 -0400174 /**
Robert Phillips7ee385e2017-03-30 08:02:11 -0400175 * After this returns any pending writes to the surface will have been issued to the
176 * backend 3D API.
177 */
178 void flushSurfaceWrites(GrSurfaceProxy*);
179
180 /**
181 * After this returns any pending reads or writes to the surface will have been issued to the
182 * backend 3D API.
183 */
184 void flushSurfaceIO(GrSurfaceProxy*);
185
186 /**
187 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
188 * if necessary.
189 *
190 * It is not necessary to call this before reading the render target via Skia/GrContext.
191 * GrContext will detect when it must perform a resolve before reading pixels back from the
192 * surface or using it as a texture.
193 */
194 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
195
Robert Phillipse78b7252017-04-06 07:59:41 -0400196 /**
197 * These flags can be used with the read/write pixels functions below.
198 */
199 enum PixelOpsFlags {
200 /** The GrContext will not be flushed before the surface read or write. This means that
201 the read or write may occur before previous draws have executed. */
202 kDontFlush_PixelOpsFlag = 0x1,
203 /** Any surface writes should be flushed to the backend 3D API after the surface operation
204 is complete */
205 kFlushWrites_PixelOp = 0x2,
206 /** The src for write or dst read is unpremultiplied. This is only respected if both the
207 config src and dst configs are an RGBA/BGRA 8888 format. */
208 kUnpremul_PixelOpsFlag = 0x4,
209 };
210
211 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400212 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400213 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400214 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400215 * @param left left edge of the rectangle to read (inclusive)
216 * @param top top edge of the rectangle to read (inclusive)
217 * @param width width of rectangle to read in pixels.
218 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400219 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400220 * @param dstColorSpace color space of the destination buffer
221 * @param buffer memory to read the rectangle into.
222 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
223 * packed.
224 * @param pixelOpsFlags see PixelOpsFlags enum above.
225 *
226 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
227 * pixel configs
228 */
Brian Salomonc320b152018-02-20 14:05:36 -0500229 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
230 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
231 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400232
233 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400234 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500235 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400236 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400237 * @param left left edge of the rectangle to write (inclusive)
238 * @param top top edge of the rectangle to write (inclusive)
239 * @param width width of rectangle to write in pixels.
240 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500241 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400242 * @param srcColorSpace color space of the source buffer
243 * @param buffer memory to read pixels from
244 * @param rowBytes number of bytes between consecutive rows. Zero
245 * means rows are tightly packed.
246 * @param pixelOpsFlags see PixelOpsFlags enum above.
247 * @return true if the write succeeded, false if not. The write can fail because of an
248 * unsupported combination of surface and src configs.
249 */
Brian Salomonc320b152018-02-20 14:05:36 -0500250 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
251 GrColorType srcColorType, SkColorSpace* srcColorSpace,
252 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400253
Brian Osman51279982017-08-23 10:12:00 -0400254 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
255
Robert Phillips6be756b2018-01-16 15:07:54 -0500256 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
257 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
258
259 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
260
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500261 GrGpu* getGpu() { return fContext->fGpu.get(); }
262 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
263
Herb Derby081e6f32019-01-16 13:46:02 -0500264 GrStrikeCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500265 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500266
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500267 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500268 GrAtlasManager* getAtlasManager() {
269 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500270 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500271
Robert Phillips62000362018-02-01 09:10:04 -0500272 void moveOpListsToDDL(SkDeferredDisplayList*);
273 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
274
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500275 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
276
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500277#ifdef SK_ENABLE_DUMP_GPU
278 /** Returns a string with detailed information about the context & GPU, in JSON format. */
279 SkString dump() const;
280#endif
281
282#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500283 /** Reset GPU stats */
284 void resetGpuStats() const ;
285
286 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
287 void dumpCacheStats(SkString*) const;
288 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
289 void printCacheStats() const;
290
291 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
292 void dumpGpuStats(SkString*) const;
293 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
294 void printGpuStats() const;
295
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500296 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
297 this is for testing only */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500298 void testingOnly_setTextBlobCacheLimit(size_t bytes);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500299
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500300 /** Get pointer to atlas texture for given mask format. Note that this wraps an
301 actively mutating texture in an SkImage. This could yield unexpected results
302 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500303 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500304
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500305 /**
306 * Purge all the unlocked resources from the cache.
307 * This entry point is mainly meant for timing texture uploads
308 * and is not defined in normal builds of Skia.
309 */
310 void testingOnly_purgeAllUnlockedResources();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500311
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500312 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
313#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500314
robertphillips4fd74ae2016-08-03 14:26:53 -0700315private:
316 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400317 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700318 GrContextPriv& operator=(const GrContextPriv&); // unimpl
319
320 // No taking addresses of this type.
321 const GrContextPriv* operator&() const;
322 GrContextPriv* operator&();
323
324 GrContext* fContext;
325
326 friend class GrContext; // to construct/copy this type.
327};
328
Robert Phillips9da87e02019-02-04 13:26:26 -0500329inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700330
Robert Phillips9da87e02019-02-04 13:26:26 -0500331inline const GrContextPriv GrContext::priv() const {
robertphillips4fd74ae2016-08-03 14:26:53 -0700332 return GrContextPriv(const_cast<GrContext*>(this));
333}
334
335#endif