blob: de4e328ff7a15175846a1ba3b306e7bcc54d67ea [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 "include/gpu/GrTexture.h"
12#include "src/core/SkAutoPixmapStorage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrClip.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040016#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrProxyProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040018#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/GrTexturePriv.h"
21#include "tests/Test.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040022#include "tests/TestUtils.h"
bsalomon@google.com686bcb82013-04-09 15:04:12 +000023
bsalomona2c23232014-11-25 07:41:12 -080024// Tests that GrSurface::asTexture(), GrSurface::asRenderTarget(), and static upcasting of texture
25// and render targets to GrSurface all work as expected.
Brian Osmanfbe24062019-04-03 16:04:45 +000026DEF_GPUTEST_FOR_MOCK_CONTEXT(GrSurface, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070027 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -050028 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050029
kkinnunen15302832015-12-01 04:35:26 -080030 GrSurfaceDesc desc;
kkinnunen15302832015-12-01 04:35:26 -080031 desc.fWidth = 256;
32 desc.fHeight = 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 =
36 resourceProvider->createTexture(desc, format, GrRenderable::kYes, 1, GrMipMapped::kNo,
37 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 =
49 resourceProvider->createTexture(desc, format, GrRenderable::kNo, 1, GrMipMapped::kNo,
50 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(
Robert Phillips0902c982019-07-16 07:47:56 -040060 backendTex, 1, GrColorType::kRGBA_8888, 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 Phillips3da9e942020-01-27 21:05:23 +000092 auto size = GrCompressedDataSize(compression, dimensions, nullptr, GrMipMapped::kNo);
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 {
100 GrSurfaceDesc desc;
Robert Phillips48257d72019-12-16 14:20:53 -0500101 desc.fWidth = dimensions.width();
102 desc.fHeight = dimensions.height();
Brian Salomona90382f2019-09-17 09:01:56 -0400103 return rp->createTexture(desc, format, renderable, 1, GrMipMapped::kNo, SkBudgeted::kNo,
104 GrProtected::kNo);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400105 }
106 };
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400107
Robert Phillipsffe27292019-08-01 10:08:07 -0400108 static constexpr int kW = 64;
109 static constexpr int kH = 64;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400110
Robert Phillipsffe27292019-08-01 10:08:07 -0400111 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
112 caps->getTestingCombinations();
Robert Phillips0902c982019-07-16 07:47:56 -0400113
Robert Phillipsffe27292019-08-01 10:08:07 -0400114 for (auto combo : combos) {
115
116 SkASSERT(combo.fColorType != GrColorType::kUnknown);
117 SkASSERT(combo.fFormat.isValid());
118
119 // Right now Vulkan has two backend formats that support ABGR_4444 (R4G4B4A4 and B4G4R4A4).
120 // Until we can create textures directly from the backend format this yields some
121 // ambiguity in what is actually supported and which textures can be created.
122 if (ctxInfo.backend() == kVulkan_GrBackend && combo.fColorType == GrColorType::kABGR_4444) {
Robert Phillips0902c982019-07-16 07:47:56 -0400123 continue;
124 }
125
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400126 for (GrSurfaceOrigin origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400127 GrSurfaceDesc desc;
128 desc.fWidth = kW;
129 desc.fHeight = kH;
Robert Phillipsdf6c7342019-12-16 13:40:51 -0500130
Robert Phillipsffe27292019-08-01 10:08:07 -0400131 // Check if 'isFormatTexturable' agrees with 'createTexture' and that the mipmap
132 // support check is working
133 {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400134
Robert Phillipsd6df7b52019-12-13 11:17:46 -0500135 bool isCompressed = caps->isFormatCompressed(combo.fFormat);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400136 bool isTexturable;
Robert Phillipsd6df7b52019-12-13 11:17:46 -0500137 if (isCompressed) {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400138 isTexturable = caps->isFormatTexturable(combo.fFormat);
139 } else {
140 isTexturable = caps->isFormatTexturableAndUploadable(combo.fColorType,
141 combo.fFormat);
142 }
Brian Osman48c99192017-06-02 08:45:06 -0400143
Robert Phillips48257d72019-12-16 14:20:53 -0500144 sk_sp<GrSurface> tex = createTexture({kW, kH}, combo.fColorType, combo.fFormat,
Robert Phillipsffe27292019-08-01 10:08:07 -0400145 GrRenderable::kNo, resourceProvider);
146 REPORTER_ASSERT(reporter, SkToBool(tex) == isTexturable,
Robert Phillipsbac46722019-08-01 15:09:17 -0400147 "ct:%s format:%s, tex:%d, isTexturable:%d",
Robert Phillipsffe27292019-08-01 10:08:07 -0400148 GrColorTypeToStr(combo.fColorType),
Robert Phillipsbac46722019-08-01 15:09:17 -0400149 combo.fFormat.toStr().c_str(),
150 SkToBool(tex), isTexturable);
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400151
Robert Phillipsffe27292019-08-01 10:08:07 -0400152 // Check that the lack of mipmap support blocks the creation of mipmapped
153 // proxies
Robert Phillipsbac46722019-08-01 15:09:17 -0400154 bool expectedMipMapability = isTexturable && caps->mipMapSupport() &&
Robert Phillipsd6df7b52019-12-13 11:17:46 -0500155 !isCompressed;
Robert Phillipsbac46722019-08-01 15:09:17 -0400156
Greg Daniel47c20e82020-01-21 14:29:57 -0500157 GrSwizzle swizzle = caps->getReadSwizzle(combo.fFormat, combo.fColorType);
158
Brian Salomonbeb7f522019-08-30 16:19:42 -0400159 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -0500160 combo.fFormat, desc, swizzle, GrRenderable::kNo, 1, origin,
161 GrMipMapped::kYes, SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
Robert Phillipsbac46722019-08-01 15:09:17 -0400162 REPORTER_ASSERT(reporter, SkToBool(proxy.get()) == expectedMipMapability,
163 "ct:%s format:%s, tex:%d, expectedMipMapability:%d",
164 GrColorTypeToStr(combo.fColorType),
165 combo.fFormat.toStr().c_str(),
166 SkToBool(proxy.get()), expectedMipMapability);
Robert Phillipsffe27292019-08-01 10:08:07 -0400167 }
168
Greg Daniel900583a2019-08-06 12:05:31 -0400169 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' (w/o MSAA)
Robert Phillipsffe27292019-08-01 10:08:07 -0400170 {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400171 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 1);
Robert Phillipsffe27292019-08-01 10:08:07 -0400172
Brian Salomon4eb38b72019-08-05 12:58:39 -0400173 sk_sp<GrSurface> tex = resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400174 desc, combo.fFormat, GrRenderable::kYes, 1, GrMipMapped::kNo,
175 SkBudgeted::kNo, GrProtected::kNo);
Robert Phillipsffe27292019-08-01 10:08:07 -0400176 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
Robert Phillipsbac46722019-08-01 15:09:17 -0400177 "ct:%s format:%s, tex:%d, isRenderable:%d",
Robert Phillipsffe27292019-08-01 10:08:07 -0400178 GrColorTypeToStr(combo.fColorType),
Robert Phillipsbac46722019-08-01 15:09:17 -0400179 combo.fFormat.toStr().c_str(),
180 SkToBool(tex), isRenderable);
Robert Phillipsffe27292019-08-01 10:08:07 -0400181 }
182
Greg Daniel900583a2019-08-06 12:05:31 -0400183 // Check if 'isFormatAsColorTypeRenderable' agrees with 'createTexture' w/ MSAA
Robert Phillipsffe27292019-08-01 10:08:07 -0400184 {
Greg Daniel6fa62e22019-08-07 15:52:37 -0400185 bool isRenderable = caps->isFormatRenderable(combo.fFormat, 2);
Robert Phillipsffe27292019-08-01 10:08:07 -0400186
Brian Salomon4eb38b72019-08-05 12:58:39 -0400187 sk_sp<GrSurface> tex = resourceProvider->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400188 desc, combo.fFormat, GrRenderable::kYes, 2, GrMipMapped::kNo,
189 SkBudgeted::kNo, GrProtected::kNo);
Robert Phillipsffe27292019-08-01 10:08:07 -0400190 REPORTER_ASSERT(reporter, SkToBool(tex) == isRenderable,
Robert Phillipsbac46722019-08-01 15:09:17 -0400191 "ct:%s format:%s, tex:%d, isRenderable:%d",
Robert Phillipsffe27292019-08-01 10:08:07 -0400192 GrColorTypeToStr(combo.fColorType),
Robert Phillipsbac46722019-08-01 15:09:17 -0400193 combo.fFormat.toStr().c_str(),
194 SkToBool(tex), isRenderable);
Robert Phillipsffe27292019-08-01 10:08:07 -0400195 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400196 }
197 }
198}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400199
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500200#include "src/gpu/GrDrawingManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -0400201#include "src/gpu/GrSurfaceProxy.h"
Brian Salomond17b4a62017-05-23 16:53:47 -0400202
Robert Phillipsffe27292019-08-01 10:08:07 -0400203// For each context, set it to always clear the textures and then run through all the
204// supported formats checking that the textures are actually cleared
Brian Salomona3e29962019-07-16 11:52:08 -0400205DEF_GPUTEST(InitialTextureClear, reporter, baseOptions) {
206 GrContextOptions options = baseOptions;
207 options.fClearAllTextures = true;
Robert Phillipsd3442842019-08-02 12:26:22 -0400208
Brian Salomond17b4a62017-05-23 16:53:47 -0400209 static constexpr int kSize = 100;
Robert Phillipsd3442842019-08-02 12:26:22 -0400210 static constexpr SkColor kClearColor = 0xABABABAB;
Robert Phillipsffe27292019-08-01 10:08:07 -0400211
212 const SkImageInfo info = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
213 kPremul_SkAlphaType);
214
Robert Phillipsd3442842019-08-02 12:26:22 -0400215 SkAutoPixmapStorage readback;
216 readback.alloc(info);
217
Robert Phillipsffe27292019-08-01 10:08:07 -0400218 GrSurfaceDesc desc;
219 desc.fWidth = desc.fHeight = kSize;
220
Brian Salomona3e29962019-07-16 11:52:08 -0400221 for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
222 sk_gpu_test::GrContextFactory factory(options);
223 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
224 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400225 continue;
226 }
Brian Salomona3e29962019-07-16 11:52:08 -0400227 auto context = factory.get(contextType);
228 if (!context) {
229 continue;
230 }
Brian Salomona3e29962019-07-16 11:52:08 -0400231
Brian Salomona3e29962019-07-16 11:52:08 -0400232 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillipsffe27292019-08-01 10:08:07 -0400233 const GrCaps* caps = context->priv().caps();
Brian Salomona3e29962019-07-16 11:52:08 -0400234
Robert Phillipsffe27292019-08-01 10:08:07 -0400235 const std::vector<GrCaps::TestFormatColorTypeCombination>& combos =
236 caps->getTestingCombinations();
237
238 for (auto combo : combos) {
239
240 SkASSERT(combo.fColorType != GrColorType::kUnknown);
241 SkASSERT(combo.fFormat.isValid());
242
Greg Daniel7bfc9132019-08-14 14:23:53 -0400243 if (!caps->isFormatTexturableAndUploadable(combo.fColorType, combo.fFormat)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400244 continue;
245 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400246
Brian Salomon614cdab2019-09-26 11:04:28 -0400247 auto checkColor = [reporter](const GrCaps::TestFormatColorTypeCombination& combo,
248 uint32_t readColor) {
249 // We expect that if there is no alpha in the src color type and we read it to a
250 // color type with alpha that we will get one for alpha rather than zero. We used to
251 // require this but the Intel Iris 6100 on Win 10 test bot doesn't put one in the
252 // alpha channel when reading back from GL_RG16 or GL_RG16F. So now we allow either.
253 uint32_t components = GrColorTypeComponentFlags(combo.fColorType);
254 bool allowAlphaOne = !(components & kAlpha_SkColorTypeComponentFlag);
255 if (allowAlphaOne) {
256 if (readColor != 0x00000000 && readColor != 0xFF000000) {
257 ERRORF(reporter,
258 "Failed on ct %s format %s 0x%08x is not 0x00000000 or 0xFF000000",
259 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
260 readColor);
261 return false;
262 }
263 } else {
264 if (readColor) {
265 ERRORF(reporter, "Failed on ct %s format %s 0x%08x != 0x00000000",
266 GrColorTypeToStr(combo.fColorType), combo.fFormat.toStr().c_str(),
267 readColor);
268 return false;
269 }
270 }
271 return true;
272 };
Robert Phillipsffe27292019-08-01 10:08:07 -0400273
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400274 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400275 if (renderable == GrRenderable::kYes &&
Greg Daniel900583a2019-08-06 12:05:31 -0400276 !caps->isFormatAsColorTypeRenderable(combo.fColorType, combo.fFormat)) {
Brian Salomona3e29962019-07-16 11:52:08 -0400277 continue;
278 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400279
280 for (auto fit : {SkBackingFit::kApprox, SkBackingFit::kExact}) {
281
282 // Does directly allocating a texture clear it?
283 {
Chris Daltond004e0b2018-09-27 09:28:03 -0600284 auto proxy = proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -0400285 {kSize, kSize}, combo.fColorType, combo.fFormat, renderable, 1,
286 kTopLeft_GrSurfaceOrigin, fit, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillipsffe27292019-08-01 10:08:07 -0400287 if (proxy) {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500288 GrSwizzle swizzle = caps->getReadSwizzle(combo.fFormat,
289 combo.fColorType);
290 GrSurfaceProxyView view(std::move(proxy), kTopLeft_GrSurfaceOrigin,
291 swizzle);
292 auto texCtx = GrSurfaceContext::Make(context, std::move(view),
Greg Danielbfa19c42019-12-19 16:41:40 -0500293 combo.fColorType,
294 kPremul_SkAlphaType, nullptr);
Robert Phillipsffe27292019-08-01 10:08:07 -0400295
Robert Phillipsd3442842019-08-02 12:26:22 -0400296 readback.erase(kClearColor);
297 if (texCtx->readPixels(readback.info(), readback.writable_addr(),
298 readback.rowBytes(), {0, 0})) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400299 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400300 if (!checkColor(combo, readback.addr32()[i])) {
Robert Phillipsffe27292019-08-01 10:08:07 -0400301 break;
302 }
303 }
304 }
305 }
306
307 context->priv().testingOnly_purgeAllUnlockedResources();
308 }
309
310 // Try creating the texture as a deferred proxy.
311 {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400312 std::unique_ptr<GrSurfaceContext> surfCtx;
Robert Phillipsffe27292019-08-01 10:08:07 -0400313 if (renderable == GrRenderable::kYes) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500314 surfCtx = GrRenderTargetContext::Make(
315 context, combo.fColorType, nullptr, fit,
316 {desc.fWidth, desc.fHeight}, 1, GrMipMapped::kNo,
317 GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Robert Phillipsffe27292019-08-01 10:08:07 -0400318 } else {
Greg Danielbfa19c42019-12-19 16:41:40 -0500319 surfCtx = GrSurfaceContext::Make(
320 context, {desc.fWidth, desc.fHeight}, combo.fFormat,
321 GrRenderable::kNo, 1, GrMipMapped::kNo, GrProtected::kNo,
322 kTopLeft_GrSurfaceOrigin, combo.fColorType,
323 kUnknown_SkAlphaType, nullptr, fit, SkBudgeted::kYes);
Robert Phillipsffe27292019-08-01 10:08:07 -0400324 }
325 if (!surfCtx) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400326 continue;
327 }
Robert Phillipsffe27292019-08-01 10:08:07 -0400328
Robert Phillipsd3442842019-08-02 12:26:22 -0400329 readback.erase(kClearColor);
330 if (surfCtx->readPixels(readback.info(), readback.writable_addr(),
331 readback.rowBytes(), {0, 0})) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400332 for (int i = 0; i < kSize * kSize; ++i) {
Brian Salomon614cdab2019-09-26 11:04:28 -0400333 if (!checkColor(combo, readback.addr32()[i])) {
Greg Daniel90f28ec2017-09-25 12:26:58 -0400334 break;
Brian Salomond17b4a62017-05-23 16:53:47 -0400335 }
336 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400337 }
Brian Salomona3e29962019-07-16 11:52:08 -0400338 context->priv().testingOnly_purgeAllUnlockedResources();
Greg Daniel90f28ec2017-09-25 12:26:58 -0400339 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400340 }
341 }
342 }
343 }
344}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500345
346DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400347 auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500348 for (int y = 0; y < p->height(); ++y) {
349 for (int x = 0; x < p->width(); ++x) {
350 *p->writable_addr32(x, y) = f(x, y);
351 }
352 }
353 };
354
355 auto comparePixels = [](const SkPixmap& p1, const SkPixmap& p2, skiatest::Reporter* reporter) {
356 SkASSERT(p1.info() == p2.info());
357 for (int y = 0; y < p1.height(); ++y) {
358 for (int x = 0; x < p1.width(); ++x) {
359 REPORTER_ASSERT(reporter, p1.getColor(x, y) == p2.getColor(x, y));
360 if (p1.getColor(x, y) != p2.getColor(x, y)) {
361 return;
362 }
363 }
364 }
365 };
366
367 static constexpr int kSize = 100;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400368 SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
369 SkAutoPixmapStorage srcPixmap;
370 srcPixmap.alloc(ii);
371 fillPixels(&srcPixmap,
Robert Phillipscb1adb42019-06-10 15:09:34 -0400372 [](int x, int y) {
373 return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
374 });
Brian Salomonc67c31c2018-12-06 10:00:03 -0500375
376 GrContext* context = context_info.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500377 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500378
379 // We test both kRW in addition to kRead mostly to ensure that the calls are structured such
380 // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
381 // kRead for the right reason.
382 for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
Robert Phillips66944402019-09-30 13:21:25 -0400383 auto backendTex = context->createBackendTexture(&srcPixmap, 1,
384 GrRenderable::kYes, GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400385
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400386 auto proxy = proxyProvider->wrapBackendTexture(backendTex, GrColorType::kRGBA_8888,
387 kTopLeft_GrSurfaceOrigin,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500388 kBorrow_GrWrapOwnership,
389 GrWrapCacheable::kNo, ioType);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500390 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
391 GrColorType::kRGBA_8888);
392 GrSurfaceProxyView view(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
393 auto surfContext = GrSurfaceContext::Make(context, std::move(view), GrColorType::kRGBA_8888,
Greg Danielbfa19c42019-12-19 16:41:40 -0500394 kPremul_SkAlphaType, nullptr);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500395
396 // Read pixels should work with a read-only texture.
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400397 {
398 SkAutoPixmapStorage read;
399 read.alloc(srcPixmap.info());
400 auto readResult = surfContext->readPixels(srcPixmap.info(), read.writable_addr(),
401 0, { 0, 0 });
402 REPORTER_ASSERT(reporter, readResult);
403 if (readResult) {
404 comparePixels(srcPixmap, read, reporter);
405 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500406 }
407
408 // Write pixels should not work with a read-only texture.
409 SkAutoPixmapStorage write;
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400410 write.alloc(srcPixmap.info());
411 fillPixels(&write, [&srcPixmap](int x, int y) { return ~*srcPixmap.addr32(); });
412 auto writeResult = surfContext->writePixels(srcPixmap.info(), write.addr(), 0, {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500413 REPORTER_ASSERT(reporter, writeResult == (ioType == kRW_GrIOType));
414 // Try the low level write.
415 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500416 auto gpuWriteResult = context->priv().getGpu()->writePixels(
Brian Salomonf77c1462019-08-01 15:19:29 -0400417 proxy->peekTexture(), 0, 0, kSize, kSize, GrColorType::kRGBA_8888,
418 GrColorType::kRGBA_8888, write.addr32(),
Brian Salomon1047a492019-07-02 12:25:21 -0400419 kSize * GrColorTypeBytesPerPixel(GrColorType::kRGBA_8888));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500420 REPORTER_ASSERT(reporter, gpuWriteResult == (ioType == kRW_GrIOType));
421
422 // Copies should not work with a read-only texture
Brian Salomon96b383a2019-08-13 16:55:41 -0400423 auto copySrc =
424 proxyProvider->createTextureProxy(SkImage::MakeFromRaster(write, nullptr, nullptr),
425 1, SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500426 REPORTER_ASSERT(reporter, copySrc);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400427 auto copyResult = surfContext->testCopy(copySrc.get());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500428 REPORTER_ASSERT(reporter, copyResult == (ioType == kRW_GrIOType));
429 // Try the low level copy.
430 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500431 auto gpuCopyResult = context->priv().getGpu()->copySurface(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400432 proxy->peekTexture(), copySrc->peekTexture(), SkIRect::MakeWH(kSize, kSize),
433 {0, 0});
Brian Salomonc67c31c2018-12-06 10:00:03 -0500434 REPORTER_ASSERT(reporter, gpuCopyResult == (ioType == kRW_GrIOType));
435
436 // Mip regen should not work with a read only texture.
Robert Phillips9da87e02019-02-04 13:26:26 -0500437 if (context->priv().caps()->mipMapSupport()) {
Brian Salomon28a8f282019-10-24 20:07:39 -0400438 DeleteBackendTexture(context, backendTex);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400439 backendTex = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400440 kSize, kSize, kRGBA_8888_SkColorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400441 SkColors::kTransparent, GrMipMapped::kYes, GrRenderable::kYes,
442 GrProtected::kNo);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400443 proxy = proxyProvider->wrapBackendTexture(backendTex, GrColorType::kRGBA_8888,
444 kTopLeft_GrSurfaceOrigin,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500445 kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
446 ioType);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500447 context->flush();
448 proxy->peekTexture()->texturePriv().markMipMapsDirty(); // avoids assert in GrGpu.
449 auto regenResult =
Robert Phillips9da87e02019-02-04 13:26:26 -0500450 context->priv().getGpu()->regenerateMipMapLevels(proxy->peekTexture());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500451 REPORTER_ASSERT(reporter, regenResult == (ioType == kRW_GrIOType));
452 }
Brian Salomon28a8f282019-10-24 20:07:39 -0400453 DeleteBackendTexture(context, backendTex);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500454 }
455}
Brian Salomon614c1a82018-12-19 15:42:06 -0500456
Greg Daniel46cfbc62019-06-07 11:43:30 -0400457static const int kSurfSize = 10;
458
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400459static sk_sp<GrTexture> make_wrapped_texture(GrContext* context, GrRenderable renderable) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400460 auto backendTexture = context->createBackendTexture(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400461 kSurfSize, kSurfSize, kRGBA_8888_SkColorType, SkColors::kTransparent, GrMipMapped::kNo,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400462 renderable, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500463 sk_sp<GrTexture> texture;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400464 if (GrRenderable::kYes == renderable) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500465 texture = context->priv().resourceProvider()->wrapRenderableBackendTexture(
Robert Phillips0902c982019-07-16 07:47:56 -0400466 backendTexture, 1, GrColorType::kRGBA_8888, kBorrow_GrWrapOwnership,
467 GrWrapCacheable::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500468 } else {
469 texture = context->priv().resourceProvider()->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400470 backendTexture, GrColorType::kRGBA_8888, kBorrow_GrWrapOwnership,
471 GrWrapCacheable::kNo, kRW_GrIOType);
Brian Salomon8cabb322019-02-22 10:44:19 -0500472 }
473 // Add a release proc that deletes the GrBackendTexture.
474 struct ReleaseContext {
475 GrContext* fContext;
476 GrBackendTexture fBackendTexture;
Brian Salomon614c1a82018-12-19 15:42:06 -0500477 };
Brian Salomon8cabb322019-02-22 10:44:19 -0500478 auto release = [](void* rc) {
479 auto releaseContext = static_cast<ReleaseContext*>(rc);
Robert Phillips9b16f812019-05-17 10:01:21 -0400480 auto context = releaseContext->fContext;
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400481 context->deleteBackendTexture(releaseContext->fBackendTexture);
Brian Salomon8cabb322019-02-22 10:44:19 -0500482 delete releaseContext;
483 };
Brian Salomon2ca31f82019-03-05 13:28:58 -0500484 texture->setRelease(release, new ReleaseContext{context, backendTexture});
Brian Salomon8cabb322019-02-22 10:44:19 -0500485 return texture;
486}
Brian Salomon614c1a82018-12-19 15:42:06 -0500487
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400488static sk_sp<GrTexture> make_normal_texture(GrContext* context, GrRenderable renderable) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500489 GrSurfaceDesc desc;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400490 desc.fWidth = desc.fHeight = kSurfSize;
Brian Salomon4eb38b72019-08-05 12:58:39 -0400491 auto format =
492 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, renderable);
Robert Phillips9313aa72019-04-09 18:41:27 -0400493 return context->priv().resourceProvider()->createTexture(
Brian Salomona90382f2019-09-17 09:01:56 -0400494 desc, format, renderable, 1, GrMipMapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon8cabb322019-02-22 10:44:19 -0500495}
Brian Salomon614c1a82018-12-19 15:42:06 -0500496
Brian Salomon8cabb322019-02-22 10:44:19 -0500497DEF_GPUTEST(TextureIdleProcTest, reporter, options) {
498 // Various ways of making textures.
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400499 auto makeWrapped = [](GrContext* context) {
500 return make_wrapped_texture(context, GrRenderable::kNo);
Brian Salomon614c1a82018-12-19 15:42:06 -0500501 };
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400502 auto makeWrappedRenderable = [](GrContext* context) {
503 return make_wrapped_texture(context, GrRenderable::kYes);
504 };
505 auto makeNormal = [](GrContext* context) {
506 return make_normal_texture(context, GrRenderable::kNo);
507 };
508 auto makeRenderable = [](GrContext* context) {
509 return make_normal_texture(context, GrRenderable::kYes);
510 };
Brian Salomon614c1a82018-12-19 15:42:06 -0500511
512 std::function<sk_sp<GrTexture>(GrContext*)> makers[] = {makeWrapped, makeWrappedRenderable,
513 makeNormal, makeRenderable};
514
515 // Add a unique key, or not.
516 auto addKey = [](GrTexture* texture) {
517 static uint32_t gN = 0;
518 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
519 GrUniqueKey key;
520 GrUniqueKey::Builder builder(&key, kDomain, 1);
521 builder[0] = gN++;
522 builder.finish();
523 texture->resourcePriv().setUniqueKey(key);
524 };
525 auto dontAddKey = [](GrTexture* texture) {};
526 std::function<void(GrTexture*)> keyAdders[] = {addKey, dontAddKey};
527
528 for (const auto& m : makers) {
529 for (const auto& keyAdder : keyAdders) {
530 for (int type = 0; type < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++type) {
531 sk_gpu_test::GrContextFactory factory;
532 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(type);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500533 GrContext* context = factory.get(contextType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500534 if (!context) {
535 continue;
536 }
537
538 // The callback we add simply adds an integer to a set.
539 std::set<int> idleIDs;
540 struct Context {
541 std::set<int>* fIdleIDs;
542 int fNum;
543 };
544 auto proc = [](void* context) {
545 static_cast<Context*>(context)->fIdleIDs->insert(
546 static_cast<Context*>(context)->fNum);
547 delete static_cast<Context*>(context);
548 };
549
550 // Makes a texture, possibly adds a key, and sets the callback.
551 auto make = [&m, &keyAdder, &proc, &idleIDs](GrContext* context, int num) {
552 sk_sp<GrTexture> texture = m(context);
Brian Salomone80b8092019-03-08 13:25:19 -0500553 texture->addIdleProc(proc, new Context{&idleIDs, num},
554 GrTexture::IdleState::kFinished);
Brian Salomon614c1a82018-12-19 15:42:06 -0500555 keyAdder(texture.get());
556 return texture;
557 };
558
559 auto texture = make(context, 1);
560 REPORTER_ASSERT(reporter, idleIDs.find(1) == idleIDs.end());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400561 auto renderable = GrRenderable(SkToBool(texture->asRenderTarget()));
Brian Salomon614c1a82018-12-19 15:42:06 -0500562 auto backendFormat = texture->backendFormat();
563 texture.reset();
564 REPORTER_ASSERT(reporter, idleIDs.find(1) != idleIDs.end());
565
566 texture = make(context, 2);
Brian Salomon8cabb322019-02-22 10:44:19 -0500567 int w = texture->width();
568 int h = texture->height();
Brian Salomon614c1a82018-12-19 15:42:06 -0500569 SkImageInfo info =
Brian Salomon8cabb322019-02-22 10:44:19 -0500570 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon614c1a82018-12-19 15:42:06 -0500571 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
572 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
573 auto singleUseLazyCB = [&texture](GrResourceProvider* rp) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400574 auto mode = GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
575 if (texture->getUniqueKey().isValid()) {
576 mode = GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
577 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400578 return GrSurfaceProxy::LazyCallbackResult{std::move(texture), true, mode};
Brian Salomon614c1a82018-12-19 15:42:06 -0500579 };
580 GrSurfaceDesc desc;
Brian Salomon8cabb322019-02-22 10:44:19 -0500581 desc.fWidth = w;
582 desc.fHeight = h;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500583 SkBudgeted budgeted;
584 if (texture->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
585 budgeted = SkBudgeted::kYes;
586 } else {
587 budgeted = SkBudgeted::kNo;
588 }
Greg Danielce3ddaa2020-01-22 16:58:15 -0500589 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(
590 backendFormat, GrColorType::kRGBA_8888);
Robert Phillips9da87e02019-02-04 13:26:26 -0500591 auto proxy = context->priv().proxyProvider()->createLazyProxy(
Greg Danielce3ddaa2020-01-22 16:58:15 -0500592 singleUseLazyCB, backendFormat, desc, readSwizzle, renderable, 1,
Brian Salomon614c1a82018-12-19 15:42:06 -0500593 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600594 GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags ::kNone,
595 SkBackingFit::kExact, budgeted, GrProtected::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400596 GrSurfaceProxy::UseAllocator::kYes);
Brian Salomonfc118442019-11-22 19:09:27 -0500597 rtc->drawTexture(GrNoClip(), proxy, GrColorType::kRGBA_8888, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400598 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
599 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
600 GrAA::kNo, GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
601 SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500602 // We still have the proxy, which should remain instantiated, thereby keeping the
603 // texture not purgeable.
604 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
605 context->flush();
606 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
Robert Phillips9da87e02019-02-04 13:26:26 -0500607 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500608 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
609
610 // This time we move the proxy into the draw.
Greg Danielc594e622019-10-15 14:01:49 -0400611 rtc->drawTexture(GrNoClip(), std::move(proxy), GrColorType::kRGBA_8888,
Brian Salomonfc118442019-11-22 19:09:27 -0500612 kPremul_SkAlphaType, GrSamplerState::Filter::kNearest,
613 SkBlendMode::kSrcOver, SkPMColor4f(), SkRect::MakeWH(w, h),
614 SkRect::MakeWH(w, h), GrAA::kNo, GrQuadAAFlags::kNone,
615 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon614c1a82018-12-19 15:42:06 -0500616 REPORTER_ASSERT(reporter, idleIDs.find(2) == idleIDs.end());
617 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500618 context->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Salomon614c1a82018-12-19 15:42:06 -0500619 // Now that the draw is fully consumed by the GPU, the texture should be idle.
620 REPORTER_ASSERT(reporter, idleIDs.find(2) != idleIDs.end());
621
Brian Salomon9bc76d92019-01-24 12:18:33 -0500622 // Make sure we make the call during various shutdown scenarios where the texture
623 // might persist after context is destroyed, abandoned, etc. We test three
624 // variations of each scenario. One where the texture is just created. Another,
625 // where the texture has been used in a draw and then the context is flushed. And
626 // one where the the texture was drawn but the context is not flushed.
627 // In each scenario we test holding a ref beyond the context shutdown and not.
Brian Salomon614c1a82018-12-19 15:42:06 -0500628
Brian Salomon9bc76d92019-01-24 12:18:33 -0500629 // These tests are difficult to get working with Vulkan. See http://skbug.com/8705
630 // and http://skbug.com/8275
631 GrBackendApi api = sk_gpu_test::GrContextFactory::ContextTypeBackend(contextType);
632 if (api == GrBackendApi::kVulkan) {
633 continue;
634 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400635 int id = 3;
Brian Salomon9bc76d92019-01-24 12:18:33 -0500636 enum class DrawType {
637 kNoDraw,
638 kDraw,
639 kDrawAndFlush,
640 };
641 for (auto drawType :
642 {DrawType::kNoDraw, DrawType::kDraw, DrawType::kDrawAndFlush}) {
643 for (bool unrefFirst : {false, true}) {
Brian Salomon8cabb322019-02-22 10:44:19 -0500644 auto possiblyDrawAndFlush = [&context, &texture, drawType, unrefFirst, w,
645 h] {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500646 if (drawType == DrawType::kNoDraw) {
647 return;
648 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500649 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType,
Brian Salomon9bc76d92019-01-24 12:18:33 -0500650 kPremul_SkAlphaType);
651 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
652 nullptr);
Robert Phillips9da87e02019-02-04 13:26:26 -0500653 auto rtc = rt->getCanvas()
Brian Salomon9bc76d92019-01-24 12:18:33 -0500654 ->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500655 auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomon2af3e702019-08-11 19:10:31 -0400656 texture, GrColorType::kRGBA_8888, kTopLeft_GrSurfaceOrigin);
Brian Salomon8cabb322019-02-22 10:44:19 -0500657 rtc->drawTexture(
Brian Salomonfc118442019-11-22 19:09:27 -0500658 GrNoClip(), proxy, GrColorType::kRGBA_8888, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400659 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
660 SkPMColor4f(), SkRect::MakeWH(w, h), SkRect::MakeWH(w, h),
661 GrAA::kNo, GrQuadAAFlags::kNone,
Brian Salomon8cabb322019-02-22 10:44:19 -0500662 SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), nullptr);
Brian Salomon9bc76d92019-01-24 12:18:33 -0500663 if (drawType == DrawType::kDrawAndFlush) {
664 context->flush();
665 }
666 if (unrefFirst) {
667 texture.reset();
668 }
669 };
670 texture = make(context, id);
671 possiblyDrawAndFlush();
672 context->abandonContext();
673 texture.reset();
674 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
675 factory.destroyContexts();
676 context = factory.get(contextType);
677 ++id;
Brian Salomon614c1a82018-12-19 15:42:06 -0500678
Brian Salomon9bc76d92019-01-24 12:18:33 -0500679 // Similar to previous, but reset the texture after the context was
680 // abandoned and then destroyed.
681 texture = make(context, id);
682 possiblyDrawAndFlush();
683 context->abandonContext();
684 factory.destroyContexts();
685 texture.reset();
686 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
687 context = factory.get(contextType);
688 id++;
689
690 texture = make(context, id);
691 possiblyDrawAndFlush();
692 factory.destroyContexts();
693 texture.reset();
694 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
695 context = factory.get(contextType);
696 id++;
697
698 texture = make(context, id);
699 possiblyDrawAndFlush();
700 factory.releaseResourcesAndAbandonContexts();
701 texture.reset();
702 REPORTER_ASSERT(reporter, idleIDs.find(id) != idleIDs.end());
703 context = factory.get(contextType);
704 id++;
705 }
706 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500707 }
708 }
709 }
710}
Brian Salomon8cabb322019-02-22 10:44:19 -0500711
712// Tests an idle proc that unrefs another resource down to zero.
713DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcCacheManipulationTest, reporter, contextInfo) {
714 GrContext* context = contextInfo.grContext();
715
716 // idle proc that releases another texture.
717 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->unref(); };
718
719 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
720 for (const auto& otherMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500721 for (auto idleState :
722 {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400723 auto idleTexture = idleMaker(context, GrRenderable::kNo);
724 auto otherTexture = otherMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500725 otherTexture->ref();
726 idleTexture->addIdleProc(idleProc, otherTexture.get(), idleState);
727 otherTexture.reset();
728 idleTexture.reset();
729 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500730 }
731 }
732}
733
734// Similar to above but more complicated. This flushes the context from the idle proc.
735// crbug.com/933526.
736DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcFlushTest, reporter, contextInfo) {
737 GrContext* context = contextInfo.grContext();
738
739 // idle proc that flushes the context.
740 auto idleProc = [](void* context) { reinterpret_cast<GrContext*>(context)->flush(); };
741
742 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Brian Salomone80b8092019-03-08 13:25:19 -0500743 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400744 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500745 idleTexture->addIdleProc(idleProc, context, idleState);
746 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
747 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 1, nullptr);
748 // We'll draw two images to the canvas. One is a normal texture-backed image. The other
749 // is a wrapped-texture backed image.
750 surf->getCanvas()->clear(SK_ColorWHITE);
751 auto img1 = surf->makeImageSnapshot();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400752
753 GrBackendTexture backendTexture;
754
Brian Salomon28a8f282019-10-24 20:07:39 -0400755 if (!CreateBackendTexture(context, &backendTexture, info, SkColors::kBlack,
756 GrMipMapped::kNo, GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400757 REPORTER_ASSERT(reporter, false);
758 continue;
759 }
760
Brian Salomone80b8092019-03-08 13:25:19 -0500761 auto img2 = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
762 info.colorType(), info.alphaType(), nullptr);
763 surf->getCanvas()->drawImage(std::move(img1), 0, 0);
764 surf->getCanvas()->drawImage(std::move(img2), 1, 1);
765 idleTexture.reset();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400766
Brian Salomon28a8f282019-10-24 20:07:39 -0400767 DeleteBackendTexture(context, backendTexture);
Brian Salomone80b8092019-03-08 13:25:19 -0500768 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500769 }
770}
771
772DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleProcRerefTest, reporter, contextInfo) {
773 GrContext* context = contextInfo.grContext();
774 // idle proc that refs the texture
775 auto idleProc = [](void* texture) { reinterpret_cast<GrTexture*>(texture)->ref(); };
776 // release proc to check whether the texture was released or not.
777 auto releaseProc = [](void* isReleased) { *reinterpret_cast<bool*>(isReleased) = true; };
Brian Salomone80b8092019-03-08 13:25:19 -0500778 for (auto idleState : {GrTexture::IdleState::kFlushed, GrTexture::IdleState::kFinished}) {
779 bool isReleased = false;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400780 auto idleTexture = make_normal_texture(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500781 // This test assumes the texture won't be cached (or else the release proc doesn't get
782 // called).
783 idleTexture->resourcePriv().removeScratchKey();
784 context->flush();
785 idleTexture->addIdleProc(idleProc, idleTexture.get(), idleState);
786 idleTexture->setRelease(releaseProc, &isReleased);
787 auto* raw = idleTexture.get();
788 idleTexture.reset();
789 REPORTER_ASSERT(reporter, !isReleased);
790 raw->unref();
791 REPORTER_ASSERT(reporter, isReleased);
792 }
793}
794
795DEF_GPUTEST_FOR_ALL_CONTEXTS(TextureIdleStateTest, reporter, contextInfo) {
796 GrContext* context = contextInfo.grContext();
797 for (const auto& idleMaker : {make_wrapped_texture, make_normal_texture}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400798 auto idleTexture = idleMaker(context, GrRenderable::kNo);
Brian Salomone80b8092019-03-08 13:25:19 -0500799
800 uint32_t flags = 0;
801 static constexpr uint32_t kFlushFlag = 0x1;
802 static constexpr uint32_t kFinishFlag = 0x2;
803 auto flushProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFlushFlag; };
804 auto finishProc = [](void* flags) { *static_cast<uint32_t*>(flags) |= kFinishFlag; };
805 idleTexture->addIdleProc(flushProc, &flags, GrTexture::IdleState::kFlushed);
806 idleTexture->addIdleProc(finishProc, &flags, GrTexture::IdleState::kFinished);
807
808 // Insert a copy from idleTexture to another texture so that we have some queued IO on
809 // idleTexture.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400810 SkImageInfo info = SkImageInfo::Make(kSurfSize, kSurfSize, kRGBA_8888_SkColorType,
811 kPremul_SkAlphaType);
Brian Salomone80b8092019-03-08 13:25:19 -0500812 auto rt = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, nullptr);
813 auto rtc = rt->getCanvas()->internal_private_accessTopLayerRenderTargetContext();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400814 auto proxy = context->priv().proxyProvider()->testingOnly_createWrapped(
Brian Salomon2af3e702019-08-11 19:10:31 -0400815 std::move(idleTexture), GrColorType::kRGBA_8888, rtc->asSurfaceProxy()->origin());
Brian Salomone80b8092019-03-08 13:25:19 -0500816 context->flush();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400817 SkAssertResult(rtc->testCopy(proxy.get()));
Brian Salomone80b8092019-03-08 13:25:19 -0500818 proxy.reset();
819 REPORTER_ASSERT(reporter, flags == 0);
820
821 // After a flush we expect idleTexture to have reached the kFlushed state on all backends.
822 // On "managed" backends we expect it to reach kFinished as well. On Vulkan, the only
823 // current "unmanaged" backend, we *may* need a sync to reach kFinished.
824 context->flush();
825 if (contextInfo.backend() == kVulkan_GrBackend) {
826 REPORTER_ASSERT(reporter, flags & kFlushFlag);
827 } else {
828 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
829 }
830 context->priv().getGpu()->testingOnly_flushGpuAndSync();
831 REPORTER_ASSERT(reporter, flags == (kFlushFlag | kFinishFlag));
832 }
Brian Salomon8cabb322019-02-22 10:44:19 -0500833}