2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkFlattenable.h"
9 #include "SkPtrRecorder.h"
10 #include "SkReadBuffer.h"
12 ///////////////////////////////////////////////////////////////////////////////
14 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
15 bool SkFlattenable::NeedsDeepUnflatten(const SkReadBuffer& buffer) {
16 return buffer.isVersionLT(SkReadBuffer::kFlattenCreateProc_Version);
20 ///////////////////////////////////////////////////////////////////////////////
22 SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {}
24 uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) {
25 uint32_t index = fFactorySet.find(factory);
29 const char* name = SkFlattenable::FactoryToName(factory);
33 *fNames.append() = name;
34 return fFactorySet.add(factory);
37 const char* SkNamedFactorySet::getNextAddedFactoryName() {
38 if (fNextAddedFactory < fNames.count()) {
39 return fNames[fNextAddedFactory++];
44 ///////////////////////////////////////////////////////////////////////////////
46 SkRefCntSet::~SkRefCntSet() {
47 // call this now, while our decPtr() is sill in scope
51 void SkRefCntSet::incPtr(void* ptr) {
52 ((SkRefCnt*)ptr)->ref();
55 void SkRefCntSet::decPtr(void* ptr) {
56 ((SkRefCnt*)ptr)->unref();
59 ///////////////////////////////////////////////////////////////////////////////
60 ///////////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////////////////
63 #define MAX_ENTRY_COUNT 1024
67 SkFlattenable::Factory fFactory;
68 SkFlattenable::Type fType;
72 static Entry gEntries[MAX_ENTRY_COUNT];
74 void SkFlattenable::Register(const char name[], Factory factory, SkFlattenable::Type type) {
78 static bool gOnce = false;
84 SkASSERT(gCount < MAX_ENTRY_COUNT);
86 gEntries[gCount].fName = name;
87 gEntries[gCount].fFactory = factory;
88 gEntries[gCount].fType = type;
93 static void report_no_entries(const char* functionName) {
95 SkDebugf("%s has no registered name/factory/type entries."
96 " Call SkFlattenable::InitializeFlattenablesIfNeeded() before using gEntries",
102 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) {
103 InitializeFlattenablesIfNeeded();
105 report_no_entries(__FUNCTION__);
107 const Entry* entries = gEntries;
108 for (int i = gCount - 1; i >= 0; --i) {
109 if (strcmp(entries[i].fName, name) == 0) {
110 return entries[i].fFactory;
116 bool SkFlattenable::NameToType(const char name[], SkFlattenable::Type* type) {
118 InitializeFlattenablesIfNeeded();
120 report_no_entries(__FUNCTION__);
122 const Entry* entries = gEntries;
123 for (int i = gCount - 1; i >= 0; --i) {
124 if (strcmp(entries[i].fName, name) == 0) {
125 *type = entries[i].fType;
132 const char* SkFlattenable::FactoryToName(Factory fact) {
133 InitializeFlattenablesIfNeeded();
135 report_no_entries(__FUNCTION__);
137 const Entry* entries = gEntries;
138 for (int i = gCount - 1; i >= 0; --i) {
139 if (entries[i].fFactory == fact) {
140 return entries[i].fName;