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 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 11 | #include "GrClip.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 12 | #include "GrColor.h" |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 13 | #include "GrPaint.h" |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 14 | #include "GrPathRendererChain.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" |
herb | 62a69c2 | 2015-09-29 11:47:45 -0700 | [diff] [blame] | 18 | #include "../private/SkMutex.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 19 | #include "SkPathEffect.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 20 | #include "SkTypes.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 21 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 22 | struct GrBatchAtlasConfig; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 23 | class GrBatchFontCache; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 24 | class GrCaps; |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 25 | struct GrContextOptions; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 26 | class GrDrawContext; |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 27 | class GrDrawTarget; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | class GrFragmentProcessor; |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 29 | class GrGpu; |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 30 | class GrIndexBuffer; |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 31 | class GrLayerCache; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 32 | class GrOvalRenderer; |
commit-bot@chromium.org | 5c8ee25 | 2013-11-01 15:23:44 +0000 | [diff] [blame] | 33 | class GrPath; |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 34 | class GrPathRenderer; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 35 | class GrPipelineBuilder; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 36 | class GrResourceEntry; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 37 | class GrResourceCache; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 38 | class GrResourceProvider; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 39 | class GrTestTarget; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 40 | class GrTextBlobCache; |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 41 | class GrTextContext; |
bsalomon@google.com | 288d954 | 2012-10-17 12:53:54 +0000 | [diff] [blame] | 42 | class GrTextureParams; |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 43 | class GrVertexBuffer; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 44 | class GrStrokeInfo; |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 45 | class GrSoftwarePathRenderer; |
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 | |
| 63 | /** |
| 64 | * The GrContext normally assumes that no outsider is setting state |
| 65 | * within the underlying 3D API's context/device/whatever. This call informs |
| 66 | * the context that the state was modified and it should resend. Shouldn't |
| 67 | * be called frequently for good performance. |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 68 | * The flag bits, state, is dpendent on which backend is used by the |
| 69 | * context, either GL or D3D (possible in future). |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 70 | */ |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 71 | void resetContext(uint32_t state = kAll_GrBackendState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 73 | /** |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 74 | * Callback function to allow classes to cleanup on GrContext destruction. |
| 75 | * The 'info' field is filled in with the 'info' passed to addCleanUp. |
| 76 | */ |
| 77 | typedef void (*PFCleanUpFunc)(const GrContext* context, void* info); |
| 78 | |
| 79 | /** |
| 80 | * Add a function to be called from within GrContext's destructor. |
| 81 | * This gives classes a chance to free resources held on a per context basis. |
| 82 | * The 'info' parameter will be stored and passed to the callback function. |
| 83 | */ |
| 84 | void addCleanUp(PFCleanUpFunc cleanUp, void* info) { |
| 85 | CleanUpData* entry = fCleanUpData.push(); |
| 86 | |
| 87 | entry->fFunc = cleanUp; |
| 88 | entry->fInfo = info; |
| 89 | } |
| 90 | |
| 91 | /** |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 92 | * Abandons all GPU resources and assumes the underlying backend 3D API |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 93 | * context is not longer usable. Call this if you have lost the associated |
| 94 | * GPU context, and thus internal texture, buffer, etc. references/IDs are |
| 95 | * now invalid. Should be called even when GrContext is no longer going to |
| 96 | * be used for two reasons: |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 97 | * 1) ~GrContext will not try to free the objects in the 3D API. |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 98 | * 2) Any GrGpuResources created by this GrContext that outlive |
| 99 | * will be marked as invalid (GrGpuResource::wasDestroyed()) and |
| 100 | * when they're destroyed no 3D API calls will be made. |
| 101 | * Content drawn since the last GrContext::flush() may be lost. After this |
| 102 | * function is called the only valid action on the GrContext or |
| 103 | * GrGpuResources it created is to destroy them. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 104 | */ |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 105 | void abandonContext(); |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 106 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 107 | /////////////////////////////////////////////////////////////////////////// |
| 108 | // Resource Cache |
| 109 | |
| 110 | /** |
| 111 | * Return the current GPU resource cache limits. |
| 112 | * |
| 113 | * @param maxResources If non-null, returns maximum number of resources that |
| 114 | * can be held in the cache. |
| 115 | * @param maxResourceBytes If non-null, returns maximum number of bytes of |
| 116 | * video memory that can be held in the cache. |
| 117 | */ |
| 118 | void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Gets the current GPU resource cache usage. |
| 122 | * |
| 123 | * @param resourceCount If non-null, returns the number of resources that are held in the |
| 124 | * cache. |
| 125 | * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held |
| 126 | * in the cache. |
| 127 | */ |
| 128 | void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const; |
| 129 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 130 | /** |
| 131 | * Specify the GPU resource cache limits. If the current cache exceeds either |
| 132 | * of these, it will be purged (LRU) to keep the cache within these limits. |
| 133 | * |
| 134 | * @param maxResources The maximum number of resources that can be held in |
| 135 | * the cache. |
| 136 | * @param maxResourceBytes The maximum number of bytes of video memory |
| 137 | * that can be held in the cache. |
| 138 | */ |
| 139 | void setResourceCacheLimits(int maxResources, size_t maxResourceBytes); |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 140 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 141 | GrTextureProvider* textureProvider() { return fTextureProvider; } |
| 142 | const GrTextureProvider* textureProvider() const { return fTextureProvider; } |
| 143 | |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 144 | /** |
bsalomon@google.com | 1e269b5 | 2012-10-15 14:25:31 +0000 | [diff] [blame] | 145 | * 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] | 146 | * pressure. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 147 | */ |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 148 | void freeGpuResources(); |
| 149 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 150 | /** |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 151 | * Purge all the unlocked resources from the cache. |
| 152 | * This entry point is mainly meant for timing texture uploads |
| 153 | * and is not defined in normal builds of Skia. |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 154 | */ |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 155 | void purgeAllUnlockedResources(); |
| 156 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 157 | /** Access the context capabilities */ |
| 158 | const GrCaps* caps() const { return fCaps; } |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 159 | |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Returns the recommended sample count for a render target when using this |
| 162 | * context. |
| 163 | * |
| 164 | * @param config the configuration of the render target. |
| 165 | * @param dpi the display density in dots per inch. |
| 166 | * |
| 167 | * @return sample count that should be perform well and have good enough |
| 168 | * rendering quality for the display. Alternatively returns 0 if |
| 169 | * MSAA is not supported or recommended to be used by default. |
| 170 | */ |
| 171 | int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const; |
| 172 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 173 | /** |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 174 | * Returns a helper object to orchestrate draws. |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 175 | * Callers should take a ref if they rely on the GrDrawContext sticking around. |
| 176 | * NULL will be returned if the context has been abandoned. |
| 177 | * |
robertphillips | 31dc1b2 | 2015-07-07 10:05:18 -0700 | [diff] [blame] | 178 | * @param surfaceProps the surface properties (mainly defines text drawing) |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 179 | * |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 180 | * @return a draw context |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 181 | */ |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 182 | GrDrawContext* drawContext(const SkSurfaceProps* surfaceProps = NULL) { |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 183 | return fDrawingMgr.drawContext(surfaceProps); |
joshualitt | 16b2789 | 2014-12-18 07:47:16 -0800 | [diff] [blame] | 184 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 185 | |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 186 | GrTextContext* textContext(const SkSurfaceProps& surfaceProps, GrRenderTarget* rt) { |
| 187 | return fDrawingMgr.textContext(surfaceProps, rt); |
| 188 | } |
| 189 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 190 | /////////////////////////////////////////////////////////////////////////// |
| 191 | // Misc. |
| 192 | |
| 193 | /** |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 194 | * Flags that affect flush() behavior. |
| 195 | */ |
| 196 | enum FlushBits { |
| 197 | /** |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 198 | * A client may reach a point where it has partially rendered a frame |
| 199 | * through a GrContext that it knows the user will never see. This flag |
| 200 | * causes the flush to skip submission of deferred content to the 3D API |
| 201 | * during the flush. |
| 202 | */ |
| 203 | kDiscard_FlushBit = 0x2, |
| 204 | }; |
| 205 | |
| 206 | /** |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 207 | * Call to ensure all drawing to the context has been issued to the |
| 208 | * underlying 3D API. |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 209 | * @param flagsBitfield flags that control the flushing behavior. See |
| 210 | * FlushBits. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 211 | */ |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 212 | void flush(int flagsBitfield = 0); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 213 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 214 | void flushIfNecessary() { |
| 215 | if (fFlushToReduceCacheSize) { |
| 216 | this->flush(); |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 217 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 218 | } |
| 219 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 220 | /** |
| 221 | * These flags can be used with the read/write pixels functions below. |
| 222 | */ |
| 223 | enum PixelOpsFlags { |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 224 | /** The GrContext will not be flushed before the surface read or write. This means that |
| 225 | the read or write may occur before previous draws have executed. */ |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 226 | kDontFlush_PixelOpsFlag = 0x1, |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 227 | /** Any surface writes should be flushed to the backend 3D API after the surface operation |
| 228 | is complete */ |
| 229 | kFlushWrites_PixelOp = 0x2, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 230 | /** The src for write or dst read is unpremultiplied. This is only respected if both the |
| 231 | config src and dst configs are an RGBA/BGRA 8888 format. */ |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 232 | kUnpremul_PixelOpsFlag = 0x4, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 235 | /** |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 236 | * Reads a rectangle of pixels from a surface. |
| 237 | * @param surface the surface to read from. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 238 | * @param left left edge of the rectangle to read (inclusive) |
| 239 | * @param top top edge of the rectangle to read (inclusive) |
| 240 | * @param width width of rectangle to read in pixels. |
| 241 | * @param height height of rectangle to read in pixels. |
| 242 | * @param config the pixel config of the destination buffer |
| 243 | * @param buffer memory to read the rectangle into. |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 244 | * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly |
| 245 | * packed. |
| 246 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 247 | * |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 248 | * @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] | 249 | * pixel configs |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 250 | */ |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 251 | bool readSurfacePixels(GrSurface* surface, |
| 252 | int left, int top, int width, int height, |
| 253 | GrPixelConfig config, void* buffer, |
| 254 | size_t rowBytes = 0, |
| 255 | uint32_t pixelOpsFlags = 0); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 256 | |
| 257 | /** |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 258 | * Writes a rectangle of pixels to a surface. |
| 259 | * @param surface the surface to write to. |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 260 | * @param left left edge of the rectangle to write (inclusive) |
| 261 | * @param top top edge of the rectangle to write (inclusive) |
| 262 | * @param width width of rectangle to write in pixels. |
| 263 | * @param height height of rectangle to write in pixels. |
| 264 | * @param config the pixel config of the source buffer |
| 265 | * @param buffer memory to read pixels from |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 266 | * @param rowBytes number of bytes between consecutive rows. Zero |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 267 | * means rows are tightly packed. |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 268 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 269 | * @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] | 270 | * unsupported combination of surface and src configs. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 271 | */ |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 272 | bool writeSurfacePixels(GrSurface* surface, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 273 | int left, int top, int width, int height, |
| 274 | GrPixelConfig config, const void* buffer, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 275 | size_t rowBytes, |
| 276 | uint32_t pixelOpsFlags = 0); |
| 277 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 278 | /** |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 279 | * Copies a rectangle of texels from src to dst. |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 280 | * bounds. |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 281 | * @param dst the surface to copy to. |
| 282 | * @param src the surface to copy from. |
| 283 | * @param srcRect the rectangle of the src that should be copied. |
| 284 | * @param dstPoint the translation applied when writing the srcRect's pixels to the dst. |
| 285 | * @param pixelOpsFlags see PixelOpsFlags enum above. (kUnpremul_PixelOpsFlag is not allowed). |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 286 | */ |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 287 | void copySurface(GrSurface* dst, |
| 288 | GrSurface* src, |
| 289 | const SkIRect& srcRect, |
| 290 | const SkIPoint& dstPoint, |
| 291 | uint32_t pixelOpsFlags = 0); |
| 292 | |
| 293 | /** Helper that copies the whole surface but fails when the two surfaces are not identically |
| 294 | sized. */ |
| 295 | bool copySurface(GrSurface* dst, GrSurface* src) { |
| 296 | if (NULL == dst || NULL == src || dst->width() != src->width() || |
| 297 | dst->height() != src->height()) { |
| 298 | return false; |
| 299 | } |
| 300 | this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()), |
| 301 | SkIPoint::Make(0,0)); |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * After this returns any pending writes to the surface will have been issued to the backend 3D API. |
| 307 | */ |
| 308 | void flushSurfaceWrites(GrSurface* surface); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 309 | |
| 310 | /** |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 311 | * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve |
| 312 | * if necessary. |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 313 | * |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 314 | * 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.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 317 | */ |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 318 | void prepareSurfaceForExternalIO(GrSurface*); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 319 | |
| 320 | /** |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 321 | * An ID associated with this context, guaranteed to be unique. |
| 322 | */ |
| 323 | uint32_t uniqueID() { return fUniqueID; } |
| 324 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 325 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 326 | // Functions intended for internal use only. |
| 327 | GrGpu* getGpu() { return fGpu; } |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 328 | const GrGpu* getGpu() const { return fGpu; } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 329 | GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 330 | GrLayerCache* getLayerCache() { return fLayerCache.get(); } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 331 | GrTextBlobCache* getTextBlobCache() { return fTextBlobCache; } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 332 | bool abandoned() const { return fDrawingMgr.abandoned(); } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 333 | GrResourceProvider* resourceProvider() { return fResourceProvider; } |
| 334 | const GrResourceProvider* resourceProvider() const { return fResourceProvider; } |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 335 | GrResourceCache* getResourceCache() { return fResourceCache; } |
bsalomon@google.com | 9923c2b | 2012-06-06 18:21:18 +0000 | [diff] [blame] | 336 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 337 | // Called by tests that draw directly to the context via GrDrawTarget |
| 338 | void getTestTarget(GrTestTarget*); |
| 339 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 340 | GrPathRenderer* getPathRenderer( |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 341 | const GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 342 | const GrPipelineBuilder*, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 343 | const SkMatrix& viewMatrix, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 344 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 345 | const GrStrokeInfo& stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 346 | bool allowSW, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 347 | GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 348 | GrPathRendererChain::StencilSupport* stencilSupport = NULL); |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 349 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 350 | /** Prints cache stats to the string if GR_CACHE_STATS == 1. */ |
| 351 | void dumpCacheStats(SkString*) const; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 352 | void printCacheStats() const; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 353 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 354 | /** Prints GPU stats to the string if GR_GPU_STATS == 1. */ |
| 355 | void dumpGpuStats(SkString*) const; |
| 356 | void printGpuStats() const; |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 357 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 358 | /** Specify the TextBlob cache limit. If the current cache exceeds this limit it will purge. |
| 359 | this is for testing only */ |
| 360 | void setTextBlobCacheLimit_ForTesting(size_t bytes); |
| 361 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 362 | /** Specify the sizes of the GrAtlasTextContext atlases. The configs pointer below should be |
| 363 | to an array of 3 entries */ |
| 364 | void setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs); |
| 365 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 366 | /** Enumerates all cached GPU resources and dumps their memory to traceMemoryDump. */ |
| 367 | void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 368 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 369 | private: |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 370 | GrGpu* fGpu; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 371 | const GrCaps* fCaps; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 372 | GrResourceCache* fResourceCache; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 373 | // this union exists because the inheritance of GrTextureProvider->GrResourceProvider |
| 374 | // is in a private header. |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 375 | union { |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 376 | GrResourceProvider* fResourceProvider; |
| 377 | GrTextureProvider* fTextureProvider; |
| 378 | }; |
| 379 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 380 | GrBatchFontCache* fBatchFontCache; |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 381 | SkAutoTDelete<GrLayerCache> fLayerCache; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 382 | SkAutoTDelete<GrTextBlobCache> fTextBlobCache; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 383 | |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 384 | GrPathRendererChain* fPathRendererChain; |
| 385 | GrSoftwarePathRenderer* fSoftwarePathRenderer; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 386 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 387 | // Set by OverbudgetCB() to request that GrContext flush before exiting a draw. |
| 388 | bool fFlushToReduceCacheSize; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 389 | bool fDidTestPMConversions; |
| 390 | int fPMToUPMConversion; |
| 391 | int fUPMToPMConversion; |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 392 | // The sw backend may call GrContext::readSurfacePixels on multiple threads |
| 393 | // We may transfer the responsibilty for using a mutex to the sw backend |
| 394 | // when there are fewer code paths that lead to a readSurfacePixels call |
| 395 | // from the sw backend. readSurfacePixels is reentrant in one case - when performing |
| 396 | // the PM conversions test. To handle this we do the PM conversions test outside |
| 397 | // of fReadPixelsMutex and use a separate mutex to guard it. When it re-enters |
| 398 | // readSurfacePixels it will grab fReadPixelsMutex and release it before the outer |
| 399 | // readSurfacePixels proceeds to grab it. |
| 400 | // TODO: Stop pretending to make GrContext thread-safe for sw rasterization and provide |
| 401 | // a mechanism to make a SkPicture safe for multithreaded sw rasterization. |
| 402 | SkMutex fReadPixelsMutex; |
| 403 | SkMutex fTestPMConversionsMutex; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 404 | |
robertphillips@google.com | cdb426d | 2012-09-24 19:33:59 +0000 | [diff] [blame] | 405 | struct CleanUpData { |
| 406 | PFCleanUpFunc fFunc; |
| 407 | void* fInfo; |
| 408 | }; |
| 409 | |
robertphillips@google.com | 44a91dc | 2013-07-25 15:32:06 +0000 | [diff] [blame] | 410 | SkTDArray<CleanUpData> fCleanUpData; |
| 411 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 412 | const uint32_t fUniqueID; |
krajcevski | 9c6d4d7 | 2014-08-12 07:26:25 -0700 | [diff] [blame] | 413 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 414 | GrContext(); // init must be called after the constructor. |
| 415 | bool init(GrBackend, GrBackendContext, const GrContextOptions& options); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 416 | |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 417 | // Currently the DrawingMgr creates a separate GrTextContext for each |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 418 | // combination of text drawing options (pixel geometry x DFT use) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 419 | // and hands the appropriate one back given the DrawContext's request. |
| 420 | // |
| 421 | // It allocates a new GrDrawContext for each GrRenderTarget |
| 422 | // but all of them still land in the same GrDrawTarget! |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 423 | // |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 424 | // In the future this class will allocate a new GrDrawContext for |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 425 | // each GrRenderTarget/GrDrawTarget and manage the DAG. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 426 | class DrawingMgr { |
| 427 | public: |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 428 | DrawingMgr() : fDrawTarget(nullptr), fNVPRTextContext(nullptr) { |
| 429 | sk_bzero(fTextContexts, sizeof(fTextContexts)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | ~DrawingMgr(); |
| 433 | |
| 434 | void init(GrContext* context); |
| 435 | |
| 436 | void abandon(); |
| 437 | bool abandoned() const { return NULL == fDrawTarget; } |
| 438 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 439 | void reset(); |
| 440 | void flush(); |
| 441 | |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 442 | // Callers assume the creation ref of the drawContext! |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 443 | // NULL will be returned if the context has been abandoned. |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 444 | GrDrawContext* drawContext(const SkSurfaceProps* surfaceProps); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 445 | |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 446 | GrTextContext* textContext(const SkSurfaceProps& props, GrRenderTarget* rt); |
| 447 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 448 | private: |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 449 | void cleanup(); |
| 450 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 451 | friend class GrContext; // for access to fDrawTarget for testing |
| 452 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 453 | static const int kNumPixelGeometries = 5; // The different pixel geometries |
| 454 | static const int kNumDFTOptions = 2; // DFT or no DFT |
| 455 | |
| 456 | GrContext* fContext; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 457 | GrDrawTarget* fDrawTarget; |
| 458 | |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 459 | GrTextContext* fNVPRTextContext; |
| 460 | GrTextContext* fTextContexts[kNumPixelGeometries][kNumDFTOptions]; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | DrawingMgr fDrawingMgr; |
| 464 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 465 | void initMockContext(); |
| 466 | void initCommon(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 467 | |
skia.committer@gmail.com | cdcb2ce | 2013-01-29 07:05:52 +0000 | [diff] [blame] | 468 | /** |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 469 | * These functions create premul <-> unpremul effects if it is possible to generate a pair |
| 470 | * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they |
| 471 | * return NULL. |
| 472 | */ |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 473 | const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, bool swapRAndB, |
| 474 | const SkMatrix&) const; |
| 475 | const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, bool swapRAndB, |
| 476 | const SkMatrix&) const; |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 477 | /** Called before either of the above two functions to determine the appropriate fragment |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 478 | processors for conversions. This must be called by readSurfacePixels before a mutex is |
| 479 | taken, since testingvPM conversions itself will call readSurfacePixels */ |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 480 | void testPMConversionsIfNecessary(uint32_t flags); |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 481 | /** Returns true if we've already determined that createPMtoUPMEffect and createUPMToPMEffect |
| 482 | will fail. In such cases fall back to SW conversion. */ |
| 483 | bool didFailPMUPMConversionTest() const; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 484 | |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 485 | /** |
| 486 | * This callback allows the resource cache to callback into the GrContext |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 487 | * when the cache is still over budget after a purge. |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 488 | */ |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 489 | static void OverBudgetCB(void* data); |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 490 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 491 | /** |
| 492 | * A callback similar to the above for use by the TextBlobCache |
| 493 | * TODO move textblob draw calls below context so we can use the call above. |
| 494 | */ |
| 495 | static void TextBlobCacheOverBudgetCB(void* data); |
| 496 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 497 | typedef SkRefCnt INHERITED; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 498 | }; |
| 499 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 500 | #endif |