SkMallocPixelRef.h revision a0d183d89160e31962e99475abe8a77c30626a40
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 the pixelref will not try
24     *  to delete the storage.
25     *
26     *  This pixelref will ref() the specified 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. If rowBytes are 0, an optimal value will be chosen automatically.
36     *  If rowBytes is > 0, then it will be respected, or NULL will be returned
37     *  if rowBytes is invalid for the specified info.
38     *
39     *  This pixelref will ref() the specified colortable (if not NULL).
40     *
41     *  Returns NULL on failure.
42     */
43    static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
44                                         size_t rowBytes, SkColorTable*);
45
46    void* getAddr() const { return fStorage; }
47
48    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
49
50protected:
51    virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
52    virtual void onUnlockPixels() SK_OVERRIDE;
53    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
54    virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
55
56    SkMallocPixelRef(SkFlattenableReadBuffer& buffer);
57    SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
58                     bool ownsPixels);
59    virtual ~SkMallocPixelRef();
60
61private:
62    void*               fStorage;
63    SkColorTable*       fCTable;
64    size_t              fRB;
65    const bool          fOwnPixels;
66
67    typedef SkPixelRef INHERITED;
68};
69
70
71#endif
72