blob: ccd27286c7341b410992fabbfa9300ca2de21937 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
8#ifndef GrContext_DEFINED
9#define GrContext_DEFINED
10
robertphillipscaef3452015-11-11 13:18:11 -080011#include "GrCaps.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000012#include "GrColor.h"
rmistry@google.comfbfcd562012-08-23 18:09:54 +000013#include "GrRenderTarget.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000014#include "SkMatrix.h"
egdaniele61c4112014-06-12 10:24:21 -070015#include "SkPathEffect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000016#include "SkTypes.h"
joshualitt27a48dc2016-01-08 07:19:47 -080017#include "../private/GrAuditTrail.h"
joshualitt1de610a2016-01-06 08:26:09 -080018#include "../private/GrSingleOwner.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000019
Brian Salomonf856fd12016-12-16 14:24:34 -050020class GrAtlasGlyphCache;
bsalomon682c2692015-05-22 14:01:46 -070021struct GrContextOptions;
robertphillips4fd74ae2016-08-03 14:26:53 -070022class GrContextPriv;
bsalomon41b952c2016-03-11 06:46:33 -080023class GrContextThreadSafeProxy;
robertphillips77a2e522015-10-17 07:43:27 -070024class GrDrawingManager;
Brian Salomon2ee084e2016-12-16 18:59:19 -050025struct GrDrawOpAtlasConfig;
Brian Osman11052242016-10-27 14:47:55 -040026class GrRenderTargetContext;
joshualittb0a8a372014-09-23 09:50:21 -070027class GrFragmentProcessor;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000028class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000029class GrIndexBuffer;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000030class GrOvalRenderer;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000031class GrPath;
egdaniel8dd688b2015-01-22 10:16:09 -080032class GrPipelineBuilder;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000033class GrResourceEntry;
bsalomon0ea80f42015-02-11 10:49:59 -080034class GrResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -070035class GrResourceProvider;
Mike Reed84dd8572017-03-08 22:21:00 -050036class GrSamplerParams;
Robert Phillips7ee385e2017-03-30 08:02:11 -040037class GrSurfaceProxy;
joshualittb7133be2015-04-08 09:08:31 -070038class GrTextBlobCache;
kkinnunenc6cb56f2014-06-24 00:12:27 -070039class GrTextContext;
Mike Reed84dd8572017-03-08 22:21:00 -050040class GrTextureProxy;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000041class GrVertexBuffer;
bsalomon6c9cd552016-01-22 07:17:34 -080042class GrSwizzle;
ericrk0a5fa482015-09-15 14:16:10 -070043class SkTraceMemoryDump;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000044
Mike Reed84dd8572017-03-08 22:21:00 -050045class SkImage;
46class SkSurfaceProps;
47
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000048class SK_API GrContext : public SkRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000049public:
50 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000051 * Creates a GrContext for a backend context.
bsalomon@google.com27847de2011-02-22 20:59:41 +000052 */
bsalomon682c2692015-05-22 14:01:46 -070053 static GrContext* Create(GrBackend, GrBackendContext, const GrContextOptions& options);
54 static GrContext* Create(GrBackend, GrBackendContext);
bsalomon@google.com27847de2011-02-22 20:59:41 +000055
bsalomon33435572014-11-05 14:47:41 -080056 /**
57 * Only defined in test apps.
58 */
59 static GrContext* CreateMockContext();
60
bsalomon@google.com27847de2011-02-22 20:59:41 +000061 virtual ~GrContext();
62
bungeman6bd52842016-10-27 09:30:08 -070063 sk_sp<GrContextThreadSafeProxy> threadSafeProxy();
bsalomon41b952c2016-03-11 06:46:33 -080064
bsalomon@google.com27847de2011-02-22 20:59:41 +000065 /**
66 * The GrContext normally assumes that no outsider is setting state
67 * within the underlying 3D API's context/device/whatever. This call informs
68 * the context that the state was modified and it should resend. Shouldn't
69 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000070 * The flag bits, state, is dpendent on which backend is used by the
71 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000072 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000073 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000074
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000076 * Callback function to allow classes to cleanup on GrContext destruction.
77 * The 'info' field is filled in with the 'info' passed to addCleanUp.
78 */
79 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
80
81 /**
82 * Add a function to be called from within GrContext's destructor.
83 * This gives classes a chance to free resources held on a per context basis.
84 * The 'info' parameter will be stored and passed to the callback function.
85 */
86 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
87 CleanUpData* entry = fCleanUpData.push();
88
89 entry->fFunc = cleanUp;
90 entry->fInfo = info;
91 }
92
93 /**
bsalomon6e2aad42016-04-01 11:54:31 -070094 * Abandons all GPU resources and assumes the underlying backend 3D API context is not longer
95 * usable. Call this if you have lost the associated GPU context, and thus internal texture,
96 * buffer, etc. references/IDs are now invalid. Calling this ensures that the destructors of the
97 * GrContext and any of its created resource objects will not make backend 3D API calls. Content
98 * rendered but not previously flushed may be lost. After this function is called all subsequent
99 * calls on the GrContext will fail or be no-ops.
100 *
101 * The typical use case for this function is that the underlying 3D context was lost and further
102 * API calls may crash.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000103 */
bsalomon2354f842014-07-28 13:48:36 -0700104 void abandonContext();
junov@google.com53a55842011-06-08 22:55:10 +0000105
bsalomon6e2aad42016-04-01 11:54:31 -0700106 /**
107 * This is similar to abandonContext() however the underlying 3D context is not yet lost and
108 * the GrContext will cleanup all allocated resources before returning. After returning it will
109 * assume that the underlying context may no longer be valid.
110 *
111 * The typical use case for this function is that the client is going to destroy the 3D context
112 * but can't guarantee that GrContext will be destroyed first (perhaps because it may be ref'ed
113 * elsewhere by either the client or Skia objects).
114 */
115 void releaseResourcesAndAbandonContext();
116
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000117 ///////////////////////////////////////////////////////////////////////////
118 // Resource Cache
119
120 /**
121 * Return the current GPU resource cache limits.
122 *
123 * @param maxResources If non-null, returns maximum number of resources that
124 * can be held in the cache.
125 * @param maxResourceBytes If non-null, returns maximum number of bytes of
126 * video memory that can be held in the cache.
127 */
128 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000129
130 /**
131 * Gets the current GPU resource cache usage.
132 *
133 * @param resourceCount If non-null, returns the number of resources that are held in the
134 * cache.
135 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
136 * in the cache.
137 */
138 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
139
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000140 /**
141 * Specify the GPU resource cache limits. If the current cache exceeds either
142 * of these, it will be purged (LRU) to keep the cache within these limits.
143 *
144 * @param maxResources The maximum number of resources that can be held in
145 * the cache.
146 * @param maxResourceBytes The maximum number of bytes of video memory
147 * that can be held in the cache.
148 */
149 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000150
junov@google.com53a55842011-06-08 22:55:10 +0000151 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000152 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000153 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000154 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000155 void freeGpuResources();
156
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000157 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000158 * Purge all the unlocked resources from the cache.
159 * This entry point is mainly meant for timing texture uploads
160 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000161 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000162 void purgeAllUnlockedResources();
163
Brian Salomon5e150852017-03-22 14:53:13 -0400164 /**
165 * Purge GPU resources that haven't been used in the past 'ms' milliseconds, regardless of
166 * whether the context is currently under budget.
167 */
168 void purgeResourcesNotUsedInMs(std::chrono::milliseconds ms);
169
bsalomon76228632015-05-29 08:02:10 -0700170 /** Access the context capabilities */
171 const GrCaps* caps() const { return fCaps; }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000172
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000173 /**
174 * Returns the recommended sample count for a render target when using this
175 * context.
176 *
177 * @param config the configuration of the render target.
178 * @param dpi the display density in dots per inch.
179 *
180 * @return sample count that should be perform well and have good enough
181 * rendering quality for the display. Alternatively returns 0 if
182 * MSAA is not supported or recommended to be used by default.
183 */
184 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
185
bsalomon@google.com27847de2011-02-22 20:59:41 +0000186 /**
Brian Osman11052242016-10-27 14:47:55 -0400187 * Create both a GrRenderTarget and a matching GrRenderTargetContext to wrap it.
188 * We guarantee that "asTexture" will succeed for renderTargetContexts created
robertphillipsebf30e82016-05-11 08:34:39 -0700189 * via this entry point.
robertphillipsd4c741e2016-04-28 09:55:15 -0700190 */
Brian Osman11052242016-10-27 14:47:55 -0400191 sk_sp<GrRenderTargetContext> makeRenderTargetContext(
192 SkBackingFit fit,
193 int width, int height,
194 GrPixelConfig config,
195 sk_sp<SkColorSpace> colorSpace,
196 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500197 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
Brian Osman11052242016-10-27 14:47:55 -0400198 const SkSurfaceProps* surfaceProps = nullptr,
199 SkBudgeted = SkBudgeted::kYes);
robertphillipsd4c741e2016-04-28 09:55:15 -0700200
Brian Salomonaeac5d82016-10-29 00:13:29 -0400201 // Create a new render target context as above but have it backed by a deferred-style
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400202 // GrRenderTargetProxy rather than one that is backed by an actual GrRenderTarget
robertphillipsd728f0c2016-11-21 11:05:03 -0800203 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
204 SkBackingFit fit,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400205 int width, int height,
206 GrPixelConfig config,
207 sk_sp<SkColorSpace> colorSpace,
208 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500209 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400210 const SkSurfaceProps* surfaceProps = nullptr,
211 SkBudgeted = SkBudgeted::kYes);
robertphillips48fde9c2016-09-06 05:20:20 -0700212 /*
Brian Osman11052242016-10-27 14:47:55 -0400213 * This method will attempt to create a renderTargetContext that has, at least, the number of
robertphillips48fde9c2016-09-06 05:20:20 -0700214 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
215 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
216 * SRGB-ness will be preserved.
217 */
Brian Osman11052242016-10-27 14:47:55 -0400218 sk_sp<GrRenderTargetContext> makeRenderTargetContextWithFallback(
robertphillips48fde9c2016-09-06 05:20:20 -0700219 SkBackingFit fit,
220 int width, int height,
221 GrPixelConfig config,
222 sk_sp<SkColorSpace> colorSpace,
223 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500224 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
robertphillips48fde9c2016-09-06 05:20:20 -0700225 const SkSurfaceProps* surfaceProps = nullptr,
226 SkBudgeted budgeted = SkBudgeted::kYes);
227
robertphillipsd728f0c2016-11-21 11:05:03 -0800228 // Create a new render target context as above but have it backed by a deferred-style
229 // GrRenderTargetProxy rather than one that is backed by an actual GrRenderTarget
230 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
231 SkBackingFit fit,
232 int width, int height,
233 GrPixelConfig config,
234 sk_sp<SkColorSpace> colorSpace,
235 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500236 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
robertphillipsd728f0c2016-11-21 11:05:03 -0800237 const SkSurfaceProps* surfaceProps = nullptr,
238 SkBudgeted budgeted = SkBudgeted::kYes);
239
bsalomon@google.com27847de2011-02-22 20:59:41 +0000240 ///////////////////////////////////////////////////////////////////////////
241 // Misc.
242
243 /**
244 * Call to ensure all drawing to the context has been issued to the
245 * underlying 3D API.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000246 */
bsalomonb77a9072016-09-07 10:02:04 -0700247 void flush();
robertphillipsea461502015-05-26 11:38:03 -0700248
bsalomon@google.com0342a852012-08-20 19:22:38 +0000249 /**
250 * These flags can be used with the read/write pixels functions below.
251 */
252 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700253 /** The GrContext will not be flushed before the surface read or write. This means that
254 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000255 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700256 /** Any surface writes should be flushed to the backend 3D API after the surface operation
257 is complete */
258 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000259 /** The src for write or dst read is unpremultiplied. This is only respected if both the
260 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700261 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000262 };
263
bsalomon@google.com27847de2011-02-22 20:59:41 +0000264 /**
bsalomone8d21e82015-07-16 08:23:13 -0700265 * Reads a rectangle of pixels from a surface.
266 * @param surface the surface to read from.
Brian Osmanb62ea222016-12-22 11:12:16 -0500267 * @param srcColorSpace color space of the surface
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000268 * @param left left edge of the rectangle to read (inclusive)
269 * @param top top edge of the rectangle to read (inclusive)
270 * @param width width of rectangle to read in pixels.
271 * @param height height of rectangle to read in pixels.
272 * @param config the pixel config of the destination buffer
Brian Osmanb62ea222016-12-22 11:12:16 -0500273 * @param dstColorSpace color space of the destination buffer
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000274 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000275 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
276 * packed.
277 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000278 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000279 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
bsalomone8d21e82015-07-16 08:23:13 -0700280 * pixel configs
bsalomon@google.com27847de2011-02-22 20:59:41 +0000281 */
Brian Osmanb62ea222016-12-22 11:12:16 -0500282 bool readSurfacePixels(GrSurface* surface, SkColorSpace* srcColorSpace,
bsalomone8d21e82015-07-16 08:23:13 -0700283 int left, int top, int width, int height,
Brian Osmanb62ea222016-12-22 11:12:16 -0500284 GrPixelConfig config, SkColorSpace* dstColorSpace, void* buffer,
bsalomone8d21e82015-07-16 08:23:13 -0700285 size_t rowBytes = 0,
286 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000287
288 /**
bsalomon81beccc2014-10-13 12:32:55 -0700289 * Writes a rectangle of pixels to a surface.
290 * @param surface the surface to write to.
Brian Osmanb62ea222016-12-22 11:12:16 -0500291 * @param dstColorSpace color space of the surface
bsalomon@google.com6f379512011-11-16 20:36:03 +0000292 * @param left left edge of the rectangle to write (inclusive)
293 * @param top top edge of the rectangle to write (inclusive)
294 * @param width width of rectangle to write in pixels.
295 * @param height height of rectangle to write in pixels.
296 * @param config the pixel config of the source buffer
Brian Osmanb62ea222016-12-22 11:12:16 -0500297 * @param srcColorSpace color space of the source buffer
bsalomon@google.com6f379512011-11-16 20:36:03 +0000298 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000299 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000300 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000301 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000302 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700303 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000304 */
Brian Osmanb62ea222016-12-22 11:12:16 -0500305 bool writeSurfacePixels(GrSurface* surface, SkColorSpace* dstColorSpace,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000306 int left, int top, int width, int height,
Brian Osmanb62ea222016-12-22 11:12:16 -0500307 GrPixelConfig config, SkColorSpace* srcColorSpace, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000308 size_t rowBytes,
309 uint32_t pixelOpsFlags = 0);
310
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000311 /**
joshualitt0acd0d32015-05-07 08:23:19 -0700312 * An ID associated with this context, guaranteed to be unique.
313 */
314 uint32_t uniqueID() { return fUniqueID; }
315
bsalomon@google.com27847de2011-02-22 20:59:41 +0000316 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000317 // Functions intended for internal use only.
318 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000319 const GrGpu* getGpu() const { return fGpu; }
Brian Salomonf856fd12016-12-16 14:24:34 -0500320 GrAtlasGlyphCache* getAtlasGlyphCache() { return fAtlasGlyphCache; }
bungeman6bd52842016-10-27 09:30:08 -0700321 GrTextBlobCache* getTextBlobCache() { return fTextBlobCache.get(); }
robertphillips77a2e522015-10-17 07:43:27 -0700322 bool abandoned() const;
bsalomond309e7a2015-04-30 14:18:54 -0700323 GrResourceProvider* resourceProvider() { return fResourceProvider; }
324 const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
bsalomon0ea80f42015-02-11 10:49:59 -0800325 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000326
joshualitte45c81c2015-12-02 09:05:37 -0800327 /** Reset GPU stats */
328 void resetGpuStats() const ;
329
mtkleinb9eb4ac2015-02-02 18:26:03 -0800330 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
331 void dumpCacheStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800332 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000333 void printCacheStats() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000334
mtkleinb9eb4ac2015-02-02 18:26:03 -0800335 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
336 void dumpGpuStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800337 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800338 void printGpuStats() const;
robertphillips754f4e92014-09-18 13:52:08 -0700339
joshualitt17d833b2015-08-03 10:17:44 -0700340 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
341 this is for testing only */
342 void setTextBlobCacheLimit_ForTesting(size_t bytes);
343
joshualittda04e0e2015-08-19 08:16:43 -0700344 /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be
345 to an array of 3 entries */
Brian Salomon2ee084e2016-12-16 18:59:19 -0500346 void setTextContextAtlasSizes_ForTesting(const GrDrawOpAtlasConfig* configs);
joshualittda04e0e2015-08-19 08:16:43 -0700347
ericrk0a5fa482015-09-15 14:16:10 -0700348 /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
349 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
350
Robert Phillips33667da2016-12-20 11:05:30 -0500351 /** Get pointer to atlas texture for given mask format. Note that this wraps an
352 actively mutating texture in an SkImage. This could yield unexpected results
353 if it gets cached or used more generally. */
Robert Phillipsc37e6142017-01-20 10:03:25 -0500354 sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format);
jvanverth629162d2015-11-08 08:07:24 -0800355
joshualitt5651ee62016-01-11 10:39:11 -0800356 GrAuditTrail* getAuditTrail() { return &fAuditTrail; }
joshualitt27a48dc2016-01-08 07:19:47 -0800357
joshualittce894002016-01-11 13:29:31 -0800358 /** This is only useful for debug purposes */
359 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } )
360
robertphillips4fd74ae2016-08-03 14:26:53 -0700361 // Provides access to functions that aren't part of the public API.
362 GrContextPriv contextPriv();
363 const GrContextPriv contextPriv() const;
364
bsalomon@google.com27847de2011-02-22 20:59:41 +0000365private:
bsalomon41b952c2016-03-11 06:46:33 -0800366 GrGpu* fGpu;
367 const GrCaps* fCaps;
368 GrResourceCache* fResourceCache;
Brian Osman32342f02017-03-04 08:12:46 -0500369 GrResourceProvider* fResourceProvider;
bsalomond309e7a2015-04-30 14:18:54 -0700370
bungeman6bd52842016-10-27 09:30:08 -0700371 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800372
Brian Salomonf856fd12016-12-16 14:24:34 -0500373 GrAtlasGlyphCache* fAtlasGlyphCache;
bungeman6bd52842016-10-27 09:30:08 -0700374 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000375
Brian Osman46da1cc2017-02-14 14:15:48 -0500376 bool fDisableGpuYUVConversion;
bsalomon41b952c2016-03-11 06:46:33 -0800377 bool fDidTestPMConversions;
378 int fPMToUPMConversion;
379 int fUPMToPMConversion;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000380
joshualitt1de610a2016-01-06 08:26:09 -0800381 // In debug builds we guard against improper thread handling
joshualittde8dc7e2016-01-08 10:09:13 -0800382 // This guard is passed to the GrDrawingManager and, from there to all the
Brian Osman32342f02017-03-04 08:12:46 -0500383 // GrRenderTargetContexts. It is also passed to the GrResourceProvider and SkGpuDevice.
bsalomon41b952c2016-03-11 06:46:33 -0800384 mutable GrSingleOwner fSingleOwner;
joshualitt1de610a2016-01-06 08:26:09 -0800385
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000386 struct CleanUpData {
387 PFCleanUpFunc fFunc;
388 void* fInfo;
389 };
390
bsalomon41b952c2016-03-11 06:46:33 -0800391 SkTDArray<CleanUpData> fCleanUpData;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000392
bsalomon41b952c2016-03-11 06:46:33 -0800393 const uint32_t fUniqueID;
krajcevski9c6d4d72014-08-12 07:26:25 -0700394
bungeman6bd52842016-10-27 09:30:08 -0700395 std::unique_ptr<GrDrawingManager> fDrawingManager;
robertphillips77a2e522015-10-17 07:43:27 -0700396
bsalomon41b952c2016-03-11 06:46:33 -0800397 GrAuditTrail fAuditTrail;
joshualitt27a48dc2016-01-08 07:19:47 -0800398
Brian Osman11052242016-10-27 14:47:55 -0400399 // TODO: have the GrClipStackClip use renderTargetContexts and rm this friending
robertphillips4fd74ae2016-08-03 14:26:53 -0700400 friend class GrContextPriv;
csmartdaltonc6f411e2016-08-05 22:32:12 -0700401
bsalomon682c2692015-05-22 14:01:46 -0700402 GrContext(); // init must be called after the constructor.
403 bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
robertphillipsea461502015-05-26 11:38:03 -0700404
bsalomon33435572014-11-05 14:47:41 -0800405 void initMockContext();
bsalomon69cfe952015-11-30 13:27:47 -0800406 void initCommon(const GrContextOptions&);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000407
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000408 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000409 * These functions create premul <-> unpremul effects if it is possible to generate a pair
410 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
bsalomon6c9cd552016-01-22 07:17:34 -0800411 * return NULL. They also can perform a swizzle as part of the draw.
bsalomon@google.comadc65362013-01-28 14:26:09 +0000412 */
Brian Osmance425512017-03-22 14:37:50 -0400413 sk_sp<GrFragmentProcessor> createPMToUPMEffect(sk_sp<GrTextureProxy>, const SkMatrix&);
414 sk_sp<GrFragmentProcessor> createUPMToPMEffect(sk_sp<GrTextureProxy>, const SkMatrix&);
bsalomon6c6f6582015-09-10 08:12:46 -0700415 /** Called before either of the above two functions to determine the appropriate fragment
Robert Phillips65115a12016-12-13 12:42:19 -0500416 processors for conversions. */
bsalomon6c6f6582015-09-10 08:12:46 -0700417 void testPMConversionsIfNecessary(uint32_t flags);
Brian Osmande1a6052017-03-22 10:57:00 -0400418 /** Returns true if we've determined that createPMtoUPMEffect and createUPMToPMEffect will
419 succeed for the passed in config. Otherwise we fall back to SW conversion. */
420 bool validPMUPMConversionExists(GrPixelConfig) const;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000421
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000422 /**
joshualitt0db6dfa2015-04-10 07:01:30 -0700423 * A callback similar to the above for use by the TextBlobCache
424 * TODO move textblob draw calls below context so we can use the call above.
425 */
426 static void TextBlobCacheOverBudgetCB(void* data);
427
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000428 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000429};
430
bsalomon41b952c2016-03-11 06:46:33 -0800431/**
432 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
433 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
434 */
435class GrContextThreadSafeProxy : public SkRefCnt {
436private:
bungeman6bd52842016-10-27 09:30:08 -0700437 GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID)
438 : fCaps(std::move(caps))
bsalomon41b952c2016-03-11 06:46:33 -0800439 , fContextUniqueID(uniqueID) {}
440
bungeman6bd52842016-10-27 09:30:08 -0700441 sk_sp<const GrCaps> fCaps;
442 uint32_t fContextUniqueID;
bsalomon41b952c2016-03-11 06:46:33 -0800443
444 friend class GrContext;
445 friend class SkImage;
446
447 typedef SkRefCnt INHERITED;
448};
449
bsalomon@google.com27847de2011-02-22 20:59:41 +0000450#endif