blob: f2b810252f7c8bc1672bfc775344570e75cf1c62 [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"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050010#include "GrContextPriv.h"
joshualitt2e3b3e32014-12-09 13:31:14 -080011#include "GrGeometryProcessor.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000012#include "GrMemoryPool.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040013#include "GrSamplerState.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() {
Robert Phillips6be756b2018-01-16 15:07:54 -050021 return fContext->contextPriv().resourceProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040022}
23
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024GrProxyProvider* GrProcessorTestData::proxyProvider() {
25 return fContext->contextPriv().proxyProvider();
26}
27
Brian Salomonc7fe0f72018-05-11 10:14:21 -040028const GrCaps* GrProcessorTestData::caps() { return fContext->contextPriv().caps(); }
Robert Phillips296b1cc2017-03-15 10:42:12 -040029
30#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
joshualitteb2a6762014-12-04 11:35:33 -080031class GrFragmentProcessor;
32class GrGeometryProcessor;
33
joshualitt9e87fa72014-10-09 13:12:35 -070034/*
35 * Originally these were both in the processor unit test header, but then it seemed to cause linker
36 * problems on android.
37 */
Brian Salomon1c053642017-07-24 10:16:19 -040038template <>
39SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
40 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
joshualitt9e87fa72014-10-09 13:12:35 -070041 return &gFactories;
42}
43
Brian Salomon1c053642017-07-24 10:16:19 -040044template <>
45SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
46 static SkTArray<GrGeometryProcessorTestFactory*, 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 Osman47c27512018-06-18 10:58:51 -040060static const int kFPFactoryCount = 37;
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
Brian Salomon1c053642017-07-24 10:16:19 -040064template <>
65void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070066 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());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040069 SK_ABORT("Wrong number of fragment processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070070 }
71}
72
Brian Salomon1c053642017-07-24 10:16:19 -040073template <>
74void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070075 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());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040078 SK_ABORT("Wrong number of geometry processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070079 }
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());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040086 SK_ABORT("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 {
Leon Scroggins III981a31e2017-10-06 11:53:53 -040099#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
mtklein15923c92016-02-29 10:14:38 -0800100static SkSpinlock gProcessorSpinlock;
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400101#endif
bsalomon5baedd62015-03-09 12:15:53 -0700102class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000103public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000104
msarett68440f82016-08-29 14:52:24 -0700105// We know in the Android framework there is only one GrContext.
106#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
107 MemoryPoolAccessor() {}
108 ~MemoryPoolAccessor() {}
109#else
110 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700111 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700112#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000113
bsalomon5baedd62015-03-09 12:15:53 -0700114 GrMemoryPool* pool() const {
115 static GrMemoryPool gPool(4096, 4096);
116 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000117 }
118};
bsalomon5baedd62015-03-09 12:15:53 -0700119}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +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) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500132 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000133}
134
Brian Salomonab015ef2017-04-04 10:15:51 -0400135void GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700136 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500137}
138
Brian Salomonab015ef2017-04-04 10:15:51 -0400139void GrResourceIOProcessor::addPendingIOs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500140 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400141 sampler->programProxy()->markPendingIO();
Brian Salomone57194f2017-01-09 15:30:02 -0500142 }
143 for (const auto& buffer : fBufferAccesses) {
144 buffer->programBuffer()->markPendingIO();
145 }
Brian Salomone57194f2017-01-09 15:30:02 -0500146}
147
Brian Salomonab015ef2017-04-04 10:15:51 -0400148void GrResourceIOProcessor::removeRefs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500149 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400150 sampler->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500151 }
152 for (const auto& buffer : fBufferAccesses) {
153 buffer->programBuffer()->removeRef();
154 }
Brian Salomone57194f2017-01-09 15:30:02 -0500155}
156
Brian Salomonab015ef2017-04-04 10:15:51 -0400157void GrResourceIOProcessor::pendingIOComplete() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500158 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400159 sampler->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500160 }
161 for (const auto& buffer : fBufferAccesses) {
162 buffer->programBuffer()->pendingIOComplete();
163 }
cdalton74b8d322016-04-11 14:47:28 -0700164}
165
Robert Phillips9bee2e52017-05-29 12:37:20 -0400166bool GrResourceIOProcessor::instantiate(GrResourceProvider* resourceProvider) const {
167 for (const auto& sampler : fTextureSamplers) {
168 if (!sampler->instantiate(resourceProvider)) {
169 return false;
170 }
171 }
172
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400173 // MDB TODO: instantiate 'fBufferAccesses' here as well
174
Robert Phillips9bee2e52017-05-29 12:37:20 -0400175 return true;
176}
177
Brian Salomonab015ef2017-04-04 10:15:51 -0400178bool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500179 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomon559f5562017-11-15 14:28:33 -0500180 this->numBuffers() != that.numBuffers()) {
bsalomon420d7e92014-10-16 09:18:09 -0700181 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000182 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500183 for (int i = 0; i < this->numTextureSamplers(); ++i) {
184 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700185 return false;
186 }
187 }
cdalton74b8d322016-04-11 14:47:28 -0700188 for (int i = 0; i < this->numBuffers(); ++i) {
189 if (this->bufferAccess(i) != that.bufferAccess(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 Salomonab015ef2017-04-04 10:15:51 -0400198GrResourceIOProcessor::TextureSampler::TextureSampler() {}
Brian Salomon0bbecb22016-11-17 11:38:22 -0500199
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400200GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400201 const GrSamplerState& samplerState) {
202 this->reset(std::move(proxy), samplerState);
Robert Phillips901f29a2017-01-24 16:24:41 -0500203}
204
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400205GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400206 GrSamplerState::Filter filterMode,
207 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400208 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400209 this->reset(std::move(proxy), filterMode, wrapXAndY, visibility);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500210}
211
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400212void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400213 const GrSamplerState& samplerState,
Brian Salomonab015ef2017-04-04 10:15:51 -0400214 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400215 fSamplerState = samplerState;
Robert Phillips18166ee2017-06-01 12:55:44 -0400216 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400217 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
Robert Phillips30f9bc62017-02-22 15:28:38 -0500218 fVisibility = visibility;
219}
220
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400221void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400222 GrSamplerState::Filter filterMode,
223 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400224 GrShaderFlags visibility) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400225 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
226 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400227 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500228 fVisibility = visibility;
229}