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 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 8 | #include "GrEffect.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 9 | #include "GrBackendEffectFactory.h" |
| 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 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 15 | SK_DEFINE_INST_COUNT(GrEffect) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 17 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 18 | SkTArray<GrEffectTestFactory*, true>* GrEffectTestFactory::GetFactories() { |
| 19 | static SkTArray<GrEffectTestFactory*, true> gFactories; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 20 | return &gFactories; |
| 21 | } |
| 22 | #endif |
| 23 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 24 | namespace GrEffectUnitTest { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 25 | const SkMatrix& TestMatrix(SkRandom* random) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 26 | static SkMatrix gMatrices[5]; |
| 27 | static bool gOnce; |
| 28 | if (!gOnce) { |
| 29 | gMatrices[0].reset(); |
| 30 | gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100)); |
| 31 | gMatrices[2].setRotate(SkIntToScalar(17)); |
| 32 | gMatrices[3].setRotate(SkIntToScalar(185)); |
| 33 | gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33)); |
| 34 | gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf); |
| 35 | gMatrices[4].setRotate(SkIntToScalar(215)); |
| 36 | gMatrices[4].set(SkMatrix::kMPersp0, SkFloatToScalar(0.00013f)); |
| 37 | gMatrices[4].set(SkMatrix::kMPersp1, SkFloatToScalar(-0.000039f)); |
| 38 | gOnce = true; |
| 39 | } |
| 40 | return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))]; |
| 41 | } |
| 42 | } |
| 43 | |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 44 | class GrEffect_Globals { |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 45 | public: |
| 46 | static GrMemoryPool* GetTLS() { |
| 47 | return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | static void* CreateTLS() { |
| 52 | return SkNEW_ARGS(GrMemoryPool, (4096, 4096)); |
| 53 | } |
| 54 | |
| 55 | static void DeleteTLS(void* pool) { |
| 56 | SkDELETE(reinterpret_cast<GrMemoryPool*>(pool)); |
| 57 | } |
| 58 | }; |
| 59 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 60 | int32_t GrBackendEffectFactory::fCurrEffectClassID = GrBackendEffectFactory::kIllegalEffectClassID; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | SK_DEFINE_INST_COUNT(GrEffectRef) |
| 65 | |
| 66 | GrEffectRef::~GrEffectRef() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 67 | SkASSERT(this->unique()); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 68 | fEffect->EffectRefDestroyed(); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 69 | fEffect->unref(); |
| 70 | } |
| 71 | |
| 72 | void* GrEffectRef::operator new(size_t size) { |
| 73 | return GrEffect_Globals::GetTLS()->allocate(size); |
| 74 | } |
| 75 | |
| 76 | void GrEffectRef::operator delete(void* target) { |
| 77 | GrEffect_Globals::GetTLS()->release(target); |
| 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 82 | GrEffect::~GrEffect() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 83 | SkASSERT(NULL == fEffectRef); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 84 | } |
| 85 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 86 | const char* GrEffect::name() const { |
| 87 | return this->getFactory().name(); |
| 88 | } |
| 89 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 90 | void GrEffect::addCoordTransform(const GrCoordTransform* transform) { |
| 91 | fCoordTransforms.push_back(transform); |
commit-bot@chromium.org | 5fd7d5c | 2013-10-04 01:20:09 +0000 | [diff] [blame] | 92 | SkDEBUGCODE(transform->setInEffect();) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 93 | } |
| 94 | |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 95 | void GrEffect::addTextureAccess(const GrTextureAccess* access) { |
| 96 | fTextureAccesses.push_back(access); |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 99 | void* GrEffect::operator new(size_t size) { |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 100 | return GrEffect_Globals::GetTLS()->allocate(size); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 101 | } |
| 102 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 103 | void GrEffect::operator delete(void* target) { |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 104 | GrEffect_Globals::GetTLS()->release(target); |
tomhudson@google.com | dcba4c2 | 2012-07-24 21:36:16 +0000 | [diff] [blame] | 105 | } |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 106 | |
| 107 | #ifdef SK_DEBUG |
| 108 | void GrEffect::assertEquality(const GrEffect& other) const { |
| 109 | SkASSERT(this->numTransforms() == other.numTransforms()); |
| 110 | for (int i = 0; i < this->numTransforms(); ++i) { |
| 111 | SkASSERT(this->coordTransform(i) == other.coordTransform(i)); |
| 112 | } |
| 113 | SkASSERT(this->numTextures() == other.numTextures()); |
| 114 | for (int i = 0; i < this->numTextures(); ++i) { |
| 115 | SkASSERT(this->textureAccess(i) == other.textureAccess(i)); |
| 116 | } |
| 117 | } |
| 118 | #endif |