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" |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 10 | #include "GrGeometryProcessor.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 11 | #include "GrInvariantOutput.h" |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 12 | #include "GrMemoryPool.h" |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 13 | #include "GrTextureParams.h" |
| 14 | #include "GrTexturePriv.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 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 18 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 19 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 20 | class GrFragmentProcessor; |
| 21 | class GrGeometryProcessor; |
| 22 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 23 | /* |
| 24 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 25 | * problems on android. |
| 26 | */ |
| 27 | template<> |
| 28 | SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>* |
| 29 | GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() { |
| 30 | static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories; |
| 31 | return &gFactories; |
| 32 | } |
| 33 | |
| 34 | template<> |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 35 | SkTArray<GrProcessorTestFactory<GrXPFactory>*, true>* |
| 36 | GrProcessorTestFactory<GrXPFactory>::GetFactories() { |
| 37 | static SkTArray<GrProcessorTestFactory<GrXPFactory>*, true> gFactories; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 38 | return &gFactories; |
| 39 | } |
| 40 | |
| 41 | template<> |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 42 | SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>* |
| 43 | GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() { |
| 44 | static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories; |
| 45 | return &gFactories; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * To ensure we always have successful static initialization, before creating from the factories |
| 50 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 51 | * manually adjusted. |
| 52 | */ |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 53 | static const int kFPFactoryCount = 40; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 54 | static const int kGPFactoryCount = 14; |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 55 | static const int kXPFactoryCount = 5; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 56 | |
| 57 | template<> |
| 58 | void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() { |
| 59 | if (kFPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 60 | SkDebugf("\nExpected %d fragment processor factories, found %d.\n", |
| 61 | kFPFactoryCount, GetFactories()->count()); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 62 | SkFAIL("Wrong number of fragment processor factories!"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | template<> |
| 67 | void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() { |
| 68 | if (kGPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 69 | SkDebugf("\nExpected %d geometry processor factories, found %d.\n", |
| 70 | kGPFactoryCount, GetFactories()->count()); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 71 | SkFAIL("Wrong number of geometry processor factories!"); |
| 72 | } |
| 73 | } |
| 74 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 75 | template<> |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 76 | void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 77 | if (kXPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 78 | SkDebugf("\nExpected %d xp factory factories, found %d.\n", |
| 79 | kXPFactoryCount, GetFactories()->count()); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 80 | SkFAIL("Wrong number of xp factory factories!"); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 86 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 87 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 88 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 89 | // 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] | 90 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 91 | namespace { |
mtklein | 15923c9 | 2016-02-29 10:14:38 -0800 | [diff] [blame] | 92 | static SkSpinlock gProcessorSpinlock; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 93 | class MemoryPoolAccessor { |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 94 | public: |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 95 | |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 96 | // We know in the Android framework there is only one GrContext. |
| 97 | #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 98 | MemoryPoolAccessor() {} |
| 99 | ~MemoryPoolAccessor() {} |
| 100 | #else |
| 101 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 102 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 103 | #endif |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 104 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 105 | GrMemoryPool* pool() const { |
| 106 | static GrMemoryPool gPool(4096, 4096); |
| 107 | return &gPool; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 108 | } |
| 109 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 110 | } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 111 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 112 | int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 113 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 116 | GrProcessor::~GrProcessor() {} |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 117 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 118 | void GrProcessor::addTextureSampler(const TextureSampler* access) { |
| 119 | fTextureSamplers.push_back(access); |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 120 | this->addGpuResource(access->getProgramTexture()); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 121 | } |
| 122 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 123 | void GrProcessor::addBufferAccess(const GrBufferAccess* access) { |
| 124 | fBufferAccesses.push_back(access); |
| 125 | this->addGpuResource(access->getProgramBuffer()); |
| 126 | } |
| 127 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 128 | void* GrProcessor::operator new(size_t size) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 129 | return MemoryPoolAccessor().pool()->allocate(size); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 130 | } |
| 131 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 132 | void GrProcessor::operator delete(void* target) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 133 | return MemoryPoolAccessor().pool()->release(target); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 134 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 135 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 136 | bool GrProcessor::hasSameSamplers(const GrProcessor& that) const { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 137 | if (this->numTextureSamplers() != that.numTextureSamplers() || |
| 138 | this->numBuffers() != that.numBuffers()) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 139 | return false; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 140 | } |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 141 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 142 | if (this->textureSampler(i) != that.textureSampler(i)) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 146 | for (int i = 0; i < this->numBuffers(); ++i) { |
| 147 | if (this->bufferAccess(i) != that.bufferAccess(i)) { |
| 148 | return false; |
| 149 | } |
| 150 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 151 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 152 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 153 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 154 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 155 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame^] | 156 | GrProcessor::TextureSampler::TextureSampler() {} |
| 157 | |
| 158 | GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, const GrTextureParams& params) { |
| 159 | this->reset(texture, params); |
| 160 | } |
| 161 | |
| 162 | GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, |
| 163 | GrTextureParams::FilterMode filterMode, |
| 164 | SkShader::TileMode tileXAndY, |
| 165 | GrShaderFlags visibility) { |
| 166 | this->reset(texture, filterMode, tileXAndY, visibility); |
| 167 | } |
| 168 | |
| 169 | void GrProcessor::TextureSampler::reset(GrTexture* texture, |
| 170 | const GrTextureParams& params, |
| 171 | GrShaderFlags visibility) { |
| 172 | SkASSERT(texture); |
| 173 | fTexture.set(SkRef(texture), kRead_GrIOType); |
| 174 | fParams = params; |
| 175 | fParams.setFilterMode(SkTMin(params.filterMode(), texture->texturePriv().highestFilterMode())); |
| 176 | fVisibility = visibility; |
| 177 | } |
| 178 | |
| 179 | void GrProcessor::TextureSampler::reset(GrTexture* texture, |
| 180 | GrTextureParams::FilterMode filterMode, |
| 181 | SkShader::TileMode tileXAndY, |
| 182 | GrShaderFlags visibility) { |
| 183 | SkASSERT(texture); |
| 184 | fTexture.set(SkRef(texture), kRead_GrIOType); |
| 185 | filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode()); |
| 186 | fParams.reset(tileXAndY, filterMode); |
| 187 | fVisibility = visibility; |
| 188 | } |
| 189 | |
| 190 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 191 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 192 | // Initial static variable from GrXPFactory |
| 193 | int32_t GrXPFactory::gCurrXPFClassID = |
| 194 | GrXPFactory::kIllegalXPFClassID; |