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