blob: 77ec2db0df804ba5867791e64f7f5920d54176dd [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
96 for (auto make : { make_deferred, make_wrapped }) {
97 // A single write
98 {
99 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
100
101 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get());
102
103 check_refs(reporter, sProxy.get(), 1, 1, 0, 1);
104
105 // In the deferred case, the discard batch created on instantiation adds an
106 // extra ref and write
107 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
108 caps.discardRenderTargetSupport();
robertphillipsd728f0c2016-11-21 11:05:03 -0800109 int expectedWrites = proxyGetsDiscardRef ? 2 : 1;
robertphillips1125a032016-11-16 11:17:17 -0800110
111 sProxy->instantiate(provider);
112
113 // In the deferred case, this checks that the refs transfered to the GrSurface
114 check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites);
115 }
116
117 // A single read
118 {
119 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
120
121 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get());
122
123 check_refs(reporter, sProxy.get(), 1, 1, 1, 0);
124
125 // In the deferred case, the discard batch created on instantiation adds an
126 // extra ref and write
127 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
128 caps.discardRenderTargetSupport();
129 int expectedWrites = proxyGetsDiscardRef ? 1 : 0;
130
131 sProxy->instantiate(provider);
132
133 // In the deferred case, this checks that the refs transfered to the GrSurface
134 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
135 }
136
137 // A single read/write pair
138 {
139 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
140
141 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(sProxy.get());
142
143 check_refs(reporter, sProxy.get(), 1, 1, 1, 1);
144
145 // In the deferred case, the discard batch created on instantiation adds an
146 // extra ref and write
147 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
148 caps.discardRenderTargetSupport();
149 int expectedWrites = proxyGetsDiscardRef ? 2 : 1;
150
151 sProxy->instantiate(provider);
152
153 // In the deferred case, this checks that the refs transfered to the GrSurface
154 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
155 }
156
157 // Multiple normal refs
158 {
159 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
160 sProxy->ref();
161 sProxy->ref();
162
163 check_refs(reporter, sProxy.get(), 3, 3, 0, 0);
164
165 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
166 caps.discardRenderTargetSupport();
167 int expectedWrites = proxyGetsDiscardRef ? 1 : 0;
168
169 sProxy->instantiate(provider);
170
171 // In the deferred case, this checks that the refs transfered to the GrSurface
172 check_refs(reporter, sProxy.get(), 3, 3, 0, expectedWrites);
173
174 sProxy->unref();
175 sProxy->unref();
176 }
177
178 // Continue using (reffing) proxy after instantiation
179 {
180 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
181 sProxy->ref();
182
183 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get());
184
185 check_refs(reporter, sProxy.get(), 2, 2, 0, 1);
186
187 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
188 caps.discardRenderTargetSupport();
189 int expectedWrites = proxyGetsDiscardRef ? 2 : 1;
190
191 sProxy->instantiate(provider);
192
193 // In the deferred case, this checks that the refs transfered to the GrSurface
194 check_refs(reporter, sProxy.get(), 2, 2, 0, expectedWrites);
195
196 sProxy->unref();
197 check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites);
198
199 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get());
200 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
201 }
202 }
203}
204
205#endif