blob: 8480deb14a56eb6561f9223aab70a067339911d6 [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 Phillipse42edcc2017-12-13 11:50:22 -050031 /**
32 * Create a GrContext without a resource cache
33 */
Kevin Lubickb5502b22018-03-12 10:17:06 -040034 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -050035
Brian Salomonc7fe0f72018-05-11 10:14:21 -040036 const GrCaps* caps() const { return fContext->fCaps.get(); }
37
Robert Phillipsc994a932018-06-19 13:09:54 -040038 sk_sp<GrOpMemoryPool> refOpMemoryPool();
39 GrOpMemoryPool* opMemoryPool();
40
bungeman6bd52842016-10-27 09:30:08 -070041 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070042
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050043 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
44 sk_sp<SkColorSpace> = nullptr,
45 const SkSurfaceProps* = nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -050046
Greg Daniel4065d452018-11-16 15:43:41 -050047 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
48 const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -050049 GrSurfaceOrigin,
Greg Daniel65c7f662017-10-30 13:39:09 -040050 GrMipMapped,
Robert Phillipsbf25d432017-04-07 10:08:53 -040051 SkBackingFit,
Brian Salomon366093f2018-02-13 09:25:22 -050052 SkBudgeted,
53 sk_sp<SkColorSpace> colorSpace = nullptr,
54 const SkSurfaceProps* = nullptr);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050055
Brian Salomond17f6582017-07-19 18:28:58 -040056 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +000057 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -050058 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -050059
Brian Osman11052242016-10-27 14:47:55 -040060 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000061 const GrBackendTexture& tex,
62 GrSurfaceOrigin origin,
63 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -040064 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -050065 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070066
Brian Osman11052242016-10-27 14:47:55 -040067 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +000068 const GrBackendRenderTarget&,
69 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -040070 sk_sp<SkColorSpace> colorSpace,
71 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070072
Brian Osman11052242016-10-27 14:47:55 -040073 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000074 const GrBackendTexture& tex,
75 GrSurfaceOrigin origin,
76 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -070077 sk_sp<SkColorSpace> colorSpace,
78 const SkSurfaceProps* = nullptr);
79
Greg Danielb46add82019-01-02 14:51:29 -050080 sk_sp<GrRenderTargetContext> makeVulkanSecondaryCBRenderTargetContext(
81 const SkImageInfo&, const GrVkDrawableInfo&, const SkSurfaceProps* = nullptr);
82
Brian Osman46da1cc2017-02-14 14:15:48 -050083 bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
Brian Osman8a83ca42018-02-12 14:32:17 -050084 bool sharpenMipmappedTextures() const { return fContext->fSharpenMipmappedTextures; }
Brian Osman46da1cc2017-02-14 14:15:48 -050085
Robert Phillips7ee385e2017-03-30 08:02:11 -040086 /**
87 * Call to ensure all drawing to the context has been issued to the
88 * underlying 3D API.
89 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
90 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
91 * provided then all current work will be flushed.
92 */
93 void flush(GrSurfaceProxy*);
94
Chris Daltonfe199b72017-05-05 11:26:15 -040095 /**
96 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
97 *
98 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
99 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400100 */
Chris Daltonfe199b72017-05-05 11:26:15 -0400101 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
102
103 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400104
Robert Phillips7ee385e2017-03-30 08:02:11 -0400105 /**
106 * After this returns any pending writes to the surface will have been issued to the
107 * backend 3D API.
108 */
109 void flushSurfaceWrites(GrSurfaceProxy*);
110
111 /**
112 * After this returns any pending reads or writes to the surface will have been issued to the
113 * backend 3D API.
114 */
115 void flushSurfaceIO(GrSurfaceProxy*);
116
117 /**
118 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
119 * if necessary.
120 *
121 * It is not necessary to call this before reading the render target via Skia/GrContext.
122 * GrContext will detect when it must perform a resolve before reading pixels back from the
123 * surface or using it as a texture.
124 */
125 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
126
Robert Phillipse78b7252017-04-06 07:59:41 -0400127 /**
128 * These flags can be used with the read/write pixels functions below.
129 */
130 enum PixelOpsFlags {
131 /** The GrContext will not be flushed before the surface read or write. This means that
132 the read or write may occur before previous draws have executed. */
133 kDontFlush_PixelOpsFlag = 0x1,
134 /** Any surface writes should be flushed to the backend 3D API after the surface operation
135 is complete */
136 kFlushWrites_PixelOp = 0x2,
137 /** The src for write or dst read is unpremultiplied. This is only respected if both the
138 config src and dst configs are an RGBA/BGRA 8888 format. */
139 kUnpremul_PixelOpsFlag = 0x4,
140 };
141
142 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400143 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400144 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400145 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400146 * @param left left edge of the rectangle to read (inclusive)
147 * @param top top edge of the rectangle to read (inclusive)
148 * @param width width of rectangle to read in pixels.
149 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400150 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400151 * @param dstColorSpace color space of the destination buffer
152 * @param buffer memory to read the rectangle into.
153 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
154 * packed.
155 * @param pixelOpsFlags see PixelOpsFlags enum above.
156 *
157 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
158 * pixel configs
159 */
Brian Salomonc320b152018-02-20 14:05:36 -0500160 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
161 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
162 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400163
164 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400165 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500166 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400167 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400168 * @param left left edge of the rectangle to write (inclusive)
169 * @param top top edge of the rectangle to write (inclusive)
170 * @param width width of rectangle to write in pixels.
171 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500172 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400173 * @param srcColorSpace color space of the source buffer
174 * @param buffer memory to read pixels from
175 * @param rowBytes number of bytes between consecutive rows. Zero
176 * means rows are tightly packed.
177 * @param pixelOpsFlags see PixelOpsFlags enum above.
178 * @return true if the write succeeded, false if not. The write can fail because of an
179 * unsupported combination of surface and src configs.
180 */
Brian Salomonc320b152018-02-20 14:05:36 -0500181 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
182 GrColorType srcColorType, SkColorSpace* srcColorSpace,
183 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400184
Greg Danielbdf12ad2018-10-12 09:31:11 -0400185 GrBackendApi getBackend() const { return fContext->fBackend; }
Greg Danielfc978fd2017-04-13 09:54:12 -0400186
Brian Osman51279982017-08-23 10:12:00 -0400187 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
188
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500189 GrProxyProvider* proxyProvider() { return fContext->fProxyProvider; }
190 const GrProxyProvider* proxyProvider() const { return fContext->fProxyProvider; }
191
Robert Phillips6be756b2018-01-16 15:07:54 -0500192 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
193 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
194
195 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
196
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500197 GrGpu* getGpu() { return fContext->fGpu.get(); }
198 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
199
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500200 GrGlyphCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500201 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500202
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500203 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500204 GrAtlasManager* getAtlasManager() {
205 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500206 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500207
Robert Phillips62000362018-02-01 09:10:04 -0500208 void moveOpListsToDDL(SkDeferredDisplayList*);
209 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
210
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500211 /**
212 * Purge all the unlocked resources from the cache.
213 * This entry point is mainly meant for timing texture uploads
214 * and is not defined in normal builds of Skia.
215 */
216 void purgeAllUnlockedResources_ForTesting();
217
218
219 /*
220 * Create a new render target context backed by a deferred-style
221 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
222 * renderTargetContexts created via this entry point.
223 */
224 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500225 const GrBackendFormat& format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500226 SkBackingFit fit,
227 int width, int height,
228 GrPixelConfig config,
229 sk_sp<SkColorSpace> colorSpace,
230 int sampleCnt = 1,
231 GrMipMapped = GrMipMapped::kNo,
232 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
233 const SkSurfaceProps* surfaceProps = nullptr,
234 SkBudgeted = SkBudgeted::kYes);
235 /*
236 * This method will attempt to create a renderTargetContext that has, at least, the number of
237 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
238 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
239 * SRGB-ness will be preserved.
240 */
241 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500242 const GrBackendFormat& format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500243 SkBackingFit fit,
244 int width, int height,
245 GrPixelConfig config,
246 sk_sp<SkColorSpace> colorSpace,
247 int sampleCnt = 1,
248 GrMipMapped = GrMipMapped::kNo,
249 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
250 const SkSurfaceProps* surfaceProps = nullptr,
251 SkBudgeted budgeted = SkBudgeted::kYes);
252
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500253 /** Reset GPU stats */
254 void resetGpuStats() const ;
255
256 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
257 void dumpCacheStats(SkString*) const;
258 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
259 void printCacheStats() const;
260
261 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
262 void dumpGpuStats(SkString*) const;
263 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
264 void printGpuStats() const;
265
266 /** Returns a string with detailed information about the context & GPU, in JSON format. */
267 SkString dump() const;
268
269 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
270 this is for testing only */
271 void setTextBlobCacheLimit_ForTesting(size_t bytes);
272
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500273 /** Get pointer to atlas texture for given mask format. Note that this wraps an
274 actively mutating texture in an SkImage. This could yield unexpected results
275 if it gets cached or used more generally. */
276 sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format, unsigned int index = 0);
277
278 GrAuditTrail* getAuditTrail() { return &fContext->fAuditTrail; }
279
280 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
281
Ethan Nicholas00543112018-07-31 09:44:36 -0400282 sk_sp<GrSkSLFPFactoryCache> getFPFactoryCache() {
283 return fContext->fFPFactoryCache;
284 }
285
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500286 /** This is only useful for debug purposes */
287 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fContext->fSingleOwner; } )
288
robertphillips4fd74ae2016-08-03 14:26:53 -0700289private:
290 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400291 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700292 GrContextPriv& operator=(const GrContextPriv&); // unimpl
293
294 // No taking addresses of this type.
295 const GrContextPriv* operator&() const;
296 GrContextPriv* operator&();
297
298 GrContext* fContext;
299
300 friend class GrContext; // to construct/copy this type.
301};
302
303inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
304
305inline const GrContextPriv GrContext::contextPriv () const {
306 return GrContextPriv(const_cast<GrContext*>(this));
307}
308
309#endif