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 | |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 62 | DEF_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.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 67 | 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.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 81 | SkRandom rand; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 82 | SkPaint paint; |
| 83 | paint.setTypeface(SkTypeface::RefDefault())->unref(); |
| 84 | 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.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 93 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 94 | 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.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 98 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 99 | uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS]; |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 100 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 101 | 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.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t))); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 109 | if (contains) { |
| 110 | REPORTER_ASSERT(reporter, NGLYPHS == first); |
| 111 | } else { |
| 112 | REPORTER_ASSERT(reporter, NGLYPHS > first); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 117 | |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 118 | // temparary api for bicubic, just be sure we can set/clear it |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 119 | DEF_TEST(Paint_filterlevel, reporter) { |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 120 | SkPaint p0, p1; |
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 | REPORTER_ASSERT(reporter, |
| 123 | SkPaint::kNone_FilterLevel == p0.getFilterLevel()); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 124 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 125 | static const SkPaint::FilterLevel gLevels[] = { |
| 126 | SkPaint::kNone_FilterLevel, |
| 127 | SkPaint::kLow_FilterLevel, |
| 128 | SkPaint::kMedium_FilterLevel, |
| 129 | SkPaint::kHigh_FilterLevel |
| 130 | }; |
| 131 | for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { |
| 132 | p0.setFilterLevel(gLevels[i]); |
| 133 | REPORTER_ASSERT(reporter, gLevels[i] == p0.getFilterLevel()); |
| 134 | p1 = p0; |
| 135 | REPORTER_ASSERT(reporter, gLevels[i] == p1.getFilterLevel()); |
| 136 | |
| 137 | p0.reset(); |
| 138 | REPORTER_ASSERT(reporter, |
| 139 | SkPaint::kNone_FilterLevel == p0.getFilterLevel()); |
| 140 | } |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 141 | } |
| 142 | |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 143 | DEF_TEST(Paint_copy, reporter) { |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 144 | 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 |
| 150 | SkLayerDrawLooper* looper = new SkLayerDrawLooper(); |
| 151 | paint.setLooper(looper)->unref(); |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 152 | SkMaskFilter* mask = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 153 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 154 | paint.setMaskFilter(mask)->unref(); |
| 155 | |
| 156 | // copy the paint using the copy constructor and check they are the same |
| 157 | SkPaint copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 159 | |
| 160 | #ifdef SK_BUILD_FOR_ANDROID |
| 161 | // the copy constructor should preserve the Generation ID |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 162 | uint32_t paintGenID = paint.getGenerationID(); |
| 163 | uint32_t copiedPaintGenID = copiedPaint.getGenerationID(); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 164 | REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID); |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 166 | #endif |
| 167 | |
| 168 | // copy the paint using the equal operator and check they are the same |
| 169 | copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 170 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 171 | |
| 172 | #ifdef SK_BUILD_FOR_ANDROID |
| 173 | // the equals operator should increment the Generation ID |
| 174 | REPORTER_ASSERT(reporter, paint.getGenerationID() == paintGenID); |
| 175 | REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID); |
| 176 | copiedPaintGenID = copiedPaint.getGenerationID(); // reset to the new value |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 177 | REPORTER_ASSERT(reporter, paint == copiedPaint); // operator== ignores fGenerationID |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 178 | #endif |
| 179 | |
| 180 | // clean the paint and check they are back to their initial states |
| 181 | SkPaint cleanPaint; |
| 182 | paint.reset(); |
| 183 | copiedPaint.reset(); |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 184 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 185 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 186 | |
| 187 | #ifdef SK_BUILD_FOR_ANDROID |
| 188 | // the reset function should increment the Generation ID |
| 189 | REPORTER_ASSERT(reporter, paint.getGenerationID() != paintGenID); |
| 190 | REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID); |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 191 | // operator== ignores fGenerationID |
| 192 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 193 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 194 | #endif |
| 195 | } |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 196 | |
| 197 | // found and fixed for webkit: mishandling when we hit recursion limit on |
| 198 | // mostly degenerate cubic flatness test |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 199 | DEF_TEST(Paint_regression_cubic, reporter) { |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 200 | SkPath path, stroke; |
| 201 | SkPaint paint; |
| 202 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 203 | path.moveTo(460.2881309415525f, |
| 204 | 303.250847066498f); |
| 205 | path.cubicTo(463.36378422175284f, |
| 206 | 302.1169735073363f, |
| 207 | 456.32239330810046f, |
| 208 | 304.720354932878f, |
| 209 | 453.15255460013304f, |
| 210 | 305.788586869862f); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 211 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 212 | SkRect fillR, strokeR; |
| 213 | fillR = path.getBounds(); |
| 214 | |
| 215 | paint.setStyle(SkPaint::kStroke_Style); |
| 216 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 217 | paint.getFillPath(path, &stroke); |
| 218 | strokeR = stroke.getBounds(); |
| 219 | |
| 220 | SkRect maxR = fillR; |
| 221 | SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter()); |
| 222 | SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ? |
| 223 | SkScalarMul(paint.getStrokeWidth(), miter) : |
| 224 | paint.getStrokeWidth(); |
| 225 | maxR.inset(-inset, -inset); |
| 226 | |
| 227 | // test that our stroke didn't explode |
| 228 | REPORTER_ASSERT(reporter, maxR.contains(strokeR)); |
| 229 | } |
| 230 | |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 231 | // found and fixed for android: not initializing rect for string's of length 0 |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 232 | DEF_TEST(Paint_regression_measureText, reporter) { |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 233 | |
| 234 | SkPaint paint; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 235 | paint.setTextSize(12.0f); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 236 | |
| 237 | SkRect r; |
| 238 | r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN); |
| 239 | |
| 240 | // test that the rect was reset |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 241 | paint.measureText("", 0, &r, 1.0f); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 242 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 243 | } |
| 244 | |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 245 | #define ASSERT(expr) REPORTER_ASSERT(r, expr) |
| 246 | |
| 247 | DEF_TEST(Paint_FlatteningTraits, r) { |
| 248 | SkPaint paint; |
| 249 | paint.setColor(0x00AABBCC); |
| 250 | paint.setTextScaleX(1.0f); // Encoded despite being the default value. |
| 251 | paint.setTextSize(19); |
| 252 | paint.setXfermode(SkXfermode::Create(SkXfermode::kModulate_Mode))->unref(); |
| 253 | paint.setLooper(NULL); // Ignored. |
| 254 | |
| 255 | SkWriteBuffer writer; |
| 256 | SkPaint::FlatteningTraits::Flatten(writer, paint); |
| 257 | const size_t expectedBytesWritten = sizeof(void*) == 8 ? 48 : 40; |
| 258 | ASSERT(expectedBytesWritten == writer.bytesWritten()); |
| 259 | |
| 260 | const uint32_t* written = writer.getWriter32()->contiguousArray(); |
| 261 | SkASSERT(written != NULL); |
| 262 | ASSERT(*written == ((1<<0) | (1<<2) | (1<<3) | (1<<9))); // Dirty bits for our 4. |
| 263 | |
| 264 | SkReadBuffer reader(written, writer.bytesWritten()); |
| 265 | SkPaint other; |
| 266 | SkPaint::FlatteningTraits::Unflatten(reader, &other); |
| 267 | ASSERT(reader.offset() == writer.bytesWritten()); |
| 268 | |
| 269 | // No matter the encoding, these must always hold. |
| 270 | ASSERT(other.getColor() == paint.getColor()); |
| 271 | ASSERT(other.getTextScaleX() == paint.getTextScaleX()); |
| 272 | ASSERT(other.getTextSize() == paint.getTextSize()); |
| 273 | ASSERT(other.getLooper() == paint.getLooper()); |
| 274 | |
| 275 | // We have to be a little looser and compare just the modes. Pointers might not be the same. |
| 276 | SkXfermode::Mode otherMode, paintMode; |
| 277 | ASSERT(other.getXfermode()->asMode(&otherMode)); |
| 278 | ASSERT(paint.getXfermode()->asMode(&paintMode)); |
| 279 | ASSERT(otherMode == paintMode); |
| 280 | } |