blob: f50746777149f68f2aef5517a2de9976e8e450eb [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
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
9#include "SkBlurMaskFilter.h"
10#include "SkLayerDrawLooper.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000011#include "SkPaint.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "SkPath.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000013#include "SkRandom.h"
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +000014#include "SkReadBuffer.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000015#include "SkTypeface.h"
16#include "SkUtils.h"
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +000017#include "SkWriteBuffer.h"
18#include "SkXfermode.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000019#include "Test.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000020
21static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) {
22 char* u8 = (char*)dst;
23 for (int i = 0; i < count; ++i) {
bsalomonfbaace02014-12-12 16:41:46 -080024 int n = SkToInt(SkUTF8_FromUnichar(src[i], u8));
reed@google.combcb42ae2013-07-02 13:56:39 +000025 u8 += n;
26 }
27 return u8 - (char*)dst;
28}
29
30static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) {
31 uint16_t* u16 = (uint16_t*)dst;
32 for (int i = 0; i < count; ++i) {
bsalomonfbaace02014-12-12 16:41:46 -080033 int n = SkToInt(SkUTF16_FromUnichar(src[i], u16));
reed@google.combcb42ae2013-07-02 13:56:39 +000034 u16 += n;
35 }
36 return (char*)u16 - (char*)dst;
37}
38
39static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) {
40 SkUnichar* u32 = (SkUnichar*)dst;
41 if (src != u32) {
42 memcpy(u32, src, count * sizeof(SkUnichar));
43 }
44 return count * sizeof(SkUnichar);
45}
46
47static SkTypeface::Encoding paint2encoding(const SkPaint& paint) {
48 SkPaint::TextEncoding enc = paint.getTextEncoding();
49 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc);
50 return (SkTypeface::Encoding)enc;
51}
52
53static int find_first_zero(const uint16_t glyphs[], int count) {
54 for (int i = 0; i < count; ++i) {
55 if (0 == glyphs[i]) {
56 return i;
57 }
58 }
59 return count;
60}
61
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000062DEF_TEST(Paint_cmap, reporter) {
63 // need to implement charsToGlyphs on other backends (e.g. linux, win)
64 // before we can run this tests everywhere
65 return;
66
reed@google.combcb42ae2013-07-02 13:56:39 +000067 static const int NGLYPHS = 64;
68
69 SkUnichar src[NGLYPHS];
70 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
71
72 static const struct {
73 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
74 SkPaint::TextEncoding fEncoding;
75 } gRec[] = {
76 { uni_to_utf8, SkPaint::kUTF8_TextEncoding },
77 { uni_to_utf16, SkPaint::kUTF16_TextEncoding },
78 { uni_to_utf32, SkPaint::kUTF32_TextEncoding },
79 };
80
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000081 SkRandom rand;
reed@google.combcb42ae2013-07-02 13:56:39 +000082 SkPaint paint;
bungeman13b9c952016-05-12 10:09:30 -070083 paint.setTypeface(SkTypeface::MakeDefault());
reed@google.combcb42ae2013-07-02 13:56:39 +000084 SkTypeface* face = paint.getTypeface();
85
86 for (int i = 0; i < 1000; ++i) {
87 // generate some random text
88 for (int j = 0; j < NGLYPHS; ++j) {
89 src[j] = ' ' + j;
90 }
91 // inject some random chars, to sometimes abort early
92 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000093
reed@google.combcb42ae2013-07-02 13:56:39 +000094 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
95 paint.setTextEncoding(gRec[k].fEncoding);
96
97 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000098
reed@google.combcb42ae2013-07-02 13:56:39 +000099 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000100
reed@google.combcb42ae2013-07-02 13:56:39 +0000101 bool contains = paint.containsText(dst, len);
102 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
103 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
104 int index = find_first_zero(glyphs1, NGLYPHS);
105
106 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
107 REPORTER_ASSERT(reporter, index == first);
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000108 REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
reed@google.combcb42ae2013-07-02 13:56:39 +0000109 if (contains) {
110 REPORTER_ASSERT(reporter, NGLYPHS == first);
111 } else {
112 REPORTER_ASSERT(reporter, NGLYPHS > first);
113 }
114 }
115 }
116}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000117
reed@google.com25b3bd52013-05-22 13:55:54 +0000118// temparary api for bicubic, just be sure we can set/clear it
reed93a12152015-03-16 10:08:34 -0700119DEF_TEST(Paint_filterQuality, reporter) {
reed@google.com9cfc83c2013-07-22 17:18:18 +0000120 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000121
reed93a12152015-03-16 10:08:34 -0700122 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000123
reed93a12152015-03-16 10:08:34 -0700124 static const SkFilterQuality gQualitys[] = {
125 kNone_SkFilterQuality,
126 kLow_SkFilterQuality,
127 kMedium_SkFilterQuality,
128 kHigh_SkFilterQuality
reed@google.com9cfc83c2013-07-22 17:18:18 +0000129 };
reed93a12152015-03-16 10:08:34 -0700130 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
131 p0.setFilterQuality(gQualitys[i]);
132 REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000133 p1 = p0;
reed93a12152015-03-16 10:08:34 -0700134 REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000135
136 p0.reset();
reed93a12152015-03-16 10:08:34 -0700137 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000138 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000139}
140
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000141DEF_TEST(Paint_copy, reporter) {
djsollen@google.comb44cd652011-12-01 17:09:21 +0000142 SkPaint paint;
143 // set a few member variables
144 paint.setStyle(SkPaint::kStrokeAndFill_Style);
145 paint.setTextAlign(SkPaint::kLeft_Align);
146 paint.setStrokeWidth(SkIntToScalar(2));
147 // set a few pointers
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000148 SkLayerDrawLooper::Builder looperBuilder;
reed7b380d02016-03-21 13:25:16 -0700149 paint.setLooper(looperBuilder.detach());
reedefdfd512016-04-04 10:02:58 -0700150 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
151 SkBlurMask::ConvertRadiusToSigma(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000152
153 // copy the paint using the copy constructor and check they are the same
154 SkPaint copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000155 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000156
djsollen@google.comb44cd652011-12-01 17:09:21 +0000157 // copy the paint using the equal operator and check they are the same
158 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000159 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000160
djsollen@google.comb44cd652011-12-01 17:09:21 +0000161 // clean the paint and check they are back to their initial states
162 SkPaint cleanPaint;
163 paint.reset();
164 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +0000165 REPORTER_ASSERT(reporter, cleanPaint == paint);
166 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000167}
reed@android.coma0f5d152009-06-22 17:38:10 +0000168
169// found and fixed for webkit: mishandling when we hit recursion limit on
170// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000171DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +0000172 SkPath path, stroke;
173 SkPaint paint;
174
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000175 path.moveTo(460.2881309415525f,
176 303.250847066498f);
177 path.cubicTo(463.36378422175284f,
178 302.1169735073363f,
179 456.32239330810046f,
180 304.720354932878f,
181 453.15255460013304f,
182 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183
reed@android.coma0f5d152009-06-22 17:38:10 +0000184 SkRect fillR, strokeR;
185 fillR = path.getBounds();
186
187 paint.setStyle(SkPaint::kStroke_Style);
188 paint.setStrokeWidth(SkIntToScalar(2));
189 paint.getFillPath(path, &stroke);
190 strokeR = stroke.getBounds();
191
192 SkRect maxR = fillR;
193 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
194 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
195 SkScalarMul(paint.getStrokeWidth(), miter) :
196 paint.getStrokeWidth();
197 maxR.inset(-inset, -inset);
198
199 // test that our stroke didn't explode
200 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
201}
202
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000203DEF_TEST(Paint_flattening, reporter) {
reed93a12152015-03-16 10:08:34 -0700204 const SkFilterQuality levels[] = {
205 kNone_SkFilterQuality,
206 kLow_SkFilterQuality,
207 kMedium_SkFilterQuality,
208 kHigh_SkFilterQuality,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000209 };
210 const SkPaint::Hinting hinting[] = {
211 SkPaint::kNo_Hinting,
212 SkPaint::kSlight_Hinting,
213 SkPaint::kNormal_Hinting,
214 SkPaint::kFull_Hinting,
215 };
216 const SkPaint::Align align[] = {
217 SkPaint::kLeft_Align,
218 SkPaint::kCenter_Align,
219 SkPaint::kRight_Align
220 };
221 const SkPaint::Cap caps[] = {
222 SkPaint::kButt_Cap,
223 SkPaint::kRound_Cap,
224 SkPaint::kSquare_Cap,
225 };
226 const SkPaint::Join joins[] = {
227 SkPaint::kMiter_Join,
228 SkPaint::kRound_Join,
229 SkPaint::kBevel_Join,
230 };
231 const SkPaint::TextEncoding encodings[] = {
232 SkPaint::kUTF8_TextEncoding,
233 SkPaint::kUTF16_TextEncoding,
234 SkPaint::kUTF32_TextEncoding,
235 SkPaint::kGlyphID_TextEncoding,
236 };
237 const SkPaint::Style styles[] = {
238 SkPaint::kFill_Style,
239 SkPaint::kStroke_Style,
240 SkPaint::kStrokeAndFill_Style,
241 };
242
243#define FOR_SETUP(index, array, setter) \
244 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
245 paint.setter(array[index]); \
246
247 SkPaint paint;
248 paint.setFlags(0x1234);
249
reed93a12152015-03-16 10:08:34 -0700250 FOR_SETUP(i, levels, setFilterQuality)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000251 FOR_SETUP(j, hinting, setHinting)
252 FOR_SETUP(k, align, setTextAlign)
253 FOR_SETUP(l, caps, setStrokeCap)
254 FOR_SETUP(m, joins, setStrokeJoin)
255 FOR_SETUP(n, encodings, setTextEncoding)
256 FOR_SETUP(p, styles, setStyle)
257
brianosmanfad98562016-05-04 11:06:28 -0700258 SkBinaryWriteBuffer writer;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000259 paint.flatten(writer);
260
mtkleinb4c899d2016-04-29 13:58:09 -0700261 SkAutoMalloc buf(writer.bytesWritten());
262 writer.writeToMemory(buf.get());
263 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000264
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000265 SkPaint paint2;
266 paint2.unflatten(reader);
267 REPORTER_ASSERT(reporter, paint2 == paint);
268
269 }}}}}}}
270#undef FOR_SETUP
271
272}
273
djsollen@google.com46348e22013-03-04 19:47:42 +0000274// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000275DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000276
277 SkPaint paint;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000278 paint.setTextSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000279
280 SkRect r;
281 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
282
283 // test that the rect was reset
reed99ae8812014-08-26 11:30:01 -0700284 paint.measureText("", 0, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000285 REPORTER_ASSERT(reporter, r.isEmpty());
286}
287
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000288#define ASSERT(expr) REPORTER_ASSERT(r, expr)
289
mtklein610a0152014-09-25 11:57:53 -0700290DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000291 SkPaint paint;
292 paint.setColor(0x00AABBCC);
mtkleinee902cd2014-09-22 11:40:33 -0700293 paint.setTextScaleX(1.0f); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000294 paint.setTextSize(19);
reedcfb6bdf2016-03-29 11:32:50 -0700295 paint.setXfermode(SkXfermode::Make(SkXfermode::kModulate_Mode));
halcanary96fcdcc2015-08-27 07:41:13 -0700296 paint.setLooper(nullptr); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000297
brianosmanfad98562016-05-04 11:06:28 -0700298 SkBinaryWriteBuffer writer;
mtklein610a0152014-09-25 11:57:53 -0700299 paint.flatten(writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000300
mtkleinb4c899d2016-04-29 13:58:09 -0700301 SkAutoMalloc buf(writer.bytesWritten());
302 writer.writeToMemory(buf.get());
303 SkReadBuffer reader(buf.get(), writer.bytesWritten());
304
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000305 SkPaint other;
mtklein610a0152014-09-25 11:57:53 -0700306 other.unflatten(reader);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000307 ASSERT(reader.offset() == writer.bytesWritten());
308
309 // No matter the encoding, these must always hold.
310 ASSERT(other.getColor() == paint.getColor());
311 ASSERT(other.getTextScaleX() == paint.getTextScaleX());
312 ASSERT(other.getTextSize() == paint.getTextSize());
313 ASSERT(other.getLooper() == paint.getLooper());
314
315 // We have to be a little looser and compare just the modes. Pointers might not be the same.
316 SkXfermode::Mode otherMode, paintMode;
317 ASSERT(other.getXfermode()->asMode(&otherMode));
318 ASSERT(paint.getXfermode()->asMode(&paintMode));
319 ASSERT(otherMode == paintMode);
320}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700321
322DEF_TEST(Paint_getHash, r) {
323 // Try not to inspect the actual hash values in here.
324 // We might want to change the hash function.
325
326 SkPaint paint;
327 const uint32_t defaultHash = paint.getHash();
328
329 // Check that some arbitrary field affects the hash.
330 paint.setColor(0xFF00FF00);
331 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
332 paint.setColor(SK_ColorBLACK); // Reset to default value.
333 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
334
335 // SkTypeface is the first field we hash, so test it specially.
bungeman13b9c952016-05-12 10:09:30 -0700336 paint.setTypeface(SkTypeface::MakeDefault());
mtkleinfb1fe4f2014-10-07 09:26:10 -0700337 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
halcanary96fcdcc2015-08-27 07:41:13 -0700338 paint.setTypeface(nullptr);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700339 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
340
341 // This is part of fBitfields, the last field we hash.
342 paint.setHinting(SkPaint::kSlight_Hinting);
343 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
344 paint.setHinting(SkPaint::kNormal_Hinting);
345 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
346}
reedf539b8c2014-11-11 12:51:33 -0800347
348#include "SkColorMatrixFilter.h"
349
350DEF_TEST(Paint_nothingToDraw, r) {
351 SkPaint paint;
352
353 REPORTER_ASSERT(r, !paint.nothingToDraw());
354 paint.setAlpha(0);
355 REPORTER_ASSERT(r, paint.nothingToDraw());
356
357 paint.setAlpha(0xFF);
358 paint.setXfermodeMode(SkXfermode::kDst_Mode);
359 REPORTER_ASSERT(r, paint.nothingToDraw());
360
361 paint.setAlpha(0);
362 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
363
364 SkColorMatrix cm;
365 cm.setIdentity(); // does not change alpha
reedd053ce92016-03-22 10:17:23 -0700366 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800367 REPORTER_ASSERT(r, paint.nothingToDraw());
368
369 cm.postTranslate(0, 0, 0, 1); // wacks alpha
reedd053ce92016-03-22 10:17:23 -0700370 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800371 REPORTER_ASSERT(r, !paint.nothingToDraw());
372}