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 |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 13 | #include "GrRenderTargetPriv.h" |
| 14 | #include "GrRenderTargetProxy.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 15 | #include "GrResourceProvider.h" |
| 16 | #include "GrSurfaceProxy.h" |
| 17 | #include "GrTextureProxy.h" |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 18 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 19 | int32_t GrIORefProxy::getProxyRefCnt_TestOnly() const { |
| 20 | return fRefCnt; |
| 21 | } |
| 22 | |
| 23 | int32_t GrIORefProxy::getBackingRefCnt_TestOnly() const { |
| 24 | if (fTarget) { |
| 25 | return fTarget->fRefCnt; |
| 26 | } |
| 27 | |
| 28 | return fRefCnt; |
| 29 | } |
| 30 | |
| 31 | int32_t GrIORefProxy::getPendingReadCnt_TestOnly() const { |
| 32 | if (fTarget) { |
| 33 | SkASSERT(!fPendingReads); |
| 34 | return fTarget->fPendingReads; |
| 35 | } |
| 36 | |
| 37 | return fPendingReads; |
| 38 | } |
| 39 | |
| 40 | int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const { |
| 41 | if (fTarget) { |
| 42 | SkASSERT(!fPendingWrites); |
| 43 | return fTarget->fPendingWrites; |
| 44 | } |
| 45 | |
| 46 | return fPendingWrites; |
| 47 | } |
| 48 | |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 49 | #ifndef SK_DISABLE_DEFERRED_PROXIES |
| 50 | |
| 51 | static const int kWidthHeight = 128; |
| 52 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 53 | static void check_refs(skiatest::Reporter* reporter, |
| 54 | GrSurfaceProxy* proxy, |
| 55 | int32_t expectedProxyRefs, |
| 56 | int32_t expectedBackingRefs, |
| 57 | int32_t expectedNumReads, |
| 58 | int32_t expectedNumWrites) { |
| 59 | REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs); |
| 60 | REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs); |
| 61 | REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads); |
| 62 | REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites); |
| 63 | |
| 64 | SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs); |
| 65 | SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs); |
| 66 | SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads); |
| 67 | SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites); |
| 68 | } |
| 69 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 70 | static sk_sp<GrSurfaceProxy> make_deferred(const GrCaps& caps, GrResourceProvider* provider) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 71 | GrSurfaceDesc desc; |
| 72 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 73 | desc.fWidth = kWidthHeight; |
| 74 | desc.fHeight = kWidthHeight; |
| 75 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 76 | |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 77 | return GrSurfaceProxy::MakeDeferred(provider, caps, desc, |
| 78 | SkBackingFit::kApprox, SkBudgeted::kYes); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 81 | static sk_sp<GrSurfaceProxy> make_wrapped(const GrCaps& caps, GrResourceProvider* provider) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 82 | GrSurfaceDesc desc; |
| 83 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 84 | desc.fWidth = kWidthHeight; |
| 85 | desc.fHeight = kWidthHeight; |
| 86 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 87 | |
| 88 | sk_sp<GrTexture> tex(provider->createTexture(desc, SkBudgeted::kNo)); |
| 89 | |
| 90 | // Flush the IOWrite from the initial discard or it will confuse the later ref count checks |
| 91 | tex->flushWrites(); |
| 92 | |
| 93 | return GrSurfaceProxy::MakeWrapped(std::move(tex)); |
| 94 | } |
| 95 | |
| 96 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 97 | GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 98 | const GrCaps& caps = *ctxInfo.grContext()->caps(); |
| 99 | |
Brian Salomon | cdcc33f | 2017-02-21 09:49:20 -0500 | [diff] [blame] | 100 | // Currently the op itself takes a pending write and the render target op list does as well. |
| 101 | static const int kWritesForDiscard = 2; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 102 | for (auto make : { make_deferred, make_wrapped }) { |
| 103 | // A single write |
| 104 | { |
| 105 | sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider)); |
| 106 | |
| 107 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get()); |
| 108 | |
| 109 | check_refs(reporter, sProxy.get(), 1, 1, 0, 1); |
| 110 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 111 | // In the deferred case, the discard op created on instantiation adds an |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 112 | // extra ref and write |
| 113 | bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() && |
| 114 | caps.discardRenderTargetSupport(); |
Brian Salomon | cdcc33f | 2017-02-21 09:49:20 -0500 | [diff] [blame] | 115 | int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 116 | |
| 117 | sProxy->instantiate(provider); |
| 118 | |
| 119 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 120 | check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites); |
| 121 | } |
| 122 | |
| 123 | // A single read |
| 124 | { |
| 125 | sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider)); |
| 126 | |
| 127 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get()); |
| 128 | |
| 129 | check_refs(reporter, sProxy.get(), 1, 1, 1, 0); |
| 130 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 131 | // In the deferred case, the discard op created on instantiation adds an |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 132 | // extra ref and write |
| 133 | bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() && |
| 134 | caps.discardRenderTargetSupport(); |
Brian Salomon | cdcc33f | 2017-02-21 09:49:20 -0500 | [diff] [blame] | 135 | int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 136 | |
| 137 | sProxy->instantiate(provider); |
| 138 | |
| 139 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 140 | check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites); |
| 141 | } |
| 142 | |
| 143 | // A single read/write pair |
| 144 | { |
| 145 | sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider)); |
| 146 | |
| 147 | GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(sProxy.get()); |
| 148 | |
| 149 | check_refs(reporter, sProxy.get(), 1, 1, 1, 1); |
| 150 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 151 | // In the deferred case, the discard op created on instantiation adds an |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 152 | // extra ref and write |
| 153 | bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() && |
| 154 | caps.discardRenderTargetSupport(); |
Brian Salomon | cdcc33f | 2017-02-21 09:49:20 -0500 | [diff] [blame] | 155 | int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 156 | |
| 157 | sProxy->instantiate(provider); |
| 158 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 159 | // In the deferred case, this checks that the refs transferred to the GrSurface |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 160 | check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites); |
| 161 | } |
| 162 | |
| 163 | // Multiple normal refs |
| 164 | { |
| 165 | sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider)); |
| 166 | sProxy->ref(); |
| 167 | sProxy->ref(); |
| 168 | |
| 169 | check_refs(reporter, sProxy.get(), 3, 3, 0, 0); |
| 170 | |
| 171 | bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() && |
| 172 | caps.discardRenderTargetSupport(); |
Brian Salomon | cdcc33f | 2017-02-21 09:49:20 -0500 | [diff] [blame] | 173 | int expectedWrites = proxyGetsDiscardRef ? kWritesForDiscard : 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 174 | |
| 175 | sProxy->instantiate(provider); |
| 176 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 177 | // In the deferred case, this checks that the refs transferred to the GrSurface |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 178 | check_refs(reporter, sProxy.get(), 3, 3, 0, expectedWrites); |
| 179 | |
| 180 | sProxy->unref(); |
| 181 | sProxy->unref(); |
| 182 | } |
| 183 | |
| 184 | // Continue using (reffing) proxy after instantiation |
| 185 | { |
| 186 | sk_sp<GrSurfaceProxy> sProxy((*make)(caps, provider)); |
| 187 | sProxy->ref(); |
| 188 | |
| 189 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(sProxy.get()); |
| 190 | |
| 191 | check_refs(reporter, sProxy.get(), 2, 2, 0, 1); |
| 192 | |
| 193 | bool proxyGetsDiscardRef = !sProxy->isWrapped_ForTesting() && |
| 194 | caps.discardRenderTargetSupport(); |
Brian Salomon | cdcc33f | 2017-02-21 09:49:20 -0500 | [diff] [blame] | 195 | int expectedWrites = 1 + (proxyGetsDiscardRef ? kWritesForDiscard : 0); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 196 | |
| 197 | sProxy->instantiate(provider); |
| 198 | |
| 199 | // In the deferred case, this checks that the refs transfered to the GrSurface |
| 200 | check_refs(reporter, sProxy.get(), 2, 2, 0, expectedWrites); |
| 201 | |
| 202 | sProxy->unref(); |
| 203 | check_refs(reporter, sProxy.get(), 1, 1, 0, expectedWrites); |
| 204 | |
| 205 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(sProxy.get()); |
| 206 | check_refs(reporter, sProxy.get(), 1, 1, 1, expectedWrites); |
| 207 | } |
| 208 | } |
| 209 | } |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 210 | #endif |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 211 | |
| 212 | #endif |