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" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 10 | #include "GrContextPriv.h" |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 11 | #include "GrGeometryProcessor.h" |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 12 | #include "GrMemoryPool.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 13 | #include "GrSamplerState.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 14 | #include "GrTextureProxy.h" |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 15 | #include "GrXferProcessor.h" |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 16 | #include "SkSpinlock.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 17 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 18 | #if GR_TEST_UTILS |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 19 | |
| 20 | GrResourceProvider* GrProcessorTestData::resourceProvider() { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 21 | return fContext->contextPriv().resourceProvider(); |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 22 | } |
| 23 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 24 | GrProxyProvider* GrProcessorTestData::proxyProvider() { |
| 25 | return fContext->contextPriv().proxyProvider(); |
| 26 | } |
| 27 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 28 | const GrCaps* GrProcessorTestData::caps() { return fContext->contextPriv().caps(); } |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 29 | |
| 30 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 31 | class GrFragmentProcessor; |
| 32 | class GrGeometryProcessor; |
| 33 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 34 | /* |
| 35 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 36 | * problems on android. |
| 37 | */ |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 38 | template <> |
| 39 | SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() { |
| 40 | static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 41 | return &gFactories; |
| 42 | } |
| 43 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 44 | template <> |
| 45 | SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() { |
| 46 | static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories; |
Brian Salomon | a8f80de | 2017-01-07 09:37:13 -0500 | [diff] [blame] | 47 | return &gFactories; |
| 48 | } |
| 49 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 50 | SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() { |
| 51 | static SkTArray<GrXPFactoryTestFactory*, true> gFactories; |
| 52 | return &gFactories; |
| 53 | } |
| 54 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 55 | /* |
| 56 | * To ensure we always have successful static initialization, before creating from the factories |
| 57 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 58 | * manually adjusted. |
| 59 | */ |
Brian Osman | 47c2751 | 2018-06-18 10:58:51 -0400 | [diff] [blame] | 60 | static const int kFPFactoryCount = 37; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 61 | static const int kGPFactoryCount = 14; |
Brian Salomon | 1c4717b | 2017-01-06 12:54:58 -0500 | [diff] [blame] | 62 | static const int kXPFactoryCount = 4; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 63 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 64 | template <> |
| 65 | void GrFragmentProcessorTestFactory::VerifyFactoryCount() { |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 66 | if (kFPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 67 | SkDebugf("\nExpected %d fragment processor factories, found %d.\n", |
| 68 | kFPFactoryCount, GetFactories()->count()); |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 69 | SK_ABORT("Wrong number of fragment processor factories!"); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 73 | template <> |
| 74 | void GrGeometryProcessorTestFactory::VerifyFactoryCount() { |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 75 | if (kGPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 76 | SkDebugf("\nExpected %d geometry processor factories, found %d.\n", |
| 77 | kGPFactoryCount, GetFactories()->count()); |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 78 | SK_ABORT("Wrong number of geometry processor factories!"); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 82 | void GrXPFactoryTestFactory::VerifyFactoryCount() { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 83 | if (kXPFactoryCount != GetFactories()->count()) { |
Ben Wagner | c03e1c5 | 2016-10-17 15:20:02 -0400 | [diff] [blame] | 84 | SkDebugf("\nExpected %d xp factory factories, found %d.\n", |
| 85 | kXPFactoryCount, GetFactories()->count()); |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 86 | SK_ABORT("Wrong number of xp factory factories!"); |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 90 | #endif |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 91 | #endif |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 92 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 93 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 94 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 95 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 96 | // 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] | 97 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 98 | namespace { |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 99 | #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
mtklein | 15923c9 | 2016-02-29 10:14:38 -0800 | [diff] [blame] | 100 | static SkSpinlock gProcessorSpinlock; |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 101 | #endif |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 102 | class MemoryPoolAccessor { |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 103 | public: |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 104 | |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 105 | // We know in the Android framework there is only one GrContext. |
| 106 | #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 107 | MemoryPoolAccessor() {} |
| 108 | ~MemoryPoolAccessor() {} |
| 109 | #else |
| 110 | MemoryPoolAccessor() { gProcessorSpinlock.acquire(); } |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 111 | ~MemoryPoolAccessor() { gProcessorSpinlock.release(); } |
msarett | 68440f8 | 2016-08-29 14:52:24 -0700 | [diff] [blame] | 112 | #endif |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 113 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 114 | GrMemoryPool* pool() const { |
| 115 | static GrMemoryPool gPool(4096, 4096); |
| 116 | return &gPool; |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 117 | } |
| 118 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 119 | } |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 120 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 123 | void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 124 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 125 | void GrProcessor::operator delete(void* target) { |
| 126 | return MemoryPoolAccessor().pool()->release(target); |
| 127 | } |