blob: 3de1dcf8911e4bb678a62a0f05fab374e7b11ba3 [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
2 * Copyright 2012 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
joshualittb0a8a372014-09-23 09:50:21 -07008#include "GrProcessor.h"
9#include "GrBackendProcessorFactory.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000010#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000011#include "GrCoordTransform.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000012#include "GrMemoryPool.h"
13#include "SkTLS.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000014
joshualitt9e87fa72014-10-09 13:12:35 -070015#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
16
17/*
18 * Originally these were both in the processor unit test header, but then it seemed to cause linker
19 * problems on android.
20 */
21template<>
22SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
23GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
24 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
25 return &gFactories;
26}
27
28template<>
29SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
30GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
31 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
32 return &gFactories;
33}
34
35/*
36 * To ensure we always have successful static initialization, before creating from the factories
37 * we verify the count is as expected. If a new factory is added, then these numbers must be
38 * manually adjusted.
39 */
40static const int kFPFactoryCount = 37;
joshualitt4973d9d2014-11-08 09:24:25 -080041static const int kGPFactoryCount = 14;
joshualitt9e87fa72014-10-09 13:12:35 -070042
43template<>
44void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
45 if (kFPFactoryCount != GetFactories()->count()) {
46 SkFAIL("Wrong number of fragment processor factories!");
47 }
48}
49
50template<>
51void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
52 if (kGPFactoryCount != GetFactories()->count()) {
53 SkFAIL("Wrong number of geometry processor factories!");
54 }
55}
56
57#endif
58
joshualittb0a8a372014-09-23 09:50:21 -070059namespace GrProcessorUnitTest {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000060const SkMatrix& TestMatrix(SkRandom* random) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000061 static SkMatrix gMatrices[5];
62 static bool gOnce;
63 if (!gOnce) {
64 gMatrices[0].reset();
65 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
66 gMatrices[2].setRotate(SkIntToScalar(17));
67 gMatrices[3].setRotate(SkIntToScalar(185));
68 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
69 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
70 gMatrices[4].setRotate(SkIntToScalar(215));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000071 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
72 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000073 gOnce = true;
74 }
75 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
76}
77}
78
joshualittb0a8a372014-09-23 09:50:21 -070079class GrProcessor_Globals {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000080public:
81 static GrMemoryPool* GetTLS() {
82 return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
83 }
84
85private:
86 static void* CreateTLS() {
87 return SkNEW_ARGS(GrMemoryPool, (4096, 4096));
88 }
89
90 static void DeleteTLS(void* pool) {
91 SkDELETE(reinterpret_cast<GrMemoryPool*>(pool));
92 }
93};
94
bsalomonf2765412014-10-15 18:34:46 -070095int32_t GrBackendProcessorFactory::fCurrProcessorClassID =
96 GrBackendProcessorFactory::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +000097
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000098///////////////////////////////////////////////////////////////////////////////
99
joshualittb0a8a372014-09-23 09:50:21 -0700100GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000101
joshualittb0a8a372014-09-23 09:50:21 -0700102const char* GrProcessor::name() const {
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000103 return this->getFactory().name();
104}
105
joshualittb0a8a372014-09-23 09:50:21 -0700106void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000107 fTextureAccesses.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700108 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000109}
110
joshualittb0a8a372014-09-23 09:50:21 -0700111void* GrProcessor::operator new(size_t size) {
112 return GrProcessor_Globals::GetTLS()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000113}
114
joshualittb0a8a372014-09-23 09:50:21 -0700115void GrProcessor::operator delete(void* target) {
116 GrProcessor_Globals::GetTLS()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000117}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000118
bsalomon420d7e92014-10-16 09:18:09 -0700119bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const {
120 if (this->numTextures() != that.numTextures()) {
121 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000122 }
bsalomon420d7e92014-10-16 09:18:09 -0700123 for (int i = 0; i < this->numTextures(); ++i) {
124 if (this->textureAccess(i) != that.textureAccess(i)) {
125 return false;
126 }
127 }
128 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000129}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700130
bsalomon420d7e92014-10-16 09:18:09 -0700131#ifdef SK_DEBUG
132
egdaniel1a8ecdf2014-10-03 06:24:12 -0700133void GrProcessor::InvariantOutput::validate() const {
134 if (fIsSingleComponent) {
135 SkASSERT(0 == fValidFlags || kRGBA_GrColorComponentFlags == fValidFlags);
136 if (kRGBA_GrColorComponentFlags == fValidFlags) {
137 SkASSERT(this->colorComponentsAllEqual());
138 }
139 }
140
141 SkASSERT(this->validPreMulColor());
egdaniel9e4d6d12014-10-15 13:49:02 -0700142
143 // If we claim that we are not using the input color we must not be modulating the input.
144 SkASSERT(fNonMulStageFound || fWillUseInputColor);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700145}
146
147bool GrProcessor::InvariantOutput::colorComponentsAllEqual() const {
148 unsigned colorA = GrColorUnpackA(fColor);
149 return(GrColorUnpackR(fColor) == colorA &&
150 GrColorUnpackG(fColor) == colorA &&
151 GrColorUnpackB(fColor) == colorA);
152}
153
154bool GrProcessor::InvariantOutput::validPreMulColor() const {
155 if (kA_GrColorComponentFlag & fValidFlags) {
156 float c[4];
157 GrColorToRGBAFloat(fColor, c);
158 if (kR_GrColorComponentFlag & fValidFlags) {
159 if (c[0] > c[3]) {
160 return false;
161 }
162 }
163 if (kG_GrColorComponentFlag & fValidFlags) {
164 if (c[1] > c[3]) {
165 return false;
166 }
167 }
168 if (kB_GrColorComponentFlag & fValidFlags) {
169 if (c[2] > c[3]) {
170 return false;
171 }
172 }
173 }
174 return true;
175}
joshualitta5305a12014-10-10 17:47:00 -0700176#endif // end DEBUG
egdaniel1a8ecdf2014-10-03 06:24:12 -0700177
joshualitta5305a12014-10-10 17:47:00 -0700178///////////////////////////////////////////////////////////////////////////////////////////////////
179
180void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
181 fCoordTransforms.push_back(transform);
bsalomonf2765412014-10-15 18:34:46 -0700182 SkDEBUGCODE(transform->setInProcessor();)
joshualitta5305a12014-10-10 17:47:00 -0700183}
bsalomonde258cd2014-10-15 19:06:21 -0700184
185bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
186 if (fCoordTransforms.count() != that.fCoordTransforms.count()) {
187 return false;
188 }
189 int count = fCoordTransforms.count();
190 for (int i = 0; i < count; ++i) {
191 if (*fCoordTransforms[i] != *that.fCoordTransforms[i]) {
192 return false;
193 }
194 }
195 return true;
196}