Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
| 11 | #include "include/core/SkPromiseImageTexture.h" |
| 12 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrContextPriv.h" |
| 15 | #include "src/gpu/GrGpu.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 16 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/image/SkImage_Gpu.h" |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 18 | #include "tests/TestUtils.h" |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 19 | |
| 20 | using namespace sk_gpu_test; |
| 21 | |
| 22 | struct PromiseTextureChecker { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 23 | // shared indicates whether the backend texture is used to fulfill more than one promise |
| 24 | // image. |
| 25 | explicit PromiseTextureChecker(const GrBackendTexture& tex, skiatest::Reporter* reporter, |
| 26 | bool shared) |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 27 | : fTexture(SkPromiseImageTexture::Make(tex)) |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 28 | , fReporter(reporter) |
| 29 | , fShared(shared) |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 30 | , fFulfillCount(0) |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 31 | , fReleaseCount(0) |
| 32 | , fDoneCount(0) {} |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 33 | sk_sp<SkPromiseImageTexture> fTexture; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 34 | skiatest::Reporter* fReporter; |
| 35 | bool fShared; |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 36 | int fFulfillCount; |
| 37 | int fReleaseCount; |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 38 | int fDoneCount; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 39 | |
| 40 | /** |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 41 | * Releases the SkPromiseImageTexture. Used to test that cached GrTexture representations |
| 42 | * in the cache are freed. |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 43 | */ |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 44 | void releaseTexture() { fTexture.reset(); } |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 45 | |
| 46 | SkTArray<GrUniqueKey> uniqueKeys() const { |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 47 | return fTexture->testingOnly_uniqueKeysToInvalidate(); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 50 | static sk_sp<SkPromiseImageTexture> Fulfill(void* self) { |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 51 | auto checker = static_cast<PromiseTextureChecker*>(self); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 52 | checker->fFulfillCount++; |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 53 | return checker->fTexture; |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 54 | } |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 55 | static void Release(void* self) { |
| 56 | auto checker = static_cast<PromiseTextureChecker*>(self); |
| 57 | checker->fReleaseCount++; |
| 58 | if (!checker->fShared) { |
| 59 | // This is only used in a single threaded fashion with a single promise image. So |
| 60 | // every fulfill should be balanced by a release before the next fulfill. |
| 61 | REPORTER_ASSERT(checker->fReporter, checker->fReleaseCount == checker->fFulfillCount); |
| 62 | } |
| 63 | } |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 64 | static void Done(void* self) { |
| 65 | static_cast<PromiseTextureChecker*>(self)->fDoneCount++; |
| 66 | } |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 69 | enum class ReleaseBalanceExpectation { |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 70 | kBalanced, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 71 | kAllUnbalanced, |
| 72 | kUnbalancedByOne, |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 75 | enum class DoneBalanceExpectation { |
| 76 | kBalanced, |
| 77 | kAllUnbalanced, |
| 78 | kUnknown, |
| 79 | kUnbalancedByOne, |
| 80 | kBalancedOrOffByOne, |
| 81 | }; |
| 82 | |
| 83 | static void check_fulfill_and_release_cnts(skiatest::Reporter* reporter, |
| 84 | const PromiseTextureChecker& promiseChecker, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 85 | int expectedFulfillCnt, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 86 | ReleaseBalanceExpectation releaseBalanceExpecation, |
| 87 | DoneBalanceExpectation doneBalanceExpecation) { |
| 88 | REPORTER_ASSERT(reporter, promiseChecker.fFulfillCount == expectedFulfillCnt); |
| 89 | if (!expectedFulfillCnt) { |
| 90 | // Release and Done should only ever be called after Fulfill. |
| 91 | REPORTER_ASSERT(reporter, !promiseChecker.fReleaseCount); |
| 92 | REPORTER_ASSERT(reporter, !promiseChecker.fDoneCount); |
| 93 | return; |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 94 | } |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 95 | int releaseDiff = promiseChecker.fFulfillCount - promiseChecker.fReleaseCount; |
| 96 | switch (releaseBalanceExpecation) { |
| 97 | case ReleaseBalanceExpectation::kBalanced: |
| 98 | REPORTER_ASSERT(reporter, !releaseDiff); |
| 99 | break; |
| 100 | case ReleaseBalanceExpectation::kAllUnbalanced: |
| 101 | REPORTER_ASSERT(reporter, releaseDiff == promiseChecker.fFulfillCount); |
| 102 | break; |
| 103 | case ReleaseBalanceExpectation::kUnbalancedByOne: |
| 104 | REPORTER_ASSERT(reporter, releaseDiff == 1); |
| 105 | break; |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 106 | } |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 107 | int doneDiff = promiseChecker.fFulfillCount - promiseChecker.fDoneCount; |
| 108 | switch (doneBalanceExpecation) { |
| 109 | case DoneBalanceExpectation::kBalanced: |
| 110 | REPORTER_ASSERT(reporter, !doneDiff); |
| 111 | break; |
| 112 | case DoneBalanceExpectation::kAllUnbalanced: |
| 113 | REPORTER_ASSERT(reporter, doneDiff == promiseChecker.fFulfillCount); |
| 114 | break; |
| 115 | case DoneBalanceExpectation::kUnknown: |
| 116 | REPORTER_ASSERT(reporter, doneDiff >= 0 && doneDiff <= promiseChecker.fFulfillCount); |
| 117 | break; |
| 118 | case DoneBalanceExpectation::kUnbalancedByOne: |
| 119 | REPORTER_ASSERT(reporter, doneDiff == 1); |
| 120 | break; |
| 121 | case DoneBalanceExpectation::kBalancedOrOffByOne: |
| 122 | REPORTER_ASSERT(reporter, doneDiff == 0 || doneDiff == 1); |
| 123 | break; |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 124 | } |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 125 | } |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 126 | |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 127 | static void check_unfulfilled(const PromiseTextureChecker& promiseChecker, |
| 128 | skiatest::Reporter* reporter) { |
| 129 | check_fulfill_and_release_cnts(reporter, promiseChecker, 0, |
| 130 | ReleaseBalanceExpectation::kBalanced, |
| 131 | DoneBalanceExpectation::kBalanced); |
| 132 | } |
| 133 | |
| 134 | static void check_only_fulfilled(skiatest::Reporter* reporter, |
| 135 | const PromiseTextureChecker& promiseChecker, |
| 136 | int expectedFulfillCnt = 1) { |
| 137 | check_fulfill_and_release_cnts(reporter, promiseChecker, expectedFulfillCnt, |
| 138 | ReleaseBalanceExpectation::kAllUnbalanced, |
| 139 | DoneBalanceExpectation::kAllUnbalanced); |
| 140 | } |
| 141 | |
| 142 | static void check_all_flushed_but_not_synced(skiatest::Reporter* reporter, |
| 143 | const PromiseTextureChecker& promiseChecker, |
| 144 | GrBackendApi api, |
| 145 | int expectedFulfillCnt = 1) { |
| 146 | DoneBalanceExpectation doneBalanceExpectation = DoneBalanceExpectation::kBalanced; |
Greg Daniel | a026e91 | 2020-06-24 13:38:08 -0400 | [diff] [blame] | 147 | // On Vulkan and D3D Done isn't guaranteed to be called until a sync has occurred. |
| 148 | if (api == GrBackendApi::kVulkan || api == GrBackendApi::kDirect3D) { |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 149 | doneBalanceExpectation = expectedFulfillCnt == 1 |
| 150 | ? DoneBalanceExpectation::kBalancedOrOffByOne |
| 151 | : DoneBalanceExpectation::kUnknown; |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 152 | } |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 153 | check_fulfill_and_release_cnts(reporter, promiseChecker, expectedFulfillCnt, |
| 154 | ReleaseBalanceExpectation::kBalanced, doneBalanceExpectation); |
| 155 | } |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 156 | |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 157 | static void check_all_done(skiatest::Reporter* reporter, |
| 158 | const PromiseTextureChecker& promiseChecker, |
| 159 | int expectedFulfillCnt = 1) { |
| 160 | check_fulfill_and_release_cnts(reporter, promiseChecker, expectedFulfillCnt, |
| 161 | ReleaseBalanceExpectation::kBalanced, |
| 162 | DoneBalanceExpectation::kBalanced); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 163 | } |
| 164 | |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 165 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PromiseImageTest, reporter, ctxInfo) { |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 166 | const int kWidth = 10; |
| 167 | const int kHeight = 10; |
| 168 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 169 | auto ctx = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 170 | GrGpu* gpu = ctx->priv().getGpu(); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 171 | |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 172 | GrBackendTexture backendTex = ctx->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 173 | kWidth, kHeight, kRGBA_8888_SkColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 174 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kYes, GrProtected::kNo); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 176 | |
| 177 | GrBackendFormat backendFormat = backendTex.getBackendFormat(); |
| 178 | REPORTER_ASSERT(reporter, backendFormat.isValid()); |
| 179 | |
| 180 | PromiseTextureChecker promiseChecker(backendTex, reporter, false); |
| 181 | GrSurfaceOrigin texOrigin = kTopLeft_GrSurfaceOrigin; |
| 182 | sk_sp<SkImage> refImg( |
| 183 | SkImage_Gpu::MakePromiseTexture( |
| 184 | ctx, backendFormat, kWidth, kHeight, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 185 | GrMipmapped::kNo, texOrigin, |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 186 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, |
| 187 | nullptr, |
| 188 | PromiseTextureChecker::Fulfill, |
| 189 | PromiseTextureChecker::Release, |
| 190 | PromiseTextureChecker::Done, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 191 | &promiseChecker, |
| 192 | SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew)); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 193 | |
| 194 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 195 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info); |
| 196 | SkCanvas* canvas = surface->getCanvas(); |
| 197 | |
Brian Salomon | d716d44 | 2019-03-07 15:23:30 +0000 | [diff] [blame] | 198 | canvas->drawImage(refImg, 0, 0); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 199 | check_unfulfilled(promiseChecker, reporter); |
Brian Salomon | d716d44 | 2019-03-07 15:23:30 +0000 | [diff] [blame] | 200 | |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 201 | surface->flushAndSubmit(); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 202 | // We still own the image so we should not have called Release or Done. |
| 203 | check_only_fulfilled(reporter, promiseChecker); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 204 | |
| 205 | gpu->testingOnly_flushGpuAndSync(); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 206 | check_only_fulfilled(reporter, promiseChecker); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 207 | |
| 208 | canvas->drawImage(refImg, 0, 0); |
| 209 | canvas->drawImage(refImg, 0, 0); |
| 210 | |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 211 | surface->flushAndSubmit(); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 212 | |
| 213 | gpu->testingOnly_flushGpuAndSync(); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 214 | // Image should still be fulfilled from the first time we drew/flushed it. |
| 215 | check_only_fulfilled(reporter, promiseChecker); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 216 | |
| 217 | canvas->drawImage(refImg, 0, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 218 | surface->flushAndSubmit(); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 219 | check_only_fulfilled(reporter, promiseChecker); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 220 | |
| 221 | canvas->drawImage(refImg, 0, 0); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 222 | refImg.reset(); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 223 | // We no longer own the image but the last draw is still unflushed. |
| 224 | check_only_fulfilled(reporter, promiseChecker); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 225 | |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 226 | surface->flushAndSubmit(); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 227 | // Flushing should have called Release. Depending on the backend and timing it may have called |
| 228 | // done. |
| 229 | check_all_flushed_but_not_synced(reporter, promiseChecker, ctx->backend()); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 230 | gpu->testingOnly_flushGpuAndSync(); |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 231 | // Now Done should definitely have been called. |
| 232 | check_all_done(reporter, promiseChecker); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 233 | |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 234 | ctx->deleteBackendTexture(backendTex); |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 237 | DEF_GPUTEST(PromiseImageTextureShutdown, reporter, ctxInfo) { |
| 238 | const int kWidth = 10; |
| 239 | const int kHeight = 10; |
| 240 | |
| 241 | // Different ways of killing contexts. |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 242 | using DeathFn = std::function<void(sk_gpu_test::GrContextFactory*, GrDirectContext*)>; |
| 243 | DeathFn destroy = [](sk_gpu_test::GrContextFactory* factory, GrDirectContext*) { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 244 | factory->destroyContexts(); |
| 245 | }; |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 246 | DeathFn abandon = [](sk_gpu_test::GrContextFactory* factory, GrDirectContext* dContext) { |
| 247 | dContext->abandonContext(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 248 | }; |
| 249 | DeathFn releaseResourcesAndAbandon = [](sk_gpu_test::GrContextFactory* factory, |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 250 | GrDirectContext* dContext) { |
| 251 | dContext->releaseResourcesAndAbandonContext(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) { |
| 255 | auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type); |
| 256 | // These tests are difficult to get working with Vulkan. See http://skbug.com/8705 |
| 257 | // and http://skbug.com/8275 |
Stephen White | b007bb2 | 2020-06-04 09:02:04 -0400 | [diff] [blame] | 258 | // Also problematic on Dawn; see http://skbug.com/10326 |
Jim Van Verth | aed25a9 | 2020-06-23 15:00:04 -0400 | [diff] [blame] | 259 | // And Direct3D, for similar reasons. |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 260 | GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType); |
Jim Van Verth | aed25a9 | 2020-06-23 15:00:04 -0400 | [diff] [blame] | 261 | if (api == GrBackendApi::kVulkan || api == GrBackendApi::kDawn || |
| 262 | api == GrBackendApi::kDirect3D) { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | DeathFn contextKillers[] = {destroy, abandon, releaseResourcesAndAbandon}; |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 266 | for (const DeathFn& contextDeath : contextKillers) { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 267 | sk_gpu_test::GrContextFactory factory; |
| 268 | auto ctx = factory.get(contextType); |
| 269 | if (!ctx) { |
| 270 | continue; |
| 271 | } |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 272 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 273 | GrBackendTexture backendTex; |
| 274 | CreateBackendTexture(ctx, &backendTex, kWidth, kHeight, kAlpha_8_SkColorType, |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 275 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, |
| 276 | GrProtected::kNo); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 277 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 278 | |
| 279 | SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, |
| 280 | kPremul_SkAlphaType); |
| 281 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info); |
| 282 | SkCanvas* canvas = surface->getCanvas(); |
| 283 | |
| 284 | PromiseTextureChecker promiseChecker(backendTex, reporter, false); |
| 285 | sk_sp<SkImage> image(SkImage_Gpu::MakePromiseTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 286 | ctx, backendTex.getBackendFormat(), kWidth, kHeight, GrMipmapped::kNo, |
Brian Salomon | f55e8d5 | 2019-01-30 17:28:20 -0500 | [diff] [blame] | 287 | kTopLeft_GrSurfaceOrigin, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr, |
| 288 | PromiseTextureChecker::Fulfill, PromiseTextureChecker::Release, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 289 | PromiseTextureChecker::Done, &promiseChecker, |
| 290 | SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew)); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 291 | REPORTER_ASSERT(reporter, image); |
| 292 | |
| 293 | canvas->drawImage(image, 0, 0); |
| 294 | image.reset(); |
| 295 | // If the surface still holds a ref to the context then the factory will not be able |
| 296 | // to destroy the context (and instead will release-all-and-abandon). |
| 297 | surface.reset(); |
| 298 | |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 299 | ctx->flushAndSubmit(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 300 | contextDeath(&factory, ctx); |
| 301 | |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 302 | check_all_done(reporter, promiseChecker); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PromiseImageTextureFullCache, reporter, ctxInfo) { |
| 308 | const int kWidth = 10; |
| 309 | const int kHeight = 10; |
| 310 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 311 | auto dContext = ctxInfo.directContext(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 312 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 313 | GrBackendTexture backendTex = dContext->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 314 | kWidth, kHeight, kAlpha_8_SkColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 315 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 316 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 317 | |
| 318 | SkImageInfo info = |
| 319 | SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 320 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 321 | SkCanvas* canvas = surface->getCanvas(); |
| 322 | |
| 323 | PromiseTextureChecker promiseChecker(backendTex, reporter, false); |
| 324 | sk_sp<SkImage> image(SkImage_Gpu::MakePromiseTexture( |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 325 | dContext, backendTex.getBackendFormat(), kWidth, kHeight, GrMipmapped::kNo, |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 326 | kTopLeft_GrSurfaceOrigin, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr, |
| 327 | PromiseTextureChecker::Fulfill, PromiseTextureChecker::Release, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 328 | PromiseTextureChecker::Done, &promiseChecker, |
| 329 | SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew)); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 330 | REPORTER_ASSERT(reporter, image); |
| 331 | |
| 332 | // Make the cache full. This tests that we don't preemptively purge cached textures for |
| 333 | // fulfillment due to cache pressure. |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 334 | static constexpr int kMaxBytes = 1; |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 335 | dContext->setResourceCacheLimit(kMaxBytes); |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 336 | SkTArray<sk_sp<GrTexture>> textures; |
| 337 | for (int i = 0; i < 5; ++i) { |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 338 | auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 339 | GrRenderable::kNo); |
| 340 | textures.emplace_back(dContext->priv().resourceProvider()->createTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 341 | {100, 100}, format, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBudgeted::kYes, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 342 | GrProtected::kNo)); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 343 | REPORTER_ASSERT(reporter, textures[i]); |
| 344 | } |
| 345 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 346 | size_t bytesUsed; |
| 347 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 348 | dContext->getResourceCacheUsage(nullptr, &bytesUsed); |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 349 | REPORTER_ASSERT(reporter, bytesUsed > kMaxBytes); |
| 350 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 351 | // Relying on the asserts in the promiseImageChecker to ensure that fulfills and releases are |
| 352 | // properly ordered. |
| 353 | canvas->drawImage(image, 0, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 354 | surface->flushAndSubmit(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 355 | canvas->drawImage(image, 1, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 356 | surface->flushAndSubmit(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 357 | canvas->drawImage(image, 2, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 358 | surface->flushAndSubmit(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 359 | canvas->drawImage(image, 3, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 360 | surface->flushAndSubmit(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 361 | canvas->drawImage(image, 4, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 362 | surface->flushAndSubmit(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 363 | canvas->drawImage(image, 5, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 364 | surface->flushAndSubmit(); |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 365 | // Must call these to ensure that all callbacks are performed before the checker is destroyed. |
| 366 | image.reset(); |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 367 | dContext->flushAndSubmit(); |
| 368 | dContext->priv().getGpu()->testingOnly_flushGpuAndSync(); |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 369 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 370 | dContext->deleteBackendTexture(backendTex); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 371 | } |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 372 | |
| 373 | // Test case where promise image fulfill returns nullptr. |
| 374 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PromiseImageNullFulfill, reporter, ctxInfo) { |
| 375 | const int kWidth = 10; |
| 376 | const int kHeight = 10; |
| 377 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 378 | auto dContext = ctxInfo.directContext(); |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 379 | |
| 380 | // Do all this just to get a valid backend format for the image. |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 381 | GrBackendTexture backendTex; |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 382 | CreateBackendTexture(dContext, &backendTex, kWidth, kHeight, kRGBA_8888_SkColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 383 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kYes, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 384 | GrProtected::kNo); |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 385 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 386 | GrBackendFormat backendFormat = backendTex.getBackendFormat(); |
| 387 | REPORTER_ASSERT(reporter, backendFormat.isValid()); |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 388 | dContext->deleteBackendTexture(backendTex); |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 389 | |
| 390 | struct Counts { |
| 391 | int fFulfillCount = 0; |
| 392 | int fReleaseCount = 0; |
| 393 | int fDoneCount = 0; |
| 394 | } counts; |
| 395 | auto fulfill = [](SkDeferredDisplayListRecorder::PromiseImageTextureContext ctx) { |
| 396 | ++static_cast<Counts*>(ctx)->fFulfillCount; |
| 397 | return sk_sp<SkPromiseImageTexture>(); |
| 398 | }; |
| 399 | auto release = [](SkDeferredDisplayListRecorder::PromiseImageTextureContext ctx) { |
| 400 | ++static_cast<Counts*>(ctx)->fReleaseCount; |
| 401 | }; |
| 402 | auto done = [](SkDeferredDisplayListRecorder::PromiseImageTextureContext ctx) { |
| 403 | ++static_cast<Counts*>(ctx)->fDoneCount; |
| 404 | }; |
| 405 | GrSurfaceOrigin texOrigin = kTopLeft_GrSurfaceOrigin; |
| 406 | sk_sp<SkImage> refImg(SkImage_Gpu::MakePromiseTexture( |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 407 | dContext, backendFormat, kWidth, kHeight, GrMipmapped::kNo, texOrigin, |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 408 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr, fulfill, release, done, &counts, |
| 409 | SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew)); |
| 410 | |
| 411 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 412 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info); |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 413 | SkCanvas* canvas = surface->getCanvas(); |
| 414 | // Draw the image a few different ways. |
| 415 | canvas->drawImage(refImg, 0, 0); |
| 416 | SkPaint paint; |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 417 | paint.setColorFilter(SkColorFilters::LinearToSRGBGamma()); |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 418 | canvas->drawImage(refImg, 0, 0, &paint); |
| 419 | auto shader = refImg->makeShader(SkTileMode::kClamp, SkTileMode::kClamp); |
| 420 | REPORTER_ASSERT(reporter, shader); |
| 421 | paint.setShader(std::move(shader)); |
| 422 | canvas->drawRect(SkRect::MakeWH(1,1), paint); |
| 423 | paint.setShader(nullptr); |
| 424 | refImg.reset(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 425 | surface->flushAndSubmit(); |
Brian Salomon | d538d3d | 2019-04-04 12:18:17 -0400 | [diff] [blame] | 426 | // We should only call each callback once and we should have made all the calls by this point. |
| 427 | REPORTER_ASSERT(reporter, counts.fFulfillCount == 1); |
| 428 | REPORTER_ASSERT(reporter, counts.fReleaseCount == 1); |
| 429 | REPORTER_ASSERT(reporter, counts.fDoneCount == 1); |
| 430 | } |