blob: 4bda2e18b6dd93c4fcc32f92515daf75386036d6 [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"
joshualitt23ac62c2015-03-30 09:53:47 -070015#include "SkSpinlock.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
bsalomon5baedd62015-03-09 12:15:53 -070099
joshualitt23ac62c2015-03-30 09:53:47 -0700100// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
101// different threads. The GrContext is not used concurrently on different threads and there is a
102// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -0700103// GrContexts and those contexts may be in use concurrently on different threads.
104namespace {
joshualitt23ac62c2015-03-30 09:53:47 -0700105SK_DECLARE_STATIC_SPINLOCK(gProcessorSpinlock);
bsalomon5baedd62015-03-09 12:15:53 -0700106class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000107public:
joshualitt23ac62c2015-03-30 09:53:47 -0700108 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000109
joshualitt23ac62c2015-03-30 09:53:47 -0700110 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000111
bsalomon5baedd62015-03-09 12:15:53 -0700112 GrMemoryPool* pool() const {
113 static GrMemoryPool gPool(4096, 4096);
114 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000115 }
116};
bsalomon5baedd62015-03-09 12:15:53 -0700117}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000118
bsalomon5baedd62015-03-09 12:15:53 -0700119int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000120
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000121///////////////////////////////////////////////////////////////////////////////
122
joshualittb0a8a372014-09-23 09:50:21 -0700123GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000124
joshualittb0a8a372014-09-23 09:50:21 -0700125void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000126 fTextureAccesses.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700127 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000128}
129
joshualittb0a8a372014-09-23 09:50:21 -0700130void* GrProcessor::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -0700131 return MemoryPoolAccessor().pool()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000132}
133
joshualittb0a8a372014-09-23 09:50:21 -0700134void GrProcessor::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -0700135 return MemoryPoolAccessor().pool()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000136}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000137
bsalomon420d7e92014-10-16 09:18:09 -0700138bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const {
139 if (this->numTextures() != that.numTextures()) {
140 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000141 }
bsalomon420d7e92014-10-16 09:18:09 -0700142 for (int i = 0; i < this->numTextures(); ++i) {
143 if (this->textureAccess(i) != that.textureAccess(i)) {
144 return false;
145 }
146 }
147 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000148}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700149
joshualitta5305a12014-10-10 17:47:00 -0700150///////////////////////////////////////////////////////////////////////////////////////////////////
151
152void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) {
153 fCoordTransforms.push_back(transform);
joshualitt290c09b2014-12-19 13:45:20 -0800154 fUsesLocalCoords = fUsesLocalCoords || transform->sourceCoords() == kLocal_GrCoordSet;
bsalomonf2765412014-10-15 18:34:46 -0700155 SkDEBUGCODE(transform->setInProcessor();)
joshualitta5305a12014-10-10 17:47:00 -0700156}
bsalomonde258cd2014-10-15 19:06:21 -0700157
158bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const {
159 if (fCoordTransforms.count() != that.fCoordTransforms.count()) {
160 return false;
161 }
162 int count = fCoordTransforms.count();
163 for (int i = 0; i < count; ++i) {
164 if (*fCoordTransforms[i] != *that.fCoordTransforms[i]) {
165 return false;
166 }
167 }
168 return true;
169}
joshualittc07379d2014-11-20 14:50:39 -0800170
joshualitt56995b52014-12-11 15:44:02 -0800171void GrFragmentProcessor::computeInvariantOutput(GrInvariantOutput* inout) const {
172 this->onComputeInvariantOutput(inout);
173}
174
joshualittc07379d2014-11-20 14:50:39 -0800175///////////////////////////////////////////////////////////////////////////////////////////////////
176
egdaniel915187b2014-12-05 12:58:28 -0800177// Initial static variable from GrXPFactory
178int32_t GrXPFactory::gCurrXPFClassID =
179 GrXPFactory::kIllegalXPFClassID;