blob: 6a7dfbdcf0ca5e8dcdbc567bbc0cce6f78758efd [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
10#include "Test.h"
11
Robert Phillips7ee385e2017-03-30 08:02:11 -040012#include "GrContextPriv.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040013#include "GrGpuResourceRef.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050014#include "GrProxyProvider.h"
robertphillips1125a032016-11-16 11:17:17 -080015#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050016#include "GrResourceProvider.h"
17#include "GrSurfaceProxy.h"
Robert Phillips646e4292017-06-13 12:44:56 -040018#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050019#include "GrTextureProxy.h"
robertphillips1125a032016-11-16 11:17:17 -080020
robertphillips1125a032016-11-16 11:17:17 -080021int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const {
22 return fRefCnt;
23}
24
25int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
26 if (fTarget) {
27 return fTarget->fRefCnt;
28 }
29
30 return fRefCnt;
31}
32
33int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const {
34 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080035 return fTarget->fPendingReads;
36 }
37
38 return fPendingReads;
39}
40
41int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
42 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080043 return fTarget->fPendingWrites;
44 }
45
46 return fPendingWrites;
47}
48
Robert Phillips7928e762017-02-28 16:30:28 -050049static const int kWidthHeight = 128;
50
robertphillips1125a032016-11-16 11:17:17 -080051static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040052 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080053 int32_t expectedProxyRefs,
54 int32_t expectedBackingRefs,
55 int32_t expectedNumReads,
56 int32_t expectedNumWrites) {
57 REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
58 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
59 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
60 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
61
62 SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
63 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
64 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
65 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
66}
67
Robert Phillips0bd24dc2018-01-16 08:06:32 -050068static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider) {
robertphillips1125a032016-11-16 11:17:17 -080069 GrSurfaceDesc desc;
70 desc.fFlags = kRenderTarget_GrSurfaceFlag;
71 desc.fWidth = kWidthHeight;
72 desc.fHeight = kWidthHeight;
73 desc.fConfig = kRGBA_8888_GrPixelConfig;
74
Brian Salomon2a4f9832018-03-03 22:43:43 -050075 return proxyProvider->createProxy(desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040076 SkBudgeted::kYes, GrInternalSurfaceFlags::kNoPendingIO);
robertphillips1125a032016-11-16 11:17:17 -080077}
78
Robert Phillips0bd24dc2018-01-16 08:06:32 -050079static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider) {
robertphillips1125a032016-11-16 11:17:17 -080080 GrSurfaceDesc desc;
81 desc.fFlags = kRenderTarget_GrSurfaceFlag;
82 desc.fWidth = kWidthHeight;
83 desc.fHeight = kWidthHeight;
84 desc.fConfig = kRGBA_8888_GrPixelConfig;
85
Brian Salomon2a4f9832018-03-03 22:43:43 -050086 return proxyProvider->createInstantiatedProxy(desc, kBottomLeft_GrSurfaceOrigin,
87 SkBackingFit::kExact, SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080088}
89
90DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050091 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050092 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080093
94 for (auto make : { make_deferred, make_wrapped }) {
95 // A single write
96 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050097 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040098 if (proxy.get()) {
99 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800100
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400101 static const int kExpectedReads = 0;
102 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800103
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400104 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800105
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500106 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800107
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400108 // In the deferred case, this checks that the refs transfered to the GrSurface
109 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
110 }
robertphillips1125a032016-11-16 11:17:17 -0800111 }
112
113 // A single read
114 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500115 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400116 if (proxy.get()) {
117 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800118
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400119 static const int kExpectedReads = 1;
120 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800121
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400122 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800123
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500124 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800125
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400126 // In the deferred case, this checks that the refs transfered to the GrSurface
127 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
128 }
robertphillips1125a032016-11-16 11:17:17 -0800129 }
130
131 // A single read/write pair
132 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500133 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400134 if (proxy.get()) {
135 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800136
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400137 static const int kExpectedReads = 1;
138 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800139
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400140 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800141
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500142 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800143
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400144 // In the deferred case, this checks that the refs transferred to the GrSurface
145 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
146 }
robertphillips1125a032016-11-16 11:17:17 -0800147 }
148
149 // Multiple normal refs
150 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500151 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400152 if (proxy.get()) {
153 proxy->ref();
154 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800155
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400156 static const int kExpectedReads = 0;
157 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800158
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400159 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800160
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500161 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800162
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400163 // In the deferred case, this checks that the refs transferred to the GrSurface
164 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800165
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400166 proxy->unref();
167 proxy->unref();
168 }
robertphillips1125a032016-11-16 11:17:17 -0800169 }
170
171 // Continue using (reffing) proxy after instantiation
172 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500173 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400174 if (proxy.get()) {
175 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800176
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400177 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800178
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400179 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800180
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400181 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800182
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500183 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800184
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400185 // In the deferred case, this checks that the refs transfered to the GrSurface
186 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800187
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400188 proxy->unref();
189 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800190
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400191 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
192 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
193 }
robertphillips1125a032016-11-16 11:17:17 -0800194 }
195 }
196}