epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 7 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 8 | #include "SkBlurMask.h" |
| 9 | #include "SkBlurMaskFilter.h" |
| 10 | #include "SkLayerDrawLooper.h" |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 11 | #include "SkPaint.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 13 | #include "SkRandom.h" |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame^] | 14 | #include "SkReadBuffer.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 15 | #include "SkTypeface.h" |
| 16 | #include "SkUtils.h" |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame^] | 17 | #include "SkWriteBuffer.h" |
| 18 | #include "SkXfermode.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 19 | #include "Test.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 20 | |
| 21 | static 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 | |
| 30 | static 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 | |
| 39 | static 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 | |
| 47 | static 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 | |
| 53 | static 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 | |
| 62 | static 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.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 77 | SkRandom rand; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 78 | 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.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 89 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 90 | 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.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 94 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 95 | uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS]; |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 96 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 97 | 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.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 114 | |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 115 | // temparary api for bicubic, just be sure we can set/clear it |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 116 | static void test_filterlevel(skiatest::Reporter* reporter) { |
| 117 | SkPaint p0, p1; |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 118 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 119 | REPORTER_ASSERT(reporter, |
| 120 | SkPaint::kNone_FilterLevel == p0.getFilterLevel()); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 121 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 122 | 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.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 138 | } |
| 139 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 140 | static 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.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 149 | SkMaskFilter* mask = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 150 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 151 | 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.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 155 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 156 | |
| 157 | #ifdef SK_BUILD_FOR_ANDROID |
| 158 | // the copy constructor should preserve the Generation ID |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 159 | uint32_t paintGenID = paint.getGenerationID(); |
| 160 | uint32_t copiedPaintGenID = copiedPaint.getGenerationID(); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 161 | REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID); |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, !memcmp(&paint, &copiedPaint, sizeof(paint))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 163 | #endif |
| 164 | |
| 165 | // copy the paint using the equal operator and check they are the same |
| 166 | copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 167 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 168 | |
| 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.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 181 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 182 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 183 | |
| 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.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 192 | |
| 193 | // found and fixed for webkit: mishandling when we hit recursion limit on |
| 194 | // mostly degenerate cubic flatness test |
| 195 | static void regression_cubic(skiatest::Reporter* reporter) { |
| 196 | SkPath path, stroke; |
| 197 | SkPaint paint; |
| 198 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 199 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 207 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 208 | 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.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 227 | // found and fixed for android: not initializing rect for string's of length 0 |
| 228 | static void regression_measureText(skiatest::Reporter* reporter) { |
| 229 | |
| 230 | SkPaint paint; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 231 | paint.setTextSize(12.0f); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 232 | |
| 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 237 | paint.measureText("", 0, &r, 1.0f); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 238 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 239 | } |
| 240 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 241 | DEF_TEST(Paint, reporter) { |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 242 | // TODO add general paint tests |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 243 | test_copy(reporter); |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 244 | |
| 245 | // regression tests |
| 246 | regression_cubic(reporter); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 247 | regression_measureText(reporter); |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 248 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 249 | test_filterlevel(reporter); |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 250 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 251 | // need to implement charsToGlyphs on other backends (e.g. linux, win) |
reed@google.com | c3eb56d | 2013-07-02 14:01:23 +0000 | [diff] [blame] | 252 | // before we can run this tests everywhere |
| 253 | if (false) { |
| 254 | test_cmap(reporter); |
| 255 | } |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 256 | } |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame^] | 257 | |
| 258 | #define ASSERT(expr) REPORTER_ASSERT(r, expr) |
| 259 | |
| 260 | DEF_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 | } |