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<> |
| 35 | SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>* |
| 36 | GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() { |
| 37 | static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories; |
| 38 | return &gFactories; |
| 39 | } |
| 40 | |
Brian Salomon | a8f80de | 2017-01-07 09:37:13 -0500 | [diff] [blame^] | 41 | SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() { |
| 42 | static SkTArray<GrXPFactoryTestFactory*, true> gFactories; |
| 43 | return &gFactories; |
| 44 | } |
| 45 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 46 | /* |
| 47 | * To ensure we always have successful static initialization, before creating from the factories |
| 48 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 49 | * manually adjusted. |
| 50 | */ |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 51 | static const int kFPFactoryCount = 40; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 52 | static const int kGPFactoryCount = 14; |
Brian Salomon | 1c4717b | 2017-01-06 12:54:58 -0500 | [diff] [blame] | 53 | static const int kXPFactoryCount = 4; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 54 | |
| 55 | template<> |
| 56 | void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() { |
| 57 | if (kFPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 58 | SkDebugf("\nExpected %d fragment processor factories, found %d.\n", |
| 59 | kFPFactoryCount, GetFactories()->count()); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 60 | SkFAIL("Wrong number of fragment processor factories!"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | template<> |
| 65 | void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() { |
| 66 | if (kGPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 67 | SkDebugf("\nExpected %d geometry processor factories, found %d.\n", |
| 68 | kGPFactoryCount, GetFactories()->count()); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 69 | SkFAIL("Wrong number of geometry processor factories!"); |
| 70 | } |
| 71 | } |
| 72 | |
Brian Salomon | a8f80de | 2017-01-07 09:37:13 -0500 | [diff] [blame^] | 73 | void GrXPFactoryTestFactory::VerifyFactoryCount() { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 74 | if (kXPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 75 | SkDebugf("\nExpected %d xp factory factories, found %d.\n", |
| 76 | kXPFactoryCount, GetFactories()->count()); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 77 | SkFAIL("Wrong number of xp factory factories!"); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 81 | #endif |
| 82 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 83 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 84 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 85 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 86 | // 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] | 87 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 88 | namespace { |
mtklein | 15923c9 | 2016-02-29 10:14:38 -0800 | [diff] [blame] | 89 | static SkSpinlock gProcessorSpinlock; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 90 | class MemoryPoolAccessor { |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 91 | public: |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 92 | |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 93 | // We know in the Android framework there is only one GrContext. |
| 94 | #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 95 | MemoryPoolAccessor() {} |
| 96 | ~MemoryPoolAccessor() {} |
| 97 | #else |
| 98 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 99 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 100 | #endif |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 101 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 102 | GrMemoryPool* pool() const { |
| 103 | static GrMemoryPool gPool(4096, 4096); |
| 104 | return &gPool; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 105 | } |
| 106 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 107 | } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 108 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 109 | int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 110 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 113 | GrProcessor::~GrProcessor() {} |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 114 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 115 | void GrProcessor::addTextureSampler(const TextureSampler* access) { |
| 116 | fTextureSamplers.push_back(access); |
Brian Salomon | db4183d | 2016-11-17 12:48:40 -0500 | [diff] [blame] | 117 | this->addGpuResource(access->programTexture()); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Brian Salomon | b014cca | 2016-11-18 11:39:15 -0500 | [diff] [blame] | 120 | void GrProcessor::addBufferAccess(const BufferAccess* access) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 121 | fBufferAccesses.push_back(access); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 122 | this->addGpuResource(access->programBuffer()); |
| 123 | } |
| 124 | |
| 125 | void GrProcessor::addImageStorageAccess(const ImageStorageAccess* access) { |
| 126 | fImageStorageAccesses.push_back(access); |
| 127 | this->addGpuResource(access->programTexture()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 128 | } |
| 129 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 130 | void* GrProcessor::operator new(size_t size) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 131 | return MemoryPoolAccessor().pool()->allocate(size); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 132 | } |
| 133 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 134 | void GrProcessor::operator delete(void* target) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 135 | return MemoryPoolAccessor().pool()->release(target); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 136 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 137 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 138 | bool GrProcessor::hasSameSamplersAndAccesses(const GrProcessor &that) const { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 139 | if (this->numTextureSamplers() != that.numTextureSamplers() || |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 140 | this->numBuffers() != that.numBuffers() || |
| 141 | this->numImageStorages() != that.numImageStorages()) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 142 | return false; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 143 | } |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 144 | for (int i = 0; i < this->numTextureSamplers(); ++i) { |
| 145 | if (this->textureSampler(i) != that.textureSampler(i)) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 149 | for (int i = 0; i < this->numBuffers(); ++i) { |
| 150 | if (this->bufferAccess(i) != that.bufferAccess(i)) { |
| 151 | return false; |
| 152 | } |
| 153 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 154 | for (int i = 0; i < this->numImageStorages(); ++i) { |
| 155 | if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) { |
| 156 | return false; |
| 157 | } |
| 158 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 159 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 160 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 161 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 162 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 163 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 164 | GrProcessor::TextureSampler::TextureSampler() {} |
| 165 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 166 | GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, const GrSamplerParams& params) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 167 | this->reset(texture, params); |
| 168 | } |
| 169 | |
| 170 | GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 171 | GrSamplerParams::FilterMode filterMode, |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 172 | SkShader::TileMode tileXAndY, |
| 173 | GrShaderFlags visibility) { |
| 174 | this->reset(texture, filterMode, tileXAndY, visibility); |
| 175 | } |
| 176 | |
| 177 | void GrProcessor::TextureSampler::reset(GrTexture* texture, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 178 | const GrSamplerParams& params, |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 179 | GrShaderFlags visibility) { |
| 180 | SkASSERT(texture); |
| 181 | fTexture.set(SkRef(texture), kRead_GrIOType); |
| 182 | fParams = params; |
| 183 | fParams.setFilterMode(SkTMin(params.filterMode(), texture->texturePriv().highestFilterMode())); |
| 184 | fVisibility = visibility; |
| 185 | } |
| 186 | |
| 187 | void GrProcessor::TextureSampler::reset(GrTexture* texture, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 188 | GrSamplerParams::FilterMode filterMode, |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 189 | SkShader::TileMode tileXAndY, |
| 190 | GrShaderFlags visibility) { |
| 191 | SkASSERT(texture); |
| 192 | fTexture.set(SkRef(texture), kRead_GrIOType); |
| 193 | filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode()); |
| 194 | fParams.reset(tileXAndY, filterMode); |
| 195 | fVisibility = visibility; |
| 196 | } |
| 197 | |
| 198 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 199 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 200 | GrProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, |
| 201 | GrSLMemoryModel memoryModel, |
| 202 | GrSLRestrict restrict, |
| 203 | GrShaderFlags visibility) { |
| 204 | SkASSERT(texture); |
| 205 | fTexture.set(texture.release(), ioType); |
| 206 | fMemoryModel = memoryModel; |
| 207 | fRestrict = restrict; |
| 208 | fVisibility = visibility; |
| 209 | // We currently infer this from the config. However, we could allow the client to specify |
| 210 | // a format that is different but compatible with the config. |
| 211 | switch (fTexture.get()->config()) { |
| 212 | case kRGBA_8888_GrPixelConfig: |
| 213 | fFormat = GrImageStorageFormat::kRGBA8; |
| 214 | break; |
| 215 | case kRGBA_8888_sint_GrPixelConfig: |
| 216 | fFormat = GrImageStorageFormat::kRGBA8i; |
| 217 | break; |
| 218 | case kRGBA_half_GrPixelConfig: |
| 219 | fFormat = GrImageStorageFormat::kRGBA16f; |
| 220 | break; |
| 221 | case kRGBA_float_GrPixelConfig: |
| 222 | fFormat = GrImageStorageFormat::kRGBA32f; |
| 223 | break; |
| 224 | default: |
| 225 | SkFAIL("Config is not (yet) supported as image storage."); |
| 226 | break; |
| 227 | } |
| 228 | } |