Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkTableMaskFilter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 Google Inc.
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
9
13#include "include/core/SkRect.h"
21#include "src/core/SkMask.h"
25
26#include <cmath>
27#include <cstdint>
28#include <cstring>
29
30class SkMatrix;
31
33public:
34 explicit SkTableMaskFilterImpl(const uint8_t table[256]);
35
36 SkMask::Format getFormat() const override;
37 bool filterMask(SkMaskBuilder*, const SkMask&, const SkMatrix&, SkIPoint*) const override;
39
40protected:
41 ~SkTableMaskFilterImpl() override;
42
43 void flatten(SkWriteBuffer&) const override;
44
45private:
47
49
50 uint8_t fTable[256];
51
52 using INHERITED = SkMaskFilter;
53};
54
56 for (int i = 0; i < 256; i++) {
57 fTable[i] = i;
58 }
59}
60
62 memcpy(fTable, table, sizeof(fTable));
63}
64
66
68 const SkMatrix&, SkIPoint* margin) const {
69 if (src.fFormat != SkMask::kA8_Format) {
70 return false;
71 }
72
73 dst->bounds() = src.fBounds;
74 dst->rowBytes() = SkAlign4(dst->fBounds.width());
75 dst->format() = SkMask::kA8_Format;
76 dst->image() = nullptr;
77
78 if (src.fImage) {
79 dst->image() = SkMaskBuilder::AllocImage(dst->computeImageSize());
80
81 const uint8_t* srcP = src.fImage;
82 uint8_t* dstP = dst->image();
83 const uint8_t* table = fTable;
84 int dstWidth = dst->fBounds.width();
85 int extraZeros = dst->fRowBytes - dstWidth;
86
87 for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
88 for (int x = dstWidth - 1; x >= 0; --x) {
89 dstP[x] = table[srcP[x]];
90 }
91 srcP += src.fRowBytes;
92 // we can't just inc dstP by rowbytes, because if it has any
93 // padding between its width and its rowbytes, we need to zero those
94 // so that the bitters can read those safely if that is faster for
95 // them
96 dstP += dstWidth;
97 for (int i = extraZeros - 1; i >= 0; --i) {
98 *dstP++ = 0;
99 }
100 }
101 }
102
103 if (margin) {
104 margin->set(0, 0);
105 }
106 return true;
107}
108
112
114 wb.writeByteArray(fTable, 256);
115}
116
117sk_sp<SkFlattenable> SkTableMaskFilterImpl::CreateProc(SkReadBuffer& buffer) {
118 uint8_t table[256];
119 if (!buffer.readByteArray(table, 256)) {
120 return nullptr;
121 }
123}
124
125///////////////////////////////////////////////////////////////////////////////
126
128 return new SkTableMaskFilterImpl(table);
129}
130
132 uint8_t table[256];
133 MakeGammaTable(table, gamma);
134 return new SkTableMaskFilterImpl(table);
135}
136
138 uint8_t table[256];
140 return new SkTableMaskFilterImpl(table);
141}
142
144 const float dx = 1 / 255.0f;
145 const float g = gamma;
146
147 float x = 0;
148 for (int i = 0; i < 256; i++) {
149 // float ee = powf(x, g) * 255;
150 table[i] = SkTPin(sk_float_round2int(powf(x, g) * 255), 0, 255);
151 x += dx;
152 }
153}
154
155void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
156 uint8_t max) {
157 if (0 == max) {
158 max = 1;
159 }
160 if (min >= max) {
161 min = max - 1;
162 }
163 SkASSERT(min < max);
164
165 SkFixed scale = (1 << 16) * 255 / (max - min);
166 memset(table, 0, min + 1);
167 for (int i = min + 1; i < max; i++) {
168 int value = SkFixedRoundToInt(scale * (i - min));
169 SkASSERT(value <= 255);
170 table[i] = value;
171 }
172 memset(table + max, 255, 256 - max);
173
174#if 0
175 int j;
176 for (j = 0; j < 256; j++) {
177 if (table[j]) {
178 break;
179 }
180 }
181 SkDebugf("%d %d start [%d]", min, max, j);
182 for (; j < 256; j++) {
183 SkDebugf(" %d", table[j]);
184 }
185 SkDebugf("\n\n");
186#endif
187}
static constexpr T SkAlign4(T x)
Definition SkAlign.h:16
#define SkASSERT(cond)
Definition SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
int32_t SkFixed
Definition SkFixed.h:25
#define SkFixedRoundToInt(x)
Definition SkFixed.h:76
#define SK_FLATTENABLE_HOOKS(type)
#define sk_float_round2int(x)
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
SI F table(const skcms_Curve *curve, F v)
void flatten(SkWriteBuffer &) const override
SkTableMaskFilterImpl(const uint8_t table[256])
SkMask::Format getFormat() const override
bool filterMask(SkMaskBuilder *, const SkMask &, const SkMatrix &, SkIPoint *) const override
SkMaskFilterBase::Type type() const override
static void MakeGammaTable(uint8_t table[256], SkScalar gamma)
static SkMaskFilter * CreateGamma(SkScalar gamma)
static SkMaskFilter * Create(const uint8_t table[256])
static void MakeClipTable(uint8_t table[256], uint8_t min, uint8_t max)
static SkMaskFilter * CreateClip(uint8_t min, uint8_t max)
virtual void writeByteArray(const void *data, size_t size)=0
float SkScalar
Definition extension.cpp:12
static const uint8_t buffer[]
uint8_t value
static float max(float r, float g, float b)
Definition hsl.cpp:49
static float min(float r, float g, float b)
Definition hsl.cpp:48
double y
double x
const Scalar scale
void set(int32_t x, int32_t y)
static uint8_t * AllocImage(size_t bytes, AllocType=kUninit_Alloc)
Definition SkMask.cpp:45
Format
Definition SkMask.h:26
@ kA8_Format
8bits per pixel mask (e.g. antialiasing)
Definition SkMask.h:28