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