tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 8 | #include "GrProcessor.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 10 | #include "GrCoordTransform.h" |
joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 11 | #include "GrGeometryData.h" |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame^] | 12 | #include "GrGeometryProcessor.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 13 | #include "GrInvariantOutput.h" |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 14 | #include "GrMemoryPool.h" |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 15 | #include "GrXferProcessor.h" |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 16 | #include "SkTLS.h" |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 17 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 18 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 19 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 20 | class GrFragmentProcessor; |
| 21 | class GrGeometryProcessor; |
| 22 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 23 | /* |
| 24 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 25 | * problems on android. |
| 26 | */ |
| 27 | template<> |
| 28 | SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true>* |
| 29 | GrProcessorTestFactory<GrFragmentProcessor>::GetFactories() { |
| 30 | static SkTArray<GrProcessorTestFactory<GrFragmentProcessor>*, true> gFactories; |
| 31 | return &gFactories; |
| 32 | } |
| 33 | |
| 34 | template<> |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 35 | SkTArray<GrProcessorTestFactory<GrXferProcessor>*, true>* |
| 36 | GrProcessorTestFactory<GrXferProcessor>::GetFactories() { |
| 37 | static SkTArray<GrProcessorTestFactory<GrXferProcessor>*, true> gFactories; |
| 38 | return &gFactories; |
| 39 | } |
| 40 | |
| 41 | template<> |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 42 | SkTArray<GrProcessorTestFactory<GrGeometryProcessor>*, true>* |
| 43 | GrProcessorTestFactory<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 | */ |
| 53 | static const int kFPFactoryCount = 37; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 54 | static const int kGPFactoryCount = 14; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 55 | static const int kXPFactoryCount = 0; |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 56 | |
| 57 | template<> |
| 58 | void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() { |
| 59 | if (kFPFactoryCount != GetFactories()->count()) { |
| 60 | SkFAIL("Wrong number of fragment processor factories!"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | template<> |
| 65 | void GrProcessorTestFactory<GrGeometryProcessor>::VerifyFactoryCount() { |
| 66 | if (kGPFactoryCount != GetFactories()->count()) { |
| 67 | SkFAIL("Wrong number of geometry processor factories!"); |
| 68 | } |
| 69 | } |
| 70 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 71 | template<> |
| 72 | void GrProcessorTestFactory<GrXferProcessor>::VerifyFactoryCount() { |
| 73 | if (kXPFactoryCount != GetFactories()->count()) { |
| 74 | SkFAIL("Wrong number of xfer processor factories!"); |
| 75 | } |
| 76 | } |
| 77 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 78 | #endif |
| 79 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | namespace GrProcessorUnitTest { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 81 | const SkMatrix& TestMatrix(SkRandom* random) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 82 | static SkMatrix gMatrices[5]; |
| 83 | static bool gOnce; |
| 84 | if (!gOnce) { |
| 85 | gMatrices[0].reset(); |
| 86 | gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100)); |
| 87 | gMatrices[2].setRotate(SkIntToScalar(17)); |
| 88 | gMatrices[3].setRotate(SkIntToScalar(185)); |
| 89 | gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33)); |
| 90 | gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf); |
| 91 | gMatrices[4].setRotate(SkIntToScalar(215)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 92 | gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f); |
| 93 | gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 94 | gOnce = true; |
| 95 | } |
| 96 | return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))]; |
| 97 | } |
| 98 | } |
| 99 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 100 | class GrProcessor_Globals { |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 101 | public: |
| 102 | static GrMemoryPool* GetTLS() { |
| 103 | return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | static void* CreateTLS() { |
| 108 | return SkNEW_ARGS(GrMemoryPool, (4096, 4096)); |
| 109 | } |
| 110 | |
| 111 | static void DeleteTLS(void* pool) { |
| 112 | SkDELETE(reinterpret_cast<GrMemoryPool*>(pool)); |
| 113 | } |
| 114 | }; |
| 115 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 116 | int32_t GrProcessor::gCurrProcessorClassID = |
| 117 | GrProcessor::kIllegalProcessorClassID; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 118 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 119 | /////////////////////////////////////////////////////////////////////////////// |
| 120 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 121 | GrProcessor::~GrProcessor() {} |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 122 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 123 | void GrProcessor::addTextureAccess(const GrTextureAccess* access) { |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 124 | fTextureAccesses.push_back(access); |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 125 | this->addGpuResource(access->getProgramTexture()); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 126 | } |
| 127 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 128 | void* GrProcessor::operator new(size_t size) { |
| 129 | return GrProcessor_Globals::GetTLS()->allocate(size); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 130 | } |
| 131 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 132 | void GrProcessor::operator delete(void* target) { |
| 133 | GrProcessor_Globals::GetTLS()->release(target); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 134 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 135 | |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 136 | bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const { |
| 137 | if (this->numTextures() != that.numTextures()) { |
| 138 | return false; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 139 | } |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 140 | for (int i = 0; i < this->numTextures(); ++i) { |
| 141 | if (this->textureAccess(i) != that.textureAccess(i)) { |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | return true; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 146 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 147 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 148 | void GrProcessor::computeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 149 | this->onComputeInvariantOutput(inout); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 150 | } |
| 151 | |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { |
| 155 | fCoordTransforms.push_back(transform); |
bsalomon | f276541 | 2014-10-15 18:34:46 -0700 | [diff] [blame] | 156 | SkDEBUGCODE(transform->setInProcessor();) |
joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame] | 157 | } |
bsalomon | de258cd | 2014-10-15 19:06:21 -0700 | [diff] [blame] | 158 | |
| 159 | bool GrFragmentProcessor::hasSameTransforms(const GrFragmentProcessor& that) const { |
| 160 | if (fCoordTransforms.count() != that.fCoordTransforms.count()) { |
| 161 | return false; |
| 162 | } |
| 163 | int count = fCoordTransforms.count(); |
| 164 | for (int i = 0; i < count; ++i) { |
| 165 | if (*fCoordTransforms[i] != *that.fCoordTransforms[i]) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | return true; |
| 170 | } |
joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 171 | |
| 172 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 173 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame^] | 174 | void GrGeometryProcessor::computeInvariantColor(GrInvariantOutput* intout) const { |
| 175 | |
| 176 | } |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 179 | |
joshualitt | c07379d | 2014-11-20 14:50:39 -0800 | [diff] [blame] | 180 | /* |
| 181 | * GrGeometryData shares the same pool so it lives in this file too |
| 182 | */ |
| 183 | void* GrGeometryData::operator new(size_t size) { |
| 184 | return GrProcessor_Globals::GetTLS()->allocate(size); |
| 185 | } |
| 186 | |
| 187 | void GrGeometryData::operator delete(void* target) { |
| 188 | GrProcessor_Globals::GetTLS()->release(target); |
| 189 | } |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 190 | |
| 191 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 192 | |
| 193 | // Initial static variable from GrXPFactory |
| 194 | int32_t GrXPFactory::gCurrXPFClassID = |
| 195 | GrXPFactory::kIllegalXPFClassID; |
| 196 | |