blob: 30cc449c99d8a3516ab8a9f4c3882090f8cf5f94 [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;
72 desc.fWidth = kWidthHeight;
73 desc.fHeight = kWidthHeight;
74 desc.fConfig = kRGBA_8888_GrPixelConfig;
75
Brian Salomon2a4f9832018-03-03 22:43:43 -050076 return proxyProvider->createProxy(desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040077 SkBudgeted::kYes, GrInternalSurfaceFlags::kNoPendingIO);
robertphillips1125a032016-11-16 11:17:17 -080078}
79
Robert Phillips0bd24dc2018-01-16 08:06:32 -050080static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider) {
robertphillips1125a032016-11-16 11:17:17 -080081 GrSurfaceDesc desc;
82 desc.fFlags = kRenderTarget_GrSurfaceFlag;
83 desc.fWidth = kWidthHeight;
84 desc.fHeight = kWidthHeight;
85 desc.fConfig = kRGBA_8888_GrPixelConfig;
86
Brian Salomon2a4f9832018-03-03 22:43:43 -050087 return proxyProvider->createInstantiatedProxy(desc, kBottomLeft_GrSurfaceOrigin,
88 SkBackingFit::kExact, SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080089}
90
91DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050092 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050093 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080094
95 for (auto make : { make_deferred, make_wrapped }) {
96 // A single write
97 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050098 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040099 if (proxy.get()) {
100 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800101
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400102 static const int kExpectedReads = 0;
103 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800104
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400105 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800106
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500107 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800108
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400109 // In the deferred case, this checks that the refs transfered to the GrSurface
110 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
111 }
robertphillips1125a032016-11-16 11:17:17 -0800112 }
113
114 // A single read
115 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500116 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400117 if (proxy.get()) {
118 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800119
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400120 static const int kExpectedReads = 1;
121 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800122
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400123 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800124
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500125 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800126
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400127 // In the deferred case, this checks that the refs transfered to the GrSurface
128 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
129 }
robertphillips1125a032016-11-16 11:17:17 -0800130 }
131
132 // A single read/write pair
133 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500134 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400135 if (proxy.get()) {
136 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800137
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400138 static const int kExpectedReads = 1;
139 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800140
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400141 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800142
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500143 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800144
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400145 // In the deferred case, this checks that the refs transferred to the GrSurface
146 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
147 }
robertphillips1125a032016-11-16 11:17:17 -0800148 }
149
150 // Multiple normal refs
151 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500152 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400153 if (proxy.get()) {
154 proxy->ref();
155 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800156
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400157 static const int kExpectedReads = 0;
158 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800159
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400160 check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800161
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500162 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800163
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400164 // In the deferred case, this checks that the refs transferred to the GrSurface
165 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800166
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400167 proxy->unref();
168 proxy->unref();
169 }
robertphillips1125a032016-11-16 11:17:17 -0800170 }
171
172 // Continue using (reffing) proxy after instantiation
173 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500174 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400175 if (proxy.get()) {
176 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800177
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400178 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800179
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400180 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800181
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400182 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800183
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500184 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800185
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400186 // In the deferred case, this checks that the refs transfered to the GrSurface
187 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800188
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400189 proxy->unref();
190 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800191
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400192 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
193 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
194 }
robertphillips1125a032016-11-16 11:17:17 -0800195 }
196 }
197}
Robert Phillips123b7b82017-04-11 09:09:24 -0400198
199#endif