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 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkDither.h" | 12 #include "SkDither.h" |
13 #include "SkFlattenable.h" | 13 #include "SkFlattenable.h" |
14 #include "SkImagePriv.h" | 14 #include "SkImagePriv.h" |
15 #include "SkMallocPixelRef.h" | 15 #include "SkMallocPixelRef.h" |
16 #include "SkMask.h" | 16 #include "SkMask.h" |
17 #include "SkOrderedReadBuffer.h" | 17 #include "SkOrderedReadBuffer.h" |
18 #include "SkOrderedWriteBuffer.h" | 18 #include "SkOrderedWriteBuffer.h" |
19 #include "SkPixelRef.h" | 19 #include "SkPixelRef.h" |
20 #include "SkThread.h" | 20 #include "SkThread.h" |
21 #include "SkUnPreMultiply.h" | 21 #include "SkUnPreMultiply.h" |
22 #include "SkUtils.h" | 22 #include "SkUtils.h" |
23 #include "SkValidationUtils.h" | 23 #include "SkValidationUtils.h" |
24 #include "SkPackBits.h" | 24 #include "SkPackBits.h" |
25 #include <new> | 25 #include <new> |
26 | 26 |
| 27 static bool isPos32Bits(const Sk64& value) { |
| 28 return !value.isNeg() && value.is32(); |
| 29 } |
| 30 |
27 struct MipLevel { | 31 struct MipLevel { |
28 void* fPixels; | 32 void* fPixels; |
29 uint32_t fRowBytes; | 33 uint32_t fRowBytes; |
30 uint32_t fWidth, fHeight; | 34 uint32_t fWidth, fHeight; |
31 }; | 35 }; |
32 | 36 |
33 struct SkBitmap::MipMap : SkNoncopyable { | 37 struct SkBitmap::MipMap : SkNoncopyable { |
34 int32_t fRefCnt; | 38 int32_t fRefCnt; |
35 int fLevelCount; | 39 int fLevelCount; |
36 // MipLevel fLevel[fLevelCount]; | 40 // MipLevel fLevel[fLevelCount]; |
37 // Pixels[] | 41 // Pixels[] |
38 | 42 |
39 static MipMap* Alloc(int levelCount, size_t pixelSize) { | 43 static MipMap* Alloc(int levelCount, size_t pixelSize) { |
40 if (levelCount < 0) { | 44 if (levelCount < 0) { |
41 return NULL; | 45 return NULL; |
42 } | 46 } |
43 int64_t size = (levelCount + 1) * sizeof(MipLevel); | 47 Sk64 size; |
44 size += sizeof(MipMap) + pixelSize; | 48 size.setMul(levelCount + 1, sizeof(MipLevel)); |
45 if (!sk_64_isS32(size)) { | 49 size.add(sizeof(MipMap)); |
| 50 size.add(SkToS32(pixelSize)); |
| 51 if (!isPos32Bits(size)) { |
46 return NULL; | 52 return NULL; |
47 } | 53 } |
48 MipMap* mm = (MipMap*)sk_malloc_throw(sk_64_asS32(size)); | 54 MipMap* mm = (MipMap*)sk_malloc_throw(size.get32()); |
49 mm->fRefCnt = 1; | 55 mm->fRefCnt = 1; |
50 mm->fLevelCount = levelCount; | 56 mm->fLevelCount = levelCount; |
51 return mm; | 57 return mm; |
52 } | 58 } |
53 | 59 |
54 const MipLevel* levels() const { return (const MipLevel*)(this + 1); } | 60 const MipLevel* levels() const { return (const MipLevel*)(this + 1); } |
55 MipLevel* levels() { return (MipLevel*)(this + 1); } | 61 MipLevel* levels() { return (MipLevel*)(this + 1); } |
56 | 62 |
57 const void* pixels() const { return levels() + fLevelCount; } | 63 const void* pixels() const { return levels() + fLevelCount; } |
58 void* pixels() { return levels() + fLevelCount; } | 64 void* pixels() { return levels() + fLevelCount; } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 break; | 178 break; |
173 } | 179 } |
174 return bpp; | 180 return bpp; |
175 } | 181 } |
176 | 182 |
177 size_t SkBitmap::ComputeRowBytes(Config c, int width) { | 183 size_t SkBitmap::ComputeRowBytes(Config c, int width) { |
178 if (width < 0) { | 184 if (width < 0) { |
179 return 0; | 185 return 0; |
180 } | 186 } |
181 | 187 |
182 int64_t rowBytes = 0; | 188 Sk64 rowBytes; |
| 189 rowBytes.setZero(); |
183 | 190 |
184 switch (c) { | 191 switch (c) { |
185 case kNo_Config: | 192 case kNo_Config: |
186 break; | 193 break; |
187 case kA8_Config: | 194 case kA8_Config: |
188 case kIndex8_Config: | 195 case kIndex8_Config: |
189 rowBytes = width; | 196 rowBytes.set(width); |
190 break; | 197 break; |
191 case kRGB_565_Config: | 198 case kRGB_565_Config: |
192 case kARGB_4444_Config: | 199 case kARGB_4444_Config: |
193 rowBytes = width << 1; | 200 rowBytes.set(width); |
| 201 rowBytes.shiftLeft(1); |
194 break; | 202 break; |
195 case kARGB_8888_Config: | 203 case kARGB_8888_Config: |
196 rowBytes = width << 2; | 204 rowBytes.set(width); |
| 205 rowBytes.shiftLeft(2); |
197 break; | 206 break; |
198 default: | 207 default: |
199 SkDEBUGFAIL("unknown config"); | 208 SkDEBUGFAIL("unknown config"); |
200 break; | 209 break; |
201 } | 210 } |
202 return sk_64_isS32(rowBytes) ? sk_64_asS32(rowBytes) : 0; | 211 return isPos32Bits(rowBytes) ? rowBytes.get32() : 0; |
203 } | 212 } |
204 | 213 |
205 int64_t SkBitmap::ComputeSize64(Config config, int width, int height) { | 214 Sk64 SkBitmap::ComputeSize64(Config c, int width, int height) { |
206 int64_t rowBytes = sk_64_mul(ComputeBytesPerPixel(config), width); | 215 Sk64 size; |
207 return rowBytes * height; | 216 size.setMul(SkToS32(SkBitmap::ComputeRowBytes(c, width)), height); |
| 217 return size; |
208 } | 218 } |
209 | 219 |
210 size_t SkBitmap::ComputeSize(Config c, int width, int height) { | 220 size_t SkBitmap::ComputeSize(Config c, int width, int height) { |
211 int64_t size = SkBitmap::ComputeSize64(c, width, height); | 221 Sk64 size = SkBitmap::ComputeSize64(c, width, height); |
212 return sk_64_isS32(size) ? sk_64_asS32(size) : 0; | 222 return isPos32Bits(size) ? size.get32() : 0; |
213 } | 223 } |
214 | 224 |
215 int64_t SkBitmap::ComputeSafeSize64(Config config, | 225 Sk64 SkBitmap::ComputeSafeSize64(Config config, |
216 uint32_t width, | 226 uint32_t width, |
217 uint32_t height, | 227 uint32_t height, |
218 size_t rowBytes) { | 228 size_t rowBytes) { |
219 int64_t safeSize = 0; | 229 Sk64 safeSize; |
| 230 safeSize.setZero(); |
220 if (height > 0) { | 231 if (height > 0) { |
221 int64_t lastRow = sk_64_mul(ComputeBytesPerPixel(config), width); | 232 // TODO: Handle the case where the return value from |
222 safeSize = sk_64_mul(height - 1, rowBytes) + lastRow; | 233 // ComputeRowBytes is more than 31 bits. |
| 234 safeSize.set(SkToS32(ComputeRowBytes(config, width))); |
| 235 Sk64 sizeAllButLastRow; |
| 236 sizeAllButLastRow.setMul(height - 1, SkToS32(rowBytes)); |
| 237 safeSize.add(sizeAllButLastRow); |
223 } | 238 } |
224 SkASSERT(safeSize >= 0); | 239 SkASSERT(!safeSize.isNeg()); |
225 return safeSize; | 240 return safeSize; |
226 } | 241 } |
227 | 242 |
228 size_t SkBitmap::ComputeSafeSize(Config config, | 243 size_t SkBitmap::ComputeSafeSize(Config config, |
229 uint32_t width, | 244 uint32_t width, |
230 uint32_t height, | 245 uint32_t height, |
231 size_t rowBytes) { | 246 size_t rowBytes) { |
232 int64_t safeSize = ComputeSafeSize64(config, width, height, rowBytes); | 247 Sk64 safeSize = ComputeSafeSize64(config, width, height, rowBytes); |
233 int32_t safeSize32 = (int32_t)safeSize; | 248 return (safeSize.is32() ? safeSize.get32() : 0); |
234 | |
235 if (safeSize32 != safeSize) { | |
236 safeSize32 = 0; | |
237 } | |
238 return safeSize32; | |
239 } | 249 } |
240 | 250 |
241 void SkBitmap::getBounds(SkRect* bounds) const { | 251 void SkBitmap::getBounds(SkRect* bounds) const { |
242 SkASSERT(bounds); | 252 SkASSERT(bounds); |
243 bounds->set(0, 0, | 253 bounds->set(0, 0, |
244 SkIntToScalar(fWidth), SkIntToScalar(fHeight)); | 254 SkIntToScalar(fWidth), SkIntToScalar(fHeight)); |
245 } | 255 } |
246 | 256 |
247 void SkBitmap::getBounds(SkIRect* bounds) const { | 257 void SkBitmap::getBounds(SkIRect* bounds) const { |
248 SkASSERT(bounds); | 258 SkASSERT(bounds); |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 | 551 |
542 /////////////////////////////////////////////////////////////////////////////// | 552 /////////////////////////////////////////////////////////////////////////////// |
543 | 553 |
544 size_t SkBitmap::getSafeSize() const { | 554 size_t SkBitmap::getSafeSize() const { |
545 // This is intended to be a size_t version of ComputeSafeSize64(), just | 555 // This is intended to be a size_t version of ComputeSafeSize64(), just |
546 // faster. The computation is meant to be identical. | 556 // faster. The computation is meant to be identical. |
547 return (fHeight ? ((fHeight - 1) * fRowBytes) + | 557 return (fHeight ? ((fHeight - 1) * fRowBytes) + |
548 ComputeRowBytes(this->config(), fWidth): 0); | 558 ComputeRowBytes(this->config(), fWidth): 0); |
549 } | 559 } |
550 | 560 |
| 561 Sk64 SkBitmap::getSafeSize64() const { |
| 562 return ComputeSafeSize64(this->config(), fWidth, fHeight, fRowBytes); |
| 563 } |
| 564 |
551 bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize, | 565 bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize, |
552 size_t dstRowBytes, bool preserveDstPad) const { | 566 size_t dstRowBytes, bool preserveDstPad) const { |
553 | 567 |
554 if (0 == dstRowBytes) { | 568 if (0 == dstRowBytes) { |
555 dstRowBytes = fRowBytes; | 569 dstRowBytes = fRowBytes; |
556 } | 570 } |
557 | 571 |
558 if (dstRowBytes < ComputeRowBytes(this->config(), fWidth) || | 572 if (dstRowBytes < ComputeRowBytes(this->config(), fWidth) || |
559 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) | 573 dst == NULL || (getPixels() == NULL && pixelRef() == NULL)) |
560 return false; | 574 return false; |
(...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1697 if (NULL != uri) { | 1711 if (NULL != uri) { |
1698 str->appendf(" uri:\"%s\"", uri); | 1712 str->appendf(" uri:\"%s\"", uri); |
1699 } else { | 1713 } else { |
1700 str->appendf(" pixelref:%p", pr); | 1714 str->appendf(" pixelref:%p", pr); |
1701 } | 1715 } |
1702 } | 1716 } |
1703 | 1717 |
1704 str->append(")"); | 1718 str->append(")"); |
1705 } | 1719 } |
1706 #endif | 1720 #endif |
OLD | NEW |