blob: 810c751f1c7686fa2bac08d48a98e751229dd00e [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"
egdaniel605dd0f2014-11-12 08:35:25 -080012#include "GrInvariantOutput.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000013#include "GrMemoryPool.h"
14#include "SkTLS.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000015
joshualitt9e87fa72014-10-09 13:12:35 -070016#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
17
joshualitteb2a6762014-12-04 11:35:33 -080018class GrFragmentProcessor;
19class GrGeometryProcessor;
20
joshualitt9e87fa72014-10-09 13:12:35 -070021/*
22 * Originally these were both in the processor unit test header, but then it seemed to cause linker
23 * problems on android.
24 */
25template<>
26SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
27GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
28 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
29 return &gFactories;
30}
31
32template<>
egdaniel378092f2014-12-03 10:40:13 -080033SkTArray<GrProcessorTestFactory<GrXferProcessor>*, true>*
34GrProcessorTestFactory<GrXferProcessor>::GetFactories() {
35 static SkTArray<GrProcessorTestFactory<GrXferProcessor>*, true> gFactories;
36 return &gFactories;
37}
38
39template<>
joshualitt9e87fa72014-10-09 13:12:35 -070040SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
41GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
42 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
43 return &gFactories;
44}
45
46/*
47 * To ensure we always have successful static initialization, before creating from the factories
48 * we verify the count is as expected. If a new factory is added, then these numbers must be
49 * manually adjusted.
50 */
51static const int kFPFactoryCount = 37;
joshualitt4973d9d2014-11-08 09:24:25 -080052static const int kGPFactoryCount = 14;
egdaniel378092f2014-12-03 10:40:13 -080053static const int kXPFactoryCount = 0;
joshualitt9e87fa72014-10-09 13:12:35 -070054
55template<>
56void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
57 if (kFPFactoryCount != GetFactories()->count()) {
58 SkFAIL("Wrong number of fragment processor factories!");
59 }
60}
61
62template<>
63void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
64 if (kGPFactoryCount != GetFactories()->count()) {
65 SkFAIL("Wrong number of geometry processor factories!");
66 }
67}
68
egdaniel378092f2014-12-03 10:40:13 -080069template<>
70void GrProcessorTestFactory<GrXferProcessor>::VerifyFactoryCount() {
71 if (kXPFactoryCount != GetFactories()->count()) {
72 SkFAIL("Wrong number of xfer processor factories!");
73 }
74}
75
joshualitt9e87fa72014-10-09 13:12:35 -070076#endif
77
joshualittb0a8a372014-09-23 09:50:21 -070078namespace GrProcessorUnitTest {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000079const SkMatrix& TestMatrix(SkRandom* random) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000080 static SkMatrix gMatrices[5];
81 static bool gOnce;
82 if (!gOnce) {
83 gMatrices[0].reset();
84 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
85 gMatrices[2].setRotate(SkIntToScalar(17));
86 gMatrices[3].setRotate(SkIntToScalar(185));
87 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
88 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
89 gMatrices[4].setRotate(SkIntToScalar(215));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000090 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
91 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000092 gOnce = true;
93 }
94 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
95}
96}
97
joshualittb0a8a372014-09-23 09:50:21 -070098class GrProcessor_Globals {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000099public:
100 static GrMemoryPool* GetTLS() {
101 return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
102 }
103
104private:
105 static void* CreateTLS() {
106 return SkNEW_ARGS(GrMemoryPool, (4096, 4096));
107 }
108
109 static void DeleteTLS(void* pool) {
110 SkDELETE(reinterpret_cast<GrMemoryPool*>(pool));
111 }
112};
113
joshualitteb2a6762014-12-04 11:35:33 -0800114int32_t GrProcessor::gCurrProcessorClassID =
115 GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000116
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000117///////////////////////////////////////////////////////////////////////////////
118
joshualittb0a8a372014-09-23 09:50:21 -0700119GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000120
joshualittb0a8a372014-09-23 09:50:21 -0700121void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000122 fTextureAccesses.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700123 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000124}
125
joshualittb0a8a372014-09-23 09:50:21 -0700126void* GrProcessor::operator new(size_t size) {
127 return GrProcessor_Globals::GetTLS()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000128}
129
joshualittb0a8a372014-09-23 09:50:21 -0700130void GrProcessor::operator delete(void* target) {
131 GrProcessor_Globals::GetTLS()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000132}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000133
bsalomon420d7e92014-10-16 09:18:09 -0700134bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const {
135 if (this->numTextures() != that.numTextures()) {
136 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000137 }
bsalomon420d7e92014-10-16 09:18:09 -0700138 for (int i = 0; i < this->numTextures(); ++i) {
139 if (this->textureAccess(i) != that.textureAccess(i)) {
140 return false;
141 }
142 }
143 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000144}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700145
egdaniel605dd0f2014-11-12 08:35:25 -0800146void GrProcessor::computeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel605dd0f2014-11-12 08:35:25 -0800147 this->onComputeInvariantOutput(inout);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700148}
149
joshualitta5305a12014-10-10 17:47:00 -0700150///////////////////////////////////////////////////////////////////////////////////////////////////
151
152void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
153 fCoordTransforms.push_back(transform);
bsalomonf2765412014-10-15 18:34:46 -0700154 SkDEBUGCODE(transform->setInProcessor();)
joshualitta5305a12014-10-10 17:47:00 -0700155}
bsalomonde258cd2014-10-15 19:06:21 -0700156
157bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
158 if (fCoordTransforms.count() != that.fCoordTransforms.count()) {
159 return false;
160 }
161 int count = fCoordTransforms.count();
162 for (int i = 0; i < count; ++i) {
163 if (*fCoordTransforms[i] != *that.fCoordTransforms[i]) {
164 return false;
165 }
166 }
167 return true;
168}
joshualittc07379d2014-11-20 14:50:39 -0800169
170///////////////////////////////////////////////////////////////////////////////////////////////////
171
172/*
173 * GrGeometryData shares the same pool so it lives in this file too
174 */
175void* GrGeometryData::operator new(size_t size) {
176 return GrProcessor_Globals::GetTLS()->allocate(size);
177}
178
179void GrGeometryData::operator delete(void* target) {
180 GrProcessor_Globals::GetTLS()->release(target);
181}