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