GrProcessor.cpp 4.2 KB

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