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