1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkPathHeap.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 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 "SkPathHeap.h" 1.12 +#include "SkPath.h" 1.13 +#include "SkStream.h" 1.14 +#include "SkReadBuffer.h" 1.15 +#include "SkTSearch.h" 1.16 +#include "SkWriteBuffer.h" 1.17 +#include <new> 1.18 + 1.19 +#define kPathCount 64 1.20 + 1.21 +SkPathHeap::SkPathHeap() : fHeap(kPathCount * sizeof(SkPath)) { 1.22 +} 1.23 + 1.24 +SkPathHeap::SkPathHeap(SkReadBuffer& buffer) 1.25 + : fHeap(kPathCount * sizeof(SkPath)) { 1.26 + const int count = buffer.readInt(); 1.27 + 1.28 + fPaths.setCount(count); 1.29 + SkPath** ptr = fPaths.begin(); 1.30 + SkPath* p = (SkPath*)fHeap.allocThrow(count * sizeof(SkPath)); 1.31 + 1.32 + for (int i = 0; i < count; i++) { 1.33 + new (p) SkPath; 1.34 + buffer.readPath(p); 1.35 + *ptr++ = p; // record the pointer 1.36 + p++; // move to the next storage location 1.37 + } 1.38 +} 1.39 + 1.40 +SkPathHeap::~SkPathHeap() { 1.41 + SkPath** iter = fPaths.begin(); 1.42 + SkPath** stop = fPaths.end(); 1.43 + while (iter < stop) { 1.44 + (*iter)->~SkPath(); 1.45 + iter++; 1.46 + } 1.47 +} 1.48 + 1.49 +int SkPathHeap::append(const SkPath& path) { 1.50 + SkPath* p = (SkPath*)fHeap.allocThrow(sizeof(SkPath)); 1.51 + new (p) SkPath(path); 1.52 + *fPaths.append() = p; 1.53 + return fPaths.count(); 1.54 +} 1.55 + 1.56 +SkPathHeap::LookupEntry::LookupEntry(const SkPath& path) 1.57 + : fGenerationID(path.getGenerationID()), fStorageSlot(0) { 1.58 +} 1.59 + 1.60 +SkPathHeap::LookupEntry* SkPathHeap::addIfNotPresent(const SkPath& path) { 1.61 + LookupEntry searchKey(path); 1.62 + int index = SkTSearch<const LookupEntry, LookupEntry::Less>( 1.63 + fLookupTable.begin(), 1.64 + fLookupTable.count(), 1.65 + searchKey, 1.66 + sizeof(LookupEntry)); 1.67 + if (index < 0) { 1.68 + index = ~index; 1.69 + *fLookupTable.insert(index) = LookupEntry(path); 1.70 + } 1.71 + 1.72 + return &fLookupTable[index];; 1.73 +} 1.74 + 1.75 +int SkPathHeap::insert(const SkPath& path) { 1.76 + SkPathHeap::LookupEntry* entry = this->addIfNotPresent(path); 1.77 + 1.78 + if (entry->storageSlot() > 0) { 1.79 + return entry->storageSlot(); 1.80 + } 1.81 + 1.82 + int newSlot = this->append(path); 1.83 + SkASSERT(newSlot > 0); 1.84 + entry->setStorageSlot(newSlot); 1.85 + return newSlot; 1.86 +} 1.87 + 1.88 +void SkPathHeap::flatten(SkWriteBuffer& buffer) const { 1.89 + int count = fPaths.count(); 1.90 + 1.91 + buffer.writeInt(count); 1.92 + SkPath* const* iter = fPaths.begin(); 1.93 + SkPath* const* stop = fPaths.end(); 1.94 + while (iter < stop) { 1.95 + buffer.writePath(**iter); 1.96 + iter++; 1.97 + } 1.98 +}