1
2/*
3 * Copyright 2008 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkMallocPixelRef_DEFINED
11#define SkMallocPixelRef_DEFINED
12
13#include "SkPixelRef.h"
14
15/** We explicitly use the same allocator for our pixels that SkMask does,
16    so that we can freely assign memory allocated by one class to the other.
17*/
18class SkMallocPixelRef : public SkPixelRef {
19public:
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.
36     *
37     *  If rowBytes is 0, an optimal value will be chosen automatically.
38     *  If rowBytes is > 0, then it will be used, unless it is invald for the
39     *  specified info, in which case NULL will be returned (failure).
40     *
41     *  This pixelref will ref() the specified colortable (if not NULL).
42     *
43     *  Returns NULL on failure.
44     */
45    static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
46                                         size_t rowBytes, SkColorTable*);
47
48    void* getAddr() const { return fStorage; }
49
50    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
51
52protected:
53    SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
54                     bool ownPixels);
55    SkMallocPixelRef(SkFlattenableReadBuffer& buffer);
56    virtual ~SkMallocPixelRef();
57
58    virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE;
59    virtual void onUnlockPixels() SK_OVERRIDE;
60    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
61    virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
62
63private:
64    void*           fStorage;
65    SkColorTable*   fCTable;
66    size_t          fRB;
67    const bool      fOwnPixels;
68
69    typedef SkPixelRef INHERITED;
70};
71
72
73#endif
74