michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrEffect.h" michael@0: #include "GrBackendEffectFactory.h" michael@0: #include "GrContext.h" michael@0: #include "GrCoordTransform.h" michael@0: #include "GrMemoryPool.h" michael@0: #include "SkTLS.h" michael@0: michael@0: #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS michael@0: SkTArray* GrEffectTestFactory::GetFactories() { michael@0: static SkTArray gFactories; michael@0: return &gFactories; michael@0: } michael@0: #endif michael@0: michael@0: namespace GrEffectUnitTest { michael@0: const SkMatrix& TestMatrix(SkRandom* random) { michael@0: static SkMatrix gMatrices[5]; michael@0: static bool gOnce; michael@0: if (!gOnce) { michael@0: gMatrices[0].reset(); michael@0: gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100)); michael@0: gMatrices[2].setRotate(SkIntToScalar(17)); michael@0: gMatrices[3].setRotate(SkIntToScalar(185)); michael@0: gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33)); michael@0: gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf); michael@0: gMatrices[4].setRotate(SkIntToScalar(215)); michael@0: gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f); michael@0: gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f); michael@0: gOnce = true; michael@0: } michael@0: return gMatrices[random->nextULessThan(static_cast(SK_ARRAY_COUNT(gMatrices)))]; michael@0: } michael@0: } michael@0: michael@0: class GrEffect_Globals { michael@0: public: michael@0: static GrMemoryPool* GetTLS() { michael@0: return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS); michael@0: } michael@0: michael@0: private: michael@0: static void* CreateTLS() { michael@0: return SkNEW_ARGS(GrMemoryPool, (4096, 4096)); michael@0: } michael@0: michael@0: static void DeleteTLS(void* pool) { michael@0: SkDELETE(reinterpret_cast(pool)); michael@0: } michael@0: }; michael@0: michael@0: int32_t GrBackendEffectFactory::fCurrEffectClassID = GrBackendEffectFactory::kIllegalEffectClassID; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: GrEffectRef::~GrEffectRef() { michael@0: SkASSERT(this->unique()); michael@0: fEffect->EffectRefDestroyed(); michael@0: fEffect->unref(); michael@0: } michael@0: michael@0: void* GrEffectRef::operator new(size_t size) { michael@0: return GrEffect_Globals::GetTLS()->allocate(size); michael@0: } michael@0: michael@0: void GrEffectRef::operator delete(void* target) { michael@0: GrEffect_Globals::GetTLS()->release(target); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: GrEffect::~GrEffect() { michael@0: SkASSERT(NULL == fEffectRef); michael@0: } michael@0: michael@0: const char* GrEffect::name() const { michael@0: return this->getFactory().name(); michael@0: } michael@0: michael@0: void GrEffect::addCoordTransform(const GrCoordTransform* transform) { michael@0: fCoordTransforms.push_back(transform); michael@0: SkDEBUGCODE(transform->setInEffect();) michael@0: } michael@0: michael@0: void GrEffect::addTextureAccess(const GrTextureAccess* access) { michael@0: fTextureAccesses.push_back(access); michael@0: } michael@0: michael@0: void* GrEffect::operator new(size_t size) { michael@0: return GrEffect_Globals::GetTLS()->allocate(size); michael@0: } michael@0: michael@0: void GrEffect::operator delete(void* target) { michael@0: GrEffect_Globals::GetTLS()->release(target); michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void GrEffect::assertEquality(const GrEffect& other) const { michael@0: SkASSERT(this->numTransforms() == other.numTransforms()); michael@0: for (int i = 0; i < this->numTransforms(); ++i) { michael@0: SkASSERT(this->coordTransform(i) == other.coordTransform(i)); michael@0: } michael@0: SkASSERT(this->numTextures() == other.numTextures()); michael@0: for (int i = 0; i < this->numTextures(); ++i) { michael@0: SkASSERT(this->textureAccess(i) == other.textureAccess(i)); michael@0: } michael@0: } michael@0: #endif