blob: 08f437d50a06e58fa9376c18ad4d68a433ee88bb [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"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +00009#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000010#include "GrCoordTransform.h"
joshualitt2e3b3e32014-12-09 13:31:14 -080011#include "GrGeometryProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080012#include "GrInvariantOutput.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000013#include "GrMemoryPool.h"
egdaniel915187b2014-12-05 12:58:28 -080014#include "GrXferProcessor.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000015#include "SkTLS.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000016
joshualitt9e87fa72014-10-09 13:12:35 -070017#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
18
joshualitteb2a6762014-12-04 11:35:33 -080019class GrFragmentProcessor;
20class GrGeometryProcessor;
21
joshualitt9e87fa72014-10-09 13:12:35 -070022/*
23 * Originally these were both in the processor unit test header, but then it seemed to cause linker
24 * problems on android.
25 */
26template<>
27SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
28GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
29 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
30 return &gFactories;
31}
32
33template<>
egdanielc2304142014-12-11 13:15:13 -080034SkTArray<GrProcessorTestFactory<GrXPFactory>*, true>*
35GrProcessorTestFactory<GrXPFactory>::GetFactories() {
36 static SkTArray<GrProcessorTestFactory<GrXPFactory>*, true> gFactories;
egdaniel378092f2014-12-03 10:40:13 -080037 return &gFactories;
38}
39
40template<>
joshualitt9e87fa72014-10-09 13:12:35 -070041SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
42GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
43 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
44 return &gFactories;
45}
46
47/*
48 * To ensure we always have successful static initialization, before creating from the factories
49 * we verify the count is as expected. If a new factory is added, then these numbers must be
50 * manually adjusted.
51 */
52static const int kFPFactoryCount = 37;
joshualitt4973d9d2014-11-08 09:24:25 -080053static const int kGPFactoryCount = 14;
egdaniel54f0e9d2015-01-16 06:29:47 -080054static const int kXPFactoryCount = 5;
joshualitt9e87fa72014-10-09 13:12:35 -070055
56template<>
57void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
58 if (kFPFactoryCount != GetFactories()->count()) {
59 SkFAIL("Wrong number of fragment processor factories!");
60 }
61}
62
63template<>
64void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
65 if (kGPFactoryCount != GetFactories()->count()) {
66 SkFAIL("Wrong number of geometry processor factories!");
67 }
68}
69
egdaniel378092f2014-12-03 10:40:13 -080070template<>
egdanielc2304142014-12-11 13:15:13 -080071void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080072 if (kXPFactoryCount != GetFactories()->count()) {
egdanielc2304142014-12-11 13:15:13 -080073 SkFAIL("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080074 }
75}
76
joshualitt9e87fa72014-10-09 13:12:35 -070077#endif
78
joshualittb0a8a372014-09-23 09:50:21 -070079namespace GrProcessorUnitTest {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000080const SkMatrix& TestMatrix(SkRandom* random) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000081 static SkMatrix gMatrices[5];
82 static bool gOnce;
83 if (!gOnce) {
84 gMatrices[0].reset();
85 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
86 gMatrices[2].setRotate(SkIntToScalar(17));
87 gMatrices[3].setRotate(SkIntToScalar(185));
88 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
89 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
90 gMatrices[4].setRotate(SkIntToScalar(215));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000091 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
92 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000093 gOnce = true;
94 }
95 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
96}
97}
98
joshualittb0a8a372014-09-23 09:50:21 -070099class GrProcessor_Globals {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000100public:
101 static GrMemoryPool* GetTLS() {
102 return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
103 }
104
105private:
106 static void* CreateTLS() {
107 return SkNEW_ARGS(GrMemoryPool, (4096, 4096));
108 }
109
110 static void DeleteTLS(void* pool) {
111 SkDELETE(reinterpret_cast<GrMemoryPool*>(pool));
112 }
113};
114
joshualitteb2a6762014-12-04 11:35:33 -0800115int32_t GrProcessor::gCurrProcessorClassID =
116 GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000117
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000118///////////////////////////////////////////////////////////////////////////////
119
joshualittb0a8a372014-09-23 09:50:21 -0700120GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000121
joshualittb0a8a372014-09-23 09:50:21 -0700122void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000123 fTextureAccesses.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700124 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000125}
126
joshualittb0a8a372014-09-23 09:50:21 -0700127void* GrProcessor::operator new(size_t size) {
128 return GrProcessor_Globals::GetTLS()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000129}
130
joshualittb0a8a372014-09-23 09:50:21 -0700131void GrProcessor::operator delete(void* target) {
132 GrProcessor_Globals::GetTLS()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000133}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000134
bsalomon420d7e92014-10-16 09:18:09 -0700135bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const {
136 if (this->numTextures() != that.numTextures()) {
137 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000138 }
bsalomon420d7e92014-10-16 09:18:09 -0700139 for (int i = 0; i < this->numTextures(); ++i) {
140 if (this->textureAccess(i) != that.textureAccess(i)) {
141 return false;
142 }
143 }
144 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000145}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700146
joshualitta5305a12014-10-10 17:47:00 -0700147///////////////////////////////////////////////////////////////////////////////////////////////////
148
149void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
150 fCoordTransforms.push_back(transform);
joshualitt290c09b2014-12-19 13:45:20 -0800151 fUsesLocalCoords = fUsesLocalCoords || transform->sourceCoords() == kLocal_GrCoordSet;
bsalomonf2765412014-10-15 18:34:46 -0700152 SkDEBUGCODE(transform->setInProcessor();)
joshualitta5305a12014-10-10 17:47:00 -0700153}
bsalomonde258cd2014-10-15 19:06:21 -0700154
155bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
156 if (fCoordTransforms.count() != that.fCoordTransforms.count()) {
157 return false;
158 }
159 int count = fCoordTransforms.count();
160 for (int i = 0; i < count; ++i) {
161 if (*fCoordTransforms[i] != *that.fCoordTransforms[i]) {
162 return false;
163 }
164 }
165 return true;
166}
joshualittc07379d2014-11-20 14:50:39 -0800167
joshualitt56995b52014-12-11 15:44:02 -0800168void GrFragmentProcessor::computeInvariantOutput(GrInvariantOutput* inout) const {
169 this->onComputeInvariantOutput(inout);
170}
171
joshualittc07379d2014-11-20 14:50:39 -0800172///////////////////////////////////////////////////////////////////////////////////////////////////
173
egdaniel915187b2014-12-05 12:58:28 -0800174// Initial static variable from GrXPFactory
175int32_t GrXPFactory::gCurrXPFClassID =
176 GrXPFactory::kIllegalXPFClassID;