blob: 653a1da33cb4c2226cf340e7d2e780871d18de39 [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.coma469c282012-10-24 18:28:34 +000015SK_DEFINE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000016
bsalomon@google.comd4726202012-08-03 14:34:46 +000017#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
bsalomon@google.coma469c282012-10-24 18:28:34 +000018SkTArray<GrEffectTestFactory*, true>* GrEffectTestFactory::GetFactories() {
19 static SkTArray<GrEffectTestFactory*, true> gFactories;
bsalomon@google.comd4726202012-08-03 14:34:46 +000020 return &gFactories;
21}
22#endif
23
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000024namespace GrEffectUnitTest {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000025const SkMatrix& TestMatrix(SkRandom* random) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000026 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.com8ea78d82012-10-24 20:11:30 +000044class GrEffect_Globals {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000045public:
46 static GrMemoryPool* GetTLS() {
47 return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
48 }
49
50private:
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.com396e61f2012-10-25 19:00:29 +000060int32_t GrBackendEffectFactory::fCurrEffectClassID = GrBackendEffectFactory::kIllegalEffectClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +000061
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000062///////////////////////////////////////////////////////////////////////////////
63
64SK_DEFINE_INST_COUNT(GrEffectRef)
65
66GrEffectRef::~GrEffectRef() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000067 SkASSERT(this->unique());
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +000068 fEffect->EffectRefDestroyed();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000069 fEffect->unref();
70}
71
72void* GrEffectRef::operator new(size_t size) {
73 return GrEffect_Globals::GetTLS()->allocate(size);
74}
75
76void GrEffectRef::operator delete(void* target) {
77 GrEffect_Globals::GetTLS()->release(target);
78}
79
80///////////////////////////////////////////////////////////////////////////////
81
bsalomon@google.coma469c282012-10-24 18:28:34 +000082GrEffect::~GrEffect() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000083 SkASSERT(NULL == fEffectRef);
tomhudson@google.com168e6342012-04-18 17:49:20 +000084}
85
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000086const char* GrEffect::name() const {
87 return this->getFactory().name();
88}
89
bsalomon@google.com77af6802013-10-02 13:04:56 +000090void GrEffect::addCoordTransform(const GrCoordTransform* transform) {
91 fCoordTransforms.push_back(transform);
92}
93
bsalomon@google.com50db75c2013-01-11 13:54:30 +000094void GrEffect::addTextureAccess(const GrTextureAccess* access) {
95 fTextureAccesses.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +000096}
97
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000098void* GrEffect::operator new(size_t size) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000099 return GrEffect_Globals::GetTLS()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000100}
101
bsalomon@google.coma469c282012-10-24 18:28:34 +0000102void GrEffect::operator delete(void* target) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000103 GrEffect_Globals::GetTLS()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000104}
bsalomon@google.com77af6802013-10-02 13:04:56 +0000105
106#ifdef SK_DEBUG
107void GrEffect::assertEquality(const GrEffect& other) const {
108 SkASSERT(this->numTransforms() == other.numTransforms());
109 for (int i = 0; i < this->numTransforms(); ++i) {
110 SkASSERT(this->coordTransform(i) == other.coordTransform(i));
111 }
112 SkASSERT(this->numTextures() == other.numTextures());
113 for (int i = 0; i < this->numTextures(); ++i) {
114 SkASSERT(this->textureAccess(i) == other.textureAccess(i));
115 }
116}
117#endif