Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PaintTest.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
12#include "include/core/SkFont.h"
16#include "include/core/SkPath.h"
19#include "include/core/SkRect.h"
24#include "src/core/SkBlurMask.h"
28#include "tests/Test.h"
30
31#include <algorithm>
32#include <array>
33#include <cstddef>
34#include <cstdint>
35#include <cstring>
36#include <initializer_list>
37#include <optional>
38
39#undef ASSERT
40
41using namespace skia_private;
42
43DEF_TEST(Paint_copy, reporter) {
45 // set a few member variables
47 paint.setStrokeWidth(SkIntToScalar(2));
48 // set a few pointers
51
52 // copy the paint using the copy constructor and check they are the same
53 SkPaint copiedPaint = paint;
54 REPORTER_ASSERT(reporter, paint == copiedPaint);
55
56 // copy the paint using the equal operator and check they are the same
57 copiedPaint = paint;
58 REPORTER_ASSERT(reporter, paint == copiedPaint);
59
60 // clean the paint and check they are back to their initial states
61 SkPaint cleanPaint;
62 paint.reset();
63 copiedPaint.reset();
64 REPORTER_ASSERT(reporter, cleanPaint == paint);
65 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
66}
67
68// found and fixed for webkit: mishandling when we hit recursion limit on
69// mostly degenerate cubic flatness test
70DEF_TEST(Paint_regression_cubic, reporter) {
71 SkPath path, stroke;
73
74 path.moveTo(460.2881309415525f,
75 303.250847066498f);
76 path.cubicTo(463.36378422175284f,
77 302.1169735073363f,
78 456.32239330810046f,
79 304.720354932878f,
80 453.15255460013304f,
81 305.788586869862f);
82
83 SkRect fillR, strokeR;
84 fillR = path.getBounds();
85
87 paint.setStrokeWidth(SkIntToScalar(2));
89 strokeR = stroke.getBounds();
90
91 SkRect maxR = fillR;
92 SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
93 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
94 paint.getStrokeWidth() * miter :
95 paint.getStrokeWidth();
96 maxR.inset(-inset, -inset);
97
98 // test that our stroke didn't explode
99 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
100}
101
102DEF_TEST(Paint_flattening, reporter) {
103 const SkPaint::Cap caps[] = {
107 };
108 const SkPaint::Join joins[] = {
112 };
113 const SkPaint::Style styles[] = {
117 };
118
119#define FOR_SETUP(index, array, setter) \
120 for (size_t index = 0; index < std::size(array); ++index) { \
121 paint.setter(array[index]);
122
124 paint.setAntiAlias(true);
125
126 // we don't serialize hinting or encoding -- soon to be removed from paint
127
128 FOR_SETUP(l, caps, setStrokeCap)
129 FOR_SETUP(m, joins, setStrokeJoin)
130 FOR_SETUP(p, styles, setStyle)
131
132 SkBinaryWriteBuffer writer({});
134
135 SkAutoMalloc buf(writer.bytesWritten());
136 writer.writeToMemory(buf.get());
137 SkReadBuffer reader(buf.get(), writer.bytesWritten());
138
139 SkPaint paint2 = reader.readPaint();
140 REPORTER_ASSERT(reporter, paint2 == paint);
141
142 }}}
143#undef FOR_SETUP
144
145}
146
147// found and fixed for android: not initializing rect for string's of length 0
148DEF_TEST(Paint_regression_measureText, reporter) {
149
151 font.setSize(12.0f);
152
153 SkRect r;
155
156 // test that the rect was reset
157 font.measureText("", 0, SkTextEncoding::kUTF8, &r);
159}
160
161#define ASSERT(expr) REPORTER_ASSERT(r, expr)
162
163DEF_TEST(Paint_MoreFlattening, r) {
165 paint.setColor(0x00AABBCC);
166 paint.setBlendMode(SkBlendMode::kModulate);
167
168 SkBinaryWriteBuffer writer({});
170
171 SkAutoMalloc buf(writer.bytesWritten());
172 writer.writeToMemory(buf.get());
173 SkReadBuffer reader(buf.get(), writer.bytesWritten());
174
175 SkPaint other = reader.readPaint();
176 ASSERT(reader.offset() == writer.bytesWritten());
177
178 // No matter the encoding, these must always hold.
179 ASSERT(other.getColor() == paint.getColor());
180 ASSERT(other.asBlendMode() == paint.asBlendMode());
181}
182
183DEF_TEST(Paint_nothingToDraw, r) {
185
186 REPORTER_ASSERT(r, !paint.nothingToDraw());
187 paint.setAlpha(0);
188 REPORTER_ASSERT(r, paint.nothingToDraw());
189
190 paint.setAlpha(0xFF);
191 paint.setBlendMode(SkBlendMode::kDst);
192 REPORTER_ASSERT(r, paint.nothingToDraw());
193
194 paint.setAlpha(0);
195 paint.setBlendMode(SkBlendMode::kSrcOver);
196
197 SkColorMatrix cm;
198 cm.setIdentity(); // does not change alpha
199 paint.setColorFilter(SkColorFilters::Matrix(cm));
200 REPORTER_ASSERT(r, paint.nothingToDraw());
201
202 cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha
203 paint.setColorFilter(SkColorFilters::Matrix(cm));
204 REPORTER_ASSERT(r, !paint.nothingToDraw());
205}
206
207DEF_TEST(Font_getpos, r) {
209 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
210 int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
211 AutoTArray<uint16_t> glyphStorage(count);
212 uint16_t* glyphs = glyphStorage.get();
213 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
214
215 AutoTArray<SkScalar> widthStorage(count);
216 AutoTArray<SkScalar> xposStorage(count);
217 AutoTArray<SkPoint> posStorage(count);
218
219 SkScalar* widths = widthStorage.get();
220 SkScalar* xpos = xposStorage.get();
221 SkPoint* pos = posStorage.get();
222
223 for (bool subpix : { false, true }) {
224 font.setSubpixel(subpix);
226 font.setHinting(hint);
227 for (auto size : { 1.0f, 12.0f, 100.0f }) {
228 font.setSize(size);
229
230 font.getWidths(glyphs, count, widths);
231 font.getXPos(glyphs, count, xpos, 10);
232 font.getPos(glyphs, count, pos, {10, 20});
233
234 auto nearly_eq = [](SkScalar a, SkScalar b) {
235 return SkScalarAbs(a - b) < 0.000001f;
236 };
237
238 SkScalar x = 10;
239 for (int i = 0; i < count; ++i) {
240 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
241 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
242 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
243 x += widths[i];
244 }
245 }
246 }
247 }
248}
249
250DEF_TEST(Paint_dither, reporter) {
251 SkPaint p;
252 p.setDither(true);
253
255
256 REPORTER_ASSERT(reporter, !shouldDither);
257}
reporter
uint16_t glyphs[5]
int count
SkPoint pos
#define ASSERT(expr)
#define FOR_SETUP(index, array, setter)
@ kModulate
r = s*d
@ kSrcOver
r = s + (1-sa)*d
@ kNormal_SkBlurStyle
fuzzy inside and outside
Definition SkBlurTypes.h:12
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kNormal
glyph outlines modified to improve constrast
@ kNone
glyph outlines unchanged
@ kSlight
minimal modification to improve constrast
@ kFull
modifies glyph outlines for maximum constrast
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SK_Scalar1
Definition SkScalar.h:18
#define SK_ScalarNaN
Definition SkScalar.h:28
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarAbs(x)
Definition SkScalar.h:39
const SkScalar widths[]
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
void * get()
static SkScalar SK_SPI ConvertRadiusToSigma(SkScalar radius)
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void postTranslate(float dr, float dg, float db, float da)
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
static bool ShouldDither(const SkPaint &, SkColorType)
static void Flatten(const SkPaint &paint, SkWriteBuffer &buffer)
@ kRound_Cap
adds circle
Definition SkPaint.h:335
@ kButt_Cap
no stroke extension
Definition SkPaint.h:334
@ kSquare_Cap
adds square
Definition SkPaint.h:336
void reset()
Definition SkPaint.cpp:103
SkColor getColor() const
Definition SkPaint.h:225
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition SkPaint.h:195
@ kRound_Join
adds circle
Definition SkPaint.h:360
@ kMiter_Join
extends to miter limit
Definition SkPaint.h:359
@ kBevel_Join
connects outside edges
Definition SkPaint.h:361
std::optional< SkBlendMode > asBlendMode() const
Definition SkPaint.cpp:138
const SkRect & getBounds() const
Definition SkPath.cpp:420
SkPaint readPaint()
size_t offset() const
const Paint & paint
float SkScalar
Definition extension.cpp:12
static bool b
struct MyStruct a[10]
std::u16string text
double x
SkFont DefaultFont()
SK_API bool FillPathWithPaint(const SkPath &src, const SkPaint &paint, SkPath *dst, const SkRect *cullRect, SkScalar resScale=1)
static SkRect inset(const SkRect &r)
void inset(float dx, float dy)
Definition SkRect.h:1060
bool contains(SkScalar x, SkScalar y) const
Definition extension.cpp:19
void setLTRB(float left, float top, float right, float bottom)
Definition SkRect.h:865
bool isEmpty() const
Definition SkRect.h:693