blob: 34f122b9b9cfb96ebdf7c95b48d9119cbd26a5eb [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* 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.com543ed932009-04-24 12:43:40 +000018#include "Sk64.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkMask.h"
20
reed@android.com543ed932009-04-24 12:43:40 +000021/** returns the product if it is positive and fits in 31 bits. Otherwise this
22 returns 0.
23 */
24static 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.com8a1c16f2008-12-17 15:59:43 +000031}
32
reed@android.com543ed932009-04-24 12:43:40 +000033size_t SkMask::computeImageSize() const {
34 return safeMul32(fBounds.height(), fRowBytes);
35}
reed@android.com8a1c16f2008-12-17 15:59:43 +000036
reed@android.com543ed932009-04-24 12:43:40 +000037size_t SkMask::computeTotalImageSize() const {
38 size_t size = this->computeImageSize();
39 if (fFormat == SkMask::k3D_Format) {
40 size = safeMul32(size, 3);
41 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 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.orgd11f0e02011-04-09 19:39:25 +000048uint8_t* SkMask::AllocImage(size_t size) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 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.orgd11f0e02011-04-09 19:39:25 +000055void SkMask::FreeImage(void* image) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 sk_free(image);
57}
58