OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMallocPixelRef.h" | 8 #include "SkMallocPixelRef.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 void* addr, | 41 void* addr, |
42 size_t rowBytes, | 42 size_t rowBytes, |
43 SkColorTable* ctable) { | 43 SkColorTable* ctable) { |
44 if (!is_valid(info, ctable)) { | 44 if (!is_valid(info, ctable)) { |
45 return nullptr; | 45 return nullptr; |
46 } | 46 } |
47 return new SkMallocPixelRef(info, addr, rowBytes, ctable, nullptr, nullptr); | 47 return new SkMallocPixelRef(info, addr, rowBytes, ctable, nullptr, nullptr); |
48 } | 48 } |
49 | 49 |
50 | 50 |
51 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info, | 51 SkMallocPixelRef* SkMallocPixelRef::NewUsing(void*(*alloc)(size_t), |
52 size_t requestedRowBytes, | 52 const SkImageInfo& info, |
53 SkColorTable* ctable) { | 53 size_t requestedRowBytes, |
| 54 SkColorTable* ctable) { |
54 if (!is_valid(info, ctable)) { | 55 if (!is_valid(info, ctable)) { |
55 return nullptr; | 56 return nullptr; |
56 } | 57 } |
57 | 58 |
58 // only want to permit 31bits of rowBytes | 59 // only want to permit 31bits of rowBytes |
59 int64_t minRB = (int64_t)info.minRowBytes64(); | 60 int64_t minRB = (int64_t)info.minRowBytes64(); |
60 if (minRB < 0 || !sk_64_isS32(minRB)) { | 61 if (minRB < 0 || !sk_64_isS32(minRB)) { |
61 return nullptr; // allocation will be too large | 62 return nullptr; // allocation will be too large |
62 } | 63 } |
63 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) { | 64 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) { |
64 return nullptr; // cannot meet requested rowbytes | 65 return nullptr; // cannot meet requested rowbytes |
65 } | 66 } |
66 | 67 |
67 int32_t rowBytes; | 68 int32_t rowBytes; |
68 if (requestedRowBytes) { | 69 if (requestedRowBytes) { |
69 rowBytes = SkToS32(requestedRowBytes); | 70 rowBytes = SkToS32(requestedRowBytes); |
70 } else { | 71 } else { |
71 rowBytes = minRB; | 72 rowBytes = minRB; |
72 } | 73 } |
73 | 74 |
74 int64_t bigSize = (int64_t)info.height() * rowBytes; | 75 int64_t bigSize = (int64_t)info.height() * rowBytes; |
75 if (!sk_64_isS32(bigSize)) { | 76 if (!sk_64_isS32(bigSize)) { |
76 return nullptr; | 77 return nullptr; |
77 } | 78 } |
78 | 79 |
79 size_t size = sk_64_asS32(bigSize); | 80 size_t size = sk_64_asS32(bigSize); |
80 SkASSERT(size >= info.getSafeSize(rowBytes)); | 81 SkASSERT(size >= info.getSafeSize(rowBytes)); |
81 void* addr = sk_malloc_flags(size, 0); | 82 void* addr = alloc(size); |
82 if (nullptr == addr) { | 83 if (nullptr == addr) { |
83 return nullptr; | 84 return nullptr; |
84 } | 85 } |
85 | 86 |
86 return new SkMallocPixelRef(info, addr, rowBytes, ctable, sk_free_releasepro
c, nullptr); | 87 return new SkMallocPixelRef(info, addr, rowBytes, ctable, sk_free_releasepro
c, nullptr); |
87 } | 88 } |
88 | 89 |
| 90 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info, |
| 91 size_t rowBytes, |
| 92 SkColorTable* ctable) { |
| 93 auto sk_malloc_nothrow = [](size_t size) { return sk_malloc_flags(size, 0);
}; |
| 94 return NewUsing(sk_malloc_nothrow, info, rowBytes, ctable); |
| 95 } |
| 96 |
| 97 SkMallocPixelRef* SkMallocPixelRef::NewZeroed(const SkImageInfo& info, |
| 98 size_t rowBytes, |
| 99 SkColorTable* ctable) { |
| 100 return NewUsing(sk_calloc, info, rowBytes, ctable); |
| 101 } |
| 102 |
89 SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info, | 103 SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info, |
90 size_t rowBytes, | 104 size_t rowBytes, |
91 SkColorTable* ctable, | 105 SkColorTable* ctable, |
92 void* addr, | 106 void* addr, |
93 SkMallocPixelRef::ReleaseProc pr
oc, | 107 SkMallocPixelRef::ReleaseProc pr
oc, |
94 void* context) { | 108 void* context) { |
95 if (!is_valid(info, ctable)) { | 109 if (!is_valid(info, ctable)) { |
96 return nullptr; | 110 return nullptr; |
97 } | 111 } |
98 return new SkMallocPixelRef(info, addr, rowBytes, ctable, proc, context); | 112 return new SkMallocPixelRef(info, addr, rowBytes, ctable, proc, context); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { | 208 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { |
195 return this->info().getSafeSize(fRB); | 209 return this->info().getSafeSize(fRB); |
196 } | 210 } |
197 | 211 |
198 /////////////////////////////////////////////////////////////////////////////// | 212 /////////////////////////////////////////////////////////////////////////////// |
199 | 213 |
200 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, size_t
rowBytes, | 214 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, size_t
rowBytes, |
201 SkColorTable* ctable) { | 215 SkColorTable* ctable) { |
202 return SkMallocPixelRef::NewAllocate(info, rowBytes, ctable); | 216 return SkMallocPixelRef::NewAllocate(info, rowBytes, ctable); |
203 } | 217 } |
| 218 |
| 219 SkPixelRef* SkMallocPixelRef::ZeroedPRFactory::create(const SkImageInfo& info, s
ize_t rowBytes, |
| 220 SkColorTable* ctable) { |
| 221 return SkMallocPixelRef::NewZeroed(info, rowBytes, ctable); |
| 222 } |
OLD | NEW |