OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkFlattenable.h" | 8 #include "SkFlattenable.h" |
9 #include "SkPtrRecorder.h" | 9 #include "SkPtrRecorder.h" |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 void SkRefCntSet::decPtr(void* ptr) { | 58 void SkRefCntSet::decPtr(void* ptr) { |
59 ((SkRefCnt*)ptr)->unref(); | 59 ((SkRefCnt*)ptr)->unref(); |
60 } | 60 } |
61 | 61 |
62 /////////////////////////////////////////////////////////////////////////////// | 62 /////////////////////////////////////////////////////////////////////////////// |
63 /////////////////////////////////////////////////////////////////////////////// | 63 /////////////////////////////////////////////////////////////////////////////// |
64 /////////////////////////////////////////////////////////////////////////////// | 64 /////////////////////////////////////////////////////////////////////////////// |
65 | 65 |
66 #define MAX_PAIR_COUNT 1024 | 66 #define MAX_PAIR_COUNT 1024 |
67 | 67 |
68 struct FlattenableInfo { | |
69 SkFlattenable::Factory fFactory; | |
70 SkFlattenable::TypeCheck fTypeCheck; | |
71 }; | |
72 | |
68 struct Pair { | 73 struct Pair { |
69 const char* fName; | 74 const char* fName; |
70 SkFlattenable::Factory fFactory; | 75 FlattenableInfo fInfo; |
71 }; | 76 }; |
mtklein
2013/10/07 19:29:56
Why not merge this together?
struct Entry {
con
sugoi1
2013/10/08 20:23:10
Done.
| |
72 | 77 |
73 static int gCount; | 78 static int gCount; |
74 static Pair gPairs[MAX_PAIR_COUNT]; | 79 static Pair gPairs[MAX_PAIR_COUNT]; |
75 | 80 |
76 void SkFlattenable::Register(const char name[], Factory factory) { | 81 void SkFlattenable::Register(const char name[], Factory factory, TypeCheck typeC heck) { |
77 SkASSERT(name); | 82 SkASSERT(name); |
78 SkASSERT(factory); | 83 SkASSERT(factory); |
84 SkASSERT(typeCheck); | |
79 | 85 |
80 static bool gOnce; | 86 static bool gOnce = false; |
81 if (!gOnce) { | 87 if (!gOnce) { |
82 gCount = 0; | 88 gCount = 0; |
83 gOnce = true; | 89 gOnce = true; |
84 } | 90 } |
85 | 91 |
86 SkASSERT(gCount < MAX_PAIR_COUNT); | 92 SkASSERT(gCount < MAX_PAIR_COUNT); |
87 | 93 |
88 gPairs[gCount].fName = name; | 94 gPairs[gCount].fName = name; |
89 gPairs[gCount].fFactory = factory; | 95 gPairs[gCount].fInfo.fFactory = factory; |
96 gPairs[gCount].fInfo.fTypeCheck = typeCheck; | |
90 gCount += 1; | 97 gCount += 1; |
91 } | 98 } |
92 | 99 |
93 #ifdef SK_DEBUG | 100 #ifdef SK_DEBUG |
94 static void report_no_entries(const char* functionName) { | 101 static void report_no_entries(const char* functionName) { |
95 if (!gCount) { | 102 if (!gCount) { |
96 SkDebugf("%s has no registered name/factory pairs." | 103 SkDebugf("%s has no registered name/factory pairs." |
97 " Call SkGraphics::Init() at process initialization time.", | 104 " Call SkGraphics::Init() at process initialization time.", |
98 functionName); | 105 functionName); |
99 } | 106 } |
100 } | 107 } |
101 #endif | 108 #endif |
102 | 109 |
103 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { | 110 SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { |
111 InitializeFlattenables(); | |
sugoi1
2013/10/07 15:54:08
Initialization is now "on demand", so any usage of
| |
104 #ifdef SK_DEBUG | 112 #ifdef SK_DEBUG |
105 report_no_entries(__FUNCTION__); | 113 report_no_entries(__FUNCTION__); |
106 #endif | 114 #endif |
107 const Pair* pairs = gPairs; | 115 const Pair* pairs = gPairs; |
108 for (int i = gCount - 1; i >= 0; --i) { | 116 for (int i = gCount - 1; i >= 0; --i) { |
109 if (strcmp(pairs[i].fName, name) == 0) { | 117 if (strcmp(pairs[i].fName, name) == 0) { |
110 return pairs[i].fFactory; | 118 return pairs[i].fInfo.fFactory; |
119 } | |
120 } | |
121 return NULL; | |
122 } | |
123 | |
124 SkFlattenable::TypeCheck SkFlattenable::NameToTypeCheck(const char name[]) { | |
125 InitializeFlattenables(); | |
mtklein
2013/10/07 19:29:56
Shall we take the opportunity to rename it Initial
sugoi1
2013/10/08 20:23:10
Done.
| |
126 #ifdef SK_DEBUG | |
127 report_no_entries(__FUNCTION__); | |
128 #endif | |
129 const Pair* pairs = gPairs; | |
130 for (int i = gCount - 1; i >= 0; --i) { | |
131 if (strcmp(pairs[i].fName, name) == 0) { | |
132 return pairs[i].fInfo.fTypeCheck; | |
111 } | 133 } |
112 } | 134 } |
113 return NULL; | 135 return NULL; |
114 } | 136 } |
115 | 137 |
116 const char* SkFlattenable::FactoryToName(Factory fact) { | 138 const char* SkFlattenable::FactoryToName(Factory fact) { |
139 InitializeFlattenables(); | |
117 #ifdef SK_DEBUG | 140 #ifdef SK_DEBUG |
118 report_no_entries(__FUNCTION__); | 141 report_no_entries(__FUNCTION__); |
119 #endif | 142 #endif |
120 const Pair* pairs = gPairs; | 143 const Pair* pairs = gPairs; |
121 for (int i = gCount - 1; i >= 0; --i) { | 144 for (int i = gCount - 1; i >= 0; --i) { |
122 if (pairs[i].fFactory == fact) { | 145 if (pairs[i].fInfo.fFactory == fact) { |
123 return pairs[i].fName; | 146 return pairs[i].fName; |
124 } | 147 } |
125 } | 148 } |
126 return NULL; | 149 return NULL; |
127 } | 150 } |
OLD | NEW |