epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 7 | |
reed@android.com | 49099b2 | 2010-09-09 16:07:53 +0000 | [diff] [blame] | 8 | #include "SkMallocPixelRef.h" |
| 9 | #include "SkBitmap.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
reed@android.com | 49099b2 | 2010-09-09 16:07:53 +0000 | [diff] [blame] | 12 | |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 13 | // assumes ptr was allocated via sk_malloc |
| 14 | static void sk_free_releaseproc(void* ptr, void*) { |
| 15 | sk_free(ptr); |
| 16 | } |
| 17 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 18 | static bool is_valid(const SkImageInfo& info) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 19 | if (info.width() < 0 || info.height() < 0 || |
| 20 | (unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType || |
| 21 | (unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 22 | { |
| 23 | return false; |
reed@android.com | 49099b2 | 2010-09-09 16:07:53 +0000 | [diff] [blame] | 24 | } |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 25 | return true; |
| 26 | } |
robertphillips@google.com | 0daa1ad | 2013-12-13 15:24:37 +0000 | [diff] [blame] | 27 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 28 | sk_sp<SkPixelRef> SkMallocPixelRef::MakeDirect(const SkImageInfo& info, |
| 29 | void* addr, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 30 | size_t rowBytes) { |
| 31 | if (!is_valid(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | return nullptr; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 33 | } |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 34 | return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, nullptr, nullptr)); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 35 | } |
| 36 | |
commit-bot@chromium.org | 2c4e75c | 2014-04-21 21:08:14 +0000 | [diff] [blame] | 37 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 38 | sk_sp<SkPixelRef> SkMallocPixelRef::MakeUsing(void*(*alloc)(size_t), |
| 39 | const SkImageInfo& info, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 40 | size_t requestedRowBytes) { |
| 41 | if (!is_valid(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | return nullptr; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 43 | } |
| 44 | |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 45 | // only want to permit 31bits of rowBytes |
| 46 | int64_t minRB = (int64_t)info.minRowBytes64(); |
| 47 | if (minRB < 0 || !sk_64_isS32(minRB)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | return nullptr; // allocation will be too large |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 49 | } |
| 50 | if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | return nullptr; // cannot meet requested rowbytes |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int32_t rowBytes; |
| 55 | if (requestedRowBytes) { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 56 | rowBytes = SkToS32(requestedRowBytes); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 57 | } else { |
| 58 | rowBytes = minRB; |
| 59 | } |
| 60 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 61 | int64_t bigSize = (int64_t)info.height() * rowBytes; |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 62 | if (!sk_64_isS32(bigSize)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | return nullptr; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 64 | } |
| 65 | |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 66 | size_t size = sk_64_asS32(bigSize); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 67 | SkASSERT(size >= info.getSafeSize(rowBytes)); |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 68 | void* addr = alloc(size); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | if (nullptr == addr) { |
| 70 | return nullptr; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 73 | return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 74 | sk_free_releaseproc, nullptr)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 77 | sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 78 | size_t rowBytes) { |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 79 | auto sk_malloc_nothrow = [](size_t size) { return sk_malloc_flags(size, 0); }; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 80 | return MakeUsing(sk_malloc_nothrow, info, rowBytes); |
mtklein | cd49541 | 2015-11-05 09:46:23 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 83 | sk_sp<SkPixelRef> SkMallocPixelRef::MakeZeroed(const SkImageInfo& info, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 84 | size_t rowBytes) { |
| 85 | return MakeUsing(sk_calloc, info, rowBytes); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void sk_data_releaseproc(void*, void* dataPtr) { |
| 89 | (static_cast<SkData*>(dataPtr))->unref(); |
| 90 | } |
| 91 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 92 | sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithProc(const SkImageInfo& info, |
| 93 | size_t rowBytes, |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 94 | void* addr, |
| 95 | SkMallocPixelRef::ReleaseProc proc, |
| 96 | void* context) { |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 97 | if (!is_valid(info)) { |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 98 | if (proc) { |
| 99 | proc(addr, context); |
| 100 | } |
| 101 | return nullptr; |
| 102 | } |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 103 | return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, proc, context)); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithData(const SkImageInfo& info, |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 107 | size_t rowBytes, |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 108 | sk_sp<SkData> data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 109 | SkASSERT(data != nullptr); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 110 | if (!is_valid(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | return nullptr; |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 112 | } |
Mike Reed | a00f347 | 2017-04-11 15:50:02 -0400 | [diff] [blame] | 113 | if ((rowBytes < info.minRowBytes()) || (data->size() < info.getSafeSize(rowBytes))) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | return nullptr; |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 115 | } |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 116 | // must get this address before we call release |
| 117 | void* pixels = const_cast<void*>(data->data()); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 118 | SkPixelRef* pr = new SkMallocPixelRef(info, pixels, rowBytes, |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 119 | sk_data_releaseproc, data.release()); |
| 120 | pr->setImmutable(); // since we were created with (immutable) data |
| 121 | return sk_sp<SkPixelRef>(pr); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /////////////////////////////////////////////////////////////////////////////// |
| 125 | |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 126 | SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 127 | size_t rowBytes, |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 128 | SkMallocPixelRef::ReleaseProc proc, |
| 129 | void* context) |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 130 | : INHERITED(info.width(), info.height(), storage, rowBytes) |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 131 | , fReleaseProc(proc) |
| 132 | , fReleaseProcContext(context) |
Mike Reed | a00f347 | 2017-04-11 15:50:02 -0400 | [diff] [blame] | 133 | {} |
reed@android.com | 49099b2 | 2010-09-09 16:07:53 +0000 | [diff] [blame] | 134 | |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 135 | |
reed@android.com | 49099b2 | 2010-09-09 16:07:53 +0000 | [diff] [blame] | 136 | SkMallocPixelRef::~SkMallocPixelRef() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | if (fReleaseProc != nullptr) { |
Mike Reed | a00f347 | 2017-04-11 15:50:02 -0400 | [diff] [blame] | 138 | fReleaseProc(this->pixels(), fReleaseProcContext); |
djsollen@google.com | c84b833 | 2012-07-27 13:41:44 +0000 | [diff] [blame] | 139 | } |
reed@android.com | 49099b2 | 2010-09-09 16:07:53 +0000 | [diff] [blame] | 140 | } |