GrProcessorUnitTest.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #ifndef GrProcessorUnitTest_DEFINED
  8. #define GrProcessorUnitTest_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #if GR_TEST_UTILS
  11. #include "include/private/SkTArray.h"
  12. #include "src/gpu/GrTestUtils.h"
  13. #include "src/gpu/GrTextureProxy.h"
  14. class SkMatrix;
  15. class GrCaps;
  16. class GrContext;
  17. class GrProxyProvider;
  18. class GrRenderTargetContext;
  19. struct GrProcessorTestData;
  20. class GrTexture;
  21. class GrXPFactory;
  22. class GrGeometryProcessor;
  23. namespace GrProcessorUnitTest {
  24. // Used to access the dummy textures in TestCreate procs.
  25. enum {
  26. kSkiaPMTextureIdx = 0,
  27. kAlphaTextureIdx = 1,
  28. };
  29. /** This allows parent FPs to implement a test create with known leaf children in order to avoid
  30. creating an unbounded FP tree which may overflow various shader limits. */
  31. std::unique_ptr<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
  32. }
  33. /*
  34. * GrProcessorTestData is an argument struct to TestCreate functions
  35. * fTextures are valid textures that can optionally be used to construct
  36. * TextureSampler. The first texture has config kSkia8888_GrPixelConfig and the second has
  37. * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
  38. * the GrContext.
  39. */
  40. struct GrProcessorTestData {
  41. GrProcessorTestData(SkRandom* random,
  42. GrContext* context,
  43. const GrRenderTargetContext* renderTargetContext,
  44. sk_sp<GrTextureProxy> proxies[2])
  45. : fRandom(random)
  46. , fRenderTargetContext(renderTargetContext)
  47. , fContext(context) {
  48. SkASSERT(proxies[0] && proxies[1]);
  49. fProxies[0] = proxies[0];
  50. fProxies[1] = proxies[1];
  51. }
  52. SkRandom* fRandom;
  53. const GrRenderTargetContext* fRenderTargetContext;
  54. GrContext* context() { return fContext; }
  55. GrResourceProvider* resourceProvider();
  56. GrProxyProvider* proxyProvider();
  57. const GrCaps* caps();
  58. sk_sp<GrTextureProxy> textureProxy(int index) { return fProxies[index]; }
  59. private:
  60. GrContext* fContext;
  61. sk_sp<GrTextureProxy> fProxies[2];
  62. };
  63. class GrProcessor;
  64. class GrTexture;
  65. template <class ProcessorSmartPtr>
  66. class GrProcessorTestFactory : private SkNoncopyable {
  67. public:
  68. using Processor = typename ProcessorSmartPtr::element_type;
  69. using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*);
  70. GrProcessorTestFactory(MakeProc makeProc) {
  71. fMakeProc = makeProc;
  72. GetFactories()->push_back(this);
  73. }
  74. /** Pick a random factory function and create a processor. */
  75. static ProcessorSmartPtr Make(GrProcessorTestData* data) {
  76. VerifyFactoryCount();
  77. if (GetFactories()->count() == 0) {
  78. return nullptr;
  79. }
  80. uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
  81. return MakeIdx(idx, data);
  82. }
  83. /** Number of registered factory functions */
  84. static int Count() { return GetFactories()->count(); }
  85. /** Use factory function at Index idx to create a processor. */
  86. static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data) {
  87. SkASSERT(idx < GetFactories()->count());
  88. GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx];
  89. ProcessorSmartPtr processor = factory->fMakeProc(data);
  90. SkASSERT(processor);
  91. return processor;
  92. }
  93. private:
  94. /**
  95. * A test function which verifies the count of factories.
  96. */
  97. static void VerifyFactoryCount();
  98. MakeProc fMakeProc;
  99. static SkTArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories();
  100. };
  101. using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
  102. using GrGeometryProcessorTestFactory = GrProcessorTestFactory<sk_sp<GrGeometryProcessor>>;
  103. class GrXPFactoryTestFactory : private SkNoncopyable {
  104. public:
  105. using GetFn = const GrXPFactory*(GrProcessorTestData*);
  106. GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); }
  107. static const GrXPFactory* Get(GrProcessorTestData* data) {
  108. VerifyFactoryCount();
  109. if (GetFactories()->count() == 0) {
  110. return nullptr;
  111. }
  112. uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
  113. const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data);
  114. SkASSERT(xpf);
  115. return xpf;
  116. }
  117. private:
  118. static void VerifyFactoryCount();
  119. GetFn* fGetProc;
  120. static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
  121. };
  122. #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
  123. /** GrProcessor subclasses should insert this macro in their declaration to be included in the
  124. * program generation unit test.
  125. */
  126. #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
  127. static GrGeometryProcessorTestFactory gTestFactory SK_UNUSED; \
  128. static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*);
  129. #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
  130. static GrFragmentProcessorTestFactory gTestFactory SK_UNUSED; \
  131. static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
  132. #define GR_DECLARE_XP_FACTORY_TEST \
  133. static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
  134. static const GrXPFactory* TestGet(GrProcessorTestData*);
  135. /** GrProcessor subclasses should insert this macro in their implementation file. They must then
  136. * also implement this static function:
  137. * GrProcessor* TestCreate(GrProcessorTestData*);
  138. */
  139. #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
  140. GrFragmentProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
  141. #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
  142. GrGeometryProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
  143. #define GR_DEFINE_XP_FACTORY_TEST(Factory) \
  144. GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
  145. #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
  146. // The unit test relies on static initializers. Just declare the TestCreate function so that
  147. // its definitions will compile.
  148. #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
  149. static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
  150. #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
  151. // The unit test relies on static initializers. Just declare the TestCreate function so that
  152. // its definitions will compile.
  153. #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
  154. static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*);
  155. #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
  156. // The unit test relies on static initializers. Just declare the TestGet function so that
  157. // its definitions will compile.
  158. #define GR_DECLARE_XP_FACTORY_TEST \
  159. const GrXPFactory* TestGet(GrProcessorTestData*);
  160. #define GR_DEFINE_XP_FACTORY_TEST(X)
  161. #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
  162. #else // GR_TEST_UTILS
  163. #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
  164. #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
  165. #define GR_DECLARE_XP_FACTORY_TEST
  166. #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
  167. #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
  168. #define GR_DEFINE_XP_FACTORY_TEST(...)
  169. #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
  170. #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
  171. #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
  172. #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
  173. #define GR_DECLARE_XP_FACTORY_TEST
  174. #define GR_DEFINE_XP_FACTORY_TEST(...)
  175. #endif // GR_TEST_UTILS
  176. #endif // GrProcessorUnitTest_DEFINED