blob: 3a7fd545b54414dac9132c4ac01c8e80f5899d80 [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"
joshualitt44701df2015-02-23 14:44:57 -080012#include "GrClip.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000013#include "GrColor.h"
bsalomon@google.com288d9542012-10-17 12:53:54 +000014#include "GrPaint.h"
rmistry@google.comfbfcd562012-08-23 18:09:54 +000015#include "GrRenderTarget.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000016#include "SkMatrix.h"
egdaniele61c4112014-06-12 10:24:21 -070017#include "SkPathEffect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000018#include "SkTypes.h"
joshualitt27a48dc2016-01-08 07:19:47 -080019#include "../private/GrAuditTrail.h"
joshualitt1de610a2016-01-06 08:26:09 -080020#include "../private/GrSingleOwner.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021
Brian Salomonf856fd12016-12-16 14:24:34 -050022class GrAtlasGlyphCache;
bsalomon682c2692015-05-22 14:01:46 -070023struct GrContextOptions;
robertphillips4fd74ae2016-08-03 14:26:53 -070024class GrContextPriv;
bsalomon41b952c2016-03-11 06:46:33 -080025class GrContextThreadSafeProxy;
robertphillips77a2e522015-10-17 07:43:27 -070026class GrDrawingManager;
Brian Salomon2ee084e2016-12-16 18:59:19 -050027struct GrDrawOpAtlasConfig;
Brian Osman11052242016-10-27 14:47:55 -040028class GrRenderTargetContext;
joshualittb0a8a372014-09-23 09:50:21 -070029class GrFragmentProcessor;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000030class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000031class GrIndexBuffer;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000032class GrOvalRenderer;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000033class GrPath;
egdaniel8dd688b2015-01-22 10:16:09 -080034class GrPipelineBuilder;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000035class GrResourceEntry;
bsalomon0ea80f42015-02-11 10:49:59 -080036class GrResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -070037class GrResourceProvider;
joshualittb7133be2015-04-08 09:08:31 -070038class GrTextBlobCache;
kkinnunenc6cb56f2014-06-24 00:12:27 -070039class GrTextContext;
Brian Salomon514baff2016-11-17 15:17:07 -050040class GrSamplerParams;
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
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000045class SK_API GrContext : public SkRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000046public:
47 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000048 * Creates a GrContext for a backend context.
bsalomon@google.com27847de2011-02-22 20:59:41 +000049 */
bsalomon682c2692015-05-22 14:01:46 -070050 static GrContext* Create(GrBackend, GrBackendContext, const GrContextOptions& options);
51 static GrContext* Create(GrBackend, GrBackendContext);
bsalomon@google.com27847de2011-02-22 20:59:41 +000052
bsalomon33435572014-11-05 14:47:41 -080053 /**
54 * Only defined in test apps.
55 */
56 static GrContext* CreateMockContext();
57
bsalomon@google.com27847de2011-02-22 20:59:41 +000058 virtual ~GrContext();
59
bungeman6bd52842016-10-27 09:30:08 -070060 sk_sp<GrContextThreadSafeProxy> threadSafeProxy();
bsalomon41b952c2016-03-11 06:46:33 -080061
bsalomon@google.com27847de2011-02-22 20:59:41 +000062 /**
63 * The GrContext normally assumes that no outsider is setting state
64 * within the underlying 3D API's context/device/whatever. This call informs
65 * the context that the state was modified and it should resend. Shouldn't
66 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000067 * The flag bits, state, is dpendent on which backend is used by the
68 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000069 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000070 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000071
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000073 * Callback function to allow classes to cleanup on GrContext destruction.
74 * The 'info' field is filled in with the 'info' passed to addCleanUp.
75 */
76 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
77
78 /**
79 * Add a function to be called from within GrContext's destructor.
80 * This gives classes a chance to free resources held on a per context basis.
81 * The 'info' parameter will be stored and passed to the callback function.
82 */
83 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
84 CleanUpData* entry = fCleanUpData.push();
85
86 entry->fFunc = cleanUp;
87 entry->fInfo = info;
88 }
89
90 /**
bsalomon6e2aad42016-04-01 11:54:31 -070091 * Abandons all GPU resources and assumes the underlying backend 3D API context is not longer
92 * usable. Call this if you have lost the associated GPU context, and thus internal texture,
93 * buffer, etc. references/IDs are now invalid. Calling this ensures that the destructors of the
94 * GrContext and any of its created resource objects will not make backend 3D API calls. Content
95 * rendered but not previously flushed may be lost. After this function is called all subsequent
96 * calls on the GrContext will fail or be no-ops.
97 *
98 * The typical use case for this function is that the underlying 3D context was lost and further
99 * API calls may crash.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100 */
bsalomon2354f842014-07-28 13:48:36 -0700101 void abandonContext();
junov@google.com53a55842011-06-08 22:55:10 +0000102
bsalomon6e2aad42016-04-01 11:54:31 -0700103 /**
104 * This is similar to abandonContext() however the underlying 3D context is not yet lost and
105 * the GrContext will cleanup all allocated resources before returning. After returning it will
106 * assume that the underlying context may no longer be valid.
107 *
108 * The typical use case for this function is that the client is going to destroy the 3D context
109 * but can't guarantee that GrContext will be destroyed first (perhaps because it may be ref'ed
110 * elsewhere by either the client or Skia objects).
111 */
112 void releaseResourcesAndAbandonContext();
113
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000114 ///////////////////////////////////////////////////////////////////////////
115 // Resource Cache
116
117 /**
118 * Return the current GPU resource cache limits.
119 *
120 * @param maxResources If non-null, returns maximum number of resources that
121 * can be held in the cache.
122 * @param maxResourceBytes If non-null, returns maximum number of bytes of
123 * video memory that can be held in the cache.
124 */
125 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000126
127 /**
128 * Gets the current GPU resource cache usage.
129 *
130 * @param resourceCount If non-null, returns the number of resources that are held in the
131 * cache.
132 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
133 * in the cache.
134 */
135 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
136
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000137 /**
138 * Specify the GPU resource cache limits. If the current cache exceeds either
139 * of these, it will be purged (LRU) to keep the cache within these limits.
140 *
141 * @param maxResources The maximum number of resources that can be held in
142 * the cache.
143 * @param maxResourceBytes The maximum number of bytes of video memory
144 * that can be held in the cache.
145 */
146 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000147
junov@google.com53a55842011-06-08 22:55:10 +0000148 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000149 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000150 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000151 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000152 void freeGpuResources();
153
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000154 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000155 * Purge all the unlocked resources from the cache.
156 * This entry point is mainly meant for timing texture uploads
157 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000158 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000159 void purgeAllUnlockedResources();
160
bsalomon76228632015-05-29 08:02:10 -0700161 /** Access the context capabilities */
162 const GrCaps* caps() const { return fCaps; }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000163
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000164 /**
165 * Returns the recommended sample count for a render target when using this
166 * context.
167 *
168 * @param config the configuration of the render target.
169 * @param dpi the display density in dots per inch.
170 *
171 * @return sample count that should be perform well and have good enough
172 * rendering quality for the display. Alternatively returns 0 if
173 * MSAA is not supported or recommended to be used by default.
174 */
175 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
176
bsalomon@google.com27847de2011-02-22 20:59:41 +0000177 /**
Brian Osman11052242016-10-27 14:47:55 -0400178 * Create both a GrRenderTarget and a matching GrRenderTargetContext to wrap it.
179 * We guarantee that "asTexture" will succeed for renderTargetContexts created
robertphillipsebf30e82016-05-11 08:34:39 -0700180 * via this entry point.
robertphillipsd4c741e2016-04-28 09:55:15 -0700181 */
Brian Osman11052242016-10-27 14:47:55 -0400182 sk_sp<GrRenderTargetContext> makeRenderTargetContext(
183 SkBackingFit fit,
184 int width, int height,
185 GrPixelConfig config,
186 sk_sp<SkColorSpace> colorSpace,
187 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500188 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
Brian Osman11052242016-10-27 14:47:55 -0400189 const SkSurfaceProps* surfaceProps = nullptr,
190 SkBudgeted = SkBudgeted::kYes);
robertphillipsd4c741e2016-04-28 09:55:15 -0700191
Brian Salomonaeac5d82016-10-29 00:13:29 -0400192 // Create a new render target context as above but have it backed by a deferred-style
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400193 // GrRenderTargetProxy rather than one that is backed by an actual GrRenderTarget
robertphillipsd728f0c2016-11-21 11:05:03 -0800194 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(
195 SkBackingFit fit,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400196 int width, int height,
197 GrPixelConfig config,
198 sk_sp<SkColorSpace> colorSpace,
199 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500200 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400201 const SkSurfaceProps* surfaceProps = nullptr,
202 SkBudgeted = SkBudgeted::kYes);
robertphillips48fde9c2016-09-06 05:20:20 -0700203 /*
Brian Osman11052242016-10-27 14:47:55 -0400204 * This method will attempt to create a renderTargetContext that has, at least, the number of
robertphillips48fde9c2016-09-06 05:20:20 -0700205 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
206 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
207 * SRGB-ness will be preserved.
208 */
Brian Osman11052242016-10-27 14:47:55 -0400209 sk_sp<GrRenderTargetContext> makeRenderTargetContextWithFallback(
robertphillips48fde9c2016-09-06 05:20:20 -0700210 SkBackingFit fit,
211 int width, int height,
212 GrPixelConfig config,
213 sk_sp<SkColorSpace> colorSpace,
214 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500215 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
robertphillips48fde9c2016-09-06 05:20:20 -0700216 const SkSurfaceProps* surfaceProps = nullptr,
217 SkBudgeted budgeted = SkBudgeted::kYes);
218
robertphillipsd728f0c2016-11-21 11:05:03 -0800219 // Create a new render target context as above but have it backed by a deferred-style
220 // GrRenderTargetProxy rather than one that is backed by an actual GrRenderTarget
221 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContextWithFallback(
222 SkBackingFit fit,
223 int width, int height,
224 GrPixelConfig config,
225 sk_sp<SkColorSpace> colorSpace,
226 int sampleCnt = 0,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500227 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
robertphillipsd728f0c2016-11-21 11:05:03 -0800228 const SkSurfaceProps* surfaceProps = nullptr,
229 SkBudgeted budgeted = SkBudgeted::kYes);
230
bsalomon@google.com27847de2011-02-22 20:59:41 +0000231 ///////////////////////////////////////////////////////////////////////////
232 // Misc.
233
234 /**
235 * Call to ensure all drawing to the context has been issued to the
236 * underlying 3D API.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000237 */
bsalomonb77a9072016-09-07 10:02:04 -0700238 void flush();
robertphillipsea461502015-05-26 11:38:03 -0700239
bsalomon@google.com0342a852012-08-20 19:22:38 +0000240 /**
241 * These flags can be used with the read/write pixels functions below.
242 */
243 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700244 /** The GrContext will not be flushed before the surface read or write. This means that
245 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000246 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700247 /** Any surface writes should be flushed to the backend 3D API after the surface operation
248 is complete */
249 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000250 /** The src for write or dst read is unpremultiplied. This is only respected if both the
251 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700252 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000253 };
254
bsalomon@google.com27847de2011-02-22 20:59:41 +0000255 /**
bsalomone8d21e82015-07-16 08:23:13 -0700256 * Reads a rectangle of pixels from a surface.
257 * @param surface the surface to read from.
Brian Osmanb62ea222016-12-22 11:12:16 -0500258 * @param srcColorSpace color space of the surface
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000259 * @param left left edge of the rectangle to read (inclusive)
260 * @param top top edge of the rectangle to read (inclusive)
261 * @param width width of rectangle to read in pixels.
262 * @param height height of rectangle to read in pixels.
263 * @param config the pixel config of the destination buffer
Brian Osmanb62ea222016-12-22 11:12:16 -0500264 * @param dstColorSpace color space of the destination buffer
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000265 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000266 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
267 * packed.
268 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000269 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000270 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
bsalomone8d21e82015-07-16 08:23:13 -0700271 * pixel configs
bsalomon@google.com27847de2011-02-22 20:59:41 +0000272 */
Brian Osmanb62ea222016-12-22 11:12:16 -0500273 bool readSurfacePixels(GrSurface* surface, SkColorSpace* srcColorSpace,
bsalomone8d21e82015-07-16 08:23:13 -0700274 int left, int top, int width, int height,
Brian Osmanb62ea222016-12-22 11:12:16 -0500275 GrPixelConfig config, SkColorSpace* dstColorSpace, void* buffer,
bsalomone8d21e82015-07-16 08:23:13 -0700276 size_t rowBytes = 0,
277 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000278
279 /**
bsalomon81beccc2014-10-13 12:32:55 -0700280 * Writes a rectangle of pixels to a surface.
281 * @param surface the surface to write to.
Brian Osmanb62ea222016-12-22 11:12:16 -0500282 * @param dstColorSpace color space of the surface
bsalomon@google.com6f379512011-11-16 20:36:03 +0000283 * @param left left edge of the rectangle to write (inclusive)
284 * @param top top edge of the rectangle to write (inclusive)
285 * @param width width of rectangle to write in pixels.
286 * @param height height of rectangle to write in pixels.
287 * @param config the pixel config of the source buffer
Brian Osmanb62ea222016-12-22 11:12:16 -0500288 * @param srcColorSpace color space of the source buffer
bsalomon@google.com6f379512011-11-16 20:36:03 +0000289 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000290 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000291 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000293 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700294 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000295 */
Brian Osmanb62ea222016-12-22 11:12:16 -0500296 bool writeSurfacePixels(GrSurface* surface, SkColorSpace* dstColorSpace,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000297 int left, int top, int width, int height,
Brian Osmanb62ea222016-12-22 11:12:16 -0500298 GrPixelConfig config, SkColorSpace* srcColorSpace, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000299 size_t rowBytes,
300 uint32_t pixelOpsFlags = 0);
301
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000302 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700303 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
304 */
305 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000306
307 /**
ajuma95243eb2016-08-24 08:19:02 -0700308 * After this returns any pending reads or writes to the surface will have been issued to the
309 * backend 3D API.
310 */
311 void flushSurfaceIO(GrSurface* surface);
312
313 /**
bsalomonc49e8682015-06-30 11:37:35 -0700314 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
315 * if necessary.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000316 *
bsalomon87a94eb2014-11-03 14:28:32 -0800317 * It is not necessary to call this before reading the render target via Skia/GrContext.
318 * GrContext will detect when it must perform a resolve before reading pixels back from the
319 * surface or using it as a texture.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000320 */
bsalomonc49e8682015-06-30 11:37:35 -0700321 void prepareSurfaceForExternalIO(GrSurface*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000322
323 /**
joshualitt0acd0d32015-05-07 08:23:19 -0700324 * An ID associated with this context, guaranteed to be unique.
325 */
326 uint32_t uniqueID() { return fUniqueID; }
327
bsalomon@google.com27847de2011-02-22 20:59:41 +0000328 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000329 // Functions intended for internal use only.
330 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000331 const GrGpu* getGpu() const { return fGpu; }
Brian Salomonf856fd12016-12-16 14:24:34 -0500332 GrAtlasGlyphCache* getAtlasGlyphCache() { return fAtlasGlyphCache; }
bungeman6bd52842016-10-27 09:30:08 -0700333 GrTextBlobCache* getTextBlobCache() { return fTextBlobCache.get(); }
robertphillips77a2e522015-10-17 07:43:27 -0700334 bool abandoned() const;
bsalomond309e7a2015-04-30 14:18:54 -0700335 GrResourceProvider* resourceProvider() { return fResourceProvider; }
336 const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
bsalomon0ea80f42015-02-11 10:49:59 -0800337 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000338
joshualitte45c81c2015-12-02 09:05:37 -0800339 /** Reset GPU stats */
340 void resetGpuStats() const ;
341
mtkleinb9eb4ac2015-02-02 18:26:03 -0800342 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
343 void dumpCacheStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800344 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000345 void printCacheStats() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000346
mtkleinb9eb4ac2015-02-02 18:26:03 -0800347 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
348 void dumpGpuStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800349 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800350 void printGpuStats() const;
robertphillips754f4e92014-09-18 13:52:08 -0700351
joshualitt17d833b2015-08-03 10:17:44 -0700352 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
353 this is for testing only */
354 void setTextBlobCacheLimit_ForTesting(size_t bytes);
355
joshualittda04e0e2015-08-19 08:16:43 -0700356 /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be
357 to an array of 3 entries */
Brian Salomon2ee084e2016-12-16 18:59:19 -0500358 void setTextContextAtlasSizes_ForTesting(const GrDrawOpAtlasConfig* configs);
joshualittda04e0e2015-08-19 08:16:43 -0700359
ericrk0a5fa482015-09-15 14:16:10 -0700360 /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
361 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
362
Robert Phillips33667da2016-12-20 11:05:30 -0500363 /** Get pointer to atlas texture for given mask format. Note that this wraps an
364 actively mutating texture in an SkImage. This could yield unexpected results
365 if it gets cached or used more generally. */
Robert Phillipsc37e6142017-01-20 10:03:25 -0500366 sk_sp<SkImage> getFontAtlasImage_ForTesting(GrMaskFormat format);
jvanverth629162d2015-11-08 08:07:24 -0800367
joshualitt5651ee62016-01-11 10:39:11 -0800368 GrAuditTrail* getAuditTrail() { return &fAuditTrail; }
joshualitt27a48dc2016-01-08 07:19:47 -0800369
joshualittce894002016-01-11 13:29:31 -0800370 /** This is only useful for debug purposes */
371 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } )
372
robertphillips4fd74ae2016-08-03 14:26:53 -0700373 // Provides access to functions that aren't part of the public API.
374 GrContextPriv contextPriv();
375 const GrContextPriv contextPriv() const;
376
bsalomon@google.com27847de2011-02-22 20:59:41 +0000377private:
bsalomon41b952c2016-03-11 06:46:33 -0800378 GrGpu* fGpu;
379 const GrCaps* fCaps;
380 GrResourceCache* fResourceCache;
Brian Osman32342f02017-03-04 08:12:46 -0500381 GrResourceProvider* fResourceProvider;
bsalomond309e7a2015-04-30 14:18:54 -0700382
bungeman6bd52842016-10-27 09:30:08 -0700383 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800384
Brian Salomonf856fd12016-12-16 14:24:34 -0500385 GrAtlasGlyphCache* fAtlasGlyphCache;
bungeman6bd52842016-10-27 09:30:08 -0700386 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000387
Brian Osman46da1cc2017-02-14 14:15:48 -0500388 bool fDisableGpuYUVConversion;
bsalomon41b952c2016-03-11 06:46:33 -0800389 bool fDidTestPMConversions;
390 int fPMToUPMConversion;
391 int fUPMToPMConversion;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000392
joshualitt1de610a2016-01-06 08:26:09 -0800393 // In debug builds we guard against improper thread handling
joshualittde8dc7e2016-01-08 10:09:13 -0800394 // This guard is passed to the GrDrawingManager and, from there to all the
Brian Osman32342f02017-03-04 08:12:46 -0500395 // GrRenderTargetContexts. It is also passed to the GrResourceProvider and SkGpuDevice.
bsalomon41b952c2016-03-11 06:46:33 -0800396 mutable GrSingleOwner fSingleOwner;
joshualitt1de610a2016-01-06 08:26:09 -0800397
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000398 struct CleanUpData {
399 PFCleanUpFunc fFunc;
400 void* fInfo;
401 };
402
bsalomon41b952c2016-03-11 06:46:33 -0800403 SkTDArray<CleanUpData> fCleanUpData;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000404
bsalomon41b952c2016-03-11 06:46:33 -0800405 const uint32_t fUniqueID;
krajcevski9c6d4d72014-08-12 07:26:25 -0700406
bungeman6bd52842016-10-27 09:30:08 -0700407 std::unique_ptr<GrDrawingManager> fDrawingManager;
robertphillips77a2e522015-10-17 07:43:27 -0700408
bsalomon41b952c2016-03-11 06:46:33 -0800409 GrAuditTrail fAuditTrail;
joshualitt27a48dc2016-01-08 07:19:47 -0800410
Brian Osman11052242016-10-27 14:47:55 -0400411 // TODO: have the GrClipStackClip use renderTargetContexts and rm this friending
robertphillips4fd74ae2016-08-03 14:26:53 -0700412 friend class GrContextPriv;
csmartdaltonc6f411e2016-08-05 22:32:12 -0700413
bsalomon682c2692015-05-22 14:01:46 -0700414 GrContext(); // init must be called after the constructor.
415 bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
robertphillipsea461502015-05-26 11:38:03 -0700416
bsalomon33435572014-11-05 14:47:41 -0800417 void initMockContext();
bsalomon69cfe952015-11-30 13:27:47 -0800418 void initCommon(const GrContextOptions&);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000419
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000420 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000421 * These functions create premul <-> unpremul effects if it is possible to generate a pair
422 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
bsalomon6c9cd552016-01-22 07:17:34 -0800423 * return NULL. They also can perform a swizzle as part of the draw.
bsalomon@google.comadc65362013-01-28 14:26:09 +0000424 */
Robert Phillips757914d2017-01-25 15:48:30 -0500425 sk_sp<GrFragmentProcessor> createPMToUPMEffect(GrTexture*, const GrSwizzle&, const SkMatrix&);
426 sk_sp<GrFragmentProcessor> createPMToUPMEffect(sk_sp<GrTextureProxy>, const GrSwizzle&,
427 const SkMatrix&);
428 sk_sp<GrFragmentProcessor> createUPMToPMEffect(sk_sp<GrTextureProxy>, const GrSwizzle&,
429 const SkMatrix&);
bsalomon6c6f6582015-09-10 08:12:46 -0700430 /** Called before either of the above two functions to determine the appropriate fragment
Robert Phillips65115a12016-12-13 12:42:19 -0500431 processors for conversions. */
bsalomon6c6f6582015-09-10 08:12:46 -0700432 void testPMConversionsIfNecessary(uint32_t flags);
bsalomon636e8022015-07-29 06:08:46 -0700433 /** Returns true if we've already determined that createPMtoUPMEffect and createUPMToPMEffect
434 will fail. In such cases fall back to SW conversion. */
435 bool didFailPMUPMConversionTest() const;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000436
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000437 /**
joshualitt0db6dfa2015-04-10 07:01:30 -0700438 * A callback similar to the above for use by the TextBlobCache
439 * TODO move textblob draw calls below context so we can use the call above.
440 */
441 static void TextBlobCacheOverBudgetCB(void* data);
442
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000443 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444};
445
bsalomon41b952c2016-03-11 06:46:33 -0800446/**
447 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
448 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
449 */
450class GrContextThreadSafeProxy : public SkRefCnt {
451private:
bungeman6bd52842016-10-27 09:30:08 -0700452 GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID)
453 : fCaps(std::move(caps))
bsalomon41b952c2016-03-11 06:46:33 -0800454 , fContextUniqueID(uniqueID) {}
455
bungeman6bd52842016-10-27 09:30:08 -0700456 sk_sp<const GrCaps> fCaps;
457 uint32_t fContextUniqueID;
bsalomon41b952c2016-03-11 06:46:33 -0800458
459 friend class GrContext;
460 friend class SkImage;
461
462 typedef SkRefCnt INHERITED;
463};
464
bsalomon@google.com27847de2011-02-22 20:59:41 +0000465#endif