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