OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFlattenable.h" | 8 #include "SkFlattenable.h" |
9 #include "SkPtrRecorder.h" | 9 #include "SkPtrRecorder.h" |
10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 ((SkRefCnt*)ptr)->unref(); | 46 ((SkRefCnt*)ptr)->unref(); |
47 } | 47 } |
48 | 48 |
49 /////////////////////////////////////////////////////////////////////////////// | 49 /////////////////////////////////////////////////////////////////////////////// |
50 | 50 |
51 #define MAX_ENTRY_COUNT 1024 | 51 #define MAX_ENTRY_COUNT 1024 |
52 | 52 |
53 struct Entry { | 53 struct Entry { |
54 const char* fName; | 54 const char* fName; |
55 SkFlattenable::Factory fFactory; | 55 SkFlattenable::Factory fFactory; |
56 SkFlattenable::Type fType; | |
57 }; | 56 }; |
58 | 57 |
59 static int gCount = 0; | 58 static int gCount = 0; |
60 static Entry gEntries[MAX_ENTRY_COUNT]; | 59 static Entry gEntries[MAX_ENTRY_COUNT]; |
61 | 60 |
62 void SkFlattenable::Register(const char name[], Factory factory, SkFlattenable::
Type type) { | 61 void SkFlattenable::Register(const char name[], Factory factory) { |
63 SkASSERT(name); | 62 SkASSERT(name); |
64 SkASSERT(factory); | 63 SkASSERT(factory); |
65 SkASSERT(gCount < MAX_ENTRY_COUNT); | 64 SkASSERT(gCount < MAX_ENTRY_COUNT); |
66 | 65 |
67 gEntries[gCount].fName = name; | 66 gEntries[gCount].fName = name; |
68 gEntries[gCount].fFactory = factory; | 67 gEntries[gCount].fFactory = factory; |
69 gEntries[gCount].fType = type; | |
70 gCount += 1; | 68 gCount += 1; |
71 } | 69 } |
72 | 70 |
73 #ifdef SK_DEBUG | 71 #ifdef SK_DEBUG |
74 static void report_no_entries(const char* functionName) { | 72 static void report_no_entries(const char* functionName) { |
75 if (!gCount) { | 73 if (!gCount) { |
76 SkDebugf("%s has no registered name/factory/type entries." | 74 SkDebugf("%s has no registered name/factory/type entries." |
77 " Call SkFlattenable::InitializeFlattenablesIfNeeded() before u
sing gEntries", | 75 " Call SkFlattenable::InitializeFlattenablesIfNeeded() before u
sing gEntries", |
78 functionName); | 76 functionName); |
79 } | 77 } |
80 } | 78 } |
81 #endif | 79 #endif |
82 | 80 |
83 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { | 81 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { |
84 InitializeFlattenablesIfNeeded(); | 82 InitializeFlattenablesIfNeeded(); |
85 #ifdef SK_DEBUG | 83 #ifdef SK_DEBUG |
86 report_no_entries(__FUNCTION__); | 84 report_no_entries(__FUNCTION__); |
87 #endif | 85 #endif |
88 const Entry* entries = gEntries; | 86 const Entry* entries = gEntries; |
89 for (int i = gCount - 1; i >= 0; --i) { | 87 for (int i = gCount - 1; i >= 0; --i) { |
90 if (strcmp(entries[i].fName, name) == 0) { | 88 if (strcmp(entries[i].fName, name) == 0) { |
91 return entries[i].fFactory; | 89 return entries[i].fFactory; |
92 } | 90 } |
93 } | 91 } |
94 return nullptr; | 92 return nullptr; |
95 } | 93 } |
96 | 94 |
97 bool SkFlattenable::NameToType(const char name[], SkFlattenable::Type* type) { | |
98 SkASSERT(type); | |
99 InitializeFlattenablesIfNeeded(); | |
100 #ifdef SK_DEBUG | |
101 report_no_entries(__FUNCTION__); | |
102 #endif | |
103 const Entry* entries = gEntries; | |
104 for (int i = gCount - 1; i >= 0; --i) { | |
105 if (strcmp(entries[i].fName, name) == 0) { | |
106 *type = entries[i].fType; | |
107 return true; | |
108 } | |
109 } | |
110 return false; | |
111 } | |
112 | |
113 const char* SkFlattenable::FactoryToName(Factory fact) { | 95 const char* SkFlattenable::FactoryToName(Factory fact) { |
114 InitializeFlattenablesIfNeeded(); | 96 InitializeFlattenablesIfNeeded(); |
115 #ifdef SK_DEBUG | 97 #ifdef SK_DEBUG |
116 report_no_entries(__FUNCTION__); | 98 report_no_entries(__FUNCTION__); |
117 #endif | 99 #endif |
118 const Entry* entries = gEntries; | 100 const Entry* entries = gEntries; |
119 for (int i = gCount - 1; i >= 0; --i) { | 101 for (int i = gCount - 1; i >= 0; --i) { |
120 if (entries[i].fFactory == fact) { | 102 if (entries[i].fFactory == fact) { |
121 return entries[i].fName; | 103 return entries[i].fName; |
122 } | 104 } |
123 } | 105 } |
124 return nullptr; | 106 return nullptr; |
125 } | 107 } |
OLD | NEW |