blob: 72a92a709d64ab7e4fc7bd2afd29b0acb1033111 [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;
Robert Phillips16d8ec62017-07-27 16:16:25 -040032 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
kkinnunen15302832015-12-01 04:35:26 -080033 desc.fWidth = 256;
34 desc.fHeight = 256;
Robert Phillips16d8ec62017-07-27 16:16:25 -040035 desc.fConfig = kRGBA_8888_GrPixelConfig;
kkinnunen15302832015-12-01 04:35:26 -080036 desc.fSampleCnt = 0;
Robert Phillips6be756b2018-01-16 15:07:54 -050037 sk_sp<GrSurface> texRT1 = resourceProvider->createTexture(desc, SkBudgeted::kNo);
bsalomona2c23232014-11-25 07:41:12 -080038
Robert Phillipse78b7252017-04-06 07:59:41 -040039 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget());
40 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080041 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
42 texRT1->asTexture());
43 REPORTER_ASSERT(reporter, texRT1->asRenderTarget() ==
44 static_cast<GrSurface*>(texRT1->asTexture()));
45 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
46 static_cast<GrSurface*>(texRT1->asTexture()));
bsalomona2c23232014-11-25 07:41:12 -080047
kkinnunen15302832015-12-01 04:35:26 -080048 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillips16d8ec62017-07-27 16:16:25 -040049 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillips6be756b2018-01-16 15:07:54 -050050 sk_sp<GrTexture> tex1 = resourceProvider->createTexture(desc, SkBudgeted::kNo);
kkinnunen15302832015-12-01 04:35:26 -080051 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040052 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
53 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000054
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050055 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -050056 nullptr, 256, 256, kRGBA_8888_GrPixelConfig, false, GrMipMapped::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080057
Robert Phillips6be756b2018-01-16 15:07:54 -050058 sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture(
59 backendTex, 0, kBorrow_GrWrapOwnership);
Greg Daniel7ef28f32017-04-20 16:41:55 +000060
bungeman6bd52842016-10-27 09:30:08 -070061 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
62 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080063 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
64 texRT2->asTexture());
65 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
66 static_cast<GrSurface*>(texRT2->asTexture()));
67 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
68 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000069
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050070 gpu->deleteTestingOnlyBackendTexture(&backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000071}
72
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040073// This test checks that the isConfigTexturable and isConfigRenderable are
74// consistent with createTexture's result.
75DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
76 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050077 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050078 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040079 const GrCaps* caps = context->caps();
80
81 GrPixelConfig configs[] = {
82 kUnknown_GrPixelConfig,
83 kAlpha_8_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050084 kAlpha_8_as_Alpha_GrPixelConfig,
85 kAlpha_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040086 kGray_8_GrPixelConfig,
Greg Daniel7af060a2017-12-05 16:27:11 -050087 kGray_8_as_Lum_GrPixelConfig,
88 kGray_8_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040089 kRGB_565_GrPixelConfig,
90 kRGBA_4444_GrPixelConfig,
91 kRGBA_8888_GrPixelConfig,
92 kBGRA_8888_GrPixelConfig,
93 kSRGBA_8888_GrPixelConfig,
94 kSBGRA_8888_GrPixelConfig,
95 kRGBA_8888_sint_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040096 kRGBA_float_GrPixelConfig,
97 kRG_float_GrPixelConfig,
98 kAlpha_half_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -050099 kAlpha_half_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400100 kRGBA_half_GrPixelConfig,
101 };
102 SkASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs));
103
104 GrSurfaceDesc desc;
105 desc.fWidth = 64;
106 desc.fHeight = 64;
107
Brian Osman48c99192017-06-02 08:45:06 -0400108 // Enough space for the first mip of our largest pixel config
109 const size_t pixelBufferSize = desc.fWidth * desc.fHeight *
110 GrBytesPerPixel(kRGBA_float_GrPixelConfig);
111 std::unique_ptr<char[]> pixelData(new char[pixelBufferSize]);
112 memset(pixelData.get(), 0, pixelBufferSize);
113
114 // We re-use the same mip level objects (with updated pointers and rowBytes) for each config
115 const int levelCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
116 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[levelCount]);
117
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400118 for (GrPixelConfig config : configs) {
119 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
120 desc.fFlags = kNone_GrSurfaceFlags;
121 desc.fOrigin = origin;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400122 desc.fConfig = config;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400123 desc.fSampleCnt = 0;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400124
Robert Phillips3b3307f2017-05-24 07:44:02 -0400125 sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
126 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigTexturable(desc.fConfig));
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400127
Brian Osman48c99192017-06-02 08:45:06 -0400128 size_t rowBytes = desc.fWidth * GrBytesPerPixel(desc.fConfig);
129 for (int i = 0; i < levelCount; ++i) {
130 texels[i].fPixels = pixelData.get();
131 texels[i].fRowBytes = rowBytes >> i;
132 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500133
134 sk_sp<GrTextureProxy> proxy = proxyProvider->createMipMapProxy(
135 desc, SkBudgeted::kNo,
136 texels.get(), levelCount);
Brian Osman48c99192017-06-02 08:45:06 -0400137 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
138 (caps->isConfigTexturable(desc.fConfig) &&
139 caps->mipMapSupport() &&
140 !GrPixelConfigIsSint(desc.fConfig)));
141
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400142 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400143 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400144 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, false));
145
146 desc.fSampleCnt = 4;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400147 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400148 REPORTER_ASSERT(reporter, SkToBool(tex.get()) == caps->isConfigRenderable(config, true));
149 }
150 }
151}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400152
Brian Salomond17b4a62017-05-23 16:53:47 -0400153#include "GrDrawingManager.h"
154#include "GrSurfaceProxy.h"
155#include "GrTextureContext.h"
156
157DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
158 static constexpr int kSize = 100;
159 GrSurfaceDesc desc;
160 desc.fWidth = desc.fHeight = kSize;
161 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500162
Brian Salomond17b4a62017-05-23 16:53:47 -0400163 GrContext* context = context_info.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500164 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
165
Brian Salomond17b4a62017-05-23 16:53:47 -0400166 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
167 desc.fConfig = static_cast<GrPixelConfig>(c);
168 if (!context_info.grContext()->caps()->isConfigTexturable(desc.fConfig)) {
169 continue;
170 }
171 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
172 for (bool rt : {false, true}) {
173 if (rt && !context->caps()->isConfigRenderable(desc.fConfig, false)) {
174 continue;
175 }
176 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400177 for (GrSurfaceOrigin origin :
178 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
179 desc.fOrigin = origin;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500180 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400181 // Try directly creating the texture.
182 // Do this twice in an attempt to hit the cache on the second time through.
183 for (int i = 0; i < 2; ++i) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500184 sk_sp<GrTextureProxy> proxy = proxyProvider->createInstantiatedProxy(
185 desc, fit, SkBudgeted::kYes);
186 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400187 continue;
188 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500189
Greg Daniel90f28ec2017-09-25 12:26:58 -0400190 auto texCtx = context->contextPriv().makeWrappedSurfaceContext(
191 std::move(proxy), nullptr);
192 SkImageInfo info = SkImageInfo::Make(
193 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
194 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
195 if (texCtx->readPixels(info, data.get(), 0, 0, 0)) {
196 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
197 for (int i = 0; i < kSize * kSize; ++i) {
198 if (cmp != data.get()[i]) {
199 ERRORF(reporter, "Failed on config %d", desc.fConfig);
200 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400201 }
202 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400203 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400204 memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t));
205 // Here we overwrite the texture so that the second time through we
206 // test against recycling without reclearing.
207 if (0 == i) {
208 texCtx->writePixels(info, data.get(), 0, 0, 0);
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400209 }
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400210 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400211 context->purgeAllUnlockedResources();
212
213 // Try creating the texture as a deferred proxy.
214 for (int i = 0; i < 2; ++i) {
215 auto surfCtx = context->contextPriv().makeDeferredSurfaceContext(
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500216 desc, GrMipMapped::kNo, fit, SkBudgeted::kYes);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400217 if (!surfCtx) {
218 continue;
219 }
220 SkImageInfo info = SkImageInfo::Make(
221 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
222 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
223 if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) {
224 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
225 for (int i = 0; i < kSize * kSize; ++i) {
226 if (cmp != data.get()[i]) {
227 ERRORF(reporter, "Failed on config %d", desc.fConfig);
228 break;
229 }
230 }
231 }
232 // Here we overwrite the texture so that the second time through we
233 // test against recycling without reclearing.
234 if (0 == i) {
235 surfCtx->writePixels(info, data.get(), 0, 0, 0);
236 }
237 }
238 context->purgeAllUnlockedResources();
Brian Salomond17b4a62017-05-23 16:53:47 -0400239 }
240 }
241 }
242 }
243}
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000244#endif