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