blob: f14c171286ae50070c0ef1e0646577781a123b91 [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"
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000011#include "GrMemoryPool.h"
12#include "SkTLS.h"
tomhudson@google.com168e6342012-04-18 17:49:20 +000013
bsalomon@google.coma469c282012-10-24 18:28:34 +000014SK_DEFINE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000015
bsalomon@google.comd4726202012-08-03 14:34:46 +000016#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
bsalomon@google.coma469c282012-10-24 18:28:34 +000017SkTArray<GrEffectTestFactory*, true>* GrEffectTestFactory::GetFactories() {
18 static SkTArray<GrEffectTestFactory*, true> gFactories;
bsalomon@google.comd4726202012-08-03 14:34:46 +000019 return &gFactories;
20}
21#endif
22
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000023namespace GrEffectUnitTest {
bsalomon@google.com73a96942013-02-13 16:31:19 +000024const SkMatrix& TestMatrix(SkMWCRandom* random) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000025 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.com8ea78d82012-10-24 20:11:30 +000043class GrEffect_Globals {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000044public:
45 static GrMemoryPool* GetTLS() {
46 return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
47 }
48
49private:
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.com396e61f2012-10-25 19:00:29 +000059int32_t GrBackendEffectFactory::fCurrEffectClassID = GrBackendEffectFactory::kIllegalEffectClassID;
tomhudson@google.com168e6342012-04-18 17:49:20 +000060
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000061///////////////////////////////////////////////////////////////////////////////
62
63SK_DEFINE_INST_COUNT(GrEffectRef)
64
65GrEffectRef::~GrEffectRef() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000066 SkASSERT(this->unique());
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +000067 fEffect->EffectRefDestroyed();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000068 fEffect->unref();
69}
70
71void* GrEffectRef::operator new(size_t size) {
72 return GrEffect_Globals::GetTLS()->allocate(size);
73}
74
75void GrEffectRef::operator delete(void* target) {
76 GrEffect_Globals::GetTLS()->release(target);
77}
78
79///////////////////////////////////////////////////////////////////////////////
80
bsalomon@google.coma469c282012-10-24 18:28:34 +000081GrEffect::~GrEffect() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000082 SkASSERT(NULL == fEffectRef);
tomhudson@google.com168e6342012-04-18 17:49:20 +000083}
84
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000085const char* GrEffect::name() const {
86 return this->getFactory().name();
87}
88
bsalomon@google.com50db75c2013-01-11 13:54:30 +000089void GrEffect::addTextureAccess(const GrTextureAccess* access) {
90 fTextureAccesses.push_back(access);
twiz@google.coma5e65ec2012-08-02 15:15:16 +000091}
92
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000093void GrEffect::addVertexAttrib(GrSLType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000094 SkASSERT(fVertexAttribTypes.count() < kMaxVertexAttribs);
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000095 fVertexAttribTypes.push_back(type);
96}
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}