reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #ifndef SkMallocPixelRef_DEFINED |
| 10 | #define SkMallocPixelRef_DEFINED |
| 11 | |
| 12 | #include "SkPixelRef.h" |
| 13 | |
| 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. |
| 16 | */ |
commit-bot@chromium.org | 3aa7a02 | 2014-01-16 14:46:16 +0000 | [diff] [blame] | 17 | class SK_API SkMallocPixelRef : public SkPixelRef { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | public: |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes, |
| 21 | * and optional colortable. The caller is responsible for managing the |
| 22 | * lifetime of the pixel storage buffer, as this pixelref will not try |
| 23 | * to delete it. |
| 24 | * |
| 25 | * The pixelref will ref() the colortable (if not NULL). |
| 26 | * |
| 27 | * Returns NULL on failure. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | */ |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 29 | static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr, |
| 30 | size_t rowBytes, SkColorTable*); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 31 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Return a new SkMallocPixelRef, automatically allocating storage for the |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 34 | * pixels. If rowBytes are 0, an optimal value will be chosen automatically. |
| 35 | * If rowBytes is > 0, then it will be respected, or NULL will be returned |
| 36 | * if rowBytes is invalid for the specified info. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 37 | * |
| 38 | * This pixelref will ref() the specified colortable (if not NULL). |
| 39 | * |
| 40 | * Returns NULL on failure. |
| 41 | */ |
| 42 | static SkMallocPixelRef* NewAllocate(const SkImageInfo& info, |
| 43 | size_t rowBytes, SkColorTable*); |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 44 | |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 45 | /** |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 46 | * Identical to NewAllocate, except all pixel bytes are zeroed. |
| 47 | */ |
| 48 | static SkMallocPixelRef* NewZeroed(const SkImageInfo& info, |
| 49 | size_t rowBytes, SkColorTable*); |
| 50 | |
| 51 | /** |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 52 | * Return a new SkMallocPixelRef with the provided pixel storage, |
| 53 | * rowBytes, and optional colortable. On destruction, ReleaseProc |
| 54 | * will be called. |
| 55 | * |
| 56 | * This pixelref will ref() the specified colortable (if not NULL). |
| 57 | * |
scroggo | 527b287 | 2015-01-29 13:52:13 -0800 | [diff] [blame] | 58 | * If ReleaseProc is NULL, the pixels will never be released. This |
| 59 | * can be useful if the pixels were stack allocated. However, such an |
| 60 | * SkMallocPixelRef must not live beyond its pixels (e.g. by copying |
| 61 | * an SkBitmap pointing to it, or drawing to an SkPicture). |
| 62 | * |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 63 | * Returns NULL on failure. |
| 64 | */ |
| 65 | typedef void (*ReleaseProc)(void* addr, void* context); |
| 66 | static SkMallocPixelRef* NewWithProc(const SkImageInfo& info, |
| 67 | size_t rowBytes, SkColorTable*, |
| 68 | void* addr, ReleaseProc proc, |
| 69 | void* context); |
| 70 | |
| 71 | /** |
| 72 | * Return a new SkMallocPixelRef that will use the provided |
| 73 | * SkData, rowBytes, and optional colortable as pixel storage. |
| 74 | * The SkData will be ref()ed and on destruction of the PielRef, |
| 75 | * the SkData will be unref()ed. |
| 76 | * |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 77 | * This pixelref will ref() the specified colortable (if not NULL). |
| 78 | * |
| 79 | * Returns NULL on failure. |
| 80 | */ |
| 81 | static SkMallocPixelRef* NewWithData(const SkImageInfo& info, |
| 82 | size_t rowBytes, |
| 83 | SkColorTable* ctable, |
commit-bot@chromium.org | 2c4e75c | 2014-04-21 21:08:14 +0000 | [diff] [blame] | 84 | SkData* data); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 85 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 86 | void* getAddr() const { return fStorage; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 88 | class PRFactory : public SkPixelRefFactory { |
| 89 | public: |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 90 | SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) override; |
| 91 | }; |
| 92 | |
| 93 | class ZeroedPRFactory : public SkPixelRefFactory { |
| 94 | public: |
| 95 | SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) override; |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | protected: |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 99 | // The ownPixels version of this constructor is deprecated. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 100 | SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, |
| 101 | bool ownPixels); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 102 | virtual ~SkMallocPixelRef(); |
robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 103 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 104 | bool onNewLockPixels(LockRec*) override; |
| 105 | void onUnlockPixels() override; |
| 106 | size_t getAllocatedSizeInBytes() const override; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 107 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | private: |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 109 | // Uses alloc to implement NewAllocate or NewZeroed. |
| 110 | static SkMallocPixelRef* NewUsing(void*(*alloc)(size_t), |
| 111 | const SkImageInfo&, |
| 112 | size_t rowBytes, |
| 113 | SkColorTable*); |
| 114 | |
robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 115 | void* fStorage; |
robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 116 | SkColorTable* fCTable; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 117 | size_t fRB; |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 118 | ReleaseProc fReleaseProc; |
| 119 | void* fReleaseProcContext; |
| 120 | |
| 121 | SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*, |
| 122 | ReleaseProc proc, void* context); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | |
| 124 | typedef SkPixelRef INHERITED; |
| 125 | }; |
| 126 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 127 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | #endif |