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 "GrRenderTargetProxy.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 15 | #include "GrResourceProvider.h" |
| 16 | #include "GrSurfaceProxy.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame^] | 17 | #include "GrTexture.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 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) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 34 | return fTarget->fPendingReads; |
| 35 | } |
| 36 | |
| 37 | return fPendingReads; |
| 38 | } |
| 39 | |
| 40 | int32_t GrIORefProxy::getPendingWriteCnt_TestOnly() const { |
| 41 | if (fTarget) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 42 | return fTarget->fPendingWrites; |
| 43 | } |
| 44 | |
| 45 | return fPendingWrites; |
| 46 | } |
| 47 | |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 48 | static const int kWidthHeight = 128; |
| 49 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 50 | static void check_refs(skiatest::Reporter* reporter, |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 51 | GrTextureProxy* proxy, |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 52 | int32_t expectedProxyRefs, |
| 53 | int32_t expectedBackingRefs, |
| 54 | int32_t expectedNumReads, |
| 55 | int32_t expectedNumWrites) { |
| 56 | REPORTER_ASSERT(reporter, proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs); |
| 57 | REPORTER_ASSERT(reporter, proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs); |
| 58 | REPORTER_ASSERT(reporter, proxy->getPendingReadCnt_TestOnly() == expectedNumReads); |
| 59 | REPORTER_ASSERT(reporter, proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites); |
| 60 | |
| 61 | SkASSERT(proxy->getProxyRefCnt_TestOnly() == expectedProxyRefs); |
| 62 | SkASSERT(proxy->getBackingRefCnt_TestOnly() == expectedBackingRefs); |
| 63 | SkASSERT(proxy->getPendingReadCnt_TestOnly() == expectedNumReads); |
| 64 | SkASSERT(proxy->getPendingWriteCnt_TestOnly() == expectedNumWrites); |
| 65 | } |
| 66 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 67 | static sk_sp<GrTextureProxy> make_deferred(GrContext* context) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 68 | GrSurfaceDesc desc; |
| 69 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 70 | desc.fWidth = kWidthHeight; |
| 71 | desc.fHeight = kWidthHeight; |
| 72 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 73 | |
Robert Phillips | 1ec1faa | 2017-03-28 16:21:27 -0400 | [diff] [blame] | 74 | return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, |
Robert Phillips | f5442bb | 2017-04-17 14:18:34 -0400 | [diff] [blame] | 75 | SkBackingFit::kApprox, SkBudgeted::kYes, |
| 76 | GrResourceProvider::kNoPendingIO_Flag); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 79 | static sk_sp<GrTextureProxy> 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 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 88 | return GrSurfaceProxy::MakeWrapped(std::move(tex)); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProxyRefTest, reporter, ctxInfo) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 92 | GrResourceProvider* provider = ctxInfo.grContext()->resourceProvider(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 93 | |
| 94 | for (auto make : { make_deferred, make_wrapped }) { |
| 95 | // A single write |
| 96 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 97 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 98 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 99 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 100 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 101 | static const int kExpectedReads = 0; |
| 102 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 103 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 104 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 105 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 106 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 107 | |
| 108 | // 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] | 109 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // A single read |
| 113 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 114 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 115 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 116 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 117 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 118 | static const int kExpectedReads = 1; |
| 119 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 120 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 121 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 122 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 123 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 124 | |
| 125 | // 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] | 126 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // A single read/write pair |
| 130 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 131 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 132 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 133 | GrPendingIOResource<GrSurfaceProxy, kRW_GrIOType> fRW(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 134 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 135 | static const int kExpectedReads = 1; |
| 136 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 137 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 138 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 139 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 140 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 141 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 142 | // 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] | 143 | check_refs(reporter, proxy.get(), 1, 1, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Multiple normal refs |
| 147 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 148 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
| 149 | proxy->ref(); |
| 150 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 151 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 152 | static const int kExpectedReads = 0; |
| 153 | static const int kExpectedWrites = 0; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 154 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 155 | check_refs(reporter, proxy.get(), 3, 3,kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 156 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 157 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 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 |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 160 | check_refs(reporter, proxy.get(), 3, 3, kExpectedReads, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 161 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 162 | proxy->unref(); |
| 163 | proxy->unref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Continue using (reffing) proxy after instantiation |
| 167 | { |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 168 | sk_sp<GrTextureProxy> proxy((*make)(ctxInfo.grContext())); |
| 169 | proxy->ref(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 170 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 171 | GrPendingIOResource<GrSurfaceProxy, kWrite_GrIOType> fWrite(proxy.get()); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 172 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 173 | static const int kExpectedWrites = 1; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 174 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 175 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 176 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 177 | proxy->instantiate(provider); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 178 | |
| 179 | // 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] | 180 | check_refs(reporter, proxy.get(), 2, 2, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 181 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 182 | proxy->unref(); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 183 | check_refs(reporter, proxy.get(), 1, 1, 0, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 184 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 185 | GrPendingIOResource<GrSurfaceProxy, kRead_GrIOType> fRead(proxy.get()); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 186 | check_refs(reporter, proxy.get(), 1, 1, 1, kExpectedWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
Robert Phillips | 123b7b8 | 2017-04-11 09:09:24 -0400 | [diff] [blame] | 190 | |
| 191 | #endif |