• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/gpu/GrContext.h"
9 #include "include/private/SkSpinlock.h"
10 #include "src/gpu/GrContextPriv.h"
11 #include "src/gpu/GrGeometryProcessor.h"
12 #include "src/gpu/GrMemoryPool.h"
13 #include "src/gpu/GrProcessor.h"
14 #include "src/gpu/GrSamplerState.h"
15 #include "src/gpu/GrTextureProxy.h"
16 #include "src/gpu/GrXferProcessor.h"
17 
18 #if GR_TEST_UTILS
19 
resourceProvider()20 GrResourceProvider* GrProcessorTestData::resourceProvider() {
21     return fContext->priv().resourceProvider();
22 }
23 
proxyProvider()24 GrProxyProvider* GrProcessorTestData::proxyProvider() {
25     return fContext->priv().proxyProvider();
26 }
27 
caps()28 const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
29 
30 class GrFragmentProcessor;
31 class GrGeometryProcessor;
32 
33 /*
34  * Originally these were both in the processor unit test header, but then it seemed to cause linker
35  * problems on android.
36  */
37 template <>
GetFactories()38 SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
39     static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
40     return &gFactories;
41 }
42 
43 template <>
GetFactories()44 SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
45     static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
46     return &gFactories;
47 }
48 
GetFactories()49 SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
50     static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
51     return &gFactories;
52 }
53 
54 /*
55  * To ensure we always have successful static initialization, before creating from the factories
56  * we verify the count is as expected.  If a new factory is added, then these numbers must be
57  * manually adjusted.
58  */
59 #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
60 static const int kFPFactoryCount = 36;
61 static const int kGPFactoryCount = 14;
62 static const int kXPFactoryCount = 4;
63 #else
64 static const int kFPFactoryCount = 0;
65 static const int kGPFactoryCount = 0;
66 static const int kXPFactoryCount = 0;
67 #endif
68 
69 template <>
VerifyFactoryCount()70 void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
71     if (kFPFactoryCount != GetFactories()->count()) {
72         SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
73                  kFPFactoryCount, GetFactories()->count());
74         SK_ABORT("Wrong number of fragment processor factories!");
75     }
76 }
77 
78 template <>
VerifyFactoryCount()79 void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
80     if (kGPFactoryCount != GetFactories()->count()) {
81         SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
82                  kGPFactoryCount, GetFactories()->count());
83         SK_ABORT("Wrong number of geometry processor factories!");
84     }
85 }
86 
VerifyFactoryCount()87 void GrXPFactoryTestFactory::VerifyFactoryCount() {
88     if (kXPFactoryCount != GetFactories()->count()) {
89         SkDebugf("\nExpected %d xp factory factories, found %d.\n",
90                  kXPFactoryCount, GetFactories()->count());
91         SK_ABORT("Wrong number of xp factory factories!");
92     }
93 }
94 
95 #endif
96 
97 
98 // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
99 // different threads. The GrContext is not used concurrently on different threads and there is a
100 // memory barrier between accesses of a context on different threads. Also, there may be multiple
101 // GrContexts and those contexts may be in use concurrently on different threads.
102 namespace {
103 #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
104 static SkSpinlock gProcessorSpinlock;
105 #endif
106 class MemoryPoolAccessor {
107 public:
108 
109 // We know in the Android framework there is only one GrContext.
110 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
MemoryPoolAccessor()111     MemoryPoolAccessor() {}
~MemoryPoolAccessor()112     ~MemoryPoolAccessor() {}
113 #else
114     MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
115     ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
116 #endif
117 
pool() const118     GrMemoryPool* pool() const {
119         static GrMemoryPool gPool(4096, 4096);
120         return &gPool;
121     }
122 };
123 }
124 
125 ///////////////////////////////////////////////////////////////////////////////
126 
operator new(size_t size)127 void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
128 
operator delete(void * target)129 void GrProcessor::operator delete(void* target) {
130     return MemoryPoolAccessor().pool()->release(target);
131 }
132