blob: 89a31717dfbe5fa8c1e7eb7857c6d9e842bc2711 [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
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000118///////////////////////////////////////////////////////////////////////////////
119
Brian Salomonab015ef2017-04-04 10:15:51 -0400120void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000121
Brian Salomonab015ef2017-04-04 10:15:51 -0400122void GrProcessor::operator delete(void* target) {
123 return MemoryPoolAccessor().pool()->release(target);
124}
125
126///////////////////////////////////////////////////////////////////////////////
127
128void GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500129 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000130}
131
Brian Salomonab015ef2017-04-04 10:15:51 -0400132void GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700133 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500134}
135
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400136void GrResourceIOProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500137 fImageStorageAccesses.push_back(access);
Brian Salomone57194f2017-01-09 15:30:02 -0500138}
139
Brian Salomonab015ef2017-04-04 10:15:51 -0400140void GrResourceIOProcessor::addPendingIOs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500141 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400142 sampler->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500143 }
144 for (const auto& buffer : fBufferAccesses) {
145 buffer->programBuffer()->markPendingIO();
146 }
147 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400148 imageStorage->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500149 }
150}
151
Brian Salomonab015ef2017-04-04 10:15:51 -0400152void GrResourceIOProcessor::removeRefs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500153 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400154 sampler->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500155 }
156 for (const auto& buffer : fBufferAccesses) {
157 buffer->programBuffer()->removeRef();
158 }
159 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400160 imageStorage->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500161 }
162}
163
Brian Salomonab015ef2017-04-04 10:15:51 -0400164void GrResourceIOProcessor::pendingIOComplete() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500165 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400166 sampler->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500167 }
168 for (const auto& buffer : fBufferAccesses) {
169 buffer->programBuffer()->pendingIOComplete();
170 }
171 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400172 imageStorage->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500173 }
cdalton74b8d322016-04-11 14:47:28 -0700174}
175
Robert Phillips9bee2e52017-05-29 12:37:20 -0400176bool GrResourceIOProcessor::instantiate(GrResourceProvider* resourceProvider) const {
177 for (const auto& sampler : fTextureSamplers) {
178 if (!sampler->instantiate(resourceProvider)) {
179 return false;
180 }
181 }
182
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400183 // MDB TODO: instantiate 'fBufferAccesses' here as well
184
Robert Phillips9bee2e52017-05-29 12:37:20 -0400185 for (const auto& imageStorage : fImageStorageAccesses) {
186 if (!imageStorage->instantiate(resourceProvider)) {
187 return false;
188 }
189 }
190
191 return true;
192}
193
Brian Salomonab015ef2017-04-04 10:15:51 -0400194bool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500195 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomonf9f45122016-11-29 11:59:17 -0500196 this->numBuffers() != that.numBuffers() ||
197 this->numImageStorages() != that.numImageStorages()) {
bsalomon420d7e92014-10-16 09:18:09 -0700198 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000199 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500200 for (int i = 0; i < this->numTextureSamplers(); ++i) {
201 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700202 return false;
203 }
204 }
cdalton74b8d322016-04-11 14:47:28 -0700205 for (int i = 0; i < this->numBuffers(); ++i) {
206 if (this->bufferAccess(i) != that.bufferAccess(i)) {
207 return false;
208 }
209 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500210 for (int i = 0; i < this->numImageStorages(); ++i) {
211 if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
212 return false;
213 }
214 }
bsalomon420d7e92014-10-16 09:18:09 -0700215 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000216}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700217
joshualitta5305a12014-10-10 17:47:00 -0700218///////////////////////////////////////////////////////////////////////////////////////////////////
219
Brian Salomonab015ef2017-04-04 10:15:51 -0400220GrResourceIOProcessor::TextureSampler::TextureSampler() {}
Brian Salomon0bbecb22016-11-17 11:38:22 -0500221
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400222GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400223 const GrSamplerState& samplerState) {
224 this->reset(std::move(proxy), samplerState);
Robert Phillips901f29a2017-01-24 16:24:41 -0500225}
226
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400227GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400228 GrSamplerState::Filter filterMode,
229 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400230 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400231 this->reset(std::move(proxy), filterMode, wrapXAndY, visibility);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500232}
233
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400234void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400235 const GrSamplerState& samplerState,
Brian Salomonab015ef2017-04-04 10:15:51 -0400236 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400237 fSamplerState = samplerState;
Robert Phillips18166ee2017-06-01 12:55:44 -0400238 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400239 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
Robert Phillips30f9bc62017-02-22 15:28:38 -0500240 fVisibility = visibility;
241}
242
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400243void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400244 GrSamplerState::Filter filterMode,
245 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400246 GrShaderFlags visibility) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400247 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
248 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400249 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500250 fVisibility = visibility;
251}
252
Brian Salomon0bbecb22016-11-17 11:38:22 -0500253///////////////////////////////////////////////////////////////////////////////////////////////////
254
Robert Phillips8a02f652017-05-12 14:49:16 -0400255GrResourceIOProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400256 GrIOType ioType,
257 GrSLMemoryModel memoryModel,
258 GrSLRestrict restrict,
Robert Phillips8a02f652017-05-12 14:49:16 -0400259 GrShaderFlags visibility)
260 : fProxyRef(std::move(proxy), ioType) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400261 SkASSERT(fProxyRef.get());
Robert Phillips8a02f652017-05-12 14:49:16 -0400262
Brian Salomonf9f45122016-11-29 11:59:17 -0500263 fMemoryModel = memoryModel;
264 fRestrict = restrict;
265 fVisibility = visibility;
266 // We currently infer this from the config. However, we could allow the client to specify
267 // a format that is different but compatible with the config.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400268 switch (fProxyRef.get()->config()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500269 case kRGBA_8888_GrPixelConfig:
270 fFormat = GrImageStorageFormat::kRGBA8;
271 break;
272 case kRGBA_8888_sint_GrPixelConfig:
273 fFormat = GrImageStorageFormat::kRGBA8i;
274 break;
275 case kRGBA_half_GrPixelConfig:
276 fFormat = GrImageStorageFormat::kRGBA16f;
277 break;
278 case kRGBA_float_GrPixelConfig:
279 fFormat = GrImageStorageFormat::kRGBA32f;
280 break;
281 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400282 SK_ABORT("Config is not (yet) supported as image storage.");
Brian Salomonf9f45122016-11-29 11:59:17 -0500283 break;
284 }
285}