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