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" |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [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" |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 19 | #include "SkMipMap.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; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 27 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 28 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 29 | desc.fWidth = 256; |
| 30 | desc.fHeight = 256; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 31 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 32 | desc.fSampleCnt = 0; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 33 | sk_sp<GrSurface> texRT1 = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 34 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget()); |
| 36 | REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 38 | texRT1->asTexture()); |
| 39 | REPORTER_ASSERT(reporter, texRT1->asRenderTarget() == |
| 40 | static_cast<GrSurface*>(texRT1->asTexture())); |
| 41 | REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) == |
| 42 | static_cast<GrSurface*>(texRT1->asTexture())); |
bsalomon | a2c2323 | 2014-11-25 07:41:12 -0800 | [diff] [blame] | 43 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 44 | desc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 45 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 46 | sk_sp<GrTexture> tex1 = context->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 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 51 | GrBackendObject backendTexHandle = context->getGpu()->createTestingOnlyBackendTexture( |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 52 | nullptr, 256, 256, kRGBA_8888_GrPixelConfig); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 53 | GrBackendTexture backendTex = GrTest::CreateBackendTexture(context->contextPriv().getBackend(), |
| 54 | 256, |
| 55 | 256, |
| 56 | kRGBA_8888_GrPixelConfig, |
| 57 | backendTexHandle); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 58 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 59 | sk_sp<GrSurface> texRT2 = context->resourceProvider()->wrapRenderableBackendTexture( |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 60 | backendTex, 0, kBorrow_GrWrapOwnership); |
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 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 71 | context->getGpu()->deleteTestingOnlyBackendTexture(backendTexHandle); |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 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(); |
Robert Phillips | 3b3307f | 2017-05-24 07:44:02 -0400 | [diff] [blame] | 78 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 79 | const GrCaps* caps = context->caps(); |
| 80 | |
| 81 | GrPixelConfig configs[] = { |
| 82 | kUnknown_GrPixelConfig, |
| 83 | kAlpha_8_GrPixelConfig, |
| 84 | kGray_8_GrPixelConfig, |
| 85 | kRGB_565_GrPixelConfig, |
| 86 | kRGBA_4444_GrPixelConfig, |
| 87 | kRGBA_8888_GrPixelConfig, |
| 88 | kBGRA_8888_GrPixelConfig, |
| 89 | kSRGBA_8888_GrPixelConfig, |
| 90 | kSBGRA_8888_GrPixelConfig, |
| 91 | kRGBA_8888_sint_GrPixelConfig, |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 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 | |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 103 | // Enough space for the first mip of our largest pixel config |
| 104 | const size_t pixelBufferSize = desc.fWidth * desc.fHeight * |
| 105 | GrBytesPerPixel(kRGBA_float_GrPixelConfig); |
| 106 | std::unique_ptr<char[]> pixelData(new char[pixelBufferSize]); |
| 107 | memset(pixelData.get(), 0, pixelBufferSize); |
| 108 | |
| 109 | // We re-use the same mip level objects (with updated pointers and rowBytes) for each config |
| 110 | const int levelCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1; |
| 111 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[levelCount]); |
| 112 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 113 | for (GrPixelConfig config : configs) { |
| 114 | for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
| 115 | desc.fFlags = kNone_GrSurfaceFlags; |
| 116 | desc.fOrigin = origin; |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 117 | desc.fConfig = config; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 118 | desc.fSampleCnt = 0; |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 119 | |
Robert Phillips | 3b3307f | 2017-05-24 07:44:02 -0400 | [diff] [blame] | 120 | sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
| 121 | REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigTexturable(desc.fConfig)); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 122 | |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 123 | size_t rowBytes = desc.fWidth * GrBytesPerPixel(desc.fConfig); |
| 124 | for (int i = 0; i < levelCount; ++i) { |
| 125 | texels[i].fPixels = pixelData.get(); |
| 126 | texels[i].fRowBytes = rowBytes >> i; |
| 127 | } |
Robert Phillips | 8e8c755 | 2017-07-10 12:06:05 -0400 | [diff] [blame] | 128 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferredMipMap(resourceProvider, |
| 129 | desc, SkBudgeted::kNo, |
| 130 | texels.get(), |
| 131 | levelCount); |
Brian Osman | 48c9919 | 2017-06-02 08:45:06 -0400 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == |
| 133 | (caps->isConfigTexturable(desc.fConfig) && |
| 134 | caps->mipMapSupport() && |
| 135 | !GrPixelConfigIsSint(desc.fConfig))); |
| 136 | |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 137 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 3b3307f | 2017-05-24 07:44:02 -0400 | [diff] [blame] | 138 | tex = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, false)); |
| 140 | |
| 141 | desc.fSampleCnt = 4; |
Robert Phillips | 3b3307f | 2017-05-24 07:44:02 -0400 | [diff] [blame] | 142 | tex = resourceProvider->createTexture(desc, SkBudgeted::kNo); |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, true)); |
| 144 | } |
| 145 | } |
| 146 | } |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 147 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 148 | #include "GrDrawingManager.h" |
| 149 | #include "GrSurfaceProxy.h" |
| 150 | #include "GrTextureContext.h" |
| 151 | |
| 152 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) { |
| 153 | static constexpr int kSize = 100; |
| 154 | GrSurfaceDesc desc; |
| 155 | desc.fWidth = desc.fHeight = kSize; |
| 156 | std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]); |
| 157 | GrContext* context = context_info.grContext(); |
| 158 | for (int c = 0; c <= kLast_GrPixelConfig; ++c) { |
| 159 | desc.fConfig = static_cast<GrPixelConfig>(c); |
| 160 | if (!context_info.grContext()->caps()->isConfigTexturable(desc.fConfig)) { |
| 161 | continue; |
| 162 | } |
| 163 | desc.fFlags = kPerformInitialClear_GrSurfaceFlag; |
| 164 | for (bool rt : {false, true}) { |
| 165 | if (rt && !context->caps()->isConfigRenderable(desc.fConfig, false)) { |
| 166 | continue; |
| 167 | } |
| 168 | desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
Greg Daniel | 52cb5fe | 2017-09-05 15:45:15 -0400 | [diff] [blame^] | 169 | for (GrSurfaceOrigin origin : |
| 170 | {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 171 | desc.fOrigin = origin; |
| 172 | for (bool approx : {false, true}) { |
| 173 | auto resourceProvider = context->resourceProvider(); |
| 174 | // Try directly creating the texture. |
| 175 | // Do this twice in an attempt to hit the cache on the second time through. |
| 176 | for (int i = 0; i < 2; ++i) { |
| 177 | sk_sp<GrTexture> tex; |
| 178 | if (approx) { |
| 179 | tex = sk_sp<GrTexture>( |
| 180 | resourceProvider->createApproxTexture(desc, 0)); |
| 181 | } else { |
| 182 | tex = resourceProvider->createTexture(desc, SkBudgeted::kYes); |
| 183 | } |
| 184 | if (!tex) { |
| 185 | continue; |
| 186 | } |
| 187 | auto proxy = GrSurfaceProxy::MakeWrapped(std::move(tex), desc.fOrigin); |
| 188 | auto texCtx = context->contextPriv().makeWrappedSurfaceContext( |
| 189 | std::move(proxy), nullptr); |
| 190 | SkImageInfo info = SkImageInfo::Make( |
| 191 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 192 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 193 | if (texCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 194 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 195 | for (int i = 0; i < kSize * kSize; ++i) { |
| 196 | if (cmp != data.get()[i]) { |
| 197 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 198 | break; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 199 | } |
| 200 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 201 | } |
Greg Daniel | 52cb5fe | 2017-09-05 15:45:15 -0400 | [diff] [blame^] | 202 | memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t)); |
| 203 | // Here we overwrite the texture so that the second time through we |
| 204 | // test against recycling without reclearing. |
| 205 | if (0 == i) { |
| 206 | texCtx->writePixels(info, data.get(), 0, 0, 0); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 207 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 208 | } |
Greg Daniel | 52cb5fe | 2017-09-05 15:45:15 -0400 | [diff] [blame^] | 209 | context->purgeAllUnlockedResources(); |
| 210 | |
| 211 | // Try creating the texture as a deferred proxy. |
| 212 | for (int i = 0; i < 2; ++i) { |
| 213 | auto surfCtx = context->contextPriv().makeDeferredSurfaceContext( |
| 214 | desc, approx ? SkBackingFit::kApprox : SkBackingFit::kExact, |
| 215 | SkBudgeted::kYes); |
| 216 | if (!surfCtx) { |
| 217 | continue; |
| 218 | } |
| 219 | SkImageInfo info = SkImageInfo::Make( |
| 220 | kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 221 | memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t)); |
| 222 | if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) { |
| 223 | uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0; |
| 224 | for (int i = 0; i < kSize * kSize; ++i) { |
| 225 | if (cmp != data.get()[i]) { |
| 226 | ERRORF(reporter, "Failed on config %d", desc.fConfig); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | // Here we overwrite the texture so that the second time through we |
| 232 | // test against recycling without reclearing. |
| 233 | if (0 == i) { |
| 234 | surfCtx->writePixels(info, data.get(), 0, 0, 0); |
| 235 | } |
| 236 | } |
| 237 | context->purgeAllUnlockedResources(); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
bsalomon@google.com | 686bcb8 | 2013-04-09 15:04:12 +0000 | [diff] [blame] | 243 | #endif |