reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* libs/graphics/sgl/SkMask.cpp |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (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 |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 18 | #include "Sk64.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkMask.h" |
| 20 | |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 21 | /** returns the product if it is positive and fits in 31 bits. Otherwise this |
| 22 | returns 0. |
| 23 | */ |
| 24 | static int32_t safeMul32(int32_t a, int32_t b) { |
| 25 | Sk64 size; |
| 26 | size.setMul(a, b); |
| 27 | if (size.is32() && size.isPos()) { |
| 28 | return size.get32(); |
| 29 | } |
| 30 | return 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | } |
| 32 | |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 33 | size_t SkMask::computeImageSize() const { |
| 34 | return safeMul32(fBounds.height(), fRowBytes); |
| 35 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 37 | size_t SkMask::computeTotalImageSize() const { |
| 38 | size_t size = this->computeImageSize(); |
| 39 | if (fFormat == SkMask::k3D_Format) { |
| 40 | size = safeMul32(size, 3); |
| 41 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | return size; |
| 43 | } |
| 44 | |
| 45 | /** We explicitly use this allocator for SkBimap pixels, so that we can |
| 46 | freely assign memory allocated by one class to the other. |
| 47 | */ |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame^] | 48 | uint8_t* SkMask::AllocImage(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | return (uint8_t*)sk_malloc_throw(SkAlign4(size)); |
| 50 | } |
| 51 | |
| 52 | /** We explicitly use this allocator for SkBimap pixels, so that we can |
| 53 | freely assign memory allocated by one class to the other. |
| 54 | */ |
mike@reedtribe.org | d11f0e0 | 2011-04-09 19:39:25 +0000 | [diff] [blame^] | 55 | void SkMask::FreeImage(void* image) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | sk_free(image); |
| 57 | } |
| 58 | |