blob: b8ca63e7a94a1c88aef859bd4f0767441b7c516c [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 Salomon2bbdcc42017-09-07 12:36:34 -040012#include "GrSamplerState.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050013#include "GrTextureProxy.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
Hal Canary6f6961e2017-01-31 13:50:44 -050017#if GR_TEST_UTILS
Robert Phillips296b1cc2017-03-15 10:42:12 -040018
19GrResourceProvider* GrProcessorTestData::resourceProvider() {
20 return fContext->resourceProvider();
21}
22
23const GrCaps* GrProcessorTestData::caps() {
24 return fContext->caps();
25}
26
27#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
joshualitteb2a6762014-12-04 11:35:33 -080028class GrFragmentProcessor;
29class GrGeometryProcessor;
30
joshualitt9e87fa72014-10-09 13:12:35 -070031/*
32 * Originally these were both in the processor unit test header, but then it seemed to cause linker
33 * problems on android.
34 */
Brian Salomon1c053642017-07-24 10:16:19 -040035template <>
36SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
37 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
joshualitt9e87fa72014-10-09 13:12:35 -070038 return &gFactories;
39}
40
Brian Salomon1c053642017-07-24 10:16:19 -040041template <>
42SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
43 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050044 return &gFactories;
45}
46
Brian Salomona1633922017-01-09 11:46:10 -050047SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
48 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
49 return &gFactories;
50}
51
joshualitt9e87fa72014-10-09 13:12:35 -070052/*
53 * To ensure we always have successful static initialization, before creating from the factories
54 * we verify the count is as expected. If a new factory is added, then these numbers must be
55 * manually adjusted.
56 */
Mike Reedf2ae2b22017-05-30 15:22:54 -040057static const int kFPFactoryCount = 42;
joshualitt4973d9d2014-11-08 09:24:25 -080058static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050059static const int kXPFactoryCount = 4;
joshualitt9e87fa72014-10-09 13:12:35 -070060
Brian Salomon1c053642017-07-24 10:16:19 -040061template <>
62void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070063 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040064 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
65 kFPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040066 SK_ABORT("Wrong number of fragment processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070067 }
68}
69
Brian Salomon1c053642017-07-24 10:16:19 -040070template <>
71void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070072 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040073 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
74 kGPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040075 SK_ABORT("Wrong number of geometry processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070076 }
77}
78
Brian Salomona1633922017-01-09 11:46:10 -050079void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080080 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040081 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
82 kXPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040083 SK_ABORT("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080084 }
85}
86
joshualitt9e87fa72014-10-09 13:12:35 -070087#endif
Hal Canary6f6961e2017-01-31 13:50:44 -050088#endif
joshualitt9e87fa72014-10-09 13:12:35 -070089
bsalomon5baedd62015-03-09 12:15:53 -070090
joshualitt23ac62c2015-03-30 09:53:47 -070091// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
92// different threads. The GrContext is not used concurrently on different threads and there is a
93// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070094// GrContexts and those contexts may be in use concurrently on different threads.
95namespace {
Leon Scroggins III981a31e2017-10-06 11:53:53 -040096#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
mtklein15923c92016-02-29 10:14:38 -080097static SkSpinlock gProcessorSpinlock;
Leon Scroggins III981a31e2017-10-06 11:53:53 -040098#endif
bsalomon5baedd62015-03-09 12:15:53 -070099class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000100public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000101
msarett68440f82016-08-29 14:52:24 -0700102// We know in the Android framework there is only one GrContext.
103#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
104 MemoryPoolAccessor() {}
105 ~MemoryPoolAccessor() {}
106#else
107 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700108 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700109#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000110
bsalomon5baedd62015-03-09 12:15:53 -0700111 GrMemoryPool* pool() const {
112 static GrMemoryPool gPool(4096, 4096);
113 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000114 }
115};
bsalomon5baedd62015-03-09 12:15:53 -0700116}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000117
bsalomon5baedd62015-03-09 12:15:53 -0700118int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000119
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000120///////////////////////////////////////////////////////////////////////////////
121
Brian Salomonab015ef2017-04-04 10:15:51 -0400122void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000123
Brian Salomonab015ef2017-04-04 10:15:51 -0400124void GrProcessor::operator delete(void* target) {
125 return MemoryPoolAccessor().pool()->release(target);
126}
127
128///////////////////////////////////////////////////////////////////////////////
129
130void GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500131 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000132}
133
Brian Salomonab015ef2017-04-04 10:15:51 -0400134void GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700135 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500136}
137
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400138void GrResourceIOProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500139 fImageStorageAccesses.push_back(access);
Brian Salomone57194f2017-01-09 15:30:02 -0500140}
141
Brian Salomonab015ef2017-04-04 10:15:51 -0400142void GrResourceIOProcessor::addPendingIOs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500143 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400144 sampler->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500145 }
146 for (const auto& buffer : fBufferAccesses) {
147 buffer->programBuffer()->markPendingIO();
148 }
149 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400150 imageStorage->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500151 }
152}
153
Brian Salomonab015ef2017-04-04 10:15:51 -0400154void GrResourceIOProcessor::removeRefs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500155 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400156 sampler->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500157 }
158 for (const auto& buffer : fBufferAccesses) {
159 buffer->programBuffer()->removeRef();
160 }
161 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400162 imageStorage->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500163 }
164}
165
Brian Salomonab015ef2017-04-04 10:15:51 -0400166void GrResourceIOProcessor::pendingIOComplete() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500167 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400168 sampler->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500169 }
170 for (const auto& buffer : fBufferAccesses) {
171 buffer->programBuffer()->pendingIOComplete();
172 }
173 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400174 imageStorage->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500175 }
cdalton74b8d322016-04-11 14:47:28 -0700176}
177
Robert Phillips9bee2e52017-05-29 12:37:20 -0400178bool GrResourceIOProcessor::instantiate(GrResourceProvider* resourceProvider) const {
179 for (const auto& sampler : fTextureSamplers) {
180 if (!sampler->instantiate(resourceProvider)) {
181 return false;
182 }
183 }
184
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400185 // MDB TODO: instantiate 'fBufferAccesses' here as well
186
Robert Phillips9bee2e52017-05-29 12:37:20 -0400187 for (const auto& imageStorage : fImageStorageAccesses) {
188 if (!imageStorage->instantiate(resourceProvider)) {
189 return false;
190 }
191 }
192
193 return true;
194}
195
Brian Salomonab015ef2017-04-04 10:15:51 -0400196bool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500197 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomonf9f45122016-11-29 11:59:17 -0500198 this->numBuffers() != that.numBuffers() ||
199 this->numImageStorages() != that.numImageStorages()) {
bsalomon420d7e92014-10-16 09:18:09 -0700200 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000201 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500202 for (int i = 0; i < this->numTextureSamplers(); ++i) {
203 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700204 return false;
205 }
206 }
cdalton74b8d322016-04-11 14:47:28 -0700207 for (int i = 0; i < this->numBuffers(); ++i) {
208 if (this->bufferAccess(i) != that.bufferAccess(i)) {
209 return false;
210 }
211 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500212 for (int i = 0; i < this->numImageStorages(); ++i) {
213 if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
214 return false;
215 }
216 }
bsalomon420d7e92014-10-16 09:18:09 -0700217 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000218}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700219
joshualitta5305a12014-10-10 17:47:00 -0700220///////////////////////////////////////////////////////////////////////////////////////////////////
221
Brian Salomonab015ef2017-04-04 10:15:51 -0400222GrResourceIOProcessor::TextureSampler::TextureSampler() {}
Brian Salomon0bbecb22016-11-17 11:38:22 -0500223
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400224GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400225 const GrSamplerState& samplerState) {
226 this->reset(std::move(proxy), samplerState);
Robert Phillips901f29a2017-01-24 16:24:41 -0500227}
228
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400229GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400230 GrSamplerState::Filter filterMode,
231 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400232 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400233 this->reset(std::move(proxy), filterMode, wrapXAndY, visibility);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500234}
235
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400236void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400237 const GrSamplerState& samplerState,
Brian Salomonab015ef2017-04-04 10:15:51 -0400238 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400239 fSamplerState = samplerState;
Robert Phillips18166ee2017-06-01 12:55:44 -0400240 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400241 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
Robert Phillips30f9bc62017-02-22 15:28:38 -0500242 fVisibility = visibility;
243}
244
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400245void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400246 GrSamplerState::Filter filterMode,
247 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400248 GrShaderFlags visibility) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400249 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
250 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400251 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500252 fVisibility = visibility;
253}
254
Brian Salomon0bbecb22016-11-17 11:38:22 -0500255///////////////////////////////////////////////////////////////////////////////////////////////////
256
Robert Phillips8a02f652017-05-12 14:49:16 -0400257GrResourceIOProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400258 GrIOType ioType,
259 GrSLMemoryModel memoryModel,
260 GrSLRestrict restrict,
Robert Phillips8a02f652017-05-12 14:49:16 -0400261 GrShaderFlags visibility)
262 : fProxyRef(std::move(proxy), ioType) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400263 SkASSERT(fProxyRef.get());
Robert Phillips8a02f652017-05-12 14:49:16 -0400264
Brian Salomonf9f45122016-11-29 11:59:17 -0500265 fMemoryModel = memoryModel;
266 fRestrict = restrict;
267 fVisibility = visibility;
268 // We currently infer this from the config. However, we could allow the client to specify
269 // a format that is different but compatible with the config.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400270 switch (fProxyRef.get()->config()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500271 case kRGBA_8888_GrPixelConfig:
272 fFormat = GrImageStorageFormat::kRGBA8;
273 break;
274 case kRGBA_8888_sint_GrPixelConfig:
275 fFormat = GrImageStorageFormat::kRGBA8i;
276 break;
277 case kRGBA_half_GrPixelConfig:
278 fFormat = GrImageStorageFormat::kRGBA16f;
279 break;
280 case kRGBA_float_GrPixelConfig:
281 fFormat = GrImageStorageFormat::kRGBA32f;
282 break;
283 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400284 SK_ABORT("Config is not (yet) supported as image storage.");
Brian Salomonf9f45122016-11-29 11:59:17 -0500285 break;
286 }
287}