blob: 0d3f8d916684d8c3766795ebd8e313d3020cd044 [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 */
reed59dc5422016-04-05 06:25:14 -070051static const int kFPFactoryCount = 39;
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:
joshualitt23ac62c2015-03-30 09:53:47 -070087 MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000088
joshualitt23ac62c2015-03-30 09:53:47 -070089 ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000090
bsalomon5baedd62015-03-09 12:15:53 -070091 GrMemoryPool* pool() const {
92 static GrMemoryPool gPool(4096, 4096);
93 return &gPool;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000094 }
95};
bsalomon5baedd62015-03-09 12:15:53 -070096}
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000097
bsalomon5baedd62015-03-09 12:15:53 -070098int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +000099
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000100///////////////////////////////////////////////////////////////////////////////
101
joshualittb0a8a372014-09-23 09:50:21 -0700102GrProcessor::~GrProcessor() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000103
joshualittb0a8a372014-09-23 09:50:21 -0700104void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000105 fTextureAccesses.push_back(access);
bsalomonf96ba022014-09-17 08:05:40 -0700106 this->addGpuResource(access->getProgramTexture());
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000107}
108
cdalton74b8d322016-04-11 14:47:28 -0700109void GrProcessor::addBufferAccess(const GrBufferAccess* access) {
110 fBufferAccesses.push_back(access);
111 this->addGpuResource(access->getProgramBuffer());
112}
113
joshualittb0a8a372014-09-23 09:50:21 -0700114void* GrProcessor::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -0700115 return MemoryPoolAccessor().pool()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000116}
117
joshualittb0a8a372014-09-23 09:50:21 -0700118void GrProcessor::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -0700119 return MemoryPoolAccessor().pool()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000120}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000121
cdalton74b8d322016-04-11 14:47:28 -0700122bool GrProcessor::hasSameSamplers(const GrProcessor& that) const {
123 if (this->numTextures() != that.numTextures() || this->numBuffers() != that.numBuffers()) {
bsalomon420d7e92014-10-16 09:18:09 -0700124 return false;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000125 }
bsalomon420d7e92014-10-16 09:18:09 -0700126 for (int i = 0; i < this->numTextures(); ++i) {
127 if (this->textureAccess(i) != that.textureAccess(i)) {
128 return false;
129 }
130 }
cdalton74b8d322016-04-11 14:47:28 -0700131 for (int i = 0; i < this->numBuffers(); ++i) {
132 if (this->bufferAccess(i) != that.bufferAccess(i)) {
133 return false;
134 }
135 }
bsalomon420d7e92014-10-16 09:18:09 -0700136 return true;
bsalomon@google.com77af6802013-10-02 13:04:56 +0000137}
egdaniel1a8ecdf2014-10-03 06:24:12 -0700138
joshualitta5305a12014-10-10 17:47:00 -0700139///////////////////////////////////////////////////////////////////////////////////////////////////
140
egdaniel915187b2014-12-05 12:58:28 -0800141// Initial static variable from GrXPFactory
142int32_t GrXPFactory::gCurrXPFClassID =
143 GrXPFactory::kIllegalXPFClassID;