blob: 1732e3a27a4f6239658d1c08f1e8ba23406882e3 [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;
41static const int kGPFactoryCount = 15;
42
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
joshualittb0a8a372014-09-23 09:50:21 -070095int32_t GrBackendProcessorFactory::fCurrEffectClassID =
96 GrBackendProcessorFactory::kIllegalEffectClassID;
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
119#ifdef SK_DEBUG
joshualittb0a8a372014-09-23 09:50:21 -0700120void GrProcessor::assertEquality(const GrProcessor& other) const {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000121 SkASSERT(this->numTextures() == other.numTextures());
122 for (int i = 0; i < this->numTextures(); ++i) {
123 SkASSERT(this->textureAccess(i) == other.textureAccess(i));
124 }
125}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700126
127void GrProcessor::InvariantOutput::validate() const {
128 if (fIsSingleComponent) {
129 SkASSERT(0 == fValidFlags || kRGBA_GrColorComponentFlags == fValidFlags);
130 if (kRGBA_GrColorComponentFlags == fValidFlags) {
131 SkASSERT(this->colorComponentsAllEqual());
132 }
133 }
134
135 SkASSERT(this->validPreMulColor());
136}
137
138bool GrProcessor::InvariantOutput::colorComponentsAllEqual() const {
139 unsigned colorA = GrColorUnpackA(fColor);
140 return(GrColorUnpackR(fColor) == colorA &&
141 GrColorUnpackG(fColor) == colorA &&
142 GrColorUnpackB(fColor) == colorA);
143}
144
145bool GrProcessor::InvariantOutput::validPreMulColor() const {
146 if (kA_GrColorComponentFlag & fValidFlags) {
147 float c[4];
148 GrColorToRGBAFloat(fColor, c);
149 if (kR_GrColorComponentFlag & fValidFlags) {
150 if (c[0] > c[3]) {
151 return false;
152 }
153 }
154 if (kG_GrColorComponentFlag & fValidFlags) {
155 if (c[1] > c[3]) {
156 return false;
157 }
158 }
159 if (kB_GrColorComponentFlag & fValidFlags) {
160 if (c[2] > c[3]) {
161 return false;
162 }
163 }
164 }
165 return true;
166}
joshualitta5305a12014-10-10 17:47:00 -0700167#endif // end DEBUG
egdaniel1a8ecdf2014-10-03 06:24:12 -0700168
joshualitta5305a12014-10-10 17:47:00 -0700169///////////////////////////////////////////////////////////////////////////////////////////////////
170
171void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
172 fCoordTransforms.push_back(transform);
173 SkDEBUGCODE(transform->setInEffect();)
174}