blob: f4bead2d0861258bdbdf10068a2c8d0aa9b901fd [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 Salomon514baff2016-11-17 15:17:07 -050013#include "GrSamplerParams.h"
Brian Salomon0bbecb22016-11-17 11:38:22 -050014#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<>
Brian Salomon003312a2017-01-09 16:00:33 +000035SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
36GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
37 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050038 return &gFactories;
39}
40
Brian Salomona1633922017-01-09 11:46:10 -050041SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
42 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
43 return &gFactories;
44}
45
joshualitt9e87fa72014-10-09 13:12:35 -070046/*
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 */
Ben Wagnerc03e1c52016-10-17 15:20:02 -040051static const int kFPFactoryCount = 40;
joshualitt4973d9d2014-11-08 09:24:25 -080052static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050053static const int kXPFactoryCount = 4;
joshualitt9e87fa72014-10-09 13:12:35 -070054
55template<>
56void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
57 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040058 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
59 kFPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070060 SkFAIL("Wrong number of fragment processor factories!");
61 }
62}
63
64template<>
65void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
66 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040067 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
68 kGPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070069 SkFAIL("Wrong number of geometry processor factories!");
70 }
71}
72
Brian Salomona1633922017-01-09 11:46:10 -050073void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080074 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040075 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
76 kXPFactoryCount, GetFactories()->count());
egdanielc2304142014-12-11 13:15:13 -080077 SkFAIL("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080078 }
79}
80
joshualitt9e87fa72014-10-09 13:12:35 -070081#endif
82
bsalomon5baedd62015-03-09 12:15:53 -070083
joshualitt23ac62c2015-03-30 09:53:47 -070084// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
85// different threads. The GrContext is not used concurrently on different threads and there is a
86// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070087// GrContexts and those contexts may be in use concurrently on different threads.
88namespace {
mtklein15923c92016-02-29 10:14:38 -080089static SkSpinlock gProcessorSpinlock;
bsalomon5baedd62015-03-09 12:15:53 -070090class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000091public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000092
msarett68440f82016-08-29 14:52:24 -070093// We know in the Android framework there is only one GrContext.
94#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
95 MemoryPoolAccessor() {}
96 ~MemoryPoolAccessor() {}
97#else
98 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -070099 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700100#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000101
bsalomon5baedd62015-03-09 12:15:53 -0700102 GrMemoryPool* pool() const {
103 static GrMemoryPool gPool(4096, 4096);
104 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000105 }
106};
bsalomon5baedd62015-03-09 12:15:53 -0700107}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000108
bsalomon5baedd62015-03-09 12:15:53 -0700109int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000110
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000111///////////////////////////////////////////////////////////////////////////////
112
joshualittb0a8a372014-09-23 09:50:21 -0700113GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000114
Brian Salomon0bbecb22016-11-17 11:38:22 -0500115void GrProcessor::addTextureSampler(const TextureSampler* access) {
116 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000117}
118
Brian Salomonb014cca2016-11-18 11:39:15 -0500119void GrProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700120 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500121}
122
123void GrProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
124 fImageStorageAccesses.push_back(access);
Brian Salomone57194f2017-01-09 15:30:02 -0500125}
126
127void GrProcessor::addPendingIOs() const {
128 for (const auto& sampler : fTextureSamplers) {
129 sampler->programTexture()->markPendingIO();
130 }
131 for (const auto& buffer : fBufferAccesses) {
132 buffer->programBuffer()->markPendingIO();
133 }
134 for (const auto& imageStorage : fImageStorageAccesses) {
135 imageStorage->programTexture()->markPendingIO();
136 }
137}
138
139void GrProcessor::removeRefs() const {
140 for (const auto& sampler : fTextureSamplers) {
141 sampler->programTexture()->removeRef();
142 }
143 for (const auto& buffer : fBufferAccesses) {
144 buffer->programBuffer()->removeRef();
145 }
146 for (const auto& imageStorage : fImageStorageAccesses) {
147 imageStorage->programTexture()->removeRef();
148 }
149}
150
151void GrProcessor::pendingIOComplete() const {
152 for (const auto& sampler : fTextureSamplers) {
153 sampler->programTexture()->pendingIOComplete();
154 }
155 for (const auto& buffer : fBufferAccesses) {
156 buffer->programBuffer()->pendingIOComplete();
157 }
158 for (const auto& imageStorage : fImageStorageAccesses) {
159 imageStorage->programTexture()->pendingIOComplete();
160 }
cdalton74b8d322016-04-11 14:47:28 -0700161}
162
joshualittb0a8a372014-09-23 09:50:21 -0700163void* GrProcessor::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -0700164 return MemoryPoolAccessor().pool()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000165}
166
joshualittb0a8a372014-09-23 09:50:21 -0700167void GrProcessor::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -0700168 return MemoryPoolAccessor().pool()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000169}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000170
Brian Salomonf9f45122016-11-29 11:59:17 -0500171bool GrProcessor::hasSameSamplersAndAccesses(const GrProcessor &that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500172 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomonf9f45122016-11-29 11:59:17 -0500173 this->numBuffers() != that.numBuffers() ||
174 this->numImageStorages() != that.numImageStorages()) {
bsalomon420d7e92014-10-16 09:18:09 -0700175 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000176 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500177 for (int i = 0; i < this->numTextureSamplers(); ++i) {
178 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700179 return false;
180 }
181 }
cdalton74b8d322016-04-11 14:47:28 -0700182 for (int i = 0; i < this->numBuffers(); ++i) {
183 if (this->bufferAccess(i) != that.bufferAccess(i)) {
184 return false;
185 }
186 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500187 for (int i = 0; i < this->numImageStorages(); ++i) {
188 if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
189 return false;
190 }
191 }
bsalomon420d7e92014-10-16 09:18:09 -0700192 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000193}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700194
joshualitta5305a12014-10-10 17:47:00 -0700195///////////////////////////////////////////////////////////////////////////////////////////////////
196
Brian Salomon0bbecb22016-11-17 11:38:22 -0500197GrProcessor::TextureSampler::TextureSampler() {}
198
Brian Salomon514baff2016-11-17 15:17:07 -0500199GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, const GrSamplerParams& params) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500200 this->reset(texture, params);
201}
202
203GrProcessor::TextureSampler::TextureSampler(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500204 GrSamplerParams::FilterMode filterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500205 SkShader::TileMode tileXAndY,
206 GrShaderFlags visibility) {
207 this->reset(texture, filterMode, tileXAndY, visibility);
208}
209
210void GrProcessor::TextureSampler::reset(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500211 const GrSamplerParams& params,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500212 GrShaderFlags visibility) {
213 SkASSERT(texture);
214 fTexture.set(SkRef(texture), kRead_GrIOType);
215 fParams = params;
216 fParams.setFilterMode(SkTMin(params.filterMode(), texture->texturePriv().highestFilterMode()));
217 fVisibility = visibility;
218}
219
220void GrProcessor::TextureSampler::reset(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500221 GrSamplerParams::FilterMode filterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500222 SkShader::TileMode tileXAndY,
223 GrShaderFlags visibility) {
224 SkASSERT(texture);
225 fTexture.set(SkRef(texture), kRead_GrIOType);
226 filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode());
227 fParams.reset(tileXAndY, filterMode);
228 fVisibility = visibility;
229}
230
231///////////////////////////////////////////////////////////////////////////////////////////////////
232
Brian Salomonf9f45122016-11-29 11:59:17 -0500233GrProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType,
234 GrSLMemoryModel memoryModel,
235 GrSLRestrict restrict,
236 GrShaderFlags visibility) {
237 SkASSERT(texture);
238 fTexture.set(texture.release(), ioType);
239 fMemoryModel = memoryModel;
240 fRestrict = restrict;
241 fVisibility = visibility;
242 // We currently infer this from the config. However, we could allow the client to specify
243 // a format that is different but compatible with the config.
244 switch (fTexture.get()->config()) {
245 case kRGBA_8888_GrPixelConfig:
246 fFormat = GrImageStorageFormat::kRGBA8;
247 break;
248 case kRGBA_8888_sint_GrPixelConfig:
249 fFormat = GrImageStorageFormat::kRGBA8i;
250 break;
251 case kRGBA_half_GrPixelConfig:
252 fFormat = GrImageStorageFormat::kRGBA16f;
253 break;
254 case kRGBA_float_GrPixelConfig:
255 fFormat = GrImageStorageFormat::kRGBA32f;
256 break;
257 default:
258 SkFAIL("Config is not (yet) supported as image storage.");
259 break;
260 }
261}