bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrProcessorUnitTest.h" |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 9 | |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrContext.h" |
| 11 | #include "src/gpu/GrContextPriv.h" |
| 12 | #include "src/gpu/GrFragmentProcessor.h" |
| 13 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 14 | #if GR_TEST_UTILS |
| 15 | |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 16 | GrProcessorTestData::GrProcessorTestData(SkRandom* random, |
| 17 | GrContext* context, |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 18 | int numViews, |
| 19 | const ViewInfo views[]) |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 20 | : fRandom(random), fContext(context) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 21 | fViews.reset(views, numViews); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 22 | fArena = std::unique_ptr<SkArenaAlloc>(new SkArenaAlloc(1000)); |
| 23 | } |
| 24 | |
| 25 | GrResourceProvider* GrProcessorTestData::resourceProvider() { |
| 26 | return fContext->priv().resourceProvider(); |
| 27 | } |
| 28 | |
| 29 | GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); } |
| 30 | |
| 31 | const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); } |
| 32 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 33 | GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() { |
| 34 | SkASSERT(!fViews.empty()); |
| 35 | return fViews[fRandom->nextULessThan(fViews.count())]; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 38 | GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 39 | int numAlphaOnly = 0; |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 40 | for (const auto& [v, ct, at] : fViews) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 41 | if (GrColorTypeIsAlphaOnly(ct)) { |
| 42 | ++numAlphaOnly; |
| 43 | } |
| 44 | } |
| 45 | SkASSERT(numAlphaOnly); |
| 46 | int idx = fRandom->nextULessThan(numAlphaOnly); |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 47 | for (const auto& [v, ct, at] : fViews) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 48 | if (GrColorTypeIsAlphaOnly(ct) && !idx--) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 49 | return {v, ct, at}; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | SkUNREACHABLE; |
| 53 | } |
| 54 | |
| 55 | class GrFragmentProcessor; |
| 56 | class GrGeometryProcessor; |
| 57 | |
| 58 | /* |
| 59 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 60 | * problems on android. |
| 61 | */ |
| 62 | template <> |
| 63 | SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() { |
| 64 | static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories; |
| 65 | return &gFactories; |
| 66 | } |
| 67 | |
| 68 | template <> |
| 69 | SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() { |
| 70 | static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories; |
| 71 | return &gFactories; |
| 72 | } |
| 73 | |
| 74 | SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() { |
| 75 | static SkTArray<GrXPFactoryTestFactory*, true> gFactories; |
| 76 | return &gFactories; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * To ensure we always have successful static initialization, before creating from the factories |
| 81 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 82 | * manually adjusted. |
| 83 | */ |
John Stiles | 7bf2600 | 2020-07-13 11:30:12 -0400 | [diff] [blame^] | 84 | static const int kFPFactoryCount = 38; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 85 | static const int kGPFactoryCount = 14; |
| 86 | static const int kXPFactoryCount = 4; |
| 87 | |
| 88 | template <> void GrFragmentProcessorTestFactory::VerifyFactoryCount() { |
| 89 | if (kFPFactoryCount != GetFactories()->count()) { |
| 90 | SkDebugf("\nExpected %d fragment processor factories, found %d.\n", kFPFactoryCount, |
| 91 | GetFactories()->count()); |
| 92 | SK_ABORT("Wrong number of fragment processor factories!"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | template <> void GrGeometryProcessorTestFactory::VerifyFactoryCount() { |
| 97 | if (kGPFactoryCount != GetFactories()->count()) { |
| 98 | SkDebugf("\nExpected %d geometry processor factories, found %d.\n", kGPFactoryCount, |
| 99 | GetFactories()->count()); |
| 100 | SK_ABORT("Wrong number of geometry processor factories!"); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void GrXPFactoryTestFactory::VerifyFactoryCount() { |
| 105 | if (kXPFactoryCount != GetFactories()->count()) { |
| 106 | SkDebugf("\nExpected %d xp factory factories, found %d.\n", kXPFactoryCount, |
| 107 | GetFactories()->count()); |
| 108 | SK_ABORT("Wrong number of xp factory factories!"); |
| 109 | } |
| 110 | } |
| 111 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 112 | std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 113 | std::unique_ptr<GrFragmentProcessor> fp; |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 114 | do { |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 115 | fp = GrFragmentProcessorTestFactory::Make(data); |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 116 | SkASSERT(fp); |
| 117 | } while (fp->numChildProcessors() != 0); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 118 | return fp; |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 119 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 120 | #endif |