blob: 18f09ad33091ef7b8b4c67c10385b510ce93910d [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
Brian Salomon766098d2019-12-18 10:41:58 -050016GrProcessorTestData::GrProcessorTestData(SkRandom* random,
Robert Phillips4e105e22020-07-16 09:18:50 -040017 GrRecordingContext* 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
John Stilese911ce52020-07-17 13:32:27 -040025GrProcessorTestData::~GrProcessorTestData() {}
Brian Salomon766098d2019-12-18 10:41:58 -050026
27GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); }
28
29const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
30
Greg Daniel026a60c2020-02-12 10:53:51 -050031GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
32 SkASSERT(!fViews.empty());
33 return fViews[fRandom->nextULessThan(fViews.count())];
Brian Salomon766098d2019-12-18 10:41:58 -050034}
35
Greg Daniel026a60c2020-02-12 10:53:51 -050036GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
Brian Salomon766098d2019-12-18 10:41:58 -050037 int numAlphaOnly = 0;
Greg Daniel026a60c2020-02-12 10:53:51 -050038 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050039 if (GrColorTypeIsAlphaOnly(ct)) {
40 ++numAlphaOnly;
41 }
42 }
43 SkASSERT(numAlphaOnly);
44 int idx = fRandom->nextULessThan(numAlphaOnly);
Greg Daniel026a60c2020-02-12 10:53:51 -050045 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050046 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
Greg Daniel026a60c2020-02-12 10:53:51 -050047 return {v, ct, at};
Brian Salomon766098d2019-12-18 10:41:58 -050048 }
49 }
50 SkUNREACHABLE;
51}
52
53class GrFragmentProcessor;
54class GrGeometryProcessor;
55
56/*
57 * Originally these were both in the processor unit test header, but then it seemed to cause linker
58 * problems on android.
59 */
60template <>
61SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
62 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
63 return &gFactories;
64}
65
66template <>
67SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
68 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
69 return &gFactories;
70}
71
72SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
73 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
74 return &gFactories;
75}
76
77/*
78 * To ensure we always have successful static initialization, before creating from the factories
79 * we verify the count is as expected. If a new factory is added, then these numbers must be
80 * manually adjusted.
81 */
John Stiles85894302020-07-13 11:39:52 -040082static const int kFPFactoryCount = 37;
Brian Salomon766098d2019-12-18 10:41:58 -050083static const int kGPFactoryCount = 14;
84static const int kXPFactoryCount = 4;
85
86template <> void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
87 if (kFPFactoryCount != GetFactories()->count()) {
88 SkDebugf("\nExpected %d fragment processor factories, found %d.\n", kFPFactoryCount,
89 GetFactories()->count());
90 SK_ABORT("Wrong number of fragment processor factories!");
91 }
92}
93
94template <> void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
95 if (kGPFactoryCount != GetFactories()->count()) {
96 SkDebugf("\nExpected %d geometry processor factories, found %d.\n", kGPFactoryCount,
97 GetFactories()->count());
98 SK_ABORT("Wrong number of geometry processor factories!");
99 }
100}
101
102void GrXPFactoryTestFactory::VerifyFactoryCount() {
103 if (kXPFactoryCount != GetFactories()->count()) {
104 SkDebugf("\nExpected %d xp factory factories, found %d.\n", kXPFactoryCount,
105 GetFactories()->count());
106 SK_ABORT("Wrong number of xp factory factories!");
107 }
108}
109
Brian Salomonaff329b2017-08-11 09:40:37 -0400110std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400111 std::unique_ptr<GrFragmentProcessor> fp;
bsalomon506c8022015-09-14 13:16:26 -0700112 do {
Brian Salomon1c053642017-07-24 10:16:19 -0400113 fp = GrFragmentProcessorTestFactory::Make(data);
bsalomon506c8022015-09-14 13:16:26 -0700114 SkASSERT(fp);
Brian Osman12c5d292020-07-13 16:11:35 -0400115 } while (fp->numNonNullChildProcessors() != 0);
bungeman06ca8ec2016-06-09 08:01:03 -0700116 return fp;
bsalomon506c8022015-09-14 13:16:26 -0700117}
Hal Canary6f6961e2017-01-31 13:50:44 -0500118#endif