SkMallocPixelRef.h revision 3aa7a02b65d3798b819abd7c12a0327a6adcabcd
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     *  The pixelref will ref() the colortable (if not NULL).
26     *
27     *  Returns NULL on failure.
28     */
29    static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr,
30                                       size_t rowBytes, SkColorTable*);
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 SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
43                                         size_t rowBytes, SkColorTable*);
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     *  This pixelref will ref() the specified colortable (if not NULL).
51     *
52     *  Returns NULL on failure.
53     */
54    typedef void (*ReleaseProc)(void* addr, void* context);
55    static SkMallocPixelRef* NewWithProc(const SkImageInfo& info,
56                                         size_t rowBytes, SkColorTable*,
57                                         void* addr, ReleaseProc proc,
58                                         void* context);
59
60    /**
61     *  Return a new SkMallocPixelRef that will use the provided
62     *  SkData, rowBytes, and optional colortable as pixel storage.
63     *  The SkData will be ref()ed and on destruction of the PielRef,
64     *  the SkData will be unref()ed.
65     *
66     *  @param offset (in bytes) into the provided SkData that the
67     *         first pixel is located at.
68     *
69     *  This pixelref will ref() the specified colortable (if not NULL).
70     *
71     *  Returns NULL on failure.
72     */
73    static SkMallocPixelRef* NewWithData(const SkImageInfo& info,
74                                         size_t rowBytes,
75                                         SkColorTable* ctable,
76                                         SkData* data,
77                                         size_t offset = 0);
78
79    void* getAddr() const { return fStorage; }
80
81    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
82
83protected:
84    // The ownPixels version of this constructor is deprecated.
85    SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
86                     bool ownPixels);
87    SkMallocPixelRef(SkFlattenableReadBuffer& buffer);
88    virtual ~SkMallocPixelRef();
89
90    virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
91    virtual void onUnlockPixels() SK_OVERRIDE;
92    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
93    virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
94
95private:
96    void*           fStorage;
97    SkColorTable*   fCTable;
98    size_t          fRB;
99    ReleaseProc     fReleaseProc;
100    void*           fReleaseProcContext;
101
102    SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
103                     ReleaseProc proc, void* context);
104
105    typedef SkPixelRef INHERITED;
106};
107
108
109#endif
110