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 | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 89 | return GrSurfaceProxy::MakeWrapped(std::move(tex)); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 93 | GrResourceProvider* provider = ctxInfo.grContext()->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 | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 98 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 99 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 100 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 101 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -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 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -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 | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 107 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 108 | |
| 109 | // In the deferred case, this checks that the refs transfered to the GrSurface |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 110 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // A single read |
| 114 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 115 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 116 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 117 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 118 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 119 | static const int kExpectedReads = 1; |
| 120 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 121 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 122 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 123 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 124 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 125 | |
| 126 | // In the deferred case, this checks that the refs transfered to the GrSurface |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 127 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // A single read/write pair |
| 131 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 132 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 133 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 134 | GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 135 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 136 | static const int kExpectedReads = 1; |
| 137 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 138 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 139 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 140 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 141 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 142 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 143 | // In the deferred case, this checks that the refs transferred to the GrSurface |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 144 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Multiple normal refs |
| 148 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 149 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
| 150 | proxy->ref(); |
| 151 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 152 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 153 | static const int kExpectedReads = 0; |
| 154 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 155 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 156 | check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites); |
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 | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 161 | check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 162 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 163 | proxy->unref(); |
| 164 | proxy->unref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Continue using (reffing) proxy after instantiation |
| 168 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 169 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
| 170 | proxy->ref(); |
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 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 173 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 174 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 175 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 176 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 177 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 178 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 179 | |
| 180 | // In the deferred case, this checks that the refs transfered to the GrSurface |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 181 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 182 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 183 | proxy->unref(); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 184 | check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 185 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 186 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame^] | 187 | check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 191 | #endif |