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