tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 8 | #include "GrProcessor.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 11 | #include "GrGeometryProcessor.h" |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 12 | #include "GrMemoryPool.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 13 | #include "GrSamplerState.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 14 | #include "GrTextureProxy.h" |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 15 | #include "GrXferProcessor.h" |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 16 | #include "SkSpinlock.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 17 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 18 | #if GR_TEST_UTILS |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 19 | |
| 20 | GrResourceProvider* GrProcessorTestData::resourceProvider() { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 21 | return fContext->contextPriv().resourceProvider(); |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 22 | } |
| 23 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 24 | GrProxyProvider* GrProcessorTestData::proxyProvider() { |
| 25 | return fContext->contextPriv().proxyProvider(); |
| 26 | } |
| 27 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 28 | const GrCaps* GrProcessorTestData::caps() { |
| 29 | return fContext->caps(); |
| 30 | } |
| 31 | |
| 32 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 33 | class GrFragmentProcessor; |
| 34 | class GrGeometryProcessor; |
| 35 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 38 | * problems on android. |
| 39 | */ |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 40 | template <> |
| 41 | SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() { |
| 42 | static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 43 | return &gFactories; |
| 44 | } |
| 45 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 46 | template <> |
| 47 | SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() { |
| 48 | static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories; |
Brian Salomon | a8f80de | 2017-01-07 09:37:13 -0500 | [diff] [blame] | 49 | return &gFactories; |
| 50 | } |
| 51 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 52 | SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() { |
| 53 | static SkTArray<GrXPFactoryTestFactory*, true> gFactories; |
| 54 | return &gFactories; |
| 55 | } |
| 56 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 57 | /* |
| 58 | * To ensure we always have successful static initialization, before creating from the factories |
| 59 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 60 | * manually adjusted. |
| 61 | */ |
Yuqian Li | 915817b | 2017-12-14 13:45:08 -0500 | [diff] [blame] | 62 | static const int kFPFactoryCount = 38; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 63 | static const int kGPFactoryCount = 14; |
Brian Salomon | 1c4717b | 2017-01-06 12:54:58 -0500 | [diff] [blame] | 64 | static const int kXPFactoryCount = 4; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 65 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 66 | template <> |
| 67 | void GrFragmentProcessorTestFactory::VerifyFactoryCount() { |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 68 | if (kFPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 69 | SkDebugf("\nExpected %d fragment processor factories, found %d.\n", |
| 70 | kFPFactoryCount, GetFactories()->count()); |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 71 | SK_ABORT("Wrong number of fragment processor factories!"); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 75 | template <> |
| 76 | void GrGeometryProcessorTestFactory::VerifyFactoryCount() { |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 77 | if (kGPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 78 | SkDebugf("\nExpected %d geometry processor factories, found %d.\n", |
| 79 | kGPFactoryCount, GetFactories()->count()); |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 80 | SK_ABORT("Wrong number of geometry processor factories!"); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 84 | void GrXPFactoryTestFactory::VerifyFactoryCount() { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 85 | if (kXPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 86 | SkDebugf("\nExpected %d xp factory factories, found %d.\n", |
| 87 | kXPFactoryCount, GetFactories()->count()); |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 88 | SK_ABORT("Wrong number of xp factory factories!"); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 92 | #endif |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 93 | #endif |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 94 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 95 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 96 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 97 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 98 | // memory barrier between accesses of a context on different threads. Also, there may be multiple |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 99 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 100 | namespace { |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 101 | #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
mtklein | 15923c9 | 2016-02-29 10:14:38 -0800 | [diff] [blame] | 102 | static SkSpinlock gProcessorSpinlock; |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 103 | #endif |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 104 | class MemoryPoolAccessor { |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 105 | public: |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 106 | |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 107 | // We know in the Android framework there is only one GrContext. |
| 108 | #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 109 | MemoryPoolAccessor() {} |
| 110 | ~MemoryPoolAccessor() {} |
| 111 | #else |
| 112 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 113 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 114 | #endif |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 115 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 116 | GrMemoryPool* pool() const { |
| 117 | static GrMemoryPool gPool(4096, 4096); |
| 118 | return &gPool; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 119 | } |
| 120 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 121 | } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 122 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 125 | void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 126 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 127 | void GrProcessor::operator delete(void* target) { |
| 128 | return MemoryPoolAccessor().pool()->release(target); |
| 129 | } |
| 130 | |
| 131 | /////////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | void GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 134 | fTextureSamplers.push_back(access); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 137 | void GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 138 | fBufferAccesses.push_back(access); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 141 | void GrResourceIOProcessor::addPendingIOs() const { |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 142 | for (const auto& sampler : fTextureSamplers) { |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 143 | sampler->programProxy()->markPendingIO(); |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 144 | } |
| 145 | for (const auto& buffer : fBufferAccesses) { |
| 146 | buffer->programBuffer()->markPendingIO(); |
| 147 | } |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 150 | void GrResourceIOProcessor::removeRefs() const { |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 151 | for (const auto& sampler : fTextureSamplers) { |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 152 | sampler->programProxy()->removeRef(); |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 153 | } |
| 154 | for (const auto& buffer : fBufferAccesses) { |
| 155 | buffer->programBuffer()->removeRef(); |
| 156 | } |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 159 | void GrResourceIOProcessor::pendingIOComplete() const { |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 160 | for (const auto& sampler : fTextureSamplers) { |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 161 | sampler->programProxy()->pendingIOComplete(); |
Brian Salomon | e57194f | 2017-01-09 15:30:02 -0500 | [diff] [blame] | 162 | } |
| 163 | for (const auto& buffer : fBufferAccesses) { |
| 164 | buffer->programBuffer()->pendingIOComplete(); |
| 165 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 168 | bool GrResourceIOProcessor::instantiate(GrResourceProvider* resourceProvider) const { |
| 169 | for (const auto& sampler : fTextureSamplers) { |
| 170 | if (!sampler->instantiate(resourceProvider)) { |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 175 | // MDB TODO: instantiate 'fBufferAccesses' here as well |
| 176 | |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 180 | bool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 181 | if (this->numTextureSamplers() != that.numTextureSamplers() || |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 182 | this->numBuffers() != that.numBuffers()) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 183 | return false; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 184 | } |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 185 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 186 | if (this->textureSampler(i) != that.textureSampler(i)) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 190 | for (int i = 0; i < this->numBuffers(); ++i) { |
| 191 | if (this->bufferAccess(i) != that.bufferAccess(i)) { |
| 192 | return false; |
| 193 | } |
| 194 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 195 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 196 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 197 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 198 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 199 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 200 | GrResourceIOProcessor::TextureSampler::TextureSampler() {} |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 201 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 202 | GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 203 | const GrSamplerState& samplerState) { |
| 204 | this->reset(std::move(proxy), samplerState); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 207 | GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 208 | GrSamplerState::Filter filterMode, |
| 209 | GrSamplerState::WrapMode wrapXAndY, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 210 | GrShaderFlags visibility) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 211 | this->reset(std::move(proxy), filterMode, wrapXAndY, visibility); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 214 | void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 215 | const GrSamplerState& samplerState, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 216 | GrShaderFlags visibility) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 217 | fSamplerState = samplerState; |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 218 | fProxyRef.setProxy(std::move(proxy), kRead_GrIOType); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 219 | fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode())); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 220 | fVisibility = visibility; |
| 221 | } |
| 222 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 223 | void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 224 | GrSamplerState::Filter filterMode, |
| 225 | GrSamplerState::WrapMode wrapXAndY, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 226 | GrShaderFlags visibility) { |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 227 | fProxyRef.setProxy(std::move(proxy), kRead_GrIOType); |
| 228 | filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode()); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 229 | fSamplerState = GrSamplerState(wrapXAndY, filterMode); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 230 | fVisibility = visibility; |
| 231 | } |