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 | |
| 10 | #include "Test.h" |
| 11 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 13 | #include "GrPendingIOResource.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 15 | #include "GrRenderTargetProxy.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 16 | #include "GrResourceProvider.h" |
| 17 | #include "GrSurfaceProxy.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 18 | #include "GrTexture.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 19 | #include "GrTextureProxy.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, |
| 73 | SkBackingFit::kApprox, SkBudgeted::kYes, |
| 74 | GrInternalSurfaceFlags::kNoPendingIO); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 77 | static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider, const GrCaps* caps) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 78 | GrSurfaceDesc desc; |
| 79 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 80 | desc.fWidth = kWidthHeight; |
| 81 | desc.fHeight = kWidthHeight; |
| 82 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 83 | |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 84 | return proxyProvider->testingOnly_createInstantiatedProxy( |
| 85 | desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kNo); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 89 | GrProxyProvider* proxyProvider = ctxInfo.grContext()->priv().proxyProvider(); |
| 90 | GrResourceProvider* resourceProvider = ctxInfo.grContext()->priv().resourceProvider(); |
| 91 | const GrCaps* caps = ctxInfo.grContext()->priv().caps(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 92 | |
| 93 | for (auto make : { make_deferred, make_wrapped }) { |
| 94 | // A single write |
| 95 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 96 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 97 | if (proxy.get()) { |
| 98 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 99 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 100 | static const int kExpectedReads = 0; |
| 101 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 102 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 103 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 104 | |
| 105 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 106 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 107 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 108 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 109 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 110 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 111 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // A single read |
| 115 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 116 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 117 | if (proxy.get()) { |
| 118 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 119 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 120 | static const int kExpectedReads = 1; |
| 121 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 122 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 123 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 124 | |
| 125 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 126 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 127 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 128 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 129 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 130 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 131 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // A single read/write pair |
| 135 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 136 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 137 | if (proxy.get()) { |
| 138 | GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 139 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 140 | static const int kExpectedReads = 1; |
| 141 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 142 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 143 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 144 | |
| 145 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 146 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 147 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 148 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 149 | // In the deferred case, this checks that the refs transferred to the GrSurface |
| 150 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 151 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Multiple normal refs |
| 155 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 156 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 157 | if (proxy.get()) { |
| 158 | proxy->ref(); |
| 159 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 160 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 161 | static const int kExpectedReads = 0; |
| 162 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 163 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 164 | int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1; |
| 165 | |
| 166 | check_refs(reporter, proxy.get(), 3, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 167 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 168 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 169 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 170 | // In the deferred case, this checks that the refs transferred to the GrSurface |
| 171 | check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 172 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 173 | proxy->unref(); |
| 174 | proxy->unref(); |
| 175 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Continue using (reffing) proxy after instantiation |
| 179 | { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 180 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider, caps)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 181 | if (proxy.get()) { |
| 182 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 183 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 184 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 185 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 186 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 187 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 188 | int backingRefs = proxy->isWrapped_ForTesting() ? 2 : -1; |
| 189 | |
| 190 | check_refs(reporter, proxy.get(), 2, backingRefs, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 191 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 192 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 193 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 194 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 195 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 196 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 197 | proxy->unref(); |
| 198 | check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 199 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 200 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
| 201 | check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites); |
| 202 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |