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 | |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 8 | #include "GrContext.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 9 | #include "GrContextPriv.h" |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 10 | #include "GrGpu.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 11 | #include "GrProxyProvider.h" |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 12 | #include "GrRenderTarget.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 14 | #include "GrTexture.h" |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame^] | 15 | #include "GrTexturePriv.h" |
| 16 | #include "SkAutoPixmapStorage.h" |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 17 | #include "SkMipMap.h" |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame^] | 18 | #include "SkTypes.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 19 | #include "Test.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 20 | |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 21 | // Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture |
| 22 | // and render targets to GrSurface all work as expected. |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 23 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrSurface, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 24 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 25 | auto resourceProvider = context->contextPriv().resourceProvider(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 26 | GrGpu* gpu = context->contextPriv().getGpu(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 27 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 28 | GrSurfaceDesc desc; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 29 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 30 | desc.fWidth = 256; |
| 31 | desc.fHeight = 256; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 32 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 33 | desc.fSampleCnt = 1; |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 34 | sk_sp<GrSurface> texRT1 = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 35 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget()); |
| 37 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 39 | texRT1->asTexture()); |
| 40 | REPORTER_ASSERT(reporter, texRT1->asRenderTarget() == |
| 41 | static_cast<GrSurface*>(texRT1->asTexture())); |
| 42 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 43 | static_cast<GrSurface*>(texRT1->asTexture())); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 44 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 45 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 46 | sk_sp<GrTexture> tex1 = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget()); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture()); |
| 49 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture()); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 50 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 51 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 52 | nullptr, 256, 256, GrColorType::kRGBA_8888, false, GrMipMapped::kNo); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 53 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 54 | sk_sp<GrSurface> texRT2 = |
| 55 | resourceProvider->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 56 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget()); |
| 58 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 60 | texRT2->asTexture()); |
| 61 | REPORTER_ASSERT(reporter, texRT2->asRenderTarget() == |
| 62 | static_cast<GrSurface*>(texRT2->asTexture())); |
| 63 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 64 | static_cast<GrSurface*>(texRT2->asTexture())); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 65 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 66 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 69 | // This test checks that the isConfigTexturable and isConfigRenderable are |
| 70 | // consistent with createTexture's result. |
| 71 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) { |
| 72 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 73 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 74 | GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider(); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 75 | const GrCaps* caps = context->contextPriv().caps(); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 76 | |
| 77 | GrPixelConfig configs[] = { |
| 78 | kUnknown_GrPixelConfig, |
| 79 | kAlpha_8_GrPixelConfig, |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 80 | kAlpha_8_as_Alpha_GrPixelConfig, |
| 81 | kAlpha_8_as_Red_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 82 | kGray_8_GrPixelConfig, |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 83 | kGray_8_as_Lum_GrPixelConfig, |
| 84 | kGray_8_as_Red_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 85 | kRGB_565_GrPixelConfig, |
| 86 | kRGBA_4444_GrPixelConfig, |
| 87 | kRGBA_8888_GrPixelConfig, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 88 | kRGB_888_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 89 | kBGRA_8888_GrPixelConfig, |
| 90 | kSRGBA_8888_GrPixelConfig, |
| 91 | kSBGRA_8888_GrPixelConfig, |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 92 | kRGBA_1010102_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 93 | kRGBA_float_GrPixelConfig, |
| 94 | kRG_float_GrPixelConfig, |
| 95 | kAlpha_half_GrPixelConfig, |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 96 | kAlpha_half_as_Red_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 97 | kRGBA_half_GrPixelConfig, |
| 98 | }; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 99 | GR_STATIC_ASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs)); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 100 | |
| 101 | GrSurfaceDesc desc; |
| 102 | desc.fWidth = 64; |
| 103 | desc.fHeight = 64; |
| 104 | |
| 105 | for (GrPixelConfig config : configs) { |
| 106 | for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
| 107 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 108 | desc.fConfig = config; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 109 | desc.fSampleCnt = 1; |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 110 | |
Robert Phillips | 3b3307f | 2017-05-24 07:44:02 -0400 | [diff] [blame] | 111 | sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 112 | bool ict = caps->isConfigTexturable(desc.fConfig); |
| 113 | REPORTER_ASSERT(reporter, SkToBool(tex) == ict, |
| 114 | "config:%d, tex:%d, isConfigTexturable:%d", config, SkToBool(tex), ict); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 115 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 116 | GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo; |
| 117 | GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(config, &srgbEncoded); |
| 118 | const GrBackendFormat format = |
| 119 | caps->getBackendFormatFromGrColorType(colorType, srgbEncoded); |
| 120 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 121 | sk_sp<GrTextureProxy> proxy = |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 122 | proxyProvider->createMipMapProxy(format, desc, origin, SkBudgeted::kNo); |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == |
| 124 | (caps->isConfigTexturable(desc.fConfig) && |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 125 | caps->mipMapSupport())); |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 126 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 127 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 3b3307f | 2017-05-24 07:44:02 -0400 | [diff] [blame] | 128 | tex = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 129 | bool isRenderable = caps->isConfigRenderable(config); |
| 130 | REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, |
| 131 | "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex), |
| 132 | isRenderable); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 133 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 134 | desc.fSampleCnt = 2; |
Robert Phillips | 3b3307f | 2017-05-24 07:44:02 -0400 | [diff] [blame] | 135 | tex = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 136 | isRenderable = SkToBool(caps->getRenderTargetSampleCount(2, config)); |
| 137 | REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable, |
| 138 | "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex), |
| 139 | isRenderable); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 143 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 144 | #include "GrDrawingManager.h" |
| 145 | #include "GrSurfaceProxy.h" |
| 146 | #include "GrTextureContext.h" |
| 147 | |
| 148 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) { |
| 149 | static constexpr int kSize = 100; |
| 150 | GrSurfaceDesc desc; |
| 151 | desc.fWidth = desc.fHeight = kSize; |
| 152 | std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 153 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 154 | GrContext* context = context_info.grContext(); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 155 | const GrCaps* caps = context->contextPriv().caps(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 156 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 157 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 158 | for (int c = 0; c <= kLast_GrPixelConfig; ++c) { |
| 159 | desc.fConfig = static_cast<GrPixelConfig>(c); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 160 | if (!caps->isConfigTexturable(desc.fConfig)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 161 | continue; |
| 162 | } |
| 163 | desc.fFlags = kPerformInitialClear_GrSurfaceFlag; |
| 164 | for (bool rt : {false, true}) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 165 | if (rt && !caps->isConfigRenderable(desc.fConfig)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 166 | continue; |
| 167 | } |
| 168 | desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 169 | for (GrSurfaceOrigin origin : |
| 170 | {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 171 | for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) { |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 172 | // Try directly creating the texture. |
| 173 | // Do this twice in an attempt to hit the cache on the second time through. |
| 174 | for (int i = 0; i < 2; ++i) { |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 175 | auto proxy = proxyProvider->testingOnly_createInstantiatedProxy( |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 176 | desc, origin, fit, SkBudgeted::kYes); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 177 | if (!proxy) { |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 178 | continue; |
| 179 | } |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 180 | auto texCtx = context->contextPriv().makeWrappedSurfaceContext( |
Brian Osman | 9aa30c6 | 2018-07-02 15:21:46 -0400 | [diff] [blame] | 181 | std::move(proxy)); |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 182 | SkImageInfo info = SkImageInfo::Make( |
| 183 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 184 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 185 | if (texCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 186 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 187 | for (int i = 0; i < kSize * kSize; ++i) { |
| 188 | if (cmp != data.get()[i]) { |
| 189 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 190 | break; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 191 | } |
| 192 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 193 | } |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 194 | memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t)); |
| 195 | // Here we overwrite the texture so that the second time through we |
| 196 | // test against recycling without reclearing. |
| 197 | if (0 == i) { |
| 198 | texCtx->writePixels(info, data.get(), 0, 0, 0); |
Greg Daniel | 52cb5fe | 2017-09-05 15:45:15 -0400 | [diff] [blame] | 199 | } |
Greg Daniel | 52cb5fe | 2017-09-05 15:45:15 -0400 | [diff] [blame] | 200 | } |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 201 | context->contextPriv().purgeAllUnlockedResources_ForTesting(); |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 202 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 203 | GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo; |
| 204 | GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(desc.fConfig, |
| 205 | &srgbEncoded); |
| 206 | const GrBackendFormat format = |
| 207 | caps->getBackendFormatFromGrColorType(colorType, srgbEncoded); |
| 208 | |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 209 | // Try creating the texture as a deferred proxy. |
| 210 | for (int i = 0; i < 2; ++i) { |
| 211 | auto surfCtx = context->contextPriv().makeDeferredSurfaceContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 212 | format, desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes); |
Greg Daniel | 90f28ec | 2017-09-25 12:26:58 -0400 | [diff] [blame] | 213 | if (!surfCtx) { |
| 214 | continue; |
| 215 | } |
| 216 | SkImageInfo info = SkImageInfo::Make( |
| 217 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 218 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 219 | if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 220 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 221 | for (int i = 0; i < kSize * kSize; ++i) { |
| 222 | if (cmp != data.get()[i]) { |
| 223 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | // Here we overwrite the texture so that the second time through we |
| 229 | // test against recycling without reclearing. |
| 230 | if (0 == i) { |
| 231 | surfCtx->writePixels(info, data.get(), 0, 0, 0); |
| 232 | } |
| 233 | } |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 234 | context->contextPriv().purgeAllUnlockedResources_ForTesting(); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame^] | 240 | |
| 241 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) { |
| 242 | auto fillPixels = [](const SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) { |
| 243 | for (int y = 0; y < p->height(); ++y) { |
| 244 | for (int x = 0; x < p->width(); ++x) { |
| 245 | *p->writable_addr32(x, y) = f(x, y); |
| 246 | } |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) { |
| 251 | SkASSERT(p1.info() == p2.info()); |
| 252 | for (int y = 0; y < p1.height(); ++y) { |
| 253 | for (int x = 0; x < p1.width(); ++x) { |
| 254 | REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y)); |
| 255 | if (p1.getColor(x, y) != p2.getColor(x, y)) { |
| 256 | return; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | static constexpr int kSize = 100; |
| 263 | SkAutoPixmapStorage pixels; |
| 264 | pixels.alloc(SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 265 | fillPixels(&pixels, |
| 266 | [](int x, int y) { return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t(x * y); }); |
| 267 | |
| 268 | GrContext* context = context_info.grContext(); |
| 269 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 270 | |
| 271 | // We test both kRW in addition to kRead mostly to ensure that the calls are structured such |
| 272 | // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with |
| 273 | // kRead for the right reason. |
| 274 | for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) { |
| 275 | auto backendTex = context->contextPriv().getGpu()->createTestingOnlyBackendTexture( |
| 276 | pixels.addr(), kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kNo); |
| 277 | auto proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin, |
| 278 | kBorrow_GrWrapOwnership, ioType); |
| 279 | auto surfContext = context->contextPriv().makeWrappedSurfaceContext(proxy); |
| 280 | |
| 281 | // Read pixels should work with a read-only texture. |
| 282 | SkAutoPixmapStorage read; |
| 283 | read.alloc(pixels.info()); |
| 284 | auto readResult = surfContext->readPixels(pixels.info(), read.writable_addr(), 0, 0, 0); |
| 285 | REPORTER_ASSERT(reporter, readResult); |
| 286 | if (readResult) { |
| 287 | comparePixels(pixels, read, reporter); |
| 288 | } |
| 289 | |
| 290 | // Write pixels should not work with a read-only texture. |
| 291 | SkAutoPixmapStorage write; |
| 292 | write.alloc(pixels.info()); |
| 293 | fillPixels(&write, [&pixels](int x, int y) { return ~*pixels.addr32(); }); |
| 294 | auto writeResult = surfContext->writePixels(pixels.info(), pixels.addr(), 0, 0, 0); |
| 295 | REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType)); |
| 296 | // Try the low level write. |
| 297 | context->flush(); |
| 298 | auto gpuWriteResult = context->contextPriv().getGpu()->writePixels( |
| 299 | proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888, write.addr32(), |
| 300 | 0); |
| 301 | REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType)); |
| 302 | |
| 303 | // Copies should not work with a read-only texture |
| 304 | auto copySrc = proxyProvider->createTextureProxy( |
| 305 | SkImage::MakeFromRaster(write, nullptr, nullptr), kNone_GrSurfaceFlags, 1, |
| 306 | SkBudgeted::kYes, SkBackingFit::kExact); |
| 307 | REPORTER_ASSERT(reporter, copySrc); |
| 308 | auto copyResult = surfContext->copy(copySrc.get()); |
| 309 | REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType)); |
| 310 | // Try the low level copy. |
| 311 | context->flush(); |
| 312 | auto gpuCopyResult = context->contextPriv().getGpu()->copySurface( |
| 313 | proxy->peekTexture(), kTopLeft_GrSurfaceOrigin, copySrc->peekTexture(), |
| 314 | kTopLeft_GrSurfaceOrigin, SkIRect::MakeWH(kSize, kSize), {0, 0}); |
| 315 | REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType)); |
| 316 | |
| 317 | // Mip regen should not work with a read only texture. |
| 318 | if (context->contextPriv().caps()->mipMapSupport()) { |
| 319 | backendTex = context->contextPriv().getGpu()->createTestingOnlyBackendTexture( |
| 320 | nullptr, kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kYes); |
| 321 | proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin, |
| 322 | kBorrow_GrWrapOwnership, ioType); |
| 323 | context->flush(); |
| 324 | proxy->peekTexture()->texturePriv().markMipMapsDirty(); // avoids assert in GrGpu. |
| 325 | auto regenResult = |
| 326 | context->contextPriv().getGpu()->regenerateMipMapLevels(proxy->peekTexture()); |
| 327 | REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType)); |
| 328 | } |
| 329 | } |
| 330 | } |