blob: ffacc95b5e0b56c9c5fe893ffed3d3b84c015933 [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>
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkSurface.h"
10#include "include/gpu/GrContext.h"
11#include "include/gpu/GrRenderTarget.h"
12#include "include/gpu/GrTexture.h"
13#include "src/core/SkAutoPixmapStorage.h"
14#include "src/core/SkMipMap.h"
15#include "src/gpu/GrClip.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGpu.h"
18#include "src/gpu/GrProxyProvider.h"
19#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/GrTexturePriv.h"
21#include "tests/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.
Brian Osmanfbe24062019-04-03 16:04:45 +000025DEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070026 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050027 auto resourceProvider = context->priv().resourceProvider();
28 GrGpu* gpu = context->priv().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 Phillips9313aa72019-04-09 18:41:27 -040036 sk_sp<GrSurface> texRT1 = resourceProvider->createTexture(
37 desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO);
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 Phillips9313aa72019-04-09 18:41:27 -040049 sk_sp<GrTexture> tex1 = resourceProvider->createTexture(
50 desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO);
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 Phillips646f6372018-09-25 09:31:10 -040056 nullptr, 256, 256, GrColorType::kRGBA_8888, false, GrMipMapped::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080057
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050058 sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture(
59 backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
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
Brian Salomon26102cb2018-03-09 09:33:19 -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 Phillips9da87e02019-02-04 13:26:26 -050077 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
78 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
79 const GrCaps* caps = context->priv().caps();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040080
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,
Brian Salomon5fba7ad2018-03-22 10:01:16 -040092 kRGB_888_GrPixelConfig,
Greg Danielf259b8b2019-02-14 09:03:43 -050093 kRGB_888X_GrPixelConfig,
Jim Van Verth69e57852018-12-05 13:38:59 -050094 kRG_88_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040095 kBGRA_8888_GrPixelConfig,
96 kSRGBA_8888_GrPixelConfig,
97 kSBGRA_8888_GrPixelConfig,
Brian Osman10fc6fd2018-03-02 11:01:10 -050098 kRGBA_1010102_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040099 kRGBA_float_GrPixelConfig,
100 kRG_float_GrPixelConfig,
101 kAlpha_half_GrPixelConfig,
Greg Danielef59d872017-11-17 16:47:21 -0500102 kAlpha_half_as_Red_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400103 kRGBA_half_GrPixelConfig,
Brian Osmand0626aa2019-03-11 15:28:06 -0400104 kRGBA_half_Clamped_GrPixelConfig,
Jim Van Verth1676cb92019-01-15 13:24:45 -0500105 kRGB_ETC1_GrPixelConfig,
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400106 };
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400107 GR_STATIC_ASSERT(kGrPixelConfigCnt == SK_ARRAY_COUNT(configs));
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400108
109 GrSurfaceDesc desc;
110 desc.fWidth = 64;
111 desc.fHeight = 64;
112
113 for (GrPixelConfig config : configs) {
114 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
115 desc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400116 desc.fConfig = config;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500117 desc.fSampleCnt = 1;
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400118
Robert Phillips9313aa72019-04-09 18:41:27 -0400119 sk_sp<GrSurface> tex = resourceProvider->createTexture(
120 desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO);
Brian Salomon1c80e992018-01-29 09:50:47 -0500121 bool ict = caps->isConfigTexturable(desc.fConfig);
122 REPORTER_ASSERT(reporter, SkToBool(tex) == ict,
123 "config:%d, tex:%d, isConfigTexturable:%d", config, SkToBool(tex), ict);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400124
Greg Daniel4065d452018-11-16 15:43:41 -0500125 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
126 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(config, &srgbEncoded);
127 const GrBackendFormat format =
128 caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
129
Brian Salomon2a4f9832018-03-03 22:43:43 -0500130 sk_sp<GrTextureProxy> proxy =
Greg Daniel4065d452018-11-16 15:43:41 -0500131 proxyProvider->createMipMapProxy(format, desc, origin, SkBudgeted::kNo);
Brian Osman48c99192017-06-02 08:45:06 -0400132 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) ==
133 (caps->isConfigTexturable(desc.fConfig) &&
Brian Salomon57111332018-02-05 15:55:54 -0500134 caps->mipMapSupport()));
Brian Osman48c99192017-06-02 08:45:06 -0400135
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400136 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips9313aa72019-04-09 18:41:27 -0400137 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo,
138 GrResourceProvider::Flags::kNoPendingIO);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500139 bool isRenderable = caps->isConfigRenderable(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
Brian Salomonbdecacf2018-02-02 20:32:49 -0500144 desc.fSampleCnt = 2;
Robert Phillips9313aa72019-04-09 18:41:27 -0400145 tex = resourceProvider->createTexture(desc, SkBudgeted::kNo,
146 GrResourceProvider::Flags::kNoPendingIO);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500147 isRenderable = SkToBool(caps->getRenderTargetSampleCount(2, config));
148 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
149 "config:%d, tex:%d, isRenderable:%d", config, SkToBool(tex),
150 isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400151 }
152 }
153}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400154
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500155#include "include/private/GrSurfaceProxy.h"
156#include "src/gpu/GrDrawingManager.h"
157#include "src/gpu/GrTextureContext.h"
Brian Salomond17b4a62017-05-23 16:53:47 -0400158
159DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info) {
160 static constexpr int kSize = 100;
161 GrSurfaceDesc desc;
162 desc.fWidth = desc.fHeight = kSize;
163 std::unique_ptr<uint32_t[]> data(new uint32_t[kSize * kSize]);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500164
Brian Salomond17b4a62017-05-23 16:53:47 -0400165 GrContext* context = context_info.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500166 const GrCaps* caps = context->priv().caps();
167 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500168
Brian Salomond17b4a62017-05-23 16:53:47 -0400169 for (int c = 0; c <= kLast_GrPixelConfig; ++c) {
170 desc.fConfig = static_cast<GrPixelConfig>(c);
Greg Daniel4065d452018-11-16 15:43:41 -0500171 if (!caps->isConfigTexturable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400172 continue;
173 }
174 desc.fFlags = kPerformInitialClear_GrSurfaceFlag;
175 for (bool rt : {false, true}) {
Greg Daniel4065d452018-11-16 15:43:41 -0500176 if (rt && !caps->isConfigRenderable(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400177 continue;
178 }
179 desc.fFlags |= rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Greg Daniel90f28ec2017-09-25 12:26:58 -0400180 for (GrSurfaceOrigin origin :
181 {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500182 for (auto fit : { SkBackingFit::kApprox, SkBackingFit::kExact }) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400183 // Try directly creating the texture.
184 // Do this twice in an attempt to hit the cache on the second time through.
185 for (int i = 0; i < 2; ++i) {
Chris Daltond004e0b2018-09-27 09:28:03 -0600186 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500187 desc, origin, fit, SkBudgeted::kYes);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500188 if (!proxy) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400189 continue;
190 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500191 auto texCtx = context->priv().makeWrappedSurfaceContext(std::move(proxy));
Greg Daniel90f28ec2017-09-25 12:26:58 -0400192 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 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500211 context->priv().testingOnly_purgeAllUnlockedResources();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400212
Greg Daniel4065d452018-11-16 15:43:41 -0500213 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
214 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(desc.fConfig,
215 &srgbEncoded);
216 const GrBackendFormat format =
217 caps->getBackendFormatFromGrColorType(colorType, srgbEncoded);
218
Greg Daniel90f28ec2017-09-25 12:26:58 -0400219 // Try creating the texture as a deferred proxy.
220 for (int i = 0; i < 2; ++i) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500221 auto surfCtx = context->priv().makeDeferredSurfaceContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500222 format, desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes);
Greg Daniel90f28ec2017-09-25 12:26:58 -0400223 if (!surfCtx) {
224 continue;
225 }
226 SkImageInfo info = SkImageInfo::Make(
227 kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
228 memset(data.get(), 0xAB, kSize * kSize * sizeof(uint32_t));
229 if (surfCtx->readPixels(info, data.get(), 0, 0, 0)) {
230 uint32_t cmp = GrPixelConfigIsOpaque(desc.fConfig) ? 0xFF000000 : 0;
231 for (int i = 0; i < kSize * kSize; ++i) {
232 if (cmp != data.get()[i]) {
233 ERRORF(reporter, "Failed on config %d", desc.fConfig);
234 break;
235 }
236 }
237 }
238 // Here we overwrite the texture so that the second time through we
239 // test against recycling without reclearing.
240 if (0 == i) {
241 surfCtx->writePixels(info, data.get(), 0, 0, 0);
242 }
243 }
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500244 context->priv().testingOnly_purgeAllUnlockedResources();
Brian Salomond17b4a62017-05-23 16:53:47 -0400245 }
246 }
247 }
248 }
249}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500250
251DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
252 auto fillPixels = [](const SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
253 for (int y = 0; y < p->height(); ++y) {
254 for (int x = 0; x < p->width(); ++x) {
255 *p->writable_addr32(x, y) = f(x, y);
256 }
257 }
258 };
259
260 auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) {
261 SkASSERT(p1.info() == p2.info());
262 for (int y = 0; y < p1.height(); ++y) {
263 for (int x = 0; x < p1.width(); ++x) {
264 REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y));
265 if (p1.getColor(x, y) != p2.getColor(x, y)) {
266 return;
267 }
268 }
269 }
270 };
271
272 static constexpr int kSize = 100;
273 SkAutoPixmapStorage pixels;
274 pixels.alloc(SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
Brian Osman50ea3c02019-02-04 10:01:53 -0500275 fillPixels(&pixels, [](int x, int y) {
276 return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
277 });
Brian Salomonc67c31c2018-12-06 10:00:03 -0500278
279 GrContext* context = context_info.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500280 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500281
282 // We test both kRW in addition to kRead mostly to ensure that the calls are structured such
283 // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
284 // kRead for the right reason.
285 for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500286 auto backendTex = context->priv().getGpu()->createTestingOnlyBackendTexture(
Brian Salomonc67c31c2018-12-06 10:00:03 -0500287 pixels.addr(), kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kNo);
288 auto proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500289 kBorrow_GrWrapOwnership,
290 GrWrapCacheable::kNo, ioType);
Robert Phillips9da87e02019-02-04 13:26:26 -0500291 auto surfContext = context->priv().makeWrappedSurfaceContext(proxy);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500292
293 // Read pixels should work with a read-only texture.
294 SkAutoPixmapStorage read;
295 read.alloc(pixels.info());
296 auto readResult = surfContext->readPixels(pixels.info(), read.writable_addr(), 0, 0, 0);
297 REPORTER_ASSERT(reporter, readResult);
298 if (readResult) {
299 comparePixels(pixels, read, reporter);
300 }
301
302 // Write pixels should not work with a read-only texture.
303 SkAutoPixmapStorage write;
304 write.alloc(pixels.info());
305 fillPixels(&write, [&pixels](int x, int y) { return ~*pixels.addr32(); });
306 auto writeResult = surfContext->writePixels(pixels.info(), pixels.addr(), 0, 0, 0);
307 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
308 // Try the low level write.
309 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500310 auto gpuWriteResult = context->priv().getGpu()->writePixels(
Brian Salomonc67c31c2018-12-06 10:00:03 -0500311 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888, write.addr32(),
312 0);
313 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
314
315 // Copies should not work with a read-only texture
316 auto copySrc = proxyProvider->createTextureProxy(
317 SkImage::MakeFromRaster(write, nullptr, nullptr), kNone_GrSurfaceFlags, 1,
318 SkBudgeted::kYes, SkBackingFit::kExact);
319 REPORTER_ASSERT(reporter, copySrc);
320 auto copyResult = surfContext->copy(copySrc.get());
321 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
322 // Try the low level copy.
323 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500324 auto gpuCopyResult = context->priv().getGpu()->copySurface(
Brian Salomonc67c31c2018-12-06 10:00:03 -0500325 proxy->peekTexture(), kTopLeft_GrSurfaceOrigin, copySrc->peekTexture(),
326 kTopLeft_GrSurfaceOrigin, SkIRect::MakeWH(kSize, kSize), {0, 0});
327 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
328
329 // Mip regen should not work with a read only texture.
Robert Phillips9da87e02019-02-04 13:26:26 -0500330 if (context->priv().caps()->mipMapSupport()) {
331 backendTex = context->priv().getGpu()->createTestingOnlyBackendTexture(
Brian Salomonc67c31c2018-12-06 10:00:03 -0500332 nullptr, kSize, kSize, kRGBA_8888_SkColorType, true, GrMipMapped::kYes);
333 proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500334 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
335 ioType);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500336 context->flush();
337 proxy->peekTexture()->texturePriv().markMipMapsDirty(); // avoids assert in GrGpu.
338 auto regenResult =
Robert Phillips9da87e02019-02-04 13:26:26 -0500339 context->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500340 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
341 }
342 }
343}
Brian Salomon614c1a82018-12-19 15:42:06 -0500344
Brian Salomon8cabb322019-02-22 10:44:19 -0500345static sk_sp<GrTexture> make_wrapped_texture(GrContext* context, bool renderable) {
346 auto backendTexture = context->priv().getGpu()->createTestingOnlyBackendTexture(
347 nullptr, 10, 10, GrColorType::kRGBA_8888, renderable, GrMipMapped::kNo);
348 sk_sp<GrTexture> texture;
349 if (renderable) {
350 texture = context->priv().resourceProvider()->wrapRenderableBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500351 backendTexture, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500352 } else {
353 texture = context->priv().resourceProvider()->wrapBackendTexture(
354 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
355 }
356 // Add a release proc that deletes the GrBackendTexture.
357 struct ReleaseContext {
358 GrContext* fContext;
359 GrBackendTexture fBackendTexture;
Brian Salomon614c1a82018-12-19 15:42:06 -0500360 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500361 auto release = [](void* rc) {
362 auto releaseContext = static_cast<ReleaseContext*>(rc);
363 if (!releaseContext->fContext->abandoned()) {
364 if (auto gpu = releaseContext->fContext->priv().getGpu()) {
365 gpu->deleteTestingOnlyBackendTexture(releaseContext->fBackendTexture);
366 }
367 }
368 delete releaseContext;
369 };
Brian Salomon2ca31f82019-03-05 13:28:58 -0500370 texture->setRelease(release, new ReleaseContext{context, backendTexture});
Brian Salomon8cabb322019-02-22 10:44:19 -0500371 return texture;
372}
Brian Salomon614c1a82018-12-19 15:42:06 -0500373
Brian Salomon8cabb322019-02-22 10:44:19 -0500374static sk_sp<GrTexture> make_normal_texture(GrContext* context, bool renderable) {
375 GrSurfaceDesc desc;
376 desc.fConfig = kRGBA_8888_GrPixelConfig;
377 desc.fWidth = desc.fHeight = 10;
378 desc.fFlags = renderable ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Robert Phillips9313aa72019-04-09 18:41:27 -0400379 return context->priv().resourceProvider()->createTexture(
380 desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO);
Brian Salomon8cabb322019-02-22 10:44:19 -0500381}
Brian Salomon614c1a82018-12-19 15:42:06 -0500382
Brian Salomon8cabb322019-02-22 10:44:19 -0500383DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
384 // Various ways of making textures.
385 auto makeWrapped = [](GrContext* context) { return make_wrapped_texture(context, false); };
386 auto makeWrappedRenderable = [](GrContext* context) {
387 return make_wrapped_texture(context, true);
Brian Salomon614c1a82018-12-19 15:42:06 -0500388 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500389 auto makeNormal = [](GrContext* context) { return make_normal_texture(context, false); };
390 auto makeRenderable = [](GrContext* context) { return make_normal_texture(context, true); };
Brian Salomon614c1a82018-12-19 15:42:06 -0500391
392 std::function<sk_sp<GrTexture>(GrContext*)> makers[] = {makeWrapped, makeWrappedRenderable,
393 makeNormal, makeRenderable};
394
395 // Add a unique key, or not.
396 auto addKey = [](GrTexture* texture) {
397 static uint32_t gN = 0;
398 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
399 GrUniqueKey key;
400 GrUniqueKey::Builder builder(&key, kDomain, 1);
401 builder[0] = gN++;
402 builder.finish();
403 texture->resourcePriv().setUniqueKey(key);
404 };
405 auto dontAddKey = [](GrTexture* texture) {};
406 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
407
408 for (const auto& m : makers) {
409 for (const auto& keyAdder : keyAdders) {
410 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
411 sk_gpu_test::GrContextFactory factory;
412 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500413 GrContext* context = factory.get(contextType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500414 if (!context) {
415 continue;
416 }
417
418 // The callback we add simply adds an integer to a set.
419 std::set<int> idleIDs;
420 struct Context {
421 std::set<int>* fIdleIDs;
422 int fNum;
423 };
424 auto proc = [](void* context) {
425 static_cast<Context*>(context)->fIdleIDs->insert(
426 static_cast<Context*>(context)->fNum);
427 delete static_cast<Context*>(context);
428 };
429
430 // Makes a texture, possibly adds a key, and sets the callback.
431 auto make = [&m, &keyAdder, &proc, &idleIDs](GrContext* context, int num) {
432 sk_sp<GrTexture> texture = m(context);
Brian Salomone80b8092019-03-08 13:25:19 -0500433 texture->addIdleProc(proc, new Context{&idleIDs, num},
434 GrTexture::IdleState::kFinished);
Brian Salomon614c1a82018-12-19 15:42:06 -0500435 keyAdder(texture.get());
436 return texture;
437 };
438
439 auto texture = make(context, 1);
440 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
441 bool isRT = SkToBool(texture->asRenderTarget());
442 auto backendFormat = texture->backendFormat();
443 texture.reset();
444 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
445
446 texture = make(context, 2);
Brian Salomon8cabb322019-02-22 10:44:19 -0500447 int w = texture->width();
448 int h = texture->height();
Brian Salomon614c1a82018-12-19 15:42:06 -0500449 SkImageInfo info =
Brian Salomon8cabb322019-02-22 10:44:19 -0500450 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500451 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
452 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
453 auto singleUseLazyCB = [&texture](GrResourceProvider* rp) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400454 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
455 if (texture->getUniqueKey().isValid()) {
456 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
457 }
458 return GrSurfaceProxy::LazyInstantiationResult{std::move(texture), mode};
Brian Salomon614c1a82018-12-19 15:42:06 -0500459 };
460 GrSurfaceDesc desc;
Brian Salomon8cabb322019-02-22 10:44:19 -0500461 desc.fWidth = w;
462 desc.fHeight = h;
Brian Salomon614c1a82018-12-19 15:42:06 -0500463 desc.fConfig = kRGBA_8888_GrPixelConfig;
464 if (isRT) {
465 desc.fFlags = kRenderTarget_GrSurfaceFlag;
466 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500467 SkBudgeted budgeted;
468 if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
469 budgeted = SkBudgeted::kYes;
470 } else {
471 budgeted = SkBudgeted::kNo;
472 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500473 auto proxy = context->priv().proxyProvider()->createLazyProxy(
Brian Salomon614c1a82018-12-19 15:42:06 -0500474 singleUseLazyCB, backendFormat, desc,
475 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
476 GrInternalSurfaceFlags ::kNone, SkBackingFit::kExact, budgeted,
477 GrSurfaceProxy::LazyInstantiationType::kSingleUse);
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500478 rtc->drawTexture(GrNoClip(), proxy, GrSamplerState::Filter::kNearest,
Brian Salomon8cabb322019-02-22 10:44:19 -0500479 SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h),
480 SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone,
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500481 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500482 // We still have the proxy, which should remain instantiated, thereby keeping the
483 // texture not purgeable.
484 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
485 context->flush();
486 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillips9da87e02019-02-04 13:26:26 -0500487 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500488 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
489
490 // This time we move the proxy into the draw.
491 rtc->drawTexture(GrNoClip(), std::move(proxy), GrSamplerState::Filter::kNearest,
Brian Salomon8cabb322019-02-22 10:44:19 -0500492 SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h),
493 SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone,
Michael Ludwigd54ca8f2019-02-13 13:25:21 -0500494 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500495 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
496 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500497 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500498 // Now that the draw is fully consumed by the GPU, the texture should be idle.
499 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
500
Brian Salomon876a0172019-03-08 11:12:14 -0500501 // Make a proxy that should deinstantiate even if we keep a ref on it.
502 auto deinstantiateLazyCB = [&make, &context](GrResourceProvider* rp) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400503 auto texture = make(context, 3);
504 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
505 if (texture->getUniqueKey().isValid()) {
506 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
507 }
508 return GrSurfaceProxy::LazyInstantiationResult{std::move(texture), mode};
Brian Salomon876a0172019-03-08 11:12:14 -0500509 };
510 proxy = context->priv().proxyProvider()->createLazyProxy(
511 deinstantiateLazyCB, backendFormat, desc,
512 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
513 GrInternalSurfaceFlags ::kNone, SkBackingFit::kExact, budgeted,
514 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate);
515 rtc->drawTexture(GrNoClip(), std::move(proxy), GrSamplerState::Filter::kNearest,
516 SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h),
517 SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone,
518 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
519 // At this point the proxy shouldn't even be instantiated, there is no texture with
520 // id 3.
521 REPORTER_ASSERT(reporter, idleIDs.find(3) == idleIDs.end());
522 context->flush();
523 context->priv().getGpu()->testingOnly_flushGpuAndSync();
524 // Now that the draw is fully consumed, we should have deinstantiated the proxy and
525 // the texture it made should be idle.
526 REPORTER_ASSERT(reporter, idleIDs.find(3) != idleIDs.end());
527
Brian Salomon9bc76d92019-01-24 12:18:33 -0500528 // Make sure we make the call during various shutdown scenarios where the texture
529 // might persist after context is destroyed, abandoned, etc. We test three
530 // variations of each scenario. One where the texture is just created. Another,
531 // where the texture has been used in a draw and then the context is flushed. And
532 // one where the the texture was drawn but the context is not flushed.
533 // In each scenario we test holding a ref beyond the context shutdown and not.
Brian Salomon614c1a82018-12-19 15:42:06 -0500534
Brian Salomon9bc76d92019-01-24 12:18:33 -0500535 // These tests are difficult to get working with Vulkan. See http://skbug.com/8705
536 // and http://skbug.com/8275
537 GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
538 if (api == GrBackendApi::kVulkan) {
539 continue;
540 }
Brian Salomon876a0172019-03-08 11:12:14 -0500541 int id = 4;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500542 enum class DrawType {
543 kNoDraw,
544 kDraw,
545 kDrawAndFlush,
546 };
547 for (auto drawType :
548 {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) {
549 for (bool unrefFirst : {false, true}) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500550 auto possiblyDrawAndFlush = [&context, &texture, drawType, unrefFirst, w,
551 h] {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500552 if (drawType == DrawType::kNoDraw) {
553 return;
554 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500555 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500556 kPremul_SkAlphaType);
557 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
558 nullptr);
Robert Phillips9da87e02019-02-04 13:26:26 -0500559 auto rtc = rt->getCanvas()
Brian Salomon9bc76d92019-01-24 12:18:33 -0500560 ->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500561 auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomon9bc76d92019-01-24 12:18:33 -0500562 texture, kTopLeft_GrSurfaceOrigin);
Brian Salomon8cabb322019-02-22 10:44:19 -0500563 rtc->drawTexture(
564 GrNoClip(), proxy, GrSamplerState::Filter::kNearest,
565 SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h),
566 SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone,
567 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500568 if (drawType == DrawType::kDrawAndFlush) {
569 context->flush();
570 }
571 if (unrefFirst) {
572 texture.reset();
573 }
574 };
575 texture = make(context, id);
576 possiblyDrawAndFlush();
577 context->abandonContext();
578 texture.reset();
579 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
580 factory.destroyContexts();
581 context = factory.get(contextType);
582 ++id;
Brian Salomon614c1a82018-12-19 15:42:06 -0500583
Brian Salomon9bc76d92019-01-24 12:18:33 -0500584 // Similar to previous, but reset the texture after the context was
585 // abandoned and then destroyed.
586 texture = make(context, id);
587 possiblyDrawAndFlush();
588 context->abandonContext();
589 factory.destroyContexts();
590 texture.reset();
591 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
592 context = factory.get(contextType);
593 id++;
594
595 texture = make(context, id);
596 possiblyDrawAndFlush();
597 factory.destroyContexts();
598 texture.reset();
599 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
600 context = factory.get(contextType);
601 id++;
602
603 texture = make(context, id);
604 possiblyDrawAndFlush();
605 factory.releaseResourcesAndAbandonContexts();
606 texture.reset();
607 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
608 context = factory.get(contextType);
609 id++;
610 }
611 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500612 }
613 }
614 }
615}
Brian Salomon8cabb322019-02-22 10:44:19 -0500616
617// Tests an idle proc that unrefs another resource down to zero.
618DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) {
619 GrContext* context = contextInfo.grContext();
620
621 // idle proc that releases another texture.
622 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); };
623
624 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
625 for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500626 for (auto idleState :
627 {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
628 auto idleTexture = idleMaker(context, false);
629 auto otherTexture = otherMaker(context, false);
630 otherTexture->ref();
631 idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState);
632 otherTexture.reset();
633 idleTexture.reset();
634 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500635 }
636 }
637}
638
639// Similar to above but more complicated. This flushes the context from the idle proc.
640// crbug.com/933526.
641DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) {
642 GrContext* context = contextInfo.grContext();
643
644 // idle proc that flushes the context.
645 auto idleProc = [](void* context) { reinterpret_cast<GrContext*>(context)->flush(); };
646
647 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500648 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
649 auto idleTexture = idleMaker(context, false);
650 idleTexture->addIdleProc(idleProc, context, idleState);
651 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
652 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 1, nullptr);
653 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
654 // is a wrapped-texture backed image.
655 surf->getCanvas()->clear(SK_ColorWHITE);
656 auto img1 = surf->makeImageSnapshot();
657 auto gpu = context->priv().getGpu();
658 std::unique_ptr<uint32_t[]> pixels(new uint32_t[info.width() * info.height()]);
659 auto backendTexture = gpu->createTestingOnlyBackendTexture(
660 pixels.get(), info.width(), info.height(), kRGBA_8888_SkColorType, false,
661 GrMipMapped::kNo);
662 auto img2 = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
663 info.colorType(), info.alphaType(), nullptr);
664 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
665 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
666 idleTexture.reset();
667 gpu->deleteTestingOnlyBackendTexture(backendTexture);
668 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500669 }
670}
671
672DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
673 GrContext* context = contextInfo.grContext();
674 // idle proc that refs the texture
675 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
676 // release proc to check whether the texture was released or not.
677 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomone80b8092019-03-08 13:25:19 -0500678 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
679 bool isReleased = false;
680 auto idleTexture = make_normal_texture(context, false);
681 // This test assumes the texture won't be cached (or else the release proc doesn't get
682 // called).
683 idleTexture->resourcePriv().removeScratchKey();
684 context->flush();
685 idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState);
686 idleTexture->setRelease(releaseProc, &isReleased);
687 auto* raw = idleTexture.get();
688 idleTexture.reset();
689 REPORTER_ASSERT(reporter, !isReleased);
690 raw->unref();
691 REPORTER_ASSERT(reporter, isReleased);
692 }
693}
694
695DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
696 GrContext* context = contextInfo.grContext();
697 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
698 auto idleTexture = idleMaker(context, false);
699
700 uint32_t flags = 0;
701 static constexpr uint32_t kFlushFlag = 0x1;
702 static constexpr uint32_t kFinishFlag = 0x2;
703 auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; };
704 auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; };
705 idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed);
706 idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished);
707
708 // Insert a copy from idleTexture to another texture so that we have some queued IO on
709 // idleTexture.
710 auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped(
711 std::move(idleTexture), kTopLeft_GrSurfaceOrigin);
712 SkImageInfo info = SkImageInfo::Make(proxy->width(), proxy->height(),
713 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
714 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
715 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
716 context->flush();
717 rtc->copy(proxy.get());
718 proxy.reset();
719 REPORTER_ASSERT(reporter, flags == 0);
720
721 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
722 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
723 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
724 context->flush();
725 if (contextInfo.backend() == kVulkan_GrBackend) {
726 REPORTER_ASSERT(reporter, flags & kFlushFlag);
727 } else {
728 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
729 }
730 context->priv().getGpu()->testingOnly_flushGpuAndSync();
731 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
732 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500733}