blob: a5f79ae6750b310671ce0f728b47f821413f5e66 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@google.combcb42ae2013-07-02 13:56:39 +00007
Hal Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkLayerDrawLooper.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040011#include "SkMaskFilter.h"
Cary Clark60ca8672018-03-06 15:09:27 -050012#include "SkPaintPriv.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "SkPath.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000014#include "SkRandom.h"
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +000015#include "SkReadBuffer.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040016#include "SkTo.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000017#include "SkTypeface.h"
Hal Canaryea60b952018-08-21 11:45:46 -040018#include "SkUTF.h"
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +000019#include "SkWriteBuffer.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "Test.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040021#undef ASSERT
reed@google.combcb42ae2013-07-02 13:56:39 +000022
23static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) {
24 char* u8 = (char*)dst;
25 for (int i = 0; i < count; ++i) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040026 int n = SkToInt(SkUTF::ToUTF8(src[i], u8));
reed@google.combcb42ae2013-07-02 13:56:39 +000027 u8 += n;
28 }
29 return u8 - (char*)dst;
30}
31
32static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) {
33 uint16_t* u16 = (uint16_t*)dst;
34 for (int i = 0; i < count; ++i) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040035 int n = SkToInt(SkUTF::ToUTF16(src[i], u16));
reed@google.combcb42ae2013-07-02 13:56:39 +000036 u16 += n;
37 }
38 return (char*)u16 - (char*)dst;
39}
40
41static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) {
42 SkUnichar* u32 = (SkUnichar*)dst;
43 if (src != u32) {
44 memcpy(u32, src, count * sizeof(SkUnichar));
45 }
46 return count * sizeof(SkUnichar);
47}
48
49static SkTypeface::Encoding paint2encoding(const SkPaint& paint) {
50 SkPaint::TextEncoding enc = paint.getTextEncoding();
51 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc);
52 return (SkTypeface::Encoding)enc;
53}
54
55static int find_first_zero(const uint16_t glyphs[], int count) {
56 for (int i = 0; i < count; ++i) {
57 if (0 == glyphs[i]) {
58 return i;
59 }
60 }
61 return count;
62}
63
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000064DEF_TEST(Paint_cmap, reporter) {
65 // need to implement charsToGlyphs on other backends (e.g. linux, win)
66 // before we can run this tests everywhere
67 return;
68
reed@google.combcb42ae2013-07-02 13:56:39 +000069 static const int NGLYPHS = 64;
70
71 SkUnichar src[NGLYPHS];
72 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
73
74 static const struct {
75 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
76 SkPaint::TextEncoding fEncoding;
77 } gRec[] = {
78 { uni_to_utf8, SkPaint::kUTF8_TextEncoding },
79 { uni_to_utf16, SkPaint::kUTF16_TextEncoding },
80 { uni_to_utf32, SkPaint::kUTF32_TextEncoding },
81 };
82
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000083 SkRandom rand;
reed@google.combcb42ae2013-07-02 13:56:39 +000084 SkPaint paint;
bungeman13b9c952016-05-12 10:09:30 -070085 paint.setTypeface(SkTypeface::MakeDefault());
reed@google.combcb42ae2013-07-02 13:56:39 +000086 SkTypeface* face = paint.getTypeface();
87
88 for (int i = 0; i < 1000; ++i) {
89 // generate some random text
90 for (int j = 0; j < NGLYPHS; ++j) {
91 src[j] = ' ' + j;
92 }
93 // inject some random chars, to sometimes abort early
94 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000095
reed@google.combcb42ae2013-07-02 13:56:39 +000096 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
97 paint.setTextEncoding(gRec[k].fEncoding);
98
99 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000100
reed@google.combcb42ae2013-07-02 13:56:39 +0000101 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000102
reed@google.combcb42ae2013-07-02 13:56:39 +0000103 bool contains = paint.containsText(dst, len);
104 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
105 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
106 int index = find_first_zero(glyphs1, NGLYPHS);
107
108 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
109 REPORTER_ASSERT(reporter, index == first);
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000110 REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
reed@google.combcb42ae2013-07-02 13:56:39 +0000111 if (contains) {
112 REPORTER_ASSERT(reporter, NGLYPHS == first);
113 } else {
114 REPORTER_ASSERT(reporter, NGLYPHS > first);
115 }
116 }
117 }
118}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000119
reed@google.com25b3bd52013-05-22 13:55:54 +0000120// temparary api for bicubic, just be sure we can set/clear it
reed93a12152015-03-16 10:08:34 -0700121DEF_TEST(Paint_filterQuality, reporter) {
reed@google.com9cfc83c2013-07-22 17:18:18 +0000122 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000123
reed93a12152015-03-16 10:08:34 -0700124 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000125
reed93a12152015-03-16 10:08:34 -0700126 static const SkFilterQuality gQualitys[] = {
127 kNone_SkFilterQuality,
128 kLow_SkFilterQuality,
129 kMedium_SkFilterQuality,
130 kHigh_SkFilterQuality
reed@google.com9cfc83c2013-07-22 17:18:18 +0000131 };
reed93a12152015-03-16 10:08:34 -0700132 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
133 p0.setFilterQuality(gQualitys[i]);
134 REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000135 p1 = p0;
reed93a12152015-03-16 10:08:34 -0700136 REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000137
138 p0.reset();
reed93a12152015-03-16 10:08:34 -0700139 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000140 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000141}
142
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000143DEF_TEST(Paint_copy, reporter) {
djsollen@google.comb44cd652011-12-01 17:09:21 +0000144 SkPaint paint;
145 // set a few member variables
146 paint.setStyle(SkPaint::kStrokeAndFill_Style);
147 paint.setTextAlign(SkPaint::kLeft_Align);
148 paint.setStrokeWidth(SkIntToScalar(2));
149 // set a few pointers
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000150 SkLayerDrawLooper::Builder looperBuilder;
reed7b380d02016-03-21 13:25:16 -0700151 paint.setLooper(looperBuilder.detach());
Mike Reed1be1f8d2018-03-14 13:01:17 -0400152 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -0700153 SkBlurMask::ConvertRadiusToSigma(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000154
155 // copy the paint using the copy constructor and check they are the same
156 SkPaint copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000157 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000158
djsollen@google.comb44cd652011-12-01 17:09:21 +0000159 // copy the paint using the equal operator and check they are the same
160 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000161 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000162
djsollen@google.comb44cd652011-12-01 17:09:21 +0000163 // clean the paint and check they are back to their initial states
164 SkPaint cleanPaint;
165 paint.reset();
166 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +0000167 REPORTER_ASSERT(reporter, cleanPaint == paint);
168 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000169}
reed@android.coma0f5d152009-06-22 17:38:10 +0000170
171// found and fixed for webkit: mishandling when we hit recursion limit on
172// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000173DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +0000174 SkPath path, stroke;
175 SkPaint paint;
176
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000177 path.moveTo(460.2881309415525f,
178 303.250847066498f);
179 path.cubicTo(463.36378422175284f,
180 302.1169735073363f,
181 456.32239330810046f,
182 304.720354932878f,
183 453.15255460013304f,
184 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185
reed@android.coma0f5d152009-06-22 17:38:10 +0000186 SkRect fillR, strokeR;
187 fillR = path.getBounds();
188
189 paint.setStyle(SkPaint::kStroke_Style);
190 paint.setStrokeWidth(SkIntToScalar(2));
191 paint.getFillPath(path, &stroke);
192 strokeR = stroke.getBounds();
193
194 SkRect maxR = fillR;
195 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
196 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
Mike Reeddf85c382017-02-14 10:59:19 -0500197 paint.getStrokeWidth() * miter :
reed@android.coma0f5d152009-06-22 17:38:10 +0000198 paint.getStrokeWidth();
199 maxR.inset(-inset, -inset);
200
201 // test that our stroke didn't explode
202 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
203}
204
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000205DEF_TEST(Paint_flattening, reporter) {
reed93a12152015-03-16 10:08:34 -0700206 const SkFilterQuality levels[] = {
207 kNone_SkFilterQuality,
208 kLow_SkFilterQuality,
209 kMedium_SkFilterQuality,
210 kHigh_SkFilterQuality,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000211 };
212 const SkPaint::Hinting hinting[] = {
213 SkPaint::kNo_Hinting,
214 SkPaint::kSlight_Hinting,
215 SkPaint::kNormal_Hinting,
216 SkPaint::kFull_Hinting,
217 };
218 const SkPaint::Align align[] = {
219 SkPaint::kLeft_Align,
220 SkPaint::kCenter_Align,
221 SkPaint::kRight_Align
222 };
223 const SkPaint::Cap caps[] = {
224 SkPaint::kButt_Cap,
225 SkPaint::kRound_Cap,
226 SkPaint::kSquare_Cap,
227 };
228 const SkPaint::Join joins[] = {
229 SkPaint::kMiter_Join,
230 SkPaint::kRound_Join,
231 SkPaint::kBevel_Join,
232 };
233 const SkPaint::TextEncoding encodings[] = {
234 SkPaint::kUTF8_TextEncoding,
235 SkPaint::kUTF16_TextEncoding,
236 SkPaint::kUTF32_TextEncoding,
237 SkPaint::kGlyphID_TextEncoding,
238 };
239 const SkPaint::Style styles[] = {
240 SkPaint::kFill_Style,
241 SkPaint::kStroke_Style,
242 SkPaint::kStrokeAndFill_Style,
243 };
244
245#define FOR_SETUP(index, array, setter) \
246 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
247 paint.setter(array[index]); \
248
249 SkPaint paint;
250 paint.setFlags(0x1234);
251
reed93a12152015-03-16 10:08:34 -0700252 FOR_SETUP(i, levels, setFilterQuality)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000253 FOR_SETUP(j, hinting, setHinting)
254 FOR_SETUP(k, align, setTextAlign)
255 FOR_SETUP(l, caps, setStrokeCap)
256 FOR_SETUP(m, joins, setStrokeJoin)
257 FOR_SETUP(n, encodings, setTextEncoding)
258 FOR_SETUP(p, styles, setStyle)
259
brianosmanfad98562016-05-04 11:06:28 -0700260 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500261 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000262
mtkleinb4c899d2016-04-29 13:58:09 -0700263 SkAutoMalloc buf(writer.bytesWritten());
264 writer.writeToMemory(buf.get());
265 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000266
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000267 SkPaint paint2;
Cary Clark60ca8672018-03-06 15:09:27 -0500268 SkPaintPriv::Unflatten(&paint2, reader);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000269 REPORTER_ASSERT(reporter, paint2 == paint);
270
271 }}}}}}}
272#undef FOR_SETUP
273
274}
275
djsollen@google.com46348e22013-03-04 19:47:42 +0000276// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000277DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000278
279 SkPaint paint;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000280 paint.setTextSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000281
282 SkRect r;
283 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
284
285 // test that the rect was reset
reed99ae8812014-08-26 11:30:01 -0700286 paint.measureText("", 0, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000287 REPORTER_ASSERT(reporter, r.isEmpty());
288}
289
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000290#define ASSERT(expr) REPORTER_ASSERT(r, expr)
291
mtklein610a0152014-09-25 11:57:53 -0700292DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000293 SkPaint paint;
294 paint.setColor(0x00AABBCC);
mtkleinee902cd2014-09-22 11:40:33 -0700295 paint.setTextScaleX(1.0f); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000296 paint.setTextSize(19);
reed374772b2016-10-05 17:33:02 -0700297 paint.setBlendMode(SkBlendMode::kModulate);
halcanary96fcdcc2015-08-27 07:41:13 -0700298 paint.setLooper(nullptr); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000299
brianosmanfad98562016-05-04 11:06:28 -0700300 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500301 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000302
mtkleinb4c899d2016-04-29 13:58:09 -0700303 SkAutoMalloc buf(writer.bytesWritten());
304 writer.writeToMemory(buf.get());
305 SkReadBuffer reader(buf.get(), writer.bytesWritten());
306
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000307 SkPaint other;
Cary Clark60ca8672018-03-06 15:09:27 -0500308 SkPaintPriv::Unflatten(&other, reader);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000309 ASSERT(reader.offset() == writer.bytesWritten());
310
311 // No matter the encoding, these must always hold.
312 ASSERT(other.getColor() == paint.getColor());
313 ASSERT(other.getTextScaleX() == paint.getTextScaleX());
314 ASSERT(other.getTextSize() == paint.getTextSize());
315 ASSERT(other.getLooper() == paint.getLooper());
reed374772b2016-10-05 17:33:02 -0700316 ASSERT(other.getBlendMode() == paint.getBlendMode());
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000317}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700318
319DEF_TEST(Paint_getHash, r) {
320 // Try not to inspect the actual hash values in here.
321 // We might want to change the hash function.
322
323 SkPaint paint;
324 const uint32_t defaultHash = paint.getHash();
325
326 // Check that some arbitrary field affects the hash.
327 paint.setColor(0xFF00FF00);
328 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
329 paint.setColor(SK_ColorBLACK); // Reset to default value.
330 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
331
332 // SkTypeface is the first field we hash, so test it specially.
bungeman13b9c952016-05-12 10:09:30 -0700333 paint.setTypeface(SkTypeface::MakeDefault());
mtkleinfb1fe4f2014-10-07 09:26:10 -0700334 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
halcanary96fcdcc2015-08-27 07:41:13 -0700335 paint.setTypeface(nullptr);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700336 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
337
338 // This is part of fBitfields, the last field we hash.
339 paint.setHinting(SkPaint::kSlight_Hinting);
340 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
341 paint.setHinting(SkPaint::kNormal_Hinting);
342 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
343}
reedf539b8c2014-11-11 12:51:33 -0800344
345#include "SkColorMatrixFilter.h"
346
347DEF_TEST(Paint_nothingToDraw, r) {
348 SkPaint paint;
349
350 REPORTER_ASSERT(r, !paint.nothingToDraw());
351 paint.setAlpha(0);
352 REPORTER_ASSERT(r, paint.nothingToDraw());
353
354 paint.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700355 paint.setBlendMode(SkBlendMode::kDst);
reedf539b8c2014-11-11 12:51:33 -0800356 REPORTER_ASSERT(r, paint.nothingToDraw());
357
358 paint.setAlpha(0);
reed374772b2016-10-05 17:33:02 -0700359 paint.setBlendMode(SkBlendMode::kSrcOver);
reedf539b8c2014-11-11 12:51:33 -0800360
361 SkColorMatrix cm;
362 cm.setIdentity(); // does not change alpha
reedd053ce92016-03-22 10:17:23 -0700363 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800364 REPORTER_ASSERT(r, paint.nothingToDraw());
365
366 cm.postTranslate(0, 0, 0, 1); // wacks alpha
reedd053ce92016-03-22 10:17:23 -0700367 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800368 REPORTER_ASSERT(r, !paint.nothingToDraw());
369}