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; |
56 }; | 57 }; |
57 | 58 |
58 static int gCount = 0; | 59 static int gCount = 0; |
59 static Entry gEntries[MAX_ENTRY_COUNT]; | 60 static Entry gEntries[MAX_ENTRY_COUNT]; |
60 | 61 |
61 void SkFlattenable::Register(const char name[], Factory factory) { | 62 void SkFlattenable::Register(const char name[], Factory factory, SkFlattenable::
Type type) { |
62 SkASSERT(name); | 63 SkASSERT(name); |
63 SkASSERT(factory); | 64 SkASSERT(factory); |
64 SkASSERT(gCount < MAX_ENTRY_COUNT); | 65 SkASSERT(gCount < MAX_ENTRY_COUNT); |
65 | 66 |
66 gEntries[gCount].fName = name; | 67 gEntries[gCount].fName = name; |
67 gEntries[gCount].fFactory = factory; | 68 gEntries[gCount].fFactory = factory; |
| 69 gEntries[gCount].fType = type; |
68 gCount += 1; | 70 gCount += 1; |
69 } | 71 } |
70 | 72 |
71 #ifdef SK_DEBUG | 73 #ifdef SK_DEBUG |
72 static void report_no_entries(const char* functionName) { | 74 static void report_no_entries(const char* functionName) { |
73 if (!gCount) { | 75 if (!gCount) { |
74 SkDebugf("%s has no registered name/factory/type entries." | 76 SkDebugf("%s has no registered name/factory/type entries." |
75 " Call SkFlattenable::InitializeFlattenablesIfNeeded() before u
sing gEntries", | 77 " Call SkFlattenable::InitializeFlattenablesIfNeeded() before u
sing gEntries", |
76 functionName); | 78 functionName); |
77 } | 79 } |
78 } | 80 } |
79 #endif | 81 #endif |
80 | 82 |
81 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { | 83 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { |
82 InitializeFlattenablesIfNeeded(); | 84 InitializeFlattenablesIfNeeded(); |
83 #ifdef SK_DEBUG | 85 #ifdef SK_DEBUG |
84 report_no_entries(__FUNCTION__); | 86 report_no_entries(__FUNCTION__); |
85 #endif | 87 #endif |
86 const Entry* entries = gEntries; | 88 const Entry* entries = gEntries; |
87 for (int i = gCount - 1; i >= 0; --i) { | 89 for (int i = gCount - 1; i >= 0; --i) { |
88 if (strcmp(entries[i].fName, name) == 0) { | 90 if (strcmp(entries[i].fName, name) == 0) { |
89 return entries[i].fFactory; | 91 return entries[i].fFactory; |
90 } | 92 } |
91 } | 93 } |
92 return nullptr; | 94 return nullptr; |
93 } | 95 } |
94 | 96 |
| 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 |
95 const char* SkFlattenable::FactoryToName(Factory fact) { | 113 const char* SkFlattenable::FactoryToName(Factory fact) { |
96 InitializeFlattenablesIfNeeded(); | 114 InitializeFlattenablesIfNeeded(); |
97 #ifdef SK_DEBUG | 115 #ifdef SK_DEBUG |
98 report_no_entries(__FUNCTION__); | 116 report_no_entries(__FUNCTION__); |
99 #endif | 117 #endif |
100 const Entry* entries = gEntries; | 118 const Entry* entries = gEntries; |
101 for (int i = gCount - 1; i >= 0; --i) { | 119 for (int i = gCount - 1; i >= 0; --i) { |
102 if (entries[i].fFactory == fact) { | 120 if (entries[i].fFactory == fact) { |
103 return entries[i].fName; | 121 return entries[i].fName; |
104 } | 122 } |
105 } | 123 } |
106 return nullptr; | 124 return nullptr; |
107 } | 125 } |
OLD | NEW |