blob: 00a92836c513d6ef9963cc9d3ca925101a2a489a [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;
robertphillips77a2e522015-10-17 07:43:27 -070027class GrDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070028class GrDrawContext;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000029class GrDrawTarget;
joshualittb0a8a372014-09-23 09:50:21 -070030class GrFragmentProcessor;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000031class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000032class GrIndexBuffer;
robertphillips@google.come930a072014-04-03 00:34:27 +000033class GrLayerCache;
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;
egdanield58a0ba2014-06-11 10:30:05 -070045class GrStrokeInfo;
bsalomon6c9cd552016-01-22 07:17:34 -080046class GrSwizzle;
ericrk0a5fa482015-09-15 14:16:10 -070047class SkTraceMemoryDump;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000048
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000049class SK_API GrContext : public SkRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000050public:
51 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000052 * Creates a GrContext for a backend context.
bsalomon@google.com27847de2011-02-22 20:59:41 +000053 */
bsalomon682c2692015-05-22 14:01:46 -070054 static GrContext* Create(GrBackend, GrBackendContext, const GrContextOptions& options);
55 static GrContext* Create(GrBackend, GrBackendContext);
bsalomon@google.com27847de2011-02-22 20:59:41 +000056
bsalomon33435572014-11-05 14:47:41 -080057 /**
58 * Only defined in test apps.
59 */
60 static GrContext* CreateMockContext();
61
bsalomon@google.com27847de2011-02-22 20:59:41 +000062 virtual ~GrContext();
63
64 /**
65 * The GrContext normally assumes that no outsider is setting state
66 * within the underlying 3D API's context/device/whatever. This call informs
67 * the context that the state was modified and it should resend. Shouldn't
68 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000069 * The flag bits, state, is dpendent on which backend is used by the
70 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000071 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000072 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000073
bsalomon@google.com8fe72472011-03-30 21:26:44 +000074 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000075 * Callback function to allow classes to cleanup on GrContext destruction.
76 * The 'info' field is filled in with the 'info' passed to addCleanUp.
77 */
78 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
79
80 /**
81 * Add a function to be called from within GrContext's destructor.
82 * This gives classes a chance to free resources held on a per context basis.
83 * The 'info' parameter will be stored and passed to the callback function.
84 */
85 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
86 CleanUpData* entry = fCleanUpData.push();
87
88 entry->fFunc = cleanUp;
89 entry->fInfo = info;
90 }
91
92 /**
mtklein2766c002015-06-26 11:45:03 -070093 * Abandons all GPU resources and assumes the underlying backend 3D API
bsalomon2354f842014-07-28 13:48:36 -070094 * context is not longer usable. Call this if you have lost the associated
95 * GPU context, and thus internal texture, buffer, etc. references/IDs are
96 * now invalid. Should be called even when GrContext is no longer going to
97 * be used for two reasons:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098 * 1) ~GrContext will not try to free the objects in the 3D API.
bsalomon2354f842014-07-28 13:48:36 -070099 * 2) Any GrGpuResources created by this GrContext that outlive
100 * will be marked as invalid (GrGpuResource::wasDestroyed()) and
101 * when they're destroyed no 3D API calls will be made.
102 * Content drawn since the last GrContext::flush() may be lost. After this
103 * function is called the only valid action on the GrContext or
104 * GrGpuResources it created is to destroy them.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000105 */
bsalomon2354f842014-07-28 13:48:36 -0700106 void abandonContext();
junov@google.com53a55842011-06-08 22:55:10 +0000107
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000108 ///////////////////////////////////////////////////////////////////////////
109 // Resource Cache
110
111 /**
112 * Return the current GPU resource cache limits.
113 *
114 * @param maxResources If non-null, returns maximum number of resources that
115 * can be held in the cache.
116 * @param maxResourceBytes If non-null, returns maximum number of bytes of
117 * video memory that can be held in the cache.
118 */
119 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000120
121 /**
122 * Gets the current GPU resource cache usage.
123 *
124 * @param resourceCount If non-null, returns the number of resources that are held in the
125 * cache.
126 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
127 * in the cache.
128 */
129 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
130
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000131 /**
132 * Specify the GPU resource cache limits. If the current cache exceeds either
133 * of these, it will be purged (LRU) to keep the cache within these limits.
134 *
135 * @param maxResources The maximum number of resources that can be held in
136 * the cache.
137 * @param maxResourceBytes The maximum number of bytes of video memory
138 * that can be held in the cache.
139 */
140 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000141
bsalomond309e7a2015-04-30 14:18:54 -0700142 GrTextureProvider* textureProvider() { return fTextureProvider; }
143 const GrTextureProvider* textureProvider() const { return fTextureProvider; }
144
junov@google.com53a55842011-06-08 22:55:10 +0000145 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000146 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000147 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000148 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000149 void freeGpuResources();
150
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000151 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000152 * Purge all the unlocked resources from the cache.
153 * This entry point is mainly meant for timing texture uploads
154 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000155 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000156 void purgeAllUnlockedResources();
157
bsalomon76228632015-05-29 08:02:10 -0700158 /** Access the context capabilities */
159 const GrCaps* caps() const { return fCaps; }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000160
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000161 /**
162 * Returns the recommended sample count for a render target when using this
163 * context.
164 *
165 * @param config the configuration of the render target.
166 * @param dpi the display density in dots per inch.
167 *
168 * @return sample count that should be perform well and have good enough
169 * rendering quality for the display. Alternatively returns 0 if
170 * MSAA is not supported or recommended to be used by default.
171 */
172 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
173
bsalomon@google.com27847de2011-02-22 20:59:41 +0000174 /**
mtklein2766c002015-06-26 11:45:03 -0700175 * Returns a helper object to orchestrate draws.
robertphillips77a2e522015-10-17 07:43:27 -0700176 * Callers assume the creation ref of the drawContext
robertphillips2334fb62015-06-17 05:43:33 -0700177 * NULL will be returned if the context has been abandoned.
178 *
robertphillips2e1e51f2015-10-15 08:01:48 -0700179 * @param rt the render target receiving the draws
robertphillips31dc1b22015-07-07 10:05:18 -0700180 * @param surfaceProps the surface properties (mainly defines text drawing)
bsalomon@google.com27847de2011-02-22 20:59:41 +0000181 *
robertphillipsea461502015-05-26 11:38:03 -0700182 * @return a draw context
bsalomon@google.com27847de2011-02-22 20:59:41 +0000183 */
robertphillips77a2e522015-10-17 07:43:27 -0700184 GrDrawContext* drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps = NULL);
robertphillipsa106c622015-10-16 09:07:06 -0700185
bsalomon@google.com27847de2011-02-22 20:59:41 +0000186 ///////////////////////////////////////////////////////////////////////////
187 // Misc.
188
189 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000190 * Flags that affect flush() behavior.
191 */
192 enum FlushBits {
193 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000194 * A client may reach a point where it has partially rendered a frame
195 * through a GrContext that it knows the user will never see. This flag
196 * causes the flush to skip submission of deferred content to the 3D API
197 * during the flush.
198 */
199 kDiscard_FlushBit = 0x2,
200 };
201
202 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000203 * Call to ensure all drawing to the context has been issued to the
204 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205 * @param flagsBitfield flags that control the flushing behavior. See
206 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000208 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000209
robertphillipsea461502015-05-26 11:38:03 -0700210 void flushIfNecessary() {
robertphillipscaef3452015-11-11 13:18:11 -0800211 if (fFlushToReduceCacheSize || this->caps()->immediateFlush()) {
robertphillipsea461502015-05-26 11:38:03 -0700212 this->flush();
mtklein2766c002015-06-26 11:45:03 -0700213 }
robertphillipsea461502015-05-26 11:38:03 -0700214 }
215
bsalomon@google.com0342a852012-08-20 19:22:38 +0000216 /**
217 * These flags can be used with the read/write pixels functions below.
218 */
219 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700220 /** The GrContext will not be flushed before the surface read or write. This means that
221 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000222 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700223 /** Any surface writes should be flushed to the backend 3D API after the surface operation
224 is complete */
225 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000226 /** The src for write or dst read is unpremultiplied. This is only respected if both the
227 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700228 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000229 };
230
bsalomon@google.com27847de2011-02-22 20:59:41 +0000231 /**
bsalomone8d21e82015-07-16 08:23:13 -0700232 * Reads a rectangle of pixels from a surface.
233 * @param surface the surface to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000234 * @param left left edge of the rectangle to read (inclusive)
235 * @param top top edge of the rectangle to read (inclusive)
236 * @param width width of rectangle to read in pixels.
237 * @param height height of rectangle to read in pixels.
238 * @param config the pixel config of the destination buffer
239 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000240 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
241 * packed.
242 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000243 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000244 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
bsalomone8d21e82015-07-16 08:23:13 -0700245 * pixel configs
bsalomon@google.com27847de2011-02-22 20:59:41 +0000246 */
bsalomone8d21e82015-07-16 08:23:13 -0700247 bool readSurfacePixels(GrSurface* surface,
248 int left, int top, int width, int height,
249 GrPixelConfig config, void* buffer,
250 size_t rowBytes = 0,
251 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000252
253 /**
bsalomon81beccc2014-10-13 12:32:55 -0700254 * Writes a rectangle of pixels to a surface.
255 * @param surface the surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000256 * @param left left edge of the rectangle to write (inclusive)
257 * @param top top edge of the rectangle to write (inclusive)
258 * @param width width of rectangle to write in pixels.
259 * @param height height of rectangle to write in pixels.
260 * @param config the pixel config of the source buffer
261 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000262 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000263 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000264 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000265 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700266 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000267 */
bsalomon81beccc2014-10-13 12:32:55 -0700268 bool writeSurfacePixels(GrSurface* surface,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000269 int left, int top, int width, int height,
270 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000271 size_t rowBytes,
272 uint32_t pixelOpsFlags = 0);
273
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000274 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700275 * Copies a rectangle of texels from src to dst.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000276 * bounds.
bsalomonf80bfed2014-10-07 05:56:02 -0700277 * @param dst the surface to copy to.
278 * @param src the surface to copy from.
279 * @param srcRect the rectangle of the src that should be copied.
280 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst.
281 * @param pixelOpsFlags see PixelOpsFlags enum above. (kUnpremul_PixelOpsFlag is not allowed).
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000282 */
bsalomonf80bfed2014-10-07 05:56:02 -0700283 void copySurface(GrSurface* dst,
284 GrSurface* src,
285 const SkIRect& srcRect,
286 const SkIPoint& dstPoint,
287 uint32_t pixelOpsFlags = 0);
288
289 /** Helper that copies the whole surface but fails when the two surfaces are not identically
290 sized. */
291 bool copySurface(GrSurface* dst, GrSurface* src) {
292 if (NULL == dst || NULL == src || dst->width() != src->width() ||
293 dst->height() != src->height()) {
294 return false;
295 }
296 this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
297 SkIPoint::Make(0,0));
298 return true;
299 }
300
301 /**
302 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
303 */
304 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000305
306 /**
bsalomonc49e8682015-06-30 11:37:35 -0700307 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
308 * if necessary.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000309 *
bsalomon87a94eb2014-11-03 14:28:32 -0800310 * It is not necessary to call this before reading the render target via Skia/GrContext.
311 * GrContext will detect when it must perform a resolve before reading pixels back from the
312 * surface or using it as a texture.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000313 */
bsalomonc49e8682015-06-30 11:37:35 -0700314 void prepareSurfaceForExternalIO(GrSurface*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000315
316 /**
joshualitt0acd0d32015-05-07 08:23:19 -0700317 * An ID associated with this context, guaranteed to be unique.
318 */
319 uint32_t uniqueID() { return fUniqueID; }
320
bsalomon@google.com27847de2011-02-22 20:59:41 +0000321 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000322 // Functions intended for internal use only.
323 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000324 const GrGpu* getGpu() const { return fGpu; }
joshualitt7c3a2f82015-03-31 13:32:05 -0700325 GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; }
robertphillips@google.come930a072014-04-03 00:34:27 +0000326 GrLayerCache* getLayerCache() { return fLayerCache.get(); }
joshualittb7133be2015-04-08 09:08:31 -0700327 GrTextBlobCache* getTextBlobCache() { return fTextBlobCache; }
robertphillips77a2e522015-10-17 07:43:27 -0700328 bool abandoned() const;
bsalomond309e7a2015-04-30 14:18:54 -0700329 GrResourceProvider* resourceProvider() { return fResourceProvider; }
330 const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
bsalomon0ea80f42015-02-11 10:49:59 -0800331 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000332
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000333 // Called by tests that draw directly to the context via GrDrawTarget
robertphillips504ce5d2015-11-16 11:02:05 -0800334 void getTestTarget(GrTestTarget*, GrRenderTarget* rt);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000335
joshualitte45c81c2015-12-02 09:05:37 -0800336 /** Reset GPU stats */
337 void resetGpuStats() const ;
338
mtkleinb9eb4ac2015-02-02 18:26:03 -0800339 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
340 void dumpCacheStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800341 void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000342 void printCacheStats() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000343
mtkleinb9eb4ac2015-02-02 18:26:03 -0800344 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
345 void dumpGpuStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800346 void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800347 void printGpuStats() const;
robertphillips754f4e92014-09-18 13:52:08 -0700348
joshualitt17d833b2015-08-03 10:17:44 -0700349 /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge.
350 this is for testing only */
351 void setTextBlobCacheLimit_ForTesting(size_t bytes);
352
joshualittda04e0e2015-08-19 08:16:43 -0700353 /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be
354 to an array of 3 entries */
355 void setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs);
356
ericrk0a5fa482015-09-15 14:16:10 -0700357 /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */
358 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
359
jvanverth0671b962015-12-08 18:53:44 -0800360 /** Get pointer to atlas texture for given mask format */
361 GrTexture* getFontAtlasTexture(GrMaskFormat format);
jvanverth629162d2015-11-08 08:07:24 -0800362
joshualitt5651ee62016-01-11 10:39:11 -0800363 GrAuditTrail* getAuditTrail() { return &fAuditTrail; }
joshualitt27a48dc2016-01-08 07:19:47 -0800364
joshualittce894002016-01-11 13:29:31 -0800365 /** This is only useful for debug purposes */
366 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } )
367
bsalomon@google.com27847de2011-02-22 20:59:41 +0000368private:
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000369 GrGpu* fGpu;
bsalomon76228632015-05-29 08:02:10 -0700370 const GrCaps* fCaps;
bsalomon0ea80f42015-02-11 10:49:59 -0800371 GrResourceCache* fResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -0700372 // this union exists because the inheritance of GrTextureProvider->GrResourceProvider
373 // is in a private header.
mtklein2766c002015-06-26 11:45:03 -0700374 union {
bsalomond309e7a2015-04-30 14:18:54 -0700375 GrResourceProvider* fResourceProvider;
376 GrTextureProvider* fTextureProvider;
377 };
378
joshualitt7c3a2f82015-03-31 13:32:05 -0700379 GrBatchFontCache* fBatchFontCache;
robertphillips@google.come930a072014-04-03 00:34:27 +0000380 SkAutoTDelete<GrLayerCache> fLayerCache;
joshualittb7133be2015-04-08 09:08:31 -0700381 SkAutoTDelete<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000382
bsalomonf21dab92014-11-13 13:33:28 -0800383 // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
384 bool fFlushToReduceCacheSize;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000385 bool fDidTestPMConversions;
386 int fPMToUPMConversion;
387 int fUPMToPMConversion;
bsalomon6c6f6582015-09-10 08:12:46 -0700388 // The sw backend may call GrContext::readSurfacePixels on multiple threads
389 // We may transfer the responsibilty for using a mutex to the sw backend
390 // when there are fewer code paths that lead to a readSurfacePixels call
391 // from the sw backend. readSurfacePixels is reentrant in one case - when performing
392 // the PM conversions test. To handle this we do the PM conversions test outside
393 // of fReadPixelsMutex and use a separate mutex to guard it. When it re-enters
394 // readSurfacePixels it will grab fReadPixelsMutex and release it before the outer
395 // readSurfacePixels proceeds to grab it.
396 // TODO: Stop pretending to make GrContext thread-safe for sw rasterization and provide
397 // a mechanism to make a SkPicture safe for multithreaded sw rasterization.
398 SkMutex fReadPixelsMutex;
399 SkMutex fTestPMConversionsMutex;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000400
joshualitt1de610a2016-01-06 08:26:09 -0800401 // In debug builds we guard against improper thread handling
joshualittde8dc7e2016-01-08 10:09:13 -0800402 // This guard is passed to the GrDrawingManager and, from there to all the
403 // GrDrawContexts. It is also passed to the GrTextureProvider and SkGpuDevice.
404 mutable GrSingleOwner fSingleOwner;
joshualitt1de610a2016-01-06 08:26:09 -0800405
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000406 struct CleanUpData {
407 PFCleanUpFunc fFunc;
408 void* fInfo;
409 };
410
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000411 SkTDArray<CleanUpData> fCleanUpData;
412
joshualitt0acd0d32015-05-07 08:23:19 -0700413 const uint32_t fUniqueID;
krajcevski9c6d4d72014-08-12 07:26:25 -0700414
robertphillips77a2e522015-10-17 07:43:27 -0700415 SkAutoTDelete<GrDrawingManager> fDrawingManager;
416
joshualitt27a48dc2016-01-08 07:19:47 -0800417 GrAuditTrail fAuditTrail;
418
robertphillips68737822015-10-29 12:12:21 -0700419 // TODO: have the CMM use drawContexts and rm this friending
420 friend class GrClipMaskManager; // the CMM is friended just so it can call 'drawingManager'
robertphillipsa13e2022015-11-11 12:01:09 -0800421 friend class GrDrawingManager; // for access to drawingManager for ProgramUnitTest
robertphillips68737822015-10-29 12:12:21 -0700422 GrDrawingManager* drawingManager() { return fDrawingManager; }
423
bsalomon682c2692015-05-22 14:01:46 -0700424 GrContext(); // init must be called after the constructor.
425 bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
robertphillipsea461502015-05-26 11:38:03 -0700426
bsalomon33435572014-11-05 14:47:41 -0800427 void initMockContext();
bsalomon69cfe952015-11-30 13:27:47 -0800428 void initCommon(const GrContextOptions&);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000429
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000430 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000431 * These functions create premul <-> unpremul effects if it is possible to generate a pair
432 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
bsalomon6c9cd552016-01-22 07:17:34 -0800433 * return NULL. They also can perform a swizzle as part of the draw.
bsalomon@google.comadc65362013-01-28 14:26:09 +0000434 */
bsalomon6c9cd552016-01-22 07:17:34 -0800435 const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, const GrSwizzle&,
bsalomon4a339522015-10-06 08:40:50 -0700436 const SkMatrix&) const;
bsalomon6c9cd552016-01-22 07:17:34 -0800437 const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, const GrSwizzle&,
bsalomon4a339522015-10-06 08:40:50 -0700438 const SkMatrix&) const;
bsalomon6c6f6582015-09-10 08:12:46 -0700439 /** Called before either of the above two functions to determine the appropriate fragment
bsalomon4a339522015-10-06 08:40:50 -0700440 processors for conversions. This must be called by readSurfacePixels before a mutex is
441 taken, since testingvPM conversions itself will call readSurfacePixels */
bsalomon6c6f6582015-09-10 08:12:46 -0700442 void testPMConversionsIfNecessary(uint32_t flags);
bsalomon636e8022015-07-29 06:08:46 -0700443 /** Returns true if we've already determined that createPMtoUPMEffect and createUPMToPMEffect
444 will fail. In such cases fall back to SW conversion. */
445 bool didFailPMUPMConversionTest() const;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000446
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000447 /**
448 * This callback allows the resource cache to callback into the GrContext
bsalomon71cb0c22014-11-14 12:10:14 -0800449 * when the cache is still over budget after a purge.
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000450 */
bsalomon71cb0c22014-11-14 12:10:14 -0800451 static void OverBudgetCB(void* data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000452
joshualitt0db6dfa2015-04-10 07:01:30 -0700453 /**
454 * A callback similar to the above for use by the TextBlobCache
455 * TODO move textblob draw calls below context so we can use the call above.
456 */
457 static void TextBlobCacheOverBudgetCB(void* data);
458
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000459 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000460};
461
bsalomon@google.com27847de2011-02-22 20:59:41 +0000462#endif