• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/core/SkMask.h"
9 
10 #include "include/private/base/SkMalloc.h"
11 #include "include/private/base/SkTo.h"
12 #include "src/base/SkSafeMath.h"
13 
14 #include <climits>
15 
16 /** returns the product if it is positive and fits in 31 bits. Otherwise this
17     returns 0.
18  */
safeMul32(int32_t a,int32_t b)19 static int32_t safeMul32(int32_t a, int32_t b) {
20     int64_t size = sk_64_mul(a, b);
21     if (size > 0 && SkTFitsIn<int32_t>(size)) {
22         return size;
23     }
24     return 0;
25 }
26 
computeImageSize() const27 size_t SkMask::computeImageSize() const {
28     return safeMul32(fBounds.height(), fRowBytes);
29 }
30 
computeTotalImageSize() const31 size_t SkMask::computeTotalImageSize() const {
32     size_t size = this->computeImageSize();
33     if (fFormat == SkMask::k3D_Format) {
34         size = safeMul32(SkToS32(size), 3);
35     }
36     return size;
37 }
38 
39 /** We explicitly use this allocator for SkBimap pixels, so that we can
40     freely assign memory allocated by one class to the other.
41 */
AllocImage(size_t size,AllocType at)42 uint8_t* SkMask::AllocImage(size_t size, AllocType at) {
43     size_t aligned_size = SkSafeMath::Align4(size);
44     unsigned flags = SK_MALLOC_THROW;
45     if (at == kZeroInit_Alloc) {
46         flags |= SK_MALLOC_ZERO_INITIALIZE;
47     }
48     return static_cast<uint8_t*>(sk_malloc_flags(aligned_size, flags));
49 }
50 
51 /** We explicitly use this allocator for SkBimap pixels, so that we can
52     freely assign memory allocated by one class to the other.
53 */
FreeImage(void * image)54 void SkMask::FreeImage(void* image) {
55     sk_free(image);
56 }
57 
PrepareDestination(int radiusX,int radiusY,const SkMask & src)58 SkMask SkMask::PrepareDestination(int radiusX, int radiusY, const SkMask& src) {
59     SkSafeMath safe;
60 
61     SkMask dst;
62     dst.fImage = nullptr;
63     dst.fFormat = SkMask::kA8_Format;
64 
65     // dstW = srcW + 2 * radiusX;
66     size_t dstW = safe.add(src.fBounds.width(), safe.add(radiusX, radiusX));
67     // dstH = srcH + 2 * radiusY;
68     size_t dstH = safe.add(src.fBounds.height(), safe.add(radiusY, radiusY));
69 
70     size_t toAlloc = safe.mul(dstW, dstH);
71 
72     // We can only deal with masks that fit in INT_MAX and sides that fit in int.
73     if (!SkTFitsIn<int>(dstW) || !SkTFitsIn<int>(dstH) || toAlloc > INT_MAX || !safe) {
74         dst.fBounds.setEmpty();
75         dst.fRowBytes = 0;
76         return dst;
77     }
78 
79     dst.fBounds.setWH(SkTo<int>(dstW), SkTo<int>(dstH));
80     dst.fBounds.offset(src.fBounds.x(), src.fBounds.y());
81     dst.fBounds.offset(-radiusX, -radiusY);
82     dst.fRowBytes = SkTo<uint32_t>(dstW);
83 
84     if (src.fImage != nullptr) {
85         dst.fImage = SkMask::AllocImage(toAlloc);
86     }
87 
88     return dst;
89 }
90 
91 
92 ///////////////////////////////////////////////////////////////////////////////
93 
94 static const int gMaskFormatToShift[] = {
95     ~0, // BW -- not supported
96     0,  // A8
97     0,  // 3D
98     2,  // ARGB32
99     1,  // LCD16
100     0,  // SDF
101 };
102 
maskFormatToShift(SkMask::Format format)103 static int maskFormatToShift(SkMask::Format format) {
104     SkASSERT((unsigned)format < std::size(gMaskFormatToShift));
105     SkASSERT(SkMask::kBW_Format != format);
106     return gMaskFormatToShift[format];
107 }
108 
getAddr(int x,int y) const109 void* SkMask::getAddr(int x, int y) const {
110     SkASSERT(kBW_Format != fFormat);
111     SkASSERT(fBounds.contains(x, y));
112     SkASSERT(fImage);
113 
114     char* addr = (char*)fImage;
115     addr += (y - fBounds.fTop) * fRowBytes;
116     addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
117     return addr;
118 }
119