3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SkFlattenable.h"
9 #include "SkPtrRecorder.h"
11 ///////////////////////////////////////////////////////////////////////////////
13 void SkFlattenable::flatten(SkFlattenableWriteBuffer&) const
15 /* we don't write anything at the moment, but this allows our subclasses
16 to not know that, since we want them to always call INHERITED::flatten()
21 ///////////////////////////////////////////////////////////////////////////////
23 SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {}
25 uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) {
26 uint32_t index = fFactorySet.find(factory);
30 const char* name = SkFlattenable::FactoryToName(factory);
34 *fNames.append() = name;
35 return fFactorySet.add(factory);
38 const char* SkNamedFactorySet::getNextAddedFactoryName() {
39 if (fNextAddedFactory < fNames.count()) {
40 return fNames[fNextAddedFactory++];
45 ///////////////////////////////////////////////////////////////////////////////
47 SkRefCntSet::~SkRefCntSet() {
48 // call this now, while our decPtr() is sill in scope
52 void SkRefCntSet::incPtr(void* ptr) {
53 ((SkRefCnt*)ptr)->ref();
56 void SkRefCntSet::decPtr(void* ptr) {
57 ((SkRefCnt*)ptr)->unref();
60 ///////////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////////////////
64 #define MAX_ENTRY_COUNT 1024
68 SkFlattenable::Factory fFactory;
69 SkFlattenable::Type fType;
73 static Entry gEntries[MAX_ENTRY_COUNT];
75 void SkFlattenable::Register(const char name[], Factory factory, SkFlattenable::Type type) {
79 static bool gOnce = false;
85 SkASSERT(gCount < MAX_ENTRY_COUNT);
87 gEntries[gCount].fName = name;
88 gEntries[gCount].fFactory = factory;
89 gEntries[gCount].fType = type;
94 static void report_no_entries(const char* functionName) {
96 SkDebugf("%s has no registered name/factory/type entries."
97 " Call SkFlattenable::InitializeFlattenablesIfNeeded() before using gEntries",
103 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) {
104 InitializeFlattenablesIfNeeded();
106 report_no_entries(__FUNCTION__);
108 const Entry* entries = gEntries;
109 for (int i = gCount - 1; i >= 0; --i) {
110 if (strcmp(entries[i].fName, name) == 0) {
111 return entries[i].fFactory;
117 bool SkFlattenable::NameToType(const char name[], SkFlattenable::Type* type) {
118 SkASSERT(NULL != type);
119 InitializeFlattenablesIfNeeded();
121 report_no_entries(__FUNCTION__);
123 const Entry* entries = gEntries;
124 for (int i = gCount - 1; i >= 0; --i) {
125 if (strcmp(entries[i].fName, name) == 0) {
126 *type = entries[i].fType;
133 const char* SkFlattenable::FactoryToName(Factory fact) {
134 InitializeFlattenablesIfNeeded();
136 report_no_entries(__FUNCTION__);
138 const Entry* entries = gEntries;
139 for (int i = gCount - 1; i >= 0; --i) {
140 if (entries[i].fFactory == fact) {
141 return entries[i].fName;