| 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" |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 10 | #include "GrCoordTransform.h" |
| joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 11 | #include "GrGeometryProcessor.h" |
| egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 12 | #include "GrInvariantOutput.h" |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 13 | #include "GrMemoryPool.h" |
| egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 14 | #include "GrXferProcessor.h" |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 15 | #include "SkSpinlock.h" |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame^] | 16 | #include "gl/GrGLFragmentProcessor.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 | */ |
| reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 53 | static const int kFPFactoryCount = 37; |
| joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 54 | static const int kGPFactoryCount = 14; |
| egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [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()) { |
| 60 | SkFAIL("Wrong number of fragment processor factories!"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | template<> |
| 65 | void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() { |
| 66 | if (kGPFactoryCount != GetFactories()->count()) { |
| 67 | SkFAIL("Wrong number of geometry processor factories!"); |
| 68 | } |
| 69 | } |
| 70 | |
| egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 71 | template<> |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 72 | void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() { |
| egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 73 | if (kXPFactoryCount != GetFactories()->count()) { |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 74 | SkFAIL("Wrong number of xp factory factories!"); |
| egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 78 | #endif |
| 79 | |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 80 | |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 81 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 82 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 83 | // 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] | 84 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 85 | namespace { |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 86 | SK_DECLARE_STATIC_SPINLOCK(gProcessorSpinlock); |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 87 | class MemoryPoolAccessor { |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 88 | public: |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 89 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 90 | |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 91 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 92 | |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 93 | GrMemoryPool* pool() const { |
| 94 | static GrMemoryPool gPool(4096, 4096); |
| 95 | return &gPool; |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 96 | } |
| 97 | }; |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 98 | } |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 99 | |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 100 | int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 101 | |
| bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 104 | GrProcessor::~GrProcessor() {} |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 105 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 106 | void GrProcessor::addTextureAccess(const GrTextureAccess* access) { |
| bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 107 | fTextureAccesses.push_back(access); |
| bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 108 | this->addGpuResource(access->getProgramTexture()); |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 111 | void* GrProcessor::operator new(size_t size) { |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 112 | return MemoryPoolAccessor().pool()->allocate(size); |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 115 | void GrProcessor::operator delete(void* target) { |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 116 | return MemoryPoolAccessor().pool()->release(target); |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 117 | } |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 118 | |
| bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 119 | bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const { |
| 120 | if (this->numTextures() != that.numTextures()) { |
| 121 | return false; |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 122 | } |
| bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 123 | for (int i = 0; i < this->numTextures(); ++i) { |
| 124 | if (this->textureAccess(i) != that.textureAccess(i)) { |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | return true; |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 129 | } |
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 130 | |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 131 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 132 | |
| wangyix | 54017d7 | 2015-08-18 07:39:33 -0700 | [diff] [blame] | 133 | bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that, |
| 134 | bool ignoreCoordTransforms) const { |
| 135 | if (this->classID() != that.classID() || |
| 136 | !this->hasSameTextureAccesses(that)) { |
| 137 | return false; |
| 138 | } |
| 139 | if (ignoreCoordTransforms) { |
| 140 | if (this->numTransforms() != that.numTransforms()) { |
| 141 | return false; |
| 142 | } |
| 143 | } else if (!this->hasSameTransforms(that)) { |
| 144 | return false; |
| 145 | } |
| 146 | if (!this->onIsEqual(that)) { |
| 147 | return false; |
| 148 | } |
| 149 | if (this->numChildProcessors() != that.numChildProcessors()) { |
| 150 | return false; |
| 151 | } |
| 152 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 153 | if (!this->childProcessor(i).isEqual(that.childProcessor(i), ignoreCoordTransforms)) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame^] | 160 | GrGLFragmentProcessor* GrFragmentProcessor::createGLInstance() const { |
| 161 | GrGLFragmentProcessor* glFragProc = this->onCreateGLInstance(); |
| 162 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 163 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| 164 | glFragProc->fChildProcessors[i] = fChildProcessors[i].processor()->createGLInstance(); |
| 165 | } |
| 166 | return glFragProc; |
| 167 | } |
| 168 | |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 169 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
| 170 | fCoordTransforms.push_back(transform); |
| joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 171 | fUsesLocalCoords = fUsesLocalCoords || transform->sourceCoords() == kLocal_GrCoordSet; |
| bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 172 | SkDEBUGCODE(transform->setInProcessor();) |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 173 | } |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 174 | |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 175 | int GrFragmentProcessor::registerChildProcessor(const GrFragmentProcessor* child) { |
| 176 | // Append the child's transforms to our transforms array and the child's textures array to our |
| 177 | // textures array |
| 178 | if (!child->fCoordTransforms.empty()) { |
| 179 | fCoordTransforms.push_back_n(child->fCoordTransforms.count(), |
| 180 | child->fCoordTransforms.begin()); |
| 181 | } |
| 182 | if (!child->fTextureAccesses.empty()) { |
| 183 | fTextureAccesses.push_back_n(child->fTextureAccesses.count(), |
| 184 | child->fTextureAccesses.begin()); |
| 185 | } |
| 186 | |
| 187 | int index = fChildProcessors.count(); |
| 188 | fChildProcessors.push_back(GrFragmentStage(child)); |
| 189 | |
| wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 190 | if (child->willReadFragmentPosition()) { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 191 | this->setWillReadFragmentPosition(); |
| wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 192 | } |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 193 | |
| 194 | return index; |
| wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 197 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 198 | if (this->numTransforms() != that.numTransforms()) { |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 199 | return false; |
| 200 | } |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 201 | int count = this->numTransforms(); |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 202 | for (int i = 0; i < count; ++i) { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 203 | if (this->coordTransform(i) != that.coordTransform(i)) { |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | return true; |
| 208 | } |
| joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 209 | |
| joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 210 | void GrFragmentProcessor::computeInvariantOutput(GrInvariantOutput* inout) const { |
| 211 | this->onComputeInvariantOutput(inout); |
| 212 | } |
| 213 | |
| joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 214 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 215 | |
| egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 216 | // Initial static variable from GrXPFactory |
| 217 | int32_t GrXPFactory::gCurrXPFClassID = |
| 218 | GrXPFactory::kIllegalXPFClassID; |
| joshualitt | 5b4f05f | 2015-07-10 07:26:21 -0700 | [diff] [blame] | 219 | |
| 220 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 221 | |
| 222 | // GrProcessorDataManager lives in the same pool |
| 223 | void* GrProcessorDataManager::operator new(size_t size) { |
| 224 | return MemoryPoolAccessor().pool()->allocate(size); |
| 225 | } |
| 226 | |
| 227 | void GrProcessorDataManager::operator delete(void* target) { |
| 228 | return MemoryPoolAccessor().pool()->release(target); |
| 229 | } |