Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkMask.cpp
Go to the documentation of this file.
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
14#include "src/base/SkSafeMath.h"
15
16#include <array>
17#include <climits>
18
19/** returns the product if it is positive and fits in 31 bits. Otherwise this
20 returns 0.
21 */
22static int32_t safeMul32(int32_t a, int32_t b) {
23 int64_t size = sk_64_mul(a, b);
24 if (size > 0 && SkTFitsIn<int32_t>(size)) {
25 return size;
26 }
27 return 0;
28}
29
32}
33
35 size_t size = this->computeImageSize();
37 size = safeMul32(SkToS32(size), 3);
38 }
39 return size;
40}
41
42/** We explicitly use this allocator for SkBimap pixels, so that we can
43 freely assign memory allocated by one class to the other.
44*/
45uint8_t* SkMaskBuilder::AllocImage(size_t size, AllocType at) {
46 size_t aligned_size = SkSafeMath::Align4(size);
47 unsigned flags = SK_MALLOC_THROW;
48 if (at == kZeroInit_Alloc) {
50 }
51 return static_cast<uint8_t*>(sk_malloc_flags(aligned_size, flags));
52}
53
54/** We explicitly use this allocator for SkBimap pixels, so that we can
55 freely assign memory allocated by one class to the other.
56*/
60
61SkMaskBuilder SkMaskBuilder::PrepareDestination(int radiusX, int radiusY, const SkMask& src) {
62 SkSafeMath safe;
63
64 SkMaskBuilder dst;
65 dst.image() = nullptr;
66 dst.format() = SkMask::kA8_Format;
67
68 // dstW = srcW + 2 * radiusX;
69 size_t dstW = safe.add(src.fBounds.width(), safe.add(radiusX, radiusX));
70 // dstH = srcH + 2 * radiusY;
71 size_t dstH = safe.add(src.fBounds.height(), safe.add(radiusY, radiusY));
72
73 size_t toAlloc = safe.mul(dstW, dstH);
74
75 // We can only deal with masks that fit in INT_MAX and sides that fit in int.
76 if (!SkTFitsIn<int>(dstW) || !SkTFitsIn<int>(dstH) || toAlloc > INT_MAX || !safe) {
77 dst.bounds().setEmpty();
78 dst.rowBytes() = 0;
79 return dst;
80 }
81
82 dst.bounds().setWH(SkTo<int>(dstW), SkTo<int>(dstH));
83 dst.bounds().offset(src.fBounds.x(), src.fBounds.y());
84 dst.bounds().offset(-radiusX, -radiusY);
85 dst.rowBytes() = SkTo<uint32_t>(dstW);
86
87 if (src.fImage != nullptr) {
88 dst.image() = SkMaskBuilder::AllocImage(toAlloc);
89 }
90
91 return dst;
92}
93
94
95///////////////////////////////////////////////////////////////////////////////
96
97static const int gMaskFormatToShift[] = {
98 ~0, // BW -- not supported
99 0, // A8
100 0, // 3D
101 2, // ARGB32
102 1, // LCD16
103 0, // SDF
104};
105
111
112const void* SkMask::getAddr(int x, int y) const {
116
117 const char* addr = (const char*)fImage;
118 addr += (y - fBounds.fTop) * fRowBytes;
119 addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
120 return addr;
121}
#define SkASSERT(cond)
Definition SkAssert.h:116
SK_API void * sk_malloc_flags(size_t size, unsigned flags)
SK_API void sk_free(void *)
@ SK_MALLOC_ZERO_INITIALIZE
Definition SkMalloc.h:34
@ SK_MALLOC_THROW
Definition SkMalloc.h:40
static int maskFormatToShift(SkMask::Format format)
Definition SkMask.cpp:106
static int32_t safeMul32(int32_t a, int32_t b)
Definition SkMask.cpp:22
static const int gMaskFormatToShift[]
Definition SkMask.cpp:97
static int64_t sk_64_mul(int64_t a, int64_t b)
Definition SkMath.h:33
constexpr int32_t SkToS32(S x)
Definition SkTo.h:25
size_t add(size_t x, size_t y)
Definition SkSafeMath.h:33
static size_t Align4(size_t x)
Definition SkSafeMath.h:69
size_t mul(size_t x, size_t y)
Definition SkSafeMath.h:29
sk_sp< SkImage > image
Definition examples.cpp:29
static bool b
struct MyStruct a[10]
FlutterSemanticsFlag flags
uint32_t uint32_t * format
double y
double x
constexpr int32_t height() const
Definition SkRect.h:165
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33
bool contains(int32_t x, int32_t y) const
Definition SkRect.h:463
static void FreeImage(void *image)
Definition SkMask.cpp:57
static SkMaskBuilder PrepareDestination(int radiusX, int radiusY, const SkMask &src)
Definition SkMask.cpp:61
@ kZeroInit_Alloc
Definition SkMask.h:293
static uint8_t * AllocImage(size_t bytes, AllocType=kUninit_Alloc)
Definition SkMask.cpp:45
uint8_t *& image()
Definition SkMask.h:236
const uint32_t fRowBytes
Definition SkMask.h:43
Format
Definition SkMask.h:26
@ k3D_Format
3 8bit per pixl planes: alpha, mul, add
Definition SkMask.h:29
@ kA8_Format
8bits per pixel mask (e.g. antialiasing)
Definition SkMask.h:28
@ kBW_Format
1bit per pixel mask (e.g. monochrome)
Definition SkMask.h:27
const void * getAddr(int x, int y) const
Definition SkMask.cpp:112
uint8_t const *const fImage
Definition SkMask.h:41
const SkIRect fBounds
Definition SkMask.h:42
size_t computeTotalImageSize() const
Definition SkMask.cpp:34
size_t computeImageSize() const
Definition SkMask.cpp:30
const Format fFormat
Definition SkMask.h:44