Home | History | Annotate | Download | only in core
      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  */
      8 #include "SkPathHeap.h"
      9 #include "SkPath.h"
     10 #include "SkStream.h"
     11 #include "SkFlattenableBuffers.h"
     12 #include <new>
     13 
     14 SK_DEFINE_INST_COUNT(SkPathHeap)
     15 
     16 #define kPathCount  64
     17 
     18 SkPathHeap::SkPathHeap() : fHeap(kPathCount * sizeof(SkPath)) {
     19 }
     20 
     21 SkPathHeap::SkPathHeap(SkFlattenableReadBuffer& buffer)
     22             : fHeap(kPathCount * sizeof(SkPath)) {
     23     const int count = buffer.readInt();
     24 
     25     fPaths.setCount(count);
     26     SkPath** ptr = fPaths.begin();
     27     SkPath* p = (SkPath*)fHeap.allocThrow(count * sizeof(SkPath));
     28 
     29     for (int i = 0; i < count; i++) {
     30         new (p) SkPath;
     31         buffer.readPath(p);
     32         *ptr++ = p; // record the pointer
     33         p++;        // move to the next storage location
     34     }
     35 }
     36 
     37 SkPathHeap::~SkPathHeap() {
     38     SkPath** iter = fPaths.begin();
     39     SkPath** stop = fPaths.end();
     40     while (iter < stop) {
     41         (*iter)->~SkPath();
     42         iter++;
     43     }
     44 }
     45 
     46 int SkPathHeap::append(const SkPath& path) {
     47     SkPath* p = (SkPath*)fHeap.allocThrow(sizeof(SkPath));
     48     new (p) SkPath(path);
     49     *fPaths.append() = p;
     50     return fPaths.count();
     51 }
     52 
     53 void SkPathHeap::flatten(SkFlattenableWriteBuffer& buffer) const {
     54     int count = fPaths.count();
     55 
     56     buffer.writeInt(count);
     57     SkPath* const* iter = fPaths.begin();
     58     SkPath* const* stop = fPaths.end();
     59     while (iter < stop) {
     60         buffer.writePath(**iter);
     61         iter++;
     62     }
     63 }
     64