blob: 288085f99807c02893041bc5d56e308d55a5b80e [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
Robert Phillips0bd24dc2018-01-16 08:06:32 -050064static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider) {
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
Brian Salomon2a4f9832018-03-03 22:43:43 -050071 return proxyProvider->createProxy(desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040072 SkBudgeted::kYes, GrInternalSurfaceFlags::kNoPendingIO);
robertphillips1125a032016-11-16 11:17:17 -080073}
74
Robert Phillips0bd24dc2018-01-16 08:06:32 -050075static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider) {
robertphillips1125a032016-11-16 11:17:17 -080076 GrSurfaceDesc desc;
77 desc.fFlags = kRenderTarget_GrSurfaceFlag;
78 desc.fWidth = kWidthHeight;
79 desc.fHeight = kWidthHeight;
80 desc.fConfig = kRGBA_8888_GrPixelConfig;
81
Chris Daltond004e0b2018-09-27 09:28:03 -060082 return proxyProvider->testingOnly_createInstantiatedProxy(
83 desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo);
robertphillips1125a032016-11-16 11:17:17 -080084}
85
86DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050087 GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -050088 GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
robertphillips1125a032016-11-16 11:17:17 -080089
90 for (auto make : { make_deferred, make_wrapped }) {
91 // A single write
92 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -050093 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -040094 if (proxy.get()) {
95 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -080096
Jim Van Verth311cc6b2017-09-20 17:51:59 -040097 static const int kExpectedReads = 0;
98 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -080099
Robert Phillips715d08c2018-07-18 13:56:48 -0400100 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
101
102 check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800103
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500104 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800105
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400106 // In the deferred case, this checks that the refs transfered to the GrSurface
107 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
108 }
robertphillips1125a032016-11-16 11:17:17 -0800109 }
110
111 // A single read
112 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500113 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400114 if (proxy.get()) {
115 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800116
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400117 static const int kExpectedReads = 1;
118 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800119
Robert Phillips715d08c2018-07-18 13:56:48 -0400120 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
121
122 check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800123
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500124 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800125
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400126 // In the deferred case, this checks that the refs transfered to the GrSurface
127 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
128 }
robertphillips1125a032016-11-16 11:17:17 -0800129 }
130
131 // A single read/write pair
132 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500133 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400134 if (proxy.get()) {
135 GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800136
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400137 static const int kExpectedReads = 1;
138 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800139
Robert Phillips715d08c2018-07-18 13:56:48 -0400140 int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1;
141
142 check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800143
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500144 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800145
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400146 // In the deferred case, this checks that the refs transferred to the GrSurface
147 check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites);
148 }
robertphillips1125a032016-11-16 11:17:17 -0800149 }
150
151 // Multiple normal refs
152 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500153 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400154 if (proxy.get()) {
155 proxy->ref();
156 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800157
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400158 static const int kExpectedReads = 0;
159 static const int kExpectedWrites = 0;
robertphillips1125a032016-11-16 11:17:17 -0800160
Robert Phillips715d08c2018-07-18 13:56:48 -0400161 int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1;
162
163 check_refs(reporter, proxy.get(), 3, backingRefs, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800164
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500165 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800166
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400167 // In the deferred case, this checks that the refs transferred to the GrSurface
168 check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800169
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400170 proxy->unref();
171 proxy->unref();
172 }
robertphillips1125a032016-11-16 11:17:17 -0800173 }
174
175 // Continue using (reffing) proxy after instantiation
176 {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500177 sk_sp<GrTextureProxy> proxy((*make)(proxyProvider));
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400178 if (proxy.get()) {
179 proxy->ref();
robertphillips1125a032016-11-16 11:17:17 -0800180
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400181 GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get());
robertphillips1125a032016-11-16 11:17:17 -0800182
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400183 static const int kExpectedWrites = 1;
robertphillips1125a032016-11-16 11:17:17 -0800184
Robert Phillips715d08c2018-07-18 13:56:48 -0400185 int backingRefs = proxy->isWrapped_ForTesting() ? 2 : -1;
186
187 check_refs(reporter, proxy.get(), 2, backingRefs, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800188
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500189 proxy->instantiate(resourceProvider);
robertphillips1125a032016-11-16 11:17:17 -0800190
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400191 // In the deferred case, this checks that the refs transfered to the GrSurface
192 check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800193
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400194 proxy->unref();
195 check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites);
robertphillips1125a032016-11-16 11:17:17 -0800196
Jim Van Verth311cc6b2017-09-20 17:51:59 -0400197 GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get());
198 check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites);
199 }
robertphillips1125a032016-11-16 11:17:17 -0800200 }
201 }
202}