blob: 3a72acb9f1ddc32cd6ff558596843dfa8dfe8551 [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
robertphillips1125a032016-11-16 11:17:17 -080018int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const {
19 return fRefCnt;
20}
21
22int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
23 if (fTarget) {
24 return fTarget->fRefCnt;
25 }
26
27 return fRefCnt;
28}
29
30int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const {
31 if (fTarget) {
32 SkASSERT(!fPendingReads);
33 return fTarget->fPendingReads;
34 }
35
36 return fPendingReads;
37}
38
39int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
40 if (fTarget) {
41 SkASSERT(!fPendingWrites);
42 return fTarget->fPendingWrites;
43 }
44
45 return fPendingWrites;
46}
47
Robert Phillips7928e762017-02-28 16:30:28 -050048#ifndef SK_DISABLE_DEFERRED_PROXIES
49
50static const int kWidthHeight = 128;
51
robertphillips1125a032016-11-16 11:17:17 -080052static void check_refs(skiatest::Reporter* reporter,
53 GrSurfaceProxy* proxy,
54 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
69static sk_sp<GrSurfaceProxy> make_deferred(const GrCaps& caps, GrTextureProvider* provider) {
70 GrSurfaceDesc desc;
71 desc.fFlags = kRenderTarget_GrSurfaceFlag;
72 desc.fWidth = kWidthHeight;
73 desc.fHeight = kWidthHeight;
74 desc.fConfig = kRGBA_8888_GrPixelConfig;
75
Robert Phillips7928e762017-02-28 16:30:28 -050076 return GrSurfaceProxy::MakeDeferred(provider, caps, desc,
77 SkBackingFit::kApprox, SkBudgeted::kYes);
robertphillips1125a032016-11-16 11:17:17 -080078}
79
80static sk_sp<GrSurfaceProxy> make_wrapped(const GrCaps& caps, GrTextureProvider* provider) {
81 GrSurfaceDesc desc;
82 desc.fFlags = kRenderTarget_GrSurfaceFlag;
83 desc.fWidth = kWidthHeight;
84 desc.fHeight = kWidthHeight;
85 desc.fConfig = kRGBA_8888_GrPixelConfig;
86
87 sk_sp<GrTexture> tex(provider->createTexture(desc, SkBudgeted::kNo));
88
89 // Flush the IOWrite from the initial discard or it will confuse the later ref count checks
90 tex->flushWrites();
91
92 return GrSurfaceProxy::MakeWrapped(std::move(tex));
93}
94
95DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
96 GrTextureProvider* provider = ctxInfo.grContext()->textureProvider();
97 const GrCaps& caps = *ctxInfo.grContext()->caps();
98
Brian Salomoncdcc33f2017-02-21 09:49:20 -050099 // Currently the op itself takes a pending write and the render target op list does as well.
100 static const int kWritesForDiscard = 2;
robertphillips1125a032016-11-16 11:17:17 -0800101 for (auto make : { make_deferred, make_wrapped }) {
102 // A single write
103 {
104 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
105
106 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get());
107
108 check_refs(reporter, sProxy.get(), 1, 1, 0, 1);
109
Brian Salomon09d994e2016-12-21 11:14:46 -0500110 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800111 // extra ref and write
112 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
113 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500114 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800115
116 sProxy->instantiate(provider);
117
118 // In the deferred case, this checks that the refs transfered to the GrSurface
119 check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites);
120 }
121
122 // A single read
123 {
124 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
125
126 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get());
127
128 check_refs(reporter, sProxy.get(), 1, 1, 1, 0);
129
Brian Salomon09d994e2016-12-21 11:14:46 -0500130 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800131 // extra ref and write
132 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
133 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500134 int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0;
robertphillips1125a032016-11-16 11:17:17 -0800135
136 sProxy->instantiate(provider);
137
138 // In the deferred case, this checks that the refs transfered to the GrSurface
139 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
140 }
141
142 // A single read/write pair
143 {
144 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
145
146 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(sProxy.get());
147
148 check_refs(reporter, sProxy.get(), 1, 1, 1, 1);
149
Brian Salomon09d994e2016-12-21 11:14:46 -0500150 // In the deferred case, the discard op created on instantiation adds an
robertphillips1125a032016-11-16 11:17:17 -0800151 // extra ref and write
152 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
153 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500154 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800155
156 sProxy->instantiate(provider);
157
Brian Salomon09d994e2016-12-21 11:14:46 -0500158 // In the deferred case, this checks that the refs transferred to the GrSurface
robertphillips1125a032016-11-16 11:17:17 -0800159 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
160 }
161
162 // Multiple normal refs
163 {
164 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
165 sProxy->ref();
166 sProxy->ref();
167
168 check_refs(reporter, sProxy.get(), 3, 3, 0, 0);
169
170 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
171 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500172 int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0;
robertphillips1125a032016-11-16 11:17:17 -0800173
174 sProxy->instantiate(provider);
175
Brian Salomon09d994e2016-12-21 11:14:46 -0500176 // In the deferred case, this checks that the refs transferred to the GrSurface
robertphillips1125a032016-11-16 11:17:17 -0800177 check_refs(reporter, sProxy.get(), 3, 3, 0, expectedWrites);
178
179 sProxy->unref();
180 sProxy->unref();
181 }
182
183 // Continue using (reffing) proxy after instantiation
184 {
185 sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider));
186 sProxy->ref();
187
188 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get());
189
190 check_refs(reporter, sProxy.get(), 2, 2, 0, 1);
191
192 bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() &&
193 caps.discardRenderTargetSupport();
Brian Salomoncdcc33f2017-02-21 09:49:20 -0500194 int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0);
robertphillips1125a032016-11-16 11:17:17 -0800195
196 sProxy->instantiate(provider);
197
198 // In the deferred case, this checks that the refs transfered to the GrSurface
199 check_refs(reporter, sProxy.get(), 2, 2, 0, expectedWrites);
200
201 sProxy->unref();
202 check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites);
203
204 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get());
205 check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites);
206 }
207 }
208}
Robert Phillips7928e762017-02-28 16:30:28 -0500209#endif
robertphillips1125a032016-11-16 11:17:17 -0800210
211#endif