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