blob: b4efe91004845cb3b00ebfc8b6d3d1131374fce7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkFlattenable.h"
mike@reedtribe.org227b5162012-08-12 19:25:08 +00009#include "SkPtrRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000011SK_DEFINE_INST_COUNT(SkFlattenable)
12
reed@google.comf2eb5ab2011-05-10 22:56:42 +000013///////////////////////////////////////////////////////////////////////////////
14
djsollen@google.com54924242012-03-29 15:18:04 +000015void SkFlattenable::flatten(SkFlattenableWriteBuffer&) const
reed@android.com8a1c16f2008-12-17 15:59:43 +000016{
17 /* we don't write anything at the moment, but this allows our subclasses
18 to not know that, since we want them to always call INHERITED::flatten()
19 in their code.
20 */
21}
22
23///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000025SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {}
26
27uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) {
28 uint32_t index = fFactorySet.find(factory);
29 if (index > 0) {
30 return index;
31 }
32 const char* name = SkFlattenable::FactoryToName(factory);
33 if (NULL == name) {
34 return 0;
35 }
36 *fNames.append() = name;
37 return fFactorySet.add(factory);
38}
39
40const char* SkNamedFactorySet::getNextAddedFactoryName() {
41 if (fNextAddedFactory < fNames.count()) {
42 return fNames[fNextAddedFactory++];
43 }
44 return NULL;
45}
46
47///////////////////////////////////////////////////////////////////////////////
48
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000049SkRefCntSet::~SkRefCntSet() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 // call this now, while our decPtr() is sill in scope
51 this->reset();
52}
53
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000054void SkRefCntSet::incPtr(void* ptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 ((SkRefCnt*)ptr)->ref();
56}
57
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000058void SkRefCntSet::decPtr(void* ptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 ((SkRefCnt*)ptr)->unref();
60}
61
62///////////////////////////////////////////////////////////////////////////////
63///////////////////////////////////////////////////////////////////////////////
64///////////////////////////////////////////////////////////////////////////////
65
bungeman@google.com488379e2012-06-27 15:57:34 +000066#define MAX_PAIR_COUNT 1024
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
68struct Pair {
69 const char* fName;
70 SkFlattenable::Factory fFactory;
71};
72
73static int gCount;
74static Pair gPairs[MAX_PAIR_COUNT];
75
76void SkFlattenable::Register(const char name[], Factory factory) {
77 SkASSERT(name);
78 SkASSERT(factory);
reed@google.com82065d62011-02-07 15:30:46 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 static bool gOnce;
81 if (!gOnce) {
82 gCount = 0;
83 gOnce = true;
84 }
reed@google.com82065d62011-02-07 15:30:46 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkASSERT(gCount < MAX_PAIR_COUNT);
reed@google.com82065d62011-02-07 15:30:46 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 gPairs[gCount].fName = name;
89 gPairs[gCount].fFactory = factory;
90 gCount += 1;
91}
92
scroggo@google.coma0c2bc22012-09-21 17:54:46 +000093#ifdef SK_DEBUG
caryclark@google.comd26147a2011-12-15 14:16:43 +000094static void report_no_entries(const char* functionName) {
95 if (!gCount) {
96 SkDebugf("%s has no registered name/factory pairs."
caryclark@google.com92454982011-12-16 16:54:07 +000097 " Call SkGraphics::Init() at process initialization time.",
98 functionName);
caryclark@google.comd26147a2011-12-15 14:16:43 +000099 }
100}
101#endif
102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) {
scroggo@google.coma0c2bc22012-09-21 17:54:46 +0000104#ifdef SK_DEBUG
caryclark@google.comd26147a2011-12-15 14:16:43 +0000105 report_no_entries(__FUNCTION__);
106#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 const Pair* pairs = gPairs;
108 for (int i = gCount - 1; i >= 0; --i) {
109 if (strcmp(pairs[i].fName, name) == 0) {
110 return pairs[i].fFactory;
111 }
112 }
113 return NULL;
114}
115
116const char* SkFlattenable::FactoryToName(Factory fact) {
scroggo@google.coma0c2bc22012-09-21 17:54:46 +0000117#ifdef SK_DEBUG
caryclark@google.comd26147a2011-12-15 14:16:43 +0000118 report_no_entries(__FUNCTION__);
119#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 const Pair* pairs = gPairs;
121 for (int i = gCount - 1; i >= 0; --i) {
122 if (pairs[i].fFactory == fact) {
123 return pairs[i].fName;
124 }
125 }
126 return NULL;
127}