2 * Copyright 2008 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #ifndef SkMallocPixelRef_DEFINED
10 #define SkMallocPixelRef_DEFINED
12 #include "SkPixelRef.h"
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.
17 class SK_API SkMallocPixelRef : public SkPixelRef {
19 SK_DECLARE_INST_COUNT(SkMallocPixelRef)
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
26 * The pixelref will ref() the colortable (if not NULL).
28 * Returns NULL on failure.
30 static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr,
31 size_t rowBytes, SkColorTable*);
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.
39 * This pixelref will ref() the specified colortable (if not NULL).
41 * Returns NULL on failure.
43 static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
44 size_t rowBytes, SkColorTable*);
47 * Return a new SkMallocPixelRef with the provided pixel storage,
48 * rowBytes, and optional colortable. On destruction, ReleaseProc
51 * This pixelref will ref() the specified colortable (if not NULL).
53 * Returns NULL on failure.
55 typedef void (*ReleaseProc)(void* addr, void* context);
56 static SkMallocPixelRef* NewWithProc(const SkImageInfo& info,
57 size_t rowBytes, SkColorTable*,
58 void* addr, ReleaseProc proc,
62 * Return a new SkMallocPixelRef that will use the provided
63 * SkData, rowBytes, and optional colortable as pixel storage.
64 * The SkData will be ref()ed and on destruction of the PielRef,
65 * the SkData will be unref()ed.
67 * @param offset (in bytes) into the provided SkData that the
68 * first pixel is located at.
70 * This pixelref will ref() the specified colortable (if not NULL).
72 * Returns NULL on failure.
74 static SkMallocPixelRef* NewWithData(const SkImageInfo& info,
80 void* getAddr() const { return fStorage; }
82 class PRFactory : public SkPixelRefFactory {
84 virtual SkPixelRef* create(const SkImageInfo&,
85 SkColorTable*) SK_OVERRIDE;
88 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
91 // The ownPixels version of this constructor is deprecated.
92 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
94 SkMallocPixelRef(SkReadBuffer& buffer);
95 virtual ~SkMallocPixelRef();
97 virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
98 virtual void onUnlockPixels() SK_OVERRIDE;
99 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
100 virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
104 SkColorTable* fCTable;
106 ReleaseProc fReleaseProc;
107 void* fReleaseProcContext;
109 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
110 ReleaseProc proc, void* context);
112 typedef SkPixelRef INHERITED;