blob: 2794058ceb87e0d6a220ea7b071413edb538a866 [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"
18#include "SkUtils.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"
reed@google.combcb42ae2013-07-02 13:56:39 +000021
22static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) {
23 char* u8 = (char*)dst;
24 for (int i = 0; i < count; ++i) {
bsalomonfbaace02014-12-12 16:41:46 -080025 int n = SkToInt(SkUTF8_FromUnichar(src[i], u8));
reed@google.combcb42ae2013-07-02 13:56:39 +000026 u8 += n;
27 }
28 return u8 - (char*)dst;
29}
30
31static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) {
32 uint16_t* u16 = (uint16_t*)dst;
33 for (int i = 0; i < count; ++i) {
bsalomonfbaace02014-12-12 16:41:46 -080034 int n = SkToInt(SkUTF16_FromUnichar(src[i], u16));
reed@google.combcb42ae2013-07-02 13:56:39 +000035 u16 += n;
36 }
37 return (char*)u16 - (char*)dst;
38}
39
40static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) {
41 SkUnichar* u32 = (SkUnichar*)dst;
42 if (src != u32) {
43 memcpy(u32, src, count * sizeof(SkUnichar));
44 }
45 return count * sizeof(SkUnichar);
46}
47
48static SkTypeface::Encoding paint2encoding(const SkPaint& paint) {
49 SkPaint::TextEncoding enc = paint.getTextEncoding();
50 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc);
51 return (SkTypeface::Encoding)enc;
52}
53
54static int find_first_zero(const uint16_t glyphs[], int count) {
55 for (int i = 0; i < count; ++i) {
56 if (0 == glyphs[i]) {
57 return i;
58 }
59 }
60 return count;
61}
62
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000063DEF_TEST(Paint_cmap, reporter) {
64 // need to implement charsToGlyphs on other backends (e.g. linux, win)
65 // before we can run this tests everywhere
66 return;
67
reed@google.combcb42ae2013-07-02 13:56:39 +000068 static const int NGLYPHS = 64;
69
70 SkUnichar src[NGLYPHS];
71 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
72
73 static const struct {
74 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
75 SkPaint::TextEncoding fEncoding;
76 } gRec[] = {
77 { uni_to_utf8, SkPaint::kUTF8_TextEncoding },
78 { uni_to_utf16, SkPaint::kUTF16_TextEncoding },
79 { uni_to_utf32, SkPaint::kUTF32_TextEncoding },
80 };
81
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000082 SkRandom rand;
reed@google.combcb42ae2013-07-02 13:56:39 +000083 SkPaint paint;
bungeman13b9c952016-05-12 10:09:30 -070084 paint.setTypeface(SkTypeface::MakeDefault());
reed@google.combcb42ae2013-07-02 13:56:39 +000085 SkTypeface* face = paint.getTypeface();
86
87 for (int i = 0; i < 1000; ++i) {
88 // generate some random text
89 for (int j = 0; j < NGLYPHS; ++j) {
90 src[j] = ' ' + j;
91 }
92 // inject some random chars, to sometimes abort early
93 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000094
reed@google.combcb42ae2013-07-02 13:56:39 +000095 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
96 paint.setTextEncoding(gRec[k].fEncoding);
97
98 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000099
reed@google.combcb42ae2013-07-02 13:56:39 +0000100 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000101
reed@google.combcb42ae2013-07-02 13:56:39 +0000102 bool contains = paint.containsText(dst, len);
103 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
104 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
105 int index = find_first_zero(glyphs1, NGLYPHS);
106
107 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
108 REPORTER_ASSERT(reporter, index == first);
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000109 REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
reed@google.combcb42ae2013-07-02 13:56:39 +0000110 if (contains) {
111 REPORTER_ASSERT(reporter, NGLYPHS == first);
112 } else {
113 REPORTER_ASSERT(reporter, NGLYPHS > first);
114 }
115 }
116 }
117}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000118
reed@google.com25b3bd52013-05-22 13:55:54 +0000119// temparary api for bicubic, just be sure we can set/clear it
reed93a12152015-03-16 10:08:34 -0700120DEF_TEST(Paint_filterQuality, reporter) {
reed@google.com9cfc83c2013-07-22 17:18:18 +0000121 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000122
reed93a12152015-03-16 10:08:34 -0700123 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000124
reed93a12152015-03-16 10:08:34 -0700125 static const SkFilterQuality gQualitys[] = {
126 kNone_SkFilterQuality,
127 kLow_SkFilterQuality,
128 kMedium_SkFilterQuality,
129 kHigh_SkFilterQuality
reed@google.com9cfc83c2013-07-22 17:18:18 +0000130 };
reed93a12152015-03-16 10:08:34 -0700131 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
132 p0.setFilterQuality(gQualitys[i]);
133 REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000134 p1 = p0;
reed93a12152015-03-16 10:08:34 -0700135 REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000136
137 p0.reset();
reed93a12152015-03-16 10:08:34 -0700138 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000139 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000140}
141
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000142DEF_TEST(Paint_copy, reporter) {
djsollen@google.comb44cd652011-12-01 17:09:21 +0000143 SkPaint paint;
144 // set a few member variables
145 paint.setStyle(SkPaint::kStrokeAndFill_Style);
146 paint.setTextAlign(SkPaint::kLeft_Align);
147 paint.setStrokeWidth(SkIntToScalar(2));
148 // set a few pointers
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000149 SkLayerDrawLooper::Builder looperBuilder;
reed7b380d02016-03-21 13:25:16 -0700150 paint.setLooper(looperBuilder.detach());
Mike Reed1be1f8d2018-03-14 13:01:17 -0400151 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -0700152 SkBlurMask::ConvertRadiusToSigma(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000153
154 // copy the paint using the copy constructor and check they are the same
155 SkPaint copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000156 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000157
djsollen@google.comb44cd652011-12-01 17:09:21 +0000158 // copy the paint using the equal operator and check they are the same
159 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000160 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000161
djsollen@google.comb44cd652011-12-01 17:09:21 +0000162 // clean the paint and check they are back to their initial states
163 SkPaint cleanPaint;
164 paint.reset();
165 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +0000166 REPORTER_ASSERT(reporter, cleanPaint == paint);
167 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000168}
reed@android.coma0f5d152009-06-22 17:38:10 +0000169
170// found and fixed for webkit: mishandling when we hit recursion limit on
171// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000172DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +0000173 SkPath path, stroke;
174 SkPaint paint;
175
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000176 path.moveTo(460.2881309415525f,
177 303.250847066498f);
178 path.cubicTo(463.36378422175284f,
179 302.1169735073363f,
180 456.32239330810046f,
181 304.720354932878f,
182 453.15255460013304f,
183 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184
reed@android.coma0f5d152009-06-22 17:38:10 +0000185 SkRect fillR, strokeR;
186 fillR = path.getBounds();
187
188 paint.setStyle(SkPaint::kStroke_Style);
189 paint.setStrokeWidth(SkIntToScalar(2));
190 paint.getFillPath(path, &stroke);
191 strokeR = stroke.getBounds();
192
193 SkRect maxR = fillR;
194 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
195 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
Mike Reeddf85c382017-02-14 10:59:19 -0500196 paint.getStrokeWidth() * miter :
reed@android.coma0f5d152009-06-22 17:38:10 +0000197 paint.getStrokeWidth();
198 maxR.inset(-inset, -inset);
199
200 // test that our stroke didn't explode
201 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
202}
203
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000204DEF_TEST(Paint_flattening, reporter) {
reed93a12152015-03-16 10:08:34 -0700205 const SkFilterQuality levels[] = {
206 kNone_SkFilterQuality,
207 kLow_SkFilterQuality,
208 kMedium_SkFilterQuality,
209 kHigh_SkFilterQuality,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000210 };
211 const SkPaint::Hinting hinting[] = {
212 SkPaint::kNo_Hinting,
213 SkPaint::kSlight_Hinting,
214 SkPaint::kNormal_Hinting,
215 SkPaint::kFull_Hinting,
216 };
217 const SkPaint::Align align[] = {
218 SkPaint::kLeft_Align,
219 SkPaint::kCenter_Align,
220 SkPaint::kRight_Align
221 };
222 const SkPaint::Cap caps[] = {
223 SkPaint::kButt_Cap,
224 SkPaint::kRound_Cap,
225 SkPaint::kSquare_Cap,
226 };
227 const SkPaint::Join joins[] = {
228 SkPaint::kMiter_Join,
229 SkPaint::kRound_Join,
230 SkPaint::kBevel_Join,
231 };
232 const SkPaint::TextEncoding encodings[] = {
233 SkPaint::kUTF8_TextEncoding,
234 SkPaint::kUTF16_TextEncoding,
235 SkPaint::kUTF32_TextEncoding,
236 SkPaint::kGlyphID_TextEncoding,
237 };
238 const SkPaint::Style styles[] = {
239 SkPaint::kFill_Style,
240 SkPaint::kStroke_Style,
241 SkPaint::kStrokeAndFill_Style,
242 };
243
244#define FOR_SETUP(index, array, setter) \
245 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
246 paint.setter(array[index]); \
247
248 SkPaint paint;
249 paint.setFlags(0x1234);
250
reed93a12152015-03-16 10:08:34 -0700251 FOR_SETUP(i, levels, setFilterQuality)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000252 FOR_SETUP(j, hinting, setHinting)
253 FOR_SETUP(k, align, setTextAlign)
254 FOR_SETUP(l, caps, setStrokeCap)
255 FOR_SETUP(m, joins, setStrokeJoin)
256 FOR_SETUP(n, encodings, setTextEncoding)
257 FOR_SETUP(p, styles, setStyle)
258
brianosmanfad98562016-05-04 11:06:28 -0700259 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500260 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000261
mtkleinb4c899d2016-04-29 13:58:09 -0700262 SkAutoMalloc buf(writer.bytesWritten());
263 writer.writeToMemory(buf.get());
264 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000265
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000266 SkPaint paint2;
Cary Clark60ca8672018-03-06 15:09:27 -0500267 SkPaintPriv::Unflatten(&paint2, reader);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000268 REPORTER_ASSERT(reporter, paint2 == paint);
269
270 }}}}}}}
271#undef FOR_SETUP
272
273}
274
djsollen@google.com46348e22013-03-04 19:47:42 +0000275// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000276DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000277
278 SkPaint paint;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000279 paint.setTextSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000280
281 SkRect r;
282 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
283
284 // test that the rect was reset
reed99ae8812014-08-26 11:30:01 -0700285 paint.measureText("", 0, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000286 REPORTER_ASSERT(reporter, r.isEmpty());
287}
288
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000289#define ASSERT(expr) REPORTER_ASSERT(r, expr)
290
mtklein610a0152014-09-25 11:57:53 -0700291DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000292 SkPaint paint;
293 paint.setColor(0x00AABBCC);
mtkleinee902cd2014-09-22 11:40:33 -0700294 paint.setTextScaleX(1.0f); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000295 paint.setTextSize(19);
reed374772b2016-10-05 17:33:02 -0700296 paint.setBlendMode(SkBlendMode::kModulate);
halcanary96fcdcc2015-08-27 07:41:13 -0700297 paint.setLooper(nullptr); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000298
brianosmanfad98562016-05-04 11:06:28 -0700299 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500300 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000301
mtkleinb4c899d2016-04-29 13:58:09 -0700302 SkAutoMalloc buf(writer.bytesWritten());
303 writer.writeToMemory(buf.get());
304 SkReadBuffer reader(buf.get(), writer.bytesWritten());
305
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000306 SkPaint other;
Cary Clark60ca8672018-03-06 15:09:27 -0500307 SkPaintPriv::Unflatten(&other, reader);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000308 ASSERT(reader.offset() == writer.bytesWritten());
309
310 // No matter the encoding, these must always hold.
311 ASSERT(other.getColor() == paint.getColor());
312 ASSERT(other.getTextScaleX() == paint.getTextScaleX());
313 ASSERT(other.getTextSize() == paint.getTextSize());
314 ASSERT(other.getLooper() == paint.getLooper());
reed374772b2016-10-05 17:33:02 -0700315 ASSERT(other.getBlendMode() == paint.getBlendMode());
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000316}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700317
318DEF_TEST(Paint_getHash, r) {
319 // Try not to inspect the actual hash values in here.
320 // We might want to change the hash function.
321
322 SkPaint paint;
323 const uint32_t defaultHash = paint.getHash();
324
325 // Check that some arbitrary field affects the hash.
326 paint.setColor(0xFF00FF00);
327 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
328 paint.setColor(SK_ColorBLACK); // Reset to default value.
329 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
330
331 // SkTypeface is the first field we hash, so test it specially.
bungeman13b9c952016-05-12 10:09:30 -0700332 paint.setTypeface(SkTypeface::MakeDefault());
mtkleinfb1fe4f2014-10-07 09:26:10 -0700333 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
halcanary96fcdcc2015-08-27 07:41:13 -0700334 paint.setTypeface(nullptr);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700335 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
336
337 // This is part of fBitfields, the last field we hash.
338 paint.setHinting(SkPaint::kSlight_Hinting);
339 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
340 paint.setHinting(SkPaint::kNormal_Hinting);
341 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
342}
reedf539b8c2014-11-11 12:51:33 -0800343
344#include "SkColorMatrixFilter.h"
345
346DEF_TEST(Paint_nothingToDraw, r) {
347 SkPaint paint;
348
349 REPORTER_ASSERT(r, !paint.nothingToDraw());
350 paint.setAlpha(0);
351 REPORTER_ASSERT(r, paint.nothingToDraw());
352
353 paint.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700354 paint.setBlendMode(SkBlendMode::kDst);
reedf539b8c2014-11-11 12:51:33 -0800355 REPORTER_ASSERT(r, paint.nothingToDraw());
356
357 paint.setAlpha(0);
reed374772b2016-10-05 17:33:02 -0700358 paint.setBlendMode(SkBlendMode::kSrcOver);
reedf539b8c2014-11-11 12:51:33 -0800359
360 SkColorMatrix cm;
361 cm.setIdentity(); // does not change alpha
reedd053ce92016-03-22 10:17:23 -0700362 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800363 REPORTER_ASSERT(r, paint.nothingToDraw());
364
365 cm.postTranslate(0, 0, 0, 1); // wacks 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}