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 | |
| 138 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
| 139 | if (fCoordTransforms.count() != that.fCoordTransforms.count()) { |
| 140 | return false; |
| 141 | } |
| 142 | int count = fCoordTransforms.count(); |
| 143 | for (int i = 0; i < count; ++i) { |
| 144 | if (*fCoordTransforms[i] != *that.fCoordTransforms[i]) { |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | return true; |
| 149 | } |
joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 150 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 151 | void GrFragmentProcessor::computeInvariantOutput(GrInvariantOutput* inout) const { |
| 152 | this->onComputeInvariantOutput(inout); |
| 153 | } |
| 154 | |
joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 155 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 156 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 157 | // Initial static variable from GrXPFactory |
| 158 | int32_t GrXPFactory::gCurrXPFClassID = |
| 159 | GrXPFactory::kIllegalXPFClassID; |
joshualitt | 5b4f05f | 2015-07-10 07:26:21 -0700 | [diff] [blame] | 160 | |
| 161 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | // GrProcessorDataManager lives in the same pool |
| 164 | void* GrProcessorDataManager::operator new(size_t size) { |
| 165 | return MemoryPoolAccessor().pool()->allocate(size); |
| 166 | } |
| 167 | |
| 168 | void GrProcessorDataManager::operator delete(void* target) { |
| 169 | return MemoryPoolAccessor().pool()->release(target); |
| 170 | } |