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