blob: de1d3a856712ec82ae65d2349144698aaf7af1a5 [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 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 Phillips7e90be92019-02-15 12:22:59 -050041 bool explicitlyAllocateGPUResources() const {
42 return fContext->explicitlyAllocateGPUResources();
43 }
44
Robert Phillipsbb606772019-02-04 17:50:57 -050045 const GrCaps* caps() const { return fContext->caps(); }
Robert Phillipsa41c6852019-02-07 10:44:10 -050046 sk_sp<const GrCaps> refCaps() const;
Robert Phillipsbb606772019-02-04 17:50:57 -050047
48 sk_sp<GrSkSLFPFactoryCache> fpFactoryCache();
49
Robert Phillipsa41c6852019-02-07 10:44:10 -050050 GrImageContext* asImageContext() { return fContext->asImageContext(); }
51 GrRecordingContext* asRecordingContext() { return fContext->asRecordingContext(); }
52 GrContext* asDirectContext() { return fContext->asDirectContext(); }
53
Robert Phillips4217ea72019-01-30 13:08:28 -050054 // from GrImageContext
Robert Phillipsa41c6852019-02-07 10:44:10 -050055 GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
56 const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
57
Robert Phillips6a6de562019-02-15 15:19:15 -050058 bool abandoned() const { return fContext->abandoned(); }
59
Robert Phillipsa41c6852019-02-07 10:44:10 -050060 /** This is only useful for debug purposes */
61 SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
Robert Phillips4217ea72019-01-30 13:08:28 -050062
63 // from GrRecordingContext
Robert Phillips6f0e02f2019-02-13 11:02:28 -050064
65 // CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real
66 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
67
Robert Phillipsd6841482019-02-08 10:29:20 -050068 sk_sp<GrOpMemoryPool> refOpMemoryPool();
69 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
70
Robert Phillipsc5058a62019-02-15 12:52:59 -050071 /**
72 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
73 *
74 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
75 * ensure its lifetime is tied to that of the context.
76 */
77 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
78
Robert Phillips292a6b22019-02-14 14:49:02 -050079 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
80 sk_sp<SkColorSpace> = nullptr,
81 const SkSurfaceProps* = nullptr);
82
83 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
84 const GrSurfaceDesc&,
85 GrSurfaceOrigin,
86 GrMipMapped,
87 SkBackingFit,
88 SkBudgeted,
89 sk_sp<SkColorSpace> colorSpace = nullptr,
90 const SkSurfaceProps* = nullptr);
91
Robert Phillipsb97da532019-02-12 15:24:12 -050092 /*
93 * Create a new render target context backed by a deferred-style
94 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
95 * renderTargetContexts created via this entry point.
96 */
97 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
98 const GrBackendFormat& format,
99 SkBackingFit fit,
100 int width, int height,
101 GrPixelConfig config,
102 sk_sp<SkColorSpace> colorSpace,
103 int sampleCnt = 1,
104 GrMipMapped = GrMipMapped::kNo,
105 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
106 const SkSurfaceProps* surfaceProps = nullptr,
107 SkBudgeted = SkBudgeted::kYes);
108
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500109 /*
110 * This method will attempt to create a renderTargetContext that has, at least, the number of
111 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
112 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
113 * SRGB-ness will be preserved.
114 */
115 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
116 const GrBackendFormat& format,
117 SkBackingFit fit,
118 int width, int height,
119 GrPixelConfig config,
120 sk_sp<SkColorSpace> colorSpace,
121 int sampleCnt = 1,
122 GrMipMapped = GrMipMapped::kNo,
123 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
124 const SkSurfaceProps* surfaceProps = nullptr,
125 SkBudgeted budgeted = SkBudgeted::kYes);
126
Robert Phillipsd6841482019-02-08 10:29:20 -0500127 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
Robert Phillips4217ea72019-01-30 13:08:28 -0500128
Robert Phillipse42edcc2017-12-13 11:50:22 -0500129 /**
130 * Create a GrContext without a resource cache
131 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400132 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -0500133
Brian Salomond17f6582017-07-19 18:28:58 -0400134 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000135 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500136 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500137
Greg Daniel8ce79912019-02-05 10:08:43 -0500138 // These match the definitions in SkSurface & GrSurface.h, for whence they came
139 typedef void* ReleaseContext;
140 typedef void (*ReleaseProc)(ReleaseContext);
141
Brian Osman11052242016-10-27 14:47:55 -0400142 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000143 const GrBackendTexture& tex,
144 GrSurfaceOrigin origin,
145 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400146 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500147 const SkSurfaceProps* = nullptr,
148 ReleaseProc = nullptr,
149 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700150
Brian Osman11052242016-10-27 14:47:55 -0400151 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000152 const GrBackendRenderTarget&,
153 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -0400154 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500155 const SkSurfaceProps* = nullptr,
156 ReleaseProc = nullptr,
157 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700158
Brian Osman11052242016-10-27 14:47:55 -0400159 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000160 const GrBackendTexture& tex,
161 GrSurfaceOrigin origin,
162 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700163 sk_sp<SkColorSpace> colorSpace,
164 const SkSurfaceProps* = nullptr);
165
Greg Danielb46add82019-01-02 14:51:29 -0500166 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
167 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
168
Robert Phillips7ee385e2017-03-30 08:02:11 -0400169 /**
170 * Call to ensure all drawing to the context has been issued to the
171 * underlying 3D API.
172 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
173 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
174 * provided then all current work will be flushed.
175 */
176 void flush(GrSurfaceProxy*);
177
Chris Daltonfe199b72017-05-05 11:26:15 -0400178 /**
Robert Phillips7ee385e2017-03-30 08:02:11 -0400179 * After this returns any pending writes to the surface will have been issued to the
180 * backend 3D API.
181 */
182 void flushSurfaceWrites(GrSurfaceProxy*);
183
184 /**
185 * After this returns any pending reads or writes to the surface will have been issued to the
186 * backend 3D API.
187 */
188 void flushSurfaceIO(GrSurfaceProxy*);
189
190 /**
191 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
192 * if necessary.
193 *
194 * It is not necessary to call this before reading the render target via Skia/GrContext.
195 * GrContext will detect when it must perform a resolve before reading pixels back from the
196 * surface or using it as a texture.
197 */
198 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
199
Robert Phillipse78b7252017-04-06 07:59:41 -0400200 /**
201 * These flags can be used with the read/write pixels functions below.
202 */
203 enum PixelOpsFlags {
204 /** The GrContext will not be flushed before the surface read or write. This means that
205 the read or write may occur before previous draws have executed. */
206 kDontFlush_PixelOpsFlag = 0x1,
207 /** Any surface writes should be flushed to the backend 3D API after the surface operation
208 is complete */
209 kFlushWrites_PixelOp = 0x2,
210 /** The src for write or dst read is unpremultiplied. This is only respected if both the
211 config src and dst configs are an RGBA/BGRA 8888 format. */
212 kUnpremul_PixelOpsFlag = 0x4,
213 };
214
215 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400216 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400217 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400218 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400219 * @param left left edge of the rectangle to read (inclusive)
220 * @param top top edge of the rectangle to read (inclusive)
221 * @param width width of rectangle to read in pixels.
222 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400223 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400224 * @param dstColorSpace color space of the destination buffer
225 * @param buffer memory to read the rectangle into.
226 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
227 * packed.
228 * @param pixelOpsFlags see PixelOpsFlags enum above.
229 *
230 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
231 * pixel configs
232 */
Brian Salomonc320b152018-02-20 14:05:36 -0500233 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
234 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
235 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400236
237 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400238 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500239 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400240 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400241 * @param left left edge of the rectangle to write (inclusive)
242 * @param top top edge of the rectangle to write (inclusive)
243 * @param width width of rectangle to write in pixels.
244 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500245 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400246 * @param srcColorSpace color space of the source buffer
247 * @param buffer memory to read pixels from
248 * @param rowBytes number of bytes between consecutive rows. Zero
249 * means rows are tightly packed.
250 * @param pixelOpsFlags see PixelOpsFlags enum above.
251 * @return true if the write succeeded, false if not. The write can fail because of an
252 * unsupported combination of surface and src configs.
253 */
Brian Salomonc320b152018-02-20 14:05:36 -0500254 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
255 GrColorType srcColorType, SkColorSpace* srcColorSpace,
256 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400257
Brian Osman51279982017-08-23 10:12:00 -0400258 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
259
Robert Phillips6be756b2018-01-16 15:07:54 -0500260 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
261 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
262
263 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
264
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500265 GrGpu* getGpu() { return fContext->fGpu.get(); }
266 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
267
Herb Derby081e6f32019-01-16 13:46:02 -0500268 GrStrikeCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500269 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500270
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500271 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500272 GrAtlasManager* getAtlasManager() {
273 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500274 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500275
Robert Phillips62000362018-02-01 09:10:04 -0500276 void moveOpListsToDDL(SkDeferredDisplayList*);
277 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
278
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500279 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
280
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500281#ifdef SK_ENABLE_DUMP_GPU
282 /** Returns a string with detailed information about the context & GPU, in JSON format. */
283 SkString dump() const;
284#endif
285
286#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500287 /** Reset GPU stats */
288 void resetGpuStats() const ;
289
290 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
291 void dumpCacheStats(SkString*) const;
292 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
293 void printCacheStats() const;
294
295 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
296 void dumpGpuStats(SkString*) const;
297 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
298 void printGpuStats() const;
299
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500300 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
301 this is for testing only */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500302 void testingOnly_setTextBlobCacheLimit(size_t bytes);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500303
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500304 /** Get pointer to atlas texture for given mask format. Note that this wraps an
305 actively mutating texture in an SkImage. This could yield unexpected results
306 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500307 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500308
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500309 /**
310 * Purge all the unlocked resources from the cache.
311 * This entry point is mainly meant for timing texture uploads
312 * and is not defined in normal builds of Skia.
313 */
314 void testingOnly_purgeAllUnlockedResources();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500315
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500316 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
317#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500318
robertphillips4fd74ae2016-08-03 14:26:53 -0700319private:
320 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400321 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700322 GrContextPriv& operator=(const GrContextPriv&); // unimpl
323
324 // No taking addresses of this type.
325 const GrContextPriv* operator&() const;
326 GrContextPriv* operator&();
327
328 GrContext* fContext;
329
330 friend class GrContext; // to construct/copy this type.
331};
332
Robert Phillips9da87e02019-02-04 13:26:26 -0500333inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700334
Robert Phillips9da87e02019-02-04 13:26:26 -0500335inline const GrContextPriv GrContext::priv() const {
robertphillips4fd74ae2016-08-03 14:26:53 -0700336 return GrContextPriv(const_cast<GrContext*>(this));
337}
338
339#endif