1/*
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
8#include "src/gpu/GrProcessorUnitTest.h"
9
10#include "include/gpu/GrContext.h"
11#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrFragmentProcessor.h"
13
14#if GR_TEST_UTILS
15
16GrProcessorTestData::GrProcessorTestData(SkRandom* random,
17 GrContext* context,
18 int numViews,
19 const ViewInfo views[])
20 : fRandom(random), fContext(context) {
21 fViews.reset(views, numViews);
22 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
33GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
34 SkASSERT(!fViews.empty());
35 return fViews[fRandom->nextULessThan(fViews.count())];
36}
37
38GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
39 int numAlphaOnly = 0;
40 for (const auto& [v, ct, at] : fViews) {
41 if (GrColorTypeIsAlphaOnly(ct)) {
42 ++numAlphaOnly;
43 }
44 }
45 SkASSERT(numAlphaOnly);
46 int idx = fRandom->nextULessThan(numAlphaOnly);
47 for (const auto& [v, ct, at] : fViews) {
48 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
49 return {v, ct, at};
50 }
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 */
84static const int kFPFactoryCount = 35;
85static 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
112std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
113 std::unique_ptr<GrFragmentProcessor> fp;
114 do {
115 fp = GrFragmentProcessorTestFactory::Make(data);
116 SkASSERT(fp);
117 } while (fp->numChildProcessors() != 0);
118 return fp;
119}
120#endif
121