blob: 986e80a57ee0196d04354e4a4bbfb56857c1e70e [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
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.coma469c282012-10-24 18:28:34 +00008#include "GrEffect.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +00009#include "GrBackendEffectFactory.h"
10#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000011#include "GrCoordTransform.h"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000012#include "GrMemoryPool.h"
13#include "SkTLS.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000014
bsalomon@google.comd4726202012-08-03 14:34:46 +000015#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
bsalomon@google.coma469c282012-10-24 18:28:34 +000016SkTArray<GrEffectTestFactory*, true>* GrEffectTestFactory::GetFactories() {
17 static SkTArray<GrEffectTestFactory*, true> gFactories;
bsalomon@google.comd4726202012-08-03 14:34:46 +000018 return &gFactories;
19}
20#endif
21
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000022namespace GrEffectUnitTest {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000023const SkMatrix& TestMatrix(SkRandom* random) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000024 static SkMatrix gMatrices[5];
25 static bool gOnce;
26 if (!gOnce) {
27 gMatrices[0].reset();
28 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
29 gMatrices[2].setRotate(SkIntToScalar(17));
30 gMatrices[3].setRotate(SkIntToScalar(185));
31 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
32 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
33 gMatrices[4].setRotate(SkIntToScalar(215));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000034 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
35 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000036 gOnce = true;
37 }
38 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
39}
40}
41
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000042class GrEffect_Globals {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000043public:
44 static GrMemoryPool* GetTLS() {
45 return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
46 }
47
48private:
49 static void* CreateTLS() {
50 return SkNEW_ARGS(GrMemoryPool, (4096, 4096));
51 }
52
53 static void DeleteTLS(void* pool) {
54 SkDELETE(reinterpret_cast<GrMemoryPool*>(pool));
55 }
56};
57
bsalomon@google.com396e61f2012-10-25 19:00:29 +000058int32_t GrBackendEffectFactory::fCurrEffectClassID = GrBackendEffectFactory::kIllegalEffectClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +000059
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000060///////////////////////////////////////////////////////////////////////////////
61
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000062GrEffectRef::~GrEffectRef() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000063 SkASSERT(this->unique());
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +000064 fEffect->EffectRefDestroyed();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000065 fEffect->unref();
66}
67
68void* GrEffectRef::operator new(size_t size) {
69 return GrEffect_Globals::GetTLS()->allocate(size);
70}
71
72void GrEffectRef::operator delete(void* target) {
73 GrEffect_Globals::GetTLS()->release(target);
74}
75
76///////////////////////////////////////////////////////////////////////////////
77
bsalomon@google.coma469c282012-10-24 18:28:34 +000078GrEffect::~GrEffect() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000079 SkASSERT(NULL == fEffectRef);
tomhudson@google.com168e6342012-04-18 17:49:20 +000080}
81
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000082const char* GrEffect::name() const {
83 return this->getFactory().name();
84}
85
bsalomon@google.com77af6802013-10-02 13:04:56 +000086void GrEffect::addCoordTransform(const GrCoordTransform* transform) {
87 fCoordTransforms.push_back(transform);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000088 SkDEBUGCODE(transform->setInEffect();)
bsalomon@google.com77af6802013-10-02 13:04:56 +000089}
90
bsalomon@google.com50db75c2013-01-11 13:54:30 +000091void GrEffect::addTextureAccess(const GrTextureAccess* access) {
92 fTextureAccesses.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +000093}
94
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000095void* GrEffect::operator new(size_t size) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000096 return GrEffect_Globals::GetTLS()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000097}
98
bsalomon@google.coma469c282012-10-24 18:28:34 +000099void GrEffect::operator delete(void* target) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000100 GrEffect_Globals::GetTLS()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000101}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000102
103#ifdef SK_DEBUG
104void GrEffect::assertEquality(const GrEffect& other) const {
105 SkASSERT(this->numTransforms() == other.numTransforms());
106 for (int i = 0; i < this->numTransforms(); ++i) {
107 SkASSERT(this->coordTransform(i) == other.coordTransform(i));
108 }
109 SkASSERT(this->numTextures() == other.numTextures());
110 for (int i = 0; i < this->numTextures(); ++i) {
111 SkASSERT(this->textureAccess(i) == other.textureAccess(i));
112 }
113}
114#endif