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 | */ |
reed | 59dc542 | 2016-04-05 06:25:14 -0700 | [diff] [blame] | 51 | static const int kFPFactoryCount = 39; |
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: |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 87 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 88 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 89 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 90 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 91 | GrMemoryPool* pool() const { |
| 92 | static GrMemoryPool gPool(4096, 4096); |
| 93 | return &gPool; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 94 | } |
| 95 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 96 | } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 97 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 98 | int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 99 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 102 | GrProcessor::~GrProcessor() {} |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 103 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 104 | void GrProcessor::addTextureAccess(const GrTextureAccess* access) { |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 105 | fTextureAccesses.push_back(access); |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 106 | this->addGpuResource(access->getProgramTexture()); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 107 | } |
| 108 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 109 | void* GrProcessor::operator new(size_t size) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 110 | return MemoryPoolAccessor().pool()->allocate(size); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 111 | } |
| 112 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 113 | void GrProcessor::operator delete(void* target) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 114 | return MemoryPoolAccessor().pool()->release(target); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 115 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 116 | |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 117 | bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const { |
| 118 | if (this->numTextures() != that.numTextures()) { |
| 119 | return false; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 120 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 121 | for (int i = 0; i < this->numTextures(); ++i) { |
| 122 | if (this->textureAccess(i) != that.textureAccess(i)) { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 127 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 128 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 130 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 131 | // Initial static variable from GrXPFactory |
| 132 | int32_t GrXPFactory::gCurrXPFClassID = |
| 133 | GrXPFactory::kIllegalXPFClassID; |