blob: eab930145c312e1a352c53b4d2c9e805e4e6ed8e [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();
28
kkinnunen15302832015-12-01 04:35:26 -080029 GrSurfaceDesc desc;
kkinnunen15302832015-12-01 04:35:26 -080030 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -040031 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
kkinnunen15302832015-12-01 04:35:26 -080032 desc.fWidth = 256;
33 desc.fHeight = 256;
Robert Phillips16d8ec62017-07-27 16:16:25 -040034 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080035 desc.fSampleCnt = 0;
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 Phillips16d8ec62017-07-27 16:16:25 -040048 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillips6be756b2018-01-16 15:07:54 -050049 sk_sp<GrTexture> tex1 = resourceProvider->createTexture(desc, SkBudgeted::kNo);
kkinnunen15302832015-12-01 04:35:26 -080050 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040051 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
52 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000053
Robert Phillipsd21b2a52017-12-12 13:01:25 -050054 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
55 nullptr, 256, 256, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080056
Robert Phillips6be756b2018-01-16 15:07:54 -050057 sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture(
58 backendTex, 0, kBorrow_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000059
bungeman6bd52842016-10-27 09:30:08 -070060 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
61 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080062 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
63 texRT2->asTexture());
64 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
65 static_cast<GrSurface*>(texRT2->asTexture()));
66 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
67 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000068
Robert Phillipsd21b2a52017-12-12 13:01:25 -050069 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000070}
71
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040072// This test checks that the isConfigTexturable and isConfigRenderable are
73// consistent with createTexture's result.
74DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
75 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050077 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040078 const GrCaps* caps = context->caps();
79
80 GrPixelConfig configs[] = {
81 kUnknown_GrPixelConfig,
82 kAlpha_8_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050083 kAlpha_8_as_Alpha_GrPixelConfig,
84 kAlpha_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040085 kGray_8_GrPixelConfig,
Greg Daniel7af060a2017-12-05 16:27:11 -050086 kGray_8_as_Lum_GrPixelConfig,
87 kGray_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040088 kRGB_565_GrPixelConfig,
89 kRGBA_4444_GrPixelConfig,
90 kRGBA_8888_GrPixelConfig,
91 kBGRA_8888_GrPixelConfig,
92 kSRGBA_8888_GrPixelConfig,
93 kSBGRA_8888_GrPixelConfig,
94 kRGBA_8888_sint_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 };
101 SkASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs));
102
103 GrSurfaceDesc desc;
104 desc.fWidth = 64;
105 desc.fHeight = 64;
106
Brian Osman48c99192017-06-02 08:45:06 -0400107 // Enough space for the first mip of our largest pixel config
108 const size_t pixelBufferSize = desc.fWidth * desc.fHeight *
109 GrBytesPerPixel(kRGBA_float_GrPixelConfig);
110 std::unique_ptr<char[]> pixelData(new char[pixelBufferSize]);
111 memset(pixelData.get(), 0, pixelBufferSize);
112
113 // We re-use the same mip level objects (with updated pointers and rowBytes) for each config
114 const int levelCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
115 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[levelCount]);
116
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400117 for (GrPixelConfig config : configs) {
118 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
119 desc.fFlags = kNone_GrSurfaceFlags;
120 desc.fOrigin = origin;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400121 desc.fConfig = config;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400122 desc.fSampleCnt = 0;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400123
Robert Phillips3b3307f2017-05-24 07:44:02 -0400124 sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
125 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigTexturable(desc.fConfig));
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400126
Brian Osman48c99192017-06-02 08:45:06 -0400127 size_t rowBytes = desc.fWidth * GrBytesPerPixel(desc.fConfig);
128 for (int i = 0; i < levelCount; ++i) {
129 texels[i].fPixels = pixelData.get();
130 texels[i].fRowBytes = rowBytes >> i;
131 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500132
133 sk_sp<GrTextureProxy> proxy = proxyProvider->createMipMapProxy(
134 desc, SkBudgeted::kNo,
135 texels.get(), levelCount);
Brian Osman48c99192017-06-02 08:45:06 -0400136 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
137 (caps->isConfigTexturable(desc.fConfig) &&
138 caps->mipMapSupport() &&
139 !GrPixelConfigIsSint(desc.fConfig)));
140
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400141 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400142 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400143 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, false));
144
145 desc.fSampleCnt = 4;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400146 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400147 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, true));
148 }
149 }
150}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400151
Brian Salomond17b4a62017-05-23 16:53:47 -0400152#include "GrDrawingManager.h"
153#include "GrSurfaceProxy.h"
154#include "GrTextureContext.h"
155
156DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
157 static constexpr int kSize = 100;
158 GrSurfaceDesc desc;
159 desc.fWidth = desc.fHeight = kSize;
160 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500161
Brian Salomond17b4a62017-05-23 16:53:47 -0400162 GrContext* context = context_info.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500163 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
164
Brian Salomond17b4a62017-05-23 16:53:47 -0400165 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
166 desc.fConfig = static_cast<GrPixelConfig>(c);
167 if (!context_info.grContext()->caps()->isConfigTexturable(desc.fConfig)) {
168 continue;
169 }
170 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
171 for (bool rt : {false, true}) {
172 if (rt && !context->caps()->isConfigRenderable(desc.fConfig, false)) {
173 continue;
174 }
175 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400176 for (GrSurfaceOrigin origin :
177 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
178 desc.fOrigin = origin;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500179 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400180 // Try directly creating the texture.
181 // Do this twice in an attempt to hit the cache on the second time through.
182 for (int i = 0; i < 2; ++i) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500183 sk_sp<GrTextureProxy> proxy = proxyProvider->createInstantiatedProxy(
184 desc, fit, SkBudgeted::kYes);
185 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400186 continue;
187 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500188
Greg Daniel90f28ec2017-09-25 12:26:58 -0400189 auto texCtx = context->contextPriv().makeWrappedSurfaceContext(
190 std::move(proxy), nullptr);
191 SkImageInfo info = SkImageInfo::Make(
192 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
193 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
194 if (texCtx->readPixels(info, data.get(), 0, 0, 0)) {
195 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
196 for (int i = 0; i < kSize * kSize; ++i) {
197 if (cmp != data.get()[i]) {
198 ERRORF(reporter, "Failed on config %d", desc.fConfig);
199 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400200 }
201 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400202 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400203 memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t));
204 // Here we overwrite the texture so that the second time through we
205 // test against recycling without reclearing.
206 if (0 == i) {
207 texCtx->writePixels(info, data.get(), 0, 0, 0);
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400208 }
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400209 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400210 context->purgeAllUnlockedResources();
211
212 // Try creating the texture as a deferred proxy.
213 for (int i = 0; i < 2; ++i) {
214 auto surfCtx = context->contextPriv().makeDeferredSurfaceContext(
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500215 desc, GrMipMapped::kNo, fit, SkBudgeted::kYes);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400216 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 Salomond17b4a62017-05-23 16:53:47 -0400238 }
239 }
240 }
241 }
242}
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000243#endif