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