Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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. |
| 6 | */ |
| 7 | |
| 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkDeferredDisplayListRecorder.h" |
| 10 | #include "include/core/SkSurfaceCharacterization.h" |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 11 | #include "include/private/SkMalloc.h" |
Robert Phillips | 3ac83b2f | 2020-10-26 13:50:57 -0400 | [diff] [blame] | 12 | #include "include/utils/SkRandom.h" |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 13 | #include "src/core/SkCanvasPriv.h" |
Robert Phillips | 83c38a8 | 2020-10-28 14:57:53 -0400 | [diff] [blame] | 14 | #include "src/core/SkMessageBus.h" |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrDirectContextPriv.h" |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrGpu.h" |
| 18 | #include "src/gpu/GrMemoryPool.h" |
| 19 | #include "src/gpu/GrOpFlushState.h" |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrProxyProvider.h" |
| 21 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrResourceProvider.h" |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrStyle.h" |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrThreadSafeCache.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 25 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 26 | #include "tests/Test.h" |
| 27 | #include "tests/TestUtils.h" |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 28 | #include "tools/gpu/ProxyUtils.h" |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 29 | |
Robert Phillips | c2fe164 | 2020-09-22 17:34:51 -0400 | [diff] [blame] | 30 | #include <thread> |
| 31 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 32 | static constexpr int kImageWH = 32; |
| 33 | static constexpr auto kImageOrigin = kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 34 | static constexpr int kNoID = -1; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 35 | |
| 36 | static SkImageInfo default_ii(int wh) { |
| 37 | return SkImageInfo::Make(wh, wh, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 38 | } |
| 39 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 40 | static std::unique_ptr<skgpu::v1::SurfaceDrawContext> new_SDC(GrRecordingContext* rContext, |
| 41 | int wh) { |
| 42 | return skgpu::v1::SurfaceDrawContext::Make(rContext, |
| 43 | GrColorType::kRGBA_8888, |
| 44 | nullptr, |
| 45 | SkBackingFit::kExact, |
| 46 | {wh, wh}, |
| 47 | SkSurfaceProps(), |
| 48 | 1, |
| 49 | GrMipMapped::kNo, |
| 50 | GrProtected::kNo, |
| 51 | kImageOrigin, |
| 52 | SkBudgeted::kYes); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 55 | static void create_view_key(GrUniqueKey* key, int wh, int id) { |
| 56 | static const GrUniqueKey::Domain kViewDomain = GrUniqueKey::GenerateDomain(); |
| 57 | GrUniqueKey::Builder builder(key, kViewDomain, 1); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 58 | builder[0] = wh; |
| 59 | builder.finish(); |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 60 | |
| 61 | if (id != kNoID) { |
| 62 | key->setCustomData(SkData::MakeWithCopy(&id, sizeof(id))); |
| 63 | } |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 64 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 65 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 66 | static void create_vert_key(GrUniqueKey* key, int wh, int id) { |
| 67 | static const GrUniqueKey::Domain kVertDomain = GrUniqueKey::GenerateDomain(); |
| 68 | GrUniqueKey::Builder builder(key, kVertDomain, 1); |
| 69 | builder[0] = wh; |
| 70 | builder.finish(); |
| 71 | |
| 72 | if (id != kNoID) { |
| 73 | key->setCustomData(SkData::MakeWithCopy(&id, sizeof(id))); |
| 74 | } |
| 75 | } |
| 76 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 77 | static bool default_is_newer_better(SkData* incumbent, SkData* challenger) { |
| 78 | return false; |
| 79 | } |
| 80 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 81 | // When testing views we create a bitmap that covers the entire screen and has an inset blue rect |
| 82 | // atop a field of white. |
| 83 | // When testing verts we clear the background to white and simply draw an inset blur rect. |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 84 | static SkBitmap create_bitmap(int wh) { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 85 | SkBitmap bitmap; |
| 86 | |
| 87 | bitmap.allocPixels(default_ii(wh)); |
| 88 | |
| 89 | SkCanvas tmp(bitmap); |
| 90 | tmp.clear(SK_ColorWHITE); |
| 91 | |
| 92 | SkPaint blue; |
| 93 | blue.setColor(SK_ColorBLUE); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 94 | blue.setAntiAlias(false); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 95 | |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 96 | tmp.drawRect({10, 10, wh-10.0f, wh-10.0f}, blue); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 97 | |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 98 | bitmap.setImmutable(); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 99 | return bitmap; |
| 100 | } |
| 101 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 102 | class GrThreadSafeVertexTestOp; |
| 103 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 104 | class TestHelper { |
| 105 | public: |
| 106 | struct Stats { |
| 107 | int fCacheHits = 0; |
| 108 | int fCacheMisses = 0; |
| 109 | |
| 110 | int fNumSWCreations = 0; |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 111 | int fNumLazyCreations = 0; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 112 | int fNumHWCreations = 0; |
| 113 | }; |
| 114 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 115 | TestHelper(GrDirectContext* dContext, |
| 116 | GrThreadSafeCache::IsNewerBetter isNewerBetter = default_is_newer_better) |
| 117 | : fDContext(dContext) |
| 118 | , fIsNewerBetter(isNewerBetter) { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 119 | |
| 120 | fDst = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, default_ii(kImageWH)); |
| 121 | SkAssertResult(fDst); |
| 122 | |
| 123 | SkSurfaceCharacterization characterization; |
| 124 | SkAssertResult(fDst->characterize(&characterization)); |
| 125 | |
| 126 | fRecorder1 = std::make_unique<SkDeferredDisplayListRecorder>(characterization); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 127 | this->ddlCanvas1()->clear(SkColors::kWhite); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 128 | |
| 129 | fRecorder2 = std::make_unique<SkDeferredDisplayListRecorder>(characterization); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 130 | this->ddlCanvas2()->clear(SkColors::kWhite); |
| 131 | |
| 132 | fDst->getCanvas()->clear(SkColors::kWhite); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | ~TestHelper() { |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 136 | fDContext->flush(); |
| 137 | fDContext->submit(true); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Stats* stats() { return &fStats; } |
| 141 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 142 | int numCacheEntries() const { return this->threadSafeCache()->numEntries(); } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 143 | |
| 144 | GrDirectContext* dContext() { return fDContext; } |
| 145 | |
| 146 | SkCanvas* liveCanvas() { return fDst ? fDst->getCanvas() : nullptr; } |
| 147 | SkCanvas* ddlCanvas1() { return fRecorder1 ? fRecorder1->getCanvas() : nullptr; } |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 148 | sk_sp<SkDeferredDisplayList> snap1() { |
| 149 | if (fRecorder1) { |
| 150 | sk_sp<SkDeferredDisplayList> tmp = fRecorder1->detach(); |
| 151 | fRecorder1 = nullptr; |
| 152 | return tmp; |
| 153 | } |
| 154 | |
| 155 | return nullptr; |
| 156 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 157 | SkCanvas* ddlCanvas2() { return fRecorder2 ? fRecorder2->getCanvas() : nullptr; } |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 158 | sk_sp<SkDeferredDisplayList> snap2() { |
| 159 | if (fRecorder2) { |
| 160 | sk_sp<SkDeferredDisplayList> tmp = fRecorder2->detach(); |
| 161 | fRecorder2 = nullptr; |
| 162 | return tmp; |
| 163 | } |
| 164 | |
| 165 | return nullptr; |
| 166 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 167 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 168 | GrThreadSafeCache* threadSafeCache() { return fDContext->priv().threadSafeCache(); } |
| 169 | const GrThreadSafeCache* threadSafeCache() const { return fDContext->priv().threadSafeCache(); } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 170 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 171 | typedef void (TestHelper::*addAccessFP)(SkCanvas*, int wh, int id, |
| 172 | bool failLookUp, bool failFillingIn); |
| 173 | typedef bool (TestHelper::*checkFP)(SkCanvas*, int wh, |
| 174 | int expectedHits, int expectedMisses, |
| 175 | int expectedNumRefs, int expectedID); |
| 176 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 177 | // Add a draw on 'canvas' that will introduce a ref on the 'wh' view |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 178 | void addViewAccess(SkCanvas* canvas, |
| 179 | int wh, |
| 180 | int id = kNoID, |
| 181 | bool failLookup = false, |
| 182 | bool failFillingIn = false) { |
| 183 | auto rContext = canvas->recordingContext(); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 184 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 185 | auto view = AccessCachedView(rContext, this->threadSafeCache(), |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 186 | wh, failLookup, failFillingIn, id, &fStats); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 187 | SkASSERT(view); |
| 188 | |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 189 | auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 190 | |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 191 | sdc->drawTexture(nullptr, |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 192 | view, |
| 193 | kPremul_SkAlphaType, |
| 194 | GrSamplerState::Filter::kNearest, |
| 195 | GrSamplerState::MipmapMode::kNone, |
| 196 | SkBlendMode::kSrcOver, |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 197 | {1.0f, 1.0f, 1.0f, 1.0f}, |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 198 | SkRect::MakeWH(wh, wh), |
| 199 | SkRect::MakeWH(wh, wh), |
| 200 | GrAA::kNo, |
| 201 | GrQuadAAFlags::kNone, |
| 202 | SkCanvas::kFast_SrcRectConstraint, |
| 203 | SkMatrix::I(), |
| 204 | nullptr); |
| 205 | } |
| 206 | |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 207 | // Besides checking that the number of refs and cache hits and misses are as expected, this |
| 208 | // method also validates that the unique key doesn't appear in any of the other caches. |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 209 | bool checkView(SkCanvas* canvas, int wh, |
| 210 | int expectedHits, int expectedMisses, int expectedNumRefs, int expectedID) { |
| 211 | if (fStats.fCacheHits != expectedHits || fStats.fCacheMisses != expectedMisses) { |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 212 | SkDebugf("Hits E: %d A: %d --- Misses E: %d A: %d\n", |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 213 | expectedHits, fStats.fCacheHits, expectedMisses, fStats.fCacheMisses); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | |
| 217 | GrUniqueKey key; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 218 | create_view_key(&key, wh, kNoID); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 219 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 220 | auto threadSafeCache = this->threadSafeCache(); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 221 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 222 | auto [view, xtraData] = threadSafeCache->findWithData(key); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 223 | if (!view.proxy()) { |
| 224 | return false; |
| 225 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 226 | |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 227 | if (expectedID < 0) { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 228 | if (xtraData) { |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | } else { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 232 | if (!xtraData) { |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 236 | const int* cachedID = static_cast<const int*>(xtraData->data()); |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 237 | if (*cachedID != expectedID) { |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 242 | if (!view.proxy()->refCntGreaterThan(expectedNumRefs+1) || // +1 for 'view's ref |
| 243 | view.proxy()->refCntGreaterThan(expectedNumRefs+2)) { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 247 | if (canvas) { |
| 248 | GrRecordingContext* rContext = canvas->recordingContext(); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 249 | GrProxyProvider* recordingProxyProvider = rContext->priv().proxyProvider(); |
| 250 | sk_sp<GrTextureProxy> result = recordingProxyProvider->findProxyByUniqueKey(key); |
| 251 | if (result) { |
| 252 | // views in this cache should never appear in the recorder's cache |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | { |
| 258 | GrProxyProvider* directProxyProvider = fDContext->priv().proxyProvider(); |
| 259 | sk_sp<GrTextureProxy> result = directProxyProvider->findProxyByUniqueKey(key); |
| 260 | if (result) { |
| 261 | // views in this cache should never appear in the main proxy cache |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | { |
| 267 | auto resourceProvider = fDContext->priv().resourceProvider(); |
| 268 | sk_sp<GrSurface> surf = resourceProvider->findByUniqueKey<GrSurface>(key); |
| 269 | if (surf) { |
| 270 | // the textures backing the views in this cache should never be discoverable in the |
| 271 | // resource cache |
| 272 | return false; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 279 | void addVertAccess(SkCanvas* canvas, |
| 280 | int wh, |
| 281 | int id, |
| 282 | bool failLookup, |
| 283 | bool failFillingIn, |
| 284 | GrThreadSafeVertexTestOp** createdOp); |
| 285 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 286 | // Add a draw on 'canvas' that will introduce a ref on a 'wh' vertex data |
| 287 | void addVertAccess(SkCanvas* canvas, |
| 288 | int wh, |
| 289 | int id = kNoID, |
| 290 | bool failLookup = false, |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 291 | bool failFillingIn = false) { |
| 292 | this->addVertAccess(canvas, wh, id, failLookup, failFillingIn, nullptr); |
| 293 | } |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 294 | |
| 295 | bool checkVert(SkCanvas* canvas, int wh, |
| 296 | int expectedHits, int expectedMisses, int expectedNumRefs, int expectedID) { |
| 297 | if (fStats.fCacheHits != expectedHits || fStats.fCacheMisses != expectedMisses) { |
| 298 | SkDebugf("Hits E: %d A: %d --- Misses E: %d A: %d\n", |
| 299 | expectedHits, fStats.fCacheHits, expectedMisses, fStats.fCacheMisses); |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | GrUniqueKey key; |
| 304 | create_vert_key(&key, wh, kNoID); |
| 305 | |
| 306 | auto threadSafeCache = this->threadSafeCache(); |
| 307 | |
| 308 | auto [vertData, xtraData] = threadSafeCache->findVertsWithData(key); |
| 309 | if (!vertData) { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | if (expectedID < 0) { |
| 314 | if (xtraData) { |
| 315 | return false; |
| 316 | } |
| 317 | } else { |
| 318 | if (!xtraData) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | const int* cachedID = static_cast<const int*>(xtraData->data()); |
| 323 | if (*cachedID != expectedID) { |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (!vertData->refCntGreaterThan(expectedNumRefs+1) || // +1 for 'vertData's ref |
| 329 | vertData->refCntGreaterThan(expectedNumRefs+2)) { |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | { |
| 334 | auto resourceProvider = fDContext->priv().resourceProvider(); |
| 335 | sk_sp<GrGpuBuffer> buffer = resourceProvider->findByUniqueKey<GrGpuBuffer>(key); |
| 336 | if (buffer) { |
| 337 | // the buffer holding the vertex data in this cache should never be discoverable |
| 338 | // in the resource cache |
| 339 | return false; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 346 | bool checkImage(skiatest::Reporter* reporter, sk_sp<SkSurface> s) { |
| 347 | SkBitmap actual; |
| 348 | |
| 349 | actual.allocPixels(default_ii(kImageWH)); |
| 350 | |
| 351 | if (!s->readPixels(actual, 0, 0)) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | SkBitmap expected = create_bitmap(kImageWH); |
| 356 | |
| 357 | const float tols[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 358 | |
| 359 | auto error = std::function<ComparePixmapsErrorReporter>( |
| 360 | [reporter](int x, int y, const float diffs[4]) { |
| 361 | SkASSERT(x >= 0 && y >= 0); |
| 362 | ERRORF(reporter, "mismatch at %d, %d (%f, %f, %f %f)", |
| 363 | x, y, diffs[0], diffs[1], diffs[2], diffs[3]); |
| 364 | }); |
| 365 | |
| 366 | return ComparePixels(expected.pixmap(), actual.pixmap(), tols, error); |
| 367 | } |
| 368 | |
| 369 | bool checkImage(skiatest::Reporter* reporter) { |
| 370 | return this->checkImage(reporter, fDst); |
| 371 | } |
| 372 | |
| 373 | bool checkImage(skiatest::Reporter* reporter, sk_sp<SkDeferredDisplayList> ddl) { |
| 374 | sk_sp<SkSurface> tmp = SkSurface::MakeRenderTarget(fDContext, |
| 375 | SkBudgeted::kNo, |
| 376 | default_ii(kImageWH)); |
| 377 | if (!tmp) { |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | if (!tmp->draw(std::move(ddl))) { |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | return this->checkImage(reporter, std::move(tmp)); |
| 386 | } |
| 387 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 388 | size_t gpuSize(int wh) const { |
| 389 | GrBackendFormat format = fDContext->defaultBackendFormat(kRGBA_8888_SkColorType, |
| 390 | GrRenderable::kNo); |
| 391 | |
Greg Daniel | 0eca74c | 2020-10-01 13:46:00 -0400 | [diff] [blame] | 392 | return GrSurface::ComputeSize(format, {wh, wh}, /*colorSamplesPerPixel=*/1, |
| 393 | GrMipMapped::kNo, /*binSize=*/false); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 394 | } |
| 395 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 396 | private: |
| 397 | static GrSurfaceProxyView AccessCachedView(GrRecordingContext*, |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 398 | GrThreadSafeCache*, |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 399 | int wh, |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 400 | bool failLookup, bool failFillingIn, int id, |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 401 | Stats*); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 402 | static GrSurfaceProxyView CreateViewOnCpu(GrRecordingContext*, int wh, Stats*); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 403 | static bool FillInViewOnGpu(GrDirectContext*, int wh, Stats*, |
| 404 | const GrSurfaceProxyView& lazyView, |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 405 | sk_sp<GrThreadSafeCache::Trampoline>); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 406 | |
| 407 | Stats fStats; |
| 408 | GrDirectContext* fDContext = nullptr; |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 409 | GrThreadSafeCache::IsNewerBetter fIsNewerBetter; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 410 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 411 | sk_sp<SkSurface> fDst; |
| 412 | std::unique_ptr<SkDeferredDisplayListRecorder> fRecorder1; |
| 413 | std::unique_ptr<SkDeferredDisplayListRecorder> fRecorder2; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 414 | }; |
| 415 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 416 | class GrThreadSafeVertexTestOp : public GrDrawOp { |
| 417 | public: |
| 418 | DEFINE_OP_CLASS_ID |
| 419 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 420 | static GrOp::Owner Make(GrRecordingContext* rContext, TestHelper::Stats* stats, |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 421 | int wh, int id, bool failLookup, bool failFillingIn, |
| 422 | GrThreadSafeCache::IsNewerBetter isNewerBetter) { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 423 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 424 | return GrOp::Make<GrThreadSafeVertexTestOp>( |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 425 | rContext, rContext, stats, wh, id, failLookup, failFillingIn, isNewerBetter); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 428 | const GrThreadSafeCache::VertexData* vertexData() const { return fVertexData.get(); } |
| 429 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 430 | private: |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 431 | friend class GrOp; // for ctor |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 432 | |
| 433 | GrThreadSafeVertexTestOp(GrRecordingContext* rContext, TestHelper::Stats* stats, int wh, int id, |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 434 | bool failLookup, bool failFillingIn, |
| 435 | GrThreadSafeCache::IsNewerBetter isNewerBetter) |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 436 | : INHERITED(ClassID()) |
| 437 | , fStats(stats) |
| 438 | , fWH(wh) |
| 439 | , fID(id) |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 440 | , fFailFillingIn(failFillingIn) |
| 441 | , fIsNewerBetter(isNewerBetter) { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 442 | this->setBounds(SkRect::MakeIWH(fWH, fWH), HasAABloat::kNo, IsHairline::kNo); |
| 443 | |
| 444 | // Normally we wouldn't add a ref to the vertex data at this point. However, it is |
| 445 | // needed in this unit test to get the ref counts on the uniquely keyed resources |
| 446 | // to be as expected. |
| 447 | this->findOrCreateVertices(rContext, failLookup, fFailFillingIn); |
| 448 | } |
| 449 | |
| 450 | const char* name() const override { return "GrThreadSafeVertexTestOp"; } |
| 451 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 452 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 453 | return GrProcessorSet::EmptySetAnalysis(); |
| 454 | } |
| 455 | |
| 456 | GrProgramInfo* createProgramInfo(const GrCaps* caps, |
| 457 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 458 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 459 | GrAppliedClip&& appliedClip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 460 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 461 | GrXferBarrierFlags renderPassXferBarriers, |
| 462 | GrLoadOp colorLoadOp) const { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 463 | using namespace GrDefaultGeoProcFactory; |
| 464 | |
| 465 | Color color({ 0.0f, 0.0f, 1.0f, 1.0f }); |
| 466 | |
| 467 | auto gp = MakeForDeviceSpace(arena, color, |
| 468 | Coverage::kSolid_Type, |
| 469 | LocalCoords::kUnused_Type, |
| 470 | SkMatrix::I()); |
| 471 | |
| 472 | return sk_gpu_test::CreateProgramInfo(caps, arena, writeView, |
| 473 | std::move(appliedClip), dstProxyView, |
| 474 | gp, SkBlendMode::kSrcOver, |
| 475 | GrPrimitiveType::kTriangleStrip, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 476 | renderPassXferBarriers, colorLoadOp); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const { |
| 480 | return this->createProgramInfo(&flushState->caps(), |
| 481 | flushState->allocator(), |
| 482 | flushState->writeView(), |
| 483 | flushState->detachAppliedClip(), |
| 484 | flushState->dstProxyView(), |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 485 | flushState->renderPassBarriers(), |
| 486 | flushState->colorLoadOp()); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void findOrCreateVertices(GrRecordingContext* rContext, bool failLookup, bool failFillingIn) { |
| 490 | |
| 491 | if (!fVertexData) { |
| 492 | auto threadSafeViewCache = rContext->priv().threadSafeCache(); |
| 493 | |
| 494 | if (rContext->asDirectContext()) { |
| 495 | // The vertex variant doesn't have a correlate to lazyProxies but increment this |
| 496 | // here to make the unit tests happy. |
| 497 | ++fStats->fNumLazyCreations; |
| 498 | } |
| 499 | |
| 500 | GrUniqueKey key; |
| 501 | create_vert_key(&key, fWH, fID); |
| 502 | |
| 503 | // We can "fail the lookup" to simulate a threaded race condition |
| 504 | auto [cachedVerts, data] = threadSafeViewCache->findVertsWithData(key); |
| 505 | if (cachedVerts && !failLookup) { |
| 506 | fVertexData = cachedVerts; |
| 507 | ++fStats->fCacheHits; |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | ++fStats->fCacheMisses; |
| 512 | if (!rContext->asDirectContext()) { |
| 513 | ++fStats->fNumSWCreations; |
| 514 | } |
| 515 | |
| 516 | constexpr size_t kVertSize = sizeof(SkPoint); |
| 517 | SkPoint* verts = static_cast<SkPoint*>(sk_malloc_throw(4 * kVertSize)); |
| 518 | |
| 519 | verts[0].set(10.0f, 10.0f); |
| 520 | verts[1].set(fWH-10.0f, 10.0f); |
| 521 | verts[2].set(10.0f, fWH-10.0f); |
| 522 | verts[3].set(fWH-10.0f, fWH-10.0f); |
| 523 | |
| 524 | fVertexData = GrThreadSafeCache::MakeVertexData(verts, 4, kVertSize); |
| 525 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 526 | auto [tmpV, tmpD] = threadSafeViewCache->addVertsWithData(key, fVertexData, |
| 527 | fIsNewerBetter); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 528 | if (tmpV != fVertexData) { |
| 529 | // Someone beat us to creating the vertex data. Use that version. |
| 530 | fVertexData = tmpV; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (auto dContext = rContext->asDirectContext(); dContext && !fVertexData->gpuBuffer()) { |
| 535 | auto rp = dContext->priv().resourceProvider(); |
| 536 | |
| 537 | if (!failFillingIn) { |
| 538 | ++fStats->fNumHWCreations; |
| 539 | |
| 540 | sk_sp<GrGpuBuffer> tmp = rp->createBuffer(fVertexData->size(), |
| 541 | GrGpuBufferType::kVertex, |
| 542 | kStatic_GrAccessPattern, |
| 543 | fVertexData->vertices()); |
| 544 | fVertexData->setGpuBuffer(std::move(tmp)); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void onPrePrepare(GrRecordingContext* rContext, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 550 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 551 | GrAppliedClip* clip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 552 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 553 | GrXferBarrierFlags renderPassXferBarriers, |
| 554 | GrLoadOp colorLoadOp) override { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 555 | SkArenaAlloc* arena = rContext->priv().recordTimeAllocator(); |
| 556 | |
| 557 | // This is equivalent to a GrOpFlushState::detachAppliedClip |
| 558 | GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled(); |
| 559 | |
| 560 | fProgramInfo = this->createProgramInfo(rContext->priv().caps(), arena, writeView, |
| 561 | std::move(appliedClip), dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 562 | renderPassXferBarriers, colorLoadOp); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 563 | |
| 564 | rContext->priv().recordProgramInfo(fProgramInfo); |
| 565 | |
| 566 | // This is now a noop (bc it is always called in the ctor) but is where we would normally |
| 567 | // create the vertices. |
| 568 | this->findOrCreateVertices(rContext, false, fFailFillingIn); |
| 569 | } |
| 570 | |
| 571 | void onPrepare(GrOpFlushState* flushState) override { |
| 572 | auto dContext = flushState->gpu()->getContext(); |
| 573 | |
| 574 | // This call site is not a noop bc this op could've been created on with DDL context |
| 575 | // and, therefore, could be lacking a gpu-side buffer |
| 576 | this->findOrCreateVertices(dContext, false, fFailFillingIn); |
| 577 | } |
| 578 | |
| 579 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 580 | if (!fVertexData || !fVertexData->gpuBuffer()) { |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | if (!fProgramInfo) { |
| 585 | fProgramInfo = this->createProgramInfo(flushState); |
| 586 | } |
| 587 | |
| 588 | flushState->bindPipeline(*fProgramInfo, SkRect::MakeIWH(fWH, fWH)); |
| 589 | flushState->bindBuffers(nullptr, nullptr, fVertexData->refGpuBuffer()); |
| 590 | flushState->draw(4, 0); |
| 591 | } |
| 592 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 593 | TestHelper::Stats* fStats; |
| 594 | int fWH; |
| 595 | int fID; |
| 596 | bool fFailFillingIn; |
| 597 | GrThreadSafeCache::IsNewerBetter fIsNewerBetter; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 598 | |
| 599 | sk_sp<GrThreadSafeCache::VertexData> fVertexData; |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 600 | GrProgramInfo* fProgramInfo = nullptr; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 601 | |
| 602 | using INHERITED = GrDrawOp; |
| 603 | }; |
| 604 | |
| 605 | void TestHelper::addVertAccess(SkCanvas* canvas, |
| 606 | int wh, int id, |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 607 | bool failLookup, bool failFillingIn, |
| 608 | GrThreadSafeVertexTestOp** createdOp) { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 609 | auto rContext = canvas->recordingContext(); |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 610 | auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 611 | |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 612 | GrOp::Owner op = GrThreadSafeVertexTestOp::Make(rContext, &fStats, |
| 613 | wh, id, |
| 614 | failLookup, failFillingIn, |
| 615 | fIsNewerBetter); |
| 616 | if (createdOp) { |
| 617 | *createdOp = (GrThreadSafeVertexTestOp*) op.get(); |
| 618 | } |
| 619 | |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 620 | sdc->addDrawOp(std::move(op)); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 621 | } |
| 622 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 623 | GrSurfaceProxyView TestHelper::CreateViewOnCpu(GrRecordingContext* rContext, |
| 624 | int wh, |
| 625 | Stats* stats) { |
| 626 | GrProxyProvider* proxyProvider = rContext->priv().proxyProvider(); |
| 627 | |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 628 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxyFromBitmap(create_bitmap(wh), |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 629 | GrMipmapped::kNo, |
| 630 | SkBackingFit::kExact, |
| 631 | SkBudgeted::kYes); |
| 632 | if (!proxy) { |
| 633 | return {}; |
| 634 | } |
| 635 | |
| 636 | GrSwizzle swizzle = rContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), |
| 637 | GrColorType::kRGBA_8888); |
| 638 | ++stats->fNumSWCreations; |
| 639 | return {std::move(proxy), kImageOrigin, swizzle}; |
| 640 | } |
| 641 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 642 | bool TestHelper::FillInViewOnGpu(GrDirectContext* dContext, int wh, Stats* stats, |
| 643 | const GrSurfaceProxyView& lazyView, |
| 644 | sk_sp<GrThreadSafeCache::Trampoline> trampoline) { |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 645 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 646 | std::unique_ptr<skgpu::v1::SurfaceDrawContext> sdc = new_SDC(dContext, wh); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 647 | |
| 648 | GrPaint paint; |
| 649 | paint.setColor4f({0.0f, 0.0f, 1.0f, 1.0f}); |
| 650 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 651 | sdc->clear(SkPMColor4f{1.0f, 1.0f, 1.0f, 1.0f}); |
| 652 | sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 653 | { 10, 10, wh-10.0f, wh-10.0f }, &GrStyle::SimpleFill()); |
| 654 | |
| 655 | ++stats->fNumHWCreations; |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 656 | auto view = sdc->readSurfaceView(); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 657 | |
| 658 | SkASSERT(view.swizzle() == lazyView.swizzle()); |
| 659 | SkASSERT(view.origin() == lazyView.origin()); |
| 660 | trampoline->fProxy = view.asTextureProxyRef(); |
| 661 | |
| 662 | return true; |
| 663 | } |
| 664 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 665 | GrSurfaceProxyView TestHelper::AccessCachedView(GrRecordingContext* rContext, |
| 666 | GrThreadSafeCache* threadSafeCache, |
| 667 | int wh, |
| 668 | bool failLookup, bool failFillingIn, int id, |
| 669 | Stats* stats) { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 670 | GrUniqueKey key; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 671 | create_view_key(&key, wh, id); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 672 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 673 | if (GrDirectContext* dContext = rContext->asDirectContext()) { |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 674 | // The gpu thread gets priority over the recording threads. If the gpu thread is first, |
| 675 | // it crams a lazy proxy into the cache and then fills it in later. |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 676 | auto [lazyView, trampoline] = GrThreadSafeCache::CreateLazyView( |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 677 | dContext, GrColorType::kRGBA_8888, {wh, wh}, kImageOrigin, SkBackingFit::kExact); |
Robert Phillips | b180712 | 2020-10-06 16:44:18 -0400 | [diff] [blame] | 678 | ++stats->fNumLazyCreations; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 679 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 680 | auto [view, data] = threadSafeCache->findOrAddWithData(key, lazyView); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 681 | if (view != lazyView) { |
| 682 | ++stats->fCacheHits; |
| 683 | return view; |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 684 | } else if (id != kNoID) { |
| 685 | // Make sure, in this case, that the customData stuck |
| 686 | SkASSERT(data); |
| 687 | SkDEBUGCODE(const int* cachedID = static_cast<const int*>(data->data());) |
| 688 | SkASSERT(*cachedID == id); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | ++stats->fCacheMisses; |
| 692 | |
| 693 | if (failFillingIn) { |
| 694 | // Simulate something going horribly wrong at flush-time so no GrTexture is |
| 695 | // available to fulfill the lazy proxy. |
| 696 | return view; |
| 697 | } |
| 698 | |
| 699 | if (!FillInViewOnGpu(dContext, wh, stats, lazyView, std::move(trampoline))) { |
| 700 | // In this case something has gone disastrously wrong so set up to drop the draw |
| 701 | // that needed this resource and reduce future pollution of the cache. |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 702 | threadSafeCache->remove(key); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 703 | return {}; |
| 704 | } |
| 705 | |
| 706 | return view; |
| 707 | } else { |
| 708 | GrSurfaceProxyView view; |
| 709 | |
| 710 | // We can "fail the lookup" to simulate a threaded race condition |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 711 | if (view = threadSafeCache->find(key); !failLookup && view) { |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 712 | ++stats->fCacheHits; |
| 713 | return view; |
| 714 | } |
| 715 | |
| 716 | ++stats->fCacheMisses; |
| 717 | |
| 718 | view = CreateViewOnCpu(rContext, wh, stats); |
| 719 | SkASSERT(view); |
| 720 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 721 | auto [newView, data] = threadSafeCache->addWithData(key, view); |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 722 | if (view == newView && id != kNoID) { |
| 723 | // Make sure, in this case, that the customData stuck |
| 724 | SkASSERT(data); |
| 725 | SkDEBUGCODE(const int* cachedID = static_cast<const int*>(data->data());) |
| 726 | SkASSERT(*cachedID == id); |
| 727 | } |
| 728 | return newView; |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 729 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 730 | } |
| 731 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 732 | // Case 1: ensure two DDL recorders share the view/vertexData |
| 733 | static void test_1(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 734 | TestHelper::addAccessFP addAccess, |
| 735 | TestHelper::checkFP check) { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 736 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 737 | TestHelper helper(dContext); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 738 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 739 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, 1, false, false); |
| 740 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 741 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, /*id*/ 1)); |
| 742 | |
| 743 | (helper.*addAccess)(helper.ddlCanvas2(), kImageWH, 2, false, false); |
| 744 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), kImageWH, |
| 745 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, /*id*/ 1)); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 746 | |
| 747 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 748 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 0); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 749 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 0); |
| 750 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 1); |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 751 | |
| 752 | helper.checkImage(reporter, helper.snap1()); |
| 753 | helper.checkImage(reporter, helper.snap2()); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 754 | } |
| 755 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 756 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache1View, reporter, ctxInfo) { |
| 757 | test_1(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 758 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 759 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 760 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache1Verts, reporter, ctxInfo) { |
| 761 | test_1(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 762 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 763 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 764 | // Case 2: ensure that, if the direct context version wins, its result is reused by the |
| 765 | // DDL recorders |
| 766 | static void test_2(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 767 | TestHelper::addAccessFP addAccess, |
| 768 | TestHelper::checkFP check) { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 769 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 770 | TestHelper helper(dContext); |
| 771 | |
| 772 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, 1, false, false); |
| 773 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 774 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, /*id*/ 1)); |
| 775 | |
| 776 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, 2, false, false); |
| 777 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 778 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, /*id*/ 1)); |
| 779 | |
| 780 | (helper.*addAccess)(helper.ddlCanvas2(), kImageWH, 3, false, false); |
| 781 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), kImageWH, |
| 782 | /*hits*/ 2, /*misses*/ 1, /*refs*/ 3, /*id*/ 1)); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 783 | |
| 784 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 785 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 786 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 1); |
| 787 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 0); |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 788 | |
| 789 | helper.checkImage(reporter); |
| 790 | helper.checkImage(reporter, helper.snap1()); |
| 791 | helper.checkImage(reporter, helper.snap2()); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 792 | } |
| 793 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 794 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache2View, reporter, ctxInfo) { |
| 795 | test_2(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 796 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 797 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 798 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache2Verts, reporter, ctxInfo) { |
| 799 | test_2(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 800 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 801 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 802 | // Case 3: ensure that, if the cpu-version wins, its result is reused by the direct context |
| 803 | static void test_3(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 804 | TestHelper::addAccessFP addAccess, |
| 805 | TestHelper::checkFP check) { |
| 806 | |
| 807 | TestHelper helper(dContext); |
| 808 | |
| 809 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, 1, false, false); |
| 810 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 811 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, /*id*/ 1)); |
| 812 | |
| 813 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, 2, false, false); |
| 814 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 815 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, /*id*/ 1)); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 816 | |
| 817 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 818 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 819 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 0); |
| 820 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 1); |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 821 | |
| 822 | helper.checkImage(reporter); |
| 823 | helper.checkImage(reporter, helper.snap1()); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 824 | } |
| 825 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 826 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache3View, reporter, ctxInfo) { |
| 827 | test_3(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 828 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 829 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 830 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache3Verts, reporter, ctxInfo) { |
| 831 | test_3(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 832 | } |
| 833 | |
| 834 | // Case 4: ensure that, if two DDL recorders get in a race, they still end up sharing a single |
| 835 | // view/vertexData |
| 836 | static void test_4(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 837 | TestHelper::addAccessFP addAccess, |
| 838 | TestHelper::checkFP check) { |
| 839 | |
| 840 | TestHelper helper(dContext); |
| 841 | |
| 842 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, 1, false, false); |
| 843 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 844 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, /*id*/ 1)); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 845 | |
| 846 | static const bool kFailLookup = true; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 847 | (helper.*addAccess)(helper.ddlCanvas2(), kImageWH, 2, kFailLookup, false); |
| 848 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), kImageWH, |
| 849 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 2, /*id*/ 1)); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 850 | |
| 851 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 852 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 0); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 853 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 0); |
| 854 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 2); |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 855 | |
| 856 | helper.checkImage(reporter, helper.snap1()); |
| 857 | helper.checkImage(reporter, helper.snap2()); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 858 | } |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 859 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 860 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache4View, reporter, ctxInfo) { |
| 861 | test_4(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 862 | } |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 863 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 864 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache4Verts, reporter, ctxInfo) { |
| 865 | test_4(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 866 | } |
| 867 | |
| 868 | // Case 4.5: check that, if a live rendering and a DDL recording get into a race, the live |
| 869 | // rendering takes precedence. |
| 870 | static void test_4_5(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 871 | TestHelper::addAccessFP addAccess, |
| 872 | TestHelper::checkFP check) { |
| 873 | |
| 874 | TestHelper helper(dContext); |
| 875 | |
| 876 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, 1, false, false); |
| 877 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 878 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, /*id*/ 1)); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 879 | |
| 880 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 881 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
| 882 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 1); |
| 883 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 0); |
| 884 | |
| 885 | static const bool kFailLookup = true; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 886 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, 2, kFailLookup, false); |
| 887 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 888 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 2, /*id*/ 1)); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 889 | |
| 890 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 891 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
| 892 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 1); |
| 893 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 1); |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 894 | |
| 895 | helper.checkImage(reporter); |
| 896 | helper.checkImage(reporter, helper.snap1()); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 897 | } |
| 898 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 899 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache4_5View, reporter, ctxInfo) { |
| 900 | test_4_5(ctxInfo.directContext(), reporter, |
| 901 | &TestHelper::addViewAccess, &TestHelper::checkView); |
| 902 | } |
| 903 | |
| 904 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache4_5Verts, reporter, ctxInfo) { |
| 905 | test_4_5(ctxInfo.directContext(), reporter, |
| 906 | &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 907 | } |
| 908 | |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 909 | // Case 4.75: check that, if a live rendering fails to generate the content needed to instantiate |
| 910 | // its lazy proxy, life goes on |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 911 | static void test_4_75(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 912 | TestHelper::addAccessFP addAccess, |
| 913 | TestHelper::checkFP check) { |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 914 | |
| 915 | TestHelper helper(dContext); |
| 916 | |
| 917 | static const bool kFailFillingIn = true; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 918 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, kNoID, false, kFailFillingIn); |
| 919 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 920 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 921 | |
| 922 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 923 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
| 924 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 0); |
| 925 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 0); |
| 926 | |
| 927 | dContext->flush(); |
| 928 | dContext->submit(true); |
| 929 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 930 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 931 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 0, kNoID)); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 932 | |
| 933 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 934 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
| 935 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 0); |
| 936 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 0); |
| 937 | } |
| 938 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 939 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache4_75View, reporter, ctxInfo) { |
| 940 | test_4_75(ctxInfo.directContext(), reporter, |
| 941 | &TestHelper::addViewAccess, &TestHelper::checkView); |
| 942 | } |
| 943 | |
| 944 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache4_75Verts, reporter, ctxInfo) { |
| 945 | test_4_75(ctxInfo.directContext(), reporter, |
| 946 | &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 947 | } |
| 948 | |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 949 | // Case 5: ensure that expanding the map works (esp. wrt custom data) |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 950 | static void test_5(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 951 | TestHelper::addAccessFP addAccess, |
| 952 | TestHelper::checkFP check) { |
| 953 | |
| 954 | TestHelper helper(dContext); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 955 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 956 | auto threadSafeCache = helper.threadSafeCache(); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 957 | |
| 958 | int size = 16; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 959 | (helper.*addAccess)(helper.ddlCanvas1(), size, /*id*/ size, false, false); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 960 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 961 | size_t initialSize = threadSafeCache->approxBytesUsedForHash(); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 962 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 963 | while (initialSize == threadSafeCache->approxBytesUsedForHash()) { |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 964 | size *= 2; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 965 | (helper.*addAccess)(helper.ddlCanvas1(), size, /*id*/ size, false, false); |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | for (int i = 16; i <= size; i *= 2) { |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 969 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), |
| 970 | /*wh*/ i, |
| 971 | /*hits*/ 0, |
| 972 | /*misses*/ threadSafeCache->numEntries(), |
| 973 | /*refs*/ 1, |
| 974 | /*id*/ i)); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 978 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache5View, reporter, ctxInfo) { |
| 979 | test_5(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 980 | } |
| 981 | |
| 982 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache5Verts, reporter, ctxInfo) { |
| 983 | test_5(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 984 | } |
| 985 | |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 986 | // Case 6: Check on dropping refs. In particular, that the cache has its own ref to keep |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 987 | // the backing resource alive and locked. |
| 988 | static void test_6(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 989 | TestHelper::addAccessFP addAccess, |
| 990 | TestHelper::checkFP check) { |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 991 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 992 | TestHelper helper(dContext); |
| 993 | |
| 994 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 995 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 996 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, kImageWH, |
| 997 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 998 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 999 | (helper.*addAccess)(helper.ddlCanvas2(), kImageWH, kNoID, false, false); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 1000 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1001 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, kImageWH, |
| 1002 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, kNoID)); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 1003 | |
| 1004 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1005 | |
| 1006 | ddl1 = nullptr; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1007 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, kImageWH, |
| 1008 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 1, kNoID)); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 1009 | |
| 1010 | ddl2 = nullptr; |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1011 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, kImageWH, |
| 1012 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 0, kNoID)); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 1013 | |
| 1014 | // The cache still has its ref |
| 1015 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1016 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1017 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, kImageWH, |
| 1018 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 0, kNoID)); |
| 1019 | } |
| 1020 | |
| 1021 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache6View, reporter, ctxInfo) { |
| 1022 | test_6(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 1023 | } |
| 1024 | |
| 1025 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache6Verts, reporter, ctxInfo) { |
| 1026 | test_6(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 1027 | } |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1028 | |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 1029 | // Case 7: Check that invoking dropAllRefs and dropUniqueRefs directly works as expected; i.e., |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1030 | // dropAllRefs removes everything while dropUniqueRefs is more measured. |
| 1031 | static void test_7(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1032 | TestHelper::addAccessFP addAccess, |
| 1033 | TestHelper::checkFP check) { |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1034 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1035 | TestHelper helper(dContext); |
| 1036 | |
| 1037 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1038 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1039 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, kImageWH, |
| 1040 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1041 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1042 | (helper.*addAccess)(helper.ddlCanvas2(), 2*kImageWH, kNoID, false, false); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1043 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1044 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, 2*kImageWH, |
| 1045 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 1, kNoID)); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1046 | |
| 1047 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 2); |
| 1048 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 1049 | helper.threadSafeCache()->dropUniqueRefs(nullptr); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1050 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 2); |
| 1051 | |
| 1052 | ddl1 = nullptr; |
| 1053 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 1054 | helper.threadSafeCache()->dropUniqueRefs(nullptr); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1055 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1056 | REPORTER_ASSERT(reporter, (helper.*check)(nullptr, 2*kImageWH, |
| 1057 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 1, kNoID)); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1058 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 1059 | helper.threadSafeCache()->dropAllRefs(); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 1060 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 0); |
| 1061 | |
| 1062 | ddl2 = nullptr; |
| 1063 | } |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1064 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1065 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache7View, reporter, ctxInfo) { |
| 1066 | test_7(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 1067 | } |
| 1068 | |
| 1069 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache7Verts, reporter, ctxInfo) { |
| 1070 | test_7(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 1071 | } |
| 1072 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1073 | // Case 8: This checks that GrContext::abandonContext works as expected wrt the thread |
| 1074 | // safe cache. This simulates the case where we have one DDL that has finished |
| 1075 | // recording but one still recording when the abandonContext fires. |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1076 | static void test_8(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1077 | TestHelper::addAccessFP addAccess, |
| 1078 | TestHelper::checkFP check) { |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1079 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1080 | TestHelper helper(dContext); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1081 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1082 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, kNoID, false, false); |
| 1083 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1084 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
| 1085 | |
| 1086 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1087 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1088 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 1089 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1090 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1091 | (helper.*addAccess)(helper.ddlCanvas2(), kImageWH, kNoID, false, false); |
| 1092 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), kImageWH, |
| 1093 | /*hits*/ 2, /*misses*/ 1, /*refs*/ 3, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1094 | |
| 1095 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 1096 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1097 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 1); |
| 1098 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 0); |
| 1099 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1100 | dContext->abandonContext(); // This should exercise dropAllRefs |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1101 | |
| 1102 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
| 1103 | |
| 1104 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 0); |
| 1105 | |
| 1106 | ddl1 = nullptr; |
| 1107 | ddl2 = nullptr; |
| 1108 | } |
| 1109 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1110 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache8View, reporter, ctxInfo) { |
| 1111 | test_8(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 1112 | } |
| 1113 | |
| 1114 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache8Verts, reporter, ctxInfo) { |
| 1115 | test_8(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 1116 | } |
| 1117 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1118 | // Case 9: This checks that GrContext::releaseResourcesAndAbandonContext works as expected wrt |
| 1119 | // the thread safe cache. This simulates the case where we have one DDL that has finished |
| 1120 | // recording but one still recording when the releaseResourcesAndAbandonContext fires. |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1121 | static void test_9(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1122 | TestHelper::addAccessFP addAccess, |
| 1123 | TestHelper::checkFP check) { |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1124 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1125 | TestHelper helper(dContext); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1126 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1127 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, kNoID, false, false); |
| 1128 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1129 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
| 1130 | |
| 1131 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1132 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1133 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 1134 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1135 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1136 | (helper.*addAccess)(helper.ddlCanvas2(), kImageWH, kNoID, false, false); |
| 1137 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), kImageWH, |
| 1138 | /*hits*/ 2, /*misses*/ 1, /*refs*/ 3, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1139 | |
| 1140 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 3380be9 | 2020-09-25 12:47:10 -0400 | [diff] [blame] | 1141 | REPORTER_ASSERT(reporter, helper.stats()->fNumLazyCreations == 1); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1142 | REPORTER_ASSERT(reporter, helper.stats()->fNumHWCreations == 1); |
| 1143 | REPORTER_ASSERT(reporter, helper.stats()->fNumSWCreations == 0); |
| 1144 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1145 | dContext->releaseResourcesAndAbandonContext(); // This should hit dropAllRefs |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1146 | |
| 1147 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
| 1148 | |
| 1149 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 0); |
| 1150 | |
| 1151 | ddl1 = nullptr; |
| 1152 | ddl2 = nullptr; |
| 1153 | } |
| 1154 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1155 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache9View, reporter, ctxInfo) { |
| 1156 | test_9(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 1157 | } |
| 1158 | |
| 1159 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache9Verts, reporter, ctxInfo) { |
| 1160 | test_9(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 1161 | } |
| 1162 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1163 | // Case 10: This checks that the GrContext::purgeUnlockedResources(size_t) variant works as |
| 1164 | // expected wrt the thread safe cache. It, in particular, tests out the MRU behavior |
| 1165 | // of the shared cache. |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1166 | static void test_10(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1167 | TestHelper::addAccessFP addAccess, |
| 1168 | TestHelper::checkFP check) { |
Brian Salomon | b8c4add | 2021-06-28 09:20:44 -0400 | [diff] [blame] | 1169 | |
| 1170 | if (GrBackendApi::kOpenGL != dContext->backend()) { |
| 1171 | // The lower-level backends have too much going on for the following simple purging |
| 1172 | // test to work |
| 1173 | return; |
| 1174 | } |
| 1175 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1176 | TestHelper helper(dContext); |
| 1177 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1178 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, kNoID, false, false); |
| 1179 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1180 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1181 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1182 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1183 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1184 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 1185 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1186 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1187 | (helper.*addAccess)(helper.liveCanvas(), 2*kImageWH, kNoID, false, false); |
| 1188 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1189 | /*hits*/ 1, /*misses*/ 2, /*refs*/ 1, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1190 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1191 | (helper.*addAccess)(helper.ddlCanvas2(), 2*kImageWH, kNoID, false, false); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1192 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1193 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), 2*kImageWH, |
| 1194 | /*hits*/ 2, /*misses*/ 2, /*refs*/ 2, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1195 | |
| 1196 | dContext->flush(); |
| 1197 | dContext->submit(true); |
| 1198 | |
| 1199 | // This should clear out everything but the textures locked in the thread-safe cache |
| 1200 | dContext->purgeUnlockedResources(false); |
| 1201 | |
| 1202 | ddl1 = nullptr; |
| 1203 | ddl2 = nullptr; |
| 1204 | |
| 1205 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 2); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1206 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1207 | /*hits*/ 2, /*misses*/ 2, /*refs*/ 0, kNoID)); |
| 1208 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1209 | /*hits*/ 2, /*misses*/ 2, /*refs*/ 0, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1210 | |
| 1211 | // Regardless of which image is MRU, this should force the other out |
| 1212 | size_t desiredBytes = helper.gpuSize(2*kImageWH) + helper.gpuSize(kImageWH)/2; |
| 1213 | |
| 1214 | auto cache = dContext->priv().getResourceCache(); |
| 1215 | size_t currentBytes = cache->getResourceBytes(); |
| 1216 | |
| 1217 | SkASSERT(currentBytes >= desiredBytes); |
| 1218 | size_t amountToPurge = currentBytes - desiredBytes; |
| 1219 | |
| 1220 | // The 2*kImageWH texture should be MRU. |
| 1221 | dContext->purgeUnlockedResources(amountToPurge, true); |
| 1222 | |
| 1223 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1224 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1225 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1226 | /*hits*/ 2, /*misses*/ 2, /*refs*/ 0, kNoID)); |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 1227 | } |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1228 | |
Brian Salomon | b8c4add | 2021-06-28 09:20:44 -0400 | [diff] [blame] | 1229 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache10View, reporter, ctxInfo) { |
| 1230 | test_10(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | // To enable test_10 with verts would require a bit more work, namely: |
| 1234 | // have a different # of verts based on size |
| 1235 | // also pass in a gpuSize function to 'test_10' |
| 1236 | //DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache10Verts, reporter, ctxInfo) { |
| 1237 | // test_10(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 1238 | //} |
| 1239 | |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1240 | // Case 11: This checks that scratch-only variant of GrContext::purgeUnlockedResources works as |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 1241 | // expected wrt the thread safe cache. In particular, that when 'scratchResourcesOnly' |
| 1242 | // is true, the call has no effect on the cache. |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1243 | static void test_11(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1244 | TestHelper::addAccessFP addAccess, |
| 1245 | TestHelper::checkFP check) { |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1246 | |
| 1247 | TestHelper helper(dContext); |
| 1248 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1249 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, kNoID, false, false); |
| 1250 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1251 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1252 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1253 | (helper.*addAccess)(helper.liveCanvas(), 2*kImageWH, kNoID, false, false); |
| 1254 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1255 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 1, kNoID)); |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1256 | |
| 1257 | dContext->flush(); |
| 1258 | dContext->submit(true); |
| 1259 | |
| 1260 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 2); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1261 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1262 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 0, kNoID)); |
| 1263 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1264 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 0, kNoID)); |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1265 | |
| 1266 | // This shouldn't remove anything from the cache |
| 1267 | dContext->purgeUnlockedResources(/* scratchResourcesOnly */ true); |
| 1268 | |
| 1269 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 2); |
| 1270 | |
| 1271 | dContext->purgeUnlockedResources(/* scratchResourcesOnly */ false); |
| 1272 | |
| 1273 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 0); |
| 1274 | } |
| 1275 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1276 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache11View, reporter, ctxInfo) { |
| 1277 | test_11(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 1278 | } |
| 1279 | |
| 1280 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache11Verts, reporter, ctxInfo) { |
| 1281 | test_11(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 1282 | } |
| 1283 | |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 1284 | // Case 12: Test out purges caused by resetting the cache budget to 0. Note that, due to |
| 1285 | // the how the cache operates (i.e., not directly driven by ref/unrefs) there |
| 1286 | // needs to be an explicit kick to purge the cache. |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1287 | static void test_12(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1288 | TestHelper::addAccessFP addAccess, |
| 1289 | TestHelper::checkFP check) { |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1290 | |
| 1291 | TestHelper helper(dContext); |
| 1292 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1293 | (helper.*addAccess)(helper.liveCanvas(), kImageWH, kNoID, false, false); |
| 1294 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1295 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
| 1296 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1297 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1298 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 1299 | /*hits*/ 1, /*misses*/ 1, /*refs*/ 2, kNoID)); |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1300 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1301 | (helper.*addAccess)(helper.liveCanvas(), 2*kImageWH, kNoID, false, false); |
| 1302 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1303 | /*hits*/ 1, /*misses*/ 2, /*refs*/ 1, kNoID)); |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1304 | |
| 1305 | dContext->flush(); |
| 1306 | dContext->submit(true); |
| 1307 | |
| 1308 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 2); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1309 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), kImageWH, |
| 1310 | /*hits*/ 1, /*misses*/ 2, /*refs*/ 1, kNoID)); |
| 1311 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1312 | /*hits*/ 1, /*misses*/ 2, /*refs*/ 0, kNoID)); |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1313 | |
| 1314 | dContext->setResourceCacheLimit(0); |
| 1315 | |
| 1316 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1317 | |
| 1318 | ddl1 = nullptr; |
| 1319 | |
Robert Phillips | defd223 | 2020-09-25 15:25:46 -0400 | [diff] [blame] | 1320 | // Explicitly kick off the purge - it won't happen automatically on unref |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 1321 | dContext->performDeferredCleanup(std::chrono::milliseconds(0)); |
| 1322 | |
| 1323 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 0); |
| 1324 | } |
Robert Phillips | c2fe164 | 2020-09-22 17:34:51 -0400 | [diff] [blame] | 1325 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1326 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache12View, reporter, ctxInfo) { |
| 1327 | test_12(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 1328 | } |
| 1329 | |
| 1330 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache12Verts, reporter, ctxInfo) { |
| 1331 | test_12(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
| 1332 | } |
| 1333 | |
Robert Phillips | c2fe164 | 2020-09-22 17:34:51 -0400 | [diff] [blame] | 1334 | // Case 13: Test out the 'msNotUsed' parameter to GrContext::performDeferredCleanup. |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1335 | static void test_13(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1336 | TestHelper::addAccessFP addAccess, |
| 1337 | TestHelper::checkFP check) { |
Robert Phillips | c2fe164 | 2020-09-22 17:34:51 -0400 | [diff] [blame] | 1338 | |
| 1339 | TestHelper helper(dContext); |
| 1340 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1341 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
| 1342 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 1343 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
Robert Phillips | c2fe164 | 2020-09-22 17:34:51 -0400 | [diff] [blame] | 1344 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
| 1345 | |
| 1346 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 1347 | auto firstTime = GrStdSteadyClock::now(); |
| 1348 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 1349 | |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1350 | (helper.*addAccess)(helper.ddlCanvas2(), 2*kImageWH, kNoID, false, false); |
| 1351 | |
| 1352 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), 2*kImageWH, |
| 1353 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 1, kNoID)); |
Robert Phillips | c2fe164 | 2020-09-22 17:34:51 -0400 | [diff] [blame] | 1354 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
| 1355 | |
| 1356 | ddl1 = nullptr; |
| 1357 | ddl2 = nullptr; |
| 1358 | |
| 1359 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 2); |
| 1360 | |
| 1361 | auto secondTime = GrStdSteadyClock::now(); |
| 1362 | |
| 1363 | auto msecs = std::chrono::duration_cast<std::chrono::milliseconds>(secondTime - firstTime); |
| 1364 | dContext->performDeferredCleanup(msecs); |
| 1365 | |
| 1366 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
Robert Phillips | 375e1f6 | 2020-10-23 16:13:57 -0400 | [diff] [blame] | 1367 | REPORTER_ASSERT(reporter, (helper.*check)(helper.liveCanvas(), 2*kImageWH, |
| 1368 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 0, kNoID)); |
| 1369 | } |
| 1370 | |
| 1371 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache13View, reporter, ctxInfo) { |
| 1372 | test_13(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView); |
| 1373 | } |
| 1374 | |
| 1375 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache13Verts, reporter, ctxInfo) { |
| 1376 | test_13(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert); |
Robert Phillips | c2fe164 | 2020-09-22 17:34:51 -0400 | [diff] [blame] | 1377 | } |
Robert Phillips | 3ac83b2f | 2020-10-26 13:50:57 -0400 | [diff] [blame] | 1378 | |
| 1379 | // Case 14: Test out mixing & matching view & vertex data w/ recycling of the cache entries to |
| 1380 | // wring out the anonymous union code. This is mainly for the MSAN bot's consumption. |
| 1381 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache14, reporter, ctxInfo) { |
| 1382 | constexpr int kBestPrimeNumber = 73; // palindromic in binary |
| 1383 | SkRandom rand(kBestPrimeNumber); |
| 1384 | |
| 1385 | TestHelper helper(ctxInfo.directContext()); |
| 1386 | |
| 1387 | for (int i = 0; i < 2; ++i) { |
| 1388 | SkCanvas* ddlCanvas = (!i) ? helper.ddlCanvas1() : helper.ddlCanvas2(); |
| 1389 | |
| 1390 | for (int j = 0; j < 10; ++j) { |
| 1391 | int numResources = 10*i + j + 1; |
| 1392 | int wh = numResources; |
| 1393 | |
| 1394 | if (rand.nextBool()) { |
| 1395 | helper.addViewAccess(ddlCanvas, wh, kNoID, false, false); |
| 1396 | REPORTER_ASSERT(reporter, helper.checkView(ddlCanvas, wh, |
| 1397 | /*hits*/ 0, /*misses*/ numResources, |
| 1398 | /*refs*/ 1, kNoID)); |
| 1399 | } else { |
| 1400 | helper.addVertAccess(ddlCanvas, wh, kNoID, false, false); |
| 1401 | REPORTER_ASSERT(reporter, helper.checkVert(ddlCanvas, wh, |
| 1402 | /*hits*/ 0, /*misses*/ numResources, |
| 1403 | /*refs*/ 1, kNoID)); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | if (!i) { |
| 1408 | // Drop all the accumulated resources from the thread-safe cache |
| 1409 | helper.snap1(); |
| 1410 | ctxInfo.directContext()->purgeUnlockedResources(/* scratchResourcesOnly */ false); |
| 1411 | } |
| 1412 | } |
| 1413 | } |
Robert Phillips | 83c38a8 | 2020-10-28 14:57:53 -0400 | [diff] [blame] | 1414 | |
| 1415 | // Case 15: Test out posting invalidation messages that involve the thread safe cache |
| 1416 | static void test_15(GrDirectContext* dContext, skiatest::Reporter* reporter, |
| 1417 | TestHelper::addAccessFP addAccess, |
| 1418 | TestHelper::checkFP check, |
| 1419 | void (*create_key)(GrUniqueKey*, int wh, int id)) { |
| 1420 | |
| 1421 | TestHelper helper(dContext); |
| 1422 | |
| 1423 | (helper.*addAccess)(helper.ddlCanvas1(), kImageWH, kNoID, false, false); |
| 1424 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas1(), kImageWH, |
| 1425 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
| 1426 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
| 1427 | |
| 1428 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1429 | |
| 1430 | GrUniqueKey key; |
| 1431 | (*create_key)(&key, kImageWH, kNoID); |
| 1432 | |
| 1433 | GrUniqueKeyInvalidatedMessage msg(key, dContext->priv().contextID(), |
| 1434 | /* inThreadSafeCache */ true); |
| 1435 | |
Robert Phillips | e7a959d | 2021-03-11 14:44:42 -0500 | [diff] [blame] | 1436 | SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(msg); |
Robert Phillips | 83c38a8 | 2020-10-28 14:57:53 -0400 | [diff] [blame] | 1437 | |
| 1438 | // This purge call is needed to process the invalidation messages |
| 1439 | dContext->purgeUnlockedResources(/* scratchResourcesOnly */ true); |
| 1440 | |
| 1441 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 0); |
| 1442 | |
| 1443 | (helper.*addAccess)(helper.ddlCanvas2(), kImageWH, kNoID, false, false); |
| 1444 | REPORTER_ASSERT(reporter, (helper.*check)(helper.ddlCanvas2(), kImageWH, |
| 1445 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 1, kNoID)); |
| 1446 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
| 1447 | |
| 1448 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1449 | |
| 1450 | helper.checkImage(reporter, std::move(ddl1)); |
| 1451 | helper.checkImage(reporter, std::move(ddl2)); |
| 1452 | } |
| 1453 | |
| 1454 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache15View, reporter, ctxInfo) { |
| 1455 | test_15(ctxInfo.directContext(), reporter, &TestHelper::addViewAccess, &TestHelper::checkView, |
| 1456 | create_view_key); |
| 1457 | } |
| 1458 | |
| 1459 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache15Verts, reporter, ctxInfo) { |
| 1460 | test_15(ctxInfo.directContext(), reporter, &TestHelper::addVertAccess, &TestHelper::checkVert, |
| 1461 | create_vert_key); |
| 1462 | } |
Robert Phillips | 67e58cb | 2020-11-02 08:57:39 -0500 | [diff] [blame] | 1463 | |
| 1464 | // Case 16: Test out pre-emption of an existing vertex-data cache entry. This test simulates |
| 1465 | // the case where there is a race to create vertex data. However, the second one |
| 1466 | // to finish is better and usurps the first's position in the cache. |
| 1467 | // |
| 1468 | // This capability isn't available for views. |
| 1469 | |
| 1470 | static bool newer_is_always_better(SkData* /* incumbent */, SkData* /* challenger */) { |
| 1471 | return true; |
| 1472 | }; |
| 1473 | |
| 1474 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrThreadSafeCache16Verts, reporter, ctxInfo) { |
| 1475 | GrUniqueKey key; |
| 1476 | create_vert_key(&key, kImageWH, kNoID); |
| 1477 | |
| 1478 | TestHelper helper(ctxInfo.directContext(), newer_is_always_better); |
| 1479 | |
| 1480 | GrThreadSafeVertexTestOp* op1 = nullptr, *op2 = nullptr; |
| 1481 | |
| 1482 | helper.addVertAccess(helper.ddlCanvas1(), kImageWH, kNoID, false, false, &op1); |
| 1483 | REPORTER_ASSERT(reporter, helper.checkVert(helper.ddlCanvas1(), kImageWH, |
| 1484 | /*hits*/ 0, /*misses*/ 1, /*refs*/ 1, kNoID)); |
| 1485 | sk_sp<SkDeferredDisplayList> ddl1 = helper.snap1(); |
| 1486 | |
| 1487 | { |
| 1488 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1489 | auto [vertexData, xtraData] = helper.threadSafeCache()->findVertsWithData(key); |
| 1490 | REPORTER_ASSERT(reporter, vertexData.get() == op1->vertexData()); |
| 1491 | } |
| 1492 | |
| 1493 | helper.addVertAccess(helper.ddlCanvas2(), kImageWH, kNoID, /* failLookup */ true, false, &op2); |
| 1494 | REPORTER_ASSERT(reporter, helper.checkVert(helper.ddlCanvas2(), kImageWH, |
| 1495 | /*hits*/ 0, /*misses*/ 2, /*refs*/ 1, kNoID)); |
| 1496 | sk_sp<SkDeferredDisplayList> ddl2 = helper.snap2(); |
| 1497 | |
| 1498 | REPORTER_ASSERT(reporter, op1->vertexData() != op2->vertexData()); |
| 1499 | |
| 1500 | { |
| 1501 | REPORTER_ASSERT(reporter, helper.numCacheEntries() == 1); |
| 1502 | auto [vertexData, xtraData] = helper.threadSafeCache()->findVertsWithData(key); |
| 1503 | REPORTER_ASSERT(reporter, vertexData.get() == op2->vertexData()); |
| 1504 | } |
| 1505 | |
| 1506 | helper.checkImage(reporter, std::move(ddl1)); |
| 1507 | helper.checkImage(reporter, std::move(ddl2)); |
| 1508 | } |