| 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 | */ |
| bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 53 | static const int kFPFactoryCount = 39; |
| 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 | |
| bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 133 | GrFragmentProcessor::~GrFragmentProcessor() { |
| bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 134 | // If we got here then our ref count must have reached zero, so we will have converted refs |
| 135 | // to pending executions for all children. |
| bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 136 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 137 | fChildProcessors[i]->completedExecution(); |
| bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| wangyix | 54017d7 | 2015-08-18 07:39:33 -0700 | [diff] [blame] | 141 | bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that, |
| 142 | bool ignoreCoordTransforms) const { |
| 143 | if (this->classID() != that.classID() || |
| 144 | !this->hasSameTextureAccesses(that)) { |
| 145 | return false; |
| 146 | } |
| 147 | if (ignoreCoordTransforms) { |
| 148 | if (this->numTransforms() != that.numTransforms()) { |
| 149 | return false; |
| 150 | } |
| 151 | } else if (!this->hasSameTransforms(that)) { |
| 152 | return false; |
| 153 | } |
| 154 | if (!this->onIsEqual(that)) { |
| 155 | return false; |
| 156 | } |
| 157 | if (this->numChildProcessors() != that.numChildProcessors()) { |
| 158 | return false; |
| 159 | } |
| 160 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 161 | if (!this->childProcessor(i).isEqual(that.childProcessor(i), ignoreCoordTransforms)) { |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 168 | GrGLFragmentProcessor* GrFragmentProcessor::createGLInstance() const { |
| 169 | GrGLFragmentProcessor* glFragProc = this->onCreateGLInstance(); |
| 170 | glFragProc->fChildProcessors.push_back_n(fChildProcessors.count()); |
| 171 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 172 | glFragProc->fChildProcessors[i] = fChildProcessors[i]->createGLInstance(); |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 173 | } |
| 174 | return glFragProc; |
| 175 | } |
| 176 | |
| wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 177 | void GrFragmentProcessor::addTextureAccess(const GrTextureAccess* textureAccess) { |
| 178 | // Can't add texture accesses after registering any children since their texture accesses have |
| 179 | // already been bubbled up into our fTextureAccesses array |
| 180 | SkASSERT(fChildProcessors.empty()); |
| 181 | |
| 182 | INHERITED::addTextureAccess(textureAccess); |
| 183 | fNumTexturesExclChildren++; |
| 184 | } |
| 185 | |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 186 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
| wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 187 | // Can't add transforms after registering any children since their transforms have already been |
| 188 | // bubbled up into our fCoordTransforms array |
| 189 | SkASSERT(fChildProcessors.empty()); |
| 190 | |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 191 | fCoordTransforms.push_back(transform); |
| joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 192 | fUsesLocalCoords = fUsesLocalCoords || transform->sourceCoords() == kLocal_GrCoordSet; |
| bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 193 | SkDEBUGCODE(transform->setInProcessor();) |
| wangyix | 93ab254 | 2015-08-19 08:23:12 -0700 | [diff] [blame] | 194 | fNumTransformsExclChildren++; |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 195 | } |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 196 | |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 197 | int GrFragmentProcessor::registerChildProcessor(const GrFragmentProcessor* child) { |
| 198 | // Append the child's transforms to our transforms array and the child's textures array to our |
| 199 | // textures array |
| 200 | if (!child->fCoordTransforms.empty()) { |
| 201 | fCoordTransforms.push_back_n(child->fCoordTransforms.count(), |
| 202 | child->fCoordTransforms.begin()); |
| 203 | } |
| 204 | if (!child->fTextureAccesses.empty()) { |
| 205 | fTextureAccesses.push_back_n(child->fTextureAccesses.count(), |
| 206 | child->fTextureAccesses.begin()); |
| 207 | } |
| 208 | |
| 209 | int index = fChildProcessors.count(); |
| bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 210 | fChildProcessors.push_back(SkRef(child)); |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 211 | |
| wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 212 | if (child->willReadFragmentPosition()) { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 213 | this->setWillReadFragmentPosition(); |
| wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 214 | } |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 215 | |
| wangyix | a71170c | 2015-08-21 06:56:49 -0700 | [diff] [blame] | 216 | if (child->usesLocalCoords()) { |
| 217 | fUsesLocalCoords = true; |
| 218 | } |
| 219 | |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 220 | return index; |
| wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| bsalomon | 4204800 | 2015-08-27 16:43:48 -0700 | [diff] [blame] | 223 | void GrFragmentProcessor::notifyRefCntIsZero() const { |
| 224 | // See comment above GrProgramElement for a detailed explanation of why we do this. |
| 225 | for (int i = 0; i < fChildProcessors.count(); ++i) { |
| 226 | fChildProcessors[i]->addPendingExecution(); |
| 227 | fChildProcessors[i]->unref(); |
| 228 | } |
| 229 | } |
| 230 | |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 231 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 232 | if (this->numTransforms() != that.numTransforms()) { |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 235 | int count = this->numTransforms(); |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 236 | for (int i = 0; i < count; ++i) { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 237 | if (this->coordTransform(i) != that.coordTransform(i)) { |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | return true; |
| 242 | } |
| joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 243 | |
| bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame^] | 244 | #include "effects/GrXfermodeFragmentProcessor.h" |
| 245 | |
| 246 | const GrFragmentProcessor* GrFragmentProcessor::MulOuputByInputAlpha( |
| 247 | const GrFragmentProcessor* fp) { |
| 248 | return GrXfermodeFragmentProcessor::CreateFromDstProcessor(fp, SkXfermode::kDstIn_Mode); |
| 249 | } |
| 250 | |
| joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 251 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 252 | |
| egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 253 | // Initial static variable from GrXPFactory |
| 254 | int32_t GrXPFactory::gCurrXPFClassID = |
| 255 | GrXPFactory::kIllegalXPFClassID; |