blob: da4729e039bc05cbadce87d2101dabe2bcbea8fe [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"
robertphillips1125a032016-11-16 11:17:17 -080019
Robert Phillips7928e762017-02-28 16:30:28 -050020static const int kWidthHeight = 128;
21
robertphillips1125a032016-11-16 11:17:17 -080022static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040023 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080024 int32_t expectedProxyRefs,
Robert Phillips3d4cac52019-06-11 08:08:08 -040025 int32_t expectedBackingRefs) {
Brian Salomona036f0d2019-08-29 11:16:04 -040026 int32_t actualProxyRefs = proxy->refCnt();
Robert Phillipsb5204762019-06-19 14:12:13 -040027 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
robertphillips1125a032016-11-16 11:17:17 -080028
Robert Phillips3d4cac52019-06-11 08:08:08 -040029 SkASSERT(actualProxyRefs == expectedProxyRefs);
30 SkASSERT(actualBackingRefs == expectedBackingRefs);
31
32 REPORTER_ASSERT(reporter, actualProxyRefs == expectedProxyRefs);
33 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
robertphillips1125a032016-11-16 11:17:17 -080034}
35
Robert Phillips0a15cc62019-07-30 12:49:10 -040036static sk_sp<GrTextureProxy> make_deferred(GrContext* context) {
37 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
38 const GrCaps* caps = context->priv().caps();
39
robertphillips1125a032016-11-16 11:17:17 -080040 GrSurfaceDesc desc;
robertphillips1125a032016-11-16 11:17:17 -080041 desc.fWidth = kWidthHeight;
42 desc.fHeight = kWidthHeight;
43 desc.fConfig = kRGBA_8888_GrPixelConfig;
44
Robert Phillips0a15cc62019-07-30 12:49:10 -040045 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
46 GrRenderable::kYes);
Brian Salomon27b4d8d2019-07-22 14:23:45 -040047 return proxyProvider->createProxy(format, desc, GrRenderable::kYes, 1,
Brian Salomonbeb7f522019-08-30 16:19:42 -040048 kBottomLeft_GrSurfaceOrigin, GrMipMapped::kNo,
49 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
robertphillips1125a032016-11-16 11:17:17 -080050}
51
Robert Phillips0a15cc62019-07-30 12:49:10 -040052static sk_sp<GrTextureProxy> make_wrapped(GrContext* context) {
53 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
54
Chris Daltond004e0b2018-09-27 09:28:03 -060055 return proxyProvider->testingOnly_createInstantiatedProxy(
Brian Salomon4eb38b72019-08-05 12:58:39 -040056 {kWidthHeight, kWidthHeight}, GrColorType::kRGBA_8888, GrRenderable::kYes, 1,
57 kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo);
robertphillips1125a032016-11-16 11:17:17 -080058}
59
60DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050061 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080062
63 for (auto make : { make_deferred, make_wrapped }) {
Robert Phillips80bff5b2019-08-20 16:56:18 -040064 // An extra ref
robertphillips1125a032016-11-16 11:17:17 -080065 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040066 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Robert Phillips80bff5b2019-08-20 16:56:18 -040067 if (proxy) {
68 sk_sp<GrTextureProxy> extraRef(proxy);
robertphillips1125a032016-11-16 11:17:17 -080069
Robert Phillipsb5204762019-06-19 14:12:13 -040070 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -080071
Robert Phillips3d4cac52019-06-11 08:08:08 -040072 check_refs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080073
Robert Phillips0bd24dc2018-01-16 08:06:32 -050074 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080075
Robert Phillipse5f73282019-06-18 17:15:04 -040076 check_refs(reporter, proxy.get(), 2, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -040077 }
Robert Phillips3d4cac52019-06-11 08:08:08 -040078 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -080079 }
80
81 // Multiple normal refs
82 {
Robert Phillips0a15cc62019-07-30 12:49:10 -040083 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040084 if (proxy.get()) {
85 proxy->ref();
86 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -080087
Robert Phillipsb5204762019-06-19 14:12:13 -040088 int backingRefs = proxy->isInstantiated() ? 1 : -1;
Robert Phillips715d08c2018-07-18 13:56:48 -040089
Robert Phillips3d4cac52019-06-11 08:08:08 -040090 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080091
Robert Phillips0bd24dc2018-01-16 08:06:32 -050092 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080093
Robert Phillipse5f73282019-06-18 17:15:04 -040094 check_refs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -080095
Jim Van Verth311cc6b2017-09-20 17:51:59 -040096 proxy->unref();
97 proxy->unref();
98 }
Robert Phillips3d4cac52019-06-11 08:08:08 -040099 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800100 }
101
102 // Continue using (reffing) proxy after instantiation
103 {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400104 sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext()));
Robert Phillips80bff5b2019-08-20 16:56:18 -0400105 if (proxy) {
106 sk_sp<GrTextureProxy> firstExtraRef(proxy);
robertphillips1125a032016-11-16 11:17:17 -0800107
Robert Phillipsb5204762019-06-19 14:12:13 -0400108 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -0800109
Robert Phillips80bff5b2019-08-20 16:56:18 -0400110 check_refs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -0800111
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500112 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800113
Robert Phillipse5f73282019-06-18 17:15:04 -0400114 check_refs(reporter, proxy.get(), 2, 1);
robertphillips1125a032016-11-16 11:17:17 -0800115
Robert Phillips80bff5b2019-08-20 16:56:18 -0400116 sk_sp<GrTextureProxy> secondExtraRef(proxy);
Robert Phillipse5f73282019-06-18 17:15:04 -0400117 check_refs(reporter, proxy.get(), 3, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400118 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400119 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800120 }
121 }
122}