| 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::addTextureAccess(const GrTextureAccess* access) { | 
| bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 107 | fTextureAccesses.push_back(access); | 
| bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 108 | this->addGpuResource(access->getProgramTexture()); | 
| twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 111 | void* GrProcessor::operator new(size_t size) { | 
|  | 112 | return GrProcessor_Globals::GetTLS()->allocate(size); | 
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 115 | void GrProcessor::operator delete(void* target) { | 
|  | 116 | GrProcessor_Globals::GetTLS()->release(target); | 
| tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 117 | } | 
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 118 |  | 
|  | 119 | #ifdef SK_DEBUG | 
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 120 | void GrProcessor::assertEquality(const GrProcessor& other) const { | 
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 121 | SkASSERT(this->numTextures() == other.numTextures()); | 
|  | 122 | for (int i = 0; i < this->numTextures(); ++i) { | 
|  | 123 | SkASSERT(this->textureAccess(i) == other.textureAccess(i)); | 
|  | 124 | } | 
|  | 125 | } | 
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | void GrProcessor::InvariantOutput::validate() const { | 
|  | 128 | if (fIsSingleComponent) { | 
|  | 129 | SkASSERT(0 == fValidFlags || kRGBA_GrColorComponentFlags == fValidFlags); | 
|  | 130 | if (kRGBA_GrColorComponentFlags == fValidFlags) { | 
|  | 131 | SkASSERT(this->colorComponentsAllEqual()); | 
|  | 132 | } | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | SkASSERT(this->validPreMulColor()); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | bool GrProcessor::InvariantOutput::colorComponentsAllEqual() const { | 
|  | 139 | unsigned colorA = GrColorUnpackA(fColor); | 
|  | 140 | return(GrColorUnpackR(fColor) == colorA && | 
|  | 141 | GrColorUnpackG(fColor) == colorA && | 
|  | 142 | GrColorUnpackB(fColor) == colorA); | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | bool GrProcessor::InvariantOutput::validPreMulColor() const { | 
|  | 146 | if (kA_GrColorComponentFlag & fValidFlags) { | 
|  | 147 | float c[4]; | 
|  | 148 | GrColorToRGBAFloat(fColor, c); | 
|  | 149 | if (kR_GrColorComponentFlag & fValidFlags) { | 
|  | 150 | if (c[0] > c[3]) { | 
|  | 151 | return false; | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 | if (kG_GrColorComponentFlag & fValidFlags) { | 
|  | 155 | if (c[1] > c[3]) { | 
|  | 156 | return false; | 
|  | 157 | } | 
|  | 158 | } | 
|  | 159 | if (kB_GrColorComponentFlag & fValidFlags) { | 
|  | 160 | if (c[2] > c[3]) { | 
|  | 161 | return false; | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 | return true; | 
|  | 166 | } | 
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 167 | #endif // end DEBUG | 
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 168 |  | 
| joshualitt | a5305a1 | 2014-10-10 17:47:00 -0700 | [diff] [blame^] | 169 | /////////////////////////////////////////////////////////////////////////////////////////////////// | 
|  | 170 |  | 
|  | 171 | void GrFragmentProcessor::addCoordTransform(const GrCoordTransform* transform) { | 
|  | 172 | fCoordTransforms.push_back(transform); | 
|  | 173 | SkDEBUGCODE(transform->setInEffect();) | 
|  | 174 | } |