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 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 64 | static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider) { |
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 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 71 | return proxyProvider->createProxy(desc, kBottomLeft_GrSurfaceOrigin, SkBackingFit::kApprox, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 72 | SkBudgeted::kYes, GrInternalSurfaceFlags::kNoPendingIO); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 75 | static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 76 | GrSurfaceDesc desc; |
| 77 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 78 | desc.fWidth = kWidthHeight; |
| 79 | desc.fHeight = kWidthHeight; |
| 80 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 81 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 82 | return proxyProvider->createInstantiatedProxy(desc, kBottomLeft_GrSurfaceOrigin, |
| 83 | SkBackingFit::kExact, SkBudgeted::kNo); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 87 | GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 88 | GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 89 | |
| 90 | for (auto make : { make_deferred, make_wrapped }) { |
| 91 | // A single write |
| 92 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 93 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 94 | if (proxy.get()) { |
| 95 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 96 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 97 | static const int kExpectedReads = 0; |
| 98 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 99 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 100 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 101 | |
| 102 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 103 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 104 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 105 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 106 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 107 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 108 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // A single read |
| 112 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 113 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 114 | if (proxy.get()) { |
| 115 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 116 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 117 | static const int kExpectedReads = 1; |
| 118 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 119 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 120 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 121 | |
| 122 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 123 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 124 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 125 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 126 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 127 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 128 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // A single read/write pair |
| 132 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 133 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 134 | if (proxy.get()) { |
| 135 | GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 136 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 137 | static const int kExpectedReads = 1; |
| 138 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 139 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 140 | int backingRefs = proxy->isWrapped_ForTesting() ? 1 : -1; |
| 141 | |
| 142 | check_refs(reporter, proxy.get(), 1, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 143 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 144 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 145 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 146 | // In the deferred case, this checks that the refs transferred to the GrSurface |
| 147 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 148 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Multiple normal refs |
| 152 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 153 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 154 | if (proxy.get()) { |
| 155 | proxy->ref(); |
| 156 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 157 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 158 | static const int kExpectedReads = 0; |
| 159 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 160 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 161 | int backingRefs = proxy->isWrapped_ForTesting() ? 3 : -1; |
| 162 | |
| 163 | check_refs(reporter, proxy.get(), 3, backingRefs, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 164 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 165 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 166 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 167 | // In the deferred case, this checks that the refs transferred to the GrSurface |
| 168 | check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites); |
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 | proxy->unref(); |
| 171 | proxy->unref(); |
| 172 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Continue using (reffing) proxy after instantiation |
| 176 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 177 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 178 | if (proxy.get()) { |
| 179 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 180 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 181 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
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 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 184 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 185 | int backingRefs = proxy->isWrapped_ForTesting() ? 2 : -1; |
| 186 | |
| 187 | check_refs(reporter, proxy.get(), 2, backingRefs, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 188 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 189 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 190 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 191 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 192 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
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 | proxy->unref(); |
| 195 | check_refs(reporter, proxy.get(), 1, 1, 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 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
| 198 | check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites); |
| 199 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |