blob: ac488a29557755af3ec5806418e3359a1301640d [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"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000025
bsalomona2c23232014-11-25 07:41:12 -080026// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
27// and render targets to GrSurface all work as expected.
Brian Osmanfbe24062019-04-03 16:04:45 +000028DEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040029 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050030 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050031
Brian Salomona56a7462020-02-07 14:17:25 -050032 static constexpr SkISize kDesc = {256, 256};
Brian Salomon4eb38b72019-08-05 12:58:39 -040033 auto format = context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
34 GrRenderable::kYes);
Brian Salomona90382f2019-09-17 09:01:56 -040035 sk_sp<GrSurface> texRT1 =
Brian Salomon7e67dca2020-07-21 09:27:25 -040036 resourceProvider->createTexture(kDesc, format, GrRenderable::kYes, 1, GrMipmapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -040037 SkBudgeted::kNo, GrProtected::kNo);
bsalomona2c23232014-11-25 07:41:12 -080038
Robert Phillipse78b7252017-04-06 07:59:41 -040039 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget());
40 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080041 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
42 texRT1->asTexture());
43 REPORTER_ASSERT(reporter, texRT1->asRenderTarget() ==
44 static_cast<GrSurface*>(texRT1->asTexture()));
45 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
46 static_cast<GrSurface*>(texRT1->asTexture()));
bsalomona2c23232014-11-25 07:41:12 -080047
Brian Salomona90382f2019-09-17 09:01:56 -040048 sk_sp<GrTexture> tex1 =
Brian Salomon7e67dca2020-07-21 09:27:25 -040049 resourceProvider->createTexture(kDesc, format, GrRenderable::kNo, 1, GrMipmapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -040050 SkBudgeted::kNo, GrProtected::kNo);
kkinnunen15302832015-12-01 04:35:26 -080051 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040052 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
53 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000054
Robert Phillips4bdd36f2019-06-04 11:03:06 -040055 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -040056 256, 256, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -040057 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080058
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050059 sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -040060 backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +000061
bungeman6bd52842016-10-27 09:30:08 -070062 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
63 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080064 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
65 texRT2->asTexture());
66 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
67 static_cast<GrSurface*>(texRT2->asTexture()));
68 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
69 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000070
Robert Phillips5c7a25b2019-05-20 08:38:07 -040071 context->deleteBackendTexture(backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000072}
73
Greg Daniel7bfc9132019-08-14 14:23:53 -040074// This test checks that the isFormatTexturable and isFormatRenderable are
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040075// consistent with createTexture's result.
76DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040077 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050078 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
79 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
80 const GrCaps* caps = context->priv().caps();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040081
Brian Salomon4eb38b72019-08-05 12:58:39 -040082 // TODO: Should only need format here but need to determine compression type from format
83 // without config.
Robert Phillips48257d72019-12-16 14:20:53 -050084 auto createTexture = [](SkISize dimensions, GrColorType colorType,
Brian Salomon4eb38b72019-08-05 12:58:39 -040085 const GrBackendFormat& format, GrRenderable renderable,
Brian Salomonbb8dde82019-06-27 10:52:13 -040086 GrResourceProvider* rp) -> sk_sp<GrTexture> {
Greg Daniel01f278c2020-06-12 16:58:17 -040087 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
Robert Phillipsd6df7b52019-12-13 11:17:46 -050088 if (compression != SkImage::CompressionType::kNone) {
Brian Salomonbb8dde82019-06-27 10:52:13 -040089 if (renderable == GrRenderable::kYes) {
90 return nullptr;
91 }
Robert Phillips99dead92020-01-27 16:11:57 -050092 auto size = SkCompressedDataSize(compression, dimensions, nullptr, false);
Brian Salomonbb8dde82019-06-27 10:52:13 -040093 auto data = SkData::MakeUninitialized(size);
94 SkColor4f color = {0, 0, 0, 0};
Brian Salomon7e67dca2020-07-21 09:27:25 -040095 GrFillInCompressedData(compression, dimensions, GrMipmapped::kNo,
Robert Phillips48257d72019-12-16 14:20:53 -050096 (char*)data->writable_data(), color);
Robert Phillipse4720c62020-01-14 14:33:24 -050097 return rp->createCompressedTexture(dimensions, format, SkBudgeted::kNo,
Brian Salomon7e67dca2020-07-21 09:27:25 -040098 GrMipmapped::kNo, GrProtected::kNo, data.get());
Brian Salomonbb8dde82019-06-27 10:52:13 -040099 } else {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400100 return rp->createTexture(dimensions, format, renderable, 1, GrMipmapped::kNo,
Brian Salomona56a7462020-02-07 14:17:25 -0500101 SkBudgeted::kNo, GrProtected::kNo);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400102 }
103 };
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400104
Brian Salomona56a7462020-02-07 14:17:25 -0500105 static constexpr SkISize kDims = {64, 64};
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400106
Robert Phillipsffe27292019-08-01 10:08:07 -0400107 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
John Stilesbd3ffa42020-07-30 20:24:57 -0400108 caps->getTestingCombinations();
Robert Phillips0902c982019-07-16 07:47:56 -0400109
John Stilesbd3ffa42020-07-30 20:24:57 -0400110 for (const GrCaps::TestFormatColorTypeCombination& combo : combos) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400111
112 SkASSERT(combo.fColorType != GrColorType::kUnknown);
113 SkASSERT(combo.fFormat.isValid());
114
115 // Right now Vulkan has two backend formats that support ABGR_4444 (R4G4B4A4 and B4G4R4A4).
116 // Until we can create textures directly from the backend format this yields some
117 // ambiguity in what is actually supported and which textures can be created.
118 if (ctxInfo.backend() == kVulkan_GrBackend && combo.fColorType == GrColorType::kABGR_4444) {
Robert Phillips0902c982019-07-16 07:47:56 -0400119 continue;
120 }
121
Greg Daniel3a365112020-02-14 10:47:18 -0500122 // Check if 'isFormatTexturable' agrees with 'createTexture' and that the mipmap
123 // support check is working
124 {
125 bool isCompressed = caps->isFormatCompressed(combo.fFormat);
Brian Salomon469046c2020-03-30 14:21:04 -0400126 bool isTexturable = caps->isFormatTexturable(combo.fFormat);
Robert Phillipsffe27292019-08-01 10:08:07 -0400127
Greg Daniel3a365112020-02-14 10:47:18 -0500128 sk_sp<GrSurface> tex = createTexture(kDims, combo.fColorType, combo.fFormat,
129 GrRenderable::kNo, resourceProvider);
130 REPORTER_ASSERT(reporter, SkToBool(tex) == isTexturable,
131 "ct:%s format:%s, tex:%d, isTexturable:%d",
132 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
133 SkToBool(tex), isTexturable);
Robert Phillipsffe27292019-08-01 10:08:07 -0400134
Greg Daniel3a365112020-02-14 10:47:18 -0500135 // Check that the lack of mipmap support blocks the creation of mipmapped
136 // proxies
Brian Salomon69100f02020-07-21 10:49:25 -0400137 bool expectedMipMapability = isTexturable && caps->mipmapSupport() && !isCompressed;
Robert Phillipsffe27292019-08-01 10:08:07 -0400138
Greg Daniel3a365112020-02-14 10:47:18 -0500139 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400140 combo.fFormat, kDims, GrRenderable::kNo, 1, GrMipmapped::kYes,
Greg Daniel3a365112020-02-14 10:47:18 -0500141 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
142 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == expectedMipMapability,
143 "ct:%s format:%s, tex:%d, expectedMipMapability:%d",
144 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
145 SkToBool(proxy.get()), expectedMipMapability);
146 }
147
148 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' (w/o MSAA)
149 {
150 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 1);
151
152 sk_sp<GrSurface> tex = resourceProvider->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400153 kDims, combo.fFormat, GrRenderable::kYes, 1, GrMipmapped::kNo, SkBudgeted::kNo,
Greg Daniel3a365112020-02-14 10:47:18 -0500154 GrProtected::kNo);
155 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
156 "ct:%s format:%s, tex:%d, isRenderable:%d",
157 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
158 SkToBool(tex), isRenderable);
159 }
160
161 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' w/ MSAA
162 {
163 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 2);
164
165 sk_sp<GrSurface> tex = resourceProvider->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400166 kDims, combo.fFormat, GrRenderable::kYes, 2, GrMipmapped::kNo, SkBudgeted::kNo,
Greg Daniel3a365112020-02-14 10:47:18 -0500167 GrProtected::kNo);
168 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
169 "ct:%s format:%s, tex:%d, isRenderable:%d",
170 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
171 SkToBool(tex), isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400172 }
173 }
174}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400175
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500176#include "src/gpu/GrDrawingManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -0400177#include "src/gpu/GrSurfaceProxy.h"
Brian Salomond17b4a62017-05-23 16:53:47 -0400178
Robert Phillipsffe27292019-08-01 10:08:07 -0400179// For each context, set it to always clear the textures and then run through all the
180// supported formats checking that the textures are actually cleared
Brian Salomona3e29962019-07-16 11:52:08 -0400181DEF_GPUTEST(InitialTextureClear, reporter, baseOptions) {
182 GrContextOptions options = baseOptions;
183 options.fClearAllTextures = true;
Robert Phillipsd3442842019-08-02 12:26:22 -0400184
Brian Salomond17b4a62017-05-23 16:53:47 -0400185 static constexpr int kSize = 100;
Robert Phillipsd3442842019-08-02 12:26:22 -0400186 static constexpr SkColor kClearColor = 0xABABABAB;
Robert Phillipsffe27292019-08-01 10:08:07 -0400187
188 const SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
189 kPremul_SkAlphaType);
190
Robert Phillipsd3442842019-08-02 12:26:22 -0400191 SkAutoPixmapStorage readback;
192 readback.alloc(info);
193
Brian Salomona56a7462020-02-07 14:17:25 -0500194 SkISize desc;
Robert Phillipsffe27292019-08-01 10:08:07 -0400195 desc.fWidth = desc.fHeight = kSize;
196
Brian Salomona3e29962019-07-16 11:52:08 -0400197 for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
198 sk_gpu_test::GrContextFactory factory(options);
199 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
200 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400201 continue;
202 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400203 auto dContext = factory.get(contextType);
204 if (!dContext) {
Brian Salomona3e29962019-07-16 11:52:08 -0400205 continue;
206 }
Brian Salomona3e29962019-07-16 11:52:08 -0400207
Adlai Hollerc95b5892020-08-11 12:02:22 -0400208 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
209 const GrCaps* caps = dContext->priv().caps();
Brian Salomona3e29962019-07-16 11:52:08 -0400210
Robert Phillipsffe27292019-08-01 10:08:07 -0400211 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
John Stilesbd3ffa42020-07-30 20:24:57 -0400212 caps->getTestingCombinations();
Robert Phillipsffe27292019-08-01 10:08:07 -0400213
John Stilesbd3ffa42020-07-30 20:24:57 -0400214 for (const GrCaps::TestFormatColorTypeCombination& combo : combos) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400215
216 SkASSERT(combo.fColorType != GrColorType::kUnknown);
217 SkASSERT(combo.fFormat.isValid());
218
Brian Salomon469046c2020-03-30 14:21:04 -0400219 if (!caps->isFormatTexturable(combo.fFormat)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400220 continue;
221 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400222
Brian Salomon614cdab2019-09-26 11:04:28 -0400223 auto checkColor = [reporter](const GrCaps::TestFormatColorTypeCombination& combo,
224 uint32_t readColor) {
225 // We expect that if there is no alpha in the src color type and we read it to a
226 // color type with alpha that we will get one for alpha rather than zero. We used to
227 // require this but the Intel Iris 6100 on Win 10 test bot doesn't put one in the
228 // alpha channel when reading back from GL_RG16 or GL_RG16F. So now we allow either.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400229 uint32_t channels = GrColorTypeChannelFlags(combo.fColorType);
230 bool allowAlphaOne = !(channels & kAlpha_SkColorChannelFlag);
Brian Salomon614cdab2019-09-26 11:04:28 -0400231 if (allowAlphaOne) {
232 if (readColor != 0x00000000 && readColor != 0xFF000000) {
233 ERRORF(reporter,
234 "Failed on ct %s format %s 0x%08x is not 0x00000000 or 0xFF000000",
235 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
236 readColor);
237 return false;
238 }
239 } else {
240 if (readColor) {
241 ERRORF(reporter, "Failed on ct %s format %s 0x%08x != 0x00000000",
242 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
243 readColor);
244 return false;
245 }
246 }
247 return true;
248 };
Robert Phillipsffe27292019-08-01 10:08:07 -0400249
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400250 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400251 if (renderable == GrRenderable::kYes &&
Greg Daniel900583a2019-08-06 12:05:31 -0400252 !caps->isFormatAsColorTypeRenderable(combo.fColorType, combo.fFormat)) {
Brian Salomona3e29962019-07-16 11:52:08 -0400253 continue;
254 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400255
256 for (auto fit : {SkBackingFit::kApprox, SkBackingFit::kExact}) {
257
258 // Does directly allocating a texture clear it?
259 {
Chris Daltond004e0b2018-09-27 09:28:03 -0600260 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400261 {kSize, kSize}, combo.fFormat, renderable, 1, fit, SkBudgeted::kYes,
262 GrProtected::kNo);
Robert Phillipsffe27292019-08-01 10:08:07 -0400263 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500264 GrSwizzle swizzle = caps->getReadSwizzle(combo.fFormat,
265 combo.fColorType);
266 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
267 swizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400268 auto texCtx = GrSurfaceContext::Make(dContext, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500269 combo.fColorType,
270 kPremul_SkAlphaType, nullptr);
Robert Phillipsffe27292019-08-01 10:08:07 -0400271
Robert Phillipsd3442842019-08-02 12:26:22 -0400272 readback.erase(kClearColor);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400273 if (texCtx->readPixels(
274 dContext, readback.info(), readback.writable_addr(),
275 readback.rowBytes(), {0, 0})) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400276 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400277 if (!checkColor(combo, readback.addr32()[i])) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400278 break;
279 }
280 }
281 }
282 }
283
Adlai Hollerc95b5892020-08-11 12:02:22 -0400284 dContext->priv().testingOnly_purgeAllUnlockedResources();
Robert Phillipsffe27292019-08-01 10:08:07 -0400285 }
286
287 // Try creating the texture as a deferred proxy.
288 {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400289 std::unique_ptr<GrSurfaceContext> surfCtx;
Robert Phillipsffe27292019-08-01 10:08:07 -0400290 if (renderable == GrRenderable::kYes) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500291 surfCtx = GrRenderTargetContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400292 dContext, combo.fColorType, nullptr, fit,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400293 {desc.fWidth, desc.fHeight}, 1, GrMipmapped::kNo,
Greg Daniele20fcad2020-01-08 11:52:34 -0500294 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Robert Phillipsffe27292019-08-01 10:08:07 -0400295 } else {
Greg Danielbfa19c42019-12-19 16:41:40 -0500296 surfCtx = GrSurfaceContext::Make(
Adlai Hollerc95b5892020-08-11 12:02:22 -0400297 dContext, {desc.fWidth, desc.fHeight}, combo.fFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400298 GrRenderable::kNo, 1, GrMipmapped::kNo, GrProtected::kNo,
Greg Danielbfa19c42019-12-19 16:41:40 -0500299 kTopLeft_GrSurfaceOrigin, combo.fColorType,
300 kUnknown_SkAlphaType, nullptr, fit, SkBudgeted::kYes);
Robert Phillipsffe27292019-08-01 10:08:07 -0400301 }
302 if (!surfCtx) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400303 continue;
304 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400305
Robert Phillipsd3442842019-08-02 12:26:22 -0400306 readback.erase(kClearColor);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400307 if (surfCtx->readPixels(dContext, readback.info(), readback.writable_addr(),
Robert Phillipsd3442842019-08-02 12:26:22 -0400308 readback.rowBytes(), {0, 0})) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400309 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400310 if (!checkColor(combo, readback.addr32()[i])) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400311 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400312 }
313 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400314 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400315 dContext->priv().testingOnly_purgeAllUnlockedResources();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400316 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400317 }
318 }
319 }
320 }
321}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500322
323DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400324 auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500325 for (int y = 0; y < p->height(); ++y) {
326 for (int x = 0; x < p->width(); ++x) {
327 *p->writable_addr32(x, y) = f(x, y);
328 }
329 }
330 };
331
332 auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) {
333 SkASSERT(p1.info() == p2.info());
334 for (int y = 0; y < p1.height(); ++y) {
335 for (int x = 0; x < p1.width(); ++x) {
336 REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y));
337 if (p1.getColor(x, y) != p2.getColor(x, y)) {
338 return;
339 }
340 }
341 }
342 };
343
344 static constexpr int kSize = 100;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400345 SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
346 SkAutoPixmapStorage srcPixmap;
347 srcPixmap.alloc(ii);
348 fillPixels(&srcPixmap,
Robert Phillipscb1adb42019-06-10 15:09:34 -0400349 [](int x, int y) {
350 return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
351 });
Brian Salomonc67c31c2018-12-06 10:00:03 -0500352
Adlai Hollerc95b5892020-08-11 12:02:22 -0400353 auto dContext = context_info.directContext();
354 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500355
356 // We test both kRW in addition to kRead mostly to ensure that the calls are structured such
357 // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
358 // kRead for the right reason.
359 for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
Adlai Hollerc95b5892020-08-11 12:02:22 -0400360 auto backendTex = dContext->createBackendTexture(&srcPixmap, 1,
361 GrRenderable::kYes, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400362
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400363 auto proxy = proxyProvider->wrapBackendTexture(
364 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, ioType);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400365 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(),
366 GrColorType::kRGBA_8888);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500367 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400368 auto surfContext = GrSurfaceContext::Make(dContext, std::move(view), GrColorType::kRGBA_8888,
Greg Danielbfa19c42019-12-19 16:41:40 -0500369 kPremul_SkAlphaType, nullptr);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500370
371 // Read pixels should work with a read-only texture.
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400372 {
373 SkAutoPixmapStorage read;
374 read.alloc(srcPixmap.info());
Adlai Hollerc95b5892020-08-11 12:02:22 -0400375 auto readResult = surfContext->readPixels(dContext, srcPixmap.info(),
376 read.writable_addr(), 0, { 0, 0 });
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400377 REPORTER_ASSERT(reporter, readResult);
378 if (readResult) {
379 comparePixels(srcPixmap, read, reporter);
380 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500381 }
382
383 // Write pixels should not work with a read-only texture.
384 SkAutoPixmapStorage write;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400385 write.alloc(srcPixmap.info());
386 fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); });
Adlai Hollerc95b5892020-08-11 12:02:22 -0400387 auto writeResult = surfContext->writePixels(dContext, srcPixmap.info(), write.addr(),
388 0, {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500389 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
390 // Try the low level write.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400391 dContext->flushAndSubmit();
392 auto gpuWriteResult = dContext->priv().getGpu()->writePixels(
Brian Salomonf77c1462019-08-01 15:19:29 -0400393 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888,
394 GrColorType::kRGBA_8888, write.addr32(),
Brian Salomon1047a492019-07-02 12:25:21 -0400395 kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500396 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
397
Greg Daniel6f5441a2020-01-28 17:02:49 -0500398 SkBitmap copySrcBitmap;
399 copySrcBitmap.installPixels(write);
400 copySrcBitmap.setImmutable();
401
Adlai Hollerc95b5892020-08-11 12:02:22 -0400402 GrBitmapTextureMaker maker(dContext, copySrcBitmap,
Brian Salomonbc074a62020-03-18 10:06:13 -0400403 GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400404 auto copySrc = maker.view(GrMipmapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500405
Greg Danielcc104db2020-02-03 14:17:08 -0500406 REPORTER_ASSERT(reporter, copySrc.proxy());
Brian Salomonc5243782020-04-02 12:50:34 -0400407 auto copyResult = surfContext->testCopy(copySrc.proxy());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500408 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
409 // Try the low level copy.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400410 dContext->flushAndSubmit();
411 auto gpuCopyResult = dContext->priv().getGpu()->copySurface(
Greg Danielcc104db2020-02-03 14:17:08 -0500412 proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400413 {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500414 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
415
416 // Mip regen should not work with a read only texture.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400417 if (dContext->priv().caps()->mipmapSupport()) {
418 DeleteBackendTexture(dContext, backendTex);
419 backendTex = dContext->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400420 kSize, kSize, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400421 SkColors::kTransparent, GrMipmapped::kYes, GrRenderable::kYes,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400422 GrProtected::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400423 proxy = proxyProvider->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership,
424 GrWrapCacheable::kNo, ioType);
Adlai Hollerc95b5892020-08-11 12:02:22 -0400425 dContext->flushAndSubmit();
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400426 proxy->peekTexture()->markMipmapsDirty(); // avoids assert in GrGpu.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500427 auto regenResult =
Adlai Hollerc95b5892020-08-11 12:02:22 -0400428 dContext->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500429 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
430 }
Adlai Hollerc95b5892020-08-11 12:02:22 -0400431 DeleteBackendTexture(dContext, backendTex);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500432 }
433}
Brian Salomon614c1a82018-12-19 15:42:06 -0500434
Greg Daniel46cfbc62019-06-07 11:43:30 -0400435static const int kSurfSize = 10;
436
Robert Phillipseffd13f2020-07-20 15:00:36 -0400437static sk_sp<GrTexture> make_wrapped_texture(GrDirectContext* dContext, GrRenderable renderable) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400438 GrBackendTexture backendTexture;
Robert Phillipseffd13f2020-07-20 15:00:36 -0400439 CreateBackendTexture(dContext, &backendTexture, kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400440 SkColors::kTransparent, GrMipmapped::kNo, renderable, GrProtected::kNo);
Greg Daniel16032b32020-05-06 15:31:10 -0400441 SkASSERT(backendTexture.isValid());
Brian Salomon8cabb322019-02-22 10:44:19 -0500442 sk_sp<GrTexture> texture;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400443 if (GrRenderable::kYes == renderable) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400444 texture = dContext->priv().resourceProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400445 backendTexture, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500446 } else {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400447 texture = dContext->priv().resourceProvider()->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400448 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon8cabb322019-02-22 10:44:19 -0500449 }
450 // Add a release proc that deletes the GrBackendTexture.
451 struct ReleaseContext {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400452 GrDirectContext* fDContext;
Brian Salomon8cabb322019-02-22 10:44:19 -0500453 GrBackendTexture fBackendTexture;
Brian Salomon614c1a82018-12-19 15:42:06 -0500454 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500455 auto release = [](void* rc) {
456 auto releaseContext = static_cast<ReleaseContext*>(rc);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400457 auto dContext = releaseContext->fDContext;
458 dContext->deleteBackendTexture(releaseContext->fBackendTexture);
Brian Salomon8cabb322019-02-22 10:44:19 -0500459 delete releaseContext;
460 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400461 texture->setRelease(release, new ReleaseContext{dContext, backendTexture});
Brian Salomon8cabb322019-02-22 10:44:19 -0500462 return texture;
463}
Brian Salomon614c1a82018-12-19 15:42:06 -0500464
Robert Phillipseffd13f2020-07-20 15:00:36 -0400465static sk_sp<GrTexture> make_normal_texture(GrDirectContext* dContext, GrRenderable renderable) {
Brian Salomona56a7462020-02-07 14:17:25 -0500466 SkISize desc;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400467 desc.fWidth = desc.fHeight = kSurfSize;
Robert Phillipseffd13f2020-07-20 15:00:36 -0400468 auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
469 renderable);
470 return dContext->priv().resourceProvider()->createTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400471 desc, format, renderable, 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500472}
Brian Salomon614c1a82018-12-19 15:42:06 -0500473
Brian Salomon8cabb322019-02-22 10:44:19 -0500474DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
475 // Various ways of making textures.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400476 auto makeWrapped = [](GrDirectContext* dContext) {
477 return make_wrapped_texture(dContext, GrRenderable::kNo);
Brian Salomon614c1a82018-12-19 15:42:06 -0500478 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400479 auto makeWrappedRenderable = [](GrDirectContext* dContext) {
480 return make_wrapped_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400481 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400482 auto makeNormal = [](GrDirectContext* dContext) {
483 return make_normal_texture(dContext, GrRenderable::kNo);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400484 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400485 auto makeRenderable = [](GrDirectContext* dContext) {
486 return make_normal_texture(dContext, GrRenderable::kYes);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400487 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500488
Robert Phillipseffd13f2020-07-20 15:00:36 -0400489 std::function<sk_sp<GrTexture>(GrDirectContext*)> makers[] = {
490 makeWrapped,
491 makeWrappedRenderable,
492 makeNormal,
493 makeRenderable
494 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500495
496 // Add a unique key, or not.
497 auto addKey = [](GrTexture* texture) {
498 static uint32_t gN = 0;
499 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
500 GrUniqueKey key;
501 GrUniqueKey::Builder builder(&key, kDomain, 1);
502 builder[0] = gN++;
503 builder.finish();
504 texture->resourcePriv().setUniqueKey(key);
505 };
506 auto dontAddKey = [](GrTexture* texture) {};
507 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
508
509 for (const auto& m : makers) {
510 for (const auto& keyAdder : keyAdders) {
511 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
512 sk_gpu_test::GrContextFactory factory;
513 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400514 auto dContext = factory.get(contextType);
515 if (!dContext) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500516 continue;
517 }
518
519 // The callback we add simply adds an integer to a set.
520 std::set<int> idleIDs;
521 struct Context {
522 std::set<int>* fIdleIDs;
523 int fNum;
524 };
525 auto proc = [](void* context) {
526 static_cast<Context*>(context)->fIdleIDs->insert(
527 static_cast<Context*>(context)->fNum);
528 delete static_cast<Context*>(context);
529 };
530
531 // Makes a texture, possibly adds a key, and sets the callback.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400532 auto make = [&m, &keyAdder, &proc, &idleIDs](GrDirectContext* dContext, int num) {
533 sk_sp<GrTexture> texture = m(dContext);
Brian Salomone80b8092019-03-08 13:25:19 -0500534 texture->addIdleProc(proc, new Context{&idleIDs, num},
535 GrTexture::IdleState::kFinished);
Brian Salomon614c1a82018-12-19 15:42:06 -0500536 keyAdder(texture.get());
537 return texture;
538 };
539
Robert Phillipseffd13f2020-07-20 15:00:36 -0400540 auto texture = make(dContext, 1);
Brian Salomon614c1a82018-12-19 15:42:06 -0500541 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400542 auto renderable = GrRenderable(SkToBool(texture->asRenderTarget()));
Brian Salomon614c1a82018-12-19 15:42:06 -0500543 auto backendFormat = texture->backendFormat();
544 texture.reset();
545 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
546
Robert Phillipseffd13f2020-07-20 15:00:36 -0400547 texture = make(dContext, 2);
Brian Salomon8cabb322019-02-22 10:44:19 -0500548 int w = texture->width();
549 int h = texture->height();
Brian Salomon614c1a82018-12-19 15:42:06 -0500550 SkImageInfo info =
Brian Salomon8cabb322019-02-22 10:44:19 -0500551 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400552 auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0, nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500553 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomon63410e92020-03-23 18:32:50 -0400554 auto singleUseLazyCB = [&texture](GrResourceProvider*,
555 const GrSurfaceProxy::LazySurfaceDesc&) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400556 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
557 if (texture->getUniqueKey().isValid()) {
558 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
559 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400560 return GrSurfaceProxy::LazyCallbackResult{std::move(texture), true, mode};
Brian Salomon614c1a82018-12-19 15:42:06 -0500561 };
Brian Salomona56a7462020-02-07 14:17:25 -0500562 SkISize desc;
Brian Salomon8cabb322019-02-22 10:44:19 -0500563 desc.fWidth = w;
564 desc.fHeight = h;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500565 SkBudgeted budgeted;
566 if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
567 budgeted = SkBudgeted::kYes;
568 } else {
569 budgeted = SkBudgeted::kNo;
570 }
Greg Danielc113f002020-08-26 13:28:22 -0400571 sk_sp<GrSurfaceProxy> proxy;
572 if (renderable == GrRenderable::kYes) {
573 static const GrProxyProvider::TextureInfo kTexInfo = {GrMipMapped::kNo,
574 GrTextureType::k2D};
575 proxy = dContext->priv().proxyProvider()->createLazyRenderTargetProxy(
576 singleUseLazyCB, backendFormat, desc, 1,
Greg Daniel638b2e82020-08-27 14:29:00 -0400577 dContext->priv().caps()->getExtraSurfaceFlagsForDeferredRT(),
578 &kTexInfo,
Greg Danielc113f002020-08-26 13:28:22 -0400579 GrMipmapStatus::kNotAllocated,
580 SkBackingFit::kExact, budgeted, GrProtected::kNo, false,
581 GrSurfaceProxy::UseAllocator::kYes);
582 } else {
583 proxy = dContext->priv().proxyProvider()->createLazyProxy(
584 singleUseLazyCB, backendFormat, desc, GrMipmapped::kNo,
585 GrMipmapStatus::kNotAllocated, GrInternalSurfaceFlags ::kNone,
586 SkBackingFit::kExact, budgeted, GrProtected::kNo,
587 GrSurfaceProxy::UseAllocator::kYes);
588 }
Robert Phillipseffd13f2020-07-20 15:00:36 -0400589 GrSwizzle readSwizzle = dContext->priv().caps()->getReadSwizzle(
Greg Danielce3ddaa2020-01-22 16:58:15 -0500590 backendFormat, GrColorType::kRGBA_8888);
Greg Daniel40903af2020-01-30 14:55:05 -0500591 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, readSwizzle);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400592 rtc->drawTexture(nullptr,
593 view,
594 kPremul_SkAlphaType,
595 GrSamplerState::Filter::kNearest,
596 GrSamplerState::MipmapMode::kNone,
597 SkBlendMode::kSrcOver,
598 SkPMColor4f(),
599 SkRect::MakeWH(w, h),
600 SkRect::MakeWH(w, h),
601 GrAA::kNo,
602 GrQuadAAFlags::kNone,
603 SkCanvas::kFast_SrcRectConstraint,
604 SkMatrix::I(),
605 nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500606 // We still have the proxy, which should remain instantiated, thereby keeping the
607 // texture not purgeable.
608 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400609 dContext->flushAndSubmit();
Brian Salomon614c1a82018-12-19 15:42:06 -0500610 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400611 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500612 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
613
614 // This time we move the proxy into the draw.
Brian Salomone69b9ef2020-07-22 11:18:06 -0400615 rtc->drawTexture(nullptr,
616 std::move(view),
617 kPremul_SkAlphaType,
618 GrSamplerState::Filter::kNearest,
619 GrSamplerState::MipmapMode::kNone,
620 SkBlendMode::kSrcOver,
621 SkPMColor4f(),
622 SkRect::MakeWH(w, h),
623 SkRect::MakeWH(w, h),
624 GrAA::kNo,
625 GrQuadAAFlags::kNone,
626 SkCanvas::kFast_SrcRectConstraint,
627 SkMatrix::I(),
628 nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500629 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400630 dContext->flushAndSubmit();
631 dContext->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500632 // Now that the draw is fully consumed by the GPU, the texture should be idle.
633 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
634
Brian Salomon9bc76d92019-01-24 12:18:33 -0500635 // Make sure we make the call during various shutdown scenarios where the texture
636 // might persist after context is destroyed, abandoned, etc. We test three
637 // variations of each scenario. One where the texture is just created. Another,
638 // where the texture has been used in a draw and then the context is flushed. And
639 // one where the the texture was drawn but the context is not flushed.
640 // In each scenario we test holding a ref beyond the context shutdown and not.
Brian Salomon614c1a82018-12-19 15:42:06 -0500641
Stephen Whiteb007bb22020-06-04 09:02:04 -0400642 // These tests are difficult to get working with Vulkan, Direct3D, and Dawn.
643 // See http://skbug.com/8705, http://skbug.com/8277, and http://skbug.com/10326
Brian Salomon9bc76d92019-01-24 12:18:33 -0500644 GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
Stephen Whiteb007bb22020-06-04 09:02:04 -0400645 if (api == GrBackendApi::kVulkan || api == GrBackendApi::kDirect3D ||
646 api == GrBackendApi::kDawn) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500647 continue;
648 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400649 int id = 3;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500650 enum class DrawType {
651 kNoDraw,
652 kDraw,
653 kDrawAndFlush,
654 };
655 for (auto drawType :
656 {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) {
657 for (bool unrefFirst : {false, true}) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400658 auto possiblyDrawAndFlush = [&dContext, &texture, drawType, unrefFirst, w,
Brian Salomon8cabb322019-02-22 10:44:19 -0500659 h] {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500660 if (drawType == DrawType::kNoDraw) {
661 return;
662 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500663 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500664 kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400665 auto rt = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500666 nullptr);
Robert Phillips9da87e02019-02-04 13:26:26 -0500667 auto rtc = rt->getCanvas()
Brian Salomon9bc76d92019-01-24 12:18:33 -0500668 ->internal_private_accessTopLayerRenderTargetContext();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400669 auto proxy = dContext->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400670 texture);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400671 GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(
Greg Daniel40903af2020-01-30 14:55:05 -0500672 proxy->backendFormat(), GrColorType::kRGBA_8888);
673 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
674 swizzle);
Brian Salomone69b9ef2020-07-22 11:18:06 -0400675 rtc->drawTexture(nullptr,
676 std::move(view),
677 kPremul_SkAlphaType,
678 GrSamplerState::Filter::kNearest,
679 GrSamplerState::MipmapMode::kNone,
680 SkBlendMode::kSrcOver,
681 SkPMColor4f(),
682 SkRect::MakeWH(w, h),
683 SkRect::MakeWH(w, h),
684 GrAA::kNo,
685 GrQuadAAFlags::kNone,
686 SkCanvas::kFast_SrcRectConstraint,
687 SkMatrix::I(),
688 nullptr);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500689 if (drawType == DrawType::kDrawAndFlush) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400690 dContext->flushAndSubmit();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500691 }
692 if (unrefFirst) {
693 texture.reset();
694 }
695 };
Robert Phillipseffd13f2020-07-20 15:00:36 -0400696 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500697 possiblyDrawAndFlush();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400698 dContext->abandonContext();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500699 texture.reset();
700 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
701 factory.destroyContexts();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400702 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500703 ++id;
Brian Salomon614c1a82018-12-19 15:42:06 -0500704
Brian Salomon9bc76d92019-01-24 12:18:33 -0500705 // Similar to previous, but reset the texture after the context was
706 // abandoned and then destroyed.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400707 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500708 possiblyDrawAndFlush();
Robert Phillipseffd13f2020-07-20 15:00:36 -0400709 dContext->abandonContext();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500710 factory.destroyContexts();
711 texture.reset();
712 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400713 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500714 id++;
715
Robert Phillipseffd13f2020-07-20 15:00:36 -0400716 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500717 possiblyDrawAndFlush();
718 factory.destroyContexts();
719 texture.reset();
720 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400721 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500722 id++;
723
Robert Phillipseffd13f2020-07-20 15:00:36 -0400724 texture = make(dContext, id);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500725 possiblyDrawAndFlush();
726 factory.releaseResourcesAndAbandonContexts();
727 texture.reset();
728 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
Robert Phillipseffd13f2020-07-20 15:00:36 -0400729 dContext = factory.get(contextType);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500730 id++;
731 }
732 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500733 }
734 }
735 }
736}
Brian Salomon8cabb322019-02-22 10:44:19 -0500737
738// Tests an idle proc that unrefs another resource down to zero.
739DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400740 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500741
742 // idle proc that releases another texture.
743 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); };
744
745 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
746 for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500747 for (auto idleState :
748 {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400749 auto idleTexture = idleMaker(context, GrRenderable::kNo);
750 auto otherTexture = otherMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500751 otherTexture->ref();
752 idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState);
753 otherTexture.reset();
754 idleTexture.reset();
755 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500756 }
757 }
758}
759
760// Similar to above but more complicated. This flushes the context from the idle proc.
761// crbug.com/933526.
762DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400763 auto dContext = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500764
765 // idle proc that flushes the context.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400766 auto idleProc = [](void* context) {
767 reinterpret_cast<GrDirectContext*>(context)->flushAndSubmit();
768 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500769
770 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500771 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400772 auto idleTexture = idleMaker(dContext, GrRenderable::kNo);
773 idleTexture->addIdleProc(idleProc, dContext, idleState);
Brian Salomone80b8092019-03-08 13:25:19 -0500774 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400775 auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 1, nullptr);
Brian Salomone80b8092019-03-08 13:25:19 -0500776 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
777 // is a wrapped-texture backed image.
778 surf->getCanvas()->clear(SK_ColorWHITE);
779 auto img1 = surf->makeImageSnapshot();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400780
781 GrBackendTexture backendTexture;
782
Robert Phillipseffd13f2020-07-20 15:00:36 -0400783 if (!CreateBackendTexture(dContext, &backendTexture, info, SkColors::kBlack,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400784 GrMipmapped::kNo, GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400785 REPORTER_ASSERT(reporter, false);
786 continue;
787 }
788
Robert Phillipseffd13f2020-07-20 15:00:36 -0400789 auto img2 = SkImage::MakeFromTexture(dContext, backendTexture, kTopLeft_GrSurfaceOrigin,
Brian Salomone80b8092019-03-08 13:25:19 -0500790 info.colorType(), info.alphaType(), nullptr);
791 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
792 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
793 idleTexture.reset();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400794
Robert Phillipseffd13f2020-07-20 15:00:36 -0400795 DeleteBackendTexture(dContext, backendTexture);
Brian Salomone80b8092019-03-08 13:25:19 -0500796 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500797 }
798}
799
800DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400801 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500802 // idle proc that refs the texture
803 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
804 // release proc to check whether the texture was released or not.
805 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomone80b8092019-03-08 13:25:19 -0500806 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
807 bool isReleased = false;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400808 auto idleTexture = make_normal_texture(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500809 // This test assumes the texture won't be cached (or else the release proc doesn't get
810 // called).
811 idleTexture->resourcePriv().removeScratchKey();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400812 context->flushAndSubmit();
Brian Salomone80b8092019-03-08 13:25:19 -0500813 idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState);
814 idleTexture->setRelease(releaseProc, &isReleased);
815 auto* raw = idleTexture.get();
816 idleTexture.reset();
817 REPORTER_ASSERT(reporter, !isReleased);
818 raw->unref();
819 REPORTER_ASSERT(reporter, isReleased);
820 }
821}
822
823DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400824 auto context = contextInfo.directContext();
Brian Salomone80b8092019-03-08 13:25:19 -0500825 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400826 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500827
828 uint32_t flags = 0;
829 static constexpr uint32_t kFlushFlag = 0x1;
830 static constexpr uint32_t kFinishFlag = 0x2;
831 auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; };
832 auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; };
833 idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed);
834 idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished);
835
836 // Insert a copy from idleTexture to another texture so that we have some queued IO on
837 // idleTexture.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400838 SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
839 kPremul_SkAlphaType);
Brian Salomone80b8092019-03-08 13:25:19 -0500840 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
841 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400842 auto proxy =
843 context->priv().proxyProvider()->testingOnly_createWrapped(std::move(idleTexture));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400844 context->flushAndSubmit();
Brian Salomonc5243782020-04-02 12:50:34 -0400845 SkAssertResult(rtc->testCopy(proxy.get()));
Brian Salomone80b8092019-03-08 13:25:19 -0500846 proxy.reset();
847 REPORTER_ASSERT(reporter, flags == 0);
848
849 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
850 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
851 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400852 context->flushAndSubmit();
Brian Salomone80b8092019-03-08 13:25:19 -0500853 if (contextInfo.backend() == kVulkan_GrBackend) {
854 REPORTER_ASSERT(reporter, flags & kFlushFlag);
855 } else {
856 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
857 }
858 context->priv().getGpu()->testingOnly_flushGpuAndSync();
859 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
860 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500861}