blob: 56d3e7f1a472d9a2a14f8c37fd63fdd9d3e7f740 [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"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050015#include "GrTextureProxy.h"
egdaniel915187b2014-12-05 12:58:28 -080016#include "GrXferProcessor.h"
joshualitt23ac62c2015-03-30 09:53:47 -070017#include "SkSpinlock.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000018
joshualitt9e87fa72014-10-09 13:12:35 -070019#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
20
joshualitteb2a6762014-12-04 11:35:33 -080021class GrFragmentProcessor;
22class GrGeometryProcessor;
23
joshualitt9e87fa72014-10-09 13:12:35 -070024/*
25 * Originally these were both in the processor unit test header, but then it seemed to cause linker
26 * problems on android.
27 */
28template<>
29SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
30GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
31 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
32 return &gFactories;
33}
34
35template<>
Brian Salomon003312a2017-01-09 16:00:33 +000036SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
37GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
38 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050039 return &gFactories;
40}
41
Brian Salomona1633922017-01-09 11:46:10 -050042SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
43 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
44 return &gFactories;
45}
46
joshualitt9e87fa72014-10-09 13:12:35 -070047/*
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 */
Ben Wagnerc03e1c52016-10-17 15:20:02 -040052static const int kFPFactoryCount = 40;
joshualitt4973d9d2014-11-08 09:24:25 -080053static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050054static const int kXPFactoryCount = 4;
joshualitt9e87fa72014-10-09 13:12:35 -070055
56template<>
57void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
58 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040059 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
60 kFPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070061 SkFAIL("Wrong number of fragment processor factories!");
62 }
63}
64
65template<>
66void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
67 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040068 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
69 kGPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070070 SkFAIL("Wrong number of geometry processor factories!");
71 }
72}
73
Brian Salomona1633922017-01-09 11:46:10 -050074void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080075 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040076 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
77 kXPFactoryCount, GetFactories()->count());
egdanielc2304142014-12-11 13:15:13 -080078 SkFAIL("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080079 }
80}
81
joshualitt9e87fa72014-10-09 13:12:35 -070082#endif
83
bsalomon5baedd62015-03-09 12:15:53 -070084
joshualitt23ac62c2015-03-30 09:53:47 -070085// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
86// different threads. The GrContext is not used concurrently on different threads and there is a
87// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070088// GrContexts and those contexts may be in use concurrently on different threads.
89namespace {
mtklein15923c92016-02-29 10:14:38 -080090static SkSpinlock gProcessorSpinlock;
bsalomon5baedd62015-03-09 12:15:53 -070091class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000092public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000093
msarett68440f82016-08-29 14:52:24 -070094// We know in the Android framework there is only one GrContext.
95#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
96 MemoryPoolAccessor() {}
97 ~MemoryPoolAccessor() {}
98#else
99 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700100 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700101#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000102
bsalomon5baedd62015-03-09 12:15:53 -0700103 GrMemoryPool* pool() const {
104 static GrMemoryPool gPool(4096, 4096);
105 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000106 }
107};
bsalomon5baedd62015-03-09 12:15:53 -0700108}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000109
bsalomon5baedd62015-03-09 12:15:53 -0700110int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000111
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000112///////////////////////////////////////////////////////////////////////////////
113
joshualittb0a8a372014-09-23 09:50:21 -0700114GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000115
Brian Salomon0bbecb22016-11-17 11:38:22 -0500116void GrProcessor::addTextureSampler(const TextureSampler* access) {
117 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000118}
119
Brian Salomonb014cca2016-11-18 11:39:15 -0500120void GrProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700121 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500122}
123
124void GrProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
125 fImageStorageAccesses.push_back(access);
Brian Salomone57194f2017-01-09 15:30:02 -0500126}
127
128void GrProcessor::addPendingIOs() const {
129 for (const auto& sampler : fTextureSamplers) {
130 sampler->programTexture()->markPendingIO();
131 }
132 for (const auto& buffer : fBufferAccesses) {
133 buffer->programBuffer()->markPendingIO();
134 }
135 for (const auto& imageStorage : fImageStorageAccesses) {
136 imageStorage->programTexture()->markPendingIO();
137 }
138}
139
140void GrProcessor::removeRefs() const {
141 for (const auto& sampler : fTextureSamplers) {
142 sampler->programTexture()->removeRef();
143 }
144 for (const auto& buffer : fBufferAccesses) {
145 buffer->programBuffer()->removeRef();
146 }
147 for (const auto& imageStorage : fImageStorageAccesses) {
148 imageStorage->programTexture()->removeRef();
149 }
150}
151
152void GrProcessor::pendingIOComplete() const {
153 for (const auto& sampler : fTextureSamplers) {
154 sampler->programTexture()->pendingIOComplete();
155 }
156 for (const auto& buffer : fBufferAccesses) {
157 buffer->programBuffer()->pendingIOComplete();
158 }
159 for (const auto& imageStorage : fImageStorageAccesses) {
160 imageStorage->programTexture()->pendingIOComplete();
161 }
cdalton74b8d322016-04-11 14:47:28 -0700162}
163
joshualittb0a8a372014-09-23 09:50:21 -0700164void* GrProcessor::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -0700165 return MemoryPoolAccessor().pool()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000166}
167
joshualittb0a8a372014-09-23 09:50:21 -0700168void GrProcessor::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -0700169 return MemoryPoolAccessor().pool()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000170}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000171
Brian Salomonf9f45122016-11-29 11:59:17 -0500172bool GrProcessor::hasSameSamplersAndAccesses(const GrProcessor &that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500173 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomonf9f45122016-11-29 11:59:17 -0500174 this->numBuffers() != that.numBuffers() ||
175 this->numImageStorages() != that.numImageStorages()) {
bsalomon420d7e92014-10-16 09:18:09 -0700176 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000177 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500178 for (int i = 0; i < this->numTextureSamplers(); ++i) {
179 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700180 return false;
181 }
182 }
cdalton74b8d322016-04-11 14:47:28 -0700183 for (int i = 0; i < this->numBuffers(); ++i) {
184 if (this->bufferAccess(i) != that.bufferAccess(i)) {
185 return false;
186 }
187 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500188 for (int i = 0; i < this->numImageStorages(); ++i) {
189 if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
190 return false;
191 }
192 }
bsalomon420d7e92014-10-16 09:18:09 -0700193 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000194}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700195
joshualitta5305a12014-10-10 17:47:00 -0700196///////////////////////////////////////////////////////////////////////////////////////////////////
197
Brian Salomon0bbecb22016-11-17 11:38:22 -0500198GrProcessor::TextureSampler::TextureSampler() {}
199
Brian Salomon514baff2016-11-17 15:17:07 -0500200GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, const GrSamplerParams& params) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500201 this->reset(texture, params);
202}
203
204GrProcessor::TextureSampler::TextureSampler(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500205 GrSamplerParams::FilterMode filterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500206 SkShader::TileMode tileXAndY,
207 GrShaderFlags visibility) {
208 this->reset(texture, filterMode, tileXAndY, visibility);
209}
210
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500211GrProcessor::TextureSampler::TextureSampler(GrTextureProvider* texProvider,
212 sk_sp<GrTextureProxy> proxy,
Robert Phillips901f29a2017-01-24 16:24:41 -0500213 const GrSamplerParams& params) {
214 // For now, end the deferral at this time. Once all the TextureSamplers are swapped over
215 // to taking a GrSurfaceProxy just use the IORefs on the proxy
216 this->reset(proxy->instantiate(texProvider), params);
217}
218
219GrProcessor::TextureSampler::TextureSampler(GrTextureProvider* texProvider,
220 sk_sp<GrTextureProxy> proxy,
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500221 GrSamplerParams::FilterMode filterMode,
222 SkShader::TileMode tileXAndY,
223 GrShaderFlags visibility) {
224 // For now, end the deferral at this time. Once all the TextureSamplers are swapped over
225 // to taking a GrSurfaceProxy just use the IORefs on the proxy
Robert Phillips901f29a2017-01-24 16:24:41 -0500226 this->reset(proxy->instantiate(texProvider), filterMode, tileXAndY, visibility);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500227}
228
Brian Salomon0bbecb22016-11-17 11:38:22 -0500229void GrProcessor::TextureSampler::reset(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500230 const GrSamplerParams& params,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500231 GrShaderFlags visibility) {
232 SkASSERT(texture);
233 fTexture.set(SkRef(texture), kRead_GrIOType);
234 fParams = params;
235 fParams.setFilterMode(SkTMin(params.filterMode(), texture->texturePriv().highestFilterMode()));
236 fVisibility = visibility;
237}
238
239void GrProcessor::TextureSampler::reset(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500240 GrSamplerParams::FilterMode filterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500241 SkShader::TileMode tileXAndY,
242 GrShaderFlags visibility) {
243 SkASSERT(texture);
244 fTexture.set(SkRef(texture), kRead_GrIOType);
245 filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode());
246 fParams.reset(tileXAndY, filterMode);
247 fVisibility = visibility;
248}
249
250///////////////////////////////////////////////////////////////////////////////////////////////////
251
Brian Salomonf9f45122016-11-29 11:59:17 -0500252GrProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType,
253 GrSLMemoryModel memoryModel,
254 GrSLRestrict restrict,
255 GrShaderFlags visibility) {
256 SkASSERT(texture);
257 fTexture.set(texture.release(), ioType);
258 fMemoryModel = memoryModel;
259 fRestrict = restrict;
260 fVisibility = visibility;
261 // We currently infer this from the config. However, we could allow the client to specify
262 // a format that is different but compatible with the config.
263 switch (fTexture.get()->config()) {
264 case kRGBA_8888_GrPixelConfig:
265 fFormat = GrImageStorageFormat::kRGBA8;
266 break;
267 case kRGBA_8888_sint_GrPixelConfig:
268 fFormat = GrImageStorageFormat::kRGBA8i;
269 break;
270 case kRGBA_half_GrPixelConfig:
271 fFormat = GrImageStorageFormat::kRGBA16f;
272 break;
273 case kRGBA_float_GrPixelConfig:
274 fFormat = GrImageStorageFormat::kRGBA32f;
275 break;
276 default:
277 SkFAIL("Config is not (yet) supported as image storage.");
278 break;
279 }
280}