blob: aa6a8ea4fc6665f2b0d3e73f58cfba6a2cab355f [file] [log] [blame]
robertphillips1125a032016-11-16 11:17:17 -08001/*
2 * Copyright 2016 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
8// This is a GPU-backend specific test.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
robertphillips1125a032016-11-16 11:17:17 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrSurfaceProxy.h"
18#include "src/gpu/GrTextureProxy.h"
Brian Salomon557e8122019-10-24 10:37:08 -040019#include "tests/TestUtils.h"
robertphillips1125a032016-11-16 11:17:17 -080020
Robert Phillips7928e762017-02-28 16:30:28 -050021static const int kWidthHeight = 128;
22
Robert Phillips0a15cc62019-07-30 12:49:10 -040023static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
24 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
25 const GrCaps* caps = context->priv().caps();
26
robertphillips1125a032016-11-16 11:17:17 -080027 GrSurfaceDesc desc;
robertphillips1125a032016-11-16 11:17:17 -080028 desc.fWidth = kWidthHeight;
29 desc.fHeight = kWidthHeight;
robertphillips1125a032016-11-16 11:17:17 -080030
Robert Phillips0a15cc62019-07-30 12:49:10 -040031 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
32 GrRenderable::kYes);
Greg Daniel47c20e82020-01-21 14:29:57 -050033 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kRGBA_8888);
34 return proxyProvider->createProxy(format, desc, swizzle, GrRenderable::kYes, 1,
Brian Salomonbeb7f522019-08-30 16:19:42 -040035 kBottomLeft_GrSurfaceOrigin, GrMipMapped::kNo,
36 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
robertphillips1125a032016-11-16 11:17:17 -080037}
38
Robert Phillips0a15cc62019-07-30 12:49:10 -040039static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
40 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
41
Chris Daltond004e0b2018-09-27 09:28:03 -060042 return proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -040043 {kWidthHeight, kWidthHeight}, GrColorType::kRGBA_8888, GrRenderable::kYes, 1,
44 kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
robertphillips1125a032016-11-16 11:17:17 -080045}
46
47DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050048 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080049
50 for (auto make : { make_deferred, make_wrapped }) {
Robert Phillips80bff5b2019-08-20 16:56:18 -040051 // An extra ref
robertphillips1125a032016-11-16 11:17:17 -080052 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040053 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Robert Phillips80bff5b2019-08-20 16:56:18 -040054 if (proxy) {
55 sk_sp<GrTextureProxy> extraRef(proxy);
robertphillips1125a032016-11-16 11:17:17 -080056
Robert Phillipsb5204762019-06-19 14:12:13 -040057 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -080058
Brian Salomon28a8f282019-10-24 20:07:39 -040059 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080060
Robert Phillips0bd24dc2018-01-16 08:06:32 -050061 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080062
Brian Salomon28a8f282019-10-24 20:07:39 -040063 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -040064 }
Brian Salomon28a8f282019-10-24 20:07:39 -040065 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -080066 }
67
68 // Multiple normal refs
69 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040070 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040071 if (proxy.get()) {
72 proxy->ref();
73 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -080074
Robert Phillipsb5204762019-06-19 14:12:13 -040075 int backingRefs = proxy->isInstantiated() ? 1 : -1;
Robert Phillips715d08c2018-07-18 13:56:48 -040076
Brian Salomon28a8f282019-10-24 20:07:39 -040077 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080078
Robert Phillips0bd24dc2018-01-16 08:06:32 -050079 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080080
Brian Salomon28a8f282019-10-24 20:07:39 -040081 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -080082
Jim Van Verth311cc6b2017-09-20 17:51:59 -040083 proxy->unref();
84 proxy->unref();
85 }
Brian Salomon28a8f282019-10-24 20:07:39 -040086 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -080087 }
88
89 // Continue using (reffing) proxy after instantiation
90 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040091 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Robert Phillips80bff5b2019-08-20 16:56:18 -040092 if (proxy) {
93 sk_sp<GrTextureProxy> firstExtraRef(proxy);
robertphillips1125a032016-11-16 11:17:17 -080094
Robert Phillipsb5204762019-06-19 14:12:13 -040095 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -080096
Brian Salomon28a8f282019-10-24 20:07:39 -040097 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080098
Robert Phillips0bd24dc2018-01-16 08:06:32 -050099 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800100
Brian Salomon28a8f282019-10-24 20:07:39 -0400101 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 2, 1);
robertphillips1125a032016-11-16 11:17:17 -0800102
Robert Phillips80bff5b2019-08-20 16:56:18 -0400103 sk_sp<GrTextureProxy> secondExtraRef(proxy);
Brian Salomon28a8f282019-10-24 20:07:39 -0400104 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 3, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400105 }
Brian Salomon28a8f282019-10-24 20:07:39 -0400106 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800107 }
108 }
109}