blob: 1533257bad8722c58b1e263f4547e03a8f09ceb7 [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"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000011#include "GrMemoryPool.h"
Brian Salomon514baff2016-11-17 15:17:07 -050012#include "GrSamplerParams.h"
Brian Salomon0bbecb22016-11-17 11:38:22 -050013#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050014#include "GrTextureProxy.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
Hal Canary6f6961e2017-01-31 13:50:44 -050018#if GR_TEST_UTILS
Robert Phillips296b1cc2017-03-15 10:42:12 -040019
20GrResourceProvider* GrProcessorTestData::resourceProvider() {
21 return fContext->resourceProvider();
22}
23
24const GrCaps* GrProcessorTestData::caps() {
25 return fContext->caps();
26}
27
28#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
joshualitteb2a6762014-12-04 11:35:33 -080029class GrFragmentProcessor;
30class GrGeometryProcessor;
31
joshualitt9e87fa72014-10-09 13:12:35 -070032/*
33 * Originally these were both in the processor unit test header, but then it seemed to cause linker
34 * problems on android.
35 */
36template<>
37SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
38GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
39 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
40 return &gFactories;
41}
42
43template<>
Brian Salomon003312a2017-01-09 16:00:33 +000044SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
45GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
46 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050047 return &gFactories;
48}
49
Brian Salomona1633922017-01-09 11:46:10 -050050SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
51 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
52 return &gFactories;
53}
54
joshualitt9e87fa72014-10-09 13:12:35 -070055/*
56 * To ensure we always have successful static initialization, before creating from the factories
57 * we verify the count is as expected. If a new factory is added, then these numbers must be
58 * manually adjusted.
59 */
Brian Osman63954c92017-03-14 12:07:12 -040060static const int kFPFactoryCount = 41;
joshualitt4973d9d2014-11-08 09:24:25 -080061static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050062static const int kXPFactoryCount = 4;
joshualitt9e87fa72014-10-09 13:12:35 -070063
64template<>
65void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
66 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040067 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
68 kFPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070069 SkFAIL("Wrong number of fragment processor factories!");
70 }
71}
72
73template<>
74void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
75 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040076 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
77 kGPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070078 SkFAIL("Wrong number of geometry processor factories!");
79 }
80}
81
Brian Salomona1633922017-01-09 11:46:10 -050082void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080083 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040084 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
85 kXPFactoryCount, GetFactories()->count());
egdanielc2304142014-12-11 13:15:13 -080086 SkFAIL("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080087 }
88}
89
joshualitt9e87fa72014-10-09 13:12:35 -070090#endif
Hal Canary6f6961e2017-01-31 13:50:44 -050091#endif
joshualitt9e87fa72014-10-09 13:12:35 -070092
bsalomon5baedd62015-03-09 12:15:53 -070093
joshualitt23ac62c2015-03-30 09:53:47 -070094// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
95// different threads. The GrContext is not used concurrently on different threads and there is a
96// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070097// GrContexts and those contexts may be in use concurrently on different threads.
98namespace {
mtklein15923c92016-02-29 10:14:38 -080099static SkSpinlock gProcessorSpinlock;
bsalomon5baedd62015-03-09 12:15:53 -0700100class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000101public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000102
msarett68440f82016-08-29 14:52:24 -0700103// We know in the Android framework there is only one GrContext.
104#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
105 MemoryPoolAccessor() {}
106 ~MemoryPoolAccessor() {}
107#else
108 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700109 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700110#endif
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
Brian Salomonab015ef2017-04-04 10:15:51 -0400123void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000124
Brian Salomonab015ef2017-04-04 10:15:51 -0400125void GrProcessor::operator delete(void* target) {
126 return MemoryPoolAccessor().pool()->release(target);
127}
128
129///////////////////////////////////////////////////////////////////////////////
130
131void GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400132 // MDB TODO: this 'isBad' call checks to ensure the underlying texture exists. It needs to
133 // be moved later.
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400134 if (access->isBad()) {
135 this->markAsBad();
136 }
137
Brian Salomon0bbecb22016-11-17 11:38:22 -0500138 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000139}
140
Brian Salomonab015ef2017-04-04 10:15:51 -0400141void GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700142 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500143}
144
Robert Phillips8a02f652017-05-12 14:49:16 -0400145void GrResourceIOProcessor::addImageStorageAccess(GrResourceProvider* resourceProvider,
146 const ImageStorageAccess* access) {
147 // MDB TODO: this 'isBad' call attempts to instantiate 'access'. It needs to be moved later.
148 if (access->isBad(resourceProvider)) {
149 this->markAsBad();
150 }
151
Brian Salomonf9f45122016-11-29 11:59:17 -0500152 fImageStorageAccesses.push_back(access);
Brian Salomone57194f2017-01-09 15:30:02 -0500153}
154
Brian Salomonab015ef2017-04-04 10:15:51 -0400155void GrResourceIOProcessor::addPendingIOs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500156 for (const auto& sampler : fTextureSamplers) {
157 sampler->programTexture()->markPendingIO();
158 }
159 for (const auto& buffer : fBufferAccesses) {
160 buffer->programBuffer()->markPendingIO();
161 }
162 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400163 imageStorage->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500164 }
165}
166
Brian Salomonab015ef2017-04-04 10:15:51 -0400167void GrResourceIOProcessor::removeRefs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500168 for (const auto& sampler : fTextureSamplers) {
169 sampler->programTexture()->removeRef();
170 }
171 for (const auto& buffer : fBufferAccesses) {
172 buffer->programBuffer()->removeRef();
173 }
174 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400175 imageStorage->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500176 }
177}
178
Brian Salomonab015ef2017-04-04 10:15:51 -0400179void GrResourceIOProcessor::pendingIOComplete() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500180 for (const auto& sampler : fTextureSamplers) {
181 sampler->programTexture()->pendingIOComplete();
182 }
183 for (const auto& buffer : fBufferAccesses) {
184 buffer->programBuffer()->pendingIOComplete();
185 }
186 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400187 imageStorage->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500188 }
cdalton74b8d322016-04-11 14:47:28 -0700189}
190
Brian Salomonab015ef2017-04-04 10:15:51 -0400191bool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500192 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomonf9f45122016-11-29 11:59:17 -0500193 this->numBuffers() != that.numBuffers() ||
194 this->numImageStorages() != that.numImageStorages()) {
bsalomon420d7e92014-10-16 09:18:09 -0700195 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000196 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500197 for (int i = 0; i < this->numTextureSamplers(); ++i) {
198 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700199 return false;
200 }
201 }
cdalton74b8d322016-04-11 14:47:28 -0700202 for (int i = 0; i < this->numBuffers(); ++i) {
203 if (this->bufferAccess(i) != that.bufferAccess(i)) {
204 return false;
205 }
206 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500207 for (int i = 0; i < this->numImageStorages(); ++i) {
208 if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
209 return false;
210 }
211 }
bsalomon420d7e92014-10-16 09:18:09 -0700212 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000213}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700214
joshualitta5305a12014-10-10 17:47:00 -0700215///////////////////////////////////////////////////////////////////////////////////////////////////
216
Brian Salomonab015ef2017-04-04 10:15:51 -0400217GrResourceIOProcessor::TextureSampler::TextureSampler() {}
Brian Salomon0bbecb22016-11-17 11:38:22 -0500218
Brian Salomonab015ef2017-04-04 10:15:51 -0400219GrResourceIOProcessor::TextureSampler::TextureSampler(GrResourceProvider* resourceProvider,
220 sk_sp<GrTextureProxy> proxy,
221 const GrSamplerParams& params) {
Brian Osman32342f02017-03-04 08:12:46 -0500222 this->reset(resourceProvider, std::move(proxy), params);
Robert Phillips901f29a2017-01-24 16:24:41 -0500223}
224
Brian Salomonab015ef2017-04-04 10:15:51 -0400225GrResourceIOProcessor::TextureSampler::TextureSampler(GrResourceProvider* resourceProvider,
226 sk_sp<GrTextureProxy> proxy,
227 GrSamplerParams::FilterMode filterMode,
228 SkShader::TileMode tileXAndY,
229 GrShaderFlags visibility) {
Brian Osman32342f02017-03-04 08:12:46 -0500230 this->reset(resourceProvider, std::move(proxy), filterMode, tileXAndY, visibility);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500231}
232
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400233// MDB TODO: remove this!
Brian Salomonab015ef2017-04-04 10:15:51 -0400234void GrResourceIOProcessor::TextureSampler::reset(GrTexture* texture,
235 GrSamplerParams::FilterMode filterMode,
236 SkShader::TileMode tileXAndY,
237 GrShaderFlags visibility) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500238 SkASSERT(texture);
239 fTexture.set(SkRef(texture), kRead_GrIOType);
240 filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode());
241 fParams.reset(tileXAndY, filterMode);
242 fVisibility = visibility;
243}
244
Brian Salomonab015ef2017-04-04 10:15:51 -0400245void GrResourceIOProcessor::TextureSampler::reset(GrResourceProvider* resourceProvider,
246 sk_sp<GrTextureProxy> proxy,
247 const GrSamplerParams& params,
248 GrShaderFlags visibility) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400249 fParams = params;
250
Robert Phillips30f9bc62017-02-22 15:28:38 -0500251 // For now, end the deferral at this time. Once all the TextureSamplers are swapped over
252 // to taking a GrSurfaceProxy just use the IORefs on the proxy
Brian Osman32342f02017-03-04 08:12:46 -0500253 GrTexture* texture = proxy->instantiate(resourceProvider);
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400254 if (texture) {
255 fTexture.set(SkRef(texture), kRead_GrIOType);
Robert Phillips49081d12017-05-08 13:41:35 -0400256 SkASSERT(texture->texturePriv().highestFilterMode() == proxy->highestFilterMode());
257 fParams.setFilterMode(SkTMin(params.filterMode(), proxy->highestFilterMode()));
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400258 }
259
Robert Phillips30f9bc62017-02-22 15:28:38 -0500260 fVisibility = visibility;
261}
262
Brian Salomonab015ef2017-04-04 10:15:51 -0400263void GrResourceIOProcessor::TextureSampler::reset(GrResourceProvider* resourceProvider,
264 sk_sp<GrTextureProxy> proxy,
265 GrSamplerParams::FilterMode filterMode,
266 SkShader::TileMode tileXAndY,
267 GrShaderFlags visibility) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500268 // For now, end the deferral at this time. Once all the TextureSamplers are swapped over
269 // to taking a GrSurfaceProxy just use the IORefs on the proxy
Brian Osman32342f02017-03-04 08:12:46 -0500270 GrTexture* texture = proxy->instantiate(resourceProvider);
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400271 if (texture) {
272 fTexture.set(SkRef(texture), kRead_GrIOType);
Robert Phillips49081d12017-05-08 13:41:35 -0400273 SkASSERT(texture->texturePriv().highestFilterMode() == proxy->highestFilterMode());
274 filterMode = SkTMin(filterMode, proxy->highestFilterMode());
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400275 }
276
Robert Phillips30f9bc62017-02-22 15:28:38 -0500277 fParams.reset(tileXAndY, filterMode);
278 fVisibility = visibility;
279}
280
Brian Salomon0bbecb22016-11-17 11:38:22 -0500281///////////////////////////////////////////////////////////////////////////////////////////////////
282
Robert Phillips8a02f652017-05-12 14:49:16 -0400283GrResourceIOProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400284 GrIOType ioType,
285 GrSLMemoryModel memoryModel,
286 GrSLRestrict restrict,
Robert Phillips8a02f652017-05-12 14:49:16 -0400287 GrShaderFlags visibility)
288 : fProxyRef(std::move(proxy), ioType) {
289 SkASSERT(fProxyRef.getProxy());
290
Brian Salomonf9f45122016-11-29 11:59:17 -0500291 fMemoryModel = memoryModel;
292 fRestrict = restrict;
293 fVisibility = visibility;
294 // We currently infer this from the config. However, we could allow the client to specify
295 // a format that is different but compatible with the config.
Robert Phillips8a02f652017-05-12 14:49:16 -0400296 switch (fProxyRef.getProxy()->config()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500297 case kRGBA_8888_GrPixelConfig:
298 fFormat = GrImageStorageFormat::kRGBA8;
299 break;
300 case kRGBA_8888_sint_GrPixelConfig:
301 fFormat = GrImageStorageFormat::kRGBA8i;
302 break;
303 case kRGBA_half_GrPixelConfig:
304 fFormat = GrImageStorageFormat::kRGBA16f;
305 break;
306 case kRGBA_float_GrPixelConfig:
307 fFormat = GrImageStorageFormat::kRGBA32f;
308 break;
309 default:
310 SkFAIL("Config is not (yet) supported as image storage.");
311 break;
312 }
313}