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