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