blob: 904a0413ec7af72f0d8784cc61adeb1f65b7b57b [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
joshualitt44701df2015-02-23 14:44:57 -080011#include "GrClip.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000012#include "GrColor.h"
bsalomon@google.com288d9542012-10-17 12:53:54 +000013#include "GrPaint.h"
bsalomon@google.com45a15f52012-12-10 19:10:17 +000014#include "GrPathRendererChain.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"
herb62a69c22015-09-29 11:47:45 -070018#include "../private/SkMutex.h"
egdaniele61c4112014-06-12 10:24:21 -070019#include "SkPathEffect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000020#include "SkTypes.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021
joshualittda04e0e2015-08-19 08:16:43 -070022struct GrBatchAtlasConfig;
joshualitt7c3a2f82015-03-31 13:32:05 -070023class GrBatchFontCache;
bsalomon76228632015-05-29 08:02:10 -070024class GrCaps;
bsalomon682c2692015-05-22 14:01:46 -070025struct GrContextOptions;
robertphillipsea461502015-05-26 11:38:03 -070026class GrDrawContext;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000027class GrDrawTarget;
joshualittb0a8a372014-09-23 09:50:21 -070028class GrFragmentProcessor;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000029class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000030class GrIndexBuffer;
robertphillips@google.come930a072014-04-03 00:34:27 +000031class GrLayerCache;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000032class GrOvalRenderer;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000033class GrPath;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000034class GrPathRenderer;
egdaniel8dd688b2015-01-22 10:16:09 -080035class GrPipelineBuilder;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000036class GrResourceEntry;
bsalomon0ea80f42015-02-11 10:49:59 -080037class GrResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -070038class GrResourceProvider;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000039class GrTestTarget;
joshualittb7133be2015-04-08 09:08:31 -070040class GrTextBlobCache;
kkinnunenc6cb56f2014-06-24 00:12:27 -070041class GrTextContext;
bsalomon@google.com288d9542012-10-17 12:53:54 +000042class GrTextureParams;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000043class GrVertexBuffer;
egdanield58a0ba2014-06-11 10:30:05 -070044class GrStrokeInfo;
robertphillips@google.com72176b22012-05-23 13:19:12 +000045class GrSoftwarePathRenderer;
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
63 /**
64 * The GrContext normally assumes that no outsider is setting state
65 * within the underlying 3D API's context/device/whatever. This call informs
66 * the context that the state was modified and it should resend. Shouldn't
67 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000068 * The flag bits, state, is dpendent on which backend is used by the
69 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000070 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000071 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000072
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000074 * Callback function to allow classes to cleanup on GrContext destruction.
75 * The 'info' field is filled in with the 'info' passed to addCleanUp.
76 */
77 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
78
79 /**
80 * Add a function to be called from within GrContext's destructor.
81 * This gives classes a chance to free resources held on a per context basis.
82 * The 'info' parameter will be stored and passed to the callback function.
83 */
84 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
85 CleanUpData* entry = fCleanUpData.push();
86
87 entry->fFunc = cleanUp;
88 entry->fInfo = info;
89 }
90
91 /**
mtklein2766c002015-06-26 11:45:03 -070092 * Abandons all GPU resources and assumes the underlying backend 3D API
bsalomon2354f842014-07-28 13:48:36 -070093 * context is not longer usable. Call this if you have lost the associated
94 * GPU context, and thus internal texture, buffer, etc. references/IDs are
95 * now invalid. Should be called even when GrContext is no longer going to
96 * be used for two reasons:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097 * 1) ~GrContext will not try to free the objects in the 3D API.
bsalomon2354f842014-07-28 13:48:36 -070098 * 2) Any GrGpuResources created by this GrContext that outlive
99 * will be marked as invalid (GrGpuResource::wasDestroyed()) and
100 * when they're destroyed no 3D API calls will be made.
101 * Content drawn since the last GrContext::flush() may be lost. After this
102 * function is called the only valid action on the GrContext or
103 * GrGpuResources it created is to destroy them.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104 */
bsalomon2354f842014-07-28 13:48:36 -0700105 void abandonContext();
junov@google.com53a55842011-06-08 22:55:10 +0000106
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000107 ///////////////////////////////////////////////////////////////////////////
108 // Resource Cache
109
110 /**
111 * Return the current GPU resource cache limits.
112 *
113 * @param maxResources If non-null, returns maximum number of resources that
114 * can be held in the cache.
115 * @param maxResourceBytes If non-null, returns maximum number of bytes of
116 * video memory that can be held in the cache.
117 */
118 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000119
120 /**
121 * Gets the current GPU resource cache usage.
122 *
123 * @param resourceCount If non-null, returns the number of resources that are held in the
124 * cache.
125 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
126 * in the cache.
127 */
128 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
129
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000130 /**
131 * Specify the GPU resource cache limits. If the current cache exceeds either
132 * of these, it will be purged (LRU) to keep the cache within these limits.
133 *
134 * @param maxResources The maximum number of resources that can be held in
135 * the cache.
136 * @param maxResourceBytes The maximum number of bytes of video memory
137 * that can be held in the cache.
138 */
139 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000140
bsalomond309e7a2015-04-30 14:18:54 -0700141 GrTextureProvider* textureProvider() { return fTextureProvider; }
142 const GrTextureProvider* textureProvider() const { return fTextureProvider; }
143
junov@google.com53a55842011-06-08 22:55:10 +0000144 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000145 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000146 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000147 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000148 void freeGpuResources();
149
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000150 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000151 * Purge all the unlocked resources from the cache.
152 * This entry point is mainly meant for timing texture uploads
153 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000154 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000155 void purgeAllUnlockedResources();
156
bsalomon76228632015-05-29 08:02:10 -0700157 /** Access the context capabilities */
158 const GrCaps* caps() const { return fCaps; }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000159
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000160 /**
161 * Returns the recommended sample count for a render target when using this
162 * context.
163 *
164 * @param config the configuration of the render target.
165 * @param dpi the display density in dots per inch.
166 *
167 * @return sample count that should be perform well and have good enough
168 * rendering quality for the display. Alternatively returns 0 if
169 * MSAA is not supported or recommended to be used by default.
170 */
171 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
172
bsalomon@google.com27847de2011-02-22 20:59:41 +0000173 /**
mtklein2766c002015-06-26 11:45:03 -0700174 * Returns a helper object to orchestrate draws.
robertphillips2334fb62015-06-17 05:43:33 -0700175 * Callers should take a ref if they rely on the GrDrawContext sticking around.
176 * NULL will be returned if the context has been abandoned.
177 *
robertphillips2e1e51f2015-10-15 08:01:48 -0700178 * @param rt the render target receiving the draws
robertphillips31dc1b22015-07-07 10:05:18 -0700179 * @param surfaceProps the surface properties (mainly defines text drawing)
bsalomon@google.com27847de2011-02-22 20:59:41 +0000180 *
robertphillipsea461502015-05-26 11:38:03 -0700181 * @return a draw context
bsalomon@google.com27847de2011-02-22 20:59:41 +0000182 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700183 GrDrawContext* drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps = NULL) {
184 return fDrawingMgr.drawContext(rt, surfaceProps);
joshualitt16b27892014-12-18 07:47:16 -0800185 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000186
robertphillips2d70dcb2015-10-06 07:38:23 -0700187 GrTextContext* textContext(const SkSurfaceProps& surfaceProps, GrRenderTarget* rt) {
188 return fDrawingMgr.textContext(surfaceProps, rt);
189 }
190
robertphillipsa106c622015-10-16 09:07:06 -0700191 // The caller automatically gets a ref on the returned drawTarget. It must
192 // be balanced by an unref call.
193 GrDrawTarget* newDrawTarget(GrRenderTarget* rt) {
194 return fDrawingMgr.newDrawTarget(rt);
195 }
196
bsalomon@google.com27847de2011-02-22 20:59:41 +0000197 ///////////////////////////////////////////////////////////////////////////
198 // Misc.
199
200 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000201 * Flags that affect flush() behavior.
202 */
203 enum FlushBits {
204 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205 * A client may reach a point where it has partially rendered a frame
206 * through a GrContext that it knows the user will never see. This flag
207 * causes the flush to skip submission of deferred content to the 3D API
208 * during the flush.
209 */
210 kDiscard_FlushBit = 0x2,
211 };
212
213 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000214 * Call to ensure all drawing to the context has been issued to the
215 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000216 * @param flagsBitfield flags that control the flushing behavior. See
217 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000219 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000220
robertphillipsea461502015-05-26 11:38:03 -0700221 void flushIfNecessary() {
222 if (fFlushToReduceCacheSize) {
223 this->flush();
mtklein2766c002015-06-26 11:45:03 -0700224 }
robertphillipsea461502015-05-26 11:38:03 -0700225 }
226
bsalomon@google.com0342a852012-08-20 19:22:38 +0000227 /**
228 * These flags can be used with the read/write pixels functions below.
229 */
230 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700231 /** The GrContext will not be flushed before the surface read or write. This means that
232 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000233 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700234 /** Any surface writes should be flushed to the backend 3D API after the surface operation
235 is complete */
236 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000237 /** The src for write or dst read is unpremultiplied. This is only respected if both the
238 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700239 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000240 };
241
bsalomon@google.com27847de2011-02-22 20:59:41 +0000242 /**
bsalomone8d21e82015-07-16 08:23:13 -0700243 * Reads a rectangle of pixels from a surface.
244 * @param surface the surface to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245 * @param left left edge of the rectangle to read (inclusive)
246 * @param top top edge of the rectangle to read (inclusive)
247 * @param width width of rectangle to read in pixels.
248 * @param height height of rectangle to read in pixels.
249 * @param config the pixel config of the destination buffer
250 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000251 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
252 * packed.
253 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000254 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000255 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
bsalomone8d21e82015-07-16 08:23:13 -0700256 * pixel configs
bsalomon@google.com27847de2011-02-22 20:59:41 +0000257 */
bsalomone8d21e82015-07-16 08:23:13 -0700258 bool readSurfacePixels(GrSurface* surface,
259 int left, int top, int width, int height,
260 GrPixelConfig config, void* buffer,
261 size_t rowBytes = 0,
262 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000263
264 /**
bsalomon81beccc2014-10-13 12:32:55 -0700265 * Writes a rectangle of pixels to a surface.
266 * @param surface the surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000267 * @param left left edge of the rectangle to write (inclusive)
268 * @param top top edge of the rectangle to write (inclusive)
269 * @param width width of rectangle to write in pixels.
270 * @param height height of rectangle to write in pixels.
271 * @param config the pixel config of the source buffer
272 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000273 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000274 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000275 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000276 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700277 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000278 */
bsalomon81beccc2014-10-13 12:32:55 -0700279 bool writeSurfacePixels(GrSurface* surface,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000280 int left, int top, int width, int height,
281 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000282 size_t rowBytes,
283 uint32_t pixelOpsFlags = 0);
284
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000285 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700286 * Copies a rectangle of texels from src to dst.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000287 * bounds.
bsalomonf80bfed2014-10-07 05:56:02 -0700288 * @param dst the surface to copy to.
289 * @param src the surface to copy from.
290 * @param srcRect the rectangle of the src that should be copied.
291 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst.
292 * @param pixelOpsFlags see PixelOpsFlags enum above. (kUnpremul_PixelOpsFlag is not allowed).
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000293 */
bsalomonf80bfed2014-10-07 05:56:02 -0700294 void copySurface(GrSurface* dst,
295 GrSurface* src,
296 const SkIRect& srcRect,
297 const SkIPoint& dstPoint,
298 uint32_t pixelOpsFlags = 0);
299
300 /** Helper that copies the whole surface but fails when the two surfaces are not identically
301 sized. */
302 bool copySurface(GrSurface* dst, GrSurface* src) {
303 if (NULL == dst || NULL == src || dst->width() != src->width() ||
304 dst->height() != src->height()) {
305 return false;
306 }
307 this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
308 SkIPoint::Make(0,0));
309 return true;
310 }
311
312 /**
313 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
314 */
315 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000316
317 /**
bsalomonc49e8682015-06-30 11:37:35 -0700318 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
319 * if necessary.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000320 *
bsalomon87a94eb2014-11-03 14:28:32 -0800321 * It is not necessary to call this before reading the render target via Skia/GrContext.
322 * GrContext will detect when it must perform a resolve before reading pixels back from the
323 * surface or using it as a texture.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000324 */
bsalomonc49e8682015-06-30 11:37:35 -0700325 void prepareSurfaceForExternalIO(GrSurface*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000326
327 /**
joshualitt0acd0d32015-05-07 08:23:19 -0700328 * An ID associated with this context, guaranteed to be unique.
329 */
330 uint32_t uniqueID() { return fUniqueID; }
331
bsalomon@google.com27847de2011-02-22 20:59:41 +0000332 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000333 // Functions intended for internal use only.
334 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000335 const GrGpu* getGpu() const { return fGpu; }
joshualitt7c3a2f82015-03-31 13:32:05 -0700336 GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; }
robertphillips@google.come930a072014-04-03 00:34:27 +0000337 GrLayerCache* getLayerCache() { return fLayerCache.get(); }
joshualittb7133be2015-04-08 09:08:31 -0700338 GrTextBlobCache* getTextBlobCache() { return fTextBlobCache; }
robertphillipsea461502015-05-26 11:38:03 -0700339 bool abandoned() const { return fDrawingMgr.abandoned(); }
bsalomond309e7a2015-04-30 14:18:54 -0700340 GrResourceProvider* resourceProvider() { return fResourceProvider; }
341 const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
bsalomon0ea80f42015-02-11 10:49:59 -0800342 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000343
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000344 // Called by tests that draw directly to the context via GrDrawTarget
345 void getTestTarget(GrTestTarget*);
346
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000347 GrPathRenderer* getPathRenderer(
joshualitt9853cce2014-11-17 14:22:48 -0800348 const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800349 const GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800350 const SkMatrix& viewMatrix,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000351 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700352 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000353 bool allowSW,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000354 GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000355 GrPathRendererChain::StencilSupport* stencilSupport = NULL);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000356
mtkleinb9eb4ac2015-02-02 18:26:03 -0800357 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
358 void dumpCacheStats(SkString*) const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000359 void printCacheStats() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000360
mtkleinb9eb4ac2015-02-02 18:26:03 -0800361 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
362 void dumpGpuStats(SkString*) const;
363 void printGpuStats() const;
robertphillips754f4e92014-09-18 13:52:08 -0700364
joshualitt17d833b2015-08-03 10:17:44 -0700365 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
366 this is for testing only */
367 void setTextBlobCacheLimit_ForTesting(size_t bytes);
368
joshualittda04e0e2015-08-19 08:16:43 -0700369 /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be
370 to an array of 3 entries */
371 void setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs);
372
ericrk0a5fa482015-09-15 14:16:10 -0700373 /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
374 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
375
bsalomon@google.com27847de2011-02-22 20:59:41 +0000376private:
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000377 GrGpu* fGpu;
bsalomon76228632015-05-29 08:02:10 -0700378 const GrCaps* fCaps;
bsalomon0ea80f42015-02-11 10:49:59 -0800379 GrResourceCache* fResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -0700380 // this union exists because the inheritance of GrTextureProvider->GrResourceProvider
381 // is in a private header.
mtklein2766c002015-06-26 11:45:03 -0700382 union {
bsalomond309e7a2015-04-30 14:18:54 -0700383 GrResourceProvider* fResourceProvider;
384 GrTextureProvider* fTextureProvider;
385 };
386
joshualitt7c3a2f82015-03-31 13:32:05 -0700387 GrBatchFontCache* fBatchFontCache;
robertphillips@google.come930a072014-04-03 00:34:27 +0000388 SkAutoTDelete<GrLayerCache> fLayerCache;
joshualittb7133be2015-04-08 09:08:31 -0700389 SkAutoTDelete<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000390
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000391 GrPathRendererChain* fPathRendererChain;
392 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000393
bsalomonf21dab92014-11-13 13:33:28 -0800394 // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
395 bool fFlushToReduceCacheSize;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000396 bool fDidTestPMConversions;
397 int fPMToUPMConversion;
398 int fUPMToPMConversion;
bsalomon6c6f6582015-09-10 08:12:46 -0700399 // The sw backend may call GrContext::readSurfacePixels on multiple threads
400 // We may transfer the responsibilty for using a mutex to the sw backend
401 // when there are fewer code paths that lead to a readSurfacePixels call
402 // from the sw backend. readSurfacePixels is reentrant in one case - when performing
403 // the PM conversions test. To handle this we do the PM conversions test outside
404 // of fReadPixelsMutex and use a separate mutex to guard it. When it re-enters
405 // readSurfacePixels it will grab fReadPixelsMutex and release it before the outer
406 // readSurfacePixels proceeds to grab it.
407 // TODO: Stop pretending to make GrContext thread-safe for sw rasterization and provide
408 // a mechanism to make a SkPicture safe for multithreaded sw rasterization.
409 SkMutex fReadPixelsMutex;
410 SkMutex fTestPMConversionsMutex;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000411
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000412 struct CleanUpData {
413 PFCleanUpFunc fFunc;
414 void* fInfo;
415 };
416
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000417 SkTDArray<CleanUpData> fCleanUpData;
418
joshualitt0acd0d32015-05-07 08:23:19 -0700419 const uint32_t fUniqueID;
krajcevski9c6d4d72014-08-12 07:26:25 -0700420
bsalomon682c2692015-05-22 14:01:46 -0700421 GrContext(); // init must be called after the constructor.
422 bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
robertphillipsea461502015-05-26 11:38:03 -0700423
robertphillips2d70dcb2015-10-06 07:38:23 -0700424 // Currently the DrawingMgr creates a separate GrTextContext for each
robertphillips2334fb62015-06-17 05:43:33 -0700425 // combination of text drawing options (pixel geometry x DFT use)
robertphillips2d70dcb2015-10-06 07:38:23 -0700426 // and hands the appropriate one back given the DrawContext's request.
427 //
428 // It allocates a new GrDrawContext for each GrRenderTarget
429 // but all of them still land in the same GrDrawTarget!
robertphillips2334fb62015-06-17 05:43:33 -0700430 //
mtklein2766c002015-06-26 11:45:03 -0700431 // In the future this class will allocate a new GrDrawContext for
robertphillips2334fb62015-06-17 05:43:33 -0700432 // each GrRenderTarget/GrDrawTarget and manage the DAG.
robertphillipsea461502015-05-26 11:38:03 -0700433 class DrawingMgr {
434 public:
robertphillipsa106c622015-10-16 09:07:06 -0700435 DrawingMgr()
436 : fContext(nullptr)
437 , fAbandoned(false)
438 , fNVPRTextContext(nullptr) {
robertphillips2d70dcb2015-10-06 07:38:23 -0700439 sk_bzero(fTextContexts, sizeof(fTextContexts));
robertphillipsea461502015-05-26 11:38:03 -0700440 }
441
442 ~DrawingMgr();
443
444 void init(GrContext* context);
445
446 void abandon();
robertphillipsa106c622015-10-16 09:07:06 -0700447 bool abandoned() const { return fAbandoned; }
robertphillipsea461502015-05-26 11:38:03 -0700448
robertphillipsea461502015-05-26 11:38:03 -0700449 void reset();
450 void flush();
451
robertphillips2d70dcb2015-10-06 07:38:23 -0700452 // Callers assume the creation ref of the drawContext!
robertphillipsea461502015-05-26 11:38:03 -0700453 // NULL will be returned if the context has been abandoned.
robertphillips2e1e51f2015-10-15 08:01:48 -0700454 GrDrawContext* drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps);
robertphillipsea461502015-05-26 11:38:03 -0700455
robertphillips2d70dcb2015-10-06 07:38:23 -0700456 GrTextContext* textContext(const SkSurfaceProps& props, GrRenderTarget* rt);
robertphillipsa106c622015-10-16 09:07:06 -0700457
458 GrDrawTarget* newDrawTarget(GrRenderTarget* rt);
robertphillips2d70dcb2015-10-06 07:38:23 -0700459
robertphillipsea461502015-05-26 11:38:03 -0700460 private:
robertphillips2334fb62015-06-17 05:43:33 -0700461 void cleanup();
462
robertphillipsea461502015-05-26 11:38:03 -0700463 friend class GrContext; // for access to fDrawTarget for testing
464
robertphillips2334fb62015-06-17 05:43:33 -0700465 static const int kNumPixelGeometries = 5; // The different pixel geometries
466 static const int kNumDFTOptions = 2; // DFT or no DFT
467
468 GrContext* fContext;
robertphillipsa106c622015-10-16 09:07:06 -0700469
470 bool fAbandoned;
471 SkTDArray<GrDrawTarget*> fDrawTargets;
robertphillipsea461502015-05-26 11:38:03 -0700472
robertphillips2d70dcb2015-10-06 07:38:23 -0700473 GrTextContext* fNVPRTextContext;
474 GrTextContext* fTextContexts[kNumPixelGeometries][kNumDFTOptions];
robertphillipsea461502015-05-26 11:38:03 -0700475 };
476
477 DrawingMgr fDrawingMgr;
478
bsalomon33435572014-11-05 14:47:41 -0800479 void initMockContext();
480 void initCommon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000481
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000482 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000483 * These functions create premul <-> unpremul effects if it is possible to generate a pair
484 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
485 * return NULL.
486 */
bsalomon4a339522015-10-06 08:40:50 -0700487 const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, bool swapRAndB,
488 const SkMatrix&) const;
489 const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, bool swapRAndB,
490 const SkMatrix&) const;
bsalomon6c6f6582015-09-10 08:12:46 -0700491 /** Called before either of the above two functions to determine the appropriate fragment
bsalomon4a339522015-10-06 08:40:50 -0700492 processors for conversions. This must be called by readSurfacePixels before a mutex is
493 taken, since testingvPM conversions itself will call readSurfacePixels */
bsalomon6c6f6582015-09-10 08:12:46 -0700494 void testPMConversionsIfNecessary(uint32_t flags);
bsalomon636e8022015-07-29 06:08:46 -0700495 /** Returns true if we've already determined that createPMtoUPMEffect and createUPMToPMEffect
496 will fail. In such cases fall back to SW conversion. */
497 bool didFailPMUPMConversionTest() const;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000498
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000499 /**
500 * This callback allows the resource cache to callback into the GrContext
bsalomon71cb0c22014-11-14 12:10:14 -0800501 * when the cache is still over budget after a purge.
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000502 */
bsalomon71cb0c22014-11-14 12:10:14 -0800503 static void OverBudgetCB(void* data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000504
joshualitt0db6dfa2015-04-10 07:01:30 -0700505 /**
506 * A callback similar to the above for use by the TextBlobCache
507 * TODO move textblob draw calls below context so we can use the call above.
508 */
509 static void TextBlobCacheOverBudgetCB(void* data);
510
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000511 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000512};
513
bsalomon@google.com27847de2011-02-22 20:59:41 +0000514#endif