OLD | NEW |
1 /* libs/graphics/sgl/SkMask.cpp | 1 /* libs/graphics/sgl/SkMask.cpp |
2 ** | 2 ** |
3 ** Copyright 2007, The Android Open Source Project | 3 ** Copyright 2007, The Android Open Source Project |
4 ** | 4 ** |
5 ** Licensed under the Apache License, Version 2.0 (the "License"); | 5 ** Licensed under the Apache License, Version 2.0 (the "License"); |
6 ** you may not use this file except in compliance with the License. | 6 ** you may not use this file except in compliance with the License. |
7 ** You may obtain a copy of the License at | 7 ** You may obtain a copy of the License at |
8 ** | 8 ** |
9 ** http://www.apache.org/licenses/LICENSE-2.0 | 9 ** http://www.apache.org/licenses/LICENSE-2.0 |
10 ** | 10 ** |
11 ** Unless required by applicable law or agreed to in writing, software | 11 ** Unless required by applicable law or agreed to in writing, software |
12 ** distributed under the License is distributed on an "AS IS" BASIS, | 12 ** distributed under the License is distributed on an "AS IS" BASIS, |
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 ** See the License for the specific language governing permissions and | 14 ** See the License for the specific language governing permissions and |
15 ** limitations under the License. | 15 ** limitations under the License. |
16 */ | 16 */ |
17 | 17 |
18 #include <limits> | 18 #include "Sk64.h" |
19 | |
20 #include "SkMask.h" | 19 #include "SkMask.h" |
21 | 20 |
22 size_t SkMask::computeImageSize() const | 21 /** returns the product if it is positive and fits in 31 bits. Otherwise this |
23 { | 22 returns 0. |
24 // Prevent too large a number. There is a better fix for this in Skia | 23 */ |
25 // trunk where it returns failure. | 24 static int32_t safeMul32(int32_t a, int32_t b) { |
26 long long size = (long long)fBounds.height() * (long long)fRowBytes; | 25 Sk64 size; |
27 if (size >= std::numeric_limits<size_t>::max() / 2) { | 26 size.setMul(a, b); |
28 #ifdef WIN32 | 27 if (size.is32() && size.isPos()) { |
29 __debugbreak(); | 28 return size.get32(); |
30 #else | |
31 abort(); | |
32 #endif | |
33 } | 29 } |
34 | 30 return 0; |
35 return size; | |
36 } | 31 } |
37 | 32 |
38 size_t SkMask::computeTotalImageSize() const | 33 size_t SkMask::computeImageSize() const { |
39 { | 34 return safeMul32(fBounds.height(), fRowBytes); |
| 35 } |
| 36 |
| 37 size_t SkMask::computeTotalImageSize() const { |
40 size_t size = this->computeImageSize(); | 38 size_t size = this->computeImageSize(); |
41 | |
42 if (fFormat == SkMask::k3D_Format) { | 39 if (fFormat == SkMask::k3D_Format) { |
43 // See computeImageSize for why we want to stop here. | 40 size = safeMul32(size, 3); |
44 if (size > std::numeric_limits<size_t>::max() / 3) { | |
45 #ifdef WIN32 | |
46 __debugbreak(); | |
47 #else | |
48 abort(); | |
49 #endif | |
50 } | |
51 | |
52 size *= 3; | |
53 } | 41 } |
54 return size; | 42 return size; |
55 } | 43 } |
56 | 44 |
57 /** We explicitly use this allocator for SkBimap pixels, so that we can | 45 /** We explicitly use this allocator for SkBimap pixels, so that we can |
58 freely assign memory allocated by one class to the other. | 46 freely assign memory allocated by one class to the other. |
59 */ | 47 */ |
60 uint8_t* SkMask::AllocImage(size_t size) | 48 uint8_t* SkMask::AllocImage(size_t size) |
61 { | 49 { |
62 return (uint8_t*)sk_malloc_throw(SkAlign4(size)); | 50 return (uint8_t*)sk_malloc_throw(SkAlign4(size)); |
63 } | 51 } |
64 | 52 |
65 /** We explicitly use this allocator for SkBimap pixels, so that we can | 53 /** We explicitly use this allocator for SkBimap pixels, so that we can |
66 freely assign memory allocated by one class to the other. | 54 freely assign memory allocated by one class to the other. |
67 */ | 55 */ |
68 void SkMask::FreeImage(void* image) | 56 void SkMask::FreeImage(void* image) |
69 { | 57 { |
70 sk_free(image); | 58 sk_free(image); |
71 } | 59 } |
72 | 60 |
OLD | NEW |