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