blob: dc2133644df5d7f4f29962b017b4f5c32682a3cd [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"
10#include "include/gpu/GrContext.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 Daniel6f5441a2020-01-28 17:02:49 -050013#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrClip.h"
15#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrTexturePriv.h"
23#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) {
bsalomon8b7451a2016-05-11 06:33:06 -070029 GrContext* context = ctxInfo.grContext();
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 Salomona56a7462020-02-07 14:17:25 -050036 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 Salomona56a7462020-02-07 14:17:25 -050049 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,
Robert Phillipsda2e67a2019-07-01 15:04:06 -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) {
77 GrContext* context = ctxInfo.grContext();
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> {
Robert Phillipsd6df7b52019-12-13 11:17:46 -050087 SkImage::CompressionType compression = rp->caps()->compressionType(format);
88 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};
Robert Phillips48257d72019-12-16 14:20:53 -050095 GrFillInCompressedData(compression, dimensions, GrMipMapped::kNo,
96 (char*)data->writable_data(), color);
Robert Phillipse4720c62020-01-14 14:33:24 -050097 return rp->createCompressedTexture(dimensions, format, SkBudgeted::kNo,
Robert Phillips3a833922020-01-21 15:25:58 -050098 GrMipMapped::kNo, GrProtected::kNo, data.get());
Brian Salomonbb8dde82019-06-27 10:52:13 -040099 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500100 return rp->createTexture(dimensions, format, renderable, 1, GrMipMapped::kNo,
101 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 =
108 caps->getTestingCombinations();
Robert Phillips0902c982019-07-16 07:47:56 -0400109
Robert Phillipsffe27292019-08-01 10:08:07 -0400110 for (auto combo : combos) {
111
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
137 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 Salomondf1bd6d2020-03-26 20:37:01 -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(
153 kDims, combo.fFormat, GrRenderable::kYes, 1, GrMipMapped::kNo, SkBudgeted::kNo,
154 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(
166 kDims, combo.fFormat, GrRenderable::kYes, 2, GrMipMapped::kNo, SkBudgeted::kNo,
167 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 }
Brian Salomona3e29962019-07-16 11:52:08 -0400203 auto context = factory.get(contextType);
204 if (!context) {
205 continue;
206 }
Brian Salomona3e29962019-07-16 11:52:08 -0400207
Brian Salomona3e29962019-07-16 11:52:08 -0400208 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillipsffe27292019-08-01 10:08:07 -0400209 const GrCaps* caps = context->priv().caps();
Brian Salomona3e29962019-07-16 11:52:08 -0400210
Robert Phillipsffe27292019-08-01 10:08:07 -0400211 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
212 caps->getTestingCombinations();
213
214 for (auto combo : combos) {
215
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);
268 auto texCtx = GrSurfaceContext::Make(context, 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);
273 if (texCtx->readPixels(readback.info(), readback.writable_addr(),
274 readback.rowBytes(), {0, 0})) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400275 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400276 if (!checkColor(combo, readback.addr32()[i])) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400277 break;
278 }
279 }
280 }
281 }
282
283 context->priv().testingOnly_purgeAllUnlockedResources();
284 }
285
286 // Try creating the texture as a deferred proxy.
287 {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400288 std::unique_ptr<GrSurfaceContext> surfCtx;
Robert Phillipsffe27292019-08-01 10:08:07 -0400289 if (renderable == GrRenderable::kYes) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500290 surfCtx = GrRenderTargetContext::Make(
291 context, combo.fColorType, nullptr, fit,
292 {desc.fWidth, desc.fHeight}, 1, GrMipMapped::kNo,
293 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Robert Phillipsffe27292019-08-01 10:08:07 -0400294 } else {
Greg Danielbfa19c42019-12-19 16:41:40 -0500295 surfCtx = GrSurfaceContext::Make(
296 context, {desc.fWidth, desc.fHeight}, combo.fFormat,
297 GrRenderable::kNo, 1, GrMipMapped::kNo, GrProtected::kNo,
298 kTopLeft_GrSurfaceOrigin, combo.fColorType,
299 kUnknown_SkAlphaType, nullptr, fit, SkBudgeted::kYes);
Robert Phillipsffe27292019-08-01 10:08:07 -0400300 }
301 if (!surfCtx) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400302 continue;
303 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400304
Robert Phillipsd3442842019-08-02 12:26:22 -0400305 readback.erase(kClearColor);
306 if (surfCtx->readPixels(readback.info(), readback.writable_addr(),
307 readback.rowBytes(), {0, 0})) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400308 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400309 if (!checkColor(combo, readback.addr32()[i])) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400310 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400311 }
312 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400313 }
Brian Salomona3e29962019-07-16 11:52:08 -0400314 context->priv().testingOnly_purgeAllUnlockedResources();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400315 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400316 }
317 }
318 }
319 }
320}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500321
322DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400323 auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500324 for (int y = 0; y < p->height(); ++y) {
325 for (int x = 0; x < p->width(); ++x) {
326 *p->writable_addr32(x, y) = f(x, y);
327 }
328 }
329 };
330
331 auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) {
332 SkASSERT(p1.info() == p2.info());
333 for (int y = 0; y < p1.height(); ++y) {
334 for (int x = 0; x < p1.width(); ++x) {
335 REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y));
336 if (p1.getColor(x, y) != p2.getColor(x, y)) {
337 return;
338 }
339 }
340 }
341 };
342
343 static constexpr int kSize = 100;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400344 SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
345 SkAutoPixmapStorage srcPixmap;
346 srcPixmap.alloc(ii);
347 fillPixels(&srcPixmap,
Robert Phillipscb1adb42019-06-10 15:09:34 -0400348 [](int x, int y) {
349 return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
350 });
Brian Salomonc67c31c2018-12-06 10:00:03 -0500351
352 GrContext* context = context_info.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500353 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500354
355 // We test both kRW in addition to kRead mostly to ensure that the calls are structured such
356 // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
357 // kRead for the right reason.
358 for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
Robert Phillips66944402019-09-30 13:21:25 -0400359 auto backendTex = context->createBackendTexture(&srcPixmap, 1,
360 GrRenderable::kYes, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400361
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400362 auto proxy = proxyProvider->wrapBackendTexture(
363 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, ioType);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500364 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
365 GrColorType::kRGBA_8888);
366 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
367 auto surfContext = GrSurfaceContext::Make(context, std::move(view), GrColorType::kRGBA_8888,
Greg Danielbfa19c42019-12-19 16:41:40 -0500368 kPremul_SkAlphaType, nullptr);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500369
370 // Read pixels should work with a read-only texture.
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400371 {
372 SkAutoPixmapStorage read;
373 read.alloc(srcPixmap.info());
374 auto readResult = surfContext->readPixels(srcPixmap.info(), read.writable_addr(),
375 0, { 0, 0 });
376 REPORTER_ASSERT(reporter, readResult);
377 if (readResult) {
378 comparePixels(srcPixmap, read, reporter);
379 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500380 }
381
382 // Write pixels should not work with a read-only texture.
383 SkAutoPixmapStorage write;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400384 write.alloc(srcPixmap.info());
385 fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); });
386 auto writeResult = surfContext->writePixels(srcPixmap.info(), write.addr(), 0, {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500387 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
388 // Try the low level write.
389 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500390 auto gpuWriteResult = context->priv().getGpu()->writePixels(
Brian Salomonf77c1462019-08-01 15:19:29 -0400391 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888,
392 GrColorType::kRGBA_8888, write.addr32(),
Brian Salomon1047a492019-07-02 12:25:21 -0400393 kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500394 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
395
Greg Daniel6f5441a2020-01-28 17:02:49 -0500396 SkBitmap copySrcBitmap;
397 copySrcBitmap.installPixels(write);
398 copySrcBitmap.setImmutable();
399
Brian Salomonbc074a62020-03-18 10:06:13 -0400400 GrBitmapTextureMaker maker(context, copySrcBitmap,
401 GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500402 auto copySrc = maker.view(GrMipMapped::kNo);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500403
Greg Danielcc104db2020-02-03 14:17:08 -0500404 REPORTER_ASSERT(reporter, copySrc.proxy());
Brian Salomonc5243782020-04-02 12:50:34 -0400405 auto copyResult = surfContext->testCopy(copySrc.proxy());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500406 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
407 // Try the low level copy.
408 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500409 auto gpuCopyResult = context->priv().getGpu()->copySurface(
Greg Danielcc104db2020-02-03 14:17:08 -0500410 proxy->peekSurface(), copySrc.proxy()->peekSurface(), SkIRect::MakeWH(kSize, kSize),
Greg Daniel46cfbc62019-06-07 11:43:30 -0400411 {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500412 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
413
414 // Mip regen should not work with a read only texture.
Robert Phillips9da87e02019-02-04 13:26:26 -0500415 if (context->priv().caps()->mipMapSupport()) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400416 DeleteBackendTexture(context, backendTex);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400417 backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400418 kSize, kSize, kRGBA_8888_SkColorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400419 SkColors::kTransparent, GrMipMapped::kYes, GrRenderable::kYes,
420 GrProtected::kNo);
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400421 proxy = proxyProvider->wrapBackendTexture(backendTex, kBorrow_GrWrapOwnership,
422 GrWrapCacheable::kNo, ioType);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500423 context->flush();
424 proxy->peekTexture()->texturePriv().markMipMapsDirty(); // avoids assert in GrGpu.
425 auto regenResult =
Robert Phillips9da87e02019-02-04 13:26:26 -0500426 context->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500427 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
428 }
Brian Salomon28a8f282019-10-24 20:07:39 -0400429 DeleteBackendTexture(context, backendTex);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500430 }
431}
Brian Salomon614c1a82018-12-19 15:42:06 -0500432
Greg Daniel46cfbc62019-06-07 11:43:30 -0400433static const int kSurfSize = 10;
434
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400435static sk_sp<GrTexture> make_wrapped_texture(GrContext* context, GrRenderable renderable) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400436 auto backendTexture = context->createBackendTexture(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400437 kSurfSize, kSurfSize, kRGBA_8888_SkColorType, SkColors::kTransparent, GrMipMapped::kNo,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400438 renderable, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500439 sk_sp<GrTexture> texture;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400440 if (GrRenderable::kYes == renderable) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500441 texture = context->priv().resourceProvider()->wrapRenderableBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400442 backendTexture, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500443 } else {
444 texture = context->priv().resourceProvider()->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400445 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon8cabb322019-02-22 10:44:19 -0500446 }
447 // Add a release proc that deletes the GrBackendTexture.
448 struct ReleaseContext {
449 GrContext* fContext;
450 GrBackendTexture fBackendTexture;
Brian Salomon614c1a82018-12-19 15:42:06 -0500451 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500452 auto release = [](void* rc) {
453 auto releaseContext = static_cast<ReleaseContext*>(rc);
Robert Phillips9b16f812019-05-17 10:01:21 -0400454 auto context = releaseContext->fContext;
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400455 context->deleteBackendTexture(releaseContext->fBackendTexture);
Brian Salomon8cabb322019-02-22 10:44:19 -0500456 delete releaseContext;
457 };
Brian Salomon2ca31f82019-03-05 13:28:58 -0500458 texture->setRelease(release, new ReleaseContext{context, backendTexture});
Brian Salomon8cabb322019-02-22 10:44:19 -0500459 return texture;
460}
Brian Salomon614c1a82018-12-19 15:42:06 -0500461
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400462static sk_sp<GrTexture> make_normal_texture(GrContext* context, GrRenderable renderable) {
Brian Salomona56a7462020-02-07 14:17:25 -0500463 SkISize desc;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400464 desc.fWidth = desc.fHeight = kSurfSize;
Brian Salomon4eb38b72019-08-05 12:58:39 -0400465 auto format =
466 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Robert Phillips9313aa72019-04-09 18:41:27 -0400467 return context->priv().resourceProvider()->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400468 desc, format, renderable, 1, GrMipMapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500469}
Brian Salomon614c1a82018-12-19 15:42:06 -0500470
Brian Salomon8cabb322019-02-22 10:44:19 -0500471DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
472 // Various ways of making textures.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400473 auto makeWrapped = [](GrContext* context) {
474 return make_wrapped_texture(context, GrRenderable::kNo);
Brian Salomon614c1a82018-12-19 15:42:06 -0500475 };
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400476 auto makeWrappedRenderable = [](GrContext* context) {
477 return make_wrapped_texture(context, GrRenderable::kYes);
478 };
479 auto makeNormal = [](GrContext* context) {
480 return make_normal_texture(context, GrRenderable::kNo);
481 };
482 auto makeRenderable = [](GrContext* context) {
483 return make_normal_texture(context, GrRenderable::kYes);
484 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500485
486 std::function<sk_sp<GrTexture>(GrContext*)> makers[] = {makeWrapped, makeWrappedRenderable,
487 makeNormal, makeRenderable};
488
489 // Add a unique key, or not.
490 auto addKey = [](GrTexture* texture) {
491 static uint32_t gN = 0;
492 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
493 GrUniqueKey key;
494 GrUniqueKey::Builder builder(&key, kDomain, 1);
495 builder[0] = gN++;
496 builder.finish();
497 texture->resourcePriv().setUniqueKey(key);
498 };
499 auto dontAddKey = [](GrTexture* texture) {};
500 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
501
502 for (const auto& m : makers) {
503 for (const auto& keyAdder : keyAdders) {
504 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
505 sk_gpu_test::GrContextFactory factory;
506 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500507 GrContext* context = factory.get(contextType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500508 if (!context) {
509 continue;
510 }
511
512 // The callback we add simply adds an integer to a set.
513 std::set<int> idleIDs;
514 struct Context {
515 std::set<int>* fIdleIDs;
516 int fNum;
517 };
518 auto proc = [](void* context) {
519 static_cast<Context*>(context)->fIdleIDs->insert(
520 static_cast<Context*>(context)->fNum);
521 delete static_cast<Context*>(context);
522 };
523
524 // Makes a texture, possibly adds a key, and sets the callback.
525 auto make = [&m, &keyAdder, &proc, &idleIDs](GrContext* context, int num) {
526 sk_sp<GrTexture> texture = m(context);
Brian Salomone80b8092019-03-08 13:25:19 -0500527 texture->addIdleProc(proc, new Context{&idleIDs, num},
528 GrTexture::IdleState::kFinished);
Brian Salomon614c1a82018-12-19 15:42:06 -0500529 keyAdder(texture.get());
530 return texture;
531 };
532
533 auto texture = make(context, 1);
534 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400535 auto renderable = GrRenderable(SkToBool(texture->asRenderTarget()));
Brian Salomon614c1a82018-12-19 15:42:06 -0500536 auto backendFormat = texture->backendFormat();
537 texture.reset();
538 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
539
540 texture = make(context, 2);
Brian Salomon8cabb322019-02-22 10:44:19 -0500541 int w = texture->width();
542 int h = texture->height();
Brian Salomon614c1a82018-12-19 15:42:06 -0500543 SkImageInfo info =
Brian Salomon8cabb322019-02-22 10:44:19 -0500544 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500545 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
546 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomon63410e92020-03-23 18:32:50 -0400547 auto singleUseLazyCB = [&texture](GrResourceProvider*,
548 const GrSurfaceProxy::LazySurfaceDesc&) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400549 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
550 if (texture->getUniqueKey().isValid()) {
551 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
552 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400553 return GrSurfaceProxy::LazyCallbackResult{std::move(texture), true, mode};
Brian Salomon614c1a82018-12-19 15:42:06 -0500554 };
Brian Salomona56a7462020-02-07 14:17:25 -0500555 SkISize desc;
Brian Salomon8cabb322019-02-22 10:44:19 -0500556 desc.fWidth = w;
557 desc.fHeight = h;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500558 SkBudgeted budgeted;
559 if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
560 budgeted = SkBudgeted::kYes;
561 } else {
562 budgeted = SkBudgeted::kNo;
563 }
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400564 auto proxy = context->priv().proxyProvider()->createLazyProxy(
565 singleUseLazyCB, backendFormat, desc, renderable, 1, GrMipMapped::kNo,
566 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags ::kNone,
567 SkBackingFit::kExact, budgeted, GrProtected::kNo,
568 GrSurfaceProxy::UseAllocator::kYes);
Greg Danielce3ddaa2020-01-22 16:58:15 -0500569 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(
570 backendFormat, GrColorType::kRGBA_8888);
Greg Daniel40903af2020-01-30 14:55:05 -0500571 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin, readSwizzle);
572 rtc->drawTexture(GrNoClip(), view, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400573 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
574 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
575 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
576 SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500577 // We still have the proxy, which should remain instantiated, thereby keeping the
578 // texture not purgeable.
579 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
580 context->flush();
581 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillips9da87e02019-02-04 13:26:26 -0500582 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500583 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
584
585 // This time we move the proxy into the draw.
Greg Daniel40903af2020-01-30 14:55:05 -0500586 rtc->drawTexture(GrNoClip(), std::move(view), kPremul_SkAlphaType,
587 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
588 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
589 GrAA::kNo, GrQuadAAFlags::kNone,
Brian Salomonfc118442019-11-22 19:09:27 -0500590 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500591 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
592 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500593 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500594 // Now that the draw is fully consumed by the GPU, the texture should be idle.
595 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
596
Brian Salomon9bc76d92019-01-24 12:18:33 -0500597 // Make sure we make the call during various shutdown scenarios where the texture
598 // might persist after context is destroyed, abandoned, etc. We test three
599 // variations of each scenario. One where the texture is just created. Another,
600 // where the texture has been used in a draw and then the context is flushed. And
601 // one where the the texture was drawn but the context is not flushed.
602 // In each scenario we test holding a ref beyond the context shutdown and not.
Brian Salomon614c1a82018-12-19 15:42:06 -0500603
Brian Salomon9bc76d92019-01-24 12:18:33 -0500604 // These tests are difficult to get working with Vulkan. See http://skbug.com/8705
605 // and http://skbug.com/8275
606 GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
607 if (api == GrBackendApi::kVulkan) {
608 continue;
609 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400610 int id = 3;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500611 enum class DrawType {
612 kNoDraw,
613 kDraw,
614 kDrawAndFlush,
615 };
616 for (auto drawType :
617 {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) {
618 for (bool unrefFirst : {false, true}) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500619 auto possiblyDrawAndFlush = [&context, &texture, drawType, unrefFirst, w,
620 h] {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500621 if (drawType == DrawType::kNoDraw) {
622 return;
623 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500624 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500625 kPremul_SkAlphaType);
626 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
627 nullptr);
Robert Phillips9da87e02019-02-04 13:26:26 -0500628 auto rtc = rt->getCanvas()
Brian Salomon9bc76d92019-01-24 12:18:33 -0500629 ->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500630 auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400631 texture);
Greg Daniel40903af2020-01-30 14:55:05 -0500632 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(
633 proxy->backendFormat(), GrColorType::kRGBA_8888);
634 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
635 swizzle);
Brian Salomon8cabb322019-02-22 10:44:19 -0500636 rtc->drawTexture(
Greg Daniel40903af2020-01-30 14:55:05 -0500637 GrNoClip(), std::move(view), kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400638 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
639 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
640 GrAA::kNo, GrQuadAAFlags::kNone,
Brian Salomon8cabb322019-02-22 10:44:19 -0500641 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500642 if (drawType == DrawType::kDrawAndFlush) {
643 context->flush();
644 }
645 if (unrefFirst) {
646 texture.reset();
647 }
648 };
649 texture = make(context, id);
650 possiblyDrawAndFlush();
651 context->abandonContext();
652 texture.reset();
653 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
654 factory.destroyContexts();
655 context = factory.get(contextType);
656 ++id;
Brian Salomon614c1a82018-12-19 15:42:06 -0500657
Brian Salomon9bc76d92019-01-24 12:18:33 -0500658 // Similar to previous, but reset the texture after the context was
659 // abandoned and then destroyed.
660 texture = make(context, id);
661 possiblyDrawAndFlush();
662 context->abandonContext();
663 factory.destroyContexts();
664 texture.reset();
665 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
666 context = factory.get(contextType);
667 id++;
668
669 texture = make(context, id);
670 possiblyDrawAndFlush();
671 factory.destroyContexts();
672 texture.reset();
673 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
674 context = factory.get(contextType);
675 id++;
676
677 texture = make(context, id);
678 possiblyDrawAndFlush();
679 factory.releaseResourcesAndAbandonContexts();
680 texture.reset();
681 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
682 context = factory.get(contextType);
683 id++;
684 }
685 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500686 }
687 }
688 }
689}
Brian Salomon8cabb322019-02-22 10:44:19 -0500690
691// Tests an idle proc that unrefs another resource down to zero.
692DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) {
693 GrContext* context = contextInfo.grContext();
694
695 // idle proc that releases another texture.
696 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); };
697
698 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
699 for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500700 for (auto idleState :
701 {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400702 auto idleTexture = idleMaker(context, GrRenderable::kNo);
703 auto otherTexture = otherMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500704 otherTexture->ref();
705 idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState);
706 otherTexture.reset();
707 idleTexture.reset();
708 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500709 }
710 }
711}
712
713// Similar to above but more complicated. This flushes the context from the idle proc.
714// crbug.com/933526.
715DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) {
716 GrContext* context = contextInfo.grContext();
717
718 // idle proc that flushes the context.
719 auto idleProc = [](void* context) { reinterpret_cast<GrContext*>(context)->flush(); };
720
721 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500722 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400723 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500724 idleTexture->addIdleProc(idleProc, context, idleState);
725 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
726 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 1, nullptr);
727 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
728 // is a wrapped-texture backed image.
729 surf->getCanvas()->clear(SK_ColorWHITE);
730 auto img1 = surf->makeImageSnapshot();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400731
732 GrBackendTexture backendTexture;
733
Brian Salomon28a8f282019-10-24 20:07:39 -0400734 if (!CreateBackendTexture(context, &backendTexture, info, SkColors::kBlack,
735 GrMipMapped::kNo, GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400736 REPORTER_ASSERT(reporter, false);
737 continue;
738 }
739
Brian Salomone80b8092019-03-08 13:25:19 -0500740 auto img2 = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
741 info.colorType(), info.alphaType(), nullptr);
742 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
743 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
744 idleTexture.reset();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400745
Brian Salomon28a8f282019-10-24 20:07:39 -0400746 DeleteBackendTexture(context, backendTexture);
Brian Salomone80b8092019-03-08 13:25:19 -0500747 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500748 }
749}
750
751DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
752 GrContext* context = contextInfo.grContext();
753 // idle proc that refs the texture
754 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
755 // release proc to check whether the texture was released or not.
756 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomone80b8092019-03-08 13:25:19 -0500757 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
758 bool isReleased = false;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400759 auto idleTexture = make_normal_texture(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500760 // This test assumes the texture won't be cached (or else the release proc doesn't get
761 // called).
762 idleTexture->resourcePriv().removeScratchKey();
763 context->flush();
764 idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState);
765 idleTexture->setRelease(releaseProc, &isReleased);
766 auto* raw = idleTexture.get();
767 idleTexture.reset();
768 REPORTER_ASSERT(reporter, !isReleased);
769 raw->unref();
770 REPORTER_ASSERT(reporter, isReleased);
771 }
772}
773
774DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
775 GrContext* context = contextInfo.grContext();
776 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400777 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500778
779 uint32_t flags = 0;
780 static constexpr uint32_t kFlushFlag = 0x1;
781 static constexpr uint32_t kFinishFlag = 0x2;
782 auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; };
783 auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; };
784 idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed);
785 idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished);
786
787 // Insert a copy from idleTexture to another texture so that we have some queued IO on
788 // idleTexture.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400789 SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
790 kPremul_SkAlphaType);
Brian Salomone80b8092019-03-08 13:25:19 -0500791 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
792 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400793 auto proxy =
794 context->priv().proxyProvider()->testingOnly_createWrapped(std::move(idleTexture));
Brian Salomone80b8092019-03-08 13:25:19 -0500795 context->flush();
Brian Salomonc5243782020-04-02 12:50:34 -0400796 SkAssertResult(rtc->testCopy(proxy.get()));
Brian Salomone80b8092019-03-08 13:25:19 -0500797 proxy.reset();
798 REPORTER_ASSERT(reporter, flags == 0);
799
800 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
801 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
802 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
803 context->flush();
804 if (contextInfo.backend() == kVulkan_GrBackend) {
805 REPORTER_ASSERT(reporter, flags & kFlushFlag);
806 } else {
807 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
808 }
809 context->priv().getGpu()->testingOnly_flushGpuAndSync();
810 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
811 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500812}