blob: 90aac0cc94de6b7add3c5f8896e2fe2375b8ff78 [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
Robert Phillips7ee385e2017-03-30 08:02:11 -040012#include "GrContextPriv.h"
Brian Salomonae5f9532018-07-31 11:03:40 -040013#include "GrPendingIOResource.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050014#include "GrProxyProvider.h"
robertphillips1125a032016-11-16 11:17:17 -080015#include "GrRenderTargetProxy.h"
Brian Osman32342f02017-03-04 08:12:46 -050016#include "GrResourceProvider.h"
17#include "GrSurfaceProxy.h"
Robert Phillips646e4292017-06-13 12:44:56 -040018#include "GrTexture.h"
Brian Osman32342f02017-03-04 08:12:46 -050019#include "GrTextureProxy.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,
73 SkBackingFit::kApprox, SkBudgeted::kYes,
74 GrInternalSurfaceFlags::kNoPendingIO);
robertphillips1125a032016-11-16 11:17:17 -080075}
76
Greg Daniel4065d452018-11-16 15:43:41 -050077static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider, const GrCaps* caps) {
robertphillips1125a032016-11-16 11:17:17 -080078 GrSurfaceDesc desc;
79 desc.fFlags = kRenderTarget_GrSurfaceFlag;
80 desc.fWidth = kWidthHeight;
81 desc.fHeight = kWidthHeight;
82 desc.fConfig = kRGBA_8888_GrPixelConfig;
83
Chris Daltond004e0b2018-09-27 09:28:03 -060084 return proxyProvider->testingOnly_createInstantiatedProxy(
85 desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080086}
87
88DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips9da87e02019-02-04 13:26:26 -050089 GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider();
90 GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider();
91 const GrCaps* caps = ctxInfo.grContext()->priv().caps();
robertphillips1125a032016-11-16 11:17:17 -080092
93 for (auto make : { make_deferred, make_wrapped }) {
94 // A single write
95 {
Greg Daniel4065d452018-11-16 15:43:41 -050096 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040097 if (proxy.get()) {
98 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -080099
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400100 static const int kExpectedReads = 0;
101 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800102
Robert Phillips715d08c2018-07-18 13:56:48 -0400103 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
104
105 check_refs(reporter, proxy.get(), 1, backingRefs, 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 {
Greg Daniel4065d452018-11-16 15:43:41 -0500116 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
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
Robert Phillips715d08c2018-07-18 13:56:48 -0400123 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
124
125 check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800126
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500127 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800128
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400129 // In the deferred case, this checks that the refs transfered to the GrSurface
130 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
131 }
robertphillips1125a032016-11-16 11:17:17 -0800132 }
133
134 // A single read/write pair
135 {
Greg Daniel4065d452018-11-16 15:43:41 -0500136 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400137 if (proxy.get()) {
138 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800139
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400140 static const int kExpectedReads = 1;
141 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800142
Robert Phillips715d08c2018-07-18 13:56:48 -0400143 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
144
145 check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800146
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500147 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800148
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400149 // In the deferred case, this checks that the refs transferred to the GrSurface
150 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
151 }
robertphillips1125a032016-11-16 11:17:17 -0800152 }
153
154 // Multiple normal refs
155 {
Greg Daniel4065d452018-11-16 15:43:41 -0500156 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400157 if (proxy.get()) {
158 proxy->ref();
159 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800160
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400161 static const int kExpectedReads = 0;
162 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800163
Robert Phillips715d08c2018-07-18 13:56:48 -0400164 int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1;
165
166 check_refs(reporter, proxy.get(), 3, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800167
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500168 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800169
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400170 // In the deferred case, this checks that the refs transferred to the GrSurface
171 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800172
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400173 proxy->unref();
174 proxy->unref();
175 }
robertphillips1125a032016-11-16 11:17:17 -0800176 }
177
178 // Continue using (reffing) proxy after instantiation
179 {
Greg Daniel4065d452018-11-16 15:43:41 -0500180 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400181 if (proxy.get()) {
182 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800183
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400184 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800185
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400186 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800187
Robert Phillips715d08c2018-07-18 13:56:48 -0400188 int backingRefs = proxy->isWrapped_ForTesting() ? 2 : -1;
189
190 check_refs(reporter, proxy.get(), 2, backingRefs, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800191
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500192 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800193
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400194 // In the deferred case, this checks that the refs transfered to the GrSurface
195 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800196
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400197 proxy->unref();
198 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800199
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400200 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
201 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
202 }
robertphillips1125a032016-11-16 11:17:17 -0800203 }
204 }
205}