michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkPathHeap.h" michael@0: #include "SkPath.h" michael@0: #include "SkStream.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkTSearch.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include michael@0: michael@0: #define kPathCount 64 michael@0: michael@0: SkPathHeap::SkPathHeap() : fHeap(kPathCount * sizeof(SkPath)) { michael@0: } michael@0: michael@0: SkPathHeap::SkPathHeap(SkReadBuffer& buffer) michael@0: : fHeap(kPathCount * sizeof(SkPath)) { michael@0: const int count = buffer.readInt(); michael@0: michael@0: fPaths.setCount(count); michael@0: SkPath** ptr = fPaths.begin(); michael@0: SkPath* p = (SkPath*)fHeap.allocThrow(count * sizeof(SkPath)); michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: new (p) SkPath; michael@0: buffer.readPath(p); michael@0: *ptr++ = p; // record the pointer michael@0: p++; // move to the next storage location michael@0: } michael@0: } michael@0: michael@0: SkPathHeap::~SkPathHeap() { michael@0: SkPath** iter = fPaths.begin(); michael@0: SkPath** stop = fPaths.end(); michael@0: while (iter < stop) { michael@0: (*iter)->~SkPath(); michael@0: iter++; michael@0: } michael@0: } michael@0: michael@0: int SkPathHeap::append(const SkPath& path) { michael@0: SkPath* p = (SkPath*)fHeap.allocThrow(sizeof(SkPath)); michael@0: new (p) SkPath(path); michael@0: *fPaths.append() = p; michael@0: return fPaths.count(); michael@0: } michael@0: michael@0: SkPathHeap::LookupEntry::LookupEntry(const SkPath& path) michael@0: : fGenerationID(path.getGenerationID()), fStorageSlot(0) { michael@0: } michael@0: michael@0: SkPathHeap::LookupEntry* SkPathHeap::addIfNotPresent(const SkPath& path) { michael@0: LookupEntry searchKey(path); michael@0: int index = SkTSearch( michael@0: fLookupTable.begin(), michael@0: fLookupTable.count(), michael@0: searchKey, michael@0: sizeof(LookupEntry)); michael@0: if (index < 0) { michael@0: index = ~index; michael@0: *fLookupTable.insert(index) = LookupEntry(path); michael@0: } michael@0: michael@0: return &fLookupTable[index];; michael@0: } michael@0: michael@0: int SkPathHeap::insert(const SkPath& path) { michael@0: SkPathHeap::LookupEntry* entry = this->addIfNotPresent(path); michael@0: michael@0: if (entry->storageSlot() > 0) { michael@0: return entry->storageSlot(); michael@0: } michael@0: michael@0: int newSlot = this->append(path); michael@0: SkASSERT(newSlot > 0); michael@0: entry->setStorageSlot(newSlot); michael@0: return newSlot; michael@0: } michael@0: michael@0: void SkPathHeap::flatten(SkWriteBuffer& buffer) const { michael@0: int count = fPaths.count(); michael@0: michael@0: buffer.writeInt(count); michael@0: SkPath* const* iter = fPaths.begin(); michael@0: SkPath* const* stop = fPaths.end(); michael@0: while (iter < stop) { michael@0: buffer.writePath(**iter); michael@0: iter++; michael@0: } michael@0: }