blob: f4f7ccb138f933acd8db3a734a2801491e163b38 [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"
egdaniel605dd0f2014-11-12 08:35:25 -080011#include "GrInvariantOutput.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000012#include "GrMemoryPool.h"
Brian Salomon514baff2016-11-17 15:17:07 -050013#include "GrSamplerParams.h"
Brian Salomon0bbecb22016-11-17 11:38:22 -050014#include "GrTexturePriv.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
joshualitt9e87fa72014-10-09 13:12:35 -070018#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
19
joshualitteb2a6762014-12-04 11:35:33 -080020class GrFragmentProcessor;
21class GrGeometryProcessor;
22
joshualitt9e87fa72014-10-09 13:12:35 -070023/*
24 * Originally these were both in the processor unit test header, but then it seemed to cause linker
25 * problems on android.
26 */
27template<>
28SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
29GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
30 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
31 return &gFactories;
32}
33
34template<>
egdanielc2304142014-12-11 13:15:13 -080035SkTArray<GrProcessorTestFactory<GrXPFactory>*, true>*
36GrProcessorTestFactory<GrXPFactory>::GetFactories() {
37 static SkTArray<GrProcessorTestFactory<GrXPFactory>*, true> gFactories;
egdaniel378092f2014-12-03 10:40:13 -080038 return &gFactories;
39}
40
41template<>
joshualitt9e87fa72014-10-09 13:12:35 -070042SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
43GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
44 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
45 return &gFactories;
46}
47
48/*
49 * To ensure we always have successful static initialization, before creating from the factories
50 * we verify the count is as expected. If a new factory is added, then these numbers must be
51 * manually adjusted.
52 */
Ben Wagnerc03e1c52016-10-17 15:20:02 -040053static const int kFPFactoryCount = 40;
joshualitt4973d9d2014-11-08 09:24:25 -080054static const int kGPFactoryCount = 14;
Ben Wagnerc03e1c52016-10-17 15:20:02 -040055static const int kXPFactoryCount = 5;
joshualitt9e87fa72014-10-09 13:12:35 -070056
57template<>
58void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
59 if (kFPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040060 SkDebugf("\nExpected %d fragment processor factories, found %d.\n",
61 kFPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070062 SkFAIL("Wrong number of fragment processor factories!");
63 }
64}
65
66template<>
67void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
68 if (kGPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040069 SkDebugf("\nExpected %d geometry processor factories, found %d.\n",
70 kGPFactoryCount, GetFactories()->count());
joshualitt9e87fa72014-10-09 13:12:35 -070071 SkFAIL("Wrong number of geometry processor factories!");
72 }
73}
74
egdaniel378092f2014-12-03 10:40:13 -080075template<>
egdanielc2304142014-12-11 13:15:13 -080076void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080077 if (kXPFactoryCount != GetFactories()->count()) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040078 SkDebugf("\nExpected %d xp factory factories, found %d.\n",
79 kXPFactoryCount, GetFactories()->count());
egdanielc2304142014-12-11 13:15:13 -080080 SkFAIL("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080081 }
82}
83
joshualitt9e87fa72014-10-09 13:12:35 -070084#endif
85
bsalomon5baedd62015-03-09 12:15:53 -070086
joshualitt23ac62c2015-03-30 09:53:47 -070087// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
88// different threads. The GrContext is not used concurrently on different threads and there is a
89// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070090// GrContexts and those contexts may be in use concurrently on different threads.
91namespace {
mtklein15923c92016-02-29 10:14:38 -080092static SkSpinlock gProcessorSpinlock;
bsalomon5baedd62015-03-09 12:15:53 -070093class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000094public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000095
msarett68440f82016-08-29 14:52:24 -070096// We know in the Android framework there is only one GrContext.
97#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
98 MemoryPoolAccessor() {}
99 ~MemoryPoolAccessor() {}
100#else
101 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -0700102 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -0700103#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000104
bsalomon5baedd62015-03-09 12:15:53 -0700105 GrMemoryPool* pool() const {
106 static GrMemoryPool gPool(4096, 4096);
107 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000108 }
109};
bsalomon5baedd62015-03-09 12:15:53 -0700110}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000111
bsalomon5baedd62015-03-09 12:15:53 -0700112int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000113
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000114///////////////////////////////////////////////////////////////////////////////
115
joshualittb0a8a372014-09-23 09:50:21 -0700116GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000117
Brian Salomon0bbecb22016-11-17 11:38:22 -0500118void GrProcessor::addTextureSampler(const TextureSampler* access) {
119 fTextureSamplers.push_back(access);
Brian Salomondb4183d2016-11-17 12:48:40 -0500120 this->addGpuResource(access->programTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000121}
122
Brian Salomonb014cca2016-11-18 11:39:15 -0500123void GrProcessor::addBufferAccess(const BufferAccess* access) {
cdalton74b8d322016-04-11 14:47:28 -0700124 fBufferAccesses.push_back(access);
Brian Salomonf9f45122016-11-29 11:59:17 -0500125 this->addGpuResource(access->programBuffer());
126}
127
128void GrProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
129 fImageStorageAccesses.push_back(access);
130 this->addGpuResource(access->programTexture());
cdalton74b8d322016-04-11 14:47:28 -0700131}
132
joshualittb0a8a372014-09-23 09:50:21 -0700133void* GrProcessor::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -0700134 return MemoryPoolAccessor().pool()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000135}
136
joshualittb0a8a372014-09-23 09:50:21 -0700137void GrProcessor::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -0700138 return MemoryPoolAccessor().pool()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000139}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000140
Brian Salomonf9f45122016-11-29 11:59:17 -0500141bool GrProcessor::hasSameSamplersAndAccesses(const GrProcessor &that) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500142 if (this->numTextureSamplers() != that.numTextureSamplers() ||
Brian Salomonf9f45122016-11-29 11:59:17 -0500143 this->numBuffers() != that.numBuffers() ||
144 this->numImageStorages() != that.numImageStorages()) {
bsalomon420d7e92014-10-16 09:18:09 -0700145 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000146 }
Brian Salomon0bbecb22016-11-17 11:38:22 -0500147 for (int i = 0; i < this->numTextureSamplers(); ++i) {
148 if (this->textureSampler(i) != that.textureSampler(i)) {
bsalomon420d7e92014-10-16 09:18:09 -0700149 return false;
150 }
151 }
cdalton74b8d322016-04-11 14:47:28 -0700152 for (int i = 0; i < this->numBuffers(); ++i) {
153 if (this->bufferAccess(i) != that.bufferAccess(i)) {
154 return false;
155 }
156 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500157 for (int i = 0; i < this->numImageStorages(); ++i) {
158 if (this->imageStorageAccess(i) != that.imageStorageAccess(i)) {
159 return false;
160 }
161 }
bsalomon420d7e92014-10-16 09:18:09 -0700162 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000163}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700164
joshualitta5305a12014-10-10 17:47:00 -0700165///////////////////////////////////////////////////////////////////////////////////////////////////
166
Brian Salomon0bbecb22016-11-17 11:38:22 -0500167GrProcessor::TextureSampler::TextureSampler() {}
168
Brian Salomon514baff2016-11-17 15:17:07 -0500169GrProcessor::TextureSampler::TextureSampler(GrTexture* texture, const GrSamplerParams& params) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500170 this->reset(texture, params);
171}
172
173GrProcessor::TextureSampler::TextureSampler(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500174 GrSamplerParams::FilterMode filterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500175 SkShader::TileMode tileXAndY,
176 GrShaderFlags visibility) {
177 this->reset(texture, filterMode, tileXAndY, visibility);
178}
179
180void GrProcessor::TextureSampler::reset(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500181 const GrSamplerParams& params,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500182 GrShaderFlags visibility) {
183 SkASSERT(texture);
184 fTexture.set(SkRef(texture), kRead_GrIOType);
185 fParams = params;
186 fParams.setFilterMode(SkTMin(params.filterMode(), texture->texturePriv().highestFilterMode()));
187 fVisibility = visibility;
188}
189
190void GrProcessor::TextureSampler::reset(GrTexture* texture,
Brian Salomon514baff2016-11-17 15:17:07 -0500191 GrSamplerParams::FilterMode filterMode,
Brian Salomon0bbecb22016-11-17 11:38:22 -0500192 SkShader::TileMode tileXAndY,
193 GrShaderFlags visibility) {
194 SkASSERT(texture);
195 fTexture.set(SkRef(texture), kRead_GrIOType);
196 filterMode = SkTMin(filterMode, texture->texturePriv().highestFilterMode());
197 fParams.reset(tileXAndY, filterMode);
198 fVisibility = visibility;
199}
200
201///////////////////////////////////////////////////////////////////////////////////////////////////
202
Brian Salomonf9f45122016-11-29 11:59:17 -0500203GrProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType,
204 GrSLMemoryModel memoryModel,
205 GrSLRestrict restrict,
206 GrShaderFlags visibility) {
207 SkASSERT(texture);
208 fTexture.set(texture.release(), ioType);
209 fMemoryModel = memoryModel;
210 fRestrict = restrict;
211 fVisibility = visibility;
212 // We currently infer this from the config. However, we could allow the client to specify
213 // a format that is different but compatible with the config.
214 switch (fTexture.get()->config()) {
215 case kRGBA_8888_GrPixelConfig:
216 fFormat = GrImageStorageFormat::kRGBA8;
217 break;
218 case kRGBA_8888_sint_GrPixelConfig:
219 fFormat = GrImageStorageFormat::kRGBA8i;
220 break;
221 case kRGBA_half_GrPixelConfig:
222 fFormat = GrImageStorageFormat::kRGBA16f;
223 break;
224 case kRGBA_float_GrPixelConfig:
225 fFormat = GrImageStorageFormat::kRGBA32f;
226 break;
227 default:
228 SkFAIL("Config is not (yet) supported as image storage.");
229 break;
230 }
231}
232
233///////////////////////////////////////////////////////////////////////////////////////////////////
234
egdaniel915187b2014-12-05 12:58:28 -0800235// Initial static variable from GrXPFactory
Brian Salomonf9f45122016-11-29 11:59:17 -0500236int32_t GrXPFactory::gCurrXPFClassID = GrXPFactory::kIllegalXPFClassID;