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