blob: 204dd8c35227fcdf73aa50602abe14eea0f91ec4 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrResourceProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050021#include "src/gpu/GrSurfaceDrawContext.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);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500270 GrColorInfo info(combo.fColorType, kPremul_SkAlphaType, nullptr);
271 auto texCtx = GrSurfaceContext::Make(dContext, std::move(view), info);
Robert Phillipsffe27292019-08-01 10:08:07 -0400272
Robert Phillipsd3442842019-08-02 12:26:22 -0400273 readback.erase(kClearColor);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400274 if (texCtx->readPixels(
275 dContext, readback.info(), readback.writable_addr(),
276 readback.rowBytes(), {0, 0})) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400277 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400278 if (!checkColor(combo, readback.addr32()[i])) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400279 break;
280 }
281 }
282 }
283 }
284
Adlai Hollerc95b5892020-08-11 12:02:22 -0400285 dContext->priv().testingOnly_purgeAllUnlockedResources();
Robert Phillipsffe27292019-08-01 10:08:07 -0400286 }
287
288 // Try creating the texture as a deferred proxy.
289 {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400290 std::unique_ptr<GrSurfaceContext> surfCtx;
Robert Phillipsffe27292019-08-01 10:08:07 -0400291 if (renderable == GrRenderable::kYes) {
Brian Salomoneebe7352020-12-09 16:37:04 -0500292 surfCtx = GrSurfaceDrawContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400293 dContext, combo.fColorType, nullptr, fit,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400294 {desc.fWidth, desc.fHeight}, 1, GrMipmapped::kNo,
Greg Daniele20fcad2020-01-08 11:52:34 -0500295 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Robert Phillipsffe27292019-08-01 10:08:07 -0400296 } else {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500297 GrImageInfo info(combo.fColorType,
298 kUnknown_SkAlphaType,
299 nullptr,
300 {desc.fHeight, desc.fHeight});
301 surfCtx = GrSurfaceContext::Make(dContext, info, combo.fFormat, fit);
Robert Phillipsffe27292019-08-01 10:08:07 -0400302 }
303 if (!surfCtx) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400304 continue;
305 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400306
Robert Phillipsd3442842019-08-02 12:26:22 -0400307 readback.erase(kClearColor);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400308 if (surfCtx->readPixels(dContext, readback.info(), readback.writable_addr(),
Robert Phillipsd3442842019-08-02 12:26:22 -0400309 readback.rowBytes(), {0, 0})) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400310 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400311 if (!checkColor(combo, readback.addr32()[i])) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400312 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400313 }
314 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400315 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400316 dContext->priv().testingOnly_purgeAllUnlockedResources();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400317 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400318 }
319 }
320 }
321 }
322}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500323
324DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400325 auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500326 for (int y = 0; y < p->height(); ++y) {
327 for (int x = 0; x < p->width(); ++x) {
328 *p->writable_addr32(x, y) = f(x, y);
329 }
330 }
331 };
332
333 auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) {
334 SkASSERT(p1.info() == p2.info());
335 for (int y = 0; y < p1.height(); ++y) {
336 for (int x = 0; x < p1.width(); ++x) {
337 REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y));
338 if (p1.getColor(x, y) != p2.getColor(x, y)) {
339 return;
340 }
341 }
342 }
343 };
344
345 static constexpr int kSize = 100;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400346 SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
347 SkAutoPixmapStorage srcPixmap;
348 srcPixmap.alloc(ii);
349 fillPixels(&srcPixmap,
Robert Phillipscb1adb42019-06-10 15:09:34 -0400350 [](int x, int y) {
351 return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
352 });
Brian Salomonc67c31c2018-12-06 10:00:03 -0500353
Adlai Hollerc95b5892020-08-11 12:02:22 -0400354 auto dContext = context_info.directContext();
355 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500356
357 // We test both kRW in addition to kRead mostly to ensure that the calls are structured such
358 // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
359 // kRead for the right reason.
360 for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
Brian Salomon72050802020-10-12 20:45:06 -0400361 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithData(
Brian Salomonb5f880a2020-12-07 11:30:16 -0500362 dContext, srcPixmap, kTopLeft_GrSurfaceOrigin, GrRenderable::kNo, GrProtected::kNo);
Brian Salomon88d7e622020-11-05 11:11:20 -0500363 if (!mbet) {
364 ERRORF(reporter, "Could not make texture.");
365 return;
366 }
Brian Salomon72050802020-10-12 20:45:06 -0400367 auto proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership,
368 GrWrapCacheable::kNo, ioType,
369 mbet->refCountedCallback());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400370 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(),
371 GrColorType::kRGBA_8888);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500372 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
Brian Salomon14f99fc2020-12-07 12:19:47 -0500373 auto surfContext = GrSurfaceContext::Make(dContext, std::move(view), ii.colorInfo());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500374 // Read pixels should work with a read-only texture.
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400375 {
376 SkAutoPixmapStorage read;
377 read.alloc(srcPixmap.info());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400378 auto readResult = surfContext->readPixels(dContext, srcPixmap.info(),
379 read.writable_addr(), 0, { 0, 0 });
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400380 REPORTER_ASSERT(reporter, readResult);
381 if (readResult) {
382 comparePixels(srcPixmap, read, reporter);
383 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500384 }
385
386 // Write pixels should not work with a read-only texture.
387 SkAutoPixmapStorage write;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400388 write.alloc(srcPixmap.info());
389 fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); });
Adlai Hollerc95b5892020-08-11 12:02:22 -0400390 auto writeResult = surfContext->writePixels(dContext, srcPixmap.info(), write.addr(),
391 0, {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500392 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
393 // Try the low level write.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400394 dContext->flushAndSubmit();
395 auto gpuWriteResult = dContext->priv().getGpu()->writePixels(
Brian Salomonf77c1462019-08-01 15:19:29 -0400396 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888,
397 GrColorType::kRGBA_8888, write.addr32(),
Brian Salomon1047a492019-07-02 12:25:21 -0400398 kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500399 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
400
Greg Daniel6f5441a2020-01-28 17:02:49 -0500401 SkBitmap copySrcBitmap;
402 copySrcBitmap.installPixels(write);
403 copySrcBitmap.setImmutable();
404
Adlai Hollerc95b5892020-08-11 12:02:22 -0400405 GrBitmapTextureMaker maker(dContext, copySrcBitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400406 GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400407 auto copySrc = maker.view(GrMipmapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500408
Greg Danielcc104db2020-02-03 14:17:08 -0500409 REPORTER_ASSERT(reporter, copySrc.proxy());
Brian Salomonc5243782020-04-02 12:50:34 -0400410 auto copyResult = surfContext->testCopy(copySrc.proxy());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500411 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
412 // Try the low level copy.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400413 dContext->flushAndSubmit();
414 auto gpuCopyResult = dContext->priv().getGpu()->copySurface(
Greg Danielcc104db2020-02-03 14:17:08 -0500415 proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400416 {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500417 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
418
419 // Mip regen should not work with a read only texture.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400420 if (dContext->priv().caps()->mipmapSupport()) {
Brian Salomon72050802020-10-12 20:45:06 -0400421 mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(dContext,
422 kSize,
423 kSize,
424 kRGBA_8888_SkColorType,
425 GrMipmapped::kYes,
426 GrRenderable::kNo,
427 GrProtected::kNo);
428 proxy = proxyProvider->wrapBackendTexture(mbet->texture(), kBorrow_GrWrapOwnership,
429 GrWrapCacheable::kNo, ioType,
430 mbet->refCountedCallback());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400431 dContext->flushAndSubmit();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400432 proxy->peekTexture()->markMipmapsDirty(); // avoids assert in GrGpu.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500433 auto regenResult =
Adlai Hollerc95b5892020-08-11 12:02:22 -0400434 dContext->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500435 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
436 }
437 }
438}
Brian Salomon614c1a82018-12-19 15:42:06 -0500439
Greg Daniel46cfbc62019-06-07 11:43:30 -0400440static const int kSurfSize = 10;
441
Robert Phillipseffd13f2020-07-20 15:00:36 -0400442static sk_sp<GrTexture> make_wrapped_texture(GrDirectContext* dContext, GrRenderable renderable) {
Brian Salomon72050802020-10-12 20:45:06 -0400443 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
444 dContext, kSurfSize, kSurfSize, kRGBA_8888_SkColorType, GrMipmapped::kNo, renderable);
445 SkASSERT(mbet);
446 sk_sp<GrTextureProxy> proxy;
447 if (renderable == GrRenderable::kYes) {
448 proxy = dContext->priv().proxyProvider()->wrapRenderableBackendTexture(
449 mbet->texture(), 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
450 mbet->refCountedCallback());
Brian Salomon8cabb322019-02-22 10:44:19 -0500451 } else {
Brian Salomon72050802020-10-12 20:45:06 -0400452 proxy = dContext->priv().proxyProvider()->wrapBackendTexture(
453 mbet->texture(), kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType,
454 mbet->refCountedCallback());
Brian Salomon8cabb322019-02-22 10:44:19 -0500455 }
Brian Salomon72050802020-10-12 20:45:06 -0400456 if (!proxy) {
457 return nullptr;
458 }
459 return sk_ref_sp(proxy->peekTexture());
Brian Salomon8cabb322019-02-22 10:44:19 -0500460}
Brian Salomon614c1a82018-12-19 15:42:06 -0500461
Robert Phillipseffd13f2020-07-20 15:00:36 -0400462static sk_sp<GrTexture> make_normal_texture(GrDirectContext* dContext, GrRenderable renderable) {
Brian Salomona56a7462020-02-07 14:17:25 -0500463 SkISize desc;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400464 desc.fWidth = desc.fHeight = kSurfSize;
Robert Phillipseffd13f2020-07-20 15:00:36 -0400465 auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
466 renderable);
467 return dContext->priv().resourceProvider()->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400468 desc, format, renderable, 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500469}
Brian Salomon614c1a82018-12-19 15:42:06 -0500470
Brian Salomon8cabb322019-02-22 10:44:19 -0500471DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
472 // Various ways of making textures.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400473 auto makeWrapped = [](GrDirectContext* dContext) {
474 return make_wrapped_texture(dContext, GrRenderable::kNo);
Brian Salomon614c1a82018-12-19 15:42:06 -0500475 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400476 auto makeWrappedRenderable = [](GrDirectContext* dContext) {
477 return make_wrapped_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400478 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400479 auto makeNormal = [](GrDirectContext* dContext) {
480 return make_normal_texture(dContext, GrRenderable::kNo);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400481 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400482 auto makeRenderable = [](GrDirectContext* dContext) {
483 return make_normal_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400484 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500485
Robert Phillipseffd13f2020-07-20 15:00:36 -0400486 std::function<sk_sp<GrTexture>(GrDirectContext*)> makers[] = {
487 makeWrapped,
488 makeWrappedRenderable,
489 makeNormal,
490 makeRenderable
491 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500492
493 // Add a unique key, or not.
494 auto addKey = [](GrTexture* texture) {
495 static uint32_t gN = 0;
496 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
497 GrUniqueKey key;
498 GrUniqueKey::Builder builder(&key, kDomain, 1);
499 builder[0] = gN++;
500 builder.finish();
501 texture->resourcePriv().setUniqueKey(key);
502 };
503 auto dontAddKey = [](GrTexture* texture) {};
504 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
505
506 for (const auto& m : makers) {
507 for (const auto& keyAdder : keyAdders) {
508 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
509 sk_gpu_test::GrContextFactory factory;
510 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400511 auto dContext = factory.get(contextType);
512 if (!dContext) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500513 continue;
514 }
515
516 // The callback we add simply adds an integer to a set.
517 std::set<int> idleIDs;
518 struct Context {
519 std::set<int>* fIdleIDs;
520 int fNum;
521 };
522 auto proc = [](void* context) {
523 static_cast<Context*>(context)->fIdleIDs->insert(
524 static_cast<Context*>(context)->fNum);
525 delete static_cast<Context*>(context);
526 };
527
528 // Makes a texture, possibly adds a key, and sets the callback.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400529 auto make = [&m, &keyAdder, &proc, &idleIDs](GrDirectContext* dContext, int num) {
530 sk_sp<GrTexture> texture = m(dContext);
Brian Salomonf1432742020-11-09 15:40:27 -0500531 texture->addIdleProc(proc, new Context{&idleIDs, num});
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 Salomonf1432742020-11-09 15:40:27 -0500743 auto idleTexture = idleMaker(context, GrRenderable::kNo);
744 auto otherTexture = otherMaker(context, GrRenderable::kNo);
745 otherTexture->ref();
746 idleTexture->addIdleProc(idleProc, otherTexture.get());
747 otherTexture.reset();
748 idleTexture.reset();
Brian Salomon8cabb322019-02-22 10:44:19 -0500749 }
750 }
751}
752
753// Similar to above but more complicated. This flushes the context from the idle proc.
754// crbug.com/933526.
755DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400756 auto dContext = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500757
758 // idle proc that flushes the context.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400759 auto idleProc = [](void* context) {
760 reinterpret_cast<GrDirectContext*>(context)->flushAndSubmit();
761 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500762
763 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomonf1432742020-11-09 15:40:27 -0500764 auto idleTexture = idleMaker(dContext, GrRenderable::kNo);
765 idleTexture->addIdleProc(idleProc, dContext);
766 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
767 auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 1, nullptr);
768 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
769 // is a wrapped-texture backed image.
770 surf->getCanvas()->clear(SK_ColorWHITE);
771 auto img1 = surf->makeImageSnapshot();
772 auto img2 = sk_gpu_test::MakeBackendTextureImage(dContext, info, SkColors::kBlack);
773 REPORTER_ASSERT(reporter, img1 && img2);
774 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
775 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
776 idleTexture.reset();
Brian Salomon8cabb322019-02-22 10:44:19 -0500777 }
778}
779
780DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400781 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500782 // idle proc that refs the texture
783 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
784 // release proc to check whether the texture was released or not.
785 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomonf1432742020-11-09 15:40:27 -0500786 bool isReleased = false;
787 auto idleTexture = make_normal_texture(context, GrRenderable::kNo);
788 // This test assumes the texture won't be cached (or else the release proc doesn't get
789 // called).
790 idleTexture->resourcePriv().removeScratchKey();
791 context->flushAndSubmit();
792 idleTexture->addIdleProc(idleProc, idleTexture.get());
793 idleTexture->setRelease(releaseProc, &isReleased);
794 auto* raw = idleTexture.get();
795 idleTexture.reset();
796 REPORTER_ASSERT(reporter, !isReleased);
797 raw->unref();
798 REPORTER_ASSERT(reporter, isReleased);
Brian Salomone80b8092019-03-08 13:25:19 -0500799}
800
801DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400802 auto context = contextInfo.directContext();
Brian Salomone80b8092019-03-08 13:25:19 -0500803 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400804 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500805
Brian Salomonf1432742020-11-09 15:40:27 -0500806 bool called = false;
807 auto finishProc = [](void* called) { *static_cast<bool*>(called) = true; };
808 idleTexture->addIdleProc(finishProc, &called);
Brian Salomone80b8092019-03-08 13:25:19 -0500809
810 // Insert a copy from idleTexture to another texture so that we have some queued IO on
811 // idleTexture.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400812 SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
813 kPremul_SkAlphaType);
Brian Salomone80b8092019-03-08 13:25:19 -0500814 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
815 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400816 auto proxy =
817 context->priv().proxyProvider()->testingOnly_createWrapped(std::move(idleTexture));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400818 context->flushAndSubmit();
Brian Salomonc5243782020-04-02 12:50:34 -0400819 SkAssertResult(rtc->testCopy(proxy.get()));
Brian Salomone80b8092019-03-08 13:25:19 -0500820 proxy.reset();
Brian Salomonf1432742020-11-09 15:40:27 -0500821 REPORTER_ASSERT(reporter, !called);
Brian Salomone80b8092019-03-08 13:25:19 -0500822
823 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
824 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
825 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400826 context->flushAndSubmit();
Brian Salomonf1432742020-11-09 15:40:27 -0500827 if (contextInfo.backend() != kVulkan_GrBackend) {
828 REPORTER_ASSERT(reporter, called);
Brian Salomone80b8092019-03-08 13:25:19 -0500829 }
Brian Salomonf1432742020-11-09 15:40:27 -0500830 context->submit(true);
831 REPORTER_ASSERT(reporter, called);
Brian Salomone80b8092019-03-08 13:25:19 -0500832 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500833}