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 "SkMallocPixelRef.h"
9 #include "SkBitmap.h"
10 #include "SkFlattenable.h"
11
SkMallocPixelRef(void * storage,size_t size,SkColorTable * ctable)12 SkMallocPixelRef::SkMallocPixelRef(void* storage, size_t size,
13 SkColorTable* ctable) {
14 if (NULL == storage) {
15 storage = sk_malloc_throw(size);
16 }
17 fStorage = storage;
18 fSize = size;
19 fCTable = ctable;
20 SkSafeRef(ctable);
21
22 this->setPreLocked(fStorage, fCTable);
23 }
24
~SkMallocPixelRef()25 SkMallocPixelRef::~SkMallocPixelRef() {
26 SkSafeUnref(fCTable);
27 sk_free(fStorage);
28 }
29
onLockPixels(SkColorTable ** ct)30 void* SkMallocPixelRef::onLockPixels(SkColorTable** ct) {
31 *ct = fCTable;
32 return fStorage;
33 }
34
onUnlockPixels()35 void SkMallocPixelRef::onUnlockPixels() {
36 // nothing to do
37 }
38
flatten(SkFlattenableWriteBuffer & buffer) const39 void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
40 this->INHERITED::flatten(buffer);
41
42 buffer.write32(fSize);
43 buffer.writePad(fStorage, fSize);
44 if (fCTable) {
45 buffer.writeBool(true);
46 fCTable->flatten(buffer);
47 } else {
48 buffer.writeBool(false);
49 }
50 }
51
SkMallocPixelRef(SkFlattenableReadBuffer & buffer)52 SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer)
53 : INHERITED(buffer, NULL) {
54 fSize = buffer.readU32();
55 fStorage = sk_malloc_throw(fSize);
56 buffer.read(fStorage, fSize);
57 if (buffer.readBool()) {
58 fCTable = SkNEW_ARGS(SkColorTable, (buffer));
59 } else {
60 fCTable = NULL;
61 }
62
63 this->setPreLocked(fStorage, fCTable);
64 }
65
66 SK_DEFINE_PIXEL_REF_REGISTRAR(SkMallocPixelRef)
67