blob: 6b2c27a77386091d604ec017c35eee74892fc252 [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"
bsalomond309e7a2015-04-30 14:18:54 -070016#include "GrTextureProvider.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000017#include "SkMatrix.h"
egdaniele61c4112014-06-12 10:24:21 -070018#include "SkPathEffect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
joshualitt27a48dc2016-01-08 07:19:47 -080020#include "../private/GrAuditTrail.h"
joshualitt1de610a2016-01-06 08:26:09 -080021#include "../private/GrSingleOwner.h"
22#include "../private/SkMutex.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000023
joshualittda04e0e2015-08-19 08:16:43 -070024struct GrBatchAtlasConfig;
joshualitt7c3a2f82015-03-31 13:32:05 -070025class GrBatchFontCache;
bsalomon682c2692015-05-22 14:01:46 -070026struct GrContextOptions;
robertphillips4fd74ae2016-08-03 14:26:53 -070027class GrContextPriv;
bsalomon41b952c2016-03-11 06:46:33 -080028class GrContextThreadSafeProxy;
robertphillips77a2e522015-10-17 07:43:27 -070029class GrDrawingManager;
Brian Osman11052242016-10-27 14:47:55 -040030class GrRenderTargetContext;
joshualittb0a8a372014-09-23 09:50:21 -070031class GrFragmentProcessor;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000032class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000033class GrIndexBuffer;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000034class GrOvalRenderer;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000035class GrPath;
egdaniel8dd688b2015-01-22 10:16:09 -080036class GrPipelineBuilder;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000037class GrResourceEntry;
bsalomon0ea80f42015-02-11 10:49:59 -080038class GrResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -070039class GrResourceProvider;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000040class GrTestTarget;
joshualittb7133be2015-04-08 09:08:31 -070041class GrTextBlobCache;
kkinnunenc6cb56f2014-06-24 00:12:27 -070042class GrTextContext;
bsalomon@google.com288d9542012-10-17 12:53:54 +000043class GrTextureParams;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000044class GrVertexBuffer;
bsalomon6c9cd552016-01-22 07:17:34 -080045class GrSwizzle;
ericrk0a5fa482015-09-15 14:16:10 -070046class SkTraceMemoryDump;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000047
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
bsalomond309e7a2015-04-30 14:18:54 -0700151 GrTextureProvider* textureProvider() { return fTextureProvider; }
152 const GrTextureProvider* textureProvider() const { return fTextureProvider; }
153
junov@google.com53a55842011-06-08 22:55:10 +0000154 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000155 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000156 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000157 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000158 void freeGpuResources();
159
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000160 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000161 * Purge all the unlocked resources from the cache.
162 * This entry point is mainly meant for timing texture uploads
163 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000164 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000165 void purgeAllUnlockedResources();
166
bsalomon76228632015-05-29 08:02:10 -0700167 /** Access the context capabilities */
168 const GrCaps* caps() const { return fCaps; }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000169
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000170 /**
171 * Returns the recommended sample count for a render target when using this
172 * context.
173 *
174 * @param config the configuration of the render target.
175 * @param dpi the display density in dots per inch.
176 *
177 * @return sample count that should be perform well and have good enough
178 * rendering quality for the display. Alternatively returns 0 if
179 * MSAA is not supported or recommended to be used by default.
180 */
181 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
182
bsalomon@google.com27847de2011-02-22 20:59:41 +0000183 /**
Brian Osman11052242016-10-27 14:47:55 -0400184 * Create both a GrRenderTarget and a matching GrRenderTargetContext to wrap it.
185 * We guarantee that "asTexture" will succeed for renderTargetContexts created
robertphillipsebf30e82016-05-11 08:34:39 -0700186 * via this entry point.
robertphillipsd4c741e2016-04-28 09:55:15 -0700187 */
Brian Osman11052242016-10-27 14:47:55 -0400188 sk_sp<GrRenderTargetContext> makeRenderTargetContext(
189 SkBackingFit fit,
190 int width, int height,
191 GrPixelConfig config,
192 sk_sp<SkColorSpace> colorSpace,
193 int sampleCnt = 0,
194 GrSurfaceOrigin origin = kDefault_GrSurfaceOrigin,
195 const SkSurfaceProps* surfaceProps = nullptr,
196 SkBudgeted = SkBudgeted::kYes);
robertphillipsd4c741e2016-04-28 09:55:15 -0700197
Brian Salomonaeac5d82016-10-29 00:13:29 -0400198 // Create a new render target context as above but have it backed by a deferred-style
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400199 // GrRenderTargetProxy rather than one that is backed by an actual GrRenderTarget
200 sk_sp<GrRenderTargetContext> makeDeferredRenderTargetContext(SkBackingFit fit,
201 int width, int height,
202 GrPixelConfig config,
203 sk_sp<SkColorSpace> colorSpace,
204 int sampleCnt = 0,
205 GrSurfaceOrigin origin = kDefault_GrSurfaceOrigin,
206 const SkSurfaceProps* surfaceProps = nullptr,
207 SkBudgeted = SkBudgeted::kYes);
robertphillips48fde9c2016-09-06 05:20:20 -0700208 /*
Brian Osman11052242016-10-27 14:47:55 -0400209 * This method will attempt to create a renderTargetContext that has, at least, the number of
robertphillips48fde9c2016-09-06 05:20:20 -0700210 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
211 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
212 * SRGB-ness will be preserved.
213 */
Brian Osman11052242016-10-27 14:47:55 -0400214 sk_sp<GrRenderTargetContext> makeRenderTargetContextWithFallback(
robertphillips48fde9c2016-09-06 05:20:20 -0700215 SkBackingFit fit,
216 int width, int height,
217 GrPixelConfig config,
218 sk_sp<SkColorSpace> colorSpace,
219 int sampleCnt = 0,
220 GrSurfaceOrigin origin = kDefault_GrSurfaceOrigin,
221 const SkSurfaceProps* surfaceProps = nullptr,
222 SkBudgeted budgeted = SkBudgeted::kYes);
223
bsalomon@google.com27847de2011-02-22 20:59:41 +0000224 ///////////////////////////////////////////////////////////////////////////
225 // Misc.
226
227 /**
228 * Call to ensure all drawing to the context has been issued to the
229 * underlying 3D API.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000230 */
bsalomonb77a9072016-09-07 10:02:04 -0700231 void flush();
robertphillipsea461502015-05-26 11:38:03 -0700232
bsalomon@google.com0342a852012-08-20 19:22:38 +0000233 /**
234 * These flags can be used with the read/write pixels functions below.
235 */
236 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700237 /** The GrContext will not be flushed before the surface read or write. This means that
238 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000239 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700240 /** Any surface writes should be flushed to the backend 3D API after the surface operation
241 is complete */
242 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000243 /** The src for write or dst read is unpremultiplied. This is only respected if both the
244 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700245 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000246 };
247
bsalomon@google.com27847de2011-02-22 20:59:41 +0000248 /**
bsalomone8d21e82015-07-16 08:23:13 -0700249 * Reads a rectangle of pixels from a surface.
250 * @param surface the surface to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000251 * @param left left edge of the rectangle to read (inclusive)
252 * @param top top edge of the rectangle to read (inclusive)
253 * @param width width of rectangle to read in pixels.
254 * @param height height of rectangle to read in pixels.
255 * @param config the pixel config of the destination buffer
256 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000257 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
258 * packed.
259 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000260 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000261 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
bsalomone8d21e82015-07-16 08:23:13 -0700262 * pixel configs
bsalomon@google.com27847de2011-02-22 20:59:41 +0000263 */
bsalomone8d21e82015-07-16 08:23:13 -0700264 bool readSurfacePixels(GrSurface* surface,
265 int left, int top, int width, int height,
266 GrPixelConfig config, void* buffer,
267 size_t rowBytes = 0,
268 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000269
270 /**
bsalomon81beccc2014-10-13 12:32:55 -0700271 * Writes a rectangle of pixels to a surface.
272 * @param surface the surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000273 * @param left left edge of the rectangle to write (inclusive)
274 * @param top top edge of the rectangle to write (inclusive)
275 * @param width width of rectangle to write in pixels.
276 * @param height height of rectangle to write in pixels.
277 * @param config the pixel config of the source buffer
278 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000279 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000280 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000281 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000282 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700283 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000284 */
bsalomon81beccc2014-10-13 12:32:55 -0700285 bool writeSurfacePixels(GrSurface* surface,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000286 int left, int top, int width, int height,
287 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000288 size_t rowBytes,
289 uint32_t pixelOpsFlags = 0);
290
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000291 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700292 * Copies a rectangle of texels from src to dst.
bsalomonf80bfed2014-10-07 05:56:02 -0700293 * @param dst the surface to copy to.
294 * @param src the surface to copy from.
295 * @param srcRect the rectangle of the src that should be copied.
296 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst.
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000297 */
bsalomonb8fea972016-02-16 07:34:17 -0800298 bool copySurface(GrSurface* dst,
bsalomonf80bfed2014-10-07 05:56:02 -0700299 GrSurface* src,
300 const SkIRect& srcRect,
bsalomonb8fea972016-02-16 07:34:17 -0800301 const SkIPoint& dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700302
303 /** Helper that copies the whole surface but fails when the two surfaces are not identically
304 sized. */
305 bool copySurface(GrSurface* dst, GrSurface* src) {
bsalomonb8fea972016-02-16 07:34:17 -0800306 return this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
307 SkIPoint::Make(0,0));
bsalomonf80bfed2014-10-07 05:56:02 -0700308 }
309
310 /**
311 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
312 */
313 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000314
315 /**
ajuma95243eb2016-08-24 08:19:02 -0700316 * After this returns any pending reads or writes to the surface will have been issued to the
317 * backend 3D API.
318 */
319 void flushSurfaceIO(GrSurface* surface);
320
321 /**
bsalomonc49e8682015-06-30 11:37:35 -0700322 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
323 * if necessary.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000324 *
bsalomon87a94eb2014-11-03 14:28:32 -0800325 * It is not necessary to call this before reading the render target via Skia/GrContext.
326 * GrContext will detect when it must perform a resolve before reading pixels back from the
327 * surface or using it as a texture.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000328 */
bsalomonc49e8682015-06-30 11:37:35 -0700329 void prepareSurfaceForExternalIO(GrSurface*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000330
331 /**
joshualitt0acd0d32015-05-07 08:23:19 -0700332 * An ID associated with this context, guaranteed to be unique.
333 */
334 uint32_t uniqueID() { return fUniqueID; }
335
bsalomon@google.com27847de2011-02-22 20:59:41 +0000336 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000337 // Functions intended for internal use only.
338 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000339 const GrGpu* getGpu() const { return fGpu; }
joshualitt7c3a2f82015-03-31 13:32:05 -0700340 GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; }
bungeman6bd52842016-10-27 09:30:08 -0700341 GrTextBlobCache* getTextBlobCache() { return fTextBlobCache.get(); }
robertphillips77a2e522015-10-17 07:43:27 -0700342 bool abandoned() const;
bsalomond309e7a2015-04-30 14:18:54 -0700343 GrResourceProvider* resourceProvider() { return fResourceProvider; }
344 const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
bsalomon0ea80f42015-02-11 10:49:59 -0800345 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000346
Brian Osman11052242016-10-27 14:47:55 -0400347 // Called by tests that draw directly to the context via GrRenderTargetContext
348 void getTestTarget(GrTestTarget*, sk_sp<GrRenderTargetContext>);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000349
joshualitte45c81c2015-12-02 09:05:37 -0800350 /** Reset GPU stats */
351 void resetGpuStats() const ;
352
mtkleinb9eb4ac2015-02-02 18:26:03 -0800353 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
354 void dumpCacheStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800355 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000356 void printCacheStats() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000357
mtkleinb9eb4ac2015-02-02 18:26:03 -0800358 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
359 void dumpGpuStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800360 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800361 void printGpuStats() const;
robertphillips754f4e92014-09-18 13:52:08 -0700362
joshualitt17d833b2015-08-03 10:17:44 -0700363 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
364 this is for testing only */
365 void setTextBlobCacheLimit_ForTesting(size_t bytes);
366
joshualittda04e0e2015-08-19 08:16:43 -0700367 /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be
368 to an array of 3 entries */
369 void setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs);
370
ericrk0a5fa482015-09-15 14:16:10 -0700371 /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
372 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
373
jvanverth0671b962015-12-08 18:53:44 -0800374 /** Get pointer to atlas texture for given mask format */
375 GrTexture* getFontAtlasTexture(GrMaskFormat format);
jvanverth629162d2015-11-08 08:07:24 -0800376
joshualitt5651ee62016-01-11 10:39:11 -0800377 GrAuditTrail* getAuditTrail() { return &fAuditTrail; }
joshualitt27a48dc2016-01-08 07:19:47 -0800378
joshualittce894002016-01-11 13:29:31 -0800379 /** This is only useful for debug purposes */
380 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } )
381
robertphillips4fd74ae2016-08-03 14:26:53 -0700382 // Provides access to functions that aren't part of the public API.
383 GrContextPriv contextPriv();
384 const GrContextPriv contextPriv() const;
385
bsalomon@google.com27847de2011-02-22 20:59:41 +0000386private:
bsalomon41b952c2016-03-11 06:46:33 -0800387 GrGpu* fGpu;
388 const GrCaps* fCaps;
389 GrResourceCache* fResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -0700390 // this union exists because the inheritance of GrTextureProvider->GrResourceProvider
391 // is in a private header.
mtklein2766c002015-06-26 11:45:03 -0700392 union {
bsalomon41b952c2016-03-11 06:46:33 -0800393 GrResourceProvider* fResourceProvider;
394 GrTextureProvider* fTextureProvider;
bsalomond309e7a2015-04-30 14:18:54 -0700395 };
396
bungeman6bd52842016-10-27 09:30:08 -0700397 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800398
399 GrBatchFontCache* fBatchFontCache;
bungeman6bd52842016-10-27 09:30:08 -0700400 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000401
bsalomon41b952c2016-03-11 06:46:33 -0800402 bool fDidTestPMConversions;
403 int fPMToUPMConversion;
404 int fUPMToPMConversion;
bsalomon6c6f6582015-09-10 08:12:46 -0700405 // The sw backend may call GrContext::readSurfacePixels on multiple threads
406 // We may transfer the responsibilty for using a mutex to the sw backend
407 // when there are fewer code paths that lead to a readSurfacePixels call
408 // from the sw backend. readSurfacePixels is reentrant in one case - when performing
409 // the PM conversions test. To handle this we do the PM conversions test outside
410 // of fReadPixelsMutex and use a separate mutex to guard it. When it re-enters
411 // readSurfacePixels it will grab fReadPixelsMutex and release it before the outer
412 // readSurfacePixels proceeds to grab it.
413 // TODO: Stop pretending to make GrContext thread-safe for sw rasterization and provide
414 // a mechanism to make a SkPicture safe for multithreaded sw rasterization.
bsalomon41b952c2016-03-11 06:46:33 -0800415 SkMutex fReadPixelsMutex;
416 SkMutex fTestPMConversionsMutex;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000417
joshualitt1de610a2016-01-06 08:26:09 -0800418 // In debug builds we guard against improper thread handling
joshualittde8dc7e2016-01-08 10:09:13 -0800419 // This guard is passed to the GrDrawingManager and, from there to all the
Brian Osman11052242016-10-27 14:47:55 -0400420 // GrRenderTargetContexts. It is also passed to the GrTextureProvider and SkGpuDevice.
bsalomon41b952c2016-03-11 06:46:33 -0800421 mutable GrSingleOwner fSingleOwner;
joshualitt1de610a2016-01-06 08:26:09 -0800422
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000423 struct CleanUpData {
424 PFCleanUpFunc fFunc;
425 void* fInfo;
426 };
427
bsalomon41b952c2016-03-11 06:46:33 -0800428 SkTDArray<CleanUpData> fCleanUpData;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000429
bsalomon41b952c2016-03-11 06:46:33 -0800430 const uint32_t fUniqueID;
krajcevski9c6d4d72014-08-12 07:26:25 -0700431
bungeman6bd52842016-10-27 09:30:08 -0700432 std::unique_ptr<GrDrawingManager> fDrawingManager;
robertphillips77a2e522015-10-17 07:43:27 -0700433
bsalomon41b952c2016-03-11 06:46:33 -0800434 GrAuditTrail fAuditTrail;
joshualitt27a48dc2016-01-08 07:19:47 -0800435
Brian Osman11052242016-10-27 14:47:55 -0400436 // TODO: have the GrClipStackClip use renderTargetContexts and rm this friending
robertphillips4fd74ae2016-08-03 14:26:53 -0700437 friend class GrContextPriv;
csmartdaltonc6f411e2016-08-05 22:32:12 -0700438
bsalomon682c2692015-05-22 14:01:46 -0700439 GrContext(); // init must be called after the constructor.
440 bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
robertphillipsea461502015-05-26 11:38:03 -0700441
bsalomon33435572014-11-05 14:47:41 -0800442 void initMockContext();
bsalomon69cfe952015-11-30 13:27:47 -0800443 void initCommon(const GrContextOptions&);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000444
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000445 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000446 * These functions create premul <-> unpremul effects if it is possible to generate a pair
447 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
bsalomon6c9cd552016-01-22 07:17:34 -0800448 * return NULL. They also can perform a swizzle as part of the draw.
bsalomon@google.comadc65362013-01-28 14:26:09 +0000449 */
bungeman06ca8ec2016-06-09 08:01:03 -0700450 sk_sp<GrFragmentProcessor> createPMToUPMEffect(GrTexture*, const GrSwizzle&,
bsalomon4a339522015-10-06 08:40:50 -0700451 const SkMatrix&) const;
bungeman06ca8ec2016-06-09 08:01:03 -0700452 sk_sp<GrFragmentProcessor> createUPMToPMEffect(GrTexture*, const GrSwizzle&,
bsalomon4a339522015-10-06 08:40:50 -0700453 const SkMatrix&) const;
bsalomon6c6f6582015-09-10 08:12:46 -0700454 /** Called before either of the above two functions to determine the appropriate fragment
bsalomon4a339522015-10-06 08:40:50 -0700455 processors for conversions. This must be called by readSurfacePixels before a mutex is
456 taken, since testingvPM conversions itself will call readSurfacePixels */
bsalomon6c6f6582015-09-10 08:12:46 -0700457 void testPMConversionsIfNecessary(uint32_t flags);
bsalomon636e8022015-07-29 06:08:46 -0700458 /** Returns true if we've already determined that createPMtoUPMEffect and createUPMToPMEffect
459 will fail. In such cases fall back to SW conversion. */
460 bool didFailPMUPMConversionTest() const;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000461
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000462 /**
joshualitt0db6dfa2015-04-10 07:01:30 -0700463 * A callback similar to the above for use by the TextBlobCache
464 * TODO move textblob draw calls below context so we can use the call above.
465 */
466 static void TextBlobCacheOverBudgetCB(void* data);
467
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000468 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000469};
470
bsalomon41b952c2016-03-11 06:46:33 -0800471/**
472 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
473 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
474 */
475class GrContextThreadSafeProxy : public SkRefCnt {
476private:
bungeman6bd52842016-10-27 09:30:08 -0700477 GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID)
478 : fCaps(std::move(caps))
bsalomon41b952c2016-03-11 06:46:33 -0800479 , fContextUniqueID(uniqueID) {}
480
bungeman6bd52842016-10-27 09:30:08 -0700481 sk_sp<const GrCaps> fCaps;
482 uint32_t fContextUniqueID;
bsalomon41b952c2016-03-11 06:46:33 -0800483
484 friend class GrContext;
485 friend class SkImage;
486
487 typedef SkRefCnt INHERITED;
488};
489
bsalomon@google.com27847de2011-02-22 20:59:41 +0000490#endif