blob: 7e6ecb8310976cb277253e9cf70660a2f48d16d2 [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 Danielbcf612b2017-05-01 13:50:58 +000015class GrBackendRenderTarget;
Brian Salomond17f6582017-07-19 18:28:58 -040016class GrOnFlushCallbackObject;
Greg Danield85f97d2017-03-07 13:37:21 -050017class GrSemaphore;
Robert Phillipse2f7d182016-12-15 09:23:05 -050018class GrSurfaceProxy;
Brian Salomond17f6582017-07-19 18:28:58 -040019class GrTextureContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -050020
Robert Phillips62000362018-02-01 09:10:04 -050021class SkDeferredDisplayList;
22
robertphillips4fd74ae2016-08-03 14:26:53 -070023/** Class that adds methods to GrContext that are only intended for use internal to Skia.
24 This class is purely a privileged window into GrContext. It should never have additional
25 data members or virtual methods. */
26class GrContextPriv {
27public:
Robert Phillipse42edcc2017-12-13 11:50:22 -050028 /**
29 * Create a GrContext without a resource cache
30 */
Kevin Lubickb5502b22018-03-12 10:17:06 -040031 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -050032
Brian Salomonc7fe0f72018-05-11 10:14:21 -040033 const GrCaps* caps() const { return fContext->fCaps.get(); }
34
bungeman6bd52842016-10-27 09:30:08 -070035 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070036
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050037 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
38 sk_sp<SkColorSpace> = nullptr,
39 const SkSurfaceProps* = nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -050040
Robert Phillipsbf25d432017-04-07 10:08:53 -040041 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -050042 GrSurfaceOrigin,
Greg Daniel65c7f662017-10-30 13:39:09 -040043 GrMipMapped,
Robert Phillipsbf25d432017-04-07 10:08:53 -040044 SkBackingFit,
Brian Salomon366093f2018-02-13 09:25:22 -050045 SkBudgeted,
46 sk_sp<SkColorSpace> colorSpace = nullptr,
47 const SkSurfaceProps* = nullptr);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050048
Brian Salomond17f6582017-07-19 18:28:58 -040049 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +000050 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -050051 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -050052
Brian Osman11052242016-10-27 14:47:55 -040053 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000054 const GrBackendTexture& tex,
55 GrSurfaceOrigin origin,
56 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -040057 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -050058 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070059
Brian Osman11052242016-10-27 14:47:55 -040060 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +000061 const GrBackendRenderTarget&,
62 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -040063 sk_sp<SkColorSpace> colorSpace,
64 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070065
Brian Osman11052242016-10-27 14:47:55 -040066 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000067 const GrBackendTexture& tex,
68 GrSurfaceOrigin origin,
69 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -070070 sk_sp<SkColorSpace> colorSpace,
71 const SkSurfaceProps* = nullptr);
72
Brian Osman46da1cc2017-02-14 14:15:48 -050073 bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
Brian Osman8a83ca42018-02-12 14:32:17 -050074 bool sharpenMipmappedTextures() const { return fContext->fSharpenMipmappedTextures; }
Brian Osman46da1cc2017-02-14 14:15:48 -050075
Robert Phillips7ee385e2017-03-30 08:02:11 -040076 /**
77 * Call to ensure all drawing to the context has been issued to the
78 * underlying 3D API.
79 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
80 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
81 * provided then all current work will be flushed.
82 */
83 void flush(GrSurfaceProxy*);
84
Chris Daltonfe199b72017-05-05 11:26:15 -040085 /**
86 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
87 *
88 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
89 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -040090 */
Chris Daltonfe199b72017-05-05 11:26:15 -040091 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
92
93 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipseb35f4d2017-03-21 07:56:47 -040094
Robert Phillips7ee385e2017-03-30 08:02:11 -040095 /**
96 * After this returns any pending writes to the surface will have been issued to the
97 * backend 3D API.
98 */
99 void flushSurfaceWrites(GrSurfaceProxy*);
100
101 /**
102 * After this returns any pending reads or writes to the surface will have been issued to the
103 * backend 3D API.
104 */
105 void flushSurfaceIO(GrSurfaceProxy*);
106
107 /**
108 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
109 * if necessary.
110 *
111 * It is not necessary to call this before reading the render target via Skia/GrContext.
112 * GrContext will detect when it must perform a resolve before reading pixels back from the
113 * surface or using it as a texture.
114 */
115 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
116
Robert Phillipse78b7252017-04-06 07:59:41 -0400117 /**
118 * These flags can be used with the read/write pixels functions below.
119 */
120 enum PixelOpsFlags {
121 /** The GrContext will not be flushed before the surface read or write. This means that
122 the read or write may occur before previous draws have executed. */
123 kDontFlush_PixelOpsFlag = 0x1,
124 /** Any surface writes should be flushed to the backend 3D API after the surface operation
125 is complete */
126 kFlushWrites_PixelOp = 0x2,
127 /** The src for write or dst read is unpremultiplied. This is only respected if both the
128 config src and dst configs are an RGBA/BGRA 8888 format. */
129 kUnpremul_PixelOpsFlag = 0x4,
130 };
131
132 /**
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400133 * Reads a rectangle of pixels from a surface. There are currently two versions of this.
134 * readSurfacePixels() is the older version which will be replaced by the more robust and
135 * maintainable (but perhaps slower) readSurfacePixels2().
136 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400137 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400138 * @param left left edge of the rectangle to read (inclusive)
139 * @param top top edge of the rectangle to read (inclusive)
140 * @param width width of rectangle to read in pixels.
141 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400142 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400143 * @param dstColorSpace color space of the destination buffer
144 * @param buffer memory to read the rectangle into.
145 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
146 * packed.
147 * @param pixelOpsFlags see PixelOpsFlags enum above.
148 *
149 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
150 * pixel configs
151 */
Brian Salomonc320b152018-02-20 14:05:36 -0500152 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
153 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
154 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400155 bool readSurfacePixels2(GrSurfaceContext* src, int left, int top, int width, int height,
156 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
157 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400158
159 /**
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500160 * Writes a rectangle of pixels to a surface. There are currently two versions of this.
161 * writeSurfacePixels() is the older version which will be replaced by the more robust and
162 * maintainable (but perhaps slower) writeSurfacePixels2().
163 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400164 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400165 * @param left left edge of the rectangle to write (inclusive)
166 * @param top top edge of the rectangle to write (inclusive)
167 * @param width width of rectangle to write in pixels.
168 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500169 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400170 * @param srcColorSpace color space of the source buffer
171 * @param buffer memory to read pixels from
172 * @param rowBytes number of bytes between consecutive rows. Zero
173 * means rows are tightly packed.
174 * @param pixelOpsFlags see PixelOpsFlags enum above.
175 * @return true if the write succeeded, false if not. The write can fail because of an
176 * unsupported combination of surface and src configs.
177 */
Brian Salomonc320b152018-02-20 14:05:36 -0500178 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
179 GrColorType srcColorType, SkColorSpace* srcColorSpace,
180 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500181 bool writeSurfacePixels2(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 Danielfc978fd2017-04-13 09:54:12 -0400185 GrBackend getBackend() const { return fContext->fBackend; }
186
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 Phillips41a3b872018-03-09 12:00:34 -0500197 GrTextureStripAtlasManager* textureStripAtlasManager() {
198 return fContext->fTextureStripAtlasManager.get();
199 }
200
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500201 GrGpu* getGpu() { return fContext->fGpu.get(); }
202 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
203
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500204 GrGlyphCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500205 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500206
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500207 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500208 GrAtlasManager* getAtlasManager() {
209 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500210 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500211
Robert Phillips62000362018-02-01 09:10:04 -0500212 void moveOpListsToDDL(SkDeferredDisplayList*);
213 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
214
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500215 /**
216 * Purge all the unlocked resources from the cache.
217 * This entry point is mainly meant for timing texture uploads
218 * and is not defined in normal builds of Skia.
219 */
220 void purgeAllUnlockedResources_ForTesting();
221
222
223 /*
224 * Create a new render target context backed by a deferred-style
225 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
226 * renderTargetContexts created via this entry point.
227 */
228 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
229 SkBackingFit fit,
230 int width, int height,
231 GrPixelConfig config,
232 sk_sp<SkColorSpace> colorSpace,
233 int sampleCnt = 1,
234 GrMipMapped = GrMipMapped::kNo,
235 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
236 const SkSurfaceProps* surfaceProps = nullptr,
237 SkBudgeted = SkBudgeted::kYes);
238 /*
239 * This method will attempt to create a renderTargetContext that has, at least, the number of
240 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
241 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
242 * SRGB-ness will be preserved.
243 */
244 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
245 SkBackingFit fit,
246 int width, int height,
247 GrPixelConfig config,
248 sk_sp<SkColorSpace> colorSpace,
249 int sampleCnt = 1,
250 GrMipMapped = GrMipMapped::kNo,
251 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
252 const SkSurfaceProps* surfaceProps = nullptr,
253 SkBudgeted budgeted = SkBudgeted::kYes);
254
255 bool abandoned() const;
256
257 /** Reset GPU stats */
258 void resetGpuStats() const ;
259
260 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
261 void dumpCacheStats(SkString*) const;
262 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
263 void printCacheStats() const;
264
265 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
266 void dumpGpuStats(SkString*) const;
267 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
268 void printGpuStats() const;
269
270 /** Returns a string with detailed information about the context & GPU, in JSON format. */
271 SkString dump() const;
272
273 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
274 this is for testing only */
275 void setTextBlobCacheLimit_ForTesting(size_t bytes);
276
277 /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be
278 to an array of 3 entries */
279 void setTextContextAtlasSizes_ForTesting(const GrDrawOpAtlasConfig* configs);
280
281 /** Get pointer to atlas texture for given mask format. Note that this wraps an
282 actively mutating texture in an SkImage. This could yield unexpected results
283 if it gets cached or used more generally. */
284 sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format, unsigned int index = 0);
285
286 GrAuditTrail* getAuditTrail() { return &fContext->fAuditTrail; }
287
288 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
289
290 /** This is only useful for debug purposes */
291 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fContext->fSingleOwner; } )
292
robertphillips4fd74ae2016-08-03 14:26:53 -0700293private:
294 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400295 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700296 GrContextPriv& operator=(const GrContextPriv&); // unimpl
297
298 // No taking addresses of this type.
299 const GrContextPriv* operator&() const;
300 GrContextPriv* operator&();
301
302 GrContext* fContext;
303
304 friend class GrContext; // to construct/copy this type.
305};
306
307inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
308
309inline const GrContextPriv GrContext::contextPriv () const {
310 return GrContextPriv(const_cast<GrContext*>(this));
311}
312
313#endif