blob: c12a7bdbe46057b37ea9ac3ec503dc0115db6476 [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"
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050012#include "src/core/SkCompressedDataUtils.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040013#include "src/gpu/GrBackendUtils.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050014#include "src/gpu/GrBitmapTextureMaker.h"
Adlai Hollera0693042020-10-14 11:23:11 -040015#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040017#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrProxyProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040019#include "src/gpu/GrRenderTarget.h"
Michael Ludwig7c12e282020-05-29 09:54:07 -040020#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000022#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tests/Test.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040024#include "tests/TestUtils.h"
Brian Salomon72050802020-10-12 20:45:06 -040025#include "tools/gpu/BackendTextureImageFactory.h"
26#include "tools/gpu/ManagedBackendTexture.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000027
bsalomona2c23232014-11-25 07:41:12 -080028// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
29// and render targets to GrSurface all work as expected.
Brian Osmanfbe24062019-04-03 16:04:45 +000030DEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040031 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050032 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050033
Brian Salomona56a7462020-02-07 14:17:25 -050034 static constexpr SkISize kDesc = {256, 256};
Brian Salomon4eb38b72019-08-05 12:58:39 -040035 auto format = context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
36 GrRenderable::kYes);
Brian Salomona90382f2019-09-17 09:01:56 -040037 sk_sp<GrSurface> texRT1 =
Brian Salomon7e67dca2020-07-21 09:27:25 -040038 resourceProvider->createTexture(kDesc, format, GrRenderable::kYes, 1, GrMipmapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -040039 SkBudgeted::kNo, GrProtected::kNo);
bsalomona2c23232014-11-25 07:41:12 -080040
Robert Phillipse78b7252017-04-06 07:59:41 -040041 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget());
42 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080043 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
44 texRT1->asTexture());
45 REPORTER_ASSERT(reporter, texRT1->asRenderTarget() ==
46 static_cast<GrSurface*>(texRT1->asTexture()));
47 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
48 static_cast<GrSurface*>(texRT1->asTexture()));
bsalomona2c23232014-11-25 07:41:12 -080049
Brian Salomona90382f2019-09-17 09:01:56 -040050 sk_sp<GrTexture> tex1 =
Brian Salomon7e67dca2020-07-21 09:27:25 -040051 resourceProvider->createTexture(kDesc, format, GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -040052 SkBudgeted::kNo, GrProtected::kNo);
kkinnunen15302832015-12-01 04:35:26 -080053 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040054 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
55 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000056
Robert Phillips4bdd36f2019-06-04 11:03:06 -040057 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -040058 256, 256, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -040059 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080060
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050061 sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -040062 backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +000063
bungeman6bd52842016-10-27 09:30:08 -070064 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
65 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080066 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
67 texRT2->asTexture());
68 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
69 static_cast<GrSurface*>(texRT2->asTexture()));
70 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
71 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000072
Robert Phillips5c7a25b2019-05-20 08:38:07 -040073 context->deleteBackendTexture(backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000074}
75
Greg Daniel7bfc9132019-08-14 14:23:53 -040076// This test checks that the isFormatTexturable and isFormatRenderable are
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040077// consistent with createTexture's result.
78DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040079 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050080 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
81 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
82 const GrCaps* caps = context->priv().caps();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040083
Brian Salomon4eb38b72019-08-05 12:58:39 -040084 // TODO: Should only need format here but need to determine compression type from format
85 // without config.
Robert Phillips48257d72019-12-16 14:20:53 -050086 auto createTexture = [](SkISize dimensions, GrColorType colorType,
Brian Salomon4eb38b72019-08-05 12:58:39 -040087 const GrBackendFormat& format, GrRenderable renderable,
Brian Salomonbb8dde82019-06-27 10:52:13 -040088 GrResourceProvider* rp) -> sk_sp<GrTexture> {
Greg Daniel01f278c2020-06-12 16:58:17 -040089 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
Robert Phillipsd6df7b52019-12-13 11:17:46 -050090 if (compression != SkImage::CompressionType::kNone) {
Brian Salomonbb8dde82019-06-27 10:52:13 -040091 if (renderable == GrRenderable::kYes) {
92 return nullptr;
93 }
Robert Phillips99dead92020-01-27 16:11:57 -050094 auto size = SkCompressedDataSize(compression, dimensions, nullptr, false);
Brian Salomonbb8dde82019-06-27 10:52:13 -040095 auto data = SkData::MakeUninitialized(size);
96 SkColor4f color = {0, 0, 0, 0};
Brian Salomon7e67dca2020-07-21 09:27:25 -040097 GrFillInCompressedData(compression, dimensions, GrMipmapped::kNo,
Robert Phillips48257d72019-12-16 14:20:53 -050098 (char*)data->writable_data(), color);
Robert Phillipse4720c62020-01-14 14:33:24 -050099 return rp->createCompressedTexture(dimensions, format, SkBudgeted::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400100 GrMipmapped::kNo, GrProtected::kNo, data.get());
Brian Salomonbb8dde82019-06-27 10:52:13 -0400101 } else {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400102 return rp->createTexture(dimensions, format, renderable, 1, GrMipmapped::kNo,
Brian Salomona56a7462020-02-07 14:17:25 -0500103 SkBudgeted::kNo, GrProtected::kNo);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400104 }
105 };
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400106
Brian Salomona56a7462020-02-07 14:17:25 -0500107 static constexpr SkISize kDims = {64, 64};
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400108
Robert Phillipsffe27292019-08-01 10:08:07 -0400109 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
John Stilesbd3ffa42020-07-30 20:24:57 -0400110 caps->getTestingCombinations();
Robert Phillips0902c982019-07-16 07:47:56 -0400111
John Stilesbd3ffa42020-07-30 20:24:57 -0400112 for (const GrCaps::TestFormatColorTypeCombination& combo : combos) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400113
114 SkASSERT(combo.fColorType != GrColorType::kUnknown);
115 SkASSERT(combo.fFormat.isValid());
116
117 // Right now Vulkan has two backend formats that support ABGR_4444 (R4G4B4A4 and B4G4R4A4).
118 // Until we can create textures directly from the backend format this yields some
119 // ambiguity in what is actually supported and which textures can be created.
120 if (ctxInfo.backend() == kVulkan_GrBackend && combo.fColorType == GrColorType::kABGR_4444) {
Robert Phillips0902c982019-07-16 07:47:56 -0400121 continue;
122 }
123
Greg Daniel3a365112020-02-14 10:47:18 -0500124 // Check if 'isFormatTexturable' agrees with 'createTexture' and that the mipmap
125 // support check is working
126 {
127 bool isCompressed = caps->isFormatCompressed(combo.fFormat);
Brian Salomon469046c2020-03-30 14:21:04 -0400128 bool isTexturable = caps->isFormatTexturable(combo.fFormat);
Robert Phillipsffe27292019-08-01 10:08:07 -0400129
Greg Daniel3a365112020-02-14 10:47:18 -0500130 sk_sp<GrSurface> tex = createTexture(kDims, combo.fColorType, combo.fFormat,
131 GrRenderable::kNo, resourceProvider);
132 REPORTER_ASSERT(reporter, SkToBool(tex) == isTexturable,
133 "ct:%s format:%s, tex:%d, isTexturable:%d",
134 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
135 SkToBool(tex), isTexturable);
Robert Phillipsffe27292019-08-01 10:08:07 -0400136
Greg Daniel3a365112020-02-14 10:47:18 -0500137 // Check that the lack of mipmap support blocks the creation of mipmapped
138 // proxies
Brian Salomon69100f02020-07-21 10:49:25 -0400139 bool expectedMipMapability = isTexturable && caps->mipmapSupport() && !isCompressed;
Robert Phillipsffe27292019-08-01 10:08:07 -0400140
Greg Daniel3a365112020-02-14 10:47:18 -0500141 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400142 combo.fFormat, kDims, GrRenderable::kNo, 1, GrMipmapped::kYes,
Greg Daniel3a365112020-02-14 10:47:18 -0500143 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
144 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == expectedMipMapability,
145 "ct:%s format:%s, tex:%d, expectedMipMapability:%d",
146 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
147 SkToBool(proxy.get()), expectedMipMapability);
148 }
149
150 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' (w/o MSAA)
151 {
152 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 1);
153
154 sk_sp<GrSurface> tex = resourceProvider->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400155 kDims, combo.fFormat, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBudgeted::kNo,
Greg Daniel3a365112020-02-14 10:47:18 -0500156 GrProtected::kNo);
157 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
158 "ct:%s format:%s, tex:%d, isRenderable:%d",
159 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
160 SkToBool(tex), isRenderable);
161 }
162
163 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' w/ MSAA
164 {
165 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 2);
166
167 sk_sp<GrSurface> tex = resourceProvider->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400168 kDims, combo.fFormat, GrRenderable::kYes, 2, GrMipmapped::kNo, SkBudgeted::kNo,
Greg Daniel3a365112020-02-14 10:47:18 -0500169 GrProtected::kNo);
170 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
171 "ct:%s format:%s, tex:%d, isRenderable:%d",
172 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
173 SkToBool(tex), isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400174 }
175 }
176}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400177
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500178#include "src/gpu/GrDrawingManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -0400179#include "src/gpu/GrSurfaceProxy.h"
Brian Salomond17b4a62017-05-23 16:53:47 -0400180
Robert Phillipsffe27292019-08-01 10:08:07 -0400181// For each context, set it to always clear the textures and then run through all the
182// supported formats checking that the textures are actually cleared
Brian Salomona3e29962019-07-16 11:52:08 -0400183DEF_GPUTEST(InitialTextureClear, reporter, baseOptions) {
184 GrContextOptions options = baseOptions;
185 options.fClearAllTextures = true;
Robert Phillipsd3442842019-08-02 12:26:22 -0400186
Brian Salomond17b4a62017-05-23 16:53:47 -0400187 static constexpr int kSize = 100;
Robert Phillipsd3442842019-08-02 12:26:22 -0400188 static constexpr SkColor kClearColor = 0xABABABAB;
Robert Phillipsffe27292019-08-01 10:08:07 -0400189
190 const SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
191 kPremul_SkAlphaType);
192
Robert Phillipsd3442842019-08-02 12:26:22 -0400193 SkAutoPixmapStorage readback;
194 readback.alloc(info);
195
Brian Salomona56a7462020-02-07 14:17:25 -0500196 SkISize desc;
Robert Phillipsffe27292019-08-01 10:08:07 -0400197 desc.fWidth = desc.fHeight = kSize;
198
Brian Salomona3e29962019-07-16 11:52:08 -0400199 for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
200 sk_gpu_test::GrContextFactory factory(options);
201 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
202 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400203 continue;
204 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400205 auto dContext = factory.get(contextType);
206 if (!dContext) {
Brian Salomona3e29962019-07-16 11:52:08 -0400207 continue;
208 }
Brian Salomona3e29962019-07-16 11:52:08 -0400209
Adlai Hollerc95b5892020-08-11 12:02:22 -0400210 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
211 const GrCaps* caps = dContext->priv().caps();
Brian Salomona3e29962019-07-16 11:52:08 -0400212
Robert Phillipsffe27292019-08-01 10:08:07 -0400213 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
John Stilesbd3ffa42020-07-30 20:24:57 -0400214 caps->getTestingCombinations();
Robert Phillipsffe27292019-08-01 10:08:07 -0400215
John Stilesbd3ffa42020-07-30 20:24:57 -0400216 for (const GrCaps::TestFormatColorTypeCombination& combo : combos) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400217
218 SkASSERT(combo.fColorType != GrColorType::kUnknown);
219 SkASSERT(combo.fFormat.isValid());
220
Brian Salomon469046c2020-03-30 14:21:04 -0400221 if (!caps->isFormatTexturable(combo.fFormat)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400222 continue;
223 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400224
Brian Salomon614cdab2019-09-26 11:04:28 -0400225 auto checkColor = [reporter](const GrCaps::TestFormatColorTypeCombination& combo,
226 uint32_t readColor) {
227 // We expect that if there is no alpha in the src color type and we read it to a
228 // color type with alpha that we will get one for alpha rather than zero. We used to
229 // require this but the Intel Iris 6100 on Win 10 test bot doesn't put one in the
230 // alpha channel when reading back from GL_RG16 or GL_RG16F. So now we allow either.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400231 uint32_t channels = GrColorTypeChannelFlags(combo.fColorType);
232 bool allowAlphaOne = !(channels & kAlpha_SkColorChannelFlag);
Brian Salomon614cdab2019-09-26 11:04:28 -0400233 if (allowAlphaOne) {
234 if (readColor != 0x00000000 && readColor != 0xFF000000) {
235 ERRORF(reporter,
236 "Failed on ct %s format %s 0x%08x is not 0x00000000 or 0xFF000000",
237 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
238 readColor);
239 return false;
240 }
241 } else {
242 if (readColor) {
243 ERRORF(reporter, "Failed on ct %s format %s 0x%08x != 0x00000000",
244 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
245 readColor);
246 return false;
247 }
248 }
249 return true;
250 };
Robert Phillipsffe27292019-08-01 10:08:07 -0400251
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400252 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400253 if (renderable == GrRenderable::kYes &&
Greg Daniel900583a2019-08-06 12:05:31 -0400254 !caps->isFormatAsColorTypeRenderable(combo.fColorType, combo.fFormat)) {
Brian Salomona3e29962019-07-16 11:52:08 -0400255 continue;
256 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400257
258 for (auto fit : {SkBackingFit::kApprox, SkBackingFit::kExact}) {
259
260 // Does directly allocating a texture clear it?
261 {
Chris Daltond004e0b2018-09-27 09:28:03 -0600262 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400263 {kSize, kSize}, combo.fFormat, renderable, 1, fit, SkBudgeted::kYes,
264 GrProtected::kNo);
Robert Phillipsffe27292019-08-01 10:08:07 -0400265 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500266 GrSwizzle swizzle = caps->getReadSwizzle(combo.fFormat,
267 combo.fColorType);
268 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
269 swizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400270 auto texCtx = GrSurfaceContext::Make(dContext, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500271 combo.fColorType,
272 kPremul_SkAlphaType, nullptr);
Robert Phillipsffe27292019-08-01 10:08:07 -0400273
Robert Phillipsd3442842019-08-02 12:26:22 -0400274 readback.erase(kClearColor);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400275 if (texCtx->readPixels(
276 dContext, readback.info(), readback.writable_addr(),
277 readback.rowBytes(), {0, 0})) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400278 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400279 if (!checkColor(combo, readback.addr32()[i])) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400280 break;
281 }
282 }
283 }
284 }
285
Adlai Hollerc95b5892020-08-11 12:02:22 -0400286 dContext->priv().testingOnly_purgeAllUnlockedResources();
Robert Phillipsffe27292019-08-01 10:08:07 -0400287 }
288
289 // Try creating the texture as a deferred proxy.
290 {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400291 std::unique_ptr<GrSurfaceContext> surfCtx;
Robert Phillipsffe27292019-08-01 10:08:07 -0400292 if (renderable == GrRenderable::kYes) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500293 surfCtx = GrRenderTargetContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400294 dContext, combo.fColorType, nullptr, fit,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400295 {desc.fWidth, desc.fHeight}, 1, GrMipmapped::kNo,
Greg Daniele20fcad2020-01-08 11:52:34 -0500296 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Robert Phillipsffe27292019-08-01 10:08:07 -0400297 } else {
Greg Danielbfa19c42019-12-19 16:41:40 -0500298 surfCtx = GrSurfaceContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400299 dContext, {desc.fWidth, desc.fHeight}, combo.fFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400300 GrRenderable::kNo, 1, GrMipmapped::kNo, GrProtected::kNo,
Greg Danielbfa19c42019-12-19 16:41:40 -0500301 kTopLeft_GrSurfaceOrigin, combo.fColorType,
302 kUnknown_SkAlphaType, nullptr, fit, SkBudgeted::kYes);
Robert Phillipsffe27292019-08-01 10:08:07 -0400303 }
304 if (!surfCtx) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400305 continue;
306 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400307
Robert Phillipsd3442842019-08-02 12:26:22 -0400308 readback.erase(kClearColor);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400309 if (surfCtx->readPixels(dContext, readback.info(), readback.writable_addr(),
Robert Phillipsd3442842019-08-02 12:26:22 -0400310 readback.rowBytes(), {0, 0})) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400311 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400312 if (!checkColor(combo, readback.addr32()[i])) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400313 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400314 }
315 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400316 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400317 dContext->priv().testingOnly_purgeAllUnlockedResources();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400318 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400319 }
320 }
321 }
322 }
323}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500324
325DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400326 auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500327 for (int y = 0; y < p->height(); ++y) {
328 for (int x = 0; x < p->width(); ++x) {
329 *p->writable_addr32(x, y) = f(x, y);
330 }
331 }
332 };
333
334 auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) {
335 SkASSERT(p1.info() == p2.info());
336 for (int y = 0; y < p1.height(); ++y) {
337 for (int x = 0; x < p1.width(); ++x) {
338 REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y));
339 if (p1.getColor(x, y) != p2.getColor(x, y)) {
340 return;
341 }
342 }
343 }
344 };
345
346 static constexpr int kSize = 100;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400347 SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
348 SkAutoPixmapStorage srcPixmap;
349 srcPixmap.alloc(ii);
350 fillPixels(&srcPixmap,
Robert Phillipscb1adb42019-06-10 15:09:34 -0400351 [](int x, int y) {
352 return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
353 });
Brian Salomonc67c31c2018-12-06 10:00:03 -0500354
Adlai Hollerc95b5892020-08-11 12:02:22 -0400355 auto dContext = context_info.directContext();
356 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500357
358 // We test both kRW in addition to kRead mostly to ensure that the calls are structured such
359 // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
360 // kRead for the right reason.
361 for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
Brian Salomon72050802020-10-12 20:45:06 -0400362 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithData(
363 dContext, srcPixmap, GrRenderable::kNo, GrProtected::kNo);
Brian Salomon88d7e622020-11-05 11:11:20 -0500364 if (!mbet) {
365 ERRORF(reporter, "Could not make texture.");
366 return;
367 }
Brian Salomon72050802020-10-12 20:45:06 -0400368 auto proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership,
369 GrWrapCacheable::kNo, ioType,
370 mbet->refCountedCallback());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400371 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(),
372 GrColorType::kRGBA_8888);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500373 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400374 auto surfContext = GrSurfaceContext::Make(dContext, std::move(view), GrColorType::kRGBA_8888,
Greg Danielbfa19c42019-12-19 16:41:40 -0500375 kPremul_SkAlphaType, nullptr);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500376
377 // Read pixels should work with a read-only texture.
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400378 {
379 SkAutoPixmapStorage read;
380 read.alloc(srcPixmap.info());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400381 auto readResult = surfContext->readPixels(dContext, srcPixmap.info(),
382 read.writable_addr(), 0, { 0, 0 });
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400383 REPORTER_ASSERT(reporter, readResult);
384 if (readResult) {
385 comparePixels(srcPixmap, read, reporter);
386 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500387 }
388
389 // Write pixels should not work with a read-only texture.
390 SkAutoPixmapStorage write;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400391 write.alloc(srcPixmap.info());
392 fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); });
Adlai Hollerc95b5892020-08-11 12:02:22 -0400393 auto writeResult = surfContext->writePixels(dContext, srcPixmap.info(), write.addr(),
394 0, {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500395 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
396 // Try the low level write.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400397 dContext->flushAndSubmit();
398 auto gpuWriteResult = dContext->priv().getGpu()->writePixels(
Brian Salomonf77c1462019-08-01 15:19:29 -0400399 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888,
400 GrColorType::kRGBA_8888, write.addr32(),
Brian Salomon1047a492019-07-02 12:25:21 -0400401 kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500402 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
403
Greg Daniel6f5441a2020-01-28 17:02:49 -0500404 SkBitmap copySrcBitmap;
405 copySrcBitmap.installPixels(write);
406 copySrcBitmap.setImmutable();
407
Adlai Hollerc95b5892020-08-11 12:02:22 -0400408 GrBitmapTextureMaker maker(dContext, copySrcBitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400409 GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400410 auto copySrc = maker.view(GrMipmapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500411
Greg Danielcc104db2020-02-03 14:17:08 -0500412 REPORTER_ASSERT(reporter, copySrc.proxy());
Brian Salomonc5243782020-04-02 12:50:34 -0400413 auto copyResult = surfContext->testCopy(copySrc.proxy());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500414 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
415 // Try the low level copy.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400416 dContext->flushAndSubmit();
417 auto gpuCopyResult = dContext->priv().getGpu()->copySurface(
Greg Danielcc104db2020-02-03 14:17:08 -0500418 proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400419 {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500420 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
421
422 // Mip regen should not work with a read only texture.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400423 if (dContext->priv().caps()->mipmapSupport()) {
Brian Salomon72050802020-10-12 20:45:06 -0400424 mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(dContext,
425 kSize,
426 kSize,
427 kRGBA_8888_SkColorType,
428 GrMipmapped::kYes,
429 GrRenderable::kNo,
430 GrProtected::kNo);
431 proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership,
432 GrWrapCacheable::kNo, ioType,
433 mbet->refCountedCallback());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400434 dContext->flushAndSubmit();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400435 proxy->peekTexture()->markMipmapsDirty(); // avoids assert in GrGpu.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500436 auto regenResult =
Adlai Hollerc95b5892020-08-11 12:02:22 -0400437 dContext->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500438 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
439 }
440 }
441}
Brian Salomon614c1a82018-12-19 15:42:06 -0500442
Greg Daniel46cfbc62019-06-07 11:43:30 -0400443static const int kSurfSize = 10;
444
Robert Phillipseffd13f2020-07-20 15:00:36 -0400445static sk_sp<GrTexture> make_wrapped_texture(GrDirectContext* dContext, GrRenderable renderable) {
Brian Salomon72050802020-10-12 20:45:06 -0400446 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
447 dContext, kSurfSize, kSurfSize, kRGBA_8888_SkColorType, GrMipmapped::kNo, renderable);
448 SkASSERT(mbet);
449 sk_sp<GrTextureProxy> proxy;
450 if (renderable == GrRenderable::kYes) {
451 proxy = dContext->priv().proxyProvider()->wrapRenderableBackendTexture(
452 mbet->texture(), 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
453 mbet->refCountedCallback());
Brian Salomon8cabb322019-02-22 10:44:19 -0500454 } else {
Brian Salomon72050802020-10-12 20:45:06 -0400455 proxy = dContext->priv().proxyProvider()->wrapBackendTexture(
456 mbet->texture(), kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType,
457 mbet->refCountedCallback());
Brian Salomon8cabb322019-02-22 10:44:19 -0500458 }
Brian Salomon72050802020-10-12 20:45:06 -0400459 if (!proxy) {
460 return nullptr;
461 }
462 return sk_ref_sp(proxy->peekTexture());
Brian Salomon8cabb322019-02-22 10:44:19 -0500463}
Brian Salomon614c1a82018-12-19 15:42:06 -0500464
Robert Phillipseffd13f2020-07-20 15:00:36 -0400465static sk_sp<GrTexture> make_normal_texture(GrDirectContext* dContext, GrRenderable renderable) {
Brian Salomona56a7462020-02-07 14:17:25 -0500466 SkISize desc;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400467 desc.fWidth = desc.fHeight = kSurfSize;
Robert Phillipseffd13f2020-07-20 15:00:36 -0400468 auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
469 renderable);
470 return dContext->priv().resourceProvider()->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400471 desc, format, renderable, 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500472}
Brian Salomon614c1a82018-12-19 15:42:06 -0500473
Brian Salomon8cabb322019-02-22 10:44:19 -0500474DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
475 // Various ways of making textures.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400476 auto makeWrapped = [](GrDirectContext* dContext) {
477 return make_wrapped_texture(dContext, GrRenderable::kNo);
Brian Salomon614c1a82018-12-19 15:42:06 -0500478 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400479 auto makeWrappedRenderable = [](GrDirectContext* dContext) {
480 return make_wrapped_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400481 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400482 auto makeNormal = [](GrDirectContext* dContext) {
483 return make_normal_texture(dContext, GrRenderable::kNo);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400484 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400485 auto makeRenderable = [](GrDirectContext* dContext) {
486 return make_normal_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400487 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500488
Robert Phillipseffd13f2020-07-20 15:00:36 -0400489 std::function<sk_sp<GrTexture>(GrDirectContext*)> makers[] = {
490 makeWrapped,
491 makeWrappedRenderable,
492 makeNormal,
493 makeRenderable
494 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500495
496 // Add a unique key, or not.
497 auto addKey = [](GrTexture* texture) {
498 static uint32_t gN = 0;
499 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
500 GrUniqueKey key;
501 GrUniqueKey::Builder builder(&key, kDomain, 1);
502 builder[0] = gN++;
503 builder.finish();
504 texture->resourcePriv().setUniqueKey(key);
505 };
506 auto dontAddKey = [](GrTexture* texture) {};
507 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
508
509 for (const auto& m : makers) {
510 for (const auto& keyAdder : keyAdders) {
511 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
512 sk_gpu_test::GrContextFactory factory;
513 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400514 auto dContext = factory.get(contextType);
515 if (!dContext) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500516 continue;
517 }
518
519 // The callback we add simply adds an integer to a set.
520 std::set<int> idleIDs;
521 struct Context {
522 std::set<int>* fIdleIDs;
523 int fNum;
524 };
525 auto proc = [](void* context) {
526 static_cast<Context*>(context)->fIdleIDs->insert(
527 static_cast<Context*>(context)->fNum);
528 delete static_cast<Context*>(context);
529 };
530
531 // Makes a texture, possibly adds a key, and sets the callback.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400532 auto make = [&m, &keyAdder, &proc, &idleIDs](GrDirectContext* dContext, int num) {
533 sk_sp<GrTexture> texture = m(dContext);
Brian Salomonf1432742020-11-09 15:40:27 -0500534 texture->addIdleProc(proc, new Context{&idleIDs, num});
Brian Salomon614c1a82018-12-19 15:42:06 -0500535 keyAdder(texture.get());
536 return texture;
537 };
538
Robert Phillipseffd13f2020-07-20 15:00:36 -0400539 auto texture = make(dContext, 1);
Brian Salomon614c1a82018-12-19 15:42:06 -0500540 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400541 auto renderable = GrRenderable(SkToBool(texture->asRenderTarget()));
Brian Salomon614c1a82018-12-19 15:42:06 -0500542 auto backendFormat = texture->backendFormat();
543 texture.reset();
544 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
545
Robert Phillipseffd13f2020-07-20 15:00:36 -0400546 texture = make(dContext, 2);
Brian Salomon8cabb322019-02-22 10:44:19 -0500547 int w = texture->width();
548 int h = texture->height();
Brian Salomon614c1a82018-12-19 15:42:06 -0500549 SkImageInfo info =
Brian Salomon8cabb322019-02-22 10:44:19 -0500550 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400551 auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0, nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500552 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomon63410e92020-03-23 18:32:50 -0400553 auto singleUseLazyCB = [&texture](GrResourceProvider*,
554 const GrSurfaceProxy::LazySurfaceDesc&) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400555 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
556 if (texture->getUniqueKey().isValid()) {
557 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
558 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400559 return GrSurfaceProxy::LazyCallbackResult{std::move(texture), true, mode};
Brian Salomon614c1a82018-12-19 15:42:06 -0500560 };
Brian Salomona56a7462020-02-07 14:17:25 -0500561 SkISize desc;
Brian Salomon8cabb322019-02-22 10:44:19 -0500562 desc.fWidth = w;
563 desc.fHeight = h;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500564 SkBudgeted budgeted;
565 if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
566 budgeted = SkBudgeted::kYes;
567 } else {
568 budgeted = SkBudgeted::kNo;
569 }
Greg Danielc113f002020-08-26 13:28:22 -0400570 sk_sp<GrSurfaceProxy> proxy;
571 if (renderable == GrRenderable::kYes) {
572 static const GrProxyProvider::TextureInfo kTexInfo = {GrMipMapped::kNo,
573 GrTextureType::k2D};
574 proxy = dContext->priv().proxyProvider()->createLazyRenderTargetProxy(
575 singleUseLazyCB, backendFormat, desc, 1,
Greg Daniel638b2e82020-08-27 14:29:00 -0400576 dContext->priv().caps()->getExtraSurfaceFlagsForDeferredRT(),
577 &kTexInfo,
Greg Danielc113f002020-08-26 13:28:22 -0400578 GrMipmapStatus::kNotAllocated,
579 SkBackingFit::kExact, budgeted, GrProtected::kNo, false,
580 GrSurfaceProxy::UseAllocator::kYes);
581 } else {
582 proxy = dContext->priv().proxyProvider()->createLazyProxy(
583 singleUseLazyCB, backendFormat, desc, GrMipmapped::kNo,
584 GrMipmapStatus::kNotAllocated, GrInternalSurfaceFlags ::kNone,
585 SkBackingFit::kExact, budgeted, GrProtected::kNo,
586 GrSurfaceProxy::UseAllocator::kYes);
587 }
Robert Phillipseffd13f2020-07-20 15:00:36 -0400588 GrSwizzle readSwizzle = dContext->priv().caps()->getReadSwizzle(
Greg Danielce3ddaa2020-01-22 16:58:15 -0500589 backendFormat, GrColorType::kRGBA_8888);
Greg Daniel40903af2020-01-30 14:55:05 -0500590 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, readSwizzle);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400591 rtc->drawTexture(nullptr,
592 view,
593 kPremul_SkAlphaType,
594 GrSamplerState::Filter::kNearest,
595 GrSamplerState::MipmapMode::kNone,
596 SkBlendMode::kSrcOver,
597 SkPMColor4f(),
598 SkRect::MakeWH(w, h),
599 SkRect::MakeWH(w, h),
600 GrAA::kNo,
601 GrQuadAAFlags::kNone,
602 SkCanvas::kFast_SrcRectConstraint,
603 SkMatrix::I(),
604 nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500605 // We still have the proxy, which should remain instantiated, thereby keeping the
606 // texture not purgeable.
607 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400608 dContext->flushAndSubmit();
Brian Salomon614c1a82018-12-19 15:42:06 -0500609 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400610 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500611 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
612
613 // This time we move the proxy into the draw.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400614 rtc->drawTexture(nullptr,
615 std::move(view),
616 kPremul_SkAlphaType,
617 GrSamplerState::Filter::kNearest,
618 GrSamplerState::MipmapMode::kNone,
619 SkBlendMode::kSrcOver,
620 SkPMColor4f(),
621 SkRect::MakeWH(w, h),
622 SkRect::MakeWH(w, h),
623 GrAA::kNo,
624 GrQuadAAFlags::kNone,
625 SkCanvas::kFast_SrcRectConstraint,
626 SkMatrix::I(),
627 nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500628 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400629 dContext->flushAndSubmit();
630 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500631 // Now that the draw is fully consumed by the GPU, the texture should be idle.
632 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
633
Brian Salomon9bc76d92019-01-24 12:18:33 -0500634 // Make sure we make the call during various shutdown scenarios where the texture
635 // might persist after context is destroyed, abandoned, etc. We test three
636 // variations of each scenario. One where the texture is just created. Another,
637 // where the texture has been used in a draw and then the context is flushed. And
638 // one where the the texture was drawn but the context is not flushed.
639 // In each scenario we test holding a ref beyond the context shutdown and not.
Brian Salomon614c1a82018-12-19 15:42:06 -0500640
Stephen Whiteb007bb22020-06-04 09:02:04 -0400641 // These tests are difficult to get working with Vulkan, Direct3D, and Dawn.
642 // See http://skbug.com/8705, http://skbug.com/8277, and http://skbug.com/10326
Brian Salomon9bc76d92019-01-24 12:18:33 -0500643 GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
Stephen Whiteb007bb22020-06-04 09:02:04 -0400644 if (api == GrBackendApi::kVulkan || api == GrBackendApi::kDirect3D ||
645 api == GrBackendApi::kDawn) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500646 continue;
647 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400648 int id = 3;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500649 enum class DrawType {
650 kNoDraw,
651 kDraw,
652 kDrawAndFlush,
653 };
654 for (auto drawType :
655 {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) {
656 for (bool unrefFirst : {false, true}) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400657 auto possiblyDrawAndFlush = [&dContext, &texture, drawType, unrefFirst, w,
Brian Salomon8cabb322019-02-22 10:44:19 -0500658 h] {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500659 if (drawType == DrawType::kNoDraw) {
660 return;
661 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500662 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500663 kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400664 auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500665 nullptr);
Robert Phillips9da87e02019-02-04 13:26:26 -0500666 auto rtc = rt->getCanvas()
Brian Salomon9bc76d92019-01-24 12:18:33 -0500667 ->internal_private_accessTopLayerRenderTargetContext();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400668 auto proxy = dContext->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400669 texture);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400670 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(
Greg Daniel40903af2020-01-30 14:55:05 -0500671 proxy->backendFormat(), GrColorType::kRGBA_8888);
672 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
673 swizzle);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400674 rtc->drawTexture(nullptr,
675 std::move(view),
676 kPremul_SkAlphaType,
677 GrSamplerState::Filter::kNearest,
678 GrSamplerState::MipmapMode::kNone,
679 SkBlendMode::kSrcOver,
680 SkPMColor4f(),
681 SkRect::MakeWH(w, h),
682 SkRect::MakeWH(w, h),
683 GrAA::kNo,
684 GrQuadAAFlags::kNone,
685 SkCanvas::kFast_SrcRectConstraint,
686 SkMatrix::I(),
687 nullptr);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500688 if (drawType == DrawType::kDrawAndFlush) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400689 dContext->flushAndSubmit();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500690 }
691 if (unrefFirst) {
692 texture.reset();
693 }
694 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400695 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500696 possiblyDrawAndFlush();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400697 dContext->abandonContext();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500698 texture.reset();
699 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
700 factory.destroyContexts();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400701 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500702 ++id;
Brian Salomon614c1a82018-12-19 15:42:06 -0500703
Brian Salomon9bc76d92019-01-24 12:18:33 -0500704 // Similar to previous, but reset the texture after the context was
705 // abandoned and then destroyed.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400706 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500707 possiblyDrawAndFlush();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400708 dContext->abandonContext();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500709 factory.destroyContexts();
710 texture.reset();
711 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400712 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500713 id++;
714
Robert Phillipseffd13f2020-07-20 15:00:36 -0400715 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500716 possiblyDrawAndFlush();
717 factory.destroyContexts();
718 texture.reset();
719 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400720 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500721 id++;
722
Robert Phillipseffd13f2020-07-20 15:00:36 -0400723 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500724 possiblyDrawAndFlush();
725 factory.releaseResourcesAndAbandonContexts();
726 texture.reset();
727 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400728 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500729 id++;
730 }
731 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500732 }
733 }
734 }
735}
Brian Salomon8cabb322019-02-22 10:44:19 -0500736
737// Tests an idle proc that unrefs another resource down to zero.
738DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400739 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500740
741 // idle proc that releases another texture.
742 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); };
743
744 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
745 for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomonf1432742020-11-09 15:40:27 -0500746 auto idleTexture = idleMaker(context, GrRenderable::kNo);
747 auto otherTexture = otherMaker(context, GrRenderable::kNo);
748 otherTexture->ref();
749 idleTexture->addIdleProc(idleProc, otherTexture.get());
750 otherTexture.reset();
751 idleTexture.reset();
Brian Salomon8cabb322019-02-22 10:44:19 -0500752 }
753 }
754}
755
756// Similar to above but more complicated. This flushes the context from the idle proc.
757// crbug.com/933526.
758DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400759 auto dContext = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500760
761 // idle proc that flushes the context.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400762 auto idleProc = [](void* context) {
763 reinterpret_cast<GrDirectContext*>(context)->flushAndSubmit();
764 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500765
766 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomonf1432742020-11-09 15:40:27 -0500767 auto idleTexture = idleMaker(dContext, GrRenderable::kNo);
768 idleTexture->addIdleProc(idleProc, dContext);
769 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
770 auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 1, nullptr);
771 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
772 // is a wrapped-texture backed image.
773 surf->getCanvas()->clear(SK_ColorWHITE);
774 auto img1 = surf->makeImageSnapshot();
775 auto img2 = sk_gpu_test::MakeBackendTextureImage(dContext, info, SkColors::kBlack);
776 REPORTER_ASSERT(reporter, img1 && img2);
777 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
778 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
779 idleTexture.reset();
Brian Salomon8cabb322019-02-22 10:44:19 -0500780 }
781}
782
783DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400784 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500785 // idle proc that refs the texture
786 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
787 // release proc to check whether the texture was released or not.
788 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomonf1432742020-11-09 15:40:27 -0500789 bool isReleased = false;
790 auto idleTexture = make_normal_texture(context, GrRenderable::kNo);
791 // This test assumes the texture won't be cached (or else the release proc doesn't get
792 // called).
793 idleTexture->resourcePriv().removeScratchKey();
794 context->flushAndSubmit();
795 idleTexture->addIdleProc(idleProc, idleTexture.get());
796 idleTexture->setRelease(releaseProc, &isReleased);
797 auto* raw = idleTexture.get();
798 idleTexture.reset();
799 REPORTER_ASSERT(reporter, !isReleased);
800 raw->unref();
801 REPORTER_ASSERT(reporter, isReleased);
Brian Salomone80b8092019-03-08 13:25:19 -0500802}
803
804DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400805 auto context = contextInfo.directContext();
Brian Salomone80b8092019-03-08 13:25:19 -0500806 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400807 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500808
Brian Salomonf1432742020-11-09 15:40:27 -0500809 bool called = false;
810 auto finishProc = [](void* called) { *static_cast<bool*>(called) = true; };
811 idleTexture->addIdleProc(finishProc, &called);
Brian Salomone80b8092019-03-08 13:25:19 -0500812
813 // Insert a copy from idleTexture to another texture so that we have some queued IO on
814 // idleTexture.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400815 SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
816 kPremul_SkAlphaType);
Brian Salomone80b8092019-03-08 13:25:19 -0500817 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
818 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400819 auto proxy =
820 context->priv().proxyProvider()->testingOnly_createWrapped(std::move(idleTexture));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400821 context->flushAndSubmit();
Brian Salomonc5243782020-04-02 12:50:34 -0400822 SkAssertResult(rtc->testCopy(proxy.get()));
Brian Salomone80b8092019-03-08 13:25:19 -0500823 proxy.reset();
Brian Salomonf1432742020-11-09 15:40:27 -0500824 REPORTER_ASSERT(reporter, !called);
Brian Salomone80b8092019-03-08 13:25:19 -0500825
826 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
827 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
828 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400829 context->flushAndSubmit();
Brian Salomonf1432742020-11-09 15:40:27 -0500830 if (contextInfo.backend() != kVulkan_GrBackend) {
831 REPORTER_ASSERT(reporter, called);
Brian Salomone80b8092019-03-08 13:25:19 -0500832 }
Brian Salomonf1432742020-11-09 15:40:27 -0500833 context->submit(true);
834 REPORTER_ASSERT(reporter, called);
Brian Salomone80b8092019-03-08 13:25:19 -0500835 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500836}