blob: 4502521116f45903ab38c999bc2f60647baa5b37 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
robertphillips1125a032016-11-16 11:17:17 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrTexture.h"
13#include "include/private/GrRenderTargetProxy.h"
14#include "include/private/GrSurfaceProxy.h"
15#include "include/private/GrTextureProxy.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrPendingIOResource.h"
18#include "src/gpu/GrProxyProvider.h"
19#include "src/gpu/GrResourceProvider.h"
robertphillips1125a032016-11-16 11:17:17 -080020
robertphillips1125a032016-11-16 11:17:17 -080021int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const {
22 if (fTarget) {
23 return fTarget->fRefCnt;
24 }
25
Robert Phillips715d08c2018-07-18 13:56:48 -040026 return -1; // no backing GrSurface
robertphillips1125a032016-11-16 11:17:17 -080027}
28
29int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const {
30 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080031 return fTarget->fPendingReads;
32 }
33
34 return fPendingReads;
35}
36
37int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const {
38 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080039 return fTarget->fPendingWrites;
40 }
41
42 return fPendingWrites;
43}
44
Robert Phillips7928e762017-02-28 16:30:28 -050045static const int kWidthHeight = 128;
46
robertphillips1125a032016-11-16 11:17:17 -080047static void check_refs(skiatest::Reporter* reporter,
Robert Phillips7ee385e2017-03-30 08:02:11 -040048 GrTextureProxy* proxy,
robertphillips1125a032016-11-16 11:17:17 -080049 int32_t expectedProxyRefs,
50 int32_t expectedBackingRefs,
51 int32_t expectedNumReads,
52 int32_t expectedNumWrites) {
Robert Phillips715d08c2018-07-18 13:56:48 -040053 REPORTER_ASSERT(reporter, proxy->priv().getProxyRefCnt() == expectedProxyRefs);
robertphillips1125a032016-11-16 11:17:17 -080054 REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
55 REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
56 REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
57
Robert Phillips715d08c2018-07-18 13:56:48 -040058 SkASSERT(proxy->priv().getProxyRefCnt() == expectedProxyRefs);
robertphillips1125a032016-11-16 11:17:17 -080059 SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs);
60 SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads);
61 SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites);
62}
63
Greg Daniel4065d452018-11-16 15:43:41 -050064static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080065 GrSurfaceDesc desc;
66 desc.fFlags = kRenderTarget_GrSurfaceFlag;
67 desc.fWidth = kWidthHeight;
68 desc.fHeight = kWidthHeight;
69 desc.fConfig = kRGBA_8888_GrPixelConfig;
70
Greg Daniel4065d452018-11-16 15:43:41 -050071 const GrBackendFormat format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
72 return proxyProvider->createProxy(format, desc, kBottomLeft_GrSurfaceOrigin,
Robert Phillips10d17212019-04-24 14:09:10 -040073 SkBackingFit::kApprox, SkBudgeted::kYes);
robertphillips1125a032016-11-16 11:17:17 -080074}
75
Greg Daniel4065d452018-11-16 15:43:41 -050076static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080077 GrSurfaceDesc desc;
78 desc.fFlags = kRenderTarget_GrSurfaceFlag;
79 desc.fWidth = kWidthHeight;
80 desc.fHeight = kWidthHeight;
81 desc.fConfig = kRGBA_8888_GrPixelConfig;
82
Chris Daltond004e0b2018-09-27 09:28:03 -060083 return proxyProvider->testingOnly_createInstantiatedProxy(
84 desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080085}
86
87DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050088 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
89 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
90 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
robertphillips1125a032016-11-16 11:17:17 -080091
92 for (auto make : { make_deferred, make_wrapped }) {
93 // A single write
94 {
Greg Daniel4065d452018-11-16 15:43:41 -050095 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040096 if (proxy.get()) {
97 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -080098
Jim Van Verth311cc6b2017-09-20 17:51:59 -040099 static const int kExpectedReads = 0;
100 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800101
Robert Phillips715d08c2018-07-18 13:56:48 -0400102 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
103
104 check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800105
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500106 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800107
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400108 // In the deferred case, this checks that the refs transfered to the GrSurface
109 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
110 }
robertphillips1125a032016-11-16 11:17:17 -0800111 }
112
113 // A single read
114 {
Greg Daniel4065d452018-11-16 15:43:41 -0500115 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400116 if (proxy.get()) {
117 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800118
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400119 static const int kExpectedReads = 1;
120 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800121
Robert Phillips715d08c2018-07-18 13:56:48 -0400122 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
123
124 check_refs(reporter, proxy.get(), 1, backingRefs, 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 {
Greg Daniel4065d452018-11-16 15:43:41 -0500135 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
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
Robert Phillips715d08c2018-07-18 13:56:48 -0400142 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
143
144 check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800145
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500146 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800147
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400148 // In the deferred case, this checks that the refs transferred to the GrSurface
149 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
150 }
robertphillips1125a032016-11-16 11:17:17 -0800151 }
152
153 // Multiple normal refs
154 {
Greg Daniel4065d452018-11-16 15:43:41 -0500155 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400156 if (proxy.get()) {
157 proxy->ref();
158 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800159
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400160 static const int kExpectedReads = 0;
161 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800162
Robert Phillips715d08c2018-07-18 13:56:48 -0400163 int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1;
164
165 check_refs(reporter, proxy.get(), 3, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800166
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500167 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800168
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400169 // In the deferred case, this checks that the refs transferred to the GrSurface
170 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800171
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400172 proxy->unref();
173 proxy->unref();
174 }
robertphillips1125a032016-11-16 11:17:17 -0800175 }
176
177 // Continue using (reffing) proxy after instantiation
178 {
Greg Daniel4065d452018-11-16 15:43:41 -0500179 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400180 if (proxy.get()) {
181 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800182
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400183 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800184
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400185 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800186
Robert Phillips715d08c2018-07-18 13:56:48 -0400187 int backingRefs = proxy->isWrapped_ForTesting() ? 2 : -1;
188
189 check_refs(reporter, proxy.get(), 2, backingRefs, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800190
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500191 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800192
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400193 // In the deferred case, this checks that the refs transfered to the GrSurface
194 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800195
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400196 proxy->unref();
197 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800198
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400199 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
200 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
201 }
robertphillips1125a032016-11-16 11:17:17 -0800202 }
203 }
204}