blob: f952483562766fdb3e5d7f1fe1eb578cd794432a [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
bsalomon97b9ab72014-07-08 06:52:35 -070062GrEffect::~GrEffect() {}
tomhudson@google.com168e6342012-04-18 17:49:20 +000063
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000064const char* GrEffect::name() const {
65 return this->getFactory().name();
66}
67
bsalomon@google.com77af6802013-10-02 13:04:56 +000068void GrEffect::addCoordTransform(const GrCoordTransform* transform) {
69 fCoordTransforms.push_back(transform);
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000070 SkDEBUGCODE(transform->setInEffect();)
bsalomon@google.com77af6802013-10-02 13:04:56 +000071}
72
bsalomon@google.com50db75c2013-01-11 13:54:30 +000073void GrEffect::addTextureAccess(const GrTextureAccess* access) {
74 fTextureAccesses.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +000075}
76
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000077void* GrEffect::operator new(size_t size) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000078 return GrEffect_Globals::GetTLS()->allocate(size);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000079}
80
bsalomon@google.coma469c282012-10-24 18:28:34 +000081void GrEffect::operator delete(void* target) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000082 GrEffect_Globals::GetTLS()->release(target);
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000083}
bsalomon@google.com77af6802013-10-02 13:04:56 +000084
85#ifdef SK_DEBUG
86void GrEffect::assertEquality(const GrEffect& other) const {
87 SkASSERT(this->numTransforms() == other.numTransforms());
88 for (int i = 0; i < this->numTransforms(); ++i) {
89 SkASSERT(this->coordTransform(i) == other.coordTransform(i));
90 }
91 SkASSERT(this->numTextures() == other.numTextures());
92 for (int i = 0; i < this->numTextures(); ++i) {
93 SkASSERT(this->textureAccess(i) == other.textureAccess(i));
94 }
95}
96#endif