blob: 4f10d5f3e160b11809d97de3025e8af4bc5d2cf4 [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
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
Greg Daniel026a60c2020-02-12 10:53:51 -050030GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
31 SkASSERT(!fViews.empty());
32 return fViews[fRandom->nextULessThan(fViews.count())];
Brian Salomon766098d2019-12-18 10:41:58 -050033}
34
Greg Daniel026a60c2020-02-12 10:53:51 -050035GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
Brian Salomon766098d2019-12-18 10:41:58 -050036 int numAlphaOnly = 0;
Greg Daniel026a60c2020-02-12 10:53:51 -050037 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050038 if (GrColorTypeIsAlphaOnly(ct)) {
39 ++numAlphaOnly;
40 }
41 }
42 SkASSERT(numAlphaOnly);
43 int idx = fRandom->nextULessThan(numAlphaOnly);
Greg Daniel026a60c2020-02-12 10:53:51 -050044 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050045 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
Greg Daniel026a60c2020-02-12 10:53:51 -050046 return {v, ct, at};
Brian Salomon766098d2019-12-18 10:41:58 -050047 }
48 }
49 SkUNREACHABLE;
50}
51
52class GrFragmentProcessor;
53class 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 */
59template <>
60SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
61 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
62 return &gFactories;
63}
64
65template <>
66SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
67 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
68 return &gFactories;
69}
70
71SkTArray<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 Stiles85894302020-07-13 11:39:52 -040081static const int kFPFactoryCount = 37;
Brian Salomon766098d2019-12-18 10:41:58 -050082static const int kGPFactoryCount = 14;
83static const int kXPFactoryCount = 4;
84
85template <> 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
93template <> 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
101void 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 Salomonaff329b2017-08-11 09:40:37 -0400109std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400110 std::unique_ptr<GrFragmentProcessor> fp;
bsalomon506c8022015-09-14 13:16:26 -0700111 do {
Brian Salomon1c053642017-07-24 10:16:19 -0400112 fp = GrFragmentProcessorTestFactory::Make(data);
bsalomon506c8022015-09-14 13:16:26 -0700113 SkASSERT(fp);
Brian Osman12c5d292020-07-13 16:11:35 -0400114 } while (fp->numNonNullChildProcessors() != 0);
bungeman06ca8ec2016-06-09 08:01:03 -0700115 return fp;
bsalomon506c8022015-09-14 13:16:26 -0700116}
Hal Canary6f6961e2017-01-31 13:50:44 -0500117#endif