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