blob: 4323ed6045dca08932aea075b1145196c68497d1 [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();
Robert Phillips6be756b2018-01-16 15:07:54 -050027 auto resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050028 GrGpu* gpu = context->contextPriv().getGpu();
Robert Phillips6be756b2018-01-16 15:07:54 -050029
kkinnunen15302832015-12-01 04:35:26 -080030 GrSurfaceDesc desc;
kkinnunen15302832015-12-01 04:35:26 -080031 desc.fFlags = kRenderTarget_GrSurfaceFlag;
32 desc.fWidth = 256;
33 desc.fHeight = 256;
Robert Phillips16d8ec62017-07-27 16:16:25 -040034 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -050035 desc.fSampleCnt = 1;
Robert Phillips6be756b2018-01-16 15:07:54 -050036 sk_sp<GrSurface> texRT1 = resourceProvider->createTexture(desc, SkBudgeted::kNo);
bsalomona2c23232014-11-25 07:41:12 -080037
Robert Phillipse78b7252017-04-06 07:59:41 -040038 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget());
39 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080040 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
41 texRT1->asTexture());
42 REPORTER_ASSERT(reporter, texRT1->asRenderTarget() ==
43 static_cast<GrSurface*>(texRT1->asTexture()));
44 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
45 static_cast<GrSurface*>(texRT1->asTexture()));
bsalomona2c23232014-11-25 07:41:12 -080046
kkinnunen15302832015-12-01 04:35:26 -080047 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillips6be756b2018-01-16 15:07:54 -050048 sk_sp<GrTexture> tex1 = resourceProvider->createTexture(desc, SkBudgeted::kNo);
kkinnunen15302832015-12-01 04:35:26 -080049 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040050 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
51 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000052
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050053 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -050054 nullptr, 256, 256, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080055
Brian Salomonbdecacf2018-02-02 20:32:49 -050056 sk_sp<GrSurface> texRT2 =
57 resourceProvider->wrapRenderableBackendTexture(backendTex, 1, kBorrow_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000058
bungeman6bd52842016-10-27 09:30:08 -070059 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
60 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080061 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
62 texRT2->asTexture());
63 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
64 static_cast<GrSurface*>(texRT2->asTexture()));
65 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
66 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000067
Brian Salomon26102cb2018-03-09 09:33:19 -050068 gpu->deleteTestingOnlyBackendTexture(backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000069}
70
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040071// This test checks that the isConfigTexturable and isConfigRenderable are
72// consistent with createTexture's result.
73DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
74 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050076 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Brian Salomonc7fe0f72018-05-11 10:14:21 -040077 const GrCaps* caps = context->contextPriv().caps();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040078
79 GrPixelConfig configs[] = {
80 kUnknown_GrPixelConfig,
81 kAlpha_8_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050082 kAlpha_8_as_Alpha_GrPixelConfig,
83 kAlpha_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040084 kGray_8_GrPixelConfig,
Greg Daniel7af060a2017-12-05 16:27:11 -050085 kGray_8_as_Lum_GrPixelConfig,
86 kGray_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040087 kRGB_565_GrPixelConfig,
88 kRGBA_4444_GrPixelConfig,
89 kRGBA_8888_GrPixelConfig,
Brian Salomon5fba7ad2018-03-22 10:01:16 -040090 kRGB_888_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040091 kBGRA_8888_GrPixelConfig,
92 kSRGBA_8888_GrPixelConfig,
93 kSBGRA_8888_GrPixelConfig,
Brian Osman10fc6fd2018-03-02 11:01:10 -050094 kRGBA_1010102_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040095 kRGBA_float_GrPixelConfig,
96 kRG_float_GrPixelConfig,
97 kAlpha_half_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050098 kAlpha_half_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040099 kRGBA_half_GrPixelConfig,
100 };
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400101 GR_STATIC_ASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs));
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400102
103 GrSurfaceDesc desc;
104 desc.fWidth = 64;
105 desc.fHeight = 64;
106
107 for (GrPixelConfig config : configs) {
108 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
109 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400110 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500111 desc.fSampleCnt = 1;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400112
Robert Phillips3b3307f2017-05-24 07:44:02 -0400113 sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomon1c80e992018-01-29 09:50:47 -0500114 bool ict = caps->isConfigTexturable(desc.fConfig);
115 REPORTER_ASSERT(reporter, SkToBool(tex) == ict,
116 "config:%d, tex:%d, isConfigTexturable:%d", config, SkToBool(tex), ict);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400117
Brian Salomon2a4f9832018-03-03 22:43:43 -0500118 sk_sp<GrTextureProxy> proxy =
119 proxyProvider->createMipMapProxy(desc, origin, SkBudgeted::kNo);
Brian Osman48c99192017-06-02 08:45:06 -0400120 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
121 (caps->isConfigTexturable(desc.fConfig) &&
Brian Salomon57111332018-02-05 15:55:54 -0500122 caps->mipMapSupport()));
Brian Osman48c99192017-06-02 08:45:06 -0400123
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400124 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400125 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500126 bool isRenderable = caps->isConfigRenderable(config);
127 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
128 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
129 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400130
Brian Salomonbdecacf2018-02-02 20:32:49 -0500131 desc.fSampleCnt = 2;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400132 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 isRenderable = SkToBool(caps->getRenderTargetSampleCount(2, config));
134 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
135 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
136 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400137 }
138 }
139}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400140
Brian Salomond17b4a62017-05-23 16:53:47 -0400141#include "GrDrawingManager.h"
142#include "GrSurfaceProxy.h"
143#include "GrTextureContext.h"
144
145DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
146 static constexpr int kSize = 100;
147 GrSurfaceDesc desc;
148 desc.fWidth = desc.fHeight = kSize;
149 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500150
Brian Salomond17b4a62017-05-23 16:53:47 -0400151 GrContext* context = context_info.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500152 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
153
Brian Salomond17b4a62017-05-23 16:53:47 -0400154 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
155 desc.fConfig = static_cast<GrPixelConfig>(c);
Brian Salomon366093f2018-02-13 09:25:22 -0500156 sk_sp<SkColorSpace> colorSpace;
157 if (GrPixelConfigIsSRGB(desc.fConfig)) {
158 colorSpace = SkColorSpace::MakeSRGB();
159 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400160 if (!context->contextPriv().caps()->isConfigTexturable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400161 continue;
162 }
163 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
164 for (bool rt : {false, true}) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400165 if (rt && !context->contextPriv().caps()->isConfigRenderable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400166 continue;
167 }
168 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400169 for (GrSurfaceOrigin origin :
170 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500171 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400172 // 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) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500175 sk_sp<GrTextureProxy> proxy = proxyProvider->createInstantiatedProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500176 desc, origin, fit, SkBudgeted::kYes);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500177 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400178 continue;
179 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400180 auto texCtx = context->contextPriv().makeWrappedSurfaceContext(
Brian Salomon366093f2018-02-13 09:25:22 -0500181 std::move(proxy), colorSpace);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400182 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 Salomond17b4a62017-05-23 16:53:47 -0400191 }
192 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400193 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400194 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 Daniel52cb5fe2017-09-05 15:45:15 -0400199 }
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400200 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500201 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400202
203 // Try creating the texture as a deferred proxy.
204 for (int i = 0; i < 2; ++i) {
205 auto surfCtx = context->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500206 desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes, colorSpace);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400207 if (!surfCtx) {
208 continue;
209 }
210 SkImageInfo info = SkImageInfo::Make(
211 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
212 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
213 if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) {
214 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
215 for (int i = 0; i < kSize * kSize; ++i) {
216 if (cmp != data.get()[i]) {
217 ERRORF(reporter, "Failed on config %d", desc.fConfig);
218 break;
219 }
220 }
221 }
222 // Here we overwrite the texture so that the second time through we
223 // test against recycling without reclearing.
224 if (0 == i) {
225 surfCtx->writePixels(info, data.get(), 0, 0, 0);
226 }
227 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500228 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Brian Salomond17b4a62017-05-23 16:53:47 -0400229 }
230 }
231 }
232 }
233}
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000234#endif