• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "Sk64.h"
19 #include "SkMask.h"
20 
21 /** returns the product if it is positive and fits in 31 bits. Otherwise this
22     returns 0.
23  */
safeMul32(int32_t a,int32_t b)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;
31 }
32 
computeImageSize() const33 size_t SkMask::computeImageSize() const {
34     return safeMul32(fBounds.height(), fRowBytes);
35 }
36 
computeTotalImageSize() const37 size_t SkMask::computeTotalImageSize() const {
38     size_t size = this->computeImageSize();
39     if (fFormat == SkMask::k3D_Format) {
40         size = safeMul32(size, 3);
41     }
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 */
AllocImage(size_t size)48 uint8_t* SkMask::AllocImage(size_t size)
49 {
50     return (uint8_t*)sk_malloc_throw(SkAlign4(size));
51 }
52 
53 /** We explicitly use this allocator for SkBimap pixels, so that we can
54     freely assign memory allocated by one class to the other.
55 */
FreeImage(void * image)56 void SkMask::FreeImage(void* image)
57 {
58     sk_free(image);
59 }
60 
61