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/SkMalloc.h"
11 #include "include/private/SkTo.h"
12 #include "src/core/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 // dstW = srcW + 2 * radiusX;
65 size_t dstW = safe.add(src.fBounds.width(), safe.add(radiusX, radiusX));
66 // dstH = srcH + 2 * radiusY;
67 size_t dstH = safe.add(src.fBounds.height(), safe.add(radiusY, radiusY));
68
69 size_t toAlloc = safe.mul(dstW, dstH);
70
71 // We can only deal with masks that fit in INT_MAX and sides that fit in int.
72 if (!SkTFitsIn<int>(dstW) || !SkTFitsIn<int>(dstH) || toAlloc > INT_MAX || !safe) {
73 dst.fBounds.setEmpty();
74 dst.fRowBytes = 0;
75 return dst;
76 }
77
78 dst.fBounds.setWH(SkTo<int>(dstW), SkTo<int>(dstH));
79 dst.fBounds.offset(src.fBounds.x(), src.fBounds.y());
80 dst.fBounds.offset(-radiusX, -radiusY);
81 dst.fRowBytes = SkTo<uint32_t>(dstW);
82
83 if (src.fImage != nullptr) {
84 dst.fImage = SkMask::AllocImage(toAlloc);
85 }
86
87 return dst;
88 }
89
90
91 ///////////////////////////////////////////////////////////////////////////////
92
93 static const int gMaskFormatToShift[] = {
94 ~0, // BW -- not supported
95 0, // A8
96 0, // 3D
97 2, // ARGB32
98 1, // LCD16
99 0, // SDF
100 };
101
maskFormatToShift(SkMask::Format format)102 static int maskFormatToShift(SkMask::Format format) {
103 SkASSERT((unsigned)format < SK_ARRAY_COUNT(gMaskFormatToShift));
104 SkASSERT(SkMask::kBW_Format != format);
105 return gMaskFormatToShift[format];
106 }
107
getAddr(int x,int y) const108 void* SkMask::getAddr(int x, int y) const {
109 SkASSERT(kBW_Format != fFormat);
110 SkASSERT(fBounds.contains(x, y));
111 SkASSERT(fImage);
112
113 char* addr = (char*)fImage;
114 addr += (y - fBounds.fTop) * fRowBytes;
115 addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
116 return addr;
117 }
118