blob: 2b02b8df20af089d4cc7292cf4d434d9540814ec [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
Robert Phillips296b1cc2017-03-15 10:42:12 -040028const GrCaps* GrProcessorTestData::caps() {
29 return fContext->caps();
30}
31
32#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
joshualitteb2a6762014-12-04 11:35:33 -080033class GrFragmentProcessor;
34class GrGeometryProcessor;
35
joshualitt9e87fa72014-10-09 13:12:35 -070036/*
37 * Originally these were both in the processor unit test header, but then it seemed to cause linker
38 * problems on android.
39 */
Brian Salomon1c053642017-07-24 10:16:19 -040040template <>
41SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
42 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
joshualitt9e87fa72014-10-09 13:12:35 -070043 return &gFactories;
44}
45
Brian Salomon1c053642017-07-24 10:16:19 -040046template <>
47SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
48 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
Brian Salomona8f80de2017-01-07 09:37:13 -050049 return &gFactories;
50}
51
Brian Salomona1633922017-01-09 11:46:10 -050052SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() {
53 static SkTArray<GrXPFactoryTestFactory*, true> gFactories;
54 return &gFactories;
55}
56
joshualitt9e87fa72014-10-09 13:12:35 -070057/*
58 * To ensure we always have successful static initialization, before creating from the factories
59 * we verify the count is as expected. If a new factory is added, then these numbers must be
60 * manually adjusted.
61 */
Yuqian Li915817b2017-12-14 13:45:08 -050062static const int kFPFactoryCount = 38;
joshualitt4973d9d2014-11-08 09:24:25 -080063static const int kGPFactoryCount = 14;
Brian Salomon1c4717b2017-01-06 12:54:58 -050064static const int kXPFactoryCount = 4;
joshualitt9e87fa72014-10-09 13:12:35 -070065
Brian Salomon1c053642017-07-24 10:16:19 -040066template <>
67void GrFragmentProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070068 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040069 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
70 kFPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040071 SK_ABORT("Wrong number of fragment processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070072 }
73}
74
Brian Salomon1c053642017-07-24 10:16:19 -040075template <>
76void GrGeometryProcessorTestFactory::VerifyFactoryCount() {
joshualitt9e87fa72014-10-09 13:12:35 -070077 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040078 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
79 kGPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040080 SK_ABORT("Wrong number of geometry processor factories!");
joshualitt9e87fa72014-10-09 13:12:35 -070081 }
82}
83
Brian Salomona1633922017-01-09 11:46:10 -050084void GrXPFactoryTestFactory::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080085 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040086 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
87 kXPFactoryCount, GetFactories()->count());
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040088 SK_ABORT("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080089 }
90}
91
joshualitt9e87fa72014-10-09 13:12:35 -070092#endif
Hal Canary6f6961e2017-01-31 13:50:44 -050093#endif
joshualitt9e87fa72014-10-09 13:12:35 -070094
bsalomon5baedd62015-03-09 12:15:53 -070095
joshualitt23ac62c2015-03-30 09:53:47 -070096// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
97// different threads. The GrContext is not used concurrently on different threads and there is a
98// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070099// GrContexts and those contexts may be in use concurrently on different threads.
100namespace {
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400101#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
mtklein15923c92016-02-29 10:14:38 -0800102static SkSpinlock gProcessorSpinlock;
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400103#endif
bsalomon5baedd62015-03-09 12:15:53 -0700104class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000105public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000106
msarett68440f82016-08-29 14:52:24 -0700107// We know in the Android framework there is only one GrContext.
108#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
109 MemoryPoolAccessor() {}
110 ~MemoryPoolAccessor() {}
111#else
112 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700113 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700114#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000115
bsalomon5baedd62015-03-09 12:15:53 -0700116 GrMemoryPool* pool() const {
117 static GrMemoryPool gPool(4096, 4096);
118 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000119 }
120};
bsalomon5baedd62015-03-09 12:15:53 -0700121}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000122
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000123///////////////////////////////////////////////////////////////////////////////
124
Brian Salomonab015ef2017-04-04 10:15:51 -0400125void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000126
Brian Salomonab015ef2017-04-04 10:15:51 -0400127void GrProcessor::operator delete(void* target) {
128 return MemoryPoolAccessor().pool()->release(target);
129}
130
131///////////////////////////////////////////////////////////////////////////////
132
133void GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500134 fTextureSamplers.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000135}
136
Brian Salomonab015ef2017-04-04 10:15:51 -0400137void GrResourceIOProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700138 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -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 }
Brian Salomone57194f2017-01-09 15:30:02 -0500148}
149
Brian Salomonab015ef2017-04-04 10:15:51 -0400150void GrResourceIOProcessor::removeRefs() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500151 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400152 sampler->programProxy()->removeRef();
Brian Salomone57194f2017-01-09 15:30:02 -0500153 }
154 for (const auto& buffer : fBufferAccesses) {
155 buffer->programBuffer()->removeRef();
156 }
Brian Salomone57194f2017-01-09 15:30:02 -0500157}
158
Brian Salomonab015ef2017-04-04 10:15:51 -0400159void GrResourceIOProcessor::pendingIOComplete() const {
Brian Salomone57194f2017-01-09 15:30:02 -0500160 for (const auto& sampler : fTextureSamplers) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400161 sampler->programProxy()->pendingIOComplete();
Brian Salomone57194f2017-01-09 15:30:02 -0500162 }
163 for (const auto& buffer : fBufferAccesses) {
164 buffer->programBuffer()->pendingIOComplete();
165 }
cdalton74b8d322016-04-11 14:47:28 -0700166}
167
Robert Phillips9bee2e52017-05-29 12:37:20 -0400168bool GrResourceIOProcessor::instantiate(GrResourceProvider* resourceProvider) const {
169 for (const auto& sampler : fTextureSamplers) {
170 if (!sampler->instantiate(resourceProvider)) {
171 return false;
172 }
173 }
174
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400175 // MDB TODO: instantiate 'fBufferAccesses' here as well
176
Robert Phillips9bee2e52017-05-29 12:37:20 -0400177 return true;
178}
179
Brian Salomonab015ef2017-04-04 10:15:51 -0400180bool GrResourceIOProcessor::hasSameSamplersAndAccesses(const GrResourceIOProcessor& that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500181 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomon559f5562017-11-15 14:28:33 -0500182 this->numBuffers() != that.numBuffers()) {
bsalomon420d7e92014-10-16 09:18:09 -0700183 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000184 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500185 for (int i = 0; i < this->numTextureSamplers(); ++i) {
186 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700187 return false;
188 }
189 }
cdalton74b8d322016-04-11 14:47:28 -0700190 for (int i = 0; i < this->numBuffers(); ++i) {
191 if (this->bufferAccess(i) != that.bufferAccess(i)) {
192 return false;
193 }
194 }
bsalomon420d7e92014-10-16 09:18:09 -0700195 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000196}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700197
joshualitta5305a12014-10-10 17:47:00 -0700198///////////////////////////////////////////////////////////////////////////////////////////////////
199
Brian Salomonab015ef2017-04-04 10:15:51 -0400200GrResourceIOProcessor::TextureSampler::TextureSampler() {}
Brian Salomon0bbecb22016-11-17 11:38:22 -0500201
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400202GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400203 const GrSamplerState& samplerState) {
204 this->reset(std::move(proxy), samplerState);
Robert Phillips901f29a2017-01-24 16:24:41 -0500205}
206
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400207GrResourceIOProcessor::TextureSampler::TextureSampler(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400208 GrSamplerState::Filter filterMode,
209 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400210 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400211 this->reset(std::move(proxy), filterMode, wrapXAndY, visibility);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500212}
213
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400214void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400215 const GrSamplerState& samplerState,
Brian Salomonab015ef2017-04-04 10:15:51 -0400216 GrShaderFlags visibility) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400217 fSamplerState = samplerState;
Robert Phillips18166ee2017-06-01 12:55:44 -0400218 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400219 fSamplerState.setFilterMode(SkTMin(samplerState.filter(), this->proxy()->highestFilterMode()));
Robert Phillips30f9bc62017-02-22 15:28:38 -0500220 fVisibility = visibility;
221}
222
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400223void GrResourceIOProcessor::TextureSampler::reset(sk_sp<GrTextureProxy> proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400224 GrSamplerState::Filter filterMode,
225 GrSamplerState::WrapMode wrapXAndY,
Brian Salomonab015ef2017-04-04 10:15:51 -0400226 GrShaderFlags visibility) {
Robert Phillips18166ee2017-06-01 12:55:44 -0400227 fProxyRef.setProxy(std::move(proxy), kRead_GrIOType);
228 filterMode = SkTMin(filterMode, this->proxy()->highestFilterMode());
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400229 fSamplerState = GrSamplerState(wrapXAndY, filterMode);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500230 fVisibility = visibility;
231}