• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkBlendMode.h"
9 #include "include/core/SkBlurTypes.h"
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkMaskFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPath.h"
17 #include "include/core/SkPathUtils.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkScalar.h"
21 #include "include/effects/SkColorMatrix.h"
22 #include "include/private/base/SkTemplates.h"
23 #include "src/base/SkAutoMalloc.h"
24 #include "src/core/SkBlurMask.h"
25 #include "src/core/SkPaintPriv.h"
26 #include "src/core/SkReadBuffer.h"
27 #include "src/core/SkWriteBuffer.h"
28 #include "tests/Test.h"
29 
30 #include <algorithm>
31 #include <cstddef>
32 #include <cstdint>
33 #include <initializer_list>
34 #include <optional>
35 #include <string>
36 
37 #undef ASSERT
38 
39 using namespace skia_private;
40 
DEF_TEST(Paint_copy,reporter)41 DEF_TEST(Paint_copy, reporter) {
42     SkPaint paint;
43     // set a few member variables
44     paint.setStyle(SkPaint::kStrokeAndFill_Style);
45     paint.setStrokeWidth(SkIntToScalar(2));
46     // set a few pointers
47     paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
48                                                SkBlurMask::ConvertRadiusToSigma(1)));
49 
50     // copy the paint using the copy constructor and check they are the same
51     SkPaint copiedPaint = paint;
52     REPORTER_ASSERT(reporter, paint == copiedPaint);
53 
54     // copy the paint using the equal operator and check they are the same
55     copiedPaint = paint;
56     REPORTER_ASSERT(reporter, paint == copiedPaint);
57 
58     // clean the paint and check they are back to their initial states
59     SkPaint cleanPaint;
60     paint.reset();
61     copiedPaint.reset();
62     REPORTER_ASSERT(reporter, cleanPaint == paint);
63     REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
64 }
65 
66 // found and fixed for webkit: mishandling when we hit recursion limit on
67 // mostly degenerate cubic flatness test
DEF_TEST(Paint_regression_cubic,reporter)68 DEF_TEST(Paint_regression_cubic, reporter) {
69     SkPath path, stroke;
70     SkPaint paint;
71 
72     path.moveTo(460.2881309415525f,
73                 303.250847066498f);
74     path.cubicTo(463.36378422175284f,
75                  302.1169735073363f,
76                  456.32239330810046f,
77                  304.720354932878f,
78                  453.15255460013304f,
79                  305.788586869862f);
80 
81     SkRect fillR, strokeR;
82     fillR = path.getBounds();
83 
84     paint.setStyle(SkPaint::kStroke_Style);
85     paint.setStrokeWidth(SkIntToScalar(2));
86     skpathutils::FillPathWithPaint(path, paint, &stroke);
87     strokeR = stroke.getBounds();
88 
89     SkRect maxR = fillR;
90     SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
91     SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
92                             paint.getStrokeWidth() * miter :
93                             paint.getStrokeWidth();
94     maxR.inset(-inset, -inset);
95 
96     // test that our stroke didn't explode
97     REPORTER_ASSERT(reporter, maxR.contains(strokeR));
98 }
99 
DEF_TEST(Paint_flattening,reporter)100 DEF_TEST(Paint_flattening, reporter) {
101     const SkPaint::Cap caps[] = {
102         SkPaint::kButt_Cap,
103         SkPaint::kRound_Cap,
104         SkPaint::kSquare_Cap,
105     };
106     const SkPaint::Join joins[] = {
107         SkPaint::kMiter_Join,
108         SkPaint::kRound_Join,
109         SkPaint::kBevel_Join,
110     };
111     const SkPaint::Style styles[] = {
112         SkPaint::kFill_Style,
113         SkPaint::kStroke_Style,
114         SkPaint::kStrokeAndFill_Style,
115     };
116 
117 #define FOR_SETUP(index, array, setter)                                 \
118     for (size_t index = 0; index < std::size(array); ++index) {         \
119         paint.setter(array[index]);
120 
121     SkPaint paint;
122     paint.setAntiAlias(true);
123 
124     // we don't serialize hinting or encoding -- soon to be removed from paint
125 
126     FOR_SETUP(l, caps, setStrokeCap)
127     FOR_SETUP(m, joins, setStrokeJoin)
128     FOR_SETUP(p, styles, setStyle)
129 
130     SkBinaryWriteBuffer writer;
131     SkPaintPriv::Flatten(paint, writer);
132 
133     SkAutoMalloc buf(writer.bytesWritten());
134     writer.writeToMemory(buf.get());
135     SkReadBuffer reader(buf.get(), writer.bytesWritten());
136 
137     SkPaint paint2 = reader.readPaint();
138     REPORTER_ASSERT(reporter, paint2 == paint);
139 
140     }}}
141 #undef FOR_SETUP
142 
143 }
144 
145 // found and fixed for android: not initializing rect for string's of length 0
146 DEF_TEST(Paint_regression_measureText, reporter) {
147 
148     SkFont font;
149     font.setSize(12.0f);
150 
151     SkRect r;
152     r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
153 
154     // test that the rect was reset
155     font.measureText("", 0, SkTextEncoding::kUTF8, &r);
156     REPORTER_ASSERT(reporter, r.isEmpty());
157 }
158 
159 #define ASSERT(expr) REPORTER_ASSERT(r, expr)
160 
161 DEF_TEST(Paint_MoreFlattening, r) {
162     SkPaint paint;
163     paint.setColor(0x00AABBCC);
164     paint.setBlendMode(SkBlendMode::kModulate);
165 
166     SkBinaryWriteBuffer writer;
167     SkPaintPriv::Flatten(paint, writer);
168 
169     SkAutoMalloc buf(writer.bytesWritten());
170     writer.writeToMemory(buf.get());
171     SkReadBuffer reader(buf.get(), writer.bytesWritten());
172 
173     SkPaint other = reader.readPaint();
174     ASSERT(reader.offset() == writer.bytesWritten());
175 
176     // No matter the encoding, these must always hold.
177     ASSERT(other.getColor()    == paint.getColor());
178     ASSERT(other.asBlendMode() == paint.asBlendMode());
179 }
180 
181 DEF_TEST(Paint_nothingToDraw, r) {
182     SkPaint paint;
183 
184     REPORTER_ASSERT(r, !paint.nothingToDraw());
185     paint.setAlpha(0);
186     REPORTER_ASSERT(r, paint.nothingToDraw());
187 
188     paint.setAlpha(0xFF);
189     paint.setBlendMode(SkBlendMode::kDst);
190     REPORTER_ASSERT(r, paint.nothingToDraw());
191 
192     paint.setAlpha(0);
193     paint.setBlendMode(SkBlendMode::kSrcOver);
194 
195     SkColorMatrix cm;
196     cm.setIdentity();   // does not change alpha
197     paint.setColorFilter(SkColorFilters::Matrix(cm));
198     REPORTER_ASSERT(r, paint.nothingToDraw());
199 
200     cm.postTranslate(0, 0, 0, 1.0f/255);    // wacks alpha
201     paint.setColorFilter(SkColorFilters::Matrix(cm));
202     REPORTER_ASSERT(r, !paint.nothingToDraw());
203 }
204 
205 DEF_TEST(Font_getpos, r) {
206     SkFont font;
207     const char text[] = "Hamburgefons!@#!#23425,./;'[]";
208     int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
209     AutoTArray<uint16_t> glyphStorage(count);
210     uint16_t* glyphs = glyphStorage.get();
211     (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
212 
213     AutoTArray<SkScalar> widthStorage(count);
214     AutoTArray<SkScalar> xposStorage(count);
215     AutoTArray<SkPoint> posStorage(count);
216 
217     SkScalar* widths = widthStorage.get();
218     SkScalar* xpos = xposStorage.get();
219     SkPoint* pos = posStorage.get();
220 
221     for (bool subpix : { false, true }) {
222         font.setSubpixel(subpix);
223         for (auto hint : { SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull}) {
224             font.setHinting(hint);
225             for (auto size : { 1.0f, 12.0f, 100.0f }) {
226                 font.setSize(size);
227 
228                 font.getWidths(glyphs, count, widths);
229                 font.getXPos(glyphs, count, xpos, 10);
230                 font.getPos(glyphs, count, pos, {10, 20});
231 
232                 auto nearly_eq = [](SkScalar a, SkScalar b) {
233                     return SkScalarAbs(a - b) < 0.000001f;
234                 };
235 
236                 SkScalar x = 10;
237                 for (int i = 0; i < count; ++i) {
238                     REPORTER_ASSERT(r, nearly_eq(x,  xpos[i]));
239                     REPORTER_ASSERT(r, nearly_eq(x,   pos[i].fX));
240                     REPORTER_ASSERT(r, nearly_eq(20,  pos[i].fY));
241                     x += widths[i];
242                 }
243             }
244         }
245     }
246 }
247 
248 DEF_TEST(Paint_dither, reporter) {
249     SkPaint p;
250     p.setDither(true);
251 
252     bool shouldDither = SkPaintPriv::ShouldDither(p, kBGRA_8888_SkColorType);
253 
254     REPORTER_ASSERT(reporter, !shouldDither);
255 }
256