SkMallocPixelRef.h revision 927138977fa256a6719baf74221882555b24008f
1 2/* 3 * Copyright 2008 The Android Open Source Project 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 9 10#ifndef SkMallocPixelRef_DEFINED 11#define SkMallocPixelRef_DEFINED 12 13#include "SkPixelRef.h" 14 15/** We explicitly use the same allocator for our pixels that SkMask does, 16 so that we can freely assign memory allocated by one class to the other. 17*/ 18class SkMallocPixelRef : public SkPixelRef { 19public: 20 /** 21 * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes, 22 * and optional colortable. The caller is responsible for managing the 23 * lifetime of the pixel storage buffer, as this pixelref will not try 24 * to delete it. 25 * 26 * The pixelref will ref() the colortable (if not NULL). 27 * 28 * Returns NULL on failure. 29 */ 30 static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr, 31 size_t rowBytes, SkColorTable*); 32 33 /** 34 * Return a new SkMallocPixelRef, automatically allocating storage for the 35 * pixels. If rowBytes are 0, an optimal value will be chosen automatically. 36 * If rowBytes is > 0, then it will be respected, or NULL will be returned 37 * if rowBytes is invalid for the specified info. 38 * 39 * This pixelref will ref() the specified colortable (if not NULL). 40 * 41 * Returns NULL on failure. 42 */ 43 static SkMallocPixelRef* NewAllocate(const SkImageInfo& info, 44 size_t rowBytes, SkColorTable*); 45 46 /** 47 * Return a new SkMallocPixelRef with the provided pixel storage, 48 * rowBytes, and optional colortable. On destruction, ReleaseProc 49 * will be called. 50 * 51 * This pixelref will ref() the specified colortable (if not NULL). 52 * 53 * Returns NULL on failure. 54 */ 55 typedef void (*ReleaseProc)(void* addr, void* context); 56 static SkMallocPixelRef* NewWithProc(const SkImageInfo& info, 57 size_t rowBytes, SkColorTable*, 58 void* addr, ReleaseProc proc, 59 void* context); 60 61 /** 62 * Return a new SkMallocPixelRef that will use the provided 63 * SkData, rowBytes, and optional colortable as pixel storage. 64 * The SkData will be ref()ed and on destruction of the PielRef, 65 * the SkData will be unref()ed. 66 * 67 * @param offset (in bytes) into the provided SkData that the 68 * first pixel is located at. 69 * 70 * This pixelref will ref() the specified colortable (if not NULL). 71 * 72 * Returns NULL on failure. 73 */ 74 static SkMallocPixelRef* NewWithData(const SkImageInfo& info, 75 size_t rowBytes, 76 SkColorTable* ctable, 77 SkData* data, 78 size_t offset = 0); 79 80 void* getAddr() const { return fStorage; } 81 82 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef) 83 84protected: 85 // The ownPixels version of this constructor is deprecated. 86 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, 87 bool ownPixels); 88 SkMallocPixelRef(SkFlattenableReadBuffer& buffer); 89 virtual ~SkMallocPixelRef(); 90 91 virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; 92 virtual void onUnlockPixels() SK_OVERRIDE; 93 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; 94 virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE; 95 96private: 97 void* fStorage; 98 SkColorTable* fCTable; 99 size_t fRB; 100 ReleaseProc fReleaseProc; 101 void* fReleaseProcContext; 102 103 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, 104 ReleaseProc proc, void* context); 105 106 typedef SkPixelRef INHERITED; 107}; 108 109 110#endif 111