blob: e7954b913685aefa3b946a1e3e82d3a304e155db [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) {
24 int n = SkUTF8_FromUnichar(src[i], u8);
25 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) {
33 int n = SkUTF16_FromUnichar(src[i], u16);
34 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
62static void test_cmap(skiatest::Reporter* reporter) {
63 static const int NGLYPHS = 64;
64
65 SkUnichar src[NGLYPHS];
66 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
67
68 static const struct {
69 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
70 SkPaint::TextEncoding fEncoding;
71 } gRec[] = {
72 { uni_to_utf8, SkPaint::kUTF8_TextEncoding },
73 { uni_to_utf16, SkPaint::kUTF16_TextEncoding },
74 { uni_to_utf32, SkPaint::kUTF32_TextEncoding },
75 };
76
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000077 SkRandom rand;
reed@google.combcb42ae2013-07-02 13:56:39 +000078 SkPaint paint;
79 paint.setTypeface(SkTypeface::RefDefault())->unref();
80 SkTypeface* face = paint.getTypeface();
81
82 for (int i = 0; i < 1000; ++i) {
83 // generate some random text
84 for (int j = 0; j < NGLYPHS; ++j) {
85 src[j] = ' ' + j;
86 }
87 // inject some random chars, to sometimes abort early
88 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000089
reed@google.combcb42ae2013-07-02 13:56:39 +000090 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
91 paint.setTextEncoding(gRec[k].fEncoding);
92
93 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000094
reed@google.combcb42ae2013-07-02 13:56:39 +000095 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000096
reed@google.combcb42ae2013-07-02 13:56:39 +000097 bool contains = paint.containsText(dst, len);
98 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
99 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
100 int index = find_first_zero(glyphs1, NGLYPHS);
101
102 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
103 REPORTER_ASSERT(reporter, index == first);
104 REPORTER_ASSERT(reporter,
105 !memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
106 if (contains) {
107 REPORTER_ASSERT(reporter, NGLYPHS == first);
108 } else {
109 REPORTER_ASSERT(reporter, NGLYPHS > first);
110 }
111 }
112 }
113}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000114
reed@google.com25b3bd52013-05-22 13:55:54 +0000115// temparary api for bicubic, just be sure we can set/clear it
reed@google.com9cfc83c2013-07-22 17:18:18 +0000116static void test_filterlevel(skiatest::Reporter* reporter) {
117 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000118
reed@google.com9cfc83c2013-07-22 17:18:18 +0000119 REPORTER_ASSERT(reporter,
120 SkPaint::kNone_FilterLevel == p0.getFilterLevel());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000121
reed@google.com9cfc83c2013-07-22 17:18:18 +0000122 static const SkPaint::FilterLevel gLevels[] = {
123 SkPaint::kNone_FilterLevel,
124 SkPaint::kLow_FilterLevel,
125 SkPaint::kMedium_FilterLevel,
126 SkPaint::kHigh_FilterLevel
127 };
128 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) {
129 p0.setFilterLevel(gLevels[i]);
130 REPORTER_ASSERT(reporter, gLevels[i] == p0.getFilterLevel());
131 p1 = p0;
132 REPORTER_ASSERT(reporter, gLevels[i] == p1.getFilterLevel());
133
134 p0.reset();
135 REPORTER_ASSERT(reporter,
136 SkPaint::kNone_FilterLevel == p0.getFilterLevel());
137 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000138}
139
djsollen@google.comb44cd652011-12-01 17:09:21 +0000140static void test_copy(skiatest::Reporter* reporter) {
141 SkPaint paint;
142 // set a few member variables
143 paint.setStyle(SkPaint::kStrokeAndFill_Style);
144 paint.setTextAlign(SkPaint::kLeft_Align);
145 paint.setStrokeWidth(SkIntToScalar(2));
146 // set a few pointers
147 SkLayerDrawLooper* looper = new SkLayerDrawLooper();
148 paint.setLooper(looper)->unref();
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +0000149 SkMaskFilter* mask = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000150 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000151 paint.setMaskFilter(mask)->unref();
152
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
157#ifdef SK_BUILD_FOR_ANDROID
158 // the copy constructor should preserve the Generation ID
djsollen@google.comefbe8e92013-02-07 18:58:35 +0000159 uint32_t paintGenID = paint.getGenerationID();
160 uint32_t copiedPaintGenID = copiedPaint.getGenerationID();
djsollen@google.comb44cd652011-12-01 17:09:21 +0000161 REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID);
robertphillips@google.comb2657412013-08-07 22:36:29 +0000162 REPORTER_ASSERT(reporter, !memcmp(&paint, &copiedPaint, sizeof(paint)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000163#endif
164
165 // copy the paint using the equal operator and check they are the same
166 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000167 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000168
169#ifdef SK_BUILD_FOR_ANDROID
170 // the equals operator should increment the Generation ID
171 REPORTER_ASSERT(reporter, paint.getGenerationID() == paintGenID);
172 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
173 copiedPaintGenID = copiedPaint.getGenerationID(); // reset to the new value
174 REPORTER_ASSERT(reporter, memcmp(&paint, &copiedPaint, sizeof(paint)));
175#endif
176
177 // clean the paint and check they are back to their initial states
178 SkPaint cleanPaint;
179 paint.reset();
180 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +0000181 REPORTER_ASSERT(reporter, cleanPaint == paint);
182 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000183
184#ifdef SK_BUILD_FOR_ANDROID
185 // the reset function should increment the Generation ID
186 REPORTER_ASSERT(reporter, paint.getGenerationID() != paintGenID);
187 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
188 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &paint, sizeof(cleanPaint)));
189 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &copiedPaint, sizeof(cleanPaint)));
190#endif
191}
reed@android.coma0f5d152009-06-22 17:38:10 +0000192
193// found and fixed for webkit: mishandling when we hit recursion limit on
194// mostly degenerate cubic flatness test
195static void regression_cubic(skiatest::Reporter* reporter) {
196 SkPath path, stroke;
197 SkPaint paint;
198
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000199 path.moveTo(460.2881309415525f,
200 303.250847066498f);
201 path.cubicTo(463.36378422175284f,
202 302.1169735073363f,
203 456.32239330810046f,
204 304.720354932878f,
205 453.15255460013304f,
206 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000207
reed@android.coma0f5d152009-06-22 17:38:10 +0000208 SkRect fillR, strokeR;
209 fillR = path.getBounds();
210
211 paint.setStyle(SkPaint::kStroke_Style);
212 paint.setStrokeWidth(SkIntToScalar(2));
213 paint.getFillPath(path, &stroke);
214 strokeR = stroke.getBounds();
215
216 SkRect maxR = fillR;
217 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
218 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
219 SkScalarMul(paint.getStrokeWidth(), miter) :
220 paint.getStrokeWidth();
221 maxR.inset(-inset, -inset);
222
223 // test that our stroke didn't explode
224 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
225}
226
djsollen@google.com46348e22013-03-04 19:47:42 +0000227// found and fixed for android: not initializing rect for string's of length 0
228static void regression_measureText(skiatest::Reporter* reporter) {
229
230 SkPaint paint;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000231 paint.setTextSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000232
233 SkRect r;
234 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
235
236 // test that the rect was reset
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000237 paint.measureText("", 0, &r, 1.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000238 REPORTER_ASSERT(reporter, r.isEmpty());
239}
240
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000241DEF_TEST(Paint, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +0000242 // TODO add general paint tests
djsollen@google.comb44cd652011-12-01 17:09:21 +0000243 test_copy(reporter);
reed@android.coma0f5d152009-06-22 17:38:10 +0000244
245 // regression tests
246 regression_cubic(reporter);
djsollen@google.com46348e22013-03-04 19:47:42 +0000247 regression_measureText(reporter);
reed@google.com25b3bd52013-05-22 13:55:54 +0000248
reed@google.com9cfc83c2013-07-22 17:18:18 +0000249 test_filterlevel(reporter);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000250
reed@google.combcb42ae2013-07-02 13:56:39 +0000251 // need to implement charsToGlyphs on other backends (e.g. linux, win)
reed@google.comc3eb56d2013-07-02 14:01:23 +0000252 // before we can run this tests everywhere
253 if (false) {
254 test_cmap(reporter);
255 }
reed@android.coma0f5d152009-06-22 17:38:10 +0000256}
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000257
258#define ASSERT(expr) REPORTER_ASSERT(r, expr)
259
260DEF_TEST(Paint_FlatteningTraits, r) {
261 SkPaint paint;
262 paint.setColor(0x00AABBCC);
263 paint.setTextScaleX(1.0f); // Encoded despite being the default value.
264 paint.setTextSize(19);
265 paint.setXfermode(SkXfermode::Create(SkXfermode::kModulate_Mode))->unref();
266 paint.setLooper(NULL); // Ignored.
267
268 SkWriteBuffer writer;
269 SkPaint::FlatteningTraits::Flatten(writer, paint);
270 const size_t expectedBytesWritten = sizeof(void*) == 8 ? 48 : 40;
271 ASSERT(expectedBytesWritten == writer.bytesWritten());
272
273 const uint32_t* written = writer.getWriter32()->contiguousArray();
274 SkASSERT(written != NULL);
275 ASSERT(*written == ((1<<0) | (1<<2) | (1<<3) | (1<<9))); // Dirty bits for our 4.
276
277 SkReadBuffer reader(written, writer.bytesWritten());
278 SkPaint other;
279 SkPaint::FlatteningTraits::Unflatten(reader, &other);
280 ASSERT(reader.offset() == writer.bytesWritten());
281
282 // No matter the encoding, these must always hold.
283 ASSERT(other.getColor() == paint.getColor());
284 ASSERT(other.getTextScaleX() == paint.getTextScaleX());
285 ASSERT(other.getTextSize() == paint.getTextSize());
286 ASSERT(other.getLooper() == paint.getLooper());
287
288 // We have to be a little looser and compare just the modes. Pointers might not be the same.
289 SkXfermode::Mode otherMode, paintMode;
290 ASSERT(other.getXfermode()->asMode(&otherMode));
291 ASSERT(paint.getXfermode()->asMode(&paintMode));
292 ASSERT(otherMode == paintMode);
293}