| 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" |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 16 | |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 17 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 18 | |
| joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 19 | class GrFragmentProcessor; |
| 20 | class GrGeometryProcessor; |
| 21 | |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 24 | * problems on android. |
| 25 | */ |
| 26 | template<> |
| 27 | SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>* |
| 28 | GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() { |
| 29 | static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories; |
| 30 | return &gFactories; |
| 31 | } |
| 32 | |
| 33 | template<> |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 34 | SkTArray<GrProcessorTestFactory<GrXPFactory>*, true>* |
| 35 | GrProcessorTestFactory<GrXPFactory>::GetFactories() { |
| 36 | static SkTArray<GrProcessorTestFactory<GrXPFactory>*, true> gFactories; |
| egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 37 | return &gFactories; |
| 38 | } |
| 39 | |
| 40 | template<> |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 41 | SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>* |
| 42 | GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() { |
| 43 | static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories; |
| 44 | return &gFactories; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * To ensure we always have successful static initialization, before creating from the factories |
| 49 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 50 | * manually adjusted. |
| 51 | */ |
| reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 52 | static const int kFPFactoryCount = 37; |
| joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 53 | static const int kGPFactoryCount = 14; |
| egdaniel | 54f0e9d | 2015-01-16 06:29:47 -0800 | [diff] [blame] | 54 | static const int kXPFactoryCount = 5; |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 55 | |
| 56 | template<> |
| 57 | void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() { |
| 58 | if (kFPFactoryCount != GetFactories()->count()) { |
| 59 | SkFAIL("Wrong number of fragment processor factories!"); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | template<> |
| 64 | void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() { |
| 65 | if (kGPFactoryCount != GetFactories()->count()) { |
| 66 | SkFAIL("Wrong number of geometry processor factories!"); |
| 67 | } |
| 68 | } |
| 69 | |
| egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 70 | template<> |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 71 | void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() { |
| egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 72 | if (kXPFactoryCount != GetFactories()->count()) { |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 73 | SkFAIL("Wrong number of xp factory factories!"); |
| egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 77 | #endif |
| 78 | |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 79 | |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 80 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 81 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 82 | // 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] | 83 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 84 | namespace { |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 85 | SK_DECLARE_STATIC_SPINLOCK(gProcessorSpinlock); |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 86 | class MemoryPoolAccessor { |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 87 | public: |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 88 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 89 | |
| joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 90 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 91 | |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 92 | GrMemoryPool* pool() const { |
| 93 | static GrMemoryPool gPool(4096, 4096); |
| 94 | return &gPool; |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 95 | } |
| 96 | }; |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 97 | } |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 98 | |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 99 | int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 100 | |
| bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 103 | GrProcessor::~GrProcessor() {} |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 104 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 105 | void GrProcessor::addTextureAccess(const GrTextureAccess* access) { |
| bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 106 | fTextureAccesses.push_back(access); |
| bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 107 | this->addGpuResource(access->getProgramTexture()); |
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 110 | void* GrProcessor::operator new(size_t size) { |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 111 | return MemoryPoolAccessor().pool()->allocate(size); |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 114 | void GrProcessor::operator delete(void* target) { |
| bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 115 | return MemoryPoolAccessor().pool()->release(target); |
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 116 | } |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 117 | |
| bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 118 | bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const { |
| 119 | if (this->numTextures() != that.numTextures()) { |
| 120 | return false; |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 121 | } |
| bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 122 | for (int i = 0; i < this->numTextures(); ++i) { |
| 123 | if (this->textureAccess(i) != that.textureAccess(i)) { |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | return true; |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 128 | } |
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 129 | |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 130 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
| 133 | fCoordTransforms.push_back(transform); |
| joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 134 | fUsesLocalCoords = fUsesLocalCoords || transform->sourceCoords() == kLocal_GrCoordSet; |
| bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 135 | SkDEBUGCODE(transform->setInProcessor();) |
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 136 | } |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 137 | |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 138 | int GrFragmentProcessor::registerChildProcessor(const GrFragmentProcessor* child) { |
| 139 | // Append the child's transforms to our transforms array and the child's textures array to our |
| 140 | // textures array |
| 141 | if (!child->fCoordTransforms.empty()) { |
| 142 | fCoordTransforms.push_back_n(child->fCoordTransforms.count(), |
| 143 | child->fCoordTransforms.begin()); |
| 144 | } |
| 145 | if (!child->fTextureAccesses.empty()) { |
| 146 | fTextureAccesses.push_back_n(child->fTextureAccesses.count(), |
| 147 | child->fTextureAccesses.begin()); |
| 148 | } |
| 149 | |
| 150 | int index = fChildProcessors.count(); |
| 151 | fChildProcessors.push_back(GrFragmentStage(child)); |
| 152 | |
| wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame^] | 153 | if (child->willReadFragmentPosition()) { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 154 | this->setWillReadFragmentPosition(); |
| wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame^] | 155 | } |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 156 | |
| 157 | return index; |
| wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 160 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 161 | if (this->numTransforms() != that.numTransforms()) { |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 164 | int count = this->numTransforms(); |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 165 | for (int i = 0; i < count; ++i) { |
| wangyix | 58d890b | 2015-08-12 09:40:47 -0700 | [diff] [blame] | 166 | if (this->coordTransform(i) != that.coordTransform(i)) { |
| bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | return true; |
| 171 | } |
| joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 172 | |
| joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 173 | void GrFragmentProcessor::computeInvariantOutput(GrInvariantOutput* inout) const { |
| 174 | this->onComputeInvariantOutput(inout); |
| 175 | } |
| 176 | |
| joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 177 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 178 | |
| egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 179 | // Initial static variable from GrXPFactory |
| 180 | int32_t GrXPFactory::gCurrXPFClassID = |
| 181 | GrXPFactory::kIllegalXPFClassID; |
| joshualitt | 5b4f05f | 2015-07-10 07:26:21 -0700 | [diff] [blame] | 182 | |
| 183 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 184 | |
| 185 | // GrProcessorDataManager lives in the same pool |
| 186 | void* GrProcessorDataManager::operator new(size_t size) { |
| 187 | return MemoryPoolAccessor().pool()->allocate(size); |
| 188 | } |
| 189 | |
| 190 | void GrProcessorDataManager::operator delete(void* target) { |
| 191 | return MemoryPoolAccessor().pool()->release(target); |
| 192 | } |