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