blob: 05dd4270145cff98ba3ff4761564efadee25fd96 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
16#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);
364 auto proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership,
365 GrWrapCacheable::kNo, ioType,
366 mbet->refCountedCallback());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400367 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(),
368 GrColorType::kRGBA_8888);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500369 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400370 auto surfContext = GrSurfaceContext::Make(dContext, std::move(view), GrColorType::kRGBA_8888,
Greg Danielbfa19c42019-12-19 16:41:40 -0500371 kPremul_SkAlphaType, nullptr);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500372
373 // Read pixels should work with a read-only texture.
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400374 {
375 SkAutoPixmapStorage read;
376 read.alloc(srcPixmap.info());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400377 auto readResult = surfContext->readPixels(dContext, srcPixmap.info(),
378 read.writable_addr(), 0, { 0, 0 });
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400379 REPORTER_ASSERT(reporter, readResult);
380 if (readResult) {
381 comparePixels(srcPixmap, read, reporter);
382 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500383 }
384
385 // Write pixels should not work with a read-only texture.
386 SkAutoPixmapStorage write;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400387 write.alloc(srcPixmap.info());
388 fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); });
Adlai Hollerc95b5892020-08-11 12:02:22 -0400389 auto writeResult = surfContext->writePixels(dContext, srcPixmap.info(), write.addr(),
390 0, {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500391 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
392 // Try the low level write.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400393 dContext->flushAndSubmit();
394 auto gpuWriteResult = dContext->priv().getGpu()->writePixels(
Brian Salomonf77c1462019-08-01 15:19:29 -0400395 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888,
396 GrColorType::kRGBA_8888, write.addr32(),
Brian Salomon1047a492019-07-02 12:25:21 -0400397 kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500398 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
399
Greg Daniel6f5441a2020-01-28 17:02:49 -0500400 SkBitmap copySrcBitmap;
401 copySrcBitmap.installPixels(write);
402 copySrcBitmap.setImmutable();
403
Adlai Hollerc95b5892020-08-11 12:02:22 -0400404 GrBitmapTextureMaker maker(dContext, copySrcBitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400405 GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400406 auto copySrc = maker.view(GrMipmapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500407
Greg Danielcc104db2020-02-03 14:17:08 -0500408 REPORTER_ASSERT(reporter, copySrc.proxy());
Brian Salomonc5243782020-04-02 12:50:34 -0400409 auto copyResult = surfContext->testCopy(copySrc.proxy());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500410 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
411 // Try the low level copy.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400412 dContext->flushAndSubmit();
413 auto gpuCopyResult = dContext->priv().getGpu()->copySurface(
Greg Danielcc104db2020-02-03 14:17:08 -0500414 proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400415 {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500416 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
417
418 // Mip regen should not work with a read only texture.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400419 if (dContext->priv().caps()->mipmapSupport()) {
Brian Salomon72050802020-10-12 20:45:06 -0400420 mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(dContext,
421 kSize,
422 kSize,
423 kRGBA_8888_SkColorType,
424 GrMipmapped::kYes,
425 GrRenderable::kNo,
426 GrProtected::kNo);
427 proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership,
428 GrWrapCacheable::kNo, ioType,
429 mbet->refCountedCallback());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400430 dContext->flushAndSubmit();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400431 proxy->peekTexture()->markMipmapsDirty(); // avoids assert in GrGpu.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500432 auto regenResult =
Adlai Hollerc95b5892020-08-11 12:02:22 -0400433 dContext->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500434 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
435 }
436 }
437}
Brian Salomon614c1a82018-12-19 15:42:06 -0500438
Greg Daniel46cfbc62019-06-07 11:43:30 -0400439static const int kSurfSize = 10;
440
Robert Phillipseffd13f2020-07-20 15:00:36 -0400441static sk_sp<GrTexture> make_wrapped_texture(GrDirectContext* dContext, GrRenderable renderable) {
Brian Salomon72050802020-10-12 20:45:06 -0400442 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
443 dContext, kSurfSize, kSurfSize, kRGBA_8888_SkColorType, GrMipmapped::kNo, renderable);
444 SkASSERT(mbet);
445 sk_sp<GrTextureProxy> proxy;
446 if (renderable == GrRenderable::kYes) {
447 proxy = dContext->priv().proxyProvider()->wrapRenderableBackendTexture(
448 mbet->texture(), 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
449 mbet->refCountedCallback());
Brian Salomon8cabb322019-02-22 10:44:19 -0500450 } else {
Brian Salomon72050802020-10-12 20:45:06 -0400451 proxy = dContext->priv().proxyProvider()->wrapBackendTexture(
452 mbet->texture(), kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType,
453 mbet->refCountedCallback());
Brian Salomon8cabb322019-02-22 10:44:19 -0500454 }
Brian Salomon72050802020-10-12 20:45:06 -0400455 if (!proxy) {
456 return nullptr;
457 }
458 return sk_ref_sp(proxy->peekTexture());
Brian Salomon8cabb322019-02-22 10:44:19 -0500459}
Brian Salomon614c1a82018-12-19 15:42:06 -0500460
Robert Phillipseffd13f2020-07-20 15:00:36 -0400461static sk_sp<GrTexture> make_normal_texture(GrDirectContext* dContext, GrRenderable renderable) {
Brian Salomona56a7462020-02-07 14:17:25 -0500462 SkISize desc;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400463 desc.fWidth = desc.fHeight = kSurfSize;
Robert Phillipseffd13f2020-07-20 15:00:36 -0400464 auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
465 renderable);
466 return dContext->priv().resourceProvider()->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400467 desc, format, renderable, 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500468}
Brian Salomon614c1a82018-12-19 15:42:06 -0500469
Brian Salomon8cabb322019-02-22 10:44:19 -0500470DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
471 // Various ways of making textures.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400472 auto makeWrapped = [](GrDirectContext* dContext) {
473 return make_wrapped_texture(dContext, GrRenderable::kNo);
Brian Salomon614c1a82018-12-19 15:42:06 -0500474 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400475 auto makeWrappedRenderable = [](GrDirectContext* dContext) {
476 return make_wrapped_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400477 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400478 auto makeNormal = [](GrDirectContext* dContext) {
479 return make_normal_texture(dContext, GrRenderable::kNo);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400480 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400481 auto makeRenderable = [](GrDirectContext* dContext) {
482 return make_normal_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400483 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500484
Robert Phillipseffd13f2020-07-20 15:00:36 -0400485 std::function<sk_sp<GrTexture>(GrDirectContext*)> makers[] = {
486 makeWrapped,
487 makeWrappedRenderable,
488 makeNormal,
489 makeRenderable
490 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500491
492 // Add a unique key, or not.
493 auto addKey = [](GrTexture* texture) {
494 static uint32_t gN = 0;
495 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
496 GrUniqueKey key;
497 GrUniqueKey::Builder builder(&key, kDomain, 1);
498 builder[0] = gN++;
499 builder.finish();
500 texture->resourcePriv().setUniqueKey(key);
501 };
502 auto dontAddKey = [](GrTexture* texture) {};
503 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
504
505 for (const auto& m : makers) {
506 for (const auto& keyAdder : keyAdders) {
507 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
508 sk_gpu_test::GrContextFactory factory;
509 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400510 auto dContext = factory.get(contextType);
511 if (!dContext) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500512 continue;
513 }
514
515 // The callback we add simply adds an integer to a set.
516 std::set<int> idleIDs;
517 struct Context {
518 std::set<int>* fIdleIDs;
519 int fNum;
520 };
521 auto proc = [](void* context) {
522 static_cast<Context*>(context)->fIdleIDs->insert(
523 static_cast<Context*>(context)->fNum);
524 delete static_cast<Context*>(context);
525 };
526
527 // Makes a texture, possibly adds a key, and sets the callback.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400528 auto make = [&m, &keyAdder, &proc, &idleIDs](GrDirectContext* dContext, int num) {
529 sk_sp<GrTexture> texture = m(dContext);
Brian Salomone80b8092019-03-08 13:25:19 -0500530 texture->addIdleProc(proc, new Context{&idleIDs, num},
531 GrTexture::IdleState::kFinished);
Brian Salomon614c1a82018-12-19 15:42:06 -0500532 keyAdder(texture.get());
533 return texture;
534 };
535
Robert Phillipseffd13f2020-07-20 15:00:36 -0400536 auto texture = make(dContext, 1);
Brian Salomon614c1a82018-12-19 15:42:06 -0500537 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400538 auto renderable = GrRenderable(SkToBool(texture->asRenderTarget()));
Brian Salomon614c1a82018-12-19 15:42:06 -0500539 auto backendFormat = texture->backendFormat();
540 texture.reset();
541 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
542
Robert Phillipseffd13f2020-07-20 15:00:36 -0400543 texture = make(dContext, 2);
Brian Salomon8cabb322019-02-22 10:44:19 -0500544 int w = texture->width();
545 int h = texture->height();
Brian Salomon614c1a82018-12-19 15:42:06 -0500546 SkImageInfo info =
Brian Salomon8cabb322019-02-22 10:44:19 -0500547 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400548 auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0, nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500549 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomon63410e92020-03-23 18:32:50 -0400550 auto singleUseLazyCB = [&texture](GrResourceProvider*,
551 const GrSurfaceProxy::LazySurfaceDesc&) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400552 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
553 if (texture->getUniqueKey().isValid()) {
554 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
555 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400556 return GrSurfaceProxy::LazyCallbackResult{std::move(texture), true, mode};
Brian Salomon614c1a82018-12-19 15:42:06 -0500557 };
Brian Salomona56a7462020-02-07 14:17:25 -0500558 SkISize desc;
Brian Salomon8cabb322019-02-22 10:44:19 -0500559 desc.fWidth = w;
560 desc.fHeight = h;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500561 SkBudgeted budgeted;
562 if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
563 budgeted = SkBudgeted::kYes;
564 } else {
565 budgeted = SkBudgeted::kNo;
566 }
Greg Danielc113f002020-08-26 13:28:22 -0400567 sk_sp<GrSurfaceProxy> proxy;
568 if (renderable == GrRenderable::kYes) {
569 static const GrProxyProvider::TextureInfo kTexInfo = {GrMipMapped::kNo,
570 GrTextureType::k2D};
571 proxy = dContext->priv().proxyProvider()->createLazyRenderTargetProxy(
572 singleUseLazyCB, backendFormat, desc, 1,
Greg Daniel638b2e82020-08-27 14:29:00 -0400573 dContext->priv().caps()->getExtraSurfaceFlagsForDeferredRT(),
574 &kTexInfo,
Greg Danielc113f002020-08-26 13:28:22 -0400575 GrMipmapStatus::kNotAllocated,
576 SkBackingFit::kExact, budgeted, GrProtected::kNo, false,
577 GrSurfaceProxy::UseAllocator::kYes);
578 } else {
579 proxy = dContext->priv().proxyProvider()->createLazyProxy(
580 singleUseLazyCB, backendFormat, desc, GrMipmapped::kNo,
581 GrMipmapStatus::kNotAllocated, GrInternalSurfaceFlags ::kNone,
582 SkBackingFit::kExact, budgeted, GrProtected::kNo,
583 GrSurfaceProxy::UseAllocator::kYes);
584 }
Robert Phillipseffd13f2020-07-20 15:00:36 -0400585 GrSwizzle readSwizzle = dContext->priv().caps()->getReadSwizzle(
Greg Danielce3ddaa2020-01-22 16:58:15 -0500586 backendFormat, GrColorType::kRGBA_8888);
Greg Daniel40903af2020-01-30 14:55:05 -0500587 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, readSwizzle);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400588 rtc->drawTexture(nullptr,
589 view,
590 kPremul_SkAlphaType,
591 GrSamplerState::Filter::kNearest,
592 GrSamplerState::MipmapMode::kNone,
593 SkBlendMode::kSrcOver,
594 SkPMColor4f(),
595 SkRect::MakeWH(w, h),
596 SkRect::MakeWH(w, h),
597 GrAA::kNo,
598 GrQuadAAFlags::kNone,
599 SkCanvas::kFast_SrcRectConstraint,
600 SkMatrix::I(),
601 nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500602 // We still have the proxy, which should remain instantiated, thereby keeping the
603 // texture not purgeable.
604 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400605 dContext->flushAndSubmit();
Brian Salomon614c1a82018-12-19 15:42:06 -0500606 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400607 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500608 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
609
610 // This time we move the proxy into the draw.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400611 rtc->drawTexture(nullptr,
612 std::move(view),
613 kPremul_SkAlphaType,
614 GrSamplerState::Filter::kNearest,
615 GrSamplerState::MipmapMode::kNone,
616 SkBlendMode::kSrcOver,
617 SkPMColor4f(),
618 SkRect::MakeWH(w, h),
619 SkRect::MakeWH(w, h),
620 GrAA::kNo,
621 GrQuadAAFlags::kNone,
622 SkCanvas::kFast_SrcRectConstraint,
623 SkMatrix::I(),
624 nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500625 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400626 dContext->flushAndSubmit();
627 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500628 // Now that the draw is fully consumed by the GPU, the texture should be idle.
629 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
630
Brian Salomon9bc76d92019-01-24 12:18:33 -0500631 // Make sure we make the call during various shutdown scenarios where the texture
632 // might persist after context is destroyed, abandoned, etc. We test three
633 // variations of each scenario. One where the texture is just created. Another,
634 // where the texture has been used in a draw and then the context is flushed. And
635 // one where the the texture was drawn but the context is not flushed.
636 // In each scenario we test holding a ref beyond the context shutdown and not.
Brian Salomon614c1a82018-12-19 15:42:06 -0500637
Stephen Whiteb007bb22020-06-04 09:02:04 -0400638 // These tests are difficult to get working with Vulkan, Direct3D, and Dawn.
639 // See http://skbug.com/8705, http://skbug.com/8277, and http://skbug.com/10326
Brian Salomon9bc76d92019-01-24 12:18:33 -0500640 GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
Stephen Whiteb007bb22020-06-04 09:02:04 -0400641 if (api == GrBackendApi::kVulkan || api == GrBackendApi::kDirect3D ||
642 api == GrBackendApi::kDawn) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500643 continue;
644 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400645 int id = 3;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500646 enum class DrawType {
647 kNoDraw,
648 kDraw,
649 kDrawAndFlush,
650 };
651 for (auto drawType :
652 {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) {
653 for (bool unrefFirst : {false, true}) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400654 auto possiblyDrawAndFlush = [&dContext, &texture, drawType, unrefFirst, w,
Brian Salomon8cabb322019-02-22 10:44:19 -0500655 h] {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500656 if (drawType == DrawType::kNoDraw) {
657 return;
658 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500659 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500660 kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400661 auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500662 nullptr);
Robert Phillips9da87e02019-02-04 13:26:26 -0500663 auto rtc = rt->getCanvas()
Brian Salomon9bc76d92019-01-24 12:18:33 -0500664 ->internal_private_accessTopLayerRenderTargetContext();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400665 auto proxy = dContext->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400666 texture);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400667 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(
Greg Daniel40903af2020-01-30 14:55:05 -0500668 proxy->backendFormat(), GrColorType::kRGBA_8888);
669 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
670 swizzle);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400671 rtc->drawTexture(nullptr,
672 std::move(view),
673 kPremul_SkAlphaType,
674 GrSamplerState::Filter::kNearest,
675 GrSamplerState::MipmapMode::kNone,
676 SkBlendMode::kSrcOver,
677 SkPMColor4f(),
678 SkRect::MakeWH(w, h),
679 SkRect::MakeWH(w, h),
680 GrAA::kNo,
681 GrQuadAAFlags::kNone,
682 SkCanvas::kFast_SrcRectConstraint,
683 SkMatrix::I(),
684 nullptr);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500685 if (drawType == DrawType::kDrawAndFlush) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400686 dContext->flushAndSubmit();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500687 }
688 if (unrefFirst) {
689 texture.reset();
690 }
691 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400692 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500693 possiblyDrawAndFlush();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400694 dContext->abandonContext();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500695 texture.reset();
696 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
697 factory.destroyContexts();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400698 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500699 ++id;
Brian Salomon614c1a82018-12-19 15:42:06 -0500700
Brian Salomon9bc76d92019-01-24 12:18:33 -0500701 // Similar to previous, but reset the texture after the context was
702 // abandoned and then destroyed.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400703 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500704 possiblyDrawAndFlush();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400705 dContext->abandonContext();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500706 factory.destroyContexts();
707 texture.reset();
708 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400709 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500710 id++;
711
Robert Phillipseffd13f2020-07-20 15:00:36 -0400712 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500713 possiblyDrawAndFlush();
714 factory.destroyContexts();
715 texture.reset();
716 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400717 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500718 id++;
719
Robert Phillipseffd13f2020-07-20 15:00:36 -0400720 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500721 possiblyDrawAndFlush();
722 factory.releaseResourcesAndAbandonContexts();
723 texture.reset();
724 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400725 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500726 id++;
727 }
728 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500729 }
730 }
731 }
732}
Brian Salomon8cabb322019-02-22 10:44:19 -0500733
734// Tests an idle proc that unrefs another resource down to zero.
735DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400736 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500737
738 // idle proc that releases another texture.
739 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); };
740
741 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
742 for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500743 for (auto idleState :
744 {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400745 auto idleTexture = idleMaker(context, GrRenderable::kNo);
746 auto otherTexture = otherMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500747 otherTexture->ref();
748 idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState);
749 otherTexture.reset();
750 idleTexture.reset();
751 }
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 Salomone80b8092019-03-08 13:25:19 -0500767 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400768 auto idleTexture = idleMaker(dContext, GrRenderable::kNo);
769 idleTexture->addIdleProc(idleProc, dContext, idleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500770 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400771 auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 1, nullptr);
Brian Salomone80b8092019-03-08 13:25:19 -0500772 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
773 // is a wrapped-texture backed image.
774 surf->getCanvas()->clear(SK_ColorWHITE);
775 auto img1 = surf->makeImageSnapshot();
Brian Salomon72050802020-10-12 20:45:06 -0400776 auto img2 = sk_gpu_test::MakeBackendTextureImage(dContext, info, SkColors::kBlack);
777 REPORTER_ASSERT(reporter, img1 && img2);
Brian Salomone80b8092019-03-08 13:25:19 -0500778 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
779 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
780 idleTexture.reset();
Brian Salomone80b8092019-03-08 13:25:19 -0500781 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500782 }
783}
784
785DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400786 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500787 // idle proc that refs the texture
788 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
789 // release proc to check whether the texture was released or not.
790 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomone80b8092019-03-08 13:25:19 -0500791 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
792 bool isReleased = false;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400793 auto idleTexture = make_normal_texture(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500794 // This test assumes the texture won't be cached (or else the release proc doesn't get
795 // called).
796 idleTexture->resourcePriv().removeScratchKey();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400797 context->flushAndSubmit();
Brian Salomone80b8092019-03-08 13:25:19 -0500798 idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState);
799 idleTexture->setRelease(releaseProc, &isReleased);
800 auto* raw = idleTexture.get();
801 idleTexture.reset();
802 REPORTER_ASSERT(reporter, !isReleased);
803 raw->unref();
804 REPORTER_ASSERT(reporter, isReleased);
805 }
806}
807
808DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400809 auto context = contextInfo.directContext();
Brian Salomone80b8092019-03-08 13:25:19 -0500810 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400811 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500812
813 uint32_t flags = 0;
814 static constexpr uint32_t kFlushFlag = 0x1;
815 static constexpr uint32_t kFinishFlag = 0x2;
816 auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; };
817 auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; };
818 idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed);
819 idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished);
820
821 // Insert a copy from idleTexture to another texture so that we have some queued IO on
822 // idleTexture.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400823 SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
824 kPremul_SkAlphaType);
Brian Salomone80b8092019-03-08 13:25:19 -0500825 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
826 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400827 auto proxy =
828 context->priv().proxyProvider()->testingOnly_createWrapped(std::move(idleTexture));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400829 context->flushAndSubmit();
Brian Salomonc5243782020-04-02 12:50:34 -0400830 SkAssertResult(rtc->testCopy(proxy.get()));
Brian Salomone80b8092019-03-08 13:25:19 -0500831 proxy.reset();
832 REPORTER_ASSERT(reporter, flags == 0);
833
834 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
835 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
836 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400837 context->flushAndSubmit();
Brian Salomone80b8092019-03-08 13:25:19 -0500838 if (contextInfo.backend() == kVulkan_GrBackend) {
839 REPORTER_ASSERT(reporter, flags & kFlushFlag);
840 } else {
841 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
842 }
843 context->priv().getGpu()->testingOnly_flushGpuAndSync();
844 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
845 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500846}