blob: afb3949f4c27de729b569818ca8f22e48e5801fa [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;
Ethan Nicholas00543112018-07-31 09:44:36 -040019class GrSkSLFPFactory;
Robert Phillipse2f7d182016-12-15 09:23:05 -050020class GrSurfaceProxy;
Brian Salomond17f6582017-07-19 18:28:58 -040021class GrTextureContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -050022
Robert Phillips62000362018-02-01 09:10:04 -050023class SkDeferredDisplayList;
24
robertphillips4fd74ae2016-08-03 14:26:53 -070025/** Class that adds methods to GrContext that are only intended for use internal to Skia.
26 This class is purely a privileged window into GrContext. It should never have additional
27 data members or virtual methods. */
28class GrContextPriv {
29public:
Robert Phillipse42edcc2017-12-13 11:50:22 -050030 /**
31 * Create a GrContext without a resource cache
32 */
Kevin Lubickb5502b22018-03-12 10:17:06 -040033 static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
Robert Phillipse42edcc2017-12-13 11:50:22 -050034
Brian Salomonc7fe0f72018-05-11 10:14:21 -040035 const GrCaps* caps() const { return fContext->fCaps.get(); }
36
Robert Phillipsc994a932018-06-19 13:09:54 -040037 sk_sp<GrOpMemoryPool> refOpMemoryPool();
38 GrOpMemoryPool* opMemoryPool();
39
bungeman6bd52842016-10-27 09:30:08 -070040 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070041
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050042 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
43 sk_sp<SkColorSpace> = nullptr,
44 const SkSurfaceProps* = nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -050045
Robert Phillipsbf25d432017-04-07 10:08:53 -040046 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrSurfaceDesc&,
Brian Salomon2a4f9832018-03-03 22:43:43 -050047 GrSurfaceOrigin,
Greg Daniel65c7f662017-10-30 13:39:09 -040048 GrMipMapped,
Robert Phillipsbf25d432017-04-07 10:08:53 -040049 SkBackingFit,
Brian Salomon366093f2018-02-13 09:25:22 -050050 SkBudgeted,
51 sk_sp<SkColorSpace> colorSpace = nullptr,
52 const SkSurfaceProps* = nullptr);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050053
Brian Salomond17f6582017-07-19 18:28:58 -040054 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +000055 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -050056 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -050057
Brian Osman11052242016-10-27 14:47:55 -040058 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000059 const GrBackendTexture& tex,
60 GrSurfaceOrigin origin,
61 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -040062 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -050063 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070064
Brian Osman11052242016-10-27 14:47:55 -040065 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +000066 const GrBackendRenderTarget&,
67 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -040068 sk_sp<SkColorSpace> colorSpace,
69 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070070
Brian Osman11052242016-10-27 14:47:55 -040071 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000072 const GrBackendTexture& tex,
73 GrSurfaceOrigin origin,
74 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -070075 sk_sp<SkColorSpace> colorSpace,
76 const SkSurfaceProps* = nullptr);
77
Brian Osman46da1cc2017-02-14 14:15:48 -050078 bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
Brian Osman8a83ca42018-02-12 14:32:17 -050079 bool sharpenMipmappedTextures() const { return fContext->fSharpenMipmappedTextures; }
Brian Osman46da1cc2017-02-14 14:15:48 -050080
Robert Phillips7ee385e2017-03-30 08:02:11 -040081 /**
82 * Call to ensure all drawing to the context has been issued to the
83 * underlying 3D API.
84 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
85 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
86 * provided then all current work will be flushed.
87 */
88 void flush(GrSurfaceProxy*);
89
Chris Daltonfe199b72017-05-05 11:26:15 -040090 /**
91 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
92 *
93 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
94 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -040095 */
Chris Daltonfe199b72017-05-05 11:26:15 -040096 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
97
98 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipseb35f4d2017-03-21 07:56:47 -040099
Robert Phillips7ee385e2017-03-30 08:02:11 -0400100 /**
101 * After this returns any pending writes to the surface will have been issued to the
102 * backend 3D API.
103 */
104 void flushSurfaceWrites(GrSurfaceProxy*);
105
106 /**
107 * After this returns any pending reads or writes to the surface will have been issued to the
108 * backend 3D API.
109 */
110 void flushSurfaceIO(GrSurfaceProxy*);
111
112 /**
113 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
114 * if necessary.
115 *
116 * It is not necessary to call this before reading the render target via Skia/GrContext.
117 * GrContext will detect when it must perform a resolve before reading pixels back from the
118 * surface or using it as a texture.
119 */
120 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
121
Robert Phillipse78b7252017-04-06 07:59:41 -0400122 /**
123 * These flags can be used with the read/write pixels functions below.
124 */
125 enum PixelOpsFlags {
126 /** The GrContext will not be flushed before the surface read or write. This means that
127 the read or write may occur before previous draws have executed. */
128 kDontFlush_PixelOpsFlag = 0x1,
129 /** Any surface writes should be flushed to the backend 3D API after the surface operation
130 is complete */
131 kFlushWrites_PixelOp = 0x2,
132 /** The src for write or dst read is unpremultiplied. This is only respected if both the
133 config src and dst configs are an RGBA/BGRA 8888 format. */
134 kUnpremul_PixelOpsFlag = 0x4,
135 };
136
137 /**
Brian Salomond494f6e2018-06-01 14:13:43 -0400138 * Reads a rectangle of pixels from a surface.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400139 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400140 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400141 * @param left left edge of the rectangle to read (inclusive)
142 * @param top top edge of the rectangle to read (inclusive)
143 * @param width width of rectangle to read in pixels.
144 * @param height height of rectangle to read in pixels.
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400145 * @param dstColorType the color type of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400146 * @param dstColorSpace color space of the destination buffer
147 * @param buffer memory to read the rectangle into.
148 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
149 * packed.
150 * @param pixelOpsFlags see PixelOpsFlags enum above.
151 *
152 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
153 * pixel configs
154 */
Brian Salomonc320b152018-02-20 14:05:36 -0500155 bool readSurfacePixels(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 Salomond494f6e2018-06-01 14:13:43 -0400160 * Writes a rectangle of pixels to a surface.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500161 *
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400162 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400163 * @param left left edge of the rectangle to write (inclusive)
164 * @param top top edge of the rectangle to write (inclusive)
165 * @param width width of rectangle to write in pixels.
166 * @param height height of rectangle to write in pixels.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500167 * @param srcColorType the color type of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400168 * @param srcColorSpace color space of the source buffer
169 * @param buffer memory to read pixels from
170 * @param rowBytes number of bytes between consecutive rows. Zero
171 * means rows are tightly packed.
172 * @param pixelOpsFlags see PixelOpsFlags enum above.
173 * @return true if the write succeeded, false if not. The write can fail because of an
174 * unsupported combination of surface and src configs.
175 */
Brian Salomonc320b152018-02-20 14:05:36 -0500176 bool writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, int height,
177 GrColorType srcColorType, SkColorSpace* srcColorSpace,
178 const void* buffer, size_t rowBytes, uint32_t pixelOpsFlags = 0);
Robert Phillipse78b7252017-04-06 07:59:41 -0400179
Greg Danielfc978fd2017-04-13 09:54:12 -0400180 GrBackend getBackend() const { return fContext->fBackend; }
181
Brian Osman51279982017-08-23 10:12:00 -0400182 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
183
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500184 GrProxyProvider* proxyProvider() { return fContext->fProxyProvider; }
185 const GrProxyProvider* proxyProvider() const { return fContext->fProxyProvider; }
186
Robert Phillips6be756b2018-01-16 15:07:54 -0500187 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
188 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
189
190 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
191
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500192 GrGpu* getGpu() { return fContext->fGpu.get(); }
193 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
194
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500195 GrGlyphCache* getGlyphCache() { return fContext->fGlyphCache; }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500196 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
Robert Phillips1056eb82018-03-01 14:16:41 -0500197
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500198 // This accessor should only ever be called by the GrOpFlushState.
Robert Phillips5a66efb2018-03-07 15:13:18 -0500199 GrAtlasManager* getAtlasManager() {
200 return fContext->onGetAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500201 }
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500202
Robert Phillips62000362018-02-01 09:10:04 -0500203 void moveOpListsToDDL(SkDeferredDisplayList*);
204 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
205
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500206 /**
207 * Purge all the unlocked resources from the cache.
208 * This entry point is mainly meant for timing texture uploads
209 * and is not defined in normal builds of Skia.
210 */
211 void purgeAllUnlockedResources_ForTesting();
212
213
214 /*
215 * Create a new render target context backed by a deferred-style
216 * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
217 * renderTargetContexts created via this entry point.
218 */
219 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
220 SkBackingFit fit,
221 int width, int height,
222 GrPixelConfig config,
223 sk_sp<SkColorSpace> colorSpace,
224 int sampleCnt = 1,
225 GrMipMapped = GrMipMapped::kNo,
226 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
227 const SkSurfaceProps* surfaceProps = nullptr,
228 SkBudgeted = SkBudgeted::kYes);
229 /*
230 * This method will attempt to create a renderTargetContext that has, at least, the number of
231 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
232 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
233 * SRGB-ness will be preserved.
234 */
235 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
236 SkBackingFit fit,
237 int width, int height,
238 GrPixelConfig config,
239 sk_sp<SkColorSpace> colorSpace,
240 int sampleCnt = 1,
241 GrMipMapped = GrMipMapped::kNo,
242 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
243 const SkSurfaceProps* surfaceProps = nullptr,
244 SkBudgeted budgeted = SkBudgeted::kYes);
245
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500246 /** Reset GPU stats */
247 void resetGpuStats() const ;
248
249 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
250 void dumpCacheStats(SkString*) const;
251 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
252 void printCacheStats() const;
253
254 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
255 void dumpGpuStats(SkString*) const;
256 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
257 void printGpuStats() const;
258
259 /** Returns a string with detailed information about the context & GPU, in JSON format. */
260 SkString dump() const;
261
262 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
263 this is for testing only */
264 void setTextBlobCacheLimit_ForTesting(size_t bytes);
265
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500266 /** Get pointer to atlas texture for given mask format. Note that this wraps an
267 actively mutating texture in an SkImage. This could yield unexpected results
268 if it gets cached or used more generally. */
269 sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format, unsigned int index = 0);
270
271 GrAuditTrail* getAuditTrail() { return &fContext->fAuditTrail; }
272
273 GrContextOptions::PersistentCache* getPersistentCache() { return fContext->fPersistentCache; }
274
Ethan Nicholas00543112018-07-31 09:44:36 -0400275 sk_sp<GrSkSLFPFactoryCache> getFPFactoryCache() {
276 return fContext->fFPFactoryCache;
277 }
278
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500279 /** This is only useful for debug purposes */
280 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fContext->fSingleOwner; } )
281
robertphillips4fd74ae2016-08-03 14:26:53 -0700282private:
283 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400284 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700285 GrContextPriv& operator=(const GrContextPriv&); // unimpl
286
287 // No taking addresses of this type.
288 const GrContextPriv* operator&() const;
289 GrContextPriv* operator&();
290
291 GrContext* fContext;
292
293 friend class GrContext; // to construct/copy this type.
294};
295
296inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
297
298inline const GrContextPriv GrContext::contextPriv () const {
299 return GrContextPriv(const_cast<GrContext*>(this));
300}
301
302#endif