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