blob: 8e5781100e2d94d37d387e73ce98bc9c378726df [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;
robertphillipsea461502015-05-26 11:38:03 -070030class GrDrawContext;
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 /**
robertphillipsd4c741e2016-04-28 09:55:15 -0700184 * Create both a GrRenderTarget and a matching GrDrawContext to wrap it.
robertphillipsebf30e82016-05-11 08:34:39 -0700185 * We guarantee that "asTexture" will succeed for drawContexts created
186 * via this entry point.
robertphillipsd4c741e2016-04-28 09:55:15 -0700187 */
robertphillips6738c702016-07-27 12:13:51 -0700188 sk_sp<GrDrawContext> makeDrawContext(SkBackingFit fit,
189 int width, int height,
190 GrPixelConfig config,
191 sk_sp<SkColorSpace> colorSpace,
192 int sampleCnt = 0,
193 GrSurfaceOrigin origin = kDefault_GrSurfaceOrigin,
194 const SkSurfaceProps* surfaceProps = nullptr,
195 SkBudgeted = SkBudgeted::kYes);
robertphillipsd4c741e2016-04-28 09:55:15 -0700196
robertphillips48fde9c2016-09-06 05:20:20 -0700197 /*
198 * This method will attempt to create a drawContext that has, at least, the number of
199 * channels and precision per channel as requested in 'config' (e.g., A8 and 888 can be
200 * converted to 8888). It may also swizzle the channels (e.g., BGRA -> RGBA).
201 * SRGB-ness will be preserved.
202 */
203 sk_sp<GrDrawContext> makeDrawContextWithFallback(
204 SkBackingFit fit,
205 int width, int height,
206 GrPixelConfig config,
207 sk_sp<SkColorSpace> colorSpace,
208 int sampleCnt = 0,
209 GrSurfaceOrigin origin = kDefault_GrSurfaceOrigin,
210 const SkSurfaceProps* surfaceProps = nullptr,
211 SkBudgeted budgeted = SkBudgeted::kYes);
212
bsalomon@google.com27847de2011-02-22 20:59:41 +0000213 ///////////////////////////////////////////////////////////////////////////
214 // Misc.
215
216 /**
217 * Call to ensure all drawing to the context has been issued to the
218 * underlying 3D API.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000219 */
bsalomonb77a9072016-09-07 10:02:04 -0700220 void flush();
robertphillipsea461502015-05-26 11:38:03 -0700221
bsalomon@google.com0342a852012-08-20 19:22:38 +0000222 /**
223 * These flags can be used with the read/write pixels functions below.
224 */
225 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700226 /** The GrContext will not be flushed before the surface read or write. This means that
227 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000228 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700229 /** Any surface writes should be flushed to the backend 3D API after the surface operation
230 is complete */
231 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000232 /** The src for write or dst read is unpremultiplied. This is only respected if both the
233 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700234 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000235 };
236
bsalomon@google.com27847de2011-02-22 20:59:41 +0000237 /**
bsalomone8d21e82015-07-16 08:23:13 -0700238 * Reads a rectangle of pixels from a surface.
239 * @param surface the surface to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000240 * @param left left edge of the rectangle to read (inclusive)
241 * @param top top edge of the rectangle to read (inclusive)
242 * @param width width of rectangle to read in pixels.
243 * @param height height of rectangle to read in pixels.
244 * @param config the pixel config of the destination buffer
245 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000246 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
247 * packed.
248 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000249 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000250 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
bsalomone8d21e82015-07-16 08:23:13 -0700251 * pixel configs
bsalomon@google.com27847de2011-02-22 20:59:41 +0000252 */
bsalomone8d21e82015-07-16 08:23:13 -0700253 bool readSurfacePixels(GrSurface* surface,
254 int left, int top, int width, int height,
255 GrPixelConfig config, void* buffer,
256 size_t rowBytes = 0,
257 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000258
259 /**
bsalomon81beccc2014-10-13 12:32:55 -0700260 * Writes a rectangle of pixels to a surface.
261 * @param surface the surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000262 * @param left left edge of the rectangle to write (inclusive)
263 * @param top top edge of the rectangle to write (inclusive)
264 * @param width width of rectangle to write in pixels.
265 * @param height height of rectangle to write in pixels.
266 * @param config the pixel config of the source buffer
267 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000268 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000269 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000270 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000271 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700272 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000273 */
bsalomon81beccc2014-10-13 12:32:55 -0700274 bool writeSurfacePixels(GrSurface* surface,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000275 int left, int top, int width, int height,
276 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000277 size_t rowBytes,
278 uint32_t pixelOpsFlags = 0);
279
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000280 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700281 * Copies a rectangle of texels from src to dst.
bsalomonf80bfed2014-10-07 05:56:02 -0700282 * @param dst the surface to copy to.
283 * @param src the surface to copy from.
284 * @param srcRect the rectangle of the src that should be copied.
285 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst.
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000286 */
bsalomonb8fea972016-02-16 07:34:17 -0800287 bool copySurface(GrSurface* dst,
bsalomonf80bfed2014-10-07 05:56:02 -0700288 GrSurface* src,
289 const SkIRect& srcRect,
bsalomonb8fea972016-02-16 07:34:17 -0800290 const SkIPoint& dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700291
292 /** Helper that copies the whole surface but fails when the two surfaces are not identically
293 sized. */
294 bool copySurface(GrSurface* dst, GrSurface* src) {
bsalomonb8fea972016-02-16 07:34:17 -0800295 return this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
296 SkIPoint::Make(0,0));
bsalomonf80bfed2014-10-07 05:56:02 -0700297 }
298
299 /**
300 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
301 */
302 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000303
304 /**
ajuma95243eb2016-08-24 08:19:02 -0700305 * After this returns any pending reads or writes to the surface will have been issued to the
306 * backend 3D API.
307 */
308 void flushSurfaceIO(GrSurface* surface);
309
310 /**
bsalomonc49e8682015-06-30 11:37:35 -0700311 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
312 * if necessary.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000313 *
bsalomon87a94eb2014-11-03 14:28:32 -0800314 * It is not necessary to call this before reading the render target via Skia/GrContext.
315 * GrContext will detect when it must perform a resolve before reading pixels back from the
316 * surface or using it as a texture.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000317 */
bsalomonc49e8682015-06-30 11:37:35 -0700318 void prepareSurfaceForExternalIO(GrSurface*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000319
320 /**
joshualitt0acd0d32015-05-07 08:23:19 -0700321 * An ID associated with this context, guaranteed to be unique.
322 */
323 uint32_t uniqueID() { return fUniqueID; }
324
bsalomon@google.com27847de2011-02-22 20:59:41 +0000325 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000326 // Functions intended for internal use only.
327 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000328 const GrGpu* getGpu() const { return fGpu; }
joshualitt7c3a2f82015-03-31 13:32:05 -0700329 GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; }
bungeman6bd52842016-10-27 09:30:08 -0700330 GrTextBlobCache* getTextBlobCache() { return fTextBlobCache.get(); }
robertphillips77a2e522015-10-17 07:43:27 -0700331 bool abandoned() const;
bsalomond309e7a2015-04-30 14:18:54 -0700332 GrResourceProvider* resourceProvider() { return fResourceProvider; }
333 const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
bsalomon0ea80f42015-02-11 10:49:59 -0800334 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000335
robertphillips87f15c82016-05-20 11:14:33 -0700336 // Called by tests that draw directly to the context via GrDrawContext
337 void getTestTarget(GrTestTarget*, sk_sp<GrDrawContext>);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +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 */
358 void setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs);
359
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
jvanverth0671b962015-12-08 18:53:44 -0800363 /** Get pointer to atlas texture for given mask format */
364 GrTexture* getFontAtlasTexture(GrMaskFormat format);
jvanverth629162d2015-11-08 08:07:24 -0800365
joshualitt5651ee62016-01-11 10:39:11 -0800366 GrAuditTrail* getAuditTrail() { return &fAuditTrail; }
joshualitt27a48dc2016-01-08 07:19:47 -0800367
joshualittce894002016-01-11 13:29:31 -0800368 /** This is only useful for debug purposes */
369 SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } )
370
robertphillips4fd74ae2016-08-03 14:26:53 -0700371 // Provides access to functions that aren't part of the public API.
372 GrContextPriv contextPriv();
373 const GrContextPriv contextPriv() const;
374
bsalomon@google.com27847de2011-02-22 20:59:41 +0000375private:
bsalomon41b952c2016-03-11 06:46:33 -0800376 GrGpu* fGpu;
377 const GrCaps* fCaps;
378 GrResourceCache* fResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -0700379 // this union exists because the inheritance of GrTextureProvider->GrResourceProvider
380 // is in a private header.
mtklein2766c002015-06-26 11:45:03 -0700381 union {
bsalomon41b952c2016-03-11 06:46:33 -0800382 GrResourceProvider* fResourceProvider;
383 GrTextureProvider* fTextureProvider;
bsalomond309e7a2015-04-30 14:18:54 -0700384 };
385
bungeman6bd52842016-10-27 09:30:08 -0700386 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800387
388 GrBatchFontCache* fBatchFontCache;
bungeman6bd52842016-10-27 09:30:08 -0700389 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000390
bsalomon41b952c2016-03-11 06:46:33 -0800391 bool fDidTestPMConversions;
392 int fPMToUPMConversion;
393 int fUPMToPMConversion;
bsalomon6c6f6582015-09-10 08:12:46 -0700394 // The sw backend may call GrContext::readSurfacePixels on multiple threads
395 // We may transfer the responsibilty for using a mutex to the sw backend
396 // when there are fewer code paths that lead to a readSurfacePixels call
397 // from the sw backend. readSurfacePixels is reentrant in one case - when performing
398 // the PM conversions test. To handle this we do the PM conversions test outside
399 // of fReadPixelsMutex and use a separate mutex to guard it. When it re-enters
400 // readSurfacePixels it will grab fReadPixelsMutex and release it before the outer
401 // readSurfacePixels proceeds to grab it.
402 // TODO: Stop pretending to make GrContext thread-safe for sw rasterization and provide
403 // a mechanism to make a SkPicture safe for multithreaded sw rasterization.
bsalomon41b952c2016-03-11 06:46:33 -0800404 SkMutex fReadPixelsMutex;
405 SkMutex fTestPMConversionsMutex;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000406
joshualitt1de610a2016-01-06 08:26:09 -0800407 // In debug builds we guard against improper thread handling
joshualittde8dc7e2016-01-08 10:09:13 -0800408 // This guard is passed to the GrDrawingManager and, from there to all the
409 // GrDrawContexts. It is also passed to the GrTextureProvider and SkGpuDevice.
bsalomon41b952c2016-03-11 06:46:33 -0800410 mutable GrSingleOwner fSingleOwner;
joshualitt1de610a2016-01-06 08:26:09 -0800411
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000412 struct CleanUpData {
413 PFCleanUpFunc fFunc;
414 void* fInfo;
415 };
416
bsalomon41b952c2016-03-11 06:46:33 -0800417 SkTDArray<CleanUpData> fCleanUpData;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000418
bsalomon41b952c2016-03-11 06:46:33 -0800419 const uint32_t fUniqueID;
krajcevski9c6d4d72014-08-12 07:26:25 -0700420
bungeman6bd52842016-10-27 09:30:08 -0700421 std::unique_ptr<GrDrawingManager> fDrawingManager;
robertphillips77a2e522015-10-17 07:43:27 -0700422
bsalomon41b952c2016-03-11 06:46:33 -0800423 GrAuditTrail fAuditTrail;
joshualitt27a48dc2016-01-08 07:19:47 -0800424
csmartdaltonc6f411e2016-08-05 22:32:12 -0700425 // TODO: have the GrClipStackClip use drawContexts and rm this friending
robertphillips4fd74ae2016-08-03 14:26:53 -0700426 friend class GrContextPriv;
csmartdaltonc6f411e2016-08-05 22:32:12 -0700427
bsalomon682c2692015-05-22 14:01:46 -0700428 GrContext(); // init must be called after the constructor.
429 bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
robertphillipsea461502015-05-26 11:38:03 -0700430
bsalomon33435572014-11-05 14:47:41 -0800431 void initMockContext();
bsalomon69cfe952015-11-30 13:27:47 -0800432 void initCommon(const GrContextOptions&);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000433
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000434 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000435 * These functions create premul <-> unpremul effects if it is possible to generate a pair
436 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
bsalomon6c9cd552016-01-22 07:17:34 -0800437 * return NULL. They also can perform a swizzle as part of the draw.
bsalomon@google.comadc65362013-01-28 14:26:09 +0000438 */
bungeman06ca8ec2016-06-09 08:01:03 -0700439 sk_sp<GrFragmentProcessor> createPMToUPMEffect(GrTexture*, const GrSwizzle&,
bsalomon4a339522015-10-06 08:40:50 -0700440 const SkMatrix&) const;
bungeman06ca8ec2016-06-09 08:01:03 -0700441 sk_sp<GrFragmentProcessor> createUPMToPMEffect(GrTexture*, const GrSwizzle&,
bsalomon4a339522015-10-06 08:40:50 -0700442 const SkMatrix&) const;
bsalomon6c6f6582015-09-10 08:12:46 -0700443 /** Called before either of the above two functions to determine the appropriate fragment
bsalomon4a339522015-10-06 08:40:50 -0700444 processors for conversions. This must be called by readSurfacePixels before a mutex is
445 taken, since testingvPM conversions itself will call readSurfacePixels */
bsalomon6c6f6582015-09-10 08:12:46 -0700446 void testPMConversionsIfNecessary(uint32_t flags);
bsalomon636e8022015-07-29 06:08:46 -0700447 /** Returns true if we've already determined that createPMtoUPMEffect and createUPMToPMEffect
448 will fail. In such cases fall back to SW conversion. */
449 bool didFailPMUPMConversionTest() const;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000450
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000451 /**
joshualitt0db6dfa2015-04-10 07:01:30 -0700452 * A callback similar to the above for use by the TextBlobCache
453 * TODO move textblob draw calls below context so we can use the call above.
454 */
455 static void TextBlobCacheOverBudgetCB(void* data);
456
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000457 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000458};
459
bsalomon41b952c2016-03-11 06:46:33 -0800460/**
461 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
462 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
463 */
464class GrContextThreadSafeProxy : public SkRefCnt {
465private:
bungeman6bd52842016-10-27 09:30:08 -0700466 GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID)
467 : fCaps(std::move(caps))
bsalomon41b952c2016-03-11 06:46:33 -0800468 , fContextUniqueID(uniqueID) {}
469
bungeman6bd52842016-10-27 09:30:08 -0700470 sk_sp<const GrCaps> fCaps;
471 uint32_t fContextUniqueID;
bsalomon41b952c2016-03-11 06:46:33 -0800472
473 friend class GrContext;
474 friend class SkImage;
475
476 typedef SkRefCnt INHERITED;
477};
478
bsalomon@google.com27847de2011-02-22 20:59:41 +0000479#endif