bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef GrContext_DEFINED |
| 9 | #define GrContext_DEFINED |
| 10 | |
robertphillips | caef345 | 2015-11-11 13:18:11 -0800 | [diff] [blame] | 11 | #include "GrCaps.h" |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 12 | #include "GrClip.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 13 | #include "GrColor.h" |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 14 | #include "GrPaint.h" |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 16 | #include "GrTextureProvider.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 17 | #include "SkMatrix.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 18 | #include "SkPathEffect.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 20 | #include "../private/GrAuditTrail.h" |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 21 | #include "../private/GrSingleOwner.h" |
| 22 | #include "../private/SkMutex.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 23 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 24 | struct GrBatchAtlasConfig; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 25 | class GrBatchFontCache; |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 26 | struct GrContextOptions; |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 27 | class GrContextPriv; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 28 | class GrContextThreadSafeProxy; |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 29 | class GrDrawingManager; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 30 | class GrDrawContext; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 31 | class GrFragmentProcessor; |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 32 | class GrGpu; |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 33 | class GrIndexBuffer; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 34 | class GrOvalRenderer; |
commit-bot@chromium.org | 5c8ee25 | 2013-11-01 15:23:44 +0000 | [diff] [blame] | 35 | class GrPath; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 36 | class GrPipelineBuilder; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 37 | class GrResourceEntry; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 38 | class GrResourceCache; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 39 | class GrResourceProvider; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 40 | class GrTestTarget; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 41 | class GrTextBlobCache; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 42 | class GrTextContext; |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 43 | class GrTextureParams; |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 44 | class GrVertexBuffer; |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 45 | class GrSwizzle; |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 46 | class SkTraceMemoryDump; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 48 | class SK_API GrContext : public SkRefCnt { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 49 | public: |
| 50 | /** |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 51 | * Creates a GrContext for a backend context. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 52 | */ |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 53 | static GrContext* Create(GrBackend, GrBackendContext, const GrContextOptions& options); |
| 54 | static GrContext* Create(GrBackend, GrBackendContext); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 55 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Only defined in test apps. |
| 58 | */ |
| 59 | static GrContext* CreateMockContext(); |
| 60 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 61 | virtual ~GrContext(); |
| 62 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 63 | GrContextThreadSafeProxy* threadSafeProxy(); |
| 64 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 65 | /** |
| 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.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 70 | * The flag bits, state, is dpendent on which backend is used by the |
| 71 | * context, either GL or D3D (possible in future). |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 72 | */ |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 73 | void resetContext(uint32_t state = kAll_GrBackendState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 75 | /** |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 76 | * 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 | /** |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 94 | * 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.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 103 | */ |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 104 | void abandonContext(); |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 105 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 106 | /** |
| 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.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 117 | /////////////////////////////////////////////////////////////////////////// |
| 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.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 129 | |
| 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.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 140 | /** |
| 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.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 150 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 151 | GrTextureProvider* textureProvider() { return fTextureProvider; } |
| 152 | const GrTextureProvider* textureProvider() const { return fTextureProvider; } |
| 153 | |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 154 | /** |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 155 | * Frees GPU created by the context. Can be called to reduce GPU memory |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 156 | * pressure. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 157 | */ |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 158 | void freeGpuResources(); |
| 159 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 160 | /** |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 161 | * 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.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 164 | */ |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 165 | void purgeAllUnlockedResources(); |
| 166 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 167 | /** Access the context capabilities */ |
| 168 | const GrCaps* caps() const { return fCaps; } |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 169 | |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 170 | /** |
| 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.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 183 | /** |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 184 | * Create both a GrRenderTarget and a matching GrDrawContext to wrap it. |
robertphillips | ebf30e8 | 2016-05-11 08:34:39 -0700 | [diff] [blame] | 185 | * We guarantee that "asTexture" will succeed for drawContexts created |
| 186 | * via this entry point. |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 187 | */ |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame] | 188 | 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); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 196 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 197 | /////////////////////////////////////////////////////////////////////////// |
| 198 | // Misc. |
| 199 | |
| 200 | /** |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 201 | * Flags that affect flush() behavior. |
| 202 | */ |
| 203 | enum FlushBits { |
| 204 | /** |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 205 | * 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.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 214 | * Call to ensure all drawing to the context has been issued to the |
| 215 | * underlying 3D API. |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 216 | * @param flagsBitfield flags that control the flushing behavior. See |
| 217 | * FlushBits. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 218 | */ |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 219 | void flush(int flagsBitfield = 0); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 220 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 221 | void flushIfNecessary() { |
robertphillips | caef345 | 2015-11-11 13:18:11 -0800 | [diff] [blame] | 222 | if (fFlushToReduceCacheSize || this->caps()->immediateFlush()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 223 | this->flush(); |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 224 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 225 | } |
| 226 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 227 | /** |
| 228 | * These flags can be used with the read/write pixels functions below. |
| 229 | */ |
| 230 | enum PixelOpsFlags { |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 231 | /** 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.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 233 | kDontFlush_PixelOpsFlag = 0x1, |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 234 | /** Any surface writes should be flushed to the backend 3D API after the surface operation |
| 235 | is complete */ |
| 236 | kFlushWrites_PixelOp = 0x2, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 237 | /** 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. */ |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 239 | kUnpremul_PixelOpsFlag = 0x4, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 242 | /** |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 243 | * Reads a rectangle of pixels from a surface. |
| 244 | * @param surface the surface to read from. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 245 | * @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.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 251 | * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly |
| 252 | * packed. |
| 253 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 254 | * |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 255 | * @return true if the read succeeded, false if not. The read can fail because of an unsupported |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 256 | * pixel configs |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 257 | */ |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 258 | 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.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 263 | |
| 264 | /** |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 265 | * Writes a rectangle of pixels to a surface. |
| 266 | * @param surface the surface to write to. |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 267 | * @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.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 273 | * @param rowBytes number of bytes between consecutive rows. Zero |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 274 | * means rows are tightly packed. |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 275 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 276 | * @return true if the write succeeded, false if not. The write can fail because of an |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 277 | * unsupported combination of surface and src configs. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 278 | */ |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 279 | bool writeSurfacePixels(GrSurface* surface, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 280 | int left, int top, int width, int height, |
| 281 | GrPixelConfig config, const void* buffer, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 282 | size_t rowBytes, |
| 283 | uint32_t pixelOpsFlags = 0); |
| 284 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 285 | /** |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 286 | * Copies a rectangle of texels from src to dst. |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 287 | * @param dst the surface to copy to. |
| 288 | * @param src the surface to copy from. |
| 289 | * @param srcRect the rectangle of the src that should be copied. |
| 290 | * @param dstPoint the translation applied when writing the srcRect's pixels to the dst. |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 291 | */ |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 292 | bool copySurface(GrSurface* dst, |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 293 | GrSurface* src, |
| 294 | const SkIRect& srcRect, |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 295 | const SkIPoint& dstPoint); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 296 | |
| 297 | /** Helper that copies the whole surface but fails when the two surfaces are not identically |
| 298 | sized. */ |
| 299 | bool copySurface(GrSurface* dst, GrSurface* src) { |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 300 | return this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()), |
| 301 | SkIPoint::Make(0,0)); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
| 305 | * After this returns any pending writes to the surface will have been issued to the backend 3D API. |
| 306 | */ |
| 307 | void flushSurfaceWrites(GrSurface* surface); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 308 | |
| 309 | /** |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 310 | * After this returns any pending reads or writes to the surface will have been issued to the |
| 311 | * backend 3D API. |
| 312 | */ |
| 313 | void flushSurfaceIO(GrSurface* surface); |
| 314 | |
| 315 | /** |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 316 | * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve |
| 317 | * if necessary. |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 318 | * |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 319 | * It is not necessary to call this before reading the render target via Skia/GrContext. |
| 320 | * GrContext will detect when it must perform a resolve before reading pixels back from the |
| 321 | * surface or using it as a texture. |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 322 | */ |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 323 | void prepareSurfaceForExternalIO(GrSurface*); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 324 | |
| 325 | /** |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 326 | * An ID associated with this context, guaranteed to be unique. |
| 327 | */ |
| 328 | uint32_t uniqueID() { return fUniqueID; } |
| 329 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 330 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 331 | // Functions intended for internal use only. |
| 332 | GrGpu* getGpu() { return fGpu; } |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 333 | const GrGpu* getGpu() const { return fGpu; } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 334 | GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 335 | GrTextBlobCache* getTextBlobCache() { return fTextBlobCache; } |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 336 | bool abandoned() const; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 337 | GrResourceProvider* resourceProvider() { return fResourceProvider; } |
| 338 | const GrResourceProvider* resourceProvider() const { return fResourceProvider; } |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 339 | GrResourceCache* getResourceCache() { return fResourceCache; } |
bsalomon@google.com | 9923c2b | 2012-06-06 18:21:18 +0000 | [diff] [blame] | 340 | |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 341 | // Called by tests that draw directly to the context via GrDrawContext |
| 342 | void getTestTarget(GrTestTarget*, sk_sp<GrDrawContext>); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 343 | |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 344 | /** Reset GPU stats */ |
| 345 | void resetGpuStats() const ; |
| 346 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 347 | /** Prints cache stats to the string if GR_CACHE_STATS == 1. */ |
| 348 | void dumpCacheStats(SkString*) const; |
joshualitt | dc5685a | 2015-12-02 14:08:25 -0800 | [diff] [blame] | 349 | void dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 350 | void printCacheStats() const; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 351 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 352 | /** Prints GPU stats to the string if GR_GPU_STATS == 1. */ |
| 353 | void dumpGpuStats(SkString*) const; |
joshualitt | dc5685a | 2015-12-02 14:08:25 -0800 | [diff] [blame] | 354 | void dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) const; |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 355 | void printGpuStats() const; |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 356 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 357 | /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge. |
| 358 | this is for testing only */ |
| 359 | void setTextBlobCacheLimit_ForTesting(size_t bytes); |
| 360 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 361 | /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be |
| 362 | to an array of 3 entries */ |
| 363 | void setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs); |
| 364 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 365 | /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */ |
| 366 | void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 367 | |
jvanverth | 0671b96 | 2015-12-08 18:53:44 -0800 | [diff] [blame] | 368 | /** Get pointer to atlas texture for given mask format */ |
| 369 | GrTexture* getFontAtlasTexture(GrMaskFormat format); |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 370 | |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 371 | GrAuditTrail* getAuditTrail() { return &fAuditTrail; } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 372 | |
joshualitt | ce89400 | 2016-01-11 13:29:31 -0800 | [diff] [blame] | 373 | /** This is only useful for debug purposes */ |
| 374 | SkDEBUGCODE(GrSingleOwner* debugSingleOwner() const { return &fSingleOwner; } ) |
| 375 | |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 376 | // Provides access to functions that aren't part of the public API. |
| 377 | GrContextPriv contextPriv(); |
| 378 | const GrContextPriv contextPriv() const; |
| 379 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 380 | private: |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 381 | GrGpu* fGpu; |
| 382 | const GrCaps* fCaps; |
| 383 | GrResourceCache* fResourceCache; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 384 | // this union exists because the inheritance of GrTextureProvider->GrResourceProvider |
| 385 | // is in a private header. |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 386 | union { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 387 | GrResourceProvider* fResourceProvider; |
| 388 | GrTextureProvider* fTextureProvider; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 389 | }; |
| 390 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 391 | SkAutoTUnref<GrContextThreadSafeProxy> fThreadSafeProxy; |
| 392 | |
| 393 | GrBatchFontCache* fBatchFontCache; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 394 | SkAutoTDelete<GrTextBlobCache> fTextBlobCache; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 395 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 396 | // Set by OverbudgetCB() to request that GrContext flush before exiting a draw. |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 397 | bool fFlushToReduceCacheSize; |
| 398 | bool fDidTestPMConversions; |
| 399 | int fPMToUPMConversion; |
| 400 | int fUPMToPMConversion; |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 401 | // The sw backend may call GrContext::readSurfacePixels on multiple threads |
| 402 | // We may transfer the responsibilty for using a mutex to the sw backend |
| 403 | // when there are fewer code paths that lead to a readSurfacePixels call |
| 404 | // from the sw backend. readSurfacePixels is reentrant in one case - when performing |
| 405 | // the PM conversions test. To handle this we do the PM conversions test outside |
| 406 | // of fReadPixelsMutex and use a separate mutex to guard it. When it re-enters |
| 407 | // readSurfacePixels it will grab fReadPixelsMutex and release it before the outer |
| 408 | // readSurfacePixels proceeds to grab it. |
| 409 | // TODO: Stop pretending to make GrContext thread-safe for sw rasterization and provide |
| 410 | // a mechanism to make a SkPicture safe for multithreaded sw rasterization. |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 411 | SkMutex fReadPixelsMutex; |
| 412 | SkMutex fTestPMConversionsMutex; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 413 | |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 414 | // In debug builds we guard against improper thread handling |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 415 | // This guard is passed to the GrDrawingManager and, from there to all the |
| 416 | // GrDrawContexts. It is also passed to the GrTextureProvider and SkGpuDevice. |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 417 | mutable GrSingleOwner fSingleOwner; |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 418 | |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 419 | struct CleanUpData { |
| 420 | PFCleanUpFunc fFunc; |
| 421 | void* fInfo; |
| 422 | }; |
| 423 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 424 | SkTDArray<CleanUpData> fCleanUpData; |
robertphillips@google.com | 44a91dc | 2013-07-25 15:32:06 +0000 | [diff] [blame] | 425 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 426 | const uint32_t fUniqueID; |
krajcevski | 9c6d4d7 | 2014-08-12 07:26:25 -0700 | [diff] [blame] | 427 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 428 | SkAutoTDelete<GrDrawingManager> fDrawingManager; |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 429 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 430 | GrAuditTrail fAuditTrail; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 431 | |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 432 | // TODO: have the GrClipStackClip use drawContexts and rm this friending |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 433 | friend class GrContextPriv; |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 434 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 435 | GrContext(); // init must be called after the constructor. |
| 436 | bool init(GrBackend, GrBackendContext, const GrContextOptions& options); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 437 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 438 | void initMockContext(); |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 439 | void initCommon(const GrContextOptions&); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 440 | |
skia.committer@gmail.com | cdcb2ce | 2013-01-29 07:05:52 +0000 | [diff] [blame] | 441 | /** |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 442 | * These functions create premul <-> unpremul effects if it is possible to generate a pair |
| 443 | * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 444 | * return NULL. They also can perform a swizzle as part of the draw. |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 445 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 446 | sk_sp<GrFragmentProcessor> createPMToUPMEffect(GrTexture*, const GrSwizzle&, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 447 | const SkMatrix&) const; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 448 | sk_sp<GrFragmentProcessor> createUPMToPMEffect(GrTexture*, const GrSwizzle&, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 449 | const SkMatrix&) const; |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 450 | /** Called before either of the above two functions to determine the appropriate fragment |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 451 | processors for conversions. This must be called by readSurfacePixels before a mutex is |
| 452 | taken, since testingvPM conversions itself will call readSurfacePixels */ |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 453 | void testPMConversionsIfNecessary(uint32_t flags); |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 454 | /** Returns true if we've already determined that createPMtoUPMEffect and createUPMToPMEffect |
| 455 | will fail. In such cases fall back to SW conversion. */ |
| 456 | bool didFailPMUPMConversionTest() const; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 457 | |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 458 | /** |
| 459 | * This callback allows the resource cache to callback into the GrContext |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 460 | * when the cache is still over budget after a purge. |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 461 | */ |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 462 | static void OverBudgetCB(void* data); |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 463 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 464 | /** |
| 465 | * A callback similar to the above for use by the TextBlobCache |
| 466 | * TODO move textblob draw calls below context so we can use the call above. |
| 467 | */ |
| 468 | static void TextBlobCacheOverBudgetCB(void* data); |
| 469 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 470 | typedef SkRefCnt INHERITED; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 473 | /** |
| 474 | * Can be used to perform actions related to the generating GrContext in a thread safe manner. The |
| 475 | * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext. |
| 476 | */ |
| 477 | class GrContextThreadSafeProxy : public SkRefCnt { |
| 478 | private: |
| 479 | GrContextThreadSafeProxy(const GrCaps* caps, uint32_t uniqueID) |
| 480 | : fCaps(SkRef(caps)) |
| 481 | , fContextUniqueID(uniqueID) {} |
| 482 | |
| 483 | SkAutoTUnref<const GrCaps> fCaps; |
| 484 | uint32_t fContextUniqueID; |
| 485 | |
| 486 | friend class GrContext; |
| 487 | friend class SkImage; |
| 488 | |
| 489 | typedef SkRefCnt INHERITED; |
| 490 | }; |
| 491 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 492 | #endif |