Line data Source code
1 : /*
2 : * Copyright 2011 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 :
8 : #include "SkFlattenable.h"
9 : #include "SkPtrRecorder.h"
10 : #include "SkReadBuffer.h"
11 :
12 0 : SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {}
13 :
14 0 : uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) {
15 0 : uint32_t index = fFactorySet.find(factory);
16 0 : if (index > 0) {
17 0 : return index;
18 : }
19 0 : const char* name = SkFlattenable::FactoryToName(factory);
20 0 : if (nullptr == name) {
21 0 : return 0;
22 : }
23 0 : *fNames.append() = name;
24 0 : return fFactorySet.add(factory);
25 : }
26 :
27 0 : const char* SkNamedFactorySet::getNextAddedFactoryName() {
28 0 : if (fNextAddedFactory < fNames.count()) {
29 0 : return fNames[fNextAddedFactory++];
30 : }
31 0 : return nullptr;
32 : }
33 :
34 : ///////////////////////////////////////////////////////////////////////////////
35 :
36 0 : SkRefCntSet::~SkRefCntSet() {
37 : // call this now, while our decPtr() is sill in scope
38 0 : this->reset();
39 0 : }
40 :
41 0 : void SkRefCntSet::incPtr(void* ptr) {
42 0 : ((SkRefCnt*)ptr)->ref();
43 0 : }
44 :
45 0 : void SkRefCntSet::decPtr(void* ptr) {
46 0 : ((SkRefCnt*)ptr)->unref();
47 0 : }
48 :
49 : ///////////////////////////////////////////////////////////////////////////////
50 :
51 : #define MAX_ENTRY_COUNT 1024
52 :
53 : struct Entry {
54 : const char* fName;
55 : SkFlattenable::Factory fFactory;
56 : SkFlattenable::Type fType;
57 : };
58 :
59 : static int gCount = 0;
60 : static Entry gEntries[MAX_ENTRY_COUNT];
61 :
62 0 : void SkFlattenable::Register(const char name[], Factory factory, SkFlattenable::Type type) {
63 0 : SkASSERT(name);
64 0 : SkASSERT(factory);
65 0 : SkASSERT(gCount < MAX_ENTRY_COUNT);
66 :
67 0 : gEntries[gCount].fName = name;
68 0 : gEntries[gCount].fFactory = factory;
69 0 : gEntries[gCount].fType = type;
70 0 : gCount += 1;
71 0 : }
72 :
73 : #ifdef SK_DEBUG
74 0 : static void report_no_entries(const char* functionName) {
75 0 : if (!gCount) {
76 : SkDebugf("%s has no registered name/factory/type entries."
77 : " Call SkFlattenable::InitializeFlattenablesIfNeeded() before using gEntries",
78 0 : functionName);
79 : }
80 0 : }
81 : #endif
82 :
83 0 : SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) {
84 0 : InitializeFlattenablesIfNeeded();
85 : #ifdef SK_DEBUG
86 0 : report_no_entries(__FUNCTION__);
87 : #endif
88 0 : const Entry* entries = gEntries;
89 0 : for (int i = gCount - 1; i >= 0; --i) {
90 0 : if (strcmp(entries[i].fName, name) == 0) {
91 0 : return entries[i].fFactory;
92 : }
93 : }
94 0 : return nullptr;
95 : }
96 :
97 0 : bool SkFlattenable::NameToType(const char name[], SkFlattenable::Type* type) {
98 0 : SkASSERT(type);
99 0 : InitializeFlattenablesIfNeeded();
100 : #ifdef SK_DEBUG
101 0 : report_no_entries(__FUNCTION__);
102 : #endif
103 0 : const Entry* entries = gEntries;
104 0 : for (int i = gCount - 1; i >= 0; --i) {
105 0 : if (strcmp(entries[i].fName, name) == 0) {
106 0 : *type = entries[i].fType;
107 0 : return true;
108 : }
109 : }
110 0 : return false;
111 : }
112 :
113 0 : const char* SkFlattenable::FactoryToName(Factory fact) {
114 0 : InitializeFlattenablesIfNeeded();
115 : #ifdef SK_DEBUG
116 0 : report_no_entries(__FUNCTION__);
117 : #endif
118 0 : const Entry* entries = gEntries;
119 0 : for (int i = gCount - 1; i >= 0; --i) {
120 0 : if (entries[i].fFactory == fact) {
121 0 : return entries[i].fName;
122 : }
123 : }
124 0 : return nullptr;
125 : }
|