blob: 3279420f319f21dadd0f2f57b918b9df859000db [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 Phillips6f0e02f2019-02-13 11:02:28 -050056
57 // CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real
58 GrDrawingManager* drawingManager() { return fContext->drawingManager(); }
59
Robert Phillipsd6841482019-02-08 10:29:20 -050060 sk_sp<GrOpMemoryPool> refOpMemoryPool();
61 GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
62
Robert Phillips292a6b22019-02-14 14:49:02 -050063 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
64 sk_sp<SkColorSpace> = nullptr,
65 const SkSurfaceProps* = nullptr);
66
67 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
68 const GrSurfaceDesc&,
69 GrSurfaceOrigin,
70 GrMipMapped,
71 SkBackingFit,
72 SkBudgeted,
73 sk_sp<SkColorSpace> colorSpace = nullptr,
74 const SkSurfaceProps* = nullptr);
75
Robert Phillipsb97da532019-02-12 15:24:12 -050076 /*
77 * Create a new render target context backed by a deferred-style
78 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
79 * renderTargetContexts created via this entry point.
80 */
81 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
82 const GrBackendFormat& format,
83 SkBackingFit fit,
84 int width, int height,
85 GrPixelConfig config,
86 sk_sp<SkColorSpace> colorSpace,
87 int sampleCnt = 1,
88 GrMipMapped = GrMipMapped::kNo,
89 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
90 const SkSurfaceProps* surfaceProps = nullptr,
91 SkBudgeted = SkBudgeted::kYes);
92
Robert Phillips6f0e02f2019-02-13 11:02:28 -050093 /*
94 * This method will attempt to create a renderTargetContext that has, at least, the number of
95 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
96 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
97 * SRGB-ness will be preserved.
98 */
99 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
100 const GrBackendFormat& format,
101 SkBackingFit fit,
102 int width, int height,
103 GrPixelConfig config,
104 sk_sp<SkColorSpace> colorSpace,
105 int sampleCnt = 1,
106 GrMipMapped = GrMipMapped::kNo,
107 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
108 const SkSurfaceProps* surfaceProps = nullptr,
109 SkBudgeted budgeted = SkBudgeted::kYes);
110
Robert Phillipsd6841482019-02-08 10:29:20 -0500111 GrAuditTrail* auditTrail() { return fContext->auditTrail(); }
Robert Phillips4217ea72019-01-30 13:08:28 -0500112
Robert Phillipse42edcc2017-12-13 11:50:22 -0500113 /**
114 * Create a GrContext without a resource cache
115 */
Kevin Lubickb5502b22018-03-12 10:17:06 -0400116 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -0500117
Brian Salomond17f6582017-07-19 18:28:58 -0400118 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000119 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500120 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500121
Greg Daniel8ce79912019-02-05 10:08:43 -0500122 // These match the definitions in SkSurface & GrSurface.h, for whence they came
123 typedef void* ReleaseContext;
124 typedef void (*ReleaseProc)(ReleaseContext);
125
Brian Osman11052242016-10-27 14:47:55 -0400126 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000127 const GrBackendTexture& tex,
128 GrSurfaceOrigin origin,
129 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400130 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500131 const SkSurfaceProps* = nullptr,
132 ReleaseProc = nullptr,
133 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700134
Brian Osman11052242016-10-27 14:47:55 -0400135 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000136 const GrBackendRenderTarget&,
137 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -0400138 sk_sp<SkColorSpace> colorSpace,
Greg Daniel8ce79912019-02-05 10:08:43 -0500139 const SkSurfaceProps* = nullptr,
140 ReleaseProc = nullptr,
141 ReleaseContext = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -0700142
Brian Osman11052242016-10-27 14:47:55 -0400143 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000144 const GrBackendTexture& tex,
145 GrSurfaceOrigin origin,
146 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700147 sk_sp<SkColorSpace> colorSpace,
148 const SkSurfaceProps* = nullptr);
149
Greg Danielb46add82019-01-02 14:51:29 -0500150 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
151 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
152
Robert Phillips7ee385e2017-03-30 08:02:11 -0400153 /**
154 * Call to ensure all drawing to the context has been issued to the
155 * underlying 3D API.
156 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
157 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
158 * provided then all current work will be flushed.
159 */
160 void flush(GrSurfaceProxy*);
161
Chris Daltonfe199b72017-05-05 11:26:15 -0400162 /**
163 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
164 *
165 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
166 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400167 */
Chris Daltonfe199b72017-05-05 11:26:15 -0400168 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
169
Robert Phillips7ee385e2017-03-30 08:02:11 -0400170 /**
171 * After this returns any pending writes to the surface will have been issued to the
172 * backend 3D API.
173 */
174 void flushSurfaceWrites(GrSurfaceProxy*);
175
176 /**
177 * After this returns any pending reads or writes to the surface will have been issued to the
178 * backend 3D API.
179 */
180 void flushSurfaceIO(GrSurfaceProxy*);
181
182 /**
183 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
184 * if necessary.
185 *
186 * It is not necessary to call this before reading the render target via Skia/GrContext.
187 * GrContext will detect when it must perform a resolve before reading pixels back from the
188 * surface or using it as a texture.
189 */
190 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
191
Robert Phillipse78b7252017-04-06 07:59:41 -0400192 /**
193 * These flags can be used with the read/write pixels functions below.
194 */
195 enum PixelOpsFlags {
196 /** The GrContext will not be flushed before the surface read or write. This means that
197 the read or write may occur before previous draws have executed. */
198 kDontFlush_PixelOpsFlag = 0x1,
199 /** Any surface writes should be flushed to the backend 3D API after the surface operation
200 is complete */
201 kFlushWrites_PixelOp = 0x2,
202 /** The src for write or dst read is unpremultiplied. This is only respected if both the
203 config src and dst configs are an RGBA/BGRA 8888 format. */
204 kUnpremul_PixelOpsFlag = 0x4,
205 };
206
207 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400208 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400209 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400210 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400211 * @param left left edge of the rectangle to read (inclusive)
212 * @param top top edge of the rectangle to read (inclusive)
213 * @param width width of rectangle to read in pixels.
214 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400215 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400216 * @param dstColorSpace color space of the destination buffer
217 * @param buffer memory to read the rectangle into.
218 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
219 * packed.
220 * @param pixelOpsFlags see PixelOpsFlags enum above.
221 *
222 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
223 * pixel configs
224 */
Brian Salomonc320b152018-02-20 14:05:36 -0500225 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
226 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
227 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400228
229 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400230 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500231 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400232 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400233 * @param left left edge of the rectangle to write (inclusive)
234 * @param top top edge of the rectangle to write (inclusive)
235 * @param width width of rectangle to write in pixels.
236 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500237 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400238 * @param srcColorSpace color space of the source buffer
239 * @param buffer memory to read pixels from
240 * @param rowBytes number of bytes between consecutive rows. Zero
241 * means rows are tightly packed.
242 * @param pixelOpsFlags see PixelOpsFlags enum above.
243 * @return true if the write succeeded, false if not. The write can fail because of an
244 * unsupported combination of surface and src configs.
245 */
Brian Salomonc320b152018-02-20 14:05:36 -0500246 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
247 GrColorType srcColorType, SkColorSpace* srcColorSpace,
248 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400249
Brian Osman51279982017-08-23 10:12:00 -0400250 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
251
Robert Phillips6be756b2018-01-16 15:07:54 -0500252 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
253 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
254
255 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
256
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500257 GrGpu* getGpu() { return fContext->fGpu.get(); }
258 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
259
Herb Derby081e6f32019-01-16 13:46:02 -0500260 GrStrikeCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500261 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500262
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500263 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500264 GrAtlasManager* getAtlasManager() {
265 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500266 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500267
Robert Phillips62000362018-02-01 09:10:04 -0500268 void moveOpListsToDDL(SkDeferredDisplayList*);
269 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
270
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500271 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
272
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500273#ifdef SK_ENABLE_DUMP_GPU
274 /** Returns a string with detailed information about the context & GPU, in JSON format. */
275 SkString dump() const;
276#endif
277
278#if GR_TEST_UTILS
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500279 /** Reset GPU stats */
280 void resetGpuStats() const ;
281
282 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
283 void dumpCacheStats(SkString*) const;
284 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
285 void printCacheStats() const;
286
287 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
288 void dumpGpuStats(SkString*) const;
289 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
290 void printGpuStats() const;
291
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500292 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
293 this is for testing only */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500294 void testingOnly_setTextBlobCacheLimit(size_t bytes);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500295
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500296 /** Get pointer to atlas texture for given mask format. Note that this wraps an
297 actively mutating texture in an SkImage. This could yield unexpected results
298 if it gets cached or used more generally. */
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500299 sk_sp<SkImage> testingOnly_getFontAtlasImage(GrMaskFormat format, unsigned int index = 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500300
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500301 /**
302 * Purge all the unlocked resources from the cache.
303 * This entry point is mainly meant for timing texture uploads
304 * and is not defined in normal builds of Skia.
305 */
306 void testingOnly_purgeAllUnlockedResources();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500307
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500308 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
309#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500310
robertphillips4fd74ae2016-08-03 14:26:53 -0700311private:
312 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400313 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700314 GrContextPriv& operator=(const GrContextPriv&); // unimpl
315
316 // No taking addresses of this type.
317 const GrContextPriv* operator&() const;
318 GrContextPriv* operator&();
319
320 GrContext* fContext;
321
322 friend class GrContext; // to construct/copy this type.
323};
324
Robert Phillips9da87e02019-02-04 13:26:26 -0500325inline GrContextPriv GrContext::priv() { return GrContextPriv(this); }
robertphillips4fd74ae2016-08-03 14:26:53 -0700326
Robert Phillips9da87e02019-02-04 13:26:26 -0500327inline const GrContextPriv GrContext::priv() const {
robertphillips4fd74ae2016-08-03 14:26:53 -0700328 return GrContextPriv(const_cast<GrContext*>(this));
329}
330
331#endif