1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkFlattenable.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 +#include "SkFlattenable.h" 1.12 +#include "SkPtrRecorder.h" 1.13 + 1.14 +/////////////////////////////////////////////////////////////////////////////// 1.15 + 1.16 +void SkFlattenable::flatten(SkWriteBuffer&) const 1.17 +{ 1.18 + /* we don't write anything at the moment, but this allows our subclasses 1.19 + to not know that, since we want them to always call INHERITED::flatten() 1.20 + in their code. 1.21 + */ 1.22 +} 1.23 + 1.24 +/////////////////////////////////////////////////////////////////////////////// 1.25 + 1.26 +SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {} 1.27 + 1.28 +uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) { 1.29 + uint32_t index = fFactorySet.find(factory); 1.30 + if (index > 0) { 1.31 + return index; 1.32 + } 1.33 + const char* name = SkFlattenable::FactoryToName(factory); 1.34 + if (NULL == name) { 1.35 + return 0; 1.36 + } 1.37 + *fNames.append() = name; 1.38 + return fFactorySet.add(factory); 1.39 +} 1.40 + 1.41 +const char* SkNamedFactorySet::getNextAddedFactoryName() { 1.42 + if (fNextAddedFactory < fNames.count()) { 1.43 + return fNames[fNextAddedFactory++]; 1.44 + } 1.45 + return NULL; 1.46 +} 1.47 + 1.48 +/////////////////////////////////////////////////////////////////////////////// 1.49 + 1.50 +SkRefCntSet::~SkRefCntSet() { 1.51 + // call this now, while our decPtr() is sill in scope 1.52 + this->reset(); 1.53 +} 1.54 + 1.55 +void SkRefCntSet::incPtr(void* ptr) { 1.56 + ((SkRefCnt*)ptr)->ref(); 1.57 +} 1.58 + 1.59 +void SkRefCntSet::decPtr(void* ptr) { 1.60 + ((SkRefCnt*)ptr)->unref(); 1.61 +} 1.62 + 1.63 +/////////////////////////////////////////////////////////////////////////////// 1.64 +/////////////////////////////////////////////////////////////////////////////// 1.65 +/////////////////////////////////////////////////////////////////////////////// 1.66 + 1.67 +#define MAX_ENTRY_COUNT 1024 1.68 + 1.69 +struct Entry { 1.70 + const char* fName; 1.71 + SkFlattenable::Factory fFactory; 1.72 + SkFlattenable::Type fType; 1.73 +}; 1.74 + 1.75 +static int gCount; 1.76 +static Entry gEntries[MAX_ENTRY_COUNT]; 1.77 + 1.78 +void SkFlattenable::Register(const char name[], Factory factory, SkFlattenable::Type type) { 1.79 + SkASSERT(name); 1.80 + SkASSERT(factory); 1.81 + 1.82 + static bool gOnce = false; 1.83 + if (!gOnce) { 1.84 + gCount = 0; 1.85 + gOnce = true; 1.86 + } 1.87 + 1.88 + SkASSERT(gCount < MAX_ENTRY_COUNT); 1.89 + 1.90 + gEntries[gCount].fName = name; 1.91 + gEntries[gCount].fFactory = factory; 1.92 + gEntries[gCount].fType = type; 1.93 + gCount += 1; 1.94 +} 1.95 + 1.96 +#ifdef SK_DEBUG 1.97 +static void report_no_entries(const char* functionName) { 1.98 + if (!gCount) { 1.99 + SkDebugf("%s has no registered name/factory/type entries." 1.100 + " Call SkFlattenable::InitializeFlattenablesIfNeeded() before using gEntries", 1.101 + functionName); 1.102 + } 1.103 +} 1.104 +#endif 1.105 + 1.106 +SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { 1.107 + InitializeFlattenablesIfNeeded(); 1.108 +#ifdef SK_DEBUG 1.109 + report_no_entries(__FUNCTION__); 1.110 +#endif 1.111 + const Entry* entries = gEntries; 1.112 + for (int i = gCount - 1; i >= 0; --i) { 1.113 + if (strcmp(entries[i].fName, name) == 0) { 1.114 + return entries[i].fFactory; 1.115 + } 1.116 + } 1.117 + return NULL; 1.118 +} 1.119 + 1.120 +bool SkFlattenable::NameToType(const char name[], SkFlattenable::Type* type) { 1.121 + SkASSERT(NULL != type); 1.122 + InitializeFlattenablesIfNeeded(); 1.123 +#ifdef SK_DEBUG 1.124 + report_no_entries(__FUNCTION__); 1.125 +#endif 1.126 + const Entry* entries = gEntries; 1.127 + for (int i = gCount - 1; i >= 0; --i) { 1.128 + if (strcmp(entries[i].fName, name) == 0) { 1.129 + *type = entries[i].fType; 1.130 + return true; 1.131 + } 1.132 + } 1.133 + return false; 1.134 +} 1.135 + 1.136 +const char* SkFlattenable::FactoryToName(Factory fact) { 1.137 + InitializeFlattenablesIfNeeded(); 1.138 +#ifdef SK_DEBUG 1.139 + report_no_entries(__FUNCTION__); 1.140 +#endif 1.141 + const Entry* entries = gEntries; 1.142 + for (int i = gCount - 1; i >= 0; --i) { 1.143 + if (entries[i].fFactory == fact) { 1.144 + return entries[i].fName; 1.145 + } 1.146 + } 1.147 + return NULL; 1.148 +}