blob: b945b027f20986d0348d5699f85524e418e10550 [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"
egdaniel915187b2014-12-05 12:58:28 -080013#include "GrXferProcessor.h"
joshualitt23ac62c2015-03-30 09:53:47 -070014#include "SkSpinlock.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000015
joshualitt9e87fa72014-10-09 13:12:35 -070016#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
17
joshualitteb2a6762014-12-04 11:35:33 -080018class GrFragmentProcessor;
19class GrGeometryProcessor;
20
joshualitt9e87fa72014-10-09 13:12:35 -070021/*
22 * Originally these were both in the processor unit test header, but then it seemed to cause linker
23 * problems on android.
24 */
25template<>
26SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>*
27GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() {
28 static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories;
29 return &gFactories;
30}
31
32template<>
egdanielc2304142014-12-11 13:15:13 -080033SkTArray<GrProcessorTestFactory<GrXPFactory>*, true>*
34GrProcessorTestFactory<GrXPFactory>::GetFactories() {
35 static SkTArray<GrProcessorTestFactory<GrXPFactory>*, true> gFactories;
egdaniel378092f2014-12-03 10:40:13 -080036 return &gFactories;
37}
38
39template<>
joshualitt9e87fa72014-10-09 13:12:35 -070040SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>*
41GrProcessorTestFactory<GrGeometryProcessor>::GetFactories() {
42 static SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true> gFactories;
43 return &gFactories;
44}
45
46/*
47 * To ensure we always have successful static initialization, before creating from the factories
48 * we verify the count is as expected. If a new factory is added, then these numbers must be
49 * manually adjusted.
50 */
brianosman2d1ee792016-05-05 12:24:31 -070051static const int kFPFactoryCount = 40;
joshualitt4973d9d2014-11-08 09:24:25 -080052static const int kGPFactoryCount = 14;
reed59dc5422016-04-05 06:25:14 -070053static const int kXPFactoryCount = 6;
joshualitt9e87fa72014-10-09 13:12:35 -070054
55template<>
56void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {
57 if (kFPFactoryCount != GetFactories()->count()) {
58 SkFAIL("Wrong number of fragment processor factories!");
59 }
60}
61
62template<>
63void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() {
64 if (kGPFactoryCount != GetFactories()->count()) {
65 SkFAIL("Wrong number of geometry processor factories!");
66 }
67}
68
egdaniel378092f2014-12-03 10:40:13 -080069template<>
egdanielc2304142014-12-11 13:15:13 -080070void GrProcessorTestFactory<GrXPFactory>::VerifyFactoryCount() {
egdaniel378092f2014-12-03 10:40:13 -080071 if (kXPFactoryCount != GetFactories()->count()) {
egdanielc2304142014-12-11 13:15:13 -080072 SkFAIL("Wrong number of xp factory factories!");
egdaniel378092f2014-12-03 10:40:13 -080073 }
74}
75
joshualitt9e87fa72014-10-09 13:12:35 -070076#endif
77
bsalomon5baedd62015-03-09 12:15:53 -070078
joshualitt23ac62c2015-03-30 09:53:47 -070079// We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
80// different threads. The GrContext is not used concurrently on different threads and there is a
81// memory barrier between accesses of a context on different threads. Also, there may be multiple
bsalomon5baedd62015-03-09 12:15:53 -070082// GrContexts and those contexts may be in use concurrently on different threads.
83namespace {
mtklein15923c92016-02-29 10:14:38 -080084static SkSpinlock gProcessorSpinlock;
bsalomon5baedd62015-03-09 12:15:53 -070085class MemoryPoolAccessor {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000086public:
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000087
msarett68440f82016-08-29 14:52:24 -070088// We know in the Android framework there is only one GrContext.
89#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
90 MemoryPoolAccessor() {}
91 ~MemoryPoolAccessor() {}
92#else
93 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
joshualitt23ac62c2015-03-30 09:53:47 -070094 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
msarett68440f82016-08-29 14:52:24 -070095#endif
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000096
bsalomon5baedd62015-03-09 12:15:53 -070097 GrMemoryPool* pool() const {
98 static GrMemoryPool gPool(4096, 4096);
99 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000100 }
101};
bsalomon5baedd62015-03-09 12:15:53 -0700102}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000103
bsalomon5baedd62015-03-09 12:15:53 -0700104int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000105
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000106///////////////////////////////////////////////////////////////////////////////
107
joshualittb0a8a372014-09-23 09:50:21 -0700108GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000109
joshualittb0a8a372014-09-23 09:50:21 -0700110void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000111 fTextureAccesses.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700112 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000113}
114
cdalton74b8d322016-04-11 14:47:28 -0700115void GrProcessor::addBufferAccess(const GrBufferAccess* access) {
116 fBufferAccesses.push_back(access);
117 this->addGpuResource(access->getProgramBuffer());
118}
119
joshualittb0a8a372014-09-23 09:50:21 -0700120void* GrProcessor::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -0700121 return MemoryPoolAccessor().pool()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000122}
123
joshualittb0a8a372014-09-23 09:50:21 -0700124void GrProcessor::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -0700125 return MemoryPoolAccessor().pool()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000126}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000127
cdalton74b8d322016-04-11 14:47:28 -0700128bool GrProcessor::hasSameSamplers(const GrProcessor& that) const {
129 if (this->numTextures() != that.numTextures() || this->numBuffers() != that.numBuffers()) {
bsalomon420d7e92014-10-16 09:18:09 -0700130 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000131 }
bsalomon420d7e92014-10-16 09:18:09 -0700132 for (int i = 0; i < this->numTextures(); ++i) {
133 if (this->textureAccess(i) != that.textureAccess(i)) {
134 return false;
135 }
136 }
cdalton74b8d322016-04-11 14:47:28 -0700137 for (int i = 0; i < this->numBuffers(); ++i) {
138 if (this->bufferAccess(i) != that.bufferAccess(i)) {
139 return false;
140 }
141 }
bsalomon420d7e92014-10-16 09:18:09 -0700142 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000143}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700144
joshualitta5305a12014-10-10 17:47:00 -0700145///////////////////////////////////////////////////////////////////////////////////////////////////
146
egdaniel915187b2014-12-05 12:58:28 -0800147// Initial static variable from GrXPFactory
148int32_t GrXPFactory::gCurrXPFClassID =
149 GrXPFactory::kIllegalXPFClassID;