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 | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 10 | #if SK_SUPPORT_GPU |
| 11 | |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 14 | #include "GrGpu.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 16 | #include "GrResourceProvider.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 17 | #include "GrTest.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 18 | #include "GrTexture.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 19 | #include "GrSurfacePriv.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 20 | #include "Test.h" |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 21 | |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 22 | // Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture |
| 23 | // and render targets to GrSurface all work as expected. |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 24 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrSurface, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 25 | GrContext* context = ctxInfo.grContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 26 | GrSurfaceDesc desc; |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 27 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 28 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 29 | desc.fWidth = 256; |
| 30 | desc.fHeight = 256; |
| 31 | desc.fSampleCnt = 0; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 32 | sk_sp<GrSurface> texRT1 = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 33 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget()); |
| 35 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 37 | texRT1->asTexture()); |
| 38 | REPORTER_ASSERT(reporter, texRT1->asRenderTarget() == |
| 39 | static_cast<GrSurface*>(texRT1->asTexture())); |
| 40 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 41 | static_cast<GrSurface*>(texRT1->asTexture())); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 42 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 43 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 44 | sk_sp<GrTexture> tex1 = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget()); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture()); |
| 47 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture()); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 48 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 49 | GrBackendObject backendTexHandle = context->getGpu()->createTestingOnlyBackendTexture( |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 50 | nullptr, 256, 256, kRGBA_8888_GrPixelConfig); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 51 | GrBackendTexture backendTex = GrTest::CreateBackendTexture(context->contextPriv().getBackend(), |
| 52 | 256, |
| 53 | 256, |
| 54 | kRGBA_8888_GrPixelConfig, |
| 55 | backendTexHandle); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 56 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 57 | sk_sp<GrSurface> texRT2 = context->resourceProvider()->wrapBackendTexture( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 58 | backendTex, kTopLeft_GrSurfaceOrigin, kRenderTarget_GrBackendTextureFlag, 0, |
| 59 | kBorrow_GrWrapOwnership); |
| 60 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget()); |
| 62 | REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 63 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 64 | texRT2->asTexture()); |
| 65 | REPORTER_ASSERT(reporter, texRT2->asRenderTarget() == |
| 66 | static_cast<GrSurface*>(texRT2->asTexture())); |
| 67 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) == |
| 68 | static_cast<GrSurface*>(texRT2->asTexture())); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 69 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 70 | context->getGpu()->deleteTestingOnlyBackendTexture(backendTexHandle); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 73 | #if 0 |
| 74 | // This test checks that the isConfigTexturable and isConfigRenderable are |
| 75 | // consistent with createTexture's result. |
| 76 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) { |
| 77 | GrContext* context = ctxInfo.grContext(); |
| 78 | const GrCaps* caps = context->caps(); |
| 79 | |
| 80 | GrPixelConfig configs[] = { |
| 81 | kUnknown_GrPixelConfig, |
| 82 | kAlpha_8_GrPixelConfig, |
| 83 | kGray_8_GrPixelConfig, |
| 84 | kRGB_565_GrPixelConfig, |
| 85 | kRGBA_4444_GrPixelConfig, |
| 86 | kRGBA_8888_GrPixelConfig, |
| 87 | kBGRA_8888_GrPixelConfig, |
| 88 | kSRGBA_8888_GrPixelConfig, |
| 89 | kSBGRA_8888_GrPixelConfig, |
| 90 | kRGBA_8888_sint_GrPixelConfig, |
| 91 | kETC1_GrPixelConfig, |
| 92 | kRGBA_float_GrPixelConfig, |
| 93 | kRG_float_GrPixelConfig, |
| 94 | kAlpha_half_GrPixelConfig, |
| 95 | kRGBA_half_GrPixelConfig, |
| 96 | }; |
| 97 | SkASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs)); |
| 98 | |
| 99 | GrSurfaceDesc desc; |
| 100 | desc.fWidth = 64; |
| 101 | desc.fHeight = 64; |
| 102 | |
| 103 | for (GrPixelConfig config : configs) { |
| 104 | for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
| 105 | desc.fFlags = kNone_GrSurfaceFlags; |
| 106 | desc.fOrigin = origin; |
| 107 | desc.fSampleCnt = 0; |
| 108 | desc.fConfig = config; |
| 109 | |
| 110 | sk_sp<GrSurface> tex = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo); |
| 111 | REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigTexturable(desc.fConfig, |
| 112 | desc.fOrigin)); |
| 113 | |
| 114 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 115 | tex = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo); |
| 116 | REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, false)); |
| 117 | |
| 118 | desc.fSampleCnt = 4; |
| 119 | tex = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo); |
| 120 | REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, true)); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | #endif |
| 125 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame^] | 126 | #include "GrDrawingManager.h" |
| 127 | #include "GrSurfaceProxy.h" |
| 128 | #include "GrTextureContext.h" |
| 129 | |
| 130 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) { |
| 131 | static constexpr int kSize = 100; |
| 132 | GrSurfaceDesc desc; |
| 133 | desc.fWidth = desc.fHeight = kSize; |
| 134 | std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]); |
| 135 | GrContext* context = context_info.grContext(); |
| 136 | for (int c = 0; c <= kLast_GrPixelConfig; ++c) { |
| 137 | desc.fConfig = static_cast<GrPixelConfig>(c); |
| 138 | if (!context_info.grContext()->caps()->isConfigTexturable(desc.fConfig)) { |
| 139 | continue; |
| 140 | } |
| 141 | desc.fFlags = kPerformInitialClear_GrSurfaceFlag; |
| 142 | for (bool rt : {false, true}) { |
| 143 | if (rt && !context->caps()->isConfigRenderable(desc.fConfig, false)) { |
| 144 | continue; |
| 145 | } |
| 146 | desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 147 | for (bool mipped : {false, true}) { |
| 148 | desc.fIsMipMapped = mipped; |
| 149 | for (GrSurfaceOrigin origin : |
| 150 | {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 151 | desc.fOrigin = origin; |
| 152 | for (bool approx : {false, true}) { |
| 153 | auto resourceProvider = context->resourceProvider(); |
| 154 | // Try directly creating the texture. |
| 155 | // Do this twice in an attempt to hit the cache on the second time through. |
| 156 | for (int i = 0; i < 2; ++i) { |
| 157 | sk_sp<GrTexture> tex; |
| 158 | if (approx) { |
| 159 | tex = sk_sp<GrTexture>( |
| 160 | resourceProvider->createApproxTexture(desc, 0)); |
| 161 | } else { |
| 162 | tex = resourceProvider->createTexture(desc, SkBudgeted::kYes); |
| 163 | } |
| 164 | if (!tex) { |
| 165 | continue; |
| 166 | } |
| 167 | auto proxy = GrSurfaceProxy::MakeWrapped(std::move(tex)); |
| 168 | auto texCtx = context->contextPriv().makeWrappedSurfaceContext( |
| 169 | std::move(proxy), nullptr); |
| 170 | SkImageInfo info = SkImageInfo::Make( |
| 171 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 172 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 173 | if (texCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 174 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 175 | for (int i = 0; i < kSize * kSize; ++i) { |
| 176 | if (cmp != data.get()[i]) { |
| 177 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t)); |
| 183 | // Here we overwrite the texture so that the second time through we |
| 184 | // test against recycling without reclearing. |
| 185 | if (0 == i) { |
| 186 | texCtx->writePixels(info, data.get(), 0, 0, 0); |
| 187 | } |
| 188 | } |
| 189 | context->purgeAllUnlockedResources(); |
| 190 | |
| 191 | // Try creating the texture as a deferred proxy. |
| 192 | for (int i = 0; i < 2; ++i) { |
| 193 | auto surfCtx = context->contextPriv().makeDeferredSurfaceContext( |
| 194 | desc, approx ? SkBackingFit::kApprox : SkBackingFit::kExact, |
| 195 | SkBudgeted::kYes); |
| 196 | if (!surfCtx) { |
| 197 | continue; |
| 198 | } |
| 199 | SkImageInfo info = SkImageInfo::Make( |
| 200 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 201 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 202 | if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 203 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 204 | for (int i = 0; i < kSize * kSize; ++i) { |
| 205 | if (cmp != data.get()[i]) { |
| 206 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | // Here we overwrite the texture so that the second time through we |
| 212 | // test against recycling without reclearing. |
| 213 | if (0 == i) { |
| 214 | surfCtx->writePixels(info, data.get(), 0, 0, 0); |
| 215 | } |
| 216 | } |
| 217 | context->purgeAllUnlockedResources(); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 224 | #endif |