blob: 4ed7515045ca538b1e65a17247b8d1090a9941e9 [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 "src/gpu/GrTexturePriv.h"
24#include "tests/Test.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040025#include "tests/TestUtils.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000026
bsalomona2c23232014-11-25 07:41:12 -080027// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
28// and render targets to GrSurface all work as expected.
Brian Osmanfbe24062019-04-03 16:04:45 +000029DEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040030 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050031 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050032
Brian Salomona56a7462020-02-07 14:17:25 -050033 static constexpr SkISize kDesc = {256, 256};
Brian Salomon4eb38b72019-08-05 12:58:39 -040034 auto format = context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
35 GrRenderable::kYes);
Brian Salomona90382f2019-09-17 09:01:56 -040036 sk_sp<GrSurface> texRT1 =
Brian Salomona56a7462020-02-07 14:17:25 -050037 resourceProvider->createTexture(kDesc, format, GrRenderable::kYes, 1, GrMipMapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -040038 SkBudgeted::kNo, GrProtected::kNo);
bsalomona2c23232014-11-25 07:41:12 -080039
Robert Phillipse78b7252017-04-06 07:59:41 -040040 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asRenderTarget());
41 REPORTER_ASSERT(reporter, texRT1.get() == texRT1->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080042 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
43 texRT1->asTexture());
44 REPORTER_ASSERT(reporter, texRT1->asRenderTarget() ==
45 static_cast<GrSurface*>(texRT1->asTexture()));
46 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT1->asRenderTarget()) ==
47 static_cast<GrSurface*>(texRT1->asTexture()));
bsalomona2c23232014-11-25 07:41:12 -080048
Brian Salomona90382f2019-09-17 09:01:56 -040049 sk_sp<GrTexture> tex1 =
Brian Salomona56a7462020-02-07 14:17:25 -050050 resourceProvider->createTexture(kDesc, format, GrRenderable::kNo, 1, GrMipMapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -040051 SkBudgeted::kNo, GrProtected::kNo);
kkinnunen15302832015-12-01 04:35:26 -080052 REPORTER_ASSERT(reporter, nullptr == tex1->asRenderTarget());
Robert Phillipse78b7252017-04-06 07:59:41 -040053 REPORTER_ASSERT(reporter, tex1.get() == tex1->asTexture());
54 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(tex1.get()) == tex1->asTexture());
bsalomon@google.com686bcb82013-04-09 15:04:12 +000055
Robert Phillips4bdd36f2019-06-04 11:03:06 -040056 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -040057 256, 256, kRGBA_8888_SkColorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -040058 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
bsalomon091f60c2015-11-10 11:54:56 -080059
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050060 sk_sp<GrSurface> texRT2 = resourceProvider->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -040061 backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
Greg Daniel7ef28f32017-04-20 16:41:55 +000062
bungeman6bd52842016-10-27 09:30:08 -070063 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asRenderTarget());
64 REPORTER_ASSERT(reporter, texRT2.get() == texRT2->asTexture());
kkinnunen15302832015-12-01 04:35:26 -080065 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
66 texRT2->asTexture());
67 REPORTER_ASSERT(reporter, texRT2->asRenderTarget() ==
68 static_cast<GrSurface*>(texRT2->asTexture()));
69 REPORTER_ASSERT(reporter, static_cast<GrSurface*>(texRT2->asRenderTarget()) ==
70 static_cast<GrSurface*>(texRT2->asTexture()));
bsalomon@google.com686bcb82013-04-09 15:04:12 +000071
Robert Phillips5c7a25b2019-05-20 08:38:07 -040072 context->deleteBackendTexture(backendTex);
bsalomon@google.com686bcb82013-04-09 15:04:12 +000073}
74
Greg Daniel7bfc9132019-08-14 14:23:53 -040075// This test checks that the isFormatTexturable and isFormatRenderable are
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040076// consistent with createTexture's result.
77DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSurfaceRenderability, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040078 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050079 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
80 GrResourceProvider* resourceProvider = context->priv().resourceProvider();
81 const GrCaps* caps = context->priv().caps();
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -040082
Brian Salomon4eb38b72019-08-05 12:58:39 -040083 // TODO: Should only need format here but need to determine compression type from format
84 // without config.
Robert Phillips48257d72019-12-16 14:20:53 -050085 auto createTexture = [](SkISize dimensions, GrColorType colorType,
Brian Salomon4eb38b72019-08-05 12:58:39 -040086 const GrBackendFormat& format, GrRenderable renderable,
Brian Salomonbb8dde82019-06-27 10:52:13 -040087 GrResourceProvider* rp) -> sk_sp<GrTexture> {
Greg Daniel01f278c2020-06-12 16:58:17 -040088 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
Robert Phillipsd6df7b52019-12-13 11:17:46 -050089 if (compression != SkImage::CompressionType::kNone) {
Brian Salomonbb8dde82019-06-27 10:52:13 -040090 if (renderable == GrRenderable::kYes) {
91 return nullptr;
92 }
Robert Phillips99dead92020-01-27 16:11:57 -050093 auto size = SkCompressedDataSize(compression, dimensions, nullptr, false);
Brian Salomonbb8dde82019-06-27 10:52:13 -040094 auto data = SkData::MakeUninitialized(size);
95 SkColor4f color = {0, 0, 0, 0};
Robert Phillips48257d72019-12-16 14:20:53 -050096 GrFillInCompressedData(compression, dimensions, GrMipMapped::kNo,
97 (char*)data->writable_data(), color);
Robert Phillipse4720c62020-01-14 14:33:24 -050098 return rp->createCompressedTexture(dimensions, format, SkBudgeted::kNo,
Robert Phillips3a833922020-01-21 15:25:58 -050099 GrMipMapped::kNo, GrProtected::kNo, data.get());
Brian Salomonbb8dde82019-06-27 10:52:13 -0400100 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500101 return rp->createTexture(dimensions, format, renderable, 1, GrMipMapped::kNo,
102 SkBudgeted::kNo, GrProtected::kNo);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400103 }
104 };
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400105
Brian Salomona56a7462020-02-07 14:17:25 -0500106 static constexpr SkISize kDims = {64, 64};
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400107
Robert Phillipsffe27292019-08-01 10:08:07 -0400108 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
109 caps->getTestingCombinations();
Robert Phillips0902c982019-07-16 07:47:56 -0400110
Robert Phillipsffe27292019-08-01 10:08:07 -0400111 for (auto combo : combos) {
112
113 SkASSERT(combo.fColorType != GrColorType::kUnknown);
114 SkASSERT(combo.fFormat.isValid());
115
116 // Right now Vulkan has two backend formats that support ABGR_4444 (R4G4B4A4 and B4G4R4A4).
117 // Until we can create textures directly from the backend format this yields some
118 // ambiguity in what is actually supported and which textures can be created.
119 if (ctxInfo.backend() == kVulkan_GrBackend && combo.fColorType == GrColorType::kABGR_4444) {
Robert Phillips0902c982019-07-16 07:47:56 -0400120 continue;
121 }
122
Greg Daniel3a365112020-02-14 10:47:18 -0500123 // Check if 'isFormatTexturable' agrees with 'createTexture' and that the mipmap
124 // support check is working
125 {
126 bool isCompressed = caps->isFormatCompressed(combo.fFormat);
Brian Salomon469046c2020-03-30 14:21:04 -0400127 bool isTexturable = caps->isFormatTexturable(combo.fFormat);
Robert Phillipsffe27292019-08-01 10:08:07 -0400128
Greg Daniel3a365112020-02-14 10:47:18 -0500129 sk_sp<GrSurface> tex = createTexture(kDims, combo.fColorType, combo.fFormat,
130 GrRenderable::kNo, resourceProvider);
131 REPORTER_ASSERT(reporter, SkToBool(tex) == isTexturable,
132 "ct:%s format:%s, tex:%d, isTexturable:%d",
133 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
134 SkToBool(tex), isTexturable);
Robert Phillipsffe27292019-08-01 10:08:07 -0400135
Greg Daniel3a365112020-02-14 10:47:18 -0500136 // Check that the lack of mipmap support blocks the creation of mipmapped
137 // proxies
138 bool expectedMipMapability = isTexturable && caps->mipMapSupport() && !isCompressed;
Robert Phillipsffe27292019-08-01 10:08:07 -0400139
Greg Daniel3a365112020-02-14 10:47:18 -0500140 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400141 combo.fFormat, kDims, GrRenderable::kNo, 1, GrMipMapped::kYes,
Greg Daniel3a365112020-02-14 10:47:18 -0500142 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
143 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == expectedMipMapability,
144 "ct:%s format:%s, tex:%d, expectedMipMapability:%d",
145 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
146 SkToBool(proxy.get()), expectedMipMapability);
147 }
148
149 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' (w/o MSAA)
150 {
151 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 1);
152
153 sk_sp<GrSurface> tex = resourceProvider->createTexture(
154 kDims, combo.fFormat, GrRenderable::kYes, 1, GrMipMapped::kNo, SkBudgeted::kNo,
155 GrProtected::kNo);
156 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
157 "ct:%s format:%s, tex:%d, isRenderable:%d",
158 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
159 SkToBool(tex), isRenderable);
160 }
161
162 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' w/ MSAA
163 {
164 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 2);
165
166 sk_sp<GrSurface> tex = resourceProvider->createTexture(
167 kDims, combo.fFormat, GrRenderable::kYes, 2, GrMipMapped::kNo, SkBudgeted::kNo,
168 GrProtected::kNo);
169 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
170 "ct:%s format:%s, tex:%d, isRenderable:%d",
171 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
172 SkToBool(tex), isRenderable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400173 }
174 }
175}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400176
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500177#include "src/gpu/GrDrawingManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -0400178#include "src/gpu/GrSurfaceProxy.h"
Brian Salomond17b4a62017-05-23 16:53:47 -0400179
Robert Phillipsffe27292019-08-01 10:08:07 -0400180// For each context, set it to always clear the textures and then run through all the
181// supported formats checking that the textures are actually cleared
Brian Salomona3e29962019-07-16 11:52:08 -0400182DEF_GPUTEST(InitialTextureClear, reporter, baseOptions) {
183 GrContextOptions options = baseOptions;
184 options.fClearAllTextures = true;
Robert Phillipsd3442842019-08-02 12:26:22 -0400185
Brian Salomond17b4a62017-05-23 16:53:47 -0400186 static constexpr int kSize = 100;
Robert Phillipsd3442842019-08-02 12:26:22 -0400187 static constexpr SkColor kClearColor = 0xABABABAB;
Robert Phillipsffe27292019-08-01 10:08:07 -0400188
189 const SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
190 kPremul_SkAlphaType);
191
Robert Phillipsd3442842019-08-02 12:26:22 -0400192 SkAutoPixmapStorage readback;
193 readback.alloc(info);
194
Brian Salomona56a7462020-02-07 14:17:25 -0500195 SkISize desc;
Robert Phillipsffe27292019-08-01 10:08:07 -0400196 desc.fWidth = desc.fHeight = kSize;
197
Brian Salomona3e29962019-07-16 11:52:08 -0400198 for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
199 sk_gpu_test::GrContextFactory factory(options);
200 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
201 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400202 continue;
203 }
Brian Salomona3e29962019-07-16 11:52:08 -0400204 auto context = factory.get(contextType);
205 if (!context) {
206 continue;
207 }
Brian Salomona3e29962019-07-16 11:52:08 -0400208
Brian Salomona3e29962019-07-16 11:52:08 -0400209 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillipsffe27292019-08-01 10:08:07 -0400210 const GrCaps* caps = context->priv().caps();
Brian Salomona3e29962019-07-16 11:52:08 -0400211
Robert Phillipsffe27292019-08-01 10:08:07 -0400212 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
213 caps->getTestingCombinations();
214
215 for (auto combo : combos) {
216
217 SkASSERT(combo.fColorType != GrColorType::kUnknown);
218 SkASSERT(combo.fFormat.isValid());
219
Brian Salomon469046c2020-03-30 14:21:04 -0400220 if (!caps->isFormatTexturable(combo.fFormat)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400221 continue;
222 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400223
Brian Salomon614cdab2019-09-26 11:04:28 -0400224 auto checkColor = [reporter](const GrCaps::TestFormatColorTypeCombination& combo,
225 uint32_t readColor) {
226 // We expect that if there is no alpha in the src color type and we read it to a
227 // color type with alpha that we will get one for alpha rather than zero. We used to
228 // require this but the Intel Iris 6100 on Win 10 test bot doesn't put one in the
229 // alpha channel when reading back from GL_RG16 or GL_RG16F. So now we allow either.
Brian Salomon2f23ae62020-03-26 16:17:56 -0400230 uint32_t channels = GrColorTypeChannelFlags(combo.fColorType);
231 bool allowAlphaOne = !(channels & kAlpha_SkColorChannelFlag);
Brian Salomon614cdab2019-09-26 11:04:28 -0400232 if (allowAlphaOne) {
233 if (readColor != 0x00000000 && readColor != 0xFF000000) {
234 ERRORF(reporter,
235 "Failed on ct %s format %s 0x%08x is not 0x00000000 or 0xFF000000",
236 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
237 readColor);
238 return false;
239 }
240 } else {
241 if (readColor) {
242 ERRORF(reporter, "Failed on ct %s format %s 0x%08x != 0x00000000",
243 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
244 readColor);
245 return false;
246 }
247 }
248 return true;
249 };
Robert Phillipsffe27292019-08-01 10:08:07 -0400250
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400251 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400252 if (renderable == GrRenderable::kYes &&
Greg Daniel900583a2019-08-06 12:05:31 -0400253 !caps->isFormatAsColorTypeRenderable(combo.fColorType, combo.fFormat)) {
Brian Salomona3e29962019-07-16 11:52:08 -0400254 continue;
255 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400256
257 for (auto fit : {SkBackingFit::kApprox, SkBackingFit::kExact}) {
258
259 // Does directly allocating a texture clear it?
260 {
Chris Daltond004e0b2018-09-27 09:28:03 -0600261 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400262 {kSize, kSize}, combo.fFormat, renderable, 1, fit, SkBudgeted::kYes,
263 GrProtected::kNo);
Robert Phillipsffe27292019-08-01 10:08:07 -0400264 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500265 GrSwizzle swizzle = caps->getReadSwizzle(combo.fFormat,
266 combo.fColorType);
267 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
268 swizzle);
269 auto texCtx = GrSurfaceContext::Make(context, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500270 combo.fColorType,
271 kPremul_SkAlphaType, nullptr);
Robert Phillipsffe27292019-08-01 10:08:07 -0400272
Robert Phillipsd3442842019-08-02 12:26:22 -0400273 readback.erase(kClearColor);
274 if (texCtx->readPixels(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
284 context->priv().testingOnly_purgeAllUnlockedResources();
285 }
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(
292 context, combo.fColorType, nullptr, fit,
293 {desc.fWidth, desc.fHeight}, 1, GrMipMapped::kNo,
294 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Robert Phillipsffe27292019-08-01 10:08:07 -0400295 } else {
Greg Danielbfa19c42019-12-19 16:41:40 -0500296 surfCtx = GrSurfaceContext::Make(
297 context, {desc.fWidth, desc.fHeight}, combo.fFormat,
298 GrRenderable::kNo, 1, GrMipMapped::kNo, GrProtected::kNo,
299 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);
307 if (surfCtx->readPixels(readback.info(), readback.writable_addr(),
308 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 }
Brian Salomona3e29962019-07-16 11:52:08 -0400315 context->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
Robert Phillips6d344c32020-07-06 10:56:46 -0400353 auto context = context_info.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500354 GrProxyProvider* proxyProvider = context->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}) {
Robert Phillips66944402019-09-30 13:21:25 -0400360 auto backendTex = context->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);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500365 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
366 GrColorType::kRGBA_8888);
367 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
368 auto surfContext = GrSurfaceContext::Make(context, 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());
375 auto readResult = surfContext->readPixels(srcPixmap.info(), read.writable_addr(),
376 0, { 0, 0 });
377 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(); });
387 auto writeResult = surfContext->writePixels(srcPixmap.info(), write.addr(), 0, {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500388 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
389 // Try the low level write.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400390 context->flushAndSubmit();
Robert Phillips9da87e02019-02-04 13:26:26 -0500391 auto gpuWriteResult = context->priv().getGpu()->writePixels(
Brian Salomonf77c1462019-08-01 15:19:29 -0400392 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888,
393 GrColorType::kRGBA_8888, write.addr32(),
Brian Salomon1047a492019-07-02 12:25:21 -0400394 kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500395 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
396
Greg Daniel6f5441a2020-01-28 17:02:49 -0500397 SkBitmap copySrcBitmap;
398 copySrcBitmap.installPixels(write);
399 copySrcBitmap.setImmutable();
400
Brian Salomonbc074a62020-03-18 10:06:13 -0400401 GrBitmapTextureMaker maker(context, copySrcBitmap,
402 GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500403 auto copySrc = maker.view(GrMipMapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500404
Greg Danielcc104db2020-02-03 14:17:08 -0500405 REPORTER_ASSERT(reporter, copySrc.proxy());
Brian Salomonc5243782020-04-02 12:50:34 -0400406 auto copyResult = surfContext->testCopy(copySrc.proxy());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500407 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
408 // Try the low level copy.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400409 context->flushAndSubmit();
Robert Phillips9da87e02019-02-04 13:26:26 -0500410 auto gpuCopyResult = context->priv().getGpu()->copySurface(
Greg Danielcc104db2020-02-03 14:17:08 -0500411 proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400412 {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500413 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
414
415 // Mip regen should not work with a read only texture.
Robert Phillips9da87e02019-02-04 13:26:26 -0500416 if (context->priv().caps()->mipMapSupport()) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400417 DeleteBackendTexture(context, backendTex);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400418 backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400419 kSize, kSize, kRGBA_8888_SkColorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400420 SkColors::kTransparent, GrMipMapped::kYes, GrRenderable::kYes,
421 GrProtected::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400422 proxy = proxyProvider->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership,
423 GrWrapCacheable::kNo, ioType);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400424 context->flushAndSubmit();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500425 proxy->peekTexture()->texturePriv().markMipMapsDirty(); // avoids assert in GrGpu.
426 auto regenResult =
Robert Phillips9da87e02019-02-04 13:26:26 -0500427 context->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500428 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
429 }
Brian Salomon28a8f282019-10-24 20:07:39 -0400430 DeleteBackendTexture(context, backendTex);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500431 }
432}
Brian Salomon614c1a82018-12-19 15:42:06 -0500433
Greg Daniel46cfbc62019-06-07 11:43:30 -0400434static const int kSurfSize = 10;
435
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400436static sk_sp<GrTexture> make_wrapped_texture(GrContext* context, GrRenderable renderable) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400437 GrBackendTexture backendTexture;
438 CreateBackendTexture(context, &backendTexture, kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
439 SkColors::kTransparent, GrMipMapped::kNo, renderable, GrProtected::kNo);
Greg Daniel16032b32020-05-06 15:31:10 -0400440 SkASSERT(backendTexture.isValid());
Brian Salomon8cabb322019-02-22 10:44:19 -0500441 sk_sp<GrTexture> texture;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400442 if (GrRenderable::kYes == renderable) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500443 texture = context->priv().resourceProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400444 backendTexture, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500445 } else {
446 texture = context->priv().resourceProvider()->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400447 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon8cabb322019-02-22 10:44:19 -0500448 }
449 // Add a release proc that deletes the GrBackendTexture.
450 struct ReleaseContext {
451 GrContext* fContext;
452 GrBackendTexture fBackendTexture;
Brian Salomon614c1a82018-12-19 15:42:06 -0500453 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500454 auto release = [](void* rc) {
455 auto releaseContext = static_cast<ReleaseContext*>(rc);
Robert Phillips9b16f812019-05-17 10:01:21 -0400456 auto context = releaseContext->fContext;
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400457 context->deleteBackendTexture(releaseContext->fBackendTexture);
Brian Salomon8cabb322019-02-22 10:44:19 -0500458 delete releaseContext;
459 };
Brian Salomon2ca31f82019-03-05 13:28:58 -0500460 texture->setRelease(release, new ReleaseContext{context, backendTexture});
Brian Salomon8cabb322019-02-22 10:44:19 -0500461 return texture;
462}
Brian Salomon614c1a82018-12-19 15:42:06 -0500463
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400464static sk_sp<GrTexture> make_normal_texture(GrContext* context, GrRenderable renderable) {
Brian Salomona56a7462020-02-07 14:17:25 -0500465 SkISize desc;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400466 desc.fWidth = desc.fHeight = kSurfSize;
Brian Salomon4eb38b72019-08-05 12:58:39 -0400467 auto format =
468 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Robert Phillips9313aa72019-04-09 18:41:27 -0400469 return context->priv().resourceProvider()->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400470 desc, format, renderable, 1, GrMipMapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500471}
Brian Salomon614c1a82018-12-19 15:42:06 -0500472
Brian Salomon8cabb322019-02-22 10:44:19 -0500473DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
474 // Various ways of making textures.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400475 auto makeWrapped = [](GrContext* context) {
476 return make_wrapped_texture(context, GrRenderable::kNo);
Brian Salomon614c1a82018-12-19 15:42:06 -0500477 };
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400478 auto makeWrappedRenderable = [](GrContext* context) {
479 return make_wrapped_texture(context, GrRenderable::kYes);
480 };
481 auto makeNormal = [](GrContext* context) {
482 return make_normal_texture(context, GrRenderable::kNo);
483 };
484 auto makeRenderable = [](GrContext* context) {
485 return make_normal_texture(context, GrRenderable::kYes);
486 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500487
488 std::function<sk_sp<GrTexture>(GrContext*)> makers[] = {makeWrapped, makeWrappedRenderable,
489 makeNormal, makeRenderable};
490
491 // Add a unique key, or not.
492 auto addKey = [](GrTexture* texture) {
493 static uint32_t gN = 0;
494 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
495 GrUniqueKey key;
496 GrUniqueKey::Builder builder(&key, kDomain, 1);
497 builder[0] = gN++;
498 builder.finish();
499 texture->resourcePriv().setUniqueKey(key);
500 };
501 auto dontAddKey = [](GrTexture* texture) {};
502 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
503
504 for (const auto& m : makers) {
505 for (const auto& keyAdder : keyAdders) {
506 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
507 sk_gpu_test::GrContextFactory factory;
508 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500509 GrContext* context = factory.get(contextType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500510 if (!context) {
511 continue;
512 }
513
514 // The callback we add simply adds an integer to a set.
515 std::set<int> idleIDs;
516 struct Context {
517 std::set<int>* fIdleIDs;
518 int fNum;
519 };
520 auto proc = [](void* context) {
521 static_cast<Context*>(context)->fIdleIDs->insert(
522 static_cast<Context*>(context)->fNum);
523 delete static_cast<Context*>(context);
524 };
525
526 // Makes a texture, possibly adds a key, and sets the callback.
527 auto make = [&m, &keyAdder, &proc, &idleIDs](GrContext* context, int num) {
528 sk_sp<GrTexture> texture = m(context);
Brian Salomone80b8092019-03-08 13:25:19 -0500529 texture->addIdleProc(proc, new Context{&idleIDs, num},
530 GrTexture::IdleState::kFinished);
Brian Salomon614c1a82018-12-19 15:42:06 -0500531 keyAdder(texture.get());
532 return texture;
533 };
534
535 auto texture = make(context, 1);
536 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400537 auto renderable = GrRenderable(SkToBool(texture->asRenderTarget()));
Brian Salomon614c1a82018-12-19 15:42:06 -0500538 auto backendFormat = texture->backendFormat();
539 texture.reset();
540 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
541
542 texture = make(context, 2);
Brian Salomon8cabb322019-02-22 10:44:19 -0500543 int w = texture->width();
544 int h = texture->height();
Brian Salomon614c1a82018-12-19 15:42:06 -0500545 SkImageInfo info =
Brian Salomon8cabb322019-02-22 10:44:19 -0500546 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500547 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
548 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomon63410e92020-03-23 18:32:50 -0400549 auto singleUseLazyCB = [&texture](GrResourceProvider*,
550 const GrSurfaceProxy::LazySurfaceDesc&) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400551 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
552 if (texture->getUniqueKey().isValid()) {
553 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
554 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400555 return GrSurfaceProxy::LazyCallbackResult{std::move(texture), true, mode};
Brian Salomon614c1a82018-12-19 15:42:06 -0500556 };
Brian Salomona56a7462020-02-07 14:17:25 -0500557 SkISize desc;
Brian Salomon8cabb322019-02-22 10:44:19 -0500558 desc.fWidth = w;
559 desc.fHeight = h;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500560 SkBudgeted budgeted;
561 if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
562 budgeted = SkBudgeted::kYes;
563 } else {
564 budgeted = SkBudgeted::kNo;
565 }
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400566 auto proxy = context->priv().proxyProvider()->createLazyProxy(
567 singleUseLazyCB, backendFormat, desc, renderable, 1, GrMipMapped::kNo,
568 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags ::kNone,
569 SkBackingFit::kExact, budgeted, GrProtected::kNo,
570 GrSurfaceProxy::UseAllocator::kYes);
Greg Danielce3ddaa2020-01-22 16:58:15 -0500571 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(
572 backendFormat, GrColorType::kRGBA_8888);
Greg Daniel40903af2020-01-30 14:55:05 -0500573 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, readSwizzle);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400574 rtc->drawTexture(nullptr, view, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400575 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
576 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
577 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
578 SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500579 // We still have the proxy, which should remain instantiated, thereby keeping the
580 // texture not purgeable.
581 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Greg Daniel0a2464f2020-05-14 15:45:44 -0400582 context->flushAndSubmit();
Brian Salomon614c1a82018-12-19 15:42:06 -0500583 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillips9da87e02019-02-04 13:26:26 -0500584 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500585 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
586
587 // This time we move the proxy into the draw.
Michael Ludwig7c12e282020-05-29 09:54:07 -0400588 rtc->drawTexture(nullptr, std::move(view), kPremul_SkAlphaType,
Greg Daniel40903af2020-01-30 14:55:05 -0500589 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
590 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
591 GrAA::kNo, GrQuadAAFlags::kNone,
Brian Salomonfc118442019-11-22 19:09:27 -0500592 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500593 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Greg Daniel0a2464f2020-05-14 15:45:44 -0400594 context->flushAndSubmit();
Robert Phillips9da87e02019-02-04 13:26:26 -0500595 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500596 // Now that the draw is fully consumed by the GPU, the texture should be idle.
597 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
598
Brian Salomon9bc76d92019-01-24 12:18:33 -0500599 // Make sure we make the call during various shutdown scenarios where the texture
600 // might persist after context is destroyed, abandoned, etc. We test three
601 // variations of each scenario. One where the texture is just created. Another,
602 // where the texture has been used in a draw and then the context is flushed. And
603 // one where the the texture was drawn but the context is not flushed.
604 // In each scenario we test holding a ref beyond the context shutdown and not.
Brian Salomon614c1a82018-12-19 15:42:06 -0500605
Stephen Whiteb007bb22020-06-04 09:02:04 -0400606 // These tests are difficult to get working with Vulkan, Direct3D, and Dawn.
607 // See http://skbug.com/8705, http://skbug.com/8277, and http://skbug.com/10326
Brian Salomon9bc76d92019-01-24 12:18:33 -0500608 GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
Stephen Whiteb007bb22020-06-04 09:02:04 -0400609 if (api == GrBackendApi::kVulkan || api == GrBackendApi::kDirect3D ||
610 api == GrBackendApi::kDawn) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500611 continue;
612 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400613 int id = 3;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500614 enum class DrawType {
615 kNoDraw,
616 kDraw,
617 kDrawAndFlush,
618 };
619 for (auto drawType :
620 {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) {
621 for (bool unrefFirst : {false, true}) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500622 auto possiblyDrawAndFlush = [&context, &texture, drawType, unrefFirst, w,
623 h] {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500624 if (drawType == DrawType::kNoDraw) {
625 return;
626 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500627 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500628 kPremul_SkAlphaType);
629 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
630 nullptr);
Robert Phillips9da87e02019-02-04 13:26:26 -0500631 auto rtc = rt->getCanvas()
Brian Salomon9bc76d92019-01-24 12:18:33 -0500632 ->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500633 auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400634 texture);
Greg Daniel40903af2020-01-30 14:55:05 -0500635 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(
636 proxy->backendFormat(), GrColorType::kRGBA_8888);
637 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
638 swizzle);
Brian Salomon8cabb322019-02-22 10:44:19 -0500639 rtc->drawTexture(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400640 nullptr, std::move(view), kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400641 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
642 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
643 GrAA::kNo, GrQuadAAFlags::kNone,
Brian Salomon8cabb322019-02-22 10:44:19 -0500644 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500645 if (drawType == DrawType::kDrawAndFlush) {
Greg Daniel0a2464f2020-05-14 15:45:44 -0400646 context->flushAndSubmit();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500647 }
648 if (unrefFirst) {
649 texture.reset();
650 }
651 };
652 texture = make(context, id);
653 possiblyDrawAndFlush();
654 context->abandonContext();
655 texture.reset();
656 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
657 factory.destroyContexts();
658 context = factory.get(contextType);
659 ++id;
Brian Salomon614c1a82018-12-19 15:42:06 -0500660
Brian Salomon9bc76d92019-01-24 12:18:33 -0500661 // Similar to previous, but reset the texture after the context was
662 // abandoned and then destroyed.
663 texture = make(context, id);
664 possiblyDrawAndFlush();
665 context->abandonContext();
666 factory.destroyContexts();
667 texture.reset();
668 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
669 context = factory.get(contextType);
670 id++;
671
672 texture = make(context, id);
673 possiblyDrawAndFlush();
674 factory.destroyContexts();
675 texture.reset();
676 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
677 context = factory.get(contextType);
678 id++;
679
680 texture = make(context, id);
681 possiblyDrawAndFlush();
682 factory.releaseResourcesAndAbandonContexts();
683 texture.reset();
684 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
685 context = factory.get(contextType);
686 id++;
687 }
688 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500689 }
690 }
691 }
692}
Brian Salomon8cabb322019-02-22 10:44:19 -0500693
694// Tests an idle proc that unrefs another resource down to zero.
695DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400696 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500697
698 // idle proc that releases another texture.
699 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); };
700
701 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
702 for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500703 for (auto idleState :
704 {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400705 auto idleTexture = idleMaker(context, GrRenderable::kNo);
706 auto otherTexture = otherMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500707 otherTexture->ref();
708 idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState);
709 otherTexture.reset();
710 idleTexture.reset();
711 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500712 }
713 }
714}
715
716// Similar to above but more complicated. This flushes the context from the idle proc.
717// crbug.com/933526.
718DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400719 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500720
721 // idle proc that flushes the context.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400722 auto idleProc = [](void* context) { reinterpret_cast<GrContext*>(context)->flushAndSubmit(); };
Brian Salomon8cabb322019-02-22 10:44:19 -0500723
724 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500725 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400726 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500727 idleTexture->addIdleProc(idleProc, context, idleState);
728 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
729 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 1, nullptr);
730 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
731 // is a wrapped-texture backed image.
732 surf->getCanvas()->clear(SK_ColorWHITE);
733 auto img1 = surf->makeImageSnapshot();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400734
735 GrBackendTexture backendTexture;
736
Brian Salomon28a8f282019-10-24 20:07:39 -0400737 if (!CreateBackendTexture(context, &backendTexture, info, SkColors::kBlack,
738 GrMipMapped::kNo, GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400739 REPORTER_ASSERT(reporter, false);
740 continue;
741 }
742
Brian Salomone80b8092019-03-08 13:25:19 -0500743 auto img2 = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
744 info.colorType(), info.alphaType(), nullptr);
745 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
746 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
747 idleTexture.reset();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400748
Brian Salomon28a8f282019-10-24 20:07:39 -0400749 DeleteBackendTexture(context, backendTexture);
Brian Salomone80b8092019-03-08 13:25:19 -0500750 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500751 }
752}
753
754DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400755 auto context = contextInfo.directContext();
Brian Salomon8cabb322019-02-22 10:44:19 -0500756 // idle proc that refs the texture
757 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
758 // release proc to check whether the texture was released or not.
759 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomone80b8092019-03-08 13:25:19 -0500760 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
761 bool isReleased = false;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400762 auto idleTexture = make_normal_texture(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500763 // This test assumes the texture won't be cached (or else the release proc doesn't get
764 // called).
765 idleTexture->resourcePriv().removeScratchKey();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400766 context->flushAndSubmit();
Brian Salomone80b8092019-03-08 13:25:19 -0500767 idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState);
768 idleTexture->setRelease(releaseProc, &isReleased);
769 auto* raw = idleTexture.get();
770 idleTexture.reset();
771 REPORTER_ASSERT(reporter, !isReleased);
772 raw->unref();
773 REPORTER_ASSERT(reporter, isReleased);
774 }
775}
776
777DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400778 auto context = contextInfo.directContext();
Brian Salomone80b8092019-03-08 13:25:19 -0500779 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400780 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500781
782 uint32_t flags = 0;
783 static constexpr uint32_t kFlushFlag = 0x1;
784 static constexpr uint32_t kFinishFlag = 0x2;
785 auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; };
786 auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; };
787 idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed);
788 idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished);
789
790 // Insert a copy from idleTexture to another texture so that we have some queued IO on
791 // idleTexture.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400792 SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
793 kPremul_SkAlphaType);
Brian Salomone80b8092019-03-08 13:25:19 -0500794 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
795 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400796 auto proxy =
797 context->priv().proxyProvider()->testingOnly_createWrapped(std::move(idleTexture));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400798 context->flushAndSubmit();
Brian Salomonc5243782020-04-02 12:50:34 -0400799 SkAssertResult(rtc->testCopy(proxy.get()));
Brian Salomone80b8092019-03-08 13:25:19 -0500800 proxy.reset();
801 REPORTER_ASSERT(reporter, flags == 0);
802
803 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
804 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
805 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400806 context->flushAndSubmit();
Brian Salomone80b8092019-03-08 13:25:19 -0500807 if (contextInfo.backend() == kVulkan_GrBackend) {
808 REPORTER_ASSERT(reporter, flags & kFlushFlag);
809 } else {
810 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
811 }
812 context->priv().getGpu()->testingOnly_flushGpuAndSync();
813 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
814 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500815}