blob: ccd4d7f7e86bae2bb64350f8839991ae24000cc8 [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"
joshualittc07379d2014-11-20 14:50:39 -080011#include "GrGeometryData.h"
joshualitt2e3b3e32014-12-09 13:31:14 -080012#include "GrGeometryProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080013#include "GrInvariantOutput.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000014#include "GrMemoryPool.h"
egdaniel915187b2014-12-05 12:58:28 -080015#include "GrXferProcessor.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000016#include "SkTLS.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000017
joshualitt9e87fa72014-10-09 13:12:35 -070018#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
19
joshualitteb2a6762014-12-04 11:35:33 -080020class GrFragmentProcessor;
21class GrGeometryProcessor;
22
joshualitt9e87fa72014-10-09 13:12:35 -070023/*
24 * Originally these were both in the processor unit test header, but then it seemed to cause linker
25 * problems on android.
26 */
27template<>
28SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
29GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
30 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
31 return &gFactories;
32}
33
34template<>
egdaniel378092f2014-12-03 10:40:13 -080035SkTArray<GrProcessorTestFactory<GrXferProcessor>*, true>*
36GrProcessorTestFactory<GrXferProcessor>::GetFactories() {
37 static SkTArray<GrProcessorTestFactory<GrXferProcessor>*, true> gFactories;
38 return &gFactories;
39}
40
41template<>
joshualitt9e87fa72014-10-09 13:12:35 -070042SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
43GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
44 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
45 return &gFactories;
46}
47
48/*
49 * To ensure we always have successful static initialization, before creating from the factories
50 * we verify the count is as expected. If a new factory is added, then these numbers must be
51 * manually adjusted.
52 */
53static const int kFPFactoryCount = 37;
joshualitt4973d9d2014-11-08 09:24:25 -080054static const int kGPFactoryCount = 14;
egdaniel378092f2014-12-03 10:40:13 -080055static const int kXPFactoryCount = 0;
joshualitt9e87fa72014-10-09 13:12:35 -070056
57template<>
58void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
59 if (kFPFactoryCount != GetFactories()->count()) {
60 SkFAIL("Wrong number of fragment processor factories!");
61 }
62}
63
64template<>
65void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
66 if (kGPFactoryCount != GetFactories()->count()) {
67 SkFAIL("Wrong number of geometry processor factories!");
68 }
69}
70
egdaniel378092f2014-12-03 10:40:13 -080071template<>
72void GrProcessorTestFactory<GrXferProcessor>::VerifyFactoryCount() {
73 if (kXPFactoryCount != GetFactories()->count()) {
74 SkFAIL("Wrong number of xfer processor factories!");
75 }
76}
77
joshualitt9e87fa72014-10-09 13:12:35 -070078#endif
79
joshualittb0a8a372014-09-23 09:50:21 -070080namespace GrProcessorUnitTest {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000081const SkMatrix& TestMatrix(SkRandom* random) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000082 static SkMatrix gMatrices[5];
83 static bool gOnce;
84 if (!gOnce) {
85 gMatrices[0].reset();
86 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
87 gMatrices[2].setRotate(SkIntToScalar(17));
88 gMatrices[3].setRotate(SkIntToScalar(185));
89 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
90 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
91 gMatrices[4].setRotate(SkIntToScalar(215));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000092 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
93 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000094 gOnce = true;
95 }
96 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
97}
98}
99
joshualittb0a8a372014-09-23 09:50:21 -0700100class GrProcessor_Globals {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000101public:
102 static GrMemoryPool* GetTLS() {
103 return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
104 }
105
106private:
107 static void* CreateTLS() {
108 return SkNEW_ARGS(GrMemoryPool, (4096, 4096));
109 }
110
111 static void DeleteTLS(void* pool) {
112 SkDELETE(reinterpret_cast<GrMemoryPool*>(pool));
113 }
114};
115
joshualitteb2a6762014-12-04 11:35:33 -0800116int32_t GrProcessor::gCurrProcessorClassID =
117 GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000118
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000119///////////////////////////////////////////////////////////////////////////////
120
joshualittb0a8a372014-09-23 09:50:21 -0700121GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000122
joshualittb0a8a372014-09-23 09:50:21 -0700123void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000124 fTextureAccesses.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700125 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000126}
127
joshualittb0a8a372014-09-23 09:50:21 -0700128void* GrProcessor::operator new(size_t size) {
129 return GrProcessor_Globals::GetTLS()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000130}
131
joshualittb0a8a372014-09-23 09:50:21 -0700132void GrProcessor::operator delete(void* target) {
133 GrProcessor_Globals::GetTLS()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000134}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000135
bsalomon420d7e92014-10-16 09:18:09 -0700136bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const {
137 if (this->numTextures() != that.numTextures()) {
138 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000139 }
bsalomon420d7e92014-10-16 09:18:09 -0700140 for (int i = 0; i < this->numTextures(); ++i) {
141 if (this->textureAccess(i) != that.textureAccess(i)) {
142 return false;
143 }
144 }
145 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000146}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700147
egdaniel605dd0f2014-11-12 08:35:25 -0800148void GrProcessor::computeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel605dd0f2014-11-12 08:35:25 -0800149 this->onComputeInvariantOutput(inout);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700150}
151
joshualitta5305a12014-10-10 17:47:00 -0700152///////////////////////////////////////////////////////////////////////////////////////////////////
153
154void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
155 fCoordTransforms.push_back(transform);
bsalomonf2765412014-10-15 18:34:46 -0700156 SkDEBUGCODE(transform->setInProcessor();)
joshualitta5305a12014-10-10 17:47:00 -0700157}
bsalomonde258cd2014-10-15 19:06:21 -0700158
159bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
160 if (fCoordTransforms.count() != that.fCoordTransforms.count()) {
161 return false;
162 }
163 int count = fCoordTransforms.count();
164 for (int i = 0; i < count; ++i) {
165 if (*fCoordTransforms[i] != *that.fCoordTransforms[i]) {
166 return false;
167 }
168 }
169 return true;
170}
joshualittc07379d2014-11-20 14:50:39 -0800171
172///////////////////////////////////////////////////////////////////////////////////////////////////
173
joshualitt2e3b3e32014-12-09 13:31:14 -0800174void GrGeometryProcessor::computeInvariantColor(GrInvariantOutput* intout) const {
175
176}
177
178///////////////////////////////////////////////////////////////////////////////////////////////////
179
joshualittc07379d2014-11-20 14:50:39 -0800180/*
181 * GrGeometryData shares the same pool so it lives in this file too
182 */
183void* GrGeometryData::operator new(size_t size) {
184 return GrProcessor_Globals::GetTLS()->allocate(size);
185}
186
187void GrGeometryData::operator delete(void* target) {
188 GrProcessor_Globals::GetTLS()->release(target);
189}
egdaniel915187b2014-12-05 12:58:28 -0800190
191///////////////////////////////////////////////////////////////////////////////////////////////////
192
193// Initial static variable from GrXPFactory
194int32_t GrXPFactory::gCurrXPFClassID =
195 GrXPFactory::kIllegalXPFClassID;
196