epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkFlattenable.h" |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 9 | #include "SkPtrRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 11 | SK_DEFINE_INST_COUNT(SkFlattenable) |
| 12 | |
reed@google.com | f2eb5ab | 2011-05-10 22:56:42 +0000 | [diff] [blame] | 13 | /////////////////////////////////////////////////////////////////////////////// |
| 14 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 15 | void SkFlattenable::flatten(SkFlattenableWriteBuffer&) const |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | { |
| 17 | /* we don't write anything at the moment, but this allows our subclasses |
| 18 | to not know that, since we want them to always call INHERITED::flatten() |
| 19 | in their code. |
| 20 | */ |
| 21 | } |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 25 | SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {} |
| 26 | |
| 27 | uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) { |
| 28 | uint32_t index = fFactorySet.find(factory); |
| 29 | if (index > 0) { |
| 30 | return index; |
| 31 | } |
| 32 | const char* name = SkFlattenable::FactoryToName(factory); |
| 33 | if (NULL == name) { |
| 34 | return 0; |
| 35 | } |
| 36 | *fNames.append() = name; |
| 37 | return fFactorySet.add(factory); |
| 38 | } |
| 39 | |
| 40 | const char* SkNamedFactorySet::getNextAddedFactoryName() { |
| 41 | if (fNextAddedFactory < fNames.count()) { |
| 42 | return fNames[fNextAddedFactory++]; |
| 43 | } |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 49 | SkRefCntSet::~SkRefCntSet() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | // call this now, while our decPtr() is sill in scope |
| 51 | this->reset(); |
| 52 | } |
| 53 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 54 | void SkRefCntSet::incPtr(void* ptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | ((SkRefCnt*)ptr)->ref(); |
| 56 | } |
| 57 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 58 | void SkRefCntSet::decPtr(void* ptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | ((SkRefCnt*)ptr)->unref(); |
| 60 | } |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | |
bungeman@google.com | 488379e | 2012-06-27 15:57:34 +0000 | [diff] [blame] | 66 | #define MAX_PAIR_COUNT 1024 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | |
| 68 | struct Pair { |
| 69 | const char* fName; |
| 70 | SkFlattenable::Factory fFactory; |
| 71 | }; |
| 72 | |
| 73 | static int gCount; |
| 74 | static Pair gPairs[MAX_PAIR_COUNT]; |
| 75 | |
| 76 | void SkFlattenable::Register(const char name[], Factory factory) { |
| 77 | SkASSERT(name); |
| 78 | SkASSERT(factory); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | static bool gOnce; |
| 81 | if (!gOnce) { |
| 82 | gCount = 0; |
| 83 | gOnce = true; |
| 84 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 85 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | SkASSERT(gCount < MAX_PAIR_COUNT); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | gPairs[gCount].fName = name; |
| 89 | gPairs[gCount].fFactory = factory; |
| 90 | gCount += 1; |
| 91 | } |
| 92 | |
scroggo@google.com | a0c2bc2 | 2012-09-21 17:54:46 +0000 | [diff] [blame] | 93 | #ifdef SK_DEBUG |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 94 | static void report_no_entries(const char* functionName) { |
| 95 | if (!gCount) { |
| 96 | SkDebugf("%s has no registered name/factory pairs." |
caryclark@google.com | 9245498 | 2011-12-16 16:54:07 +0000 | [diff] [blame] | 97 | " Call SkGraphics::Init() at process initialization time.", |
| 98 | functionName); |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | #endif |
| 102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { |
scroggo@google.com | a0c2bc2 | 2012-09-21 17:54:46 +0000 | [diff] [blame] | 104 | #ifdef SK_DEBUG |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 105 | report_no_entries(__FUNCTION__); |
| 106 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | const Pair* pairs = gPairs; |
| 108 | for (int i = gCount - 1; i >= 0; --i) { |
| 109 | if (strcmp(pairs[i].fName, name) == 0) { |
| 110 | return pairs[i].fFactory; |
| 111 | } |
| 112 | } |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | const char* SkFlattenable::FactoryToName(Factory fact) { |
scroggo@google.com | a0c2bc2 | 2012-09-21 17:54:46 +0000 | [diff] [blame] | 117 | #ifdef SK_DEBUG |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 118 | report_no_entries(__FUNCTION__); |
| 119 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | const Pair* pairs = gPairs; |
| 121 | for (int i = gCount - 1; i >= 0; --i) { |
| 122 | if (pairs[i].fFactory == fact) { |
| 123 | return pairs[i].fName; |
| 124 | } |
| 125 | } |
| 126 | return NULL; |
| 127 | } |