blob: f2b49988a715b5f2c9a421f184b7a37cdaee9c77 [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"
joshualitt2e3b3e32014-12-09 13:31:14 -080010#include "GrGeometryProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080011#include "GrInvariantOutput.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000012#include "GrMemoryPool.h"
Brian Salomon0bbecb22016-11-17 11:38:22 -050013#include "GrTextureParams.h"
14#include "GrTexturePriv.h"
egdaniel915187b2014-12-05 12:58:28 -080015#include "GrXferProcessor.h"
joshualitt23ac62c2015-03-30 09:53:47 -070016#include "SkSpinlock.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<>
egdanielc2304142014-12-11 13:15:13 -080035SkTArray<GrProcessorTestFactory<GrXPFactory>*, true>*
36GrProcessorTestFactory<GrXPFactory>::GetFactories() {
37 static SkTArray<GrProcessorTestFactory<GrXPFactory>*, true> gFactories;
egdaniel378092f2014-12-03 10:40:13 -080038 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 */
Ben Wagnerc03e1c52016-10-17 15:20:02 -040053static const int kFPFactoryCount = 40;
joshualitt4973d9d2014-11-08 09:24:25 -080054static const int kGPFactoryCount = 14;
Ben Wagnerc03e1c52016-10-17 15:20:02 -040055static const int kXPFactoryCount = 5;
joshualitt9e87fa72014-10-09 13:12:35 -070056
57template<>
58void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
59 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040060 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
61 kFPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070062 SkFAIL("Wrong number of fragment processor factories!");
63 }
64}
65
66template<>
67void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
68 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040069 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
70 kGPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070071 SkFAIL("Wrong number of geometry processor factories!");
72 }
73}
74
egdaniel378092f2014-12-03 10:40:13 -080075template<>
egdanielc2304142014-12-11 13:15:13 -080076void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080077 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040078 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
79 kXPFactoryCount, GetFactories()->count());
egdanielc2304142014-12-11 13:15:13 -080080 SkFAIL("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080081 }
82}
83
joshualitt9e87fa72014-10-09 13:12:35 -070084#endif
85
bsalomon5baedd62015-03-09 12:15:53 -070086
joshualitt23ac62c2015-03-30 09:53:47 -070087// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
88// different threads. The GrContext is not used concurrently on different threads and there is a
89// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070090// GrContexts and those contexts may be in use concurrently on different threads.
91namespace {
mtklein15923c92016-02-29 10:14:38 -080092static SkSpinlock gProcessorSpinlock;
bsalomon5baedd62015-03-09 12:15:53 -070093class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000094public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000095
msarett68440f82016-08-29 14:52:24 -070096// We know in the Android framework there is only one GrContext.
97#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
98 MemoryPoolAccessor() {}
99 ~MemoryPoolAccessor() {}
100#else
101 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700102 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700103#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000104
bsalomon5baedd62015-03-09 12:15:53 -0700105 GrMemoryPool* pool() const {
106 static GrMemoryPool gPool(4096, 4096);
107 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000108 }
109};
bsalomon5baedd62015-03-09 12:15:53 -0700110}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000111
bsalomon5baedd62015-03-09 12:15:53 -0700112int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000113
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000114///////////////////////////////////////////////////////////////////////////////
115
joshualittb0a8a372014-09-23 09:50:21 -0700116GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000117
Brian Salomon0bbecb22016-11-17 11:38:22 -0500118void GrProcessor::addTextureSampler(const TextureSampler* access) {
119 fTextureSamplers.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700120 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000121}
122
cdalton74b8d322016-04-11 14:47:28 -0700123void GrProcessor::addBufferAccess(const GrBufferAccess* access) {
124 fBufferAccesses.push_back(access);
125 this->addGpuResource(access->getProgramBuffer());
126}
127
joshualittb0a8a372014-09-23 09:50:21 -0700128void* GrProcessor::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -0700129 return MemoryPoolAccessor().pool()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000130}
131
joshualittb0a8a372014-09-23 09:50:21 -0700132void GrProcessor::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -0700133 return MemoryPoolAccessor().pool()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000134}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000135
cdalton74b8d322016-04-11 14:47:28 -0700136bool GrProcessor::hasSameSamplers(const GrProcessor& that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500137 if (this->numTextureSamplers() != that.numTextureSamplers() ||
138 this->numBuffers() != that.numBuffers()) {
bsalomon420d7e92014-10-16 09:18:09 -0700139 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000140 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500141 for (int i = 0; i < this->numTextureSamplers(); ++i) {
142 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700143 return false;
144 }
145 }
cdalton74b8d322016-04-11 14:47:28 -0700146 for (int i = 0; i < this->numBuffers(); ++i) {
147 if (this->bufferAccess(i) != that.bufferAccess(i)) {
148 return false;
149 }
150 }
bsalomon420d7e92014-10-16 09:18:09 -0700151 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000152}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700153
joshualitta5305a12014-10-10 17:47:00 -0700154///////////////////////////////////////////////////////////////////////////////////////////////////
155
Brian Salomon0bbecb22016-11-17 11:38:22 -0500156GrProcessor::TextureSampler::TextureSampler() {}
157
158GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, const GrTextureParams& params) {
159 this->reset(texture, params);
160}
161
162GrProcessor::TextureSampler::TextureSampler(GrTexture* texture,
163 GrTextureParams::FilterMode filterMode,
164 SkShader::TileMode tileXAndY,
165 GrShaderFlags visibility) {
166 this->reset(texture, filterMode, tileXAndY, visibility);
167}
168
169void GrProcessor::TextureSampler::reset(GrTexture* texture,
170 const GrTextureParams& params,
171 GrShaderFlags visibility) {
172 SkASSERT(texture);
173 fTexture.set(SkRef(texture), kRead_GrIOType);
174 fParams = params;
175 fParams.setFilterMode(SkTMin(params.filterMode(), texture->texturePriv().highestFilterMode()));
176 fVisibility = visibility;
177}
178
179void GrProcessor::TextureSampler::reset(GrTexture* texture,
180 GrTextureParams::FilterMode filterMode,
181 SkShader::TileMode tileXAndY,
182 GrShaderFlags visibility) {
183 SkASSERT(texture);
184 fTexture.set(SkRef(texture), kRead_GrIOType);
185 filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode());
186 fParams.reset(tileXAndY, filterMode);
187 fVisibility = visibility;
188}
189
190///////////////////////////////////////////////////////////////////////////////////////////////////
191
egdaniel915187b2014-12-05 12:58:28 -0800192// Initial static variable from GrXPFactory
193int32_t GrXPFactory::gCurrXPFClassID =
194 GrXPFactory::kIllegalXPFClassID;