SkMallocPixelRef.h revision cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8
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    /** Allocate the specified buffer for pixels. The memory is freed when the
21        last owner of this pixelref is gone. If addr is NULL, sk_malloc_throw()
22        is called to allocate it.
23     */
24    SkMallocPixelRef(void* addr, size_t size, SkColorTable* ctable, bool ownPixels = true);
25    virtual ~SkMallocPixelRef();
26
27    void* getAddr() const { return fStorage; }
28
29    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
30
31protected:
32    // overrides from SkPixelRef
33    virtual void* onLockPixels(SkColorTable**);
34    virtual void onUnlockPixels();
35
36    SkMallocPixelRef(SkFlattenableReadBuffer& buffer);
37    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
38
39    // Returns the allocation size for the pixels
40    virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE { return fSize; }
41
42private:
43    void*           fStorage;
44    size_t          fSize;
45    SkColorTable*   fCTable;
46    bool            fOwnPixels;
47
48    typedef SkPixelRef INHERITED;
49};
50
51
52#endif
53