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 * If ReleaseProc is NULL, the pixels will never be released. This 54 * can be useful if the pixels were stack allocated. However, such an 55 * SkMallocPixelRef must not live beyond its pixels (e.g. by copying 56 * an SkBitmap pointing to it, or drawing to an SkPicture). 57 * 58 * Returns NULL on failure. 59 */ 60 typedef void (*ReleaseProc)(void* addr, void* context); 61 static SkMallocPixelRef* NewWithProc(const SkImageInfo& info, 62 size_t rowBytes, SkColorTable*, 63 void* addr, ReleaseProc proc, 64 void* context); 65 66 /** 67 * Return a new SkMallocPixelRef that will use the provided 68 * SkData, rowBytes, and optional colortable as pixel storage. 69 * The SkData will be ref()ed and on destruction of the PielRef, 70 * the SkData will be unref()ed. 71 * 72 * This pixelref will ref() the specified colortable (if not NULL). 73 * 74 * Returns NULL on failure. 75 */ 76 static SkMallocPixelRef* NewWithData(const SkImageInfo& info, 77 size_t rowBytes, 78 SkColorTable* ctable, 79 SkData* data); 80 getAddr()81 void* getAddr() const { return fStorage; } 82 83 class PRFactory : public SkPixelRefFactory { 84 public: 85 virtual SkPixelRef* create(const SkImageInfo&, 86 size_t rowBytes, 87 SkColorTable*) override; 88 }; 89 90 protected: 91 // The ownPixels version of this constructor is deprecated. 92 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, 93 bool ownPixels); 94 virtual ~SkMallocPixelRef(); 95 96 bool onNewLockPixels(LockRec*) override; 97 void onUnlockPixels() override; 98 size_t getAllocatedSizeInBytes() const override; 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