1 /* 2 * Copyright 2008 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 9 #ifndef SkMallocPixelRef_DEFINED 10 #define SkMallocPixelRef_DEFINED 11 12 #include "SkPixelRef.h" 13 14 /** We explicitly use the same allocator for our pixels that SkMask does, 15 so that we can freely assign memory allocated by one class to the other. 16 */ 17 class SK_API SkMallocPixelRef : public SkPixelRef { 18 public: 19 SK_DECLARE_INST_COUNT(SkMallocPixelRef) 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 * This pixelref will ref() the specified colortable (if not NULL). 68 * 69 * Returns NULL on failure. 70 */ 71 static SkMallocPixelRef* NewWithData(const SkImageInfo& info, 72 size_t rowBytes, 73 SkColorTable* ctable, 74 SkData* data); 75 getAddr()76 void* getAddr() const { return fStorage; } 77 78 class PRFactory : public SkPixelRefFactory { 79 public: 80 virtual SkPixelRef* create(const SkImageInfo&, 81 SkColorTable*) SK_OVERRIDE; 82 }; 83 84 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef) 85 86 protected: 87 // The ownPixels version of this constructor is deprecated. 88 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, 89 bool ownPixels); 90 SkMallocPixelRef(SkReadBuffer& buffer); 91 virtual ~SkMallocPixelRef(); 92 93 virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; 94 virtual void onUnlockPixels() SK_OVERRIDE; 95 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 96 virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE; 97 rowBytes()98 size_t rowBytes() const { return fRB; } 99 100 private: 101 void* fStorage; 102 SkColorTable* fCTable; 103 size_t fRB; 104 ReleaseProc fReleaseProc; 105 void* fReleaseProcContext; 106 107 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, 108 ReleaseProc proc, void* context); 109 110 typedef SkPixelRef INHERITED; 111 }; 112 113 114 #endif 115