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 #ifndef SkMallocPixelRef_DEFINED 9 #define SkMallocPixelRef_DEFINED 10 11 #include "SkPixelRef.h" 12 #include "SkRefCnt.h" 13 #include "SkTypes.h" 14 class SkData; 15 struct SkImageInfo; 16 17 /** We explicitly use the same allocator for our pixels that SkMask does, 18 so that we can freely assign memory allocated by one class to the other. 19 */ 20 class SK_API SkMallocPixelRef : public SkPixelRef { 21 public: 22 /** 23 * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes, 24 * and optional colortable. The caller is responsible for managing the 25 * lifetime of the pixel storage buffer, as this pixelref will not try 26 * to delete it. 27 * 28 * Returns NULL on failure. 29 */ 30 static sk_sp<SkPixelRef> MakeDirect(const SkImageInfo&, void* addr, size_t rowBytes); 31 32 /** 33 * Return a new SkMallocPixelRef, automatically allocating storage for the 34 * pixels. If rowBytes are 0, an optimal value will be chosen automatically. 35 * If rowBytes is > 0, then it will be respected, or NULL will be returned 36 * if rowBytes is invalid for the specified info. 37 * 38 * This pixelref will ref() the specified colortable (if not NULL). 39 * 40 * Returns NULL on failure. 41 */ 42 static sk_sp<SkPixelRef> MakeAllocate(const SkImageInfo&, size_t rowBytes); 43 44 /** 45 * Identical to MakeAllocate, except all pixel bytes are zeroed. 46 */ 47 static sk_sp<SkPixelRef> MakeZeroed(const SkImageInfo&, size_t rowBytes); 48 49 /** 50 * Return a new SkMallocPixelRef with the provided pixel storage, 51 * rowBytes, and optional colortable. On destruction, ReleaseProc 52 * will be called. 53 * 54 * If ReleaseProc is NULL, the pixels will never be released. This 55 * can be useful if the pixels were stack allocated. However, such an 56 * SkMallocPixelRef must not live beyond its pixels (e.g. by copying 57 * an SkBitmap pointing to it, or drawing to an SkPicture). 58 * 59 * Returns NULL on failure. 60 */ 61 typedef void (*ReleaseProc)(void* addr, void* context); 62 static sk_sp<SkPixelRef> MakeWithProc(const SkImageInfo& info, size_t rowBytes, void* addr, 63 ReleaseProc proc, void* context); 64 65 /** 66 * Return a new SkMallocPixelRef that will use the provided 67 * SkData, rowBytes, and optional colortable as pixel storage. 68 * The SkData will be ref()ed and on destruction of the PielRef, 69 * the SkData will be unref()ed. 70 * 71 * Returns NULL on failure. 72 */ 73 static sk_sp<SkPixelRef> MakeWithData(const SkImageInfo&, size_t rowBytes, sk_sp<SkData> data); 74 75 protected: 76 ~SkMallocPixelRef() override; 77 78 private: 79 // Uses alloc to implement NewAllocate or NewZeroed. 80 static sk_sp<SkPixelRef> MakeUsing(void*(*alloc)(size_t), 81 const SkImageInfo&, 82 size_t rowBytes); 83 84 ReleaseProc fReleaseProc; 85 void* fReleaseProcContext; 86 87 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, ReleaseProc proc, void* context); 88 89 typedef SkPixelRef INHERITED; 90 }; 91 92 93 #endif 94