blob: 582510c1b4f3ece6c3af960bf029f8a898a46bcf [file] [log] [blame]
bsalomon@google.com686bcb82013-04-09 15:04:12 +00001/*
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
bsalomona2c23232014-11-25 07:41:12 -08008#include "SkTypes.h"
9
bsalomon@google.com686bcb82013-04-09 15:04:12 +000010#if SK_SUPPORT_GPU
11
bsalomon@google.com686bcb82013-04-09 15:04:12 +000012#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrContextPriv.h"
bsalomon091f60c2015-11-10 11:54:56 -080014#include "GrGpu.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrProxyProvider.h"
Robert Phillips2890fbf2017-07-26 15:48:41 -040016#include "GrRenderTarget.h"
Brian Osman32342f02017-03-04 08:12:46 -050017#include "GrResourceProvider.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000018#include "GrTest.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000019#include "GrTexture.h"
Brian Osman48c99192017-06-02 08:45:06 -040020#include "SkMipMap.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000021#include "Test.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000022
bsalomona2c23232014-11-25 07:41:12 -080023// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
24// and render targets to GrSurface all work as expected.
bsalomon758586c2016-04-06 14:02:39 -070025DEF_GPUTEST_FOR_NULLGL_CONTEXT(GrSurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070026 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080027 GrSurfaceDesc desc;
kkinnunen15302832015-12-01 04:35:26 -080028 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -040029 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
kkinnunen15302832015-12-01 04:35:26 -080030 desc.fWidth = 256;
31 desc.fHeight = 256;
Robert Phillips16d8ec62017-07-27 16:16:25 -040032 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080033 desc.fSampleCnt = 0;
Robert Phillipse78b7252017-04-06 07:59:41 -040034 sk_sp<GrSurface> texRT1 = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo);
bsalomona2c23232014-11-25 07:41:12 -080035
Robert Phillipse78b7252017-04-06 07:59:41 -040036 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget());
37 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080038 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()));
bsalomona2c23232014-11-25 07:41:12 -080044
kkinnunen15302832015-12-01 04:35:26 -080045 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillips16d8ec62017-07-27 16:16:25 -040046 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillipse78b7252017-04-06 07:59:41 -040047 sk_sp<GrTexture> tex1 = context->resourceProvider()->createTexture(desc, SkBudgeted::kNo);
kkinnunen15302832015-12-01 04:35:26 -080048 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040049 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
50 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000051
Robert Phillipsd21b2a52017-12-12 13:01:25 -050052 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
53 nullptr, 256, 256, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080054
Brian Salomond17f6582017-07-19 18:28:58 -040055 sk_sp<GrSurface> texRT2 = context->resourceProvider()->wrapRenderableBackendTexture(
Robert Phillipsb0e93a22017-08-29 08:26:54 -040056 backendTex, 0, kBorrow_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000057
bungeman6bd52842016-10-27 09:30:08 -070058 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
59 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080060 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
61 texRT2->asTexture());
62 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
63 static_cast<GrSurface*>(texRT2->asTexture()));
64 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
65 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000066
Robert Phillipsd21b2a52017-12-12 13:01:25 -050067 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000068}
69
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040070// This test checks that the isConfigTexturable and isConfigRenderable are
71// consistent with createTexture's result.
72DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
73 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips3b3307f2017-05-24 07:44:02 -040075 GrResourceProvider* resourceProvider = context->resourceProvider();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040076 const GrCaps* caps = context->caps();
77
78 GrPixelConfig configs[] = {
79 kUnknown_GrPixelConfig,
80 kAlpha_8_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050081 kAlpha_8_as_Alpha_GrPixelConfig,
82 kAlpha_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040083 kGray_8_GrPixelConfig,
Greg Daniel7af060a2017-12-05 16:27:11 -050084 kGray_8_as_Lum_GrPixelConfig,
85 kGray_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040086 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 Phillipsb7b7e5f2017-05-22 13:23:19 -040093 kRGBA_float_GrPixelConfig,
94 kRG_float_GrPixelConfig,
95 kAlpha_half_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050096 kAlpha_half_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040097 kRGBA_half_GrPixelConfig,
98 };
99 SkASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs));
100
101 GrSurfaceDesc desc;
102 desc.fWidth = 64;
103 desc.fHeight = 64;
104
Brian Osman48c99192017-06-02 08:45:06 -0400105 // Enough space for the first mip of our largest pixel config
106 const size_t pixelBufferSize = desc.fWidth * desc.fHeight *
107 GrBytesPerPixel(kRGBA_float_GrPixelConfig);
108 std::unique_ptr<char[]> pixelData(new char[pixelBufferSize]);
109 memset(pixelData.get(), 0, pixelBufferSize);
110
111 // We re-use the same mip level objects (with updated pointers and rowBytes) for each config
112 const int levelCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
113 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[levelCount]);
114
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400115 for (GrPixelConfig config : configs) {
116 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
117 desc.fFlags = kNone_GrSurfaceFlags;
118 desc.fOrigin = origin;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400119 desc.fConfig = config;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400120 desc.fSampleCnt = 0;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400121
Robert Phillips3b3307f2017-05-24 07:44:02 -0400122 sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
123 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigTexturable(desc.fConfig));
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400124
Brian Osman48c99192017-06-02 08:45:06 -0400125 size_t rowBytes = desc.fWidth * GrBytesPerPixel(desc.fConfig);
126 for (int i = 0; i < levelCount; ++i) {
127 texels[i].fPixels = pixelData.get();
128 texels[i].fRowBytes = rowBytes >> i;
129 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500130
131 sk_sp<GrTextureProxy> proxy = proxyProvider->createMipMapProxy(
132 desc, SkBudgeted::kNo,
133 texels.get(), levelCount);
Brian Osman48c99192017-06-02 08:45:06 -0400134 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
135 (caps->isConfigTexturable(desc.fConfig) &&
136 caps->mipMapSupport() &&
137 !GrPixelConfigIsSint(desc.fConfig)));
138
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400139 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400140 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400141 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, false));
142
143 desc.fSampleCnt = 4;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400144 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400145 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, true));
146 }
147 }
148}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400149
Brian Salomond17b4a62017-05-23 16:53:47 -0400150#include "GrDrawingManager.h"
151#include "GrSurfaceProxy.h"
152#include "GrTextureContext.h"
153
154DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
155 static constexpr int kSize = 100;
156 GrSurfaceDesc desc;
157 desc.fWidth = desc.fHeight = kSize;
158 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500159
Brian Salomond17b4a62017-05-23 16:53:47 -0400160 GrContext* context = context_info.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500161 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
162
Brian Salomond17b4a62017-05-23 16:53:47 -0400163 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
164 desc.fConfig = static_cast<GrPixelConfig>(c);
165 if (!context_info.grContext()->caps()->isConfigTexturable(desc.fConfig)) {
166 continue;
167 }
168 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
169 for (bool rt : {false, true}) {
170 if (rt && !context->caps()->isConfigRenderable(desc.fConfig, false)) {
171 continue;
172 }
173 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400174 for (GrSurfaceOrigin origin :
175 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
176 desc.fOrigin = origin;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500177 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400178 // Try directly creating the texture.
179 // Do this twice in an attempt to hit the cache on the second time through.
180 for (int i = 0; i < 2; ++i) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500181 sk_sp<GrTextureProxy> proxy = proxyProvider->createInstantiatedProxy(
182 desc, fit, SkBudgeted::kYes);
183 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400184 continue;
185 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500186
Greg Daniel90f28ec2017-09-25 12:26:58 -0400187 auto texCtx = context->contextPriv().makeWrappedSurfaceContext(
188 std::move(proxy), nullptr);
189 SkImageInfo info = SkImageInfo::Make(
190 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
191 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
192 if (texCtx->readPixels(info, data.get(), 0, 0, 0)) {
193 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
194 for (int i = 0; i < kSize * kSize; ++i) {
195 if (cmp != data.get()[i]) {
196 ERRORF(reporter, "Failed on config %d", desc.fConfig);
197 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400198 }
199 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400200 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400201 memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t));
202 // Here we overwrite the texture so that the second time through we
203 // test against recycling without reclearing.
204 if (0 == i) {
205 texCtx->writePixels(info, data.get(), 0, 0, 0);
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400206 }
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400207 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400208 context->purgeAllUnlockedResources();
209
210 // Try creating the texture as a deferred proxy.
211 for (int i = 0; i < 2; ++i) {
212 auto surfCtx = context->contextPriv().makeDeferredSurfaceContext(
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500213 desc, GrMipMapped::kNo, fit, SkBudgeted::kYes);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400214 if (!surfCtx) {
215 continue;
216 }
217 SkImageInfo info = SkImageInfo::Make(
218 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
219 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
220 if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) {
221 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
222 for (int i = 0; i < kSize * kSize; ++i) {
223 if (cmp != data.get()[i]) {
224 ERRORF(reporter, "Failed on config %d", desc.fConfig);
225 break;
226 }
227 }
228 }
229 // Here we overwrite the texture so that the second time through we
230 // test against recycling without reclearing.
231 if (0 == i) {
232 surfCtx->writePixels(info, data.get(), 0, 0, 0);
233 }
234 }
235 context->purgeAllUnlockedResources();
Brian Salomond17b4a62017-05-23 16:53:47 -0400236 }
237 }
238 }
239 }
240}
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000241#endif