blob: 852954bd88d2b2f6fddc253c462efeea85b26d14 [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,
John Stiles278b4a62020-07-17 16:44:49 -040017 int numViews, const ViewInfo views[])
18 : GrProcessorTestData(random, context, numViews, views, /*inputFP=*/nullptr) {}
19
20GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context,
John Stiles6609cb62020-07-17 14:52:12 -040021 int numViews, const ViewInfo views[],
22 std::unique_ptr<GrFragmentProcessor> inputFP)
23 : fRandom(random), fContext(context), fInputFP(std::move(inputFP)) {
Greg Daniel026a60c2020-02-12 10:53:51 -050024 fViews.reset(views, numViews);
Brian Salomon766098d2019-12-18 10:41:58 -050025 fArena = std::unique_ptr<SkArenaAlloc>(new SkArenaAlloc(1000));
26}
27
John Stilese911ce52020-07-17 13:32:27 -040028GrProcessorTestData::~GrProcessorTestData() {}
Brian Salomon766098d2019-12-18 10:41:58 -050029
30GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); }
31
32const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
33
John Stiles6609cb62020-07-17 14:52:12 -040034std::unique_ptr<GrFragmentProcessor> GrProcessorTestData::inputFP() { return std::move(fInputFP); }
35
Greg Daniel026a60c2020-02-12 10:53:51 -050036GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
37 SkASSERT(!fViews.empty());
38 return fViews[fRandom->nextULessThan(fViews.count())];
Brian Salomon766098d2019-12-18 10:41:58 -050039}
40
Greg Daniel026a60c2020-02-12 10:53:51 -050041GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
Brian Salomon766098d2019-12-18 10:41:58 -050042 int numAlphaOnly = 0;
Greg Daniel026a60c2020-02-12 10:53:51 -050043 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050044 if (GrColorTypeIsAlphaOnly(ct)) {
45 ++numAlphaOnly;
46 }
47 }
48 SkASSERT(numAlphaOnly);
49 int idx = fRandom->nextULessThan(numAlphaOnly);
Greg Daniel026a60c2020-02-12 10:53:51 -050050 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050051 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
Greg Daniel026a60c2020-02-12 10:53:51 -050052 return {v, ct, at};
Brian Salomon766098d2019-12-18 10:41:58 -050053 }
54 }
55 SkUNREACHABLE;
56}
57
58class GrFragmentProcessor;
59class 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 */
65template <>
66SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
67 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
68 return &gFactories;
69}
70
71template <>
72SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
73 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
74 return &gFactories;
75}
76
77SkTArray<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 Stiles85894302020-07-13 11:39:52 -040087static const int kFPFactoryCount = 37;
Brian Salomon766098d2019-12-18 10:41:58 -050088static const int kGPFactoryCount = 14;
89static const int kXPFactoryCount = 4;
90
91template <> 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
99template <> 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
107void 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 Salomonaff329b2017-08-11 09:40:37 -0400115std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400116 std::unique_ptr<GrFragmentProcessor> fp;
bsalomon506c8022015-09-14 13:16:26 -0700117 do {
Brian Salomon1c053642017-07-24 10:16:19 -0400118 fp = GrFragmentProcessorTestFactory::Make(data);
bsalomon506c8022015-09-14 13:16:26 -0700119 SkASSERT(fp);
Brian Osman12c5d292020-07-13 16:11:35 -0400120 } while (fp->numNonNullChildProcessors() != 0);
bungeman06ca8ec2016-06-09 08:01:03 -0700121 return fp;
bsalomon506c8022015-09-14 13:16:26 -0700122}
Hal Canary6f6961e2017-01-31 13:50:44 -0500123#endif