blob: 7b8cd20f41a62c5f18edd2caa21b15cc6b5b2973 [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
Brian Salomon766098d2019-12-18 10:41:58 -050010#include "include/gpu/GrContext.h"
11#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrFragmentProcessor.h"
13
Hal Canary6f6961e2017-01-31 13:50:44 -050014#if GR_TEST_UTILS
15
Brian Salomon766098d2019-12-18 10:41:58 -050016GrProcessorTestData::GrProcessorTestData(SkRandom* random,
17 GrContext* context,
Greg Daniel026a60c2020-02-12 10:53:51 -050018 int numViews,
19 const ViewInfo views[])
Brian Salomon766098d2019-12-18 10:41:58 -050020 : fRandom(random), fContext(context) {
Greg Daniel026a60c2020-02-12 10:53:51 -050021 fViews.reset(views, numViews);
Brian Salomon766098d2019-12-18 10:41:58 -050022 fArena = std::unique_ptr<SkArenaAlloc>(new SkArenaAlloc(1000));
23}
24
25GrResourceProvider* GrProcessorTestData::resourceProvider() {
26 return fContext->priv().resourceProvider();
27}
28
29GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); }
30
31const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
32
Greg Daniel026a60c2020-02-12 10:53:51 -050033GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
34 SkASSERT(!fViews.empty());
35 return fViews[fRandom->nextULessThan(fViews.count())];
Brian Salomon766098d2019-12-18 10:41:58 -050036}
37
Greg Daniel026a60c2020-02-12 10:53:51 -050038GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
Brian Salomon766098d2019-12-18 10:41:58 -050039 int numAlphaOnly = 0;
Greg Daniel026a60c2020-02-12 10:53:51 -050040 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050041 if (GrColorTypeIsAlphaOnly(ct)) {
42 ++numAlphaOnly;
43 }
44 }
45 SkASSERT(numAlphaOnly);
46 int idx = fRandom->nextULessThan(numAlphaOnly);
Greg Daniel026a60c2020-02-12 10:53:51 -050047 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050048 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
Greg Daniel026a60c2020-02-12 10:53:51 -050049 return {v, ct, at};
Brian Salomon766098d2019-12-18 10:41:58 -050050 }
51 }
52 SkUNREACHABLE;
53}
54
55class GrFragmentProcessor;
56class 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 */
62template <>
63SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
64 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
65 return &gFactories;
66}
67
68template <>
69SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
70 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
71 return &gFactories;
72}
73
74SkTArray<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 Stiles7bf26002020-07-13 11:30:12 -040084static const int kFPFactoryCount = 38;
Brian Salomon766098d2019-12-18 10:41:58 -050085static const int kGPFactoryCount = 14;
86static const int kXPFactoryCount = 4;
87
88template <> 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
96template <> 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
104void 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 Salomonaff329b2017-08-11 09:40:37 -0400112std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400113 std::unique_ptr<GrFragmentProcessor> fp;
bsalomon506c8022015-09-14 13:16:26 -0700114 do {
Brian Salomon1c053642017-07-24 10:16:19 -0400115 fp = GrFragmentProcessorTestFactory::Make(data);
bsalomon506c8022015-09-14 13:16:26 -0700116 SkASSERT(fp);
117 } while (fp->numChildProcessors() != 0);
bungeman06ca8ec2016-06-09 08:01:03 -0700118 return fp;
bsalomon506c8022015-09-14 13:16:26 -0700119}
Hal Canary6f6961e2017-01-31 13:50:44 -0500120#endif