blob: 901c05dd7b97cd3f96cc0c3ae5b1bb3c13aad18e [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
Brian Salomon614c1a82018-12-19 15:42:06 -05008#include <set>
9#include "GrClip.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000010#include "GrContext.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000011#include "GrContextPriv.h"
bsalomon091f60c2015-11-10 11:54:56 -080012#include "GrGpu.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050013#include "GrProxyProvider.h"
Robert Phillips2890fbf2017-07-26 15:48:41 -040014#include "GrRenderTarget.h"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000016#include "GrTexture.h"
Brian Salomonc67c31c2018-12-06 10:00:03 -050017#include "GrTexturePriv.h"
18#include "SkAutoPixmapStorage.h"
Brian Osman48c99192017-06-02 08:45:06 -040019#include "SkMipMap.h"
Brian Salomon614c1a82018-12-19 15:42:06 -050020#include "SkSurface.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 Phillips646f6372018-09-25 09:31:10 -040054 nullptr, 256, 256, GrColorType::kRGBA_8888, 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,
Jim Van Verth69e57852018-12-05 13:38:59 -050091 kRG_88_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040092 kBGRA_8888_GrPixelConfig,
93 kSRGBA_8888_GrPixelConfig,
94 kSBGRA_8888_GrPixelConfig,
Brian Osman10fc6fd2018-03-02 11:01:10 -050095 kRGBA_1010102_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,
Jim Van Verth1676cb92019-01-15 13:24:45 -0500101 kRGB_ETC1_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400102 };
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400103 GR_STATIC_ASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs));
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400104
105 GrSurfaceDesc desc;
106 desc.fWidth = 64;
107 desc.fHeight = 64;
108
109 for (GrPixelConfig config : configs) {
110 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
111 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400112 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500113 desc.fSampleCnt = 1;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400114
Robert Phillips3b3307f2017-05-24 07:44:02 -0400115 sk_sp<GrSurface> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomon1c80e992018-01-29 09:50:47 -0500116 bool ict = caps->isConfigTexturable(desc.fConfig);
117 REPORTER_ASSERT(reporter, SkToBool(tex) == ict,
118 "config:%d, tex:%d, isConfigTexturable:%d", config, SkToBool(tex), ict);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400119
Greg Daniel4065d452018-11-16 15:43:41 -0500120 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
121 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(config, &srgbEncoded);
122 const GrBackendFormat format =
123 caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
124
Brian Salomon2a4f9832018-03-03 22:43:43 -0500125 sk_sp<GrTextureProxy> proxy =
Greg Daniel4065d452018-11-16 15:43:41 -0500126 proxyProvider->createMipMapProxy(format, desc, origin, SkBudgeted::kNo);
Brian Osman48c99192017-06-02 08:45:06 -0400127 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
128 (caps->isConfigTexturable(desc.fConfig) &&
Brian Salomon57111332018-02-05 15:55:54 -0500129 caps->mipMapSupport()));
Brian Osman48c99192017-06-02 08:45:06 -0400130
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400131 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400132 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 bool isRenderable = caps->isConfigRenderable(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
Brian Salomonbdecacf2018-02-02 20:32:49 -0500138 desc.fSampleCnt = 2;
Robert Phillips3b3307f2017-05-24 07:44:02 -0400139 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500140 isRenderable = SkToBool(caps->getRenderTargetSampleCount(2, config));
141 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
142 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
143 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400144 }
145 }
146}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400147
Brian Salomond17b4a62017-05-23 16:53:47 -0400148#include "GrDrawingManager.h"
149#include "GrSurfaceProxy.h"
150#include "GrTextureContext.h"
151
152DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
153 static constexpr int kSize = 100;
154 GrSurfaceDesc desc;
155 desc.fWidth = desc.fHeight = kSize;
156 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500157
Brian Salomond17b4a62017-05-23 16:53:47 -0400158 GrContext* context = context_info.grContext();
Greg Daniel4065d452018-11-16 15:43:41 -0500159 const GrCaps* caps = context->contextPriv().caps();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500160 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
161
Brian Salomond17b4a62017-05-23 16:53:47 -0400162 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
163 desc.fConfig = static_cast<GrPixelConfig>(c);
Greg Daniel4065d452018-11-16 15:43:41 -0500164 if (!caps->isConfigTexturable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400165 continue;
166 }
167 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
168 for (bool rt : {false, true}) {
Greg Daniel4065d452018-11-16 15:43:41 -0500169 if (rt && !caps->isConfigRenderable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400170 continue;
171 }
172 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400173 for (GrSurfaceOrigin origin :
174 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500175 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400176 // Try directly creating the texture.
177 // Do this twice in an attempt to hit the cache on the second time through.
178 for (int i = 0; i < 2; ++i) {
Chris Daltond004e0b2018-09-27 09:28:03 -0600179 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500180 desc, origin, fit, SkBudgeted::kYes);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500181 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400182 continue;
183 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400184 auto texCtx = context->contextPriv().makeWrappedSurfaceContext(
Brian Osman9aa30c62018-07-02 15:21:46 -0400185 std::move(proxy));
Greg Daniel90f28ec2017-09-25 12:26:58 -0400186 SkImageInfo info = SkImageInfo::Make(
187 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
188 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
189 if (texCtx->readPixels(info, data.get(), 0, 0, 0)) {
190 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
191 for (int i = 0; i < kSize * kSize; ++i) {
192 if (cmp != data.get()[i]) {
193 ERRORF(reporter, "Failed on config %d", desc.fConfig);
194 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400195 }
196 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400197 }
Greg Daniel90f28ec2017-09-25 12:26:58 -0400198 memset(data.get(), 0xBC, kSize * kSize * sizeof(uint32_t));
199 // Here we overwrite the texture so that the second time through we
200 // test against recycling without reclearing.
201 if (0 == i) {
202 texCtx->writePixels(info, data.get(), 0, 0, 0);
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400203 }
Greg Daniel52cb5fe2017-09-05 15:45:15 -0400204 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500205 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400206
Greg Daniel4065d452018-11-16 15:43:41 -0500207 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
208 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(desc.fConfig,
209 &srgbEncoded);
210 const GrBackendFormat format =
211 caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
212
Greg Daniel90f28ec2017-09-25 12:26:58 -0400213 // Try creating the texture as a deferred proxy.
214 for (int i = 0; i < 2; ++i) {
215 auto surfCtx = context->contextPriv().makeDeferredSurfaceContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500216 format, desc, origin, 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 }
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500238 context->contextPriv().purgeAllUnlockedResources_ForTesting();
Brian Salomond17b4a62017-05-23 16:53:47 -0400239 }
240 }
241 }
242 }
243}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500244
245DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
246 auto fillPixels = [](const SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
247 for (int y = 0; y < p->height(); ++y) {
248 for (int x = 0; x < p->width(); ++x) {
249 *p->writable_addr32(x, y) = f(x, y);
250 }
251 }
252 };
253
254 auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) {
255 SkASSERT(p1.info() == p2.info());
256 for (int y = 0; y < p1.height(); ++y) {
257 for (int x = 0; x < p1.width(); ++x) {
258 REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y));
259 if (p1.getColor(x, y) != p2.getColor(x, y)) {
260 return;
261 }
262 }
263 }
264 };
265
266 static constexpr int kSize = 100;
267 SkAutoPixmapStorage pixels;
268 pixels.alloc(SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
269 fillPixels(&pixels,
270 [](int x, int y) { return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t(x * y); });
271
272 GrContext* context = context_info.grContext();
273 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
274
275 // We test both kRW in addition to kRead mostly to ensure that the calls are structured such
276 // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
277 // kRead for the right reason.
278 for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
279 auto backendTex = context->contextPriv().getGpu()->createTestingOnlyBackendTexture(
280 pixels.addr(), kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kNo);
281 auto proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin,
282 kBorrow_GrWrapOwnership, ioType);
283 auto surfContext = context->contextPriv().makeWrappedSurfaceContext(proxy);
284
285 // Read pixels should work with a read-only texture.
286 SkAutoPixmapStorage read;
287 read.alloc(pixels.info());
288 auto readResult = surfContext->readPixels(pixels.info(), read.writable_addr(), 0, 0, 0);
289 REPORTER_ASSERT(reporter, readResult);
290 if (readResult) {
291 comparePixels(pixels, read, reporter);
292 }
293
294 // Write pixels should not work with a read-only texture.
295 SkAutoPixmapStorage write;
296 write.alloc(pixels.info());
297 fillPixels(&write, [&pixels](int x, int y) { return ~*pixels.addr32(); });
298 auto writeResult = surfContext->writePixels(pixels.info(), pixels.addr(), 0, 0, 0);
299 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
300 // Try the low level write.
301 context->flush();
302 auto gpuWriteResult = context->contextPriv().getGpu()->writePixels(
303 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888, write.addr32(),
304 0);
305 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
306
307 // Copies should not work with a read-only texture
308 auto copySrc = proxyProvider->createTextureProxy(
309 SkImage::MakeFromRaster(write, nullptr, nullptr), kNone_GrSurfaceFlags, 1,
310 SkBudgeted::kYes, SkBackingFit::kExact);
311 REPORTER_ASSERT(reporter, copySrc);
312 auto copyResult = surfContext->copy(copySrc.get());
313 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
314 // Try the low level copy.
315 context->flush();
316 auto gpuCopyResult = context->contextPriv().getGpu()->copySurface(
317 proxy->peekTexture(), kTopLeft_GrSurfaceOrigin, copySrc->peekTexture(),
318 kTopLeft_GrSurfaceOrigin, SkIRect::MakeWH(kSize, kSize), {0, 0});
319 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
320
321 // Mip regen should not work with a read only texture.
322 if (context->contextPriv().caps()->mipMapSupport()) {
323 backendTex = context->contextPriv().getGpu()->createTestingOnlyBackendTexture(
324 nullptr, kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kYes);
325 proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin,
326 kBorrow_GrWrapOwnership, ioType);
327 context->flush();
328 proxy->peekTexture()->texturePriv().markMipMapsDirty(); // avoids assert in GrGpu.
329 auto regenResult =
330 context->contextPriv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
331 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
332 }
333 }
334}
Brian Salomon614c1a82018-12-19 15:42:06 -0500335
336DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
337 GrContext* context;
338 static const int kS = 10;
339
340 // Helper to delete a backend texture in a GrTexture's release proc.
341 static const auto installBackendTextureReleaseProc = [](GrTexture* texture) {
342 auto backendTexture = texture->getBackendTexture();
343 auto context = texture->getContext();
344 struct ReleaseContext {
345 GrContext* fContext;
346 GrBackendTexture fBackendTexture;
347 };
348 auto release = [](void* rc) {
349 auto releaseContext = static_cast<ReleaseContext*>(rc);
350 if (!releaseContext->fContext->abandoned()) {
351 if (auto gpu = releaseContext->fContext->contextPriv().getGpu()) {
352 gpu->deleteTestingOnlyBackendTexture(releaseContext->fBackendTexture);
353 }
354 }
355 delete releaseContext;
356 };
357 texture->setRelease(sk_make_sp<GrReleaseProcHelper>(
358 release, new ReleaseContext{context, backendTexture}));
359 };
360
361 // Various ways of making textures.
362 auto makeWrapped = [](GrContext* context) {
363 auto backendTexture = context->contextPriv().getGpu()->createTestingOnlyBackendTexture(
364 nullptr, kS, kS, GrColorType::kRGBA_8888, false, GrMipMapped::kNo);
365 auto texture = context->contextPriv().resourceProvider()->wrapBackendTexture(
366 backendTexture, kBorrow_GrWrapOwnership, kRW_GrIOType);
367 installBackendTextureReleaseProc(texture.get());
368 return texture;
369 };
370
371 auto makeWrappedRenderable = [](GrContext* context) {
372 auto backendTexture = context->contextPriv().getGpu()->createTestingOnlyBackendTexture(
373 nullptr, kS, kS, GrColorType::kRGBA_8888, true, GrMipMapped::kNo);
374 auto texture = context->contextPriv().resourceProvider()->wrapRenderableBackendTexture(
375 backendTexture, 1, kBorrow_GrWrapOwnership);
376 installBackendTextureReleaseProc(texture.get());
377 return texture;
378 };
379
380 auto makeNormal = [](GrContext* context) {
381 GrSurfaceDesc desc;
382 desc.fConfig = kRGBA_8888_GrPixelConfig;
383 desc.fWidth = desc.fHeight = kS;
384 return context->contextPriv().resourceProvider()->createTexture(desc, SkBudgeted::kNo);
385 };
386
387 auto makeRenderable = [](GrContext* context) {
388 GrSurfaceDesc desc;
389 desc.fFlags = kRenderTarget_GrSurfaceFlag;
390 desc.fConfig = kRGBA_8888_GrPixelConfig;
391 desc.fWidth = desc.fHeight = kS;
392 return context->contextPriv().resourceProvider()->createTexture(desc, SkBudgeted::kNo);
393 };
394
395 std::function<sk_sp<GrTexture>(GrContext*)> makers[] = {makeWrapped, makeWrappedRenderable,
396 makeNormal, makeRenderable};
397
398 // Add a unique key, or not.
399 auto addKey = [](GrTexture* texture) {
400 static uint32_t gN = 0;
401 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
402 GrUniqueKey key;
403 GrUniqueKey::Builder builder(&key, kDomain, 1);
404 builder[0] = gN++;
405 builder.finish();
406 texture->resourcePriv().setUniqueKey(key);
407 };
408 auto dontAddKey = [](GrTexture* texture) {};
409 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
410
411 for (const auto& m : makers) {
412 for (const auto& keyAdder : keyAdders) {
413 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
414 sk_gpu_test::GrContextFactory factory;
415 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
416 context = factory.get(contextType);
417 if (!context) {
418 continue;
419 }
420
421 // The callback we add simply adds an integer to a set.
422 std::set<int> idleIDs;
423 struct Context {
424 std::set<int>* fIdleIDs;
425 int fNum;
426 };
427 auto proc = [](void* context) {
428 static_cast<Context*>(context)->fIdleIDs->insert(
429 static_cast<Context*>(context)->fNum);
430 delete static_cast<Context*>(context);
431 };
432
433 // Makes a texture, possibly adds a key, and sets the callback.
434 auto make = [&m, &keyAdder, &proc, &idleIDs](GrContext* context, int num) {
435 sk_sp<GrTexture> texture = m(context);
436 texture->setIdleProc(proc, new Context{&idleIDs, num});
437 keyAdder(texture.get());
438 return texture;
439 };
440
441 auto texture = make(context, 1);
442 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
443 bool isRT = SkToBool(texture->asRenderTarget());
444 auto backendFormat = texture->backendFormat();
445 texture.reset();
446 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
447
448 texture = make(context, 2);
449 SkImageInfo info =
450 SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
451 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
452 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
453 auto singleUseLazyCB = [&texture](GrResourceProvider* rp) {
454 return rp ? std::move(texture) : nullptr;
455 };
456 GrSurfaceDesc desc;
457 desc.fWidth = desc.fHeight = kS;
458 desc.fConfig = kRGBA_8888_GrPixelConfig;
459 if (isRT) {
460 desc.fFlags = kRenderTarget_GrSurfaceFlag;
461 }
462 SkBudgeted budgeted(texture->resourcePriv().isBudgeted());
463 auto proxy = context->contextPriv().proxyProvider()->createLazyProxy(
464 singleUseLazyCB, backendFormat, desc,
465 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
466 GrInternalSurfaceFlags ::kNone, SkBackingFit::kExact, budgeted,
467 GrSurfaceProxy::LazyInstantiationType::kSingleUse);
468 rtc->drawTexture(GrNoClip(), proxy, GrSamplerState::Filter::kNearest, SkPMColor4f(),
469 SkRect::MakeWH(kS, kS), SkRect::MakeWH(kS, kS),
470 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
471 SkMatrix::I(), nullptr);
472 // We still have the proxy, which should remain instantiated, thereby keeping the
473 // texture not purgeable.
474 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
475 context->flush();
476 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
477 context->contextPriv().getGpu()->testingOnly_flushGpuAndSync();
478 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
479
480 // This time we move the proxy into the draw.
481 rtc->drawTexture(GrNoClip(), std::move(proxy), GrSamplerState::Filter::kNearest,
482 SkPMColor4f(), SkRect::MakeWH(kS, kS), SkRect::MakeWH(kS, kS),
483 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
484 SkMatrix::I(), nullptr);
485 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
486 context->flush();
487 context->contextPriv().getGpu()->testingOnly_flushGpuAndSync();
488 // Now that the draw is fully consumed by the GPU, the texture should be idle.
489 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
490
491 // Make a proxy that should deinstantiate even if we keep a ref on it.
492 auto deinstantiateLazyCB = [&make, &context](GrResourceProvider* rp) {
493 return rp ? make(context, 3) : nullptr;
494 };
495 proxy = context->contextPriv().proxyProvider()->createLazyProxy(
496 deinstantiateLazyCB, backendFormat, desc,
497 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
498 GrInternalSurfaceFlags ::kNone, SkBackingFit::kExact, budgeted,
499 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
500 rtc->drawTexture(GrNoClip(), std::move(proxy), GrSamplerState::Filter::kNearest,
501 SkPMColor4f(), SkRect::MakeWH(kS, kS), SkRect::MakeWH(kS, kS),
502 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
503 SkMatrix::I(), nullptr);
504 // At this point the proxy shouldn't even be instantiated, there is no texture with
505 // id 3.
506 REPORTER_ASSERT(reporter, idleIDs.find(3) == idleIDs.end());
507 context->flush();
508 context->contextPriv().getGpu()->testingOnly_flushGpuAndSync();
509 // Now that the draw is fully consumed, we should have deinstantiated the proxy and
510 // the texture it made should be idle.
511 REPORTER_ASSERT(reporter, idleIDs.find(3) != idleIDs.end());
512
513 // Make sure we make the call during various shutdown scenarios.
514 texture = make(context, 4);
515 context->abandonContext();
516 REPORTER_ASSERT(reporter, idleIDs.find(4) != idleIDs.end());
517 factory.destroyContexts();
518 context = factory.get(contextType);
519
520 texture = make(context, 5);
521 factory.destroyContexts();
522 REPORTER_ASSERT(reporter, idleIDs.find(5) != idleIDs.end());
523 context = factory.get(contextType);
524
525 texture = make(context, 6);
526 factory.releaseResourcesAndAbandonContexts();
527 REPORTER_ASSERT(reporter, idleIDs.find(6) != idleIDs.end());
528 }
529 }
530 }
531}