1#include "SkMallocPixelRef.h"
2#include "SkBitmap.h"
3#include "SkFlattenable.h"
4
5SkMallocPixelRef::SkMallocPixelRef(void* storage, size_t size,
6                                   SkColorTable* ctable) {
7    if (NULL == storage) {
8        storage = sk_malloc_throw(size);
9    }
10    fStorage = storage;
11    fSize = size;
12    fCTable = ctable;
13    SkSafeRef(ctable);
14}
15
16SkMallocPixelRef::~SkMallocPixelRef() {
17    SkSafeUnref(fCTable);
18    sk_free(fStorage);
19}
20
21void* SkMallocPixelRef::onLockPixels(SkColorTable** ct) {
22    *ct = fCTable;
23    return fStorage;
24}
25
26void SkMallocPixelRef::onUnlockPixels() {
27    // nothing to do
28}
29
30void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
31    this->INHERITED::flatten(buffer);
32
33    buffer.write32(fSize);
34    buffer.writePad(fStorage, fSize);
35    if (fCTable) {
36        buffer.writeBool(true);
37        fCTable->flatten(buffer);
38    } else {
39        buffer.writeBool(false);
40    }
41}
42
43SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer)
44        : INHERITED(buffer, NULL) {
45    fSize = buffer.readU32();
46    fStorage = sk_malloc_throw(fSize);
47    buffer.read(fStorage, fSize);
48    if (buffer.readBool()) {
49        fCTable = SkNEW_ARGS(SkColorTable, (buffer));
50    } else {
51        fCTable = NULL;
52    }
53}
54
55static SkPixelRef::Registrar reg("SkMallocPixelRef",
56                                 SkMallocPixelRef::Create);
57
58