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 | |
| 12 | #if SK_SUPPORT_GPU |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 14 | #include "GrGpuResourceRef.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 15 | #include "GrProxyProvider.h" |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 16 | #include "GrRenderTargetProxy.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
| 18 | #include "GrSurfaceProxy.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 19 | #include "GrTexture.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 20 | #include "GrTextureProxy.h" |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 21 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 22 | int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const { |
| 23 | return fRefCnt; |
| 24 | } |
| 25 | |
| 26 | int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const { |
| 27 | if (fTarget) { |
| 28 | return fTarget->fRefCnt; |
| 29 | } |
| 30 | |
| 31 | return fRefCnt; |
| 32 | } |
| 33 | |
| 34 | int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const { |
| 35 | if (fTarget) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 36 | return fTarget->fPendingReads; |
| 37 | } |
| 38 | |
| 39 | return fPendingReads; |
| 40 | } |
| 41 | |
| 42 | int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const { |
| 43 | if (fTarget) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 44 | return fTarget->fPendingWrites; |
| 45 | } |
| 46 | |
| 47 | return fPendingWrites; |
| 48 | } |
| 49 | |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 50 | static const int kWidthHeight = 128; |
| 51 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 52 | static void check_refs(skiatest::Reporter* reporter, |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 53 | GrTextureProxy* proxy, |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 54 | int32_t expectedProxyRefs, |
| 55 | int32_t expectedBackingRefs, |
| 56 | int32_t expectedNumReads, |
| 57 | int32_t expectedNumWrites) { |
| 58 | REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs); |
| 59 | REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs); |
| 60 | REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads); |
| 61 | REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites); |
| 62 | |
| 63 | SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs); |
| 64 | SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs); |
| 65 | SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads); |
| 66 | SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites); |
| 67 | } |
| 68 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 69 | static sk_sp<GrTextureProxy> make_deferred(GrProxyProvider* proxyProvider) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 70 | GrSurfaceDesc desc; |
| 71 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 72 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 73 | desc.fWidth = kWidthHeight; |
| 74 | desc.fHeight = kWidthHeight; |
| 75 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 76 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 77 | return proxyProvider->createProxy(desc, SkBackingFit::kApprox, SkBudgeted::kYes, |
| 78 | GrResourceProvider::kNoPendingIO_Flag); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 81 | static sk_sp<GrTextureProxy> make_wrapped(GrProxyProvider* proxyProvider) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 82 | GrSurfaceDesc desc; |
| 83 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 84 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 85 | desc.fWidth = kWidthHeight; |
| 86 | desc.fHeight = kWidthHeight; |
| 87 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 88 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 89 | return proxyProvider->createInstantiatedProxy(desc, SkBackingFit::kExact, SkBudgeted::kNo); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 93 | GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider(); |
| 94 | GrResourceProvider* resourceProvider = ctxInfo.grContext()->resourceProvider(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 95 | |
| 96 | for (auto make : { make_deferred, make_wrapped }) { |
| 97 | // A single write |
| 98 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 99 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 100 | if (proxy.get()) { |
| 101 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 102 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 103 | static const int kExpectedReads = 0; |
| 104 | static const int kExpectedWrites = 1; |
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 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 107 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 108 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 109 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 110 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 111 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
| 112 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // A single read |
| 116 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 117 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 118 | if (proxy.get()) { |
| 119 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 120 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 121 | static const int kExpectedReads = 1; |
| 122 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 123 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 124 | check_refs(reporter, proxy.get(), 1, 1, 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 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 135 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
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 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 142 | check_refs(reporter, proxy.get(), 1, 1, 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 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 161 | check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 162 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 163 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 164 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 165 | // In the deferred case, this checks that the refs transferred to the GrSurface |
| 166 | check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 167 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 168 | proxy->unref(); |
| 169 | proxy->unref(); |
| 170 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Continue using (reffing) proxy after instantiation |
| 174 | { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 175 | sk_sp<GrTextureProxy> proxy((*make)(proxyProvider)); |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 176 | if (proxy.get()) { |
| 177 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 178 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 179 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
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 | static const int kExpectedWrites = 1; |
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 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 184 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame^] | 185 | proxy->instantiate(resourceProvider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 186 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 187 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 188 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 189 | |
Jim Van Verth | 311cc6b | 2017-09-20 17:51:59 -0400 | [diff] [blame] | 190 | proxy->unref(); |
| 191 | check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites); |
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 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
| 194 | check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites); |
| 195 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
Robert Phillips | 123b7b8 | 2017-04-11 09:09:24 -0400 | [diff] [blame] | 199 | |
| 200 | #endif |