blob: 4f392f88b3c20298903d0794918092c3a10f1bc2 [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;
Robert Phillipsc994a932018-06-19 13:09:54 -040016class GrOpMemoryPool;
Brian Salomond17f6582017-07-19 18:28:58 -040017class GrOnFlushCallbackObject;
Greg Danield85f97d2017-03-07 13:37:21 -050018class GrSemaphore;
Robert Phillipse2f7d182016-12-15 09:23:05 -050019class GrSurfaceProxy;
Brian Salomond17f6582017-07-19 18:28:58 -040020class GrTextureContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -050021
Robert Phillips62000362018-02-01 09:10:04 -050022class SkDeferredDisplayList;
23
robertphillips4fd74ae2016-08-03 14:26:53 -070024/** Class that adds methods to GrContext that are only intended for use internal to Skia.
25 This class is purely a privileged window into GrContext. It should never have additional
26 data members or virtual methods. */
27class GrContextPriv {
28public:
Robert Phillipse42edcc2017-12-13 11:50:22 -050029 /**
30 * Create a GrContext without a resource cache
31 */
Kevin Lubickb5502b22018-03-12 10:17:06 -040032 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -050033
Brian Salomonc7fe0f72018-05-11 10:14:21 -040034 const GrCaps* caps() const { return fContext->fCaps.get(); }
35
Robert Phillipsc994a932018-06-19 13:09:54 -040036 sk_sp<GrOpMemoryPool> refOpMemoryPool();
37 GrOpMemoryPool* opMemoryPool();
38
bungeman6bd52842016-10-27 09:30:08 -070039 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070040
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050041 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
42 sk_sp<SkColorSpace> = nullptr,
43 const SkSurfaceProps* = nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -050044
Robert Phillipsbf25d432017-04-07 10:08:53 -040045 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -050046 GrSurfaceOrigin,
Greg Daniel65c7f662017-10-30 13:39:09 -040047 GrMipMapped,
Robert Phillipsbf25d432017-04-07 10:08:53 -040048 SkBackingFit,
Brian Salomon366093f2018-02-13 09:25:22 -050049 SkBudgeted,
50 sk_sp<SkColorSpace> colorSpace = nullptr,
51 const SkSurfaceProps* = nullptr);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050052
Brian Salomond17f6582017-07-19 18:28:58 -040053 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +000054 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -050055 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -050056
Brian Osman11052242016-10-27 14:47:55 -040057 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000058 const GrBackendTexture& tex,
59 GrSurfaceOrigin origin,
60 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -040061 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -050062 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070063
Brian Osman11052242016-10-27 14:47:55 -040064 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +000065 const GrBackendRenderTarget&,
66 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -040067 sk_sp<SkColorSpace> colorSpace,
68 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070069
Brian Osman11052242016-10-27 14:47:55 -040070 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000071 const GrBackendTexture& tex,
72 GrSurfaceOrigin origin,
73 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -070074 sk_sp<SkColorSpace> colorSpace,
75 const SkSurfaceProps* = nullptr);
76
Brian Osman46da1cc2017-02-14 14:15:48 -050077 bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
Brian Osman8a83ca42018-02-12 14:32:17 -050078 bool sharpenMipmappedTextures() const { return fContext->fSharpenMipmappedTextures; }
Brian Osman46da1cc2017-02-14 14:15:48 -050079
Robert Phillips7ee385e2017-03-30 08:02:11 -040080 /**
81 * Call to ensure all drawing to the context has been issued to the
82 * underlying 3D API.
83 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
84 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
85 * provided then all current work will be flushed.
86 */
87 void flush(GrSurfaceProxy*);
88
Chris Daltonfe199b72017-05-05 11:26:15 -040089 /**
90 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
91 *
92 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
93 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -040094 */
Chris Daltonfe199b72017-05-05 11:26:15 -040095 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
96
97 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipseb35f4d2017-03-21 07:56:47 -040098
Robert Phillips7ee385e2017-03-30 08:02:11 -040099 /**
100 * After this returns any pending writes to the surface will have been issued to the
101 * backend 3D API.
102 */
103 void flushSurfaceWrites(GrSurfaceProxy*);
104
105 /**
106 * After this returns any pending reads or writes to the surface will have been issued to the
107 * backend 3D API.
108 */
109 void flushSurfaceIO(GrSurfaceProxy*);
110
111 /**
112 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
113 * if necessary.
114 *
115 * It is not necessary to call this before reading the render target via Skia/GrContext.
116 * GrContext will detect when it must perform a resolve before reading pixels back from the
117 * surface or using it as a texture.
118 */
119 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
120
Robert Phillipse78b7252017-04-06 07:59:41 -0400121 /**
122 * These flags can be used with the read/write pixels functions below.
123 */
124 enum PixelOpsFlags {
125 /** The GrContext will not be flushed before the surface read or write. This means that
126 the read or write may occur before previous draws have executed. */
127 kDontFlush_PixelOpsFlag = 0x1,
128 /** Any surface writes should be flushed to the backend 3D API after the surface operation
129 is complete */
130 kFlushWrites_PixelOp = 0x2,
131 /** The src for write or dst read is unpremultiplied. This is only respected if both the
132 config src and dst configs are an RGBA/BGRA 8888 format. */
133 kUnpremul_PixelOpsFlag = 0x4,
134 };
135
136 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400137 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400138 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400139 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400140 * @param left left edge of the rectangle to read (inclusive)
141 * @param top top edge of the rectangle to read (inclusive)
142 * @param width width of rectangle to read in pixels.
143 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400144 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400145 * @param dstColorSpace color space of the destination buffer
146 * @param buffer memory to read the rectangle into.
147 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
148 * packed.
149 * @param pixelOpsFlags see PixelOpsFlags enum above.
150 *
151 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
152 * pixel configs
153 */
Brian Salomonc320b152018-02-20 14:05:36 -0500154 bool readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, int height,
155 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
156 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400157
158 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400159 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500160 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400161 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400162 * @param left left edge of the rectangle to write (inclusive)
163 * @param top top edge of the rectangle to write (inclusive)
164 * @param width width of rectangle to write in pixels.
165 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500166 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400167 * @param srcColorSpace color space of the source buffer
168 * @param buffer memory to read pixels from
169 * @param rowBytes number of bytes between consecutive rows. Zero
170 * means rows are tightly packed.
171 * @param pixelOpsFlags see PixelOpsFlags enum above.
172 * @return true if the write succeeded, false if not. The write can fail because of an
173 * unsupported combination of surface and src configs.
174 */
Brian Salomonc320b152018-02-20 14:05:36 -0500175 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
176 GrColorType srcColorType, SkColorSpace* srcColorSpace,
177 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400178
Greg Danielfc978fd2017-04-13 09:54:12 -0400179 GrBackend getBackend() const { return fContext->fBackend; }
180
Brian Osman51279982017-08-23 10:12:00 -0400181 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
182
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500183 GrProxyProvider* proxyProvider() { return fContext->fProxyProvider; }
184 const GrProxyProvider* proxyProvider() const { return fContext->fProxyProvider; }
185
Robert Phillips6be756b2018-01-16 15:07:54 -0500186 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
187 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
188
189 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
190
Robert Phillips41a3b872018-03-09 12:00:34 -0500191 GrTextureStripAtlasManager* textureStripAtlasManager() {
192 return fContext->fTextureStripAtlasManager.get();
193 }
194
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500195 GrGpu* getGpu() { return fContext->fGpu.get(); }
196 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
197
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500198 GrGlyphCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500199 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500200
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500201 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500202 GrAtlasManager* getAtlasManager() {
203 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500204 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500205
Robert Phillips62000362018-02-01 09:10:04 -0500206 void moveOpListsToDDL(SkDeferredDisplayList*);
207 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
208
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500209 /**
210 * Purge all the unlocked resources from the cache.
211 * This entry point is mainly meant for timing texture uploads
212 * and is not defined in normal builds of Skia.
213 */
214 void purgeAllUnlockedResources_ForTesting();
215
216
217 /*
218 * Create a new render target context backed by a deferred-style
219 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
220 * renderTargetContexts created via this entry point.
221 */
222 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
223 SkBackingFit fit,
224 int width, int height,
225 GrPixelConfig config,
226 sk_sp<SkColorSpace> colorSpace,
227 int sampleCnt = 1,
228 GrMipMapped = GrMipMapped::kNo,
229 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
230 const SkSurfaceProps* surfaceProps = nullptr,
231 SkBudgeted = SkBudgeted::kYes);
232 /*
233 * This method will attempt to create a renderTargetContext that has, at least, the number of
234 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
235 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
236 * SRGB-ness will be preserved.
237 */
238 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
239 SkBackingFit fit,
240 int width, int height,
241 GrPixelConfig config,
242 sk_sp<SkColorSpace> colorSpace,
243 int sampleCnt = 1,
244 GrMipMapped = GrMipMapped::kNo,
245 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
246 const SkSurfaceProps* surfaceProps = nullptr,
247 SkBudgeted budgeted = SkBudgeted::kYes);
248
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500249 /** Reset GPU stats */
250 void resetGpuStats() const ;
251
252 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
253 void dumpCacheStats(SkString*) const;
254 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
255 void printCacheStats() const;
256
257 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
258 void dumpGpuStats(SkString*) const;
259 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
260 void printGpuStats() const;
261
262 /** Returns a string with detailed information about the context & GPU, in JSON format. */
263 SkString dump() const;
264
265 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
266 this is for testing only */
267 void setTextBlobCacheLimit_ForTesting(size_t bytes);
268
269 /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be
270 to an array of 3 entries */
271 void setTextContextAtlasSizes_ForTesting(const GrDrawOpAtlasConfig* configs);
272
273 /** 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
282 /** This is only useful for debug purposes */
283 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fContext->fSingleOwner; } )
284
robertphillips4fd74ae2016-08-03 14:26:53 -0700285private:
286 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400287 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700288 GrContextPriv& operator=(const GrContextPriv&); // unimpl
289
290 // No taking addresses of this type.
291 const GrContextPriv* operator&() const;
292 GrContextPriv* operator&();
293
294 GrContext* fContext;
295
296 friend class GrContext; // to construct/copy this type.
297};
298
299inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
300
301inline const GrContextPriv GrContext::contextPriv () const {
302 return GrContextPriv(const_cast<GrContext*>(this));
303}
304
305#endif