blob: a8df361c676ac581bdc7d73d550f3b969b7615cf [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"
14#include "src/gpu/GrPendingIOResource.h"
15#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSurfaceProxy.h"
19#include "src/gpu/GrTextureProxy.h"
robertphillips1125a032016-11-16 11:17:17 -080020
Robert Phillips7928e762017-02-28 16:30:28 -050021static const int kWidthHeight = 128;
22
robertphillips1125a032016-11-16 11:17:17 -080023static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040024 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080025 int32_t expectedProxyRefs,
Robert Phillips3d4cac52019-06-11 08:08:08 -040026 int32_t expectedBackingRefs) {
27 int32_t actualProxyRefs = proxy->priv().getProxyRefCnt();
Robert Phillipsb5204762019-06-19 14:12:13 -040028 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
robertphillips1125a032016-11-16 11:17:17 -080029
Robert Phillips3d4cac52019-06-11 08:08:08 -040030 SkASSERT(actualProxyRefs == expectedProxyRefs);
31 SkASSERT(actualBackingRefs == expectedBackingRefs);
32
33 REPORTER_ASSERT(reporter, actualProxyRefs == expectedProxyRefs);
34 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
robertphillips1125a032016-11-16 11:17:17 -080035}
36
Greg Daniel4065d452018-11-16 15:43:41 -050037static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080038 GrSurfaceDesc desc;
39 desc.fFlags = kRenderTarget_GrSurfaceFlag;
40 desc.fWidth = kWidthHeight;
41 desc.fHeight = kWidthHeight;
42 desc.fConfig = kRGBA_8888_GrPixelConfig;
43
Greg Daniel4065d452018-11-16 15:43:41 -050044 const GrBackendFormat format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
45 return proxyProvider->createProxy(format, desc, kBottomLeft_GrSurfaceOrigin,
Robert Phillips10d17212019-04-24 14:09:10 -040046 SkBackingFit::kApprox, SkBudgeted::kYes);
robertphillips1125a032016-11-16 11:17:17 -080047}
48
Greg Daniel4065d452018-11-16 15:43:41 -050049static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080050 GrSurfaceDesc desc;
51 desc.fFlags = kRenderTarget_GrSurfaceFlag;
52 desc.fWidth = kWidthHeight;
53 desc.fHeight = kWidthHeight;
54 desc.fConfig = kRGBA_8888_GrPixelConfig;
55
Chris Daltond004e0b2018-09-27 09:28:03 -060056 return proxyProvider->testingOnly_createInstantiatedProxy(
57 desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::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 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
62 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
63 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
robertphillips1125a032016-11-16 11:17:17 -080064
65 for (auto make : { make_deferred, make_wrapped }) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040066 // Pending IO ref
robertphillips1125a032016-11-16 11:17:17 -080067 {
Greg Daniel4065d452018-11-16 15:43:41 -050068 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040069 if (proxy.get()) {
Robert Phillips3d4cac52019-06-11 08:08:08 -040070 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -080071
Robert Phillipsb5204762019-06-19 14:12:13 -040072 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -080073
Robert Phillips3d4cac52019-06-11 08:08:08 -040074 check_refs(reporter, proxy.get(), 2, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080075
Robert Phillips0bd24dc2018-01-16 08:06:32 -050076 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080077
Robert Phillipse5f73282019-06-18 17:15:04 -040078 check_refs(reporter, proxy.get(), 2, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -040079 }
Robert Phillips3d4cac52019-06-11 08:08:08 -040080 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -080081 }
82
83 // Multiple normal refs
84 {
Greg Daniel4065d452018-11-16 15:43:41 -050085 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040086 if (proxy.get()) {
87 proxy->ref();
88 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -080089
Robert Phillipsb5204762019-06-19 14:12:13 -040090 int backingRefs = proxy->isInstantiated() ? 1 : -1;
Robert Phillips715d08c2018-07-18 13:56:48 -040091
Robert Phillips3d4cac52019-06-11 08:08:08 -040092 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -080093
Robert Phillips0bd24dc2018-01-16 08:06:32 -050094 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -080095
Robert Phillipse5f73282019-06-18 17:15:04 -040096 check_refs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -080097
Jim Van Verth311cc6b2017-09-20 17:51:59 -040098 proxy->unref();
99 proxy->unref();
100 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400101 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800102 }
103
104 // Continue using (reffing) proxy after instantiation
105 {
Greg Daniel4065d452018-11-16 15:43:41 -0500106 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400107 if (proxy.get()) {
108 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800109
Robert Phillips3d4cac52019-06-11 08:08:08 -0400110 GrProxyPendingIO pendingIO(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800111
Robert Phillipsb5204762019-06-19 14:12:13 -0400112 int backingRefs = proxy->isInstantiated() ? 1 : -1;
robertphillips1125a032016-11-16 11:17:17 -0800113
Robert Phillips3d4cac52019-06-11 08:08:08 -0400114 check_refs(reporter, proxy.get(), 3, backingRefs);
robertphillips1125a032016-11-16 11:17:17 -0800115
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500116 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800117
Robert Phillipse5f73282019-06-18 17:15:04 -0400118 check_refs(reporter, proxy.get(), 3, 1);
robertphillips1125a032016-11-16 11:17:17 -0800119
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400120 proxy->unref();
Robert Phillipse5f73282019-06-18 17:15:04 -0400121 check_refs(reporter, proxy.get(), 2, 1);
robertphillips1125a032016-11-16 11:17:17 -0800122
Robert Phillips3d4cac52019-06-11 08:08:08 -0400123 GrProxyPendingIO secondPendingIO(proxy.get());
Robert Phillipse5f73282019-06-18 17:15:04 -0400124 check_refs(reporter, proxy.get(), 3, 1);
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400125 }
Robert Phillips3d4cac52019-06-11 08:08:08 -0400126 check_refs(reporter, proxy.get(), 1, 1);
robertphillips1125a032016-11-16 11:17:17 -0800127 }
128 }
129}