blob: 6cd4cb0b0e052c002388ee8daa13c2660037a6dd [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
13#include "GrSurfaceProxy.h"
14#include "GrTextureProxy.h"
15#include "GrRenderTargetPriv.h"
16#include "GrRenderTargetProxy.h"
17
18static const int kWidthHeight = 128;
19
20int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const {
21 return fRefCnt;
22}
23
24int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
25 if (fTarget) {
26 return fTarget->fRefCnt;
27 }
28
29 return fRefCnt;
30}
31
32int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const {
33 if (fTarget) {
34 SkASSERT(!fPendingReads);
35 return fTarget->fPendingReads;
36 }
37
38 return fPendingReads;
39}
40
41int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
42 if (fTarget) {
43 SkASSERT(!fPendingWrites);
44 return fTarget->fPendingWrites;
45 }
46
47 return fPendingWrites;
48}
49
50static void check_refs(skiatest::Reporter* reporter,
51 GrSurfaceProxy* proxy,
52 int32_t expectedProxyRefs,
53 int32_t expectedBackingRefs,
54 int32_t expectedNumReads,
55 int32_t expectedNumWrites) {
56 REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
57 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
58 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
59 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
60
61 SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs);
62 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
63 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
64 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
65}
66
67static sk_sp<GrSurfaceProxy> make_deferred(const GrCaps& caps, GrTextureProvider* provider) {
68 GrSurfaceDesc desc;
69 desc.fFlags = kRenderTarget_GrSurfaceFlag;
70 desc.fWidth = kWidthHeight;
71 desc.fHeight = kWidthHeight;
72 desc.fConfig = kRGBA_8888_GrPixelConfig;
73
74 return GrSurfaceProxy::MakeDeferred(caps, desc, SkBackingFit::kApprox, SkBudgeted::kYes);
75}
76
77static sk_sp<GrSurfaceProxy> make_wrapped(const GrCaps& caps, GrTextureProvider* provider) {
78 GrSurfaceDesc desc;
79 desc.fFlags = kRenderTarget_GrSurfaceFlag;
80 desc.fWidth = kWidthHeight;
81 desc.fHeight = kWidthHeight;
82 desc.fConfig = kRGBA_8888_GrPixelConfig;
83
84 sk_sp<GrTexture> tex(provider->createTexture(desc, SkBudgeted::kNo));
85
86 // Flush the IOWrite from the initial discard or it will confuse the later ref count checks
87 tex->flushWrites();
88
89 return GrSurfaceProxy::MakeWrapped(std::move(tex));
90}
91
92DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
93 GrTextureProvider* provider = ctxInfo.grContext()->textureProvider();
94 const GrCaps& caps = *ctxInfo.grContext()->caps();
95
Brian Salomoncdcc33f2017-02-21 09:49:20 -050096 // Currently the op itself takes a pending write and the render target op list does as well.
97 static const int kWritesForDiscard = 2;
robertphillips1125a032016-11-16 11:17:17 -080098 for (auto make : { make_deferred, make_wrapped }) {
99 // A single write
100 {
101 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
102
103 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get());
104
105 check_refs(reporter, sProxy.get(), 1, 1, 0, 1);
106
Brian Salomon09d994e2016-12-21 11:14:46 -0500107 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800108 // extra ref and write
109 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
110 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500111 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800112
113 sProxy->instantiate(provider);
114
115 // In the deferred case, this checks that the refs transfered to the GrSurface
116 check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites);
117 }
118
119 // A single read
120 {
121 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
122
123 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get());
124
125 check_refs(reporter, sProxy.get(), 1, 1, 1, 0);
126
Brian Salomon09d994e2016-12-21 11:14:46 -0500127 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800128 // extra ref and write
129 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
130 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500131 int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0;
robertphillips1125a032016-11-16 11:17:17 -0800132
133 sProxy->instantiate(provider);
134
135 // In the deferred case, this checks that the refs transfered to the GrSurface
136 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
137 }
138
139 // A single read/write pair
140 {
141 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
142
143 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(sProxy.get());
144
145 check_refs(reporter, sProxy.get(), 1, 1, 1, 1);
146
Brian Salomon09d994e2016-12-21 11:14:46 -0500147 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800148 // extra ref and write
149 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
150 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500151 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800152
153 sProxy->instantiate(provider);
154
Brian Salomon09d994e2016-12-21 11:14:46 -0500155 // In the deferred case, this checks that the refs transferred to the GrSurface
robertphillips1125a032016-11-16 11:17:17 -0800156 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
157 }
158
159 // Multiple normal refs
160 {
161 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
162 sProxy->ref();
163 sProxy->ref();
164
165 check_refs(reporter, sProxy.get(), 3, 3, 0, 0);
166
167 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
168 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500169 int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0;
robertphillips1125a032016-11-16 11:17:17 -0800170
171 sProxy->instantiate(provider);
172
Brian Salomon09d994e2016-12-21 11:14:46 -0500173 // In the deferred case, this checks that the refs transferred to the GrSurface
robertphillips1125a032016-11-16 11:17:17 -0800174 check_refs(reporter, sProxy.get(), 3, 3, 0, expectedWrites);
175
176 sProxy->unref();
177 sProxy->unref();
178 }
179
180 // Continue using (reffing) proxy after instantiation
181 {
182 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
183 sProxy->ref();
184
185 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get());
186
187 check_refs(reporter, sProxy.get(), 2, 2, 0, 1);
188
189 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
190 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500191 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800192
193 sProxy->instantiate(provider);
194
195 // In the deferred case, this checks that the refs transfered to the GrSurface
196 check_refs(reporter, sProxy.get(), 2, 2, 0, expectedWrites);
197
198 sProxy->unref();
199 check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites);
200
201 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get());
202 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
203 }
204 }
205}
206
207#endif