bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 8 | #include <set> |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkSurface.h" |
| 10 | #include "include/gpu/GrContext.h" |
| 11 | #include "include/gpu/GrRenderTarget.h" |
| 12 | #include "include/gpu/GrTexture.h" |
| 13 | #include "src/core/SkAutoPixmapStorage.h" |
| 14 | #include "src/core/SkMipMap.h" |
| 15 | #include "src/gpu/GrClip.h" |
| 16 | #include "src/gpu/GrContextPriv.h" |
| 17 | #include "src/gpu/GrGpu.h" |
| 18 | #include "src/gpu/GrProxyProvider.h" |
| 19 | #include "src/gpu/GrResourceProvider.h" |
| 20 | #include "src/gpu/GrTexturePriv.h" |
| 21 | #include "tests/Test.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 22 | |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 23 | // Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture |
| 24 | // and render targets to GrSurface all work as expected. |
Brian Osman | fbe2406 | 2019-04-03 16:04:45 +0000 | [diff] [blame] | 25 | DEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 26 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 27 | auto resourceProvider = context->priv().resourceProvider(); |
| 28 | GrGpu* gpu = context->priv().getGpu(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 29 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 30 | GrSurfaceDesc desc; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 31 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 32 | desc.fWidth = 256; |
| 33 | desc.fHeight = 256; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 34 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 35 | desc.fSampleCnt = 1; |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 36 | sk_sp<GrSurface> texRT1 = resourceProvider->createTexture( |
| 37 | desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 38 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget()); |
| 40 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 42 | texRT1->asTexture()); |
| 43 | REPORTER_ASSERT(reporter, texRT1->asRenderTarget() == |
| 44 | static_cast<GrSurface*>(texRT1->asTexture())); |
| 45 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 46 | static_cast<GrSurface*>(texRT1->asTexture())); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 47 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 48 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 49 | sk_sp<GrTexture> tex1 = resourceProvider->createTexture( |
| 50 | desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget()); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture()); |
| 53 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture()); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 54 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 55 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 56 | nullptr, 256, 256, GrColorType::kRGBA_8888, false, GrMipMapped::kNo); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 57 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 58 | sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture( |
| 59 | backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 60 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget()); |
| 62 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 63 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 64 | texRT2->asTexture()); |
| 65 | REPORTER_ASSERT(reporter, texRT2->asRenderTarget() == |
| 66 | static_cast<GrSurface*>(texRT2->asTexture())); |
| 67 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 68 | static_cast<GrSurface*>(texRT2->asTexture())); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 69 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 70 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 73 | // This test checks that the isConfigTexturable and isConfigRenderable are |
| 74 | // consistent with createTexture's result. |
| 75 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) { |
| 76 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 77 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 78 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
| 79 | const GrCaps* caps = context->priv().caps(); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 80 | |
| 81 | GrPixelConfig configs[] = { |
| 82 | kUnknown_GrPixelConfig, |
| 83 | kAlpha_8_GrPixelConfig, |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 84 | kAlpha_8_as_Alpha_GrPixelConfig, |
| 85 | kAlpha_8_as_Red_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 86 | kGray_8_GrPixelConfig, |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 87 | kGray_8_as_Lum_GrPixelConfig, |
| 88 | kGray_8_as_Red_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 89 | kRGB_565_GrPixelConfig, |
| 90 | kRGBA_4444_GrPixelConfig, |
| 91 | kRGBA_8888_GrPixelConfig, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 92 | kRGB_888_GrPixelConfig, |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 93 | kRGB_888X_GrPixelConfig, |
Jim Van Verth | 69e5785 | 2018-12-05 13:38:59 -0500 | [diff] [blame] | 94 | kRG_88_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 95 | kBGRA_8888_GrPixelConfig, |
| 96 | kSRGBA_8888_GrPixelConfig, |
| 97 | kSBGRA_8888_GrPixelConfig, |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 98 | kRGBA_1010102_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 99 | kRGBA_float_GrPixelConfig, |
| 100 | kRG_float_GrPixelConfig, |
| 101 | kAlpha_half_GrPixelConfig, |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 102 | kAlpha_half_as_Red_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 103 | kRGBA_half_GrPixelConfig, |
Brian Osman | d0626aa | 2019-03-11 15:28:06 -0400 | [diff] [blame] | 104 | kRGBA_half_Clamped_GrPixelConfig, |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 105 | kRGB_ETC1_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 106 | }; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 107 | GR_STATIC_ASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs)); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 108 | |
| 109 | GrSurfaceDesc desc; |
| 110 | desc.fWidth = 64; |
| 111 | desc.fHeight = 64; |
| 112 | |
| 113 | for (GrPixelConfig config : configs) { |
| 114 | for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
| 115 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 116 | desc.fConfig = config; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 117 | desc.fSampleCnt = 1; |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 118 | |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 119 | sk_sp<GrSurface> tex = resourceProvider->createTexture( |
| 120 | desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 121 | bool ict = caps->isConfigTexturable(desc.fConfig); |
| 122 | REPORTER_ASSERT(reporter, SkToBool(tex) == ict, |
| 123 | "config:%d, tex:%d, isConfigTexturable:%d", config, SkToBool(tex), ict); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 124 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 125 | GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo; |
| 126 | GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(config, &srgbEncoded); |
| 127 | const GrBackendFormat format = |
| 128 | caps->getBackendFormatFromGrColorType(colorType, srgbEncoded); |
| 129 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 130 | sk_sp<GrTextureProxy> proxy = |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 131 | proxyProvider->createMipMapProxy(format, desc, origin, SkBudgeted::kNo); |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == |
| 133 | (caps->isConfigTexturable(desc.fConfig) && |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 134 | caps->mipMapSupport())); |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 135 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 136 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 137 | tex = resourceProvider->createTexture(desc, SkBudgeted::kNo, |
| 138 | GrResourceProvider::Flags::kNoPendingIO); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 139 | bool isRenderable = caps->isConfigRenderable(config); |
| 140 | REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, |
| 141 | "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex), |
| 142 | isRenderable); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 143 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 144 | desc.fSampleCnt = 2; |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 145 | tex = resourceProvider->createTexture(desc, SkBudgeted::kNo, |
| 146 | GrResourceProvider::Flags::kNoPendingIO); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 147 | isRenderable = SkToBool(caps->getRenderTargetSampleCount(2, config)); |
| 148 | REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, |
| 149 | "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex), |
| 150 | isRenderable); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 154 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 155 | #include "include/private/GrSurfaceProxy.h" |
| 156 | #include "src/gpu/GrDrawingManager.h" |
| 157 | #include "src/gpu/GrTextureContext.h" |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 158 | |
| 159 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) { |
| 160 | static constexpr int kSize = 100; |
| 161 | GrSurfaceDesc desc; |
| 162 | desc.fWidth = desc.fHeight = kSize; |
| 163 | std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 164 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 165 | GrContext* context = context_info.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 166 | const GrCaps* caps = context->priv().caps(); |
| 167 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 168 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 169 | for (int c = 0; c <= kLast_GrPixelConfig; ++c) { |
| 170 | desc.fConfig = static_cast<GrPixelConfig>(c); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 171 | if (!caps->isConfigTexturable(desc.fConfig)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 172 | continue; |
| 173 | } |
| 174 | desc.fFlags = kPerformInitialClear_GrSurfaceFlag; |
| 175 | for (bool rt : {false, true}) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 176 | if (rt && !caps->isConfigRenderable(desc.fConfig)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 177 | continue; |
| 178 | } |
| 179 | desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 180 | for (GrSurfaceOrigin origin : |
| 181 | {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 182 | for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) { |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 183 | // Try directly creating the texture. |
| 184 | // Do this twice in an attempt to hit the cache on the second time through. |
| 185 | for (int i = 0; i < 2; ++i) { |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 186 | auto proxy = proxyProvider->testingOnly_createInstantiatedProxy( |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 187 | desc, origin, fit, SkBudgeted::kYes); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 188 | if (!proxy) { |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 189 | continue; |
| 190 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 191 | auto texCtx = context->priv().makeWrappedSurfaceContext(std::move(proxy)); |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 192 | SkImageInfo info = SkImageInfo::Make( |
| 193 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 194 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 195 | if (texCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 196 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 197 | for (int i = 0; i < kSize * kSize; ++i) { |
| 198 | if (cmp != data.get()[i]) { |
| 199 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 200 | break; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 201 | } |
| 202 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 203 | } |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 204 | memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t)); |
| 205 | // Here we overwrite the texture so that the second time through we |
| 206 | // test against recycling without reclearing. |
| 207 | if (0 == i) { |
| 208 | texCtx->writePixels(info, data.get(), 0, 0, 0); |
Greg Daniel | 52cb5fe | 2017-09-05 15:45:15 -0400 | [diff] [blame] | 209 | } |
Greg Daniel | 52cb5fe | 2017-09-05 15:45:15 -0400 | [diff] [blame] | 210 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 211 | context->priv().testingOnly_purgeAllUnlockedResources(); |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 212 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 213 | GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo; |
| 214 | GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(desc.fConfig, |
| 215 | &srgbEncoded); |
| 216 | const GrBackendFormat format = |
| 217 | caps->getBackendFormatFromGrColorType(colorType, srgbEncoded); |
| 218 | |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 219 | // Try creating the texture as a deferred proxy. |
| 220 | for (int i = 0; i < 2; ++i) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 221 | auto surfCtx = context->priv().makeDeferredSurfaceContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 222 | format, desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes); |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 223 | if (!surfCtx) { |
| 224 | continue; |
| 225 | } |
| 226 | SkImageInfo info = SkImageInfo::Make( |
| 227 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 228 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 229 | if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 230 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 231 | for (int i = 0; i < kSize * kSize; ++i) { |
| 232 | if (cmp != data.get()[i]) { |
| 233 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | // Here we overwrite the texture so that the second time through we |
| 239 | // test against recycling without reclearing. |
| 240 | if (0 == i) { |
| 241 | surfCtx->writePixels(info, data.get(), 0, 0, 0); |
| 242 | } |
| 243 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 244 | context->priv().testingOnly_purgeAllUnlockedResources(); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 250 | |
| 251 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) { |
| 252 | auto fillPixels = [](const SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) { |
| 253 | for (int y = 0; y < p->height(); ++y) { |
| 254 | for (int x = 0; x < p->width(); ++x) { |
| 255 | *p->writable_addr32(x, y) = f(x, y); |
| 256 | } |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) { |
| 261 | SkASSERT(p1.info() == p2.info()); |
| 262 | for (int y = 0; y < p1.height(); ++y) { |
| 263 | for (int x = 0; x < p1.width(); ++x) { |
| 264 | REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y)); |
| 265 | if (p1.getColor(x, y) != p2.getColor(x, y)) { |
| 266 | return; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | static constexpr int kSize = 100; |
| 273 | SkAutoPixmapStorage pixels; |
| 274 | pixels.alloc(SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 275 | fillPixels(&pixels, [](int x, int y) { |
| 276 | return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF); |
| 277 | }); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 278 | |
| 279 | GrContext* context = context_info.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 280 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 281 | |
| 282 | // We test both kRW in addition to kRead mostly to ensure that the calls are structured such |
| 283 | // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with |
| 284 | // kRead for the right reason. |
| 285 | for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 286 | auto backendTex = context->priv().getGpu()->createTestingOnlyBackendTexture( |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 287 | pixels.addr(), kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kNo); |
| 288 | auto proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 289 | kBorrow_GrWrapOwnership, |
| 290 | GrWrapCacheable::kNo, ioType); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 291 | auto surfContext = context->priv().makeWrappedSurfaceContext(proxy); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 292 | |
| 293 | // Read pixels should work with a read-only texture. |
| 294 | SkAutoPixmapStorage read; |
| 295 | read.alloc(pixels.info()); |
| 296 | auto readResult = surfContext->readPixels(pixels.info(), read.writable_addr(), 0, 0, 0); |
| 297 | REPORTER_ASSERT(reporter, readResult); |
| 298 | if (readResult) { |
| 299 | comparePixels(pixels, read, reporter); |
| 300 | } |
| 301 | |
| 302 | // Write pixels should not work with a read-only texture. |
| 303 | SkAutoPixmapStorage write; |
| 304 | write.alloc(pixels.info()); |
| 305 | fillPixels(&write, [&pixels](int x, int y) { return ~*pixels.addr32(); }); |
| 306 | auto writeResult = surfContext->writePixels(pixels.info(), pixels.addr(), 0, 0, 0); |
| 307 | REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType)); |
| 308 | // Try the low level write. |
| 309 | context->flush(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 310 | auto gpuWriteResult = context->priv().getGpu()->writePixels( |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 311 | proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888, write.addr32(), |
| 312 | 0); |
| 313 | REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType)); |
| 314 | |
| 315 | // Copies should not work with a read-only texture |
| 316 | auto copySrc = proxyProvider->createTextureProxy( |
| 317 | SkImage::MakeFromRaster(write, nullptr, nullptr), kNone_GrSurfaceFlags, 1, |
| 318 | SkBudgeted::kYes, SkBackingFit::kExact); |
| 319 | REPORTER_ASSERT(reporter, copySrc); |
| 320 | auto copyResult = surfContext->copy(copySrc.get()); |
| 321 | REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType)); |
| 322 | // Try the low level copy. |
| 323 | context->flush(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 324 | auto gpuCopyResult = context->priv().getGpu()->copySurface( |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 325 | proxy->peekTexture(), kTopLeft_GrSurfaceOrigin, copySrc->peekTexture(), |
| 326 | kTopLeft_GrSurfaceOrigin, SkIRect::MakeWH(kSize, kSize), {0, 0}); |
| 327 | REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType)); |
| 328 | |
| 329 | // Mip regen should not work with a read only texture. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 330 | if (context->priv().caps()->mipMapSupport()) { |
| 331 | backendTex = context->priv().getGpu()->createTestingOnlyBackendTexture( |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 332 | nullptr, kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kYes); |
| 333 | proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 334 | kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, |
| 335 | ioType); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 336 | context->flush(); |
| 337 | proxy->peekTexture()->texturePriv().markMipMapsDirty(); // avoids assert in GrGpu. |
| 338 | auto regenResult = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 339 | context->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 340 | REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType)); |
| 341 | } |
| 342 | } |
| 343 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 344 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 345 | static sk_sp<GrTexture> make_wrapped_texture(GrContext* context, bool renderable) { |
| 346 | auto backendTexture = context->priv().getGpu()->createTestingOnlyBackendTexture( |
| 347 | nullptr, 10, 10, GrColorType::kRGBA_8888, renderable, GrMipMapped::kNo); |
| 348 | sk_sp<GrTexture> texture; |
| 349 | if (renderable) { |
| 350 | texture = context->priv().resourceProvider()->wrapRenderableBackendTexture( |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 351 | backendTexture, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 352 | } else { |
| 353 | texture = context->priv().resourceProvider()->wrapBackendTexture( |
| 354 | backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType); |
| 355 | } |
| 356 | // Add a release proc that deletes the GrBackendTexture. |
| 357 | struct ReleaseContext { |
| 358 | GrContext* fContext; |
| 359 | GrBackendTexture fBackendTexture; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 360 | }; |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 361 | auto release = [](void* rc) { |
| 362 | auto releaseContext = static_cast<ReleaseContext*>(rc); |
| 363 | if (!releaseContext->fContext->abandoned()) { |
| 364 | if (auto gpu = releaseContext->fContext->priv().getGpu()) { |
| 365 | gpu->deleteTestingOnlyBackendTexture(releaseContext->fBackendTexture); |
| 366 | } |
| 367 | } |
| 368 | delete releaseContext; |
| 369 | }; |
Brian Salomon | 2ca31f8 | 2019-03-05 13:28:58 -0500 | [diff] [blame] | 370 | texture->setRelease(release, new ReleaseContext{context, backendTexture}); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 371 | return texture; |
| 372 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 373 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 374 | static sk_sp<GrTexture> make_normal_texture(GrContext* context, bool renderable) { |
| 375 | GrSurfaceDesc desc; |
| 376 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 377 | desc.fWidth = desc.fHeight = 10; |
| 378 | desc.fFlags = renderable ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 379 | return context->priv().resourceProvider()->createTexture( |
| 380 | desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 381 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 382 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 383 | DEF_GPUTEST(TextureIdleProcTest, reporter, options) { |
| 384 | // Various ways of making textures. |
| 385 | auto makeWrapped = [](GrContext* context) { return make_wrapped_texture(context, false); }; |
| 386 | auto makeWrappedRenderable = [](GrContext* context) { |
| 387 | return make_wrapped_texture(context, true); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 388 | }; |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 389 | auto makeNormal = [](GrContext* context) { return make_normal_texture(context, false); }; |
| 390 | auto makeRenderable = [](GrContext* context) { return make_normal_texture(context, true); }; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 391 | |
| 392 | std::function<sk_sp<GrTexture>(GrContext*)> makers[] = {makeWrapped, makeWrappedRenderable, |
| 393 | makeNormal, makeRenderable}; |
| 394 | |
| 395 | // Add a unique key, or not. |
| 396 | auto addKey = [](GrTexture* texture) { |
| 397 | static uint32_t gN = 0; |
| 398 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 399 | GrUniqueKey key; |
| 400 | GrUniqueKey::Builder builder(&key, kDomain, 1); |
| 401 | builder[0] = gN++; |
| 402 | builder.finish(); |
| 403 | texture->resourcePriv().setUniqueKey(key); |
| 404 | }; |
| 405 | auto dontAddKey = [](GrTexture* texture) {}; |
| 406 | std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey}; |
| 407 | |
| 408 | for (const auto& m : makers) { |
| 409 | for (const auto& keyAdder : keyAdders) { |
| 410 | for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) { |
| 411 | sk_gpu_test::GrContextFactory factory; |
| 412 | auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 413 | GrContext* context = factory.get(contextType); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 414 | if (!context) { |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | // The callback we add simply adds an integer to a set. |
| 419 | std::set<int> idleIDs; |
| 420 | struct Context { |
| 421 | std::set<int>* fIdleIDs; |
| 422 | int fNum; |
| 423 | }; |
| 424 | auto proc = [](void* context) { |
| 425 | static_cast<Context*>(context)->fIdleIDs->insert( |
| 426 | static_cast<Context*>(context)->fNum); |
| 427 | delete static_cast<Context*>(context); |
| 428 | }; |
| 429 | |
| 430 | // Makes a texture, possibly adds a key, and sets the callback. |
| 431 | auto make = [&m, &keyAdder, &proc, &idleIDs](GrContext* context, int num) { |
| 432 | sk_sp<GrTexture> texture = m(context); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 433 | texture->addIdleProc(proc, new Context{&idleIDs, num}, |
| 434 | GrTexture::IdleState::kFinished); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 435 | keyAdder(texture.get()); |
| 436 | return texture; |
| 437 | }; |
| 438 | |
| 439 | auto texture = make(context, 1); |
| 440 | REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end()); |
| 441 | bool isRT = SkToBool(texture->asRenderTarget()); |
| 442 | auto backendFormat = texture->backendFormat(); |
| 443 | texture.reset(); |
| 444 | REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end()); |
| 445 | |
| 446 | texture = make(context, 2); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 447 | int w = texture->width(); |
| 448 | int h = texture->height(); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 449 | SkImageInfo info = |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 450 | SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 451 | auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr); |
| 452 | auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext(); |
| 453 | auto singleUseLazyCB = [&texture](GrResourceProvider* rp) { |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 454 | auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced; |
| 455 | if (texture->getUniqueKey().isValid()) { |
| 456 | mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced; |
| 457 | } |
| 458 | return GrSurfaceProxy::LazyInstantiationResult{std::move(texture), mode}; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 459 | }; |
| 460 | GrSurfaceDesc desc; |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 461 | desc.fWidth = w; |
| 462 | desc.fHeight = h; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 463 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 464 | if (isRT) { |
| 465 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 466 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 467 | SkBudgeted budgeted; |
| 468 | if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) { |
| 469 | budgeted = SkBudgeted::kYes; |
| 470 | } else { |
| 471 | budgeted = SkBudgeted::kNo; |
| 472 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 473 | auto proxy = context->priv().proxyProvider()->createLazyProxy( |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 474 | singleUseLazyCB, backendFormat, desc, |
| 475 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, |
| 476 | GrInternalSurfaceFlags ::kNone, SkBackingFit::kExact, budgeted, |
| 477 | GrSurfaceProxy::LazyInstantiationType::kSingleUse); |
Michael Ludwig | d54ca8f | 2019-02-13 13:25:21 -0500 | [diff] [blame] | 478 | rtc->drawTexture(GrNoClip(), proxy, GrSamplerState::Filter::kNearest, |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 479 | SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h), |
| 480 | SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone, |
Michael Ludwig | d54ca8f | 2019-02-13 13:25:21 -0500 | [diff] [blame] | 481 | SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 482 | // We still have the proxy, which should remain instantiated, thereby keeping the |
| 483 | // texture not purgeable. |
| 484 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
| 485 | context->flush(); |
| 486 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 487 | context->priv().getGpu()->testingOnly_flushGpuAndSync(); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 488 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
| 489 | |
| 490 | // This time we move the proxy into the draw. |
| 491 | rtc->drawTexture(GrNoClip(), std::move(proxy), GrSamplerState::Filter::kNearest, |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 492 | SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h), |
| 493 | SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone, |
Michael Ludwig | d54ca8f | 2019-02-13 13:25:21 -0500 | [diff] [blame] | 494 | SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 495 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
| 496 | context->flush(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 497 | context->priv().getGpu()->testingOnly_flushGpuAndSync(); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 498 | // Now that the draw is fully consumed by the GPU, the texture should be idle. |
| 499 | REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end()); |
| 500 | |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 501 | // Make a proxy that should deinstantiate even if we keep a ref on it. |
| 502 | auto deinstantiateLazyCB = [&make, &context](GrResourceProvider* rp) { |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 503 | auto texture = make(context, 3); |
| 504 | auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced; |
| 505 | if (texture->getUniqueKey().isValid()) { |
| 506 | mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced; |
| 507 | } |
| 508 | return GrSurfaceProxy::LazyInstantiationResult{std::move(texture), mode}; |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 509 | }; |
| 510 | proxy = context->priv().proxyProvider()->createLazyProxy( |
| 511 | deinstantiateLazyCB, backendFormat, desc, |
| 512 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, |
| 513 | GrInternalSurfaceFlags ::kNone, SkBackingFit::kExact, budgeted, |
| 514 | GrSurfaceProxy::LazyInstantiationType::kDeinstantiate); |
| 515 | rtc->drawTexture(GrNoClip(), std::move(proxy), GrSamplerState::Filter::kNearest, |
| 516 | SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h), |
| 517 | SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone, |
| 518 | SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr); |
| 519 | // At this point the proxy shouldn't even be instantiated, there is no texture with |
| 520 | // id 3. |
| 521 | REPORTER_ASSERT(reporter, idleIDs.find(3) == idleIDs.end()); |
| 522 | context->flush(); |
| 523 | context->priv().getGpu()->testingOnly_flushGpuAndSync(); |
| 524 | // Now that the draw is fully consumed, we should have deinstantiated the proxy and |
| 525 | // the texture it made should be idle. |
| 526 | REPORTER_ASSERT(reporter, idleIDs.find(3) != idleIDs.end()); |
| 527 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 528 | // Make sure we make the call during various shutdown scenarios where the texture |
| 529 | // might persist after context is destroyed, abandoned, etc. We test three |
| 530 | // variations of each scenario. One where the texture is just created. Another, |
| 531 | // where the texture has been used in a draw and then the context is flushed. And |
| 532 | // one where the the texture was drawn but the context is not flushed. |
| 533 | // In each scenario we test holding a ref beyond the context shutdown and not. |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 534 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 535 | // These tests are difficult to get working with Vulkan. See http://skbug.com/8705 |
| 536 | // and http://skbug.com/8275 |
| 537 | GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType); |
| 538 | if (api == GrBackendApi::kVulkan) { |
| 539 | continue; |
| 540 | } |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 541 | int id = 4; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 542 | enum class DrawType { |
| 543 | kNoDraw, |
| 544 | kDraw, |
| 545 | kDrawAndFlush, |
| 546 | }; |
| 547 | for (auto drawType : |
| 548 | {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) { |
| 549 | for (bool unrefFirst : {false, true}) { |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 550 | auto possiblyDrawAndFlush = [&context, &texture, drawType, unrefFirst, w, |
| 551 | h] { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 552 | if (drawType == DrawType::kNoDraw) { |
| 553 | return; |
| 554 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 555 | SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 556 | kPremul_SkAlphaType); |
| 557 | auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, |
| 558 | nullptr); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 559 | auto rtc = rt->getCanvas() |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 560 | ->internal_private_accessTopLayerRenderTargetContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 561 | auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped( |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 562 | texture, kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 563 | rtc->drawTexture( |
| 564 | GrNoClip(), proxy, GrSamplerState::Filter::kNearest, |
| 565 | SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h), |
| 566 | SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone, |
| 567 | SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 568 | if (drawType == DrawType::kDrawAndFlush) { |
| 569 | context->flush(); |
| 570 | } |
| 571 | if (unrefFirst) { |
| 572 | texture.reset(); |
| 573 | } |
| 574 | }; |
| 575 | texture = make(context, id); |
| 576 | possiblyDrawAndFlush(); |
| 577 | context->abandonContext(); |
| 578 | texture.reset(); |
| 579 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
| 580 | factory.destroyContexts(); |
| 581 | context = factory.get(contextType); |
| 582 | ++id; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 583 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 584 | // Similar to previous, but reset the texture after the context was |
| 585 | // abandoned and then destroyed. |
| 586 | texture = make(context, id); |
| 587 | possiblyDrawAndFlush(); |
| 588 | context->abandonContext(); |
| 589 | factory.destroyContexts(); |
| 590 | texture.reset(); |
| 591 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
| 592 | context = factory.get(contextType); |
| 593 | id++; |
| 594 | |
| 595 | texture = make(context, id); |
| 596 | possiblyDrawAndFlush(); |
| 597 | factory.destroyContexts(); |
| 598 | texture.reset(); |
| 599 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
| 600 | context = factory.get(contextType); |
| 601 | id++; |
| 602 | |
| 603 | texture = make(context, id); |
| 604 | possiblyDrawAndFlush(); |
| 605 | factory.releaseResourcesAndAbandonContexts(); |
| 606 | texture.reset(); |
| 607 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
| 608 | context = factory.get(contextType); |
| 609 | id++; |
| 610 | } |
| 611 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 616 | |
| 617 | // Tests an idle proc that unrefs another resource down to zero. |
| 618 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) { |
| 619 | GrContext* context = contextInfo.grContext(); |
| 620 | |
| 621 | // idle proc that releases another texture. |
| 622 | auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); }; |
| 623 | |
| 624 | for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) { |
| 625 | for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 626 | for (auto idleState : |
| 627 | {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) { |
| 628 | auto idleTexture = idleMaker(context, false); |
| 629 | auto otherTexture = otherMaker(context, false); |
| 630 | otherTexture->ref(); |
| 631 | idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState); |
| 632 | otherTexture.reset(); |
| 633 | idleTexture.reset(); |
| 634 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | // Similar to above but more complicated. This flushes the context from the idle proc. |
| 640 | // crbug.com/933526. |
| 641 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) { |
| 642 | GrContext* context = contextInfo.grContext(); |
| 643 | |
| 644 | // idle proc that flushes the context. |
| 645 | auto idleProc = [](void* context) { reinterpret_cast<GrContext*>(context)->flush(); }; |
| 646 | |
| 647 | for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 648 | for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) { |
| 649 | auto idleTexture = idleMaker(context, false); |
| 650 | idleTexture->addIdleProc(idleProc, context, idleState); |
| 651 | auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 652 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 1, nullptr); |
| 653 | // We'll draw two images to the canvas. One is a normal texture-backed image. The other |
| 654 | // is a wrapped-texture backed image. |
| 655 | surf->getCanvas()->clear(SK_ColorWHITE); |
| 656 | auto img1 = surf->makeImageSnapshot(); |
| 657 | auto gpu = context->priv().getGpu(); |
| 658 | std::unique_ptr<uint32_t[]> pixels(new uint32_t[info.width() * info.height()]); |
| 659 | auto backendTexture = gpu->createTestingOnlyBackendTexture( |
| 660 | pixels.get(), info.width(), info.height(), kRGBA_8888_SkColorType, false, |
| 661 | GrMipMapped::kNo); |
| 662 | auto img2 = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin, |
| 663 | info.colorType(), info.alphaType(), nullptr); |
| 664 | surf->getCanvas()->drawImage(std::move(img1), 0, 0); |
| 665 | surf->getCanvas()->drawImage(std::move(img2), 1, 1); |
| 666 | idleTexture.reset(); |
| 667 | gpu->deleteTestingOnlyBackendTexture(backendTexture); |
| 668 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
| 672 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) { |
| 673 | GrContext* context = contextInfo.grContext(); |
| 674 | // idle proc that refs the texture |
| 675 | auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); }; |
| 676 | // release proc to check whether the texture was released or not. |
| 677 | auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; }; |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 678 | for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) { |
| 679 | bool isReleased = false; |
| 680 | auto idleTexture = make_normal_texture(context, false); |
| 681 | // This test assumes the texture won't be cached (or else the release proc doesn't get |
| 682 | // called). |
| 683 | idleTexture->resourcePriv().removeScratchKey(); |
| 684 | context->flush(); |
| 685 | idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState); |
| 686 | idleTexture->setRelease(releaseProc, &isReleased); |
| 687 | auto* raw = idleTexture.get(); |
| 688 | idleTexture.reset(); |
| 689 | REPORTER_ASSERT(reporter, !isReleased); |
| 690 | raw->unref(); |
| 691 | REPORTER_ASSERT(reporter, isReleased); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) { |
| 696 | GrContext* context = contextInfo.grContext(); |
| 697 | for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) { |
| 698 | auto idleTexture = idleMaker(context, false); |
| 699 | |
| 700 | uint32_t flags = 0; |
| 701 | static constexpr uint32_t kFlushFlag = 0x1; |
| 702 | static constexpr uint32_t kFinishFlag = 0x2; |
| 703 | auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; }; |
| 704 | auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; }; |
| 705 | idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed); |
| 706 | idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished); |
| 707 | |
| 708 | // Insert a copy from idleTexture to another texture so that we have some queued IO on |
| 709 | // idleTexture. |
| 710 | auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped( |
| 711 | std::move(idleTexture), kTopLeft_GrSurfaceOrigin); |
| 712 | SkImageInfo info = SkImageInfo::Make(proxy->width(), proxy->height(), |
| 713 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 714 | auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr); |
| 715 | auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext(); |
| 716 | context->flush(); |
| 717 | rtc->copy(proxy.get()); |
| 718 | proxy.reset(); |
| 719 | REPORTER_ASSERT(reporter, flags == 0); |
| 720 | |
| 721 | // After a flush we expect idleTexture to have reached the kFlushed state on all backends. |
| 722 | // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only |
| 723 | // current "unmanaged" backend, we *may* need a sync to reach kFinished. |
| 724 | context->flush(); |
| 725 | if (contextInfo.backend() == kVulkan_GrBackend) { |
| 726 | REPORTER_ASSERT(reporter, flags & kFlushFlag); |
| 727 | } else { |
| 728 | REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag)); |
| 729 | } |
| 730 | context->priv().getGpu()->testingOnly_flushGpuAndSync(); |
| 731 | REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag)); |
| 732 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 733 | } |