blob: e84e6e7585a4fed318dc1299707967f4c320d81b [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
12#if SK_SUPPORT_GPU
Robert Phillips7ee385e2017-03-30 08:02:11 -040013#include "GrContextPriv.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040014#include "GrGpuResourceRef.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrProxyProvider.h"
robertphillips1125a032016-11-16 11:17:17 -080016#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050017#include "GrResourceProvider.h"
18#include "GrSurfaceProxy.h"
Robert Phillips646e4292017-06-13 12:44:56 -040019#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050020#include "GrTextureProxy.h"
robertphillips1125a032016-11-16 11:17:17 -080021
robertphillips1125a032016-11-16 11:17:17 -080022int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const {
23 return fRefCnt;
24}
25
26int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
27 if (fTarget) {
28 return fTarget->fRefCnt;
29 }
30
31 return fRefCnt;
32}
33
34int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const {
35 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080036 return fTarget->fPendingReads;
37 }
38
39 return fPendingReads;
40}
41
42int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
43 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080044 return fTarget->fPendingWrites;
45 }
46
47 return fPendingWrites;
48}
49
Robert Phillips7928e762017-02-28 16:30:28 -050050static const int kWidthHeight = 128;
51
robertphillips1125a032016-11-16 11:17:17 -080052static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040053 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080054 int32_t expectedProxyRefs,
55 int32_t expectedBackingRefs,
56 int32_t expectedNumReads,
57 int32_t expectedNumWrites) {
58 REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
59 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
60 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
61 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
62
63 SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
64 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
65 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
66 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
67}
68
Robert Phillips0bd24dc2018-01-16 08:06:32 -050069static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider) {
robertphillips1125a032016-11-16 11:17:17 -080070 GrSurfaceDesc desc;
71 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -040072 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
robertphillips1125a032016-11-16 11:17:17 -080073 desc.fWidth = kWidthHeight;
74 desc.fHeight = kWidthHeight;
75 desc.fConfig = kRGBA_8888_GrPixelConfig;
76
Robert Phillips0bd24dc2018-01-16 08:06:32 -050077 return proxyProvider->createProxy(desc, SkBackingFit::kApprox, SkBudgeted::kYes,
78 GrResourceProvider::kNoPendingIO_Flag);
robertphillips1125a032016-11-16 11:17:17 -080079}
80
Robert Phillips0bd24dc2018-01-16 08:06:32 -050081static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider) {
robertphillips1125a032016-11-16 11:17:17 -080082 GrSurfaceDesc desc;
83 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipse44ef102017-07-21 15:37:19 -040084 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
robertphillips1125a032016-11-16 11:17:17 -080085 desc.fWidth = kWidthHeight;
86 desc.fHeight = kWidthHeight;
87 desc.fConfig = kRGBA_8888_GrPixelConfig;
88
Robert Phillips0bd24dc2018-01-16 08:06:32 -050089 return proxyProvider->createInstantiatedProxy(desc, SkBackingFit::kExact, SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080090}
91
92DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050093 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050094 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080095
96 for (auto make : { make_deferred, make_wrapped }) {
97 // A single write
98 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050099 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400100 if (proxy.get()) {
101 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800102
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400103 static const int kExpectedReads = 0;
104 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800105
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400106 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800107
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500108 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800109
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400110 // In the deferred case, this checks that the refs transfered to the GrSurface
111 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
112 }
robertphillips1125a032016-11-16 11:17:17 -0800113 }
114
115 // A single read
116 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500117 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400118 if (proxy.get()) {
119 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800120
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400121 static const int kExpectedReads = 1;
122 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800123
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400124 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800125
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500126 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800127
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400128 // In the deferred case, this checks that the refs transfered to the GrSurface
129 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
130 }
robertphillips1125a032016-11-16 11:17:17 -0800131 }
132
133 // A single read/write pair
134 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500135 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400136 if (proxy.get()) {
137 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800138
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400139 static const int kExpectedReads = 1;
140 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800141
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400142 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800143
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500144 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800145
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400146 // In the deferred case, this checks that the refs transferred to the GrSurface
147 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
148 }
robertphillips1125a032016-11-16 11:17:17 -0800149 }
150
151 // Multiple normal refs
152 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500153 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400154 if (proxy.get()) {
155 proxy->ref();
156 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800157
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400158 static const int kExpectedReads = 0;
159 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800160
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400161 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800162
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500163 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800164
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400165 // In the deferred case, this checks that the refs transferred to the GrSurface
166 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800167
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400168 proxy->unref();
169 proxy->unref();
170 }
robertphillips1125a032016-11-16 11:17:17 -0800171 }
172
173 // Continue using (reffing) proxy after instantiation
174 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500175 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400176 if (proxy.get()) {
177 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800178
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400179 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800180
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400181 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800182
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400183 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800184
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500185 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800186
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400187 // In the deferred case, this checks that the refs transfered to the GrSurface
188 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800189
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400190 proxy->unref();
191 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800192
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400193 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
194 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
195 }
robertphillips1125a032016-11-16 11:17:17 -0800196 }
197 }
198}
Robert Phillips123b7b82017-04-11 09:09:24 -0400199
200#endif