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