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 | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 13 | #include "GrSamplerParams.h" |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 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); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 120 | this->addGpuResource(access->programTexture()); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Brian Salomon | b014cca | 2016-11-18 11:39:15 -0500 | [diff] [blame] | 123 | void GrProcessor::addBufferAccess(const BufferAccess* access) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 124 | fBufferAccesses.push_back(access); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 125 | this->addGpuResource(access->programBuffer()); |
| 126 | } |
| 127 | |
| 128 | void GrProcessor::addImageStorageAccess(const ImageStorageAccess* access) { |
| 129 | fImageStorageAccesses.push_back(access); |
| 130 | this->addGpuResource(access->programTexture()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 131 | } |
| 132 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 133 | void* GrProcessor::operator new(size_t size) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 134 | return MemoryPoolAccessor().pool()->allocate(size); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 135 | } |
| 136 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 137 | void GrProcessor::operator delete(void* target) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 138 | return MemoryPoolAccessor().pool()->release(target); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 139 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 140 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 141 | bool GrProcessor::hasSameSamplersAndAccesses(const GrProcessor &that) const { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 142 | if (this->numTextureSamplers() != that.numTextureSamplers() || |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 143 | this->numBuffers() != that.numBuffers() || |
| 144 | this->numImageStorages() != that.numImageStorages()) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 145 | return false; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 146 | } |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 147 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 148 | if (this->textureSampler(i) != that.textureSampler(i)) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 152 | for (int i = 0; i < this->numBuffers(); ++i) { |
| 153 | if (this->bufferAccess(i) != that.bufferAccess(i)) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 157 | for (int i = 0; i < this->numImageStorages(); ++i) { |
| 158 | if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) { |
| 159 | return false; |
| 160 | } |
| 161 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 162 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 163 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 164 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 165 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 166 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 167 | GrProcessor::TextureSampler::TextureSampler() {} |
| 168 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 169 | GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, const GrSamplerParams& params) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 170 | this->reset(texture, params); |
| 171 | } |
| 172 | |
| 173 | GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 174 | GrSamplerParams::FilterMode filterMode, |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 175 | SkShader::TileMode tileXAndY, |
| 176 | GrShaderFlags visibility) { |
| 177 | this->reset(texture, filterMode, tileXAndY, visibility); |
| 178 | } |
| 179 | |
| 180 | void GrProcessor::TextureSampler::reset(GrTexture* texture, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 181 | const GrSamplerParams& params, |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 182 | GrShaderFlags visibility) { |
| 183 | SkASSERT(texture); |
| 184 | fTexture.set(SkRef(texture), kRead_GrIOType); |
| 185 | fParams = params; |
| 186 | fParams.setFilterMode(SkTMin(params.filterMode(), texture->texturePriv().highestFilterMode())); |
| 187 | fVisibility = visibility; |
| 188 | } |
| 189 | |
| 190 | void GrProcessor::TextureSampler::reset(GrTexture* texture, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 191 | GrSamplerParams::FilterMode filterMode, |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 192 | SkShader::TileMode tileXAndY, |
| 193 | GrShaderFlags visibility) { |
| 194 | SkASSERT(texture); |
| 195 | fTexture.set(SkRef(texture), kRead_GrIOType); |
| 196 | filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode()); |
| 197 | fParams.reset(tileXAndY, filterMode); |
| 198 | fVisibility = visibility; |
| 199 | } |
| 200 | |
| 201 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 202 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 203 | GrProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, |
| 204 | GrSLMemoryModel memoryModel, |
| 205 | GrSLRestrict restrict, |
| 206 | GrShaderFlags visibility) { |
| 207 | SkASSERT(texture); |
| 208 | fTexture.set(texture.release(), ioType); |
| 209 | fMemoryModel = memoryModel; |
| 210 | fRestrict = restrict; |
| 211 | fVisibility = visibility; |
| 212 | // We currently infer this from the config. However, we could allow the client to specify |
| 213 | // a format that is different but compatible with the config. |
| 214 | switch (fTexture.get()->config()) { |
| 215 | case kRGBA_8888_GrPixelConfig: |
| 216 | fFormat = GrImageStorageFormat::kRGBA8; |
| 217 | break; |
| 218 | case kRGBA_8888_sint_GrPixelConfig: |
| 219 | fFormat = GrImageStorageFormat::kRGBA8i; |
| 220 | break; |
| 221 | case kRGBA_half_GrPixelConfig: |
| 222 | fFormat = GrImageStorageFormat::kRGBA16f; |
| 223 | break; |
| 224 | case kRGBA_float_GrPixelConfig: |
| 225 | fFormat = GrImageStorageFormat::kRGBA32f; |
| 226 | break; |
| 227 | default: |
| 228 | SkFAIL("Config is not (yet) supported as image storage."); |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 234 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 235 | // Initial static variable from GrXPFactory |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 236 | int32_t GrXPFactory::gCurrXPFClassID = GrXPFactory::kIllegalXPFClassID; |