Home | History | Annotate | Download | only in core
      1 #include "SkMallocPixelRef.h"
      2 #include "SkBitmap.h"
      3 #include "SkFlattenable.h"
      4 
      5 SkMallocPixelRef::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 
     16 SkMallocPixelRef::~SkMallocPixelRef() {
     17     SkSafeUnref(fCTable);
     18     sk_free(fStorage);
     19 }
     20 
     21 void* SkMallocPixelRef::onLockPixels(SkColorTable** ct) {
     22     *ct = fCTable;
     23     return fStorage;
     24 }
     25 
     26 void SkMallocPixelRef::onUnlockPixels() {
     27     // nothing to do
     28 }
     29 
     30 void 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 
     43 SkMallocPixelRef::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 
     55 static SkPixelRef::Registrar reg("SkMallocPixelRef",
     56                                  SkMallocPixelRef::Create);
     57 
     58