OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkMallocPixelRef_DEFINED | 10 #ifndef SkMallocPixelRef_DEFINED |
11 #define SkMallocPixelRef_DEFINED | 11 #define SkMallocPixelRef_DEFINED |
12 | 12 |
13 #include "SkPixelRef.h" | 13 #include "SkPixelRef.h" |
14 | 14 |
15 /** We explicitly use the same allocator for our pixels that SkMask does, | 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. | 16 so that we can freely assign memory allocated by one class to the other. |
17 */ | 17 */ |
18 class SkMallocPixelRef : public SkPixelRef { | 18 class SkMallocPixelRef : public SkPixelRef { |
19 public: | 19 public: |
20 /** Allocate the specified buffer for pixels. The memory is freed when the | 20 /** |
21 last owner of this pixelref is gone. If addr is NULL, sk_malloc_throw() | 21 * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes, |
22 is called to allocate it. | 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. |
23 */ | 29 */ |
24 SkMallocPixelRef(void* addr, size_t size, SkColorTable* ctable, bool ownPixe
ls = true); | 30 static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr, |
25 virtual ~SkMallocPixelRef(); | 31 size_t rowBytes, SkColorTable*); |
26 | 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 |
27 void* getAddr() const { return fStorage; } | 48 void* getAddr() const { return fStorage; } |
28 | 49 |
29 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef) | 50 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef) |
30 | 51 |
31 protected: | 52 protected: |
32 // overrides from SkPixelRef | 53 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, |
33 virtual void* onLockPixels(SkColorTable**); | 54 bool ownPixels); |
34 virtual void onUnlockPixels(); | 55 SkMallocPixelRef(SkFlattenableReadBuffer& buffer); |
| 56 virtual ~SkMallocPixelRef(); |
35 | 57 |
36 SkMallocPixelRef(SkFlattenableReadBuffer& buffer); | 58 virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE; |
| 59 virtual void onUnlockPixels() SK_OVERRIDE; |
37 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; | 60 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
38 | 61 virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE; |
39 // Returns the allocation size for the pixels | |
40 virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE { return fSize; } | |
41 | 62 |
42 private: | 63 private: |
43 void* fStorage; | 64 void* fStorage; |
44 size_t fSize; | |
45 SkColorTable* fCTable; | 65 SkColorTable* fCTable; |
46 bool fOwnPixels; | 66 size_t fRB; |
| 67 const bool fOwnPixels; |
47 | 68 |
48 typedef SkPixelRef INHERITED; | 69 typedef SkPixelRef INHERITED; |
49 }; | 70 }; |
50 | 71 |
51 | 72 |
52 #endif | 73 #endif |
OLD | NEW |