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" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkAutoPixmapStorage.h" |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 12 | #include "src/core/SkCompressedDataUtils.h" |
Greg Daniel | 01f278c | 2020-06-12 16:58:17 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrBackendUtils.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrContextPriv.h" |
| 16 | #include "src/gpu/GrGpu.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrProxyProvider.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrRenderTarget.h" |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrResourceProvider.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 22 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tests/Test.h" |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 24 | #include "tests/TestUtils.h" |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame^] | 25 | #include "tools/gpu/BackendTextureImageFactory.h" |
| 26 | #include "tools/gpu/ManagedBackendTexture.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 27 | |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 28 | // Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture |
| 29 | // and render targets to GrSurface all work as expected. |
Brian Osman | fbe2406 | 2019-04-03 16:04:45 +0000 | [diff] [blame] | 30 | DEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 31 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 32 | auto resourceProvider = context->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 33 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 34 | static constexpr SkISize kDesc = {256, 256}; |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 35 | auto format = context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 36 | GrRenderable::kYes); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 37 | sk_sp<GrSurface> texRT1 = |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 38 | resourceProvider->createTexture(kDesc, format, GrRenderable::kYes, 1, GrMipmapped::kNo, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 39 | SkBudgeted::kNo, GrProtected::kNo); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 40 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget()); |
| 42 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 43 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 44 | texRT1->asTexture()); |
| 45 | REPORTER_ASSERT(reporter, texRT1->asRenderTarget() == |
| 46 | static_cast<GrSurface*>(texRT1->asTexture())); |
| 47 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 48 | static_cast<GrSurface*>(texRT1->asTexture())); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 49 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 50 | sk_sp<GrTexture> tex1 = |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 51 | resourceProvider->createTexture(kDesc, format, GrRenderable::kNo, 1, GrMipmapped::kNo, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 52 | SkBudgeted::kNo, GrProtected::kNo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget()); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture()); |
| 55 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture()); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 56 | |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 57 | GrBackendTexture backendTex = context->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 58 | 256, 256, kRGBA_8888_SkColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 59 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 60 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 61 | sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture( |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 62 | backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 63 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget()); |
| 65 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 67 | texRT2->asTexture()); |
| 68 | REPORTER_ASSERT(reporter, texRT2->asRenderTarget() == |
| 69 | static_cast<GrSurface*>(texRT2->asTexture())); |
| 70 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 71 | static_cast<GrSurface*>(texRT2->asTexture())); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 72 | |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 73 | context->deleteBackendTexture(backendTex); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 76 | // This test checks that the isFormatTexturable and isFormatRenderable are |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 77 | // consistent with createTexture's result. |
| 78 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 79 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 80 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 81 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
| 82 | const GrCaps* caps = context->priv().caps(); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 83 | |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 84 | // TODO: Should only need format here but need to determine compression type from format |
| 85 | // without config. |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 86 | auto createTexture = [](SkISize dimensions, GrColorType colorType, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 87 | const GrBackendFormat& format, GrRenderable renderable, |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 88 | GrResourceProvider* rp) -> sk_sp<GrTexture> { |
Greg Daniel | 01f278c | 2020-06-12 16:58:17 -0400 | [diff] [blame] | 89 | SkImage::CompressionType compression = GrBackendFormatToCompressionType(format); |
Robert Phillips | d6df7b5 | 2019-12-13 11:17:46 -0500 | [diff] [blame] | 90 | if (compression != SkImage::CompressionType::kNone) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 91 | if (renderable == GrRenderable::kYes) { |
| 92 | return nullptr; |
| 93 | } |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 94 | auto size = SkCompressedDataSize(compression, dimensions, nullptr, false); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 95 | auto data = SkData::MakeUninitialized(size); |
| 96 | SkColor4f color = {0, 0, 0, 0}; |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 97 | GrFillInCompressedData(compression, dimensions, GrMipmapped::kNo, |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 98 | (char*)data->writable_data(), color); |
Robert Phillips | e4720c6 | 2020-01-14 14:33:24 -0500 | [diff] [blame] | 99 | return rp->createCompressedTexture(dimensions, format, SkBudgeted::kNo, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 100 | GrMipmapped::kNo, GrProtected::kNo, data.get()); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 101 | } else { |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 102 | return rp->createTexture(dimensions, format, renderable, 1, GrMipmapped::kNo, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 103 | SkBudgeted::kNo, GrProtected::kNo); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 104 | } |
| 105 | }; |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 106 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 107 | static constexpr SkISize kDims = {64, 64}; |
Robert Phillips | a5e78be | 2019-07-09 12:34:38 -0400 | [diff] [blame] | 108 | |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 109 | const std::vector<GrCaps::TestFormatColorTypeCombination>& combos = |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 110 | caps->getTestingCombinations(); |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 111 | |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 112 | for (const GrCaps::TestFormatColorTypeCombination& combo : combos) { |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 113 | |
| 114 | SkASSERT(combo.fColorType != GrColorType::kUnknown); |
| 115 | SkASSERT(combo.fFormat.isValid()); |
| 116 | |
| 117 | // Right now Vulkan has two backend formats that support ABGR_4444 (R4G4B4A4 and B4G4R4A4). |
| 118 | // Until we can create textures directly from the backend format this yields some |
| 119 | // ambiguity in what is actually supported and which textures can be created. |
| 120 | if (ctxInfo.backend() == kVulkan_GrBackend && combo.fColorType == GrColorType::kABGR_4444) { |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 124 | // Check if 'isFormatTexturable' agrees with 'createTexture' and that the mipmap |
| 125 | // support check is working |
| 126 | { |
| 127 | bool isCompressed = caps->isFormatCompressed(combo.fFormat); |
Brian Salomon | 469046c | 2020-03-30 14:21:04 -0400 | [diff] [blame] | 128 | bool isTexturable = caps->isFormatTexturable(combo.fFormat); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 129 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 130 | sk_sp<GrSurface> tex = createTexture(kDims, combo.fColorType, combo.fFormat, |
| 131 | GrRenderable::kNo, resourceProvider); |
| 132 | REPORTER_ASSERT(reporter, SkToBool(tex) == isTexturable, |
| 133 | "ct:%s format:%s, tex:%d, isTexturable:%d", |
| 134 | GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), |
| 135 | SkToBool(tex), isTexturable); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 136 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 137 | // Check that the lack of mipmap support blocks the creation of mipmapped |
| 138 | // proxies |
Brian Salomon | 69100f0 | 2020-07-21 10:49:25 -0400 | [diff] [blame] | 139 | bool expectedMipMapability = isTexturable && caps->mipmapSupport() && !isCompressed; |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 140 | |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 141 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 142 | combo.fFormat, kDims, GrRenderable::kNo, 1, GrMipmapped::kYes, |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 143 | SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo); |
| 144 | REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == expectedMipMapability, |
| 145 | "ct:%s format:%s, tex:%d, expectedMipMapability:%d", |
| 146 | GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), |
| 147 | SkToBool(proxy.get()), expectedMipMapability); |
| 148 | } |
| 149 | |
| 150 | // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' (w/o MSAA) |
| 151 | { |
| 152 | bool isRenderable = caps->isFormatRenderable(combo.fFormat, 1); |
| 153 | |
| 154 | sk_sp<GrSurface> tex = resourceProvider->createTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 155 | kDims, combo.fFormat, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBudgeted::kNo, |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 156 | GrProtected::kNo); |
| 157 | REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, |
| 158 | "ct:%s format:%s, tex:%d, isRenderable:%d", |
| 159 | GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), |
| 160 | SkToBool(tex), isRenderable); |
| 161 | } |
| 162 | |
| 163 | // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' w/ MSAA |
| 164 | { |
| 165 | bool isRenderable = caps->isFormatRenderable(combo.fFormat, 2); |
| 166 | |
| 167 | sk_sp<GrSurface> tex = resourceProvider->createTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 168 | kDims, combo.fFormat, GrRenderable::kYes, 2, GrMipmapped::kNo, SkBudgeted::kNo, |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 169 | GrProtected::kNo); |
| 170 | REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, |
| 171 | "ct:%s format:%s, tex:%d, isRenderable:%d", |
| 172 | GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), |
| 173 | SkToBool(tex), isRenderable); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 177 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 178 | #include "src/gpu/GrDrawingManager.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 179 | #include "src/gpu/GrSurfaceProxy.h" |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 180 | |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 181 | // For each context, set it to always clear the textures and then run through all the |
| 182 | // supported formats checking that the textures are actually cleared |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 183 | DEF_GPUTEST(InitialTextureClear, reporter, baseOptions) { |
| 184 | GrContextOptions options = baseOptions; |
| 185 | options.fClearAllTextures = true; |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 186 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 187 | static constexpr int kSize = 100; |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 188 | static constexpr SkColor kClearColor = 0xABABABAB; |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 189 | |
| 190 | const SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, |
| 191 | kPremul_SkAlphaType); |
| 192 | |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 193 | SkAutoPixmapStorage readback; |
| 194 | readback.alloc(info); |
| 195 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 196 | SkISize desc; |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 197 | desc.fWidth = desc.fHeight = kSize; |
| 198 | |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 199 | for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) { |
| 200 | sk_gpu_test::GrContextFactory factory(options); |
| 201 | auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct); |
| 202 | if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 203 | continue; |
| 204 | } |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 205 | auto dContext = factory.get(contextType); |
| 206 | if (!dContext) { |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 207 | continue; |
| 208 | } |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 209 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 210 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
| 211 | const GrCaps* caps = dContext->priv().caps(); |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 212 | |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 213 | const std::vector<GrCaps::TestFormatColorTypeCombination>& combos = |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 214 | caps->getTestingCombinations(); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 215 | |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 216 | for (const GrCaps::TestFormatColorTypeCombination& combo : combos) { |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 217 | |
| 218 | SkASSERT(combo.fColorType != GrColorType::kUnknown); |
| 219 | SkASSERT(combo.fFormat.isValid()); |
| 220 | |
Brian Salomon | 469046c | 2020-03-30 14:21:04 -0400 | [diff] [blame] | 221 | if (!caps->isFormatTexturable(combo.fFormat)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 222 | continue; |
| 223 | } |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 224 | |
Brian Salomon | 614cdab | 2019-09-26 11:04:28 -0400 | [diff] [blame] | 225 | auto checkColor = [reporter](const GrCaps::TestFormatColorTypeCombination& combo, |
| 226 | uint32_t readColor) { |
| 227 | // We expect that if there is no alpha in the src color type and we read it to a |
| 228 | // color type with alpha that we will get one for alpha rather than zero. We used to |
| 229 | // require this but the Intel Iris 6100 on Win 10 test bot doesn't put one in the |
| 230 | // alpha channel when reading back from GL_RG16 or GL_RG16F. So now we allow either. |
Brian Salomon | 2f23ae6 | 2020-03-26 16:17:56 -0400 | [diff] [blame] | 231 | uint32_t channels = GrColorTypeChannelFlags(combo.fColorType); |
| 232 | bool allowAlphaOne = !(channels & kAlpha_SkColorChannelFlag); |
Brian Salomon | 614cdab | 2019-09-26 11:04:28 -0400 | [diff] [blame] | 233 | if (allowAlphaOne) { |
| 234 | if (readColor != 0x00000000 && readColor != 0xFF000000) { |
| 235 | ERRORF(reporter, |
| 236 | "Failed on ct %s format %s 0x%08x is not 0x00000000 or 0xFF000000", |
| 237 | GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), |
| 238 | readColor); |
| 239 | return false; |
| 240 | } |
| 241 | } else { |
| 242 | if (readColor) { |
| 243 | ERRORF(reporter, "Failed on ct %s format %s 0x%08x != 0x00000000", |
| 244 | GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(), |
| 245 | readColor); |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | return true; |
| 250 | }; |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 251 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 252 | for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 253 | if (renderable == GrRenderable::kYes && |
Greg Daniel | 900583a | 2019-08-06 12:05:31 -0400 | [diff] [blame] | 254 | !caps->isFormatAsColorTypeRenderable(combo.fColorType, combo.fFormat)) { |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 255 | continue; |
| 256 | } |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 257 | |
| 258 | for (auto fit : {SkBackingFit::kApprox, SkBackingFit::kExact}) { |
| 259 | |
| 260 | // Does directly allocating a texture clear it? |
| 261 | { |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 262 | auto proxy = proxyProvider->testingOnly_createInstantiatedProxy( |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 263 | {kSize, kSize}, combo.fFormat, renderable, 1, fit, SkBudgeted::kYes, |
| 264 | GrProtected::kNo); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 265 | if (proxy) { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 266 | GrSwizzle swizzle = caps->getReadSwizzle(combo.fFormat, |
| 267 | combo.fColorType); |
| 268 | GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, |
| 269 | swizzle); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 270 | auto texCtx = GrSurfaceContext::Make(dContext, std::move(view), |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 271 | combo.fColorType, |
| 272 | kPremul_SkAlphaType, nullptr); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 273 | |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 274 | readback.erase(kClearColor); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 275 | if (texCtx->readPixels( |
| 276 | dContext, readback.info(), readback.writable_addr(), |
| 277 | readback.rowBytes(), {0, 0})) { |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 278 | for (int i = 0; i < kSize * kSize; ++i) { |
Brian Salomon | 614cdab | 2019-09-26 11:04:28 -0400 | [diff] [blame] | 279 | if (!checkColor(combo, readback.addr32()[i])) { |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 286 | dContext->priv().testingOnly_purgeAllUnlockedResources(); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Try creating the texture as a deferred proxy. |
| 290 | { |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 291 | std::unique_ptr<GrSurfaceContext> surfCtx; |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 292 | if (renderable == GrRenderable::kYes) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 293 | surfCtx = GrRenderTargetContext::Make( |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 294 | dContext, combo.fColorType, nullptr, fit, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 295 | {desc.fWidth, desc.fHeight}, 1, GrMipmapped::kNo, |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 296 | GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 297 | } else { |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 298 | surfCtx = GrSurfaceContext::Make( |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 299 | dContext, {desc.fWidth, desc.fHeight}, combo.fFormat, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 300 | GrRenderable::kNo, 1, GrMipmapped::kNo, GrProtected::kNo, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 301 | kTopLeft_GrSurfaceOrigin, combo.fColorType, |
| 302 | kUnknown_SkAlphaType, nullptr, fit, SkBudgeted::kYes); |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 303 | } |
| 304 | if (!surfCtx) { |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 305 | continue; |
| 306 | } |
Robert Phillips | ffe2729 | 2019-08-01 10:08:07 -0400 | [diff] [blame] | 307 | |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 308 | readback.erase(kClearColor); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 309 | if (surfCtx->readPixels(dContext, readback.info(), readback.writable_addr(), |
Robert Phillips | d344284 | 2019-08-02 12:26:22 -0400 | [diff] [blame] | 310 | readback.rowBytes(), {0, 0})) { |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 311 | for (int i = 0; i < kSize * kSize; ++i) { |
Brian Salomon | 614cdab | 2019-09-26 11:04:28 -0400 | [diff] [blame] | 312 | if (!checkColor(combo, readback.addr32()[i])) { |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 313 | break; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 314 | } |
| 315 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 316 | } |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 317 | dContext->priv().testingOnly_purgeAllUnlockedResources(); |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 318 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 324 | |
| 325 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) { |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 326 | auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 327 | for (int y = 0; y < p->height(); ++y) { |
| 328 | for (int x = 0; x < p->width(); ++x) { |
| 329 | *p->writable_addr32(x, y) = f(x, y); |
| 330 | } |
| 331 | } |
| 332 | }; |
| 333 | |
| 334 | auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) { |
| 335 | SkASSERT(p1.info() == p2.info()); |
| 336 | for (int y = 0; y < p1.height(); ++y) { |
| 337 | for (int x = 0; x < p1.width(); ++x) { |
| 338 | REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y)); |
| 339 | if (p1.getColor(x, y) != p2.getColor(x, y)) { |
| 340 | return; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | static constexpr int kSize = 100; |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 347 | SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 348 | SkAutoPixmapStorage srcPixmap; |
| 349 | srcPixmap.alloc(ii); |
| 350 | fillPixels(&srcPixmap, |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 351 | [](int x, int y) { |
| 352 | return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF); |
| 353 | }); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 354 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 355 | auto dContext = context_info.directContext(); |
| 356 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 357 | |
| 358 | // We test both kRW in addition to kRead mostly to ensure that the calls are structured such |
| 359 | // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with |
| 360 | // kRead for the right reason. |
| 361 | for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame^] | 362 | auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithData( |
| 363 | dContext, srcPixmap, GrRenderable::kNo, GrProtected::kNo); |
| 364 | auto proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership, |
| 365 | GrWrapCacheable::kNo, ioType, |
| 366 | mbet->refCountedCallback()); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 367 | GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), |
| 368 | GrColorType::kRGBA_8888); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 369 | GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 370 | auto surfContext = GrSurfaceContext::Make(dContext, std::move(view), GrColorType::kRGBA_8888, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 371 | kPremul_SkAlphaType, nullptr); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 372 | |
| 373 | // Read pixels should work with a read-only texture. |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 374 | { |
| 375 | SkAutoPixmapStorage read; |
| 376 | read.alloc(srcPixmap.info()); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 377 | auto readResult = surfContext->readPixels(dContext, srcPixmap.info(), |
| 378 | read.writable_addr(), 0, { 0, 0 }); |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 379 | REPORTER_ASSERT(reporter, readResult); |
| 380 | if (readResult) { |
| 381 | comparePixels(srcPixmap, read, reporter); |
| 382 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // Write pixels should not work with a read-only texture. |
| 386 | SkAutoPixmapStorage write; |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 387 | write.alloc(srcPixmap.info()); |
| 388 | fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); }); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 389 | auto writeResult = surfContext->writePixels(dContext, srcPixmap.info(), write.addr(), |
| 390 | 0, {0, 0}); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 391 | REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType)); |
| 392 | // Try the low level write. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 393 | dContext->flushAndSubmit(); |
| 394 | auto gpuWriteResult = dContext->priv().getGpu()->writePixels( |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 395 | proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888, |
| 396 | GrColorType::kRGBA_8888, write.addr32(), |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 397 | kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888)); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 398 | REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType)); |
| 399 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 400 | SkBitmap copySrcBitmap; |
| 401 | copySrcBitmap.installPixels(write); |
| 402 | copySrcBitmap.setImmutable(); |
| 403 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 404 | GrBitmapTextureMaker maker(dContext, copySrcBitmap, |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 405 | GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 406 | auto copySrc = maker.view(GrMipmapped::kNo); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 407 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 408 | REPORTER_ASSERT(reporter, copySrc.proxy()); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 409 | auto copyResult = surfContext->testCopy(copySrc.proxy()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 410 | REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType)); |
| 411 | // Try the low level copy. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 412 | dContext->flushAndSubmit(); |
| 413 | auto gpuCopyResult = dContext->priv().getGpu()->copySurface( |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 414 | proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize), |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 415 | {0, 0}); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 416 | REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType)); |
| 417 | |
| 418 | // Mip regen should not work with a read only texture. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 419 | if (dContext->priv().caps()->mipmapSupport()) { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame^] | 420 | mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(dContext, |
| 421 | kSize, |
| 422 | kSize, |
| 423 | kRGBA_8888_SkColorType, |
| 424 | GrMipmapped::kYes, |
| 425 | GrRenderable::kNo, |
| 426 | GrProtected::kNo); |
| 427 | proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership, |
| 428 | GrWrapCacheable::kNo, ioType, |
| 429 | mbet->refCountedCallback()); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 430 | dContext->flushAndSubmit(); |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 431 | proxy->peekTexture()->markMipmapsDirty(); // avoids assert in GrGpu. |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 432 | auto regenResult = |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 433 | dContext->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 434 | REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType)); |
| 435 | } |
| 436 | } |
| 437 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 438 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 439 | static const int kSurfSize = 10; |
| 440 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 441 | static sk_sp<GrTexture> make_wrapped_texture(GrDirectContext* dContext, GrRenderable renderable) { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame^] | 442 | auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData( |
| 443 | dContext, kSurfSize, kSurfSize, kRGBA_8888_SkColorType, GrMipmapped::kNo, renderable); |
| 444 | SkASSERT(mbet); |
| 445 | sk_sp<GrTextureProxy> proxy; |
| 446 | if (renderable == GrRenderable::kYes) { |
| 447 | proxy = dContext->priv().proxyProvider()->wrapRenderableBackendTexture( |
| 448 | mbet->texture(), 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, |
| 449 | mbet->refCountedCallback()); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 450 | } else { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame^] | 451 | proxy = dContext->priv().proxyProvider()->wrapBackendTexture( |
| 452 | mbet->texture(), kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType, |
| 453 | mbet->refCountedCallback()); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 454 | } |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame^] | 455 | if (!proxy) { |
| 456 | return nullptr; |
| 457 | } |
| 458 | return sk_ref_sp(proxy->peekTexture()); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 459 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 460 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 461 | static sk_sp<GrTexture> make_normal_texture(GrDirectContext* dContext, GrRenderable renderable) { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 462 | SkISize desc; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 463 | desc.fWidth = desc.fHeight = kSurfSize; |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 464 | auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 465 | renderable); |
| 466 | return dContext->priv().resourceProvider()->createTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 467 | desc, format, renderable, 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 468 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 469 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 470 | DEF_GPUTEST(TextureIdleProcTest, reporter, options) { |
| 471 | // Various ways of making textures. |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 472 | auto makeWrapped = [](GrDirectContext* dContext) { |
| 473 | return make_wrapped_texture(dContext, GrRenderable::kNo); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 474 | }; |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 475 | auto makeWrappedRenderable = [](GrDirectContext* dContext) { |
| 476 | return make_wrapped_texture(dContext, GrRenderable::kYes); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 477 | }; |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 478 | auto makeNormal = [](GrDirectContext* dContext) { |
| 479 | return make_normal_texture(dContext, GrRenderable::kNo); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 480 | }; |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 481 | auto makeRenderable = [](GrDirectContext* dContext) { |
| 482 | return make_normal_texture(dContext, GrRenderable::kYes); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 483 | }; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 484 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 485 | std::function<sk_sp<GrTexture>(GrDirectContext*)> makers[] = { |
| 486 | makeWrapped, |
| 487 | makeWrappedRenderable, |
| 488 | makeNormal, |
| 489 | makeRenderable |
| 490 | }; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 491 | |
| 492 | // Add a unique key, or not. |
| 493 | auto addKey = [](GrTexture* texture) { |
| 494 | static uint32_t gN = 0; |
| 495 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 496 | GrUniqueKey key; |
| 497 | GrUniqueKey::Builder builder(&key, kDomain, 1); |
| 498 | builder[0] = gN++; |
| 499 | builder.finish(); |
| 500 | texture->resourcePriv().setUniqueKey(key); |
| 501 | }; |
| 502 | auto dontAddKey = [](GrTexture* texture) {}; |
| 503 | std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey}; |
| 504 | |
| 505 | for (const auto& m : makers) { |
| 506 | for (const auto& keyAdder : keyAdders) { |
| 507 | for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) { |
| 508 | sk_gpu_test::GrContextFactory factory; |
| 509 | auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 510 | auto dContext = factory.get(contextType); |
| 511 | if (!dContext) { |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 512 | continue; |
| 513 | } |
| 514 | |
| 515 | // The callback we add simply adds an integer to a set. |
| 516 | std::set<int> idleIDs; |
| 517 | struct Context { |
| 518 | std::set<int>* fIdleIDs; |
| 519 | int fNum; |
| 520 | }; |
| 521 | auto proc = [](void* context) { |
| 522 | static_cast<Context*>(context)->fIdleIDs->insert( |
| 523 | static_cast<Context*>(context)->fNum); |
| 524 | delete static_cast<Context*>(context); |
| 525 | }; |
| 526 | |
| 527 | // Makes a texture, possibly adds a key, and sets the callback. |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 528 | auto make = [&m, &keyAdder, &proc, &idleIDs](GrDirectContext* dContext, int num) { |
| 529 | sk_sp<GrTexture> texture = m(dContext); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 530 | texture->addIdleProc(proc, new Context{&idleIDs, num}, |
| 531 | GrTexture::IdleState::kFinished); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 532 | keyAdder(texture.get()); |
| 533 | return texture; |
| 534 | }; |
| 535 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 536 | auto texture = make(dContext, 1); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 537 | REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end()); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 538 | auto renderable = GrRenderable(SkToBool(texture->asRenderTarget())); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 539 | auto backendFormat = texture->backendFormat(); |
| 540 | texture.reset(); |
| 541 | REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end()); |
| 542 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 543 | texture = make(dContext, 2); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 544 | int w = texture->width(); |
| 545 | int h = texture->height(); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 546 | SkImageInfo info = |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 547 | SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 548 | auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0, nullptr); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 549 | auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext(); |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 550 | auto singleUseLazyCB = [&texture](GrResourceProvider*, |
| 551 | const GrSurfaceProxy::LazySurfaceDesc&) { |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 552 | auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced; |
| 553 | if (texture->getUniqueKey().isValid()) { |
| 554 | mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced; |
| 555 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 556 | return GrSurfaceProxy::LazyCallbackResult{std::move(texture), true, mode}; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 557 | }; |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 558 | SkISize desc; |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 559 | desc.fWidth = w; |
| 560 | desc.fHeight = h; |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 561 | SkBudgeted budgeted; |
| 562 | if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) { |
| 563 | budgeted = SkBudgeted::kYes; |
| 564 | } else { |
| 565 | budgeted = SkBudgeted::kNo; |
| 566 | } |
Greg Daniel | c113f00 | 2020-08-26 13:28:22 -0400 | [diff] [blame] | 567 | sk_sp<GrSurfaceProxy> proxy; |
| 568 | if (renderable == GrRenderable::kYes) { |
| 569 | static const GrProxyProvider::TextureInfo kTexInfo = {GrMipMapped::kNo, |
| 570 | GrTextureType::k2D}; |
| 571 | proxy = dContext->priv().proxyProvider()->createLazyRenderTargetProxy( |
| 572 | singleUseLazyCB, backendFormat, desc, 1, |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 573 | dContext->priv().caps()->getExtraSurfaceFlagsForDeferredRT(), |
| 574 | &kTexInfo, |
Greg Daniel | c113f00 | 2020-08-26 13:28:22 -0400 | [diff] [blame] | 575 | GrMipmapStatus::kNotAllocated, |
| 576 | SkBackingFit::kExact, budgeted, GrProtected::kNo, false, |
| 577 | GrSurfaceProxy::UseAllocator::kYes); |
| 578 | } else { |
| 579 | proxy = dContext->priv().proxyProvider()->createLazyProxy( |
| 580 | singleUseLazyCB, backendFormat, desc, GrMipmapped::kNo, |
| 581 | GrMipmapStatus::kNotAllocated, GrInternalSurfaceFlags ::kNone, |
| 582 | SkBackingFit::kExact, budgeted, GrProtected::kNo, |
| 583 | GrSurfaceProxy::UseAllocator::kYes); |
| 584 | } |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 585 | GrSwizzle readSwizzle = dContext->priv().caps()->getReadSwizzle( |
Greg Daniel | ce3ddaa | 2020-01-22 16:58:15 -0500 | [diff] [blame] | 586 | backendFormat, GrColorType::kRGBA_8888); |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 587 | GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, readSwizzle); |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 588 | rtc->drawTexture(nullptr, |
| 589 | view, |
| 590 | kPremul_SkAlphaType, |
| 591 | GrSamplerState::Filter::kNearest, |
| 592 | GrSamplerState::MipmapMode::kNone, |
| 593 | SkBlendMode::kSrcOver, |
| 594 | SkPMColor4f(), |
| 595 | SkRect::MakeWH(w, h), |
| 596 | SkRect::MakeWH(w, h), |
| 597 | GrAA::kNo, |
| 598 | GrQuadAAFlags::kNone, |
| 599 | SkCanvas::kFast_SrcRectConstraint, |
| 600 | SkMatrix::I(), |
| 601 | nullptr); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 602 | // We still have the proxy, which should remain instantiated, thereby keeping the |
| 603 | // texture not purgeable. |
| 604 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 605 | dContext->flushAndSubmit(); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 606 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 607 | dContext->priv().getGpu()->testingOnly_flushGpuAndSync(); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 608 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
| 609 | |
| 610 | // This time we move the proxy into the draw. |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 611 | rtc->drawTexture(nullptr, |
| 612 | std::move(view), |
| 613 | kPremul_SkAlphaType, |
| 614 | GrSamplerState::Filter::kNearest, |
| 615 | GrSamplerState::MipmapMode::kNone, |
| 616 | SkBlendMode::kSrcOver, |
| 617 | SkPMColor4f(), |
| 618 | SkRect::MakeWH(w, h), |
| 619 | SkRect::MakeWH(w, h), |
| 620 | GrAA::kNo, |
| 621 | GrQuadAAFlags::kNone, |
| 622 | SkCanvas::kFast_SrcRectConstraint, |
| 623 | SkMatrix::I(), |
| 624 | nullptr); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 625 | REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end()); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 626 | dContext->flushAndSubmit(); |
| 627 | dContext->priv().getGpu()->testingOnly_flushGpuAndSync(); |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 628 | // Now that the draw is fully consumed by the GPU, the texture should be idle. |
| 629 | REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end()); |
| 630 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 631 | // Make sure we make the call during various shutdown scenarios where the texture |
| 632 | // might persist after context is destroyed, abandoned, etc. We test three |
| 633 | // variations of each scenario. One where the texture is just created. Another, |
| 634 | // where the texture has been used in a draw and then the context is flushed. And |
| 635 | // one where the the texture was drawn but the context is not flushed. |
| 636 | // 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] | 637 | |
Stephen White | b007bb2 | 2020-06-04 09:02:04 -0400 | [diff] [blame] | 638 | // These tests are difficult to get working with Vulkan, Direct3D, and Dawn. |
| 639 | // See http://skbug.com/8705, http://skbug.com/8277, and http://skbug.com/10326 |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 640 | GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType); |
Stephen White | b007bb2 | 2020-06-04 09:02:04 -0400 | [diff] [blame] | 641 | if (api == GrBackendApi::kVulkan || api == GrBackendApi::kDirect3D || |
| 642 | api == GrBackendApi::kDawn) { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 643 | continue; |
| 644 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 645 | int id = 3; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 646 | enum class DrawType { |
| 647 | kNoDraw, |
| 648 | kDraw, |
| 649 | kDrawAndFlush, |
| 650 | }; |
| 651 | for (auto drawType : |
| 652 | {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) { |
| 653 | for (bool unrefFirst : {false, true}) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 654 | auto possiblyDrawAndFlush = [&dContext, &texture, drawType, unrefFirst, w, |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 655 | h] { |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 656 | if (drawType == DrawType::kNoDraw) { |
| 657 | return; |
| 658 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 659 | SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 660 | kPremul_SkAlphaType); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 661 | auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0, |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 662 | nullptr); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 663 | auto rtc = rt->getCanvas() |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 664 | ->internal_private_accessTopLayerRenderTargetContext(); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 665 | auto proxy = dContext->priv().proxyProvider()->testingOnly_createWrapped( |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 666 | texture); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 667 | GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle( |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 668 | proxy->backendFormat(), GrColorType::kRGBA_8888); |
| 669 | GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, |
| 670 | swizzle); |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 671 | rtc->drawTexture(nullptr, |
| 672 | std::move(view), |
| 673 | kPremul_SkAlphaType, |
| 674 | GrSamplerState::Filter::kNearest, |
| 675 | GrSamplerState::MipmapMode::kNone, |
| 676 | SkBlendMode::kSrcOver, |
| 677 | SkPMColor4f(), |
| 678 | SkRect::MakeWH(w, h), |
| 679 | SkRect::MakeWH(w, h), |
| 680 | GrAA::kNo, |
| 681 | GrQuadAAFlags::kNone, |
| 682 | SkCanvas::kFast_SrcRectConstraint, |
| 683 | SkMatrix::I(), |
| 684 | nullptr); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 685 | if (drawType == DrawType::kDrawAndFlush) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 686 | dContext->flushAndSubmit(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 687 | } |
| 688 | if (unrefFirst) { |
| 689 | texture.reset(); |
| 690 | } |
| 691 | }; |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 692 | texture = make(dContext, id); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 693 | possiblyDrawAndFlush(); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 694 | dContext->abandonContext(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 695 | texture.reset(); |
| 696 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
| 697 | factory.destroyContexts(); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 698 | dContext = factory.get(contextType); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 699 | ++id; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 700 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 701 | // Similar to previous, but reset the texture after the context was |
| 702 | // abandoned and then destroyed. |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 703 | texture = make(dContext, id); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 704 | possiblyDrawAndFlush(); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 705 | dContext->abandonContext(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 706 | factory.destroyContexts(); |
| 707 | texture.reset(); |
| 708 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 709 | dContext = factory.get(contextType); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 710 | id++; |
| 711 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 712 | texture = make(dContext, id); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 713 | possiblyDrawAndFlush(); |
| 714 | factory.destroyContexts(); |
| 715 | texture.reset(); |
| 716 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 717 | dContext = factory.get(contextType); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 718 | id++; |
| 719 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 720 | texture = make(dContext, id); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 721 | possiblyDrawAndFlush(); |
| 722 | factory.releaseResourcesAndAbandonContexts(); |
| 723 | texture.reset(); |
| 724 | REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end()); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 725 | dContext = factory.get(contextType); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 726 | id++; |
| 727 | } |
| 728 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | } |
| 732 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 733 | |
| 734 | // Tests an idle proc that unrefs another resource down to zero. |
| 735 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 736 | auto context = contextInfo.directContext(); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 737 | |
| 738 | // idle proc that releases another texture. |
| 739 | auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); }; |
| 740 | |
| 741 | for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) { |
| 742 | for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 743 | for (auto idleState : |
| 744 | {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 745 | auto idleTexture = idleMaker(context, GrRenderable::kNo); |
| 746 | auto otherTexture = otherMaker(context, GrRenderable::kNo); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 747 | otherTexture->ref(); |
| 748 | idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState); |
| 749 | otherTexture.reset(); |
| 750 | idleTexture.reset(); |
| 751 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // Similar to above but more complicated. This flushes the context from the idle proc. |
| 757 | // crbug.com/933526. |
| 758 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 759 | auto dContext = contextInfo.directContext(); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 760 | |
| 761 | // idle proc that flushes the context. |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 762 | auto idleProc = [](void* context) { |
| 763 | reinterpret_cast<GrDirectContext*>(context)->flushAndSubmit(); |
| 764 | }; |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 765 | |
| 766 | for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 767 | for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 768 | auto idleTexture = idleMaker(dContext, GrRenderable::kNo); |
| 769 | idleTexture->addIdleProc(idleProc, dContext, idleState); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 770 | auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 771 | auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 1, nullptr); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 772 | // We'll draw two images to the canvas. One is a normal texture-backed image. The other |
| 773 | // is a wrapped-texture backed image. |
| 774 | surf->getCanvas()->clear(SK_ColorWHITE); |
| 775 | auto img1 = surf->makeImageSnapshot(); |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame^] | 776 | auto img2 = sk_gpu_test::MakeBackendTextureImage(dContext, info, SkColors::kBlack); |
| 777 | REPORTER_ASSERT(reporter, img1 && img2); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 778 | surf->getCanvas()->drawImage(std::move(img1), 0, 0); |
| 779 | surf->getCanvas()->drawImage(std::move(img2), 1, 1); |
| 780 | idleTexture.reset(); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 781 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
| 785 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 786 | auto context = contextInfo.directContext(); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 787 | // idle proc that refs the texture |
| 788 | auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); }; |
| 789 | // release proc to check whether the texture was released or not. |
| 790 | auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; }; |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 791 | for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) { |
| 792 | bool isReleased = false; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 793 | auto idleTexture = make_normal_texture(context, GrRenderable::kNo); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 794 | // This test assumes the texture won't be cached (or else the release proc doesn't get |
| 795 | // called). |
| 796 | idleTexture->resourcePriv().removeScratchKey(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 797 | context->flushAndSubmit(); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 798 | idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState); |
| 799 | idleTexture->setRelease(releaseProc, &isReleased); |
| 800 | auto* raw = idleTexture.get(); |
| 801 | idleTexture.reset(); |
| 802 | REPORTER_ASSERT(reporter, !isReleased); |
| 803 | raw->unref(); |
| 804 | REPORTER_ASSERT(reporter, isReleased); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 809 | auto context = contextInfo.directContext(); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 810 | for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 811 | auto idleTexture = idleMaker(context, GrRenderable::kNo); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 812 | |
| 813 | uint32_t flags = 0; |
| 814 | static constexpr uint32_t kFlushFlag = 0x1; |
| 815 | static constexpr uint32_t kFinishFlag = 0x2; |
| 816 | auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; }; |
| 817 | auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; }; |
| 818 | idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed); |
| 819 | idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished); |
| 820 | |
| 821 | // Insert a copy from idleTexture to another texture so that we have some queued IO on |
| 822 | // idleTexture. |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 823 | SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType, |
| 824 | kPremul_SkAlphaType); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 825 | auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr); |
| 826 | auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext(); |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 827 | auto proxy = |
| 828 | context->priv().proxyProvider()->testingOnly_createWrapped(std::move(idleTexture)); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 829 | context->flushAndSubmit(); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 830 | SkAssertResult(rtc->testCopy(proxy.get())); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 831 | proxy.reset(); |
| 832 | REPORTER_ASSERT(reporter, flags == 0); |
| 833 | |
| 834 | // After a flush we expect idleTexture to have reached the kFlushed state on all backends. |
| 835 | // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only |
| 836 | // current "unmanaged" backend, we *may* need a sync to reach kFinished. |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 837 | context->flushAndSubmit(); |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 838 | if (contextInfo.backend() == kVulkan_GrBackend) { |
| 839 | REPORTER_ASSERT(reporter, flags & kFlushFlag); |
| 840 | } else { |
| 841 | REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag)); |
| 842 | } |
| 843 | context->priv().getGpu()->testingOnly_flushGpuAndSync(); |
| 844 | REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag)); |
| 845 | } |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 846 | } |