blob: 1d6aef9aa969f4fd3a7f0c4f222faf84761186a3 [file] [log] [blame]
bsalomon506c8022015-09-14 13:16:26 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProcessorUnitTest.h"
bsalomon506c8022015-09-14 13:16:26 -07009
Robert Phillips4e105e22020-07-16 09:18:50 -040010#include "include/gpu/GrRecordingContext.h"
Brian Salomon766098d2019-12-18 10:41:58 -050011#include "src/gpu/GrFragmentProcessor.h"
Robert Phillips4e105e22020-07-16 09:18:50 -040012#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon766098d2019-12-18 10:41:58 -050013
Hal Canary6f6961e2017-01-31 13:50:44 -050014#if GR_TEST_UTILS
15
John Stiles6609cb62020-07-17 14:52:12 -040016GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context,
17 int numViews, const ViewInfo views[],
18 std::unique_ptr<GrFragmentProcessor> inputFP)
19 : fRandom(random), fContext(context), fInputFP(std::move(inputFP)) {
Greg Daniel026a60c2020-02-12 10:53:51 -050020 fViews.reset(views, numViews);
Brian Salomon766098d2019-12-18 10:41:58 -050021 fArena = std::unique_ptr<SkArenaAlloc>(new SkArenaAlloc(1000));
22}
23
John Stilese911ce52020-07-17 13:32:27 -040024GrProcessorTestData::~GrProcessorTestData() {}
Brian Salomon766098d2019-12-18 10:41:58 -050025
26GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); }
27
28const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
29
John Stiles6609cb62020-07-17 14:52:12 -040030std::unique_ptr<GrFragmentProcessor> GrProcessorTestData::inputFP() { return std::move(fInputFP); }
31
Greg Daniel026a60c2020-02-12 10:53:51 -050032GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
33 SkASSERT(!fViews.empty());
34 return fViews[fRandom->nextULessThan(fViews.count())];
Brian Salomon766098d2019-12-18 10:41:58 -050035}
36
Greg Daniel026a60c2020-02-12 10:53:51 -050037GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
Brian Salomon766098d2019-12-18 10:41:58 -050038 int numAlphaOnly = 0;
Greg Daniel026a60c2020-02-12 10:53:51 -050039 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050040 if (GrColorTypeIsAlphaOnly(ct)) {
41 ++numAlphaOnly;
42 }
43 }
44 SkASSERT(numAlphaOnly);
45 int idx = fRandom->nextULessThan(numAlphaOnly);
Greg Daniel026a60c2020-02-12 10:53:51 -050046 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050047 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
Greg Daniel026a60c2020-02-12 10:53:51 -050048 return {v, ct, at};
Brian Salomon766098d2019-12-18 10:41:58 -050049 }
50 }
51 SkUNREACHABLE;
52}
53
54class GrFragmentProcessor;
55class GrGeometryProcessor;
56
57/*
58 * Originally these were both in the processor unit test header, but then it seemed to cause linker
59 * problems on android.
60 */
61template <>
62SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
63 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
64 return &gFactories;
65}
66
67template <>
68SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
69 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
70 return &gFactories;
71}
72
73SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
74 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
75 return &gFactories;
76}
77
78/*
79 * To ensure we always have successful static initialization, before creating from the factories
80 * we verify the count is as expected. If a new factory is added, then these numbers must be
81 * manually adjusted.
82 */
John Stiles85894302020-07-13 11:39:52 -040083static const int kFPFactoryCount = 37;
Brian Salomon766098d2019-12-18 10:41:58 -050084static const int kGPFactoryCount = 14;
85static const int kXPFactoryCount = 4;
86
87template <> void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
88 if (kFPFactoryCount != GetFactories()->count()) {
89 SkDebugf("\nExpected %d fragment processor factories, found %d.\n", kFPFactoryCount,
90 GetFactories()->count());
91 SK_ABORT("Wrong number of fragment processor factories!");
92 }
93}
94
95template <> void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
96 if (kGPFactoryCount != GetFactories()->count()) {
97 SkDebugf("\nExpected %d geometry processor factories, found %d.\n", kGPFactoryCount,
98 GetFactories()->count());
99 SK_ABORT("Wrong number of geometry processor factories!");
100 }
101}
102
103void GrXPFactoryTestFactory::VerifyFactoryCount() {
104 if (kXPFactoryCount != GetFactories()->count()) {
105 SkDebugf("\nExpected %d xp factory factories, found %d.\n", kXPFactoryCount,
106 GetFactories()->count());
107 SK_ABORT("Wrong number of xp factory factories!");
108 }
109}
110
Brian Salomonaff329b2017-08-11 09:40:37 -0400111std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400112 std::unique_ptr<GrFragmentProcessor> fp;
bsalomon506c8022015-09-14 13:16:26 -0700113 do {
Brian Salomon1c053642017-07-24 10:16:19 -0400114 fp = GrFragmentProcessorTestFactory::Make(data);
bsalomon506c8022015-09-14 13:16:26 -0700115 SkASSERT(fp);
Brian Osman12c5d292020-07-13 16:11:35 -0400116 } while (fp->numNonNullChildProcessors() != 0);
bungeman06ca8ec2016-06-09 08:01:03 -0700117 return fp;
bsalomon506c8022015-09-14 13:16:26 -0700118}
Hal Canary6f6961e2017-01-31 13:50:44 -0500119#endif