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" |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 13 | #include "GrXferProcessor.h" |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 14 | #include "SkSpinlock.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 15 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 16 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 17 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 18 | class GrFragmentProcessor; |
| 19 | class GrGeometryProcessor; |
| 20 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 21 | /* |
| 22 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 23 | * problems on android. |
| 24 | */ |
| 25 | template<> |
| 26 | SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>* |
| 27 | GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() { |
| 28 | static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories; |
| 29 | return &gFactories; |
| 30 | } |
| 31 | |
| 32 | template<> |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 33 | SkTArray<GrProcessorTestFactory<GrXPFactory>*, true>* |
| 34 | GrProcessorTestFactory<GrXPFactory>::GetFactories() { |
| 35 | static SkTArray<GrProcessorTestFactory<GrXPFactory>*, true> gFactories; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 36 | return &gFactories; |
| 37 | } |
| 38 | |
| 39 | template<> |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 40 | SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>* |
| 41 | GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() { |
| 42 | static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories; |
| 43 | return &gFactories; |
| 44 | } |
| 45 | |
| 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 | */ |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [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; |
reed | 59dc542 | 2016-04-05 06:25:14 -0700 | [diff] [blame] | 53 | static const int kXPFactoryCount = 6; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 54 | |
| 55 | template<> |
| 56 | void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() { |
| 57 | if (kFPFactoryCount != GetFactories()->count()) { |
| 58 | SkFAIL("Wrong number of fragment processor factories!"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | template<> |
| 63 | void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() { |
| 64 | if (kGPFactoryCount != GetFactories()->count()) { |
| 65 | SkFAIL("Wrong number of geometry processor factories!"); |
| 66 | } |
| 67 | } |
| 68 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 69 | template<> |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 70 | void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 71 | if (kXPFactoryCount != GetFactories()->count()) { |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 72 | SkFAIL("Wrong number of xp factory factories!"); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 76 | #endif |
| 77 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 78 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 79 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 80 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 81 | // 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] | 82 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 83 | namespace { |
mtklein | 15923c9 | 2016-02-29 10:14:38 -0800 | [diff] [blame] | 84 | static SkSpinlock gProcessorSpinlock; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 85 | class MemoryPoolAccessor { |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 86 | public: |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 87 | |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame^] | 88 | // We know in the Android framework there is only one GrContext. |
| 89 | #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 90 | MemoryPoolAccessor() {} |
| 91 | ~MemoryPoolAccessor() {} |
| 92 | #else |
| 93 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 94 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame^] | 95 | #endif |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 96 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 97 | GrMemoryPool* pool() const { |
| 98 | static GrMemoryPool gPool(4096, 4096); |
| 99 | return &gPool; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 100 | } |
| 101 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 102 | } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 103 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 104 | int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 108 | GrProcessor::~GrProcessor() {} |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 109 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 110 | void GrProcessor::addTextureAccess(const GrTextureAccess* access) { |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 111 | fTextureAccesses.push_back(access); |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 112 | this->addGpuResource(access->getProgramTexture()); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 113 | } |
| 114 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 115 | void GrProcessor::addBufferAccess(const GrBufferAccess* access) { |
| 116 | fBufferAccesses.push_back(access); |
| 117 | this->addGpuResource(access->getProgramBuffer()); |
| 118 | } |
| 119 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 120 | void* GrProcessor::operator new(size_t size) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 121 | return MemoryPoolAccessor().pool()->allocate(size); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 124 | void GrProcessor::operator delete(void* target) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 125 | return MemoryPoolAccessor().pool()->release(target); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 126 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 127 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 128 | bool GrProcessor::hasSameSamplers(const GrProcessor& that) const { |
| 129 | if (this->numTextures() != that.numTextures() || this->numBuffers() != that.numBuffers()) { |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 130 | return false; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 131 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 132 | for (int i = 0; i < this->numTextures(); ++i) { |
| 133 | if (this->textureAccess(i) != that.textureAccess(i)) { |
| 134 | return false; |
| 135 | } |
| 136 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 137 | for (int i = 0; i < this->numBuffers(); ++i) { |
| 138 | if (this->bufferAccess(i) != that.bufferAccess(i)) { |
| 139 | return false; |
| 140 | } |
| 141 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 142 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 143 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 144 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 145 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 146 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 147 | // Initial static variable from GrXPFactory |
| 148 | int32_t GrXPFactory::gCurrXPFClassID = |
| 149 | GrXPFactory::kIllegalXPFClassID; |