blob: 807eb06b309bf4d6b30c28a78bf278058b2fe51d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.combf790232013-12-13 19:45:58 +00007
reed@android.com49099b22010-09-09 16:07:53 +00008#include "SkMallocPixelRef.h"
9#include "SkBitmap.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
reed@android.com49099b22010-09-09 16:07:53 +000012
halcanary@google.com1bed6872014-01-02 17:29:28 +000013// assumes ptr was allocated via sk_malloc
14static void sk_free_releaseproc(void* ptr, void*) {
15 sk_free(ptr);
16}
17
Mike Reed086a4272017-07-18 10:53:11 -040018static bool is_valid(const SkImageInfo& info) {
reede5ea5002014-09-03 11:54:58 -070019 if (info.width() < 0 || info.height() < 0 ||
20 (unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType ||
21 (unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType)
reed@google.combf790232013-12-13 19:45:58 +000022 {
23 return false;
reed@android.com49099b22010-09-09 16:07:53 +000024 }
reed@google.combf790232013-12-13 19:45:58 +000025 return true;
26}
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000027
Mike Reed6b3155c2017-04-03 14:41:44 -040028sk_sp<SkPixelRef> SkMallocPixelRef::MakeDirect(const SkImageInfo& info,
29 void* addr,
Mike Reed086a4272017-07-18 10:53:11 -040030 size_t rowBytes) {
31 if (!is_valid(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -070032 return nullptr;
reed@google.combf790232013-12-13 19:45:58 +000033 }
Mike Reed086a4272017-07-18 10:53:11 -040034 return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, nullptr, nullptr));
reed@google.combf790232013-12-13 19:45:58 +000035}
36
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +000037
Mike Reedcd284c52017-09-29 15:22:56 -040038sk_sp<SkPixelRef> SkMallocPixelRef::MakeUsing(void*(*allocProc)(size_t),
39 const SkImageInfo& info,
40 size_t rowBytes) {
41 if (rowBytes == 0) {
42 rowBytes = info.minRowBytes();
43 }
44
45 if (!is_valid(info) || !info.validRowBytes(rowBytes)) {
halcanary96fcdcc2015-08-27 07:41:13 -070046 return nullptr;
reed@google.combf790232013-12-13 19:45:58 +000047 }
48
Mike Reedcd284c52017-09-29 15:22:56 -040049 size_t size = 0;
50 // if the info is empty, or rowBytes is 0 (which can be valid), then we don't need to compute
51 // a size.
52 if (!info.isEmpty() && rowBytes > 0) {
53#ifdef SK_SUPPORT_LEGACY_SAFESIZE64
54 int64_t bigSize = (int64_t)info.height() * rowBytes;
55 if (!sk_64_isS32(bigSize)) {
56 return nullptr;
57 }
58
59 size = sk_64_asS32(bigSize);
60 SkASSERT(size >= info.getSafeSize(rowBytes));
61 SkASSERT(info.computeByteSize(rowBytes) == info.getSafeSize(rowBytes));
62#else
63 size = info.computeByteSize(rowBytes);
64#endif
65 if (size == 0) {
66 return nullptr; // overflow
67 }
reed@google.combf790232013-12-13 19:45:58 +000068 }
69
Mike Reedcd284c52017-09-29 15:22:56 -040070 void* addr = allocProc(size);
halcanary96fcdcc2015-08-27 07:41:13 -070071 if (nullptr == addr) {
72 return nullptr;
reed@google.combf790232013-12-13 19:45:58 +000073 }
74
Mike Reede74dafc2017-09-29 13:41:58 -040075 return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes,
76 sk_free_releaseproc, nullptr));
halcanary@google.com1bed6872014-01-02 17:29:28 +000077}
78
Mike Reed6b3155c2017-04-03 14:41:44 -040079sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info,
Mike Reed086a4272017-07-18 10:53:11 -040080 size_t rowBytes) {
mtkleincd495412015-11-05 09:46:23 -080081 auto sk_malloc_nothrow = [](size_t size) { return sk_malloc_flags(size, 0); };
Mike Reed086a4272017-07-18 10:53:11 -040082 return MakeUsing(sk_malloc_nothrow, info, rowBytes);
mtkleincd495412015-11-05 09:46:23 -080083}
84
Mike Reed6b3155c2017-04-03 14:41:44 -040085sk_sp<SkPixelRef> SkMallocPixelRef::MakeZeroed(const SkImageInfo& info,
Mike Reed086a4272017-07-18 10:53:11 -040086 size_t rowBytes) {
87 return MakeUsing(sk_calloc, info, rowBytes);
halcanary@google.com1bed6872014-01-02 17:29:28 +000088}
89
90static void sk_data_releaseproc(void*, void* dataPtr) {
91 (static_cast<SkData*>(dataPtr))->unref();
92}
93
Mike Reed6b3155c2017-04-03 14:41:44 -040094sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithProc(const SkImageInfo& info,
95 size_t rowBytes,
Mike Reed6b3155c2017-04-03 14:41:44 -040096 void* addr,
97 SkMallocPixelRef::ReleaseProc proc,
98 void* context) {
Mike Reed086a4272017-07-18 10:53:11 -040099 if (!is_valid(info)) {
Mike Reed6b3155c2017-04-03 14:41:44 -0400100 if (proc) {
101 proc(addr, context);
102 }
103 return nullptr;
104 }
Mike Reed086a4272017-07-18 10:53:11 -0400105 return sk_sp<SkPixelRef>(new SkMallocPixelRef(info, addr, rowBytes, proc, context));
Mike Reed6b3155c2017-04-03 14:41:44 -0400106}
107
108sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithData(const SkImageInfo& info,
halcanary@google.com1bed6872014-01-02 17:29:28 +0000109 size_t rowBytes,
Mike Reed6b3155c2017-04-03 14:41:44 -0400110 sk_sp<SkData> data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 SkASSERT(data != nullptr);
Mike Reedcd284c52017-09-29 15:22:56 -0400112 if (!is_valid(info) || !info.validRowBytes(rowBytes)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700113 return nullptr;
halcanary@google.com1bed6872014-01-02 17:29:28 +0000114 }
Mike Reedcd284c52017-09-29 15:22:56 -0400115 size_t sizeNeeded = info.computeByteSize(rowBytes);
116 if (!info.isEmpty() && (sizeNeeded == 0 || data->size() < sizeNeeded)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700117 return nullptr;
halcanary@google.com1bed6872014-01-02 17:29:28 +0000118 }
Mike Reed6b3155c2017-04-03 14:41:44 -0400119 // must get this address before we call release
120 void* pixels = const_cast<void*>(data->data());
Mike Reed086a4272017-07-18 10:53:11 -0400121 SkPixelRef* pr = new SkMallocPixelRef(info, pixels, rowBytes,
Mike Reed6b3155c2017-04-03 14:41:44 -0400122 sk_data_releaseproc, data.release());
123 pr->setImmutable(); // since we were created with (immutable) data
124 return sk_sp<SkPixelRef>(pr);
reed@google.combf790232013-12-13 19:45:58 +0000125}
126
127///////////////////////////////////////////////////////////////////////////////
128
halcanary@google.com1bed6872014-01-02 17:29:28 +0000129SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
Mike Reed086a4272017-07-18 10:53:11 -0400130 size_t rowBytes,
halcanary@google.com1bed6872014-01-02 17:29:28 +0000131 SkMallocPixelRef::ReleaseProc proc,
132 void* context)
Mike Reed086a4272017-07-18 10:53:11 -0400133 : INHERITED(info.width(), info.height(), storage, rowBytes)
halcanary@google.com1bed6872014-01-02 17:29:28 +0000134 , fReleaseProc(proc)
135 , fReleaseProcContext(context)
Mike Reeda00f3472017-04-11 15:50:02 -0400136{}
reed@android.com49099b22010-09-09 16:07:53 +0000137
halcanary@google.com1bed6872014-01-02 17:29:28 +0000138
reed@android.com49099b22010-09-09 16:07:53 +0000139SkMallocPixelRef::~SkMallocPixelRef() {
halcanary96fcdcc2015-08-27 07:41:13 -0700140 if (fReleaseProc != nullptr) {
Mike Reeda00f3472017-04-11 15:50:02 -0400141 fReleaseProc(this->pixels(), fReleaseProcContext);
djsollen@google.comc84b8332012-07-27 13:41:44 +0000142 }
reed@android.com49099b22010-09-09 16:07:53 +0000143}