blob: a7dd63c5e18d542f7bc81dfdd4c3463442cdd835 [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 */
Brian Salomon1c053642017-07-24 10:16:19 -040036template <>
37SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
38 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
joshualitt9e87fa72014-10-09 13:12:35 -070039 return &gFactories;
40}
41
Brian Salomon1c053642017-07-24 10:16:19 -040042template <>
43SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
44 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050045 return &gFactories;
46}
47
Brian Salomona1633922017-01-09 11:46:10 -050048SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
49 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
50 return &gFactories;
51}
52
joshualitt9e87fa72014-10-09 13:12:35 -070053/*
54 * To ensure we always have successful static initialization, before creating from the factories
55 * we verify the count is as expected. If a new factory is added, then these numbers must be
56 * manually adjusted.
57 */
Mike Reedf2ae2b22017-05-30 15:22:54 -040058static const int kFPFactoryCount = 42;
joshualitt4973d9d2014-11-08 09:24:25 -080059static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050060static const int kXPFactoryCount = 4;
joshualitt9e87fa72014-10-09 13:12:35 -070061
Brian Salomon1c053642017-07-24 10:16:19 -040062template <>
63void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070064 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040065 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
66 kFPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040067 SK_ABORT("Wrong number of fragment processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070068 }
69}
70
Brian Salomon1c053642017-07-24 10:16:19 -040071template <>
72void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070073 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040074 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
75 kGPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040076 SK_ABORT("Wrong number of geometry processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070077 }
78}
79
Brian Salomona1633922017-01-09 11:46:10 -050080void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080081 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040082 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
83 kXPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040084 SK_ABORT("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080085 }
86}
87
joshualitt9e87fa72014-10-09 13:12:35 -070088#endif
Hal Canary6f6961e2017-01-31 13:50:44 -050089#endif
joshualitt9e87fa72014-10-09 13:12:35 -070090
bsalomon5baedd62015-03-09 12:15:53 -070091
joshualitt23ac62c2015-03-30 09:53:47 -070092// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
93// different threads. The GrContext is not used concurrently on different threads and there is a
94// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070095// GrContexts and those contexts may be in use concurrently on different threads.
96namespace {
mtklein15923c92016-02-29 10:14:38 -080097static SkSpinlock gProcessorSpinlock;
bsalomon5baedd62015-03-09 12:15:53 -070098class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000099public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000100
msarett68440f82016-08-29 14:52:24 -0700101// We know in the Android framework there is only one GrContext.
102#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
103 MemoryPoolAccessor() {}
104 ~MemoryPoolAccessor() {}
105#else
106 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700107 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700108#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000109
bsalomon5baedd62015-03-09 12:15:53 -0700110 GrMemoryPool* pool() const {
111 static GrMemoryPool gPool(4096, 4096);
112 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000113 }
114};
bsalomon5baedd62015-03-09 12:15:53 -0700115}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000116
bsalomon5baedd62015-03-09 12:15:53 -0700117int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000118
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000119///////////////////////////////////////////////////////////////////////////////
120
Brian Salomonab015ef2017-04-04 10:15:51 -0400121void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000122
Brian Salomonab015ef2017-04-04 10:15:51 -0400123void GrProcessor::operator delete(void* target) {
124 return MemoryPoolAccessor().pool()->release(target);
125}
126
127///////////////////////////////////////////////////////////////////////////////
128
129void GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500130 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000131}
132
Brian Salomonab015ef2017-04-04 10:15:51 -0400133void GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700134 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500135}
136
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400137void GrResourceIOProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500138 fImageStorageAccesses.push_back(access);
Brian Salomone57194f2017-01-09 15:30:02 -0500139}
140
Brian Salomonab015ef2017-04-04 10:15:51 -0400141void GrResourceIOProcessor::addPendingIOs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500142 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400143 sampler->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500144 }
145 for (const auto& buffer : fBufferAccesses) {
146 buffer->programBuffer()->markPendingIO();
147 }
148 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400149 imageStorage->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500150 }
151}
152
Brian Salomonab015ef2017-04-04 10:15:51 -0400153void GrResourceIOProcessor::removeRefs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500154 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400155 sampler->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500156 }
157 for (const auto& buffer : fBufferAccesses) {
158 buffer->programBuffer()->removeRef();
159 }
160 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400161 imageStorage->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500162 }
163}
164
Brian Salomonab015ef2017-04-04 10:15:51 -0400165void GrResourceIOProcessor::pendingIOComplete() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500166 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400167 sampler->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500168 }
169 for (const auto& buffer : fBufferAccesses) {
170 buffer->programBuffer()->pendingIOComplete();
171 }
172 for (const auto& imageStorage : fImageStorageAccesses) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400173 imageStorage->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500174 }
cdalton74b8d322016-04-11 14:47:28 -0700175}
176
Robert Phillips9bee2e52017-05-29 12:37:20 -0400177bool GrResourceIOProcessor::instantiate(GrResourceProvider* resourceProvider) const {
178 for (const auto& sampler : fTextureSamplers) {
179 if (!sampler->instantiate(resourceProvider)) {
180 return false;
181 }
182 }
183
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400184 // MDB TODO: instantiate 'fBufferAccesses' here as well
185
Robert Phillips9bee2e52017-05-29 12:37:20 -0400186 for (const auto& imageStorage : fImageStorageAccesses) {
187 if (!imageStorage->instantiate(resourceProvider)) {
188 return false;
189 }
190 }
191
192 return true;
193}
194
Brian Salomonab015ef2017-04-04 10:15:51 -0400195bool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500196 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomonf9f45122016-11-29 11:59:17 -0500197 this->numBuffers() != that.numBuffers() ||
198 this->numImageStorages() != that.numImageStorages()) {
bsalomon420d7e92014-10-16 09:18:09 -0700199 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000200 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500201 for (int i = 0; i < this->numTextureSamplers(); ++i) {
202 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700203 return false;
204 }
205 }
cdalton74b8d322016-04-11 14:47:28 -0700206 for (int i = 0; i < this->numBuffers(); ++i) {
207 if (this->bufferAccess(i) != that.bufferAccess(i)) {
208 return false;
209 }
210 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500211 for (int i = 0; i < this->numImageStorages(); ++i) {
212 if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
213 return false;
214 }
215 }
bsalomon420d7e92014-10-16 09:18:09 -0700216 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000217}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700218
joshualitta5305a12014-10-10 17:47:00 -0700219///////////////////////////////////////////////////////////////////////////////////////////////////
220
Brian Salomonab015ef2017-04-04 10:15:51 -0400221GrResourceIOProcessor::TextureSampler::TextureSampler() {}
Brian Salomon0bbecb22016-11-17 11:38:22 -0500222
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400223GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400224 const GrSamplerParams& params) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400225 this->reset(std::move(proxy), params);
Robert Phillips901f29a2017-01-24 16:24:41 -0500226}
227
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400228GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400229 GrSamplerParams::FilterMode filterMode,
230 SkShader::TileMode tileXAndY,
231 GrShaderFlags visibility) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400232 this->reset(std::move(proxy), filterMode, tileXAndY, visibility);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500233}
234
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400235void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400236 const GrSamplerParams& params,
237 GrShaderFlags visibility) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400238 fParams = params;
Robert Phillips18166ee2017-06-01 12:55:44 -0400239 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
240 fParams.setFilterMode(SkTMin(params.filterMode(), this->proxy()->highestFilterMode()));
Robert Phillips30f9bc62017-02-22 15:28:38 -0500241 fVisibility = visibility;
242}
243
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400244void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400245 GrSamplerParams::FilterMode filterMode,
246 SkShader::TileMode tileXAndY,
247 GrShaderFlags visibility) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400248 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
249 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
Robert Phillips30f9bc62017-02-22 15:28:38 -0500250 fParams.reset(tileXAndY, filterMode);
251 fVisibility = visibility;
252}
253
Brian Salomon0bbecb22016-11-17 11:38:22 -0500254///////////////////////////////////////////////////////////////////////////////////////////////////
255
Robert Phillips8a02f652017-05-12 14:49:16 -0400256GrResourceIOProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTextureProxy> proxy,
Brian Salomonab015ef2017-04-04 10:15:51 -0400257 GrIOType ioType,
258 GrSLMemoryModel memoryModel,
259 GrSLRestrict restrict,
Robert Phillips8a02f652017-05-12 14:49:16 -0400260 GrShaderFlags visibility)
261 : fProxyRef(std::move(proxy), ioType) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400262 SkASSERT(fProxyRef.get());
Robert Phillips8a02f652017-05-12 14:49:16 -0400263
Brian Salomonf9f45122016-11-29 11:59:17 -0500264 fMemoryModel = memoryModel;
265 fRestrict = restrict;
266 fVisibility = visibility;
267 // We currently infer this from the config. However, we could allow the client to specify
268 // a format that is different but compatible with the config.
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400269 switch (fProxyRef.get()->config()) {
Brian Salomonf9f45122016-11-29 11:59:17 -0500270 case kRGBA_8888_GrPixelConfig:
271 fFormat = GrImageStorageFormat::kRGBA8;
272 break;
273 case kRGBA_8888_sint_GrPixelConfig:
274 fFormat = GrImageStorageFormat::kRGBA8i;
275 break;
276 case kRGBA_half_GrPixelConfig:
277 fFormat = GrImageStorageFormat::kRGBA16f;
278 break;
279 case kRGBA_float_GrPixelConfig:
280 fFormat = GrImageStorageFormat::kRGBA32f;
281 break;
282 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400283 SK_ABORT("Config is not (yet) supported as image storage.");
Brian Salomonf9f45122016-11-29 11:59:17 -0500284 break;
285 }
286}