robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "tests/Test.h" |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #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" |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 20 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 21 | int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const { |
| 22 | if (fTarget) { |
| 23 | return fTarget->fRefCnt; |
| 24 | } |
| 25 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 26 | return -1; // no backing GrSurface |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const { |
| 30 | if (fTarget) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 31 | return fTarget->fPendingReads; |
| 32 | } |
| 33 | |
| 34 | return fPendingReads; |
| 35 | } |
| 36 | |
| 37 | int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const { |
| 38 | if (fTarget) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 39 | return fTarget->fPendingWrites; |
| 40 | } |
| 41 | |
| 42 | return fPendingWrites; |
| 43 | } |
| 44 | |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 45 | static const int kWidthHeight = 128; |
| 46 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 47 | static void check_refs(skiatest::Reporter* reporter, |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 48 | GrTextureProxy* proxy, |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 49 | int32_t expectedProxyRefs, |
| 50 | int32_t expectedBackingRefs, |
| 51 | int32_t expectedNumReads, |
| 52 | int32_t expectedNumWrites) { |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, proxy->priv().getProxyRefCnt() == expectedProxyRefs); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 54 | 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 Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 58 | SkASSERT(proxy->priv().getProxyRefCnt() == expectedProxyRefs); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 59 | SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs); |
| 60 | SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads); |
| 61 | SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites); |
| 62 | } |
| 63 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 64 | static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider, const GrCaps* caps) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 65 | GrSurfaceDesc desc; |
| 66 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 67 | desc.fWidth = kWidthHeight; |
| 68 | desc.fHeight = kWidthHeight; |
| 69 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 70 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 71 | const GrBackendFormat format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
| 72 | return proxyProvider->createProxy(format, desc, kBottomLeft_GrSurfaceOrigin, |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 73 | SkBackingFit::kApprox, SkBudgeted::kYes); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 76 | static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider, const GrCaps* caps) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 77 | GrSurfaceDesc desc; |
| 78 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 79 | desc.fWidth = kWidthHeight; |
| 80 | desc.fHeight = kWidthHeight; |
| 81 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 82 | |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 83 | return proxyProvider->testingOnly_createInstantiatedProxy( |
| 84 | desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 88 | GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider(); |
| 89 | GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider(); |
| 90 | const GrCaps* caps = ctxInfo.grContext()->priv().caps(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 91 | |
| 92 | for (auto make : { make_deferred, make_wrapped }) { |
| 93 | // A single write |
| 94 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 95 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 96 | if (proxy.get()) { |
| 97 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 98 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 99 | static const int kExpectedReads = 0; |
| 100 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 101 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 102 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 103 | |
| 104 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 105 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 106 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 107 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 108 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 109 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 110 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // A single read |
| 114 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 115 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 116 | if (proxy.get()) { |
| 117 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 118 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 119 | static const int kExpectedReads = 1; |
| 120 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 121 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 122 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 123 | |
| 124 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 125 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 126 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 127 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 128 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 129 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 130 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // A single read/write pair |
| 134 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 135 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 136 | if (proxy.get()) { |
| 137 | GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 138 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 139 | static const int kExpectedReads = 1; |
| 140 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 141 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 142 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 143 | |
| 144 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 145 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 146 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 147 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 148 | // In the deferred case, this checks that the refs transferred to the GrSurface |
| 149 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 150 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Multiple normal refs |
| 154 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 155 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 156 | if (proxy.get()) { |
| 157 | proxy->ref(); |
| 158 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 159 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 160 | static const int kExpectedReads = 0; |
| 161 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 162 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 163 | int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1; |
| 164 | |
| 165 | check_refs(reporter, proxy.get(), 3, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 166 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 167 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 168 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 169 | // In the deferred case, this checks that the refs transferred to the GrSurface |
| 170 | check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 171 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 172 | proxy->unref(); |
| 173 | proxy->unref(); |
| 174 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Continue using (reffing) proxy after instantiation |
| 178 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 179 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 180 | if (proxy.get()) { |
| 181 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 182 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 183 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 184 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 185 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 186 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 187 | int backingRefs = proxy->isWrapped_ForTesting() ? 2 : -1; |
| 188 | |
| 189 | check_refs(reporter, proxy.get(), 2, backingRefs, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 190 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 191 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 192 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 193 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 194 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 195 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 196 | proxy->unref(); |
| 197 | check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 198 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 199 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
| 200 | check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites); |
| 201 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |