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 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 8 | #include "SkAutoMalloc.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 10 | #include "SkLayerDrawLooper.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 11 | #include "SkMaskFilter.h" |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 12 | #include "SkPaintPriv.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 15 | #include "SkReadBuffer.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 16 | #include "SkTo.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 17 | #include "SkTypeface.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 18 | #include "SkUTF.h" |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 19 | #include "SkWriteBuffer.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 20 | #include "Test.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 21 | #undef ASSERT |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 22 | |
| 23 | static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) { |
| 24 | char* u8 = (char*)dst; |
| 25 | for (int i = 0; i < count; ++i) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 26 | int n = SkToInt(SkUTF::ToUTF8(src[i], u8)); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 27 | u8 += n; |
| 28 | } |
| 29 | return u8 - (char*)dst; |
| 30 | } |
| 31 | |
| 32 | static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) { |
| 33 | uint16_t* u16 = (uint16_t*)dst; |
| 34 | for (int i = 0; i < count; ++i) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 35 | int n = SkToInt(SkUTF::ToUTF16(src[i], u16)); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 36 | u16 += n; |
| 37 | } |
| 38 | return (char*)u16 - (char*)dst; |
| 39 | } |
| 40 | |
| 41 | static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) { |
| 42 | SkUnichar* u32 = (SkUnichar*)dst; |
| 43 | if (src != u32) { |
| 44 | memcpy(u32, src, count * sizeof(SkUnichar)); |
| 45 | } |
| 46 | return count * sizeof(SkUnichar); |
| 47 | } |
| 48 | |
| 49 | static SkTypeface::Encoding paint2encoding(const SkPaint& paint) { |
| 50 | SkPaint::TextEncoding enc = paint.getTextEncoding(); |
| 51 | SkASSERT(SkPaint::kGlyphID_TextEncoding != enc); |
| 52 | return (SkTypeface::Encoding)enc; |
| 53 | } |
| 54 | |
| 55 | static int find_first_zero(const uint16_t glyphs[], int count) { |
| 56 | for (int i = 0; i < count; ++i) { |
| 57 | if (0 == glyphs[i]) { |
| 58 | return i; |
| 59 | } |
| 60 | } |
| 61 | return count; |
| 62 | } |
| 63 | |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 64 | DEF_TEST(Paint_cmap, reporter) { |
| 65 | // need to implement charsToGlyphs on other backends (e.g. linux, win) |
| 66 | // before we can run this tests everywhere |
| 67 | return; |
| 68 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 69 | static const int NGLYPHS = 64; |
| 70 | |
| 71 | SkUnichar src[NGLYPHS]; |
| 72 | SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage |
| 73 | |
| 74 | static const struct { |
| 75 | size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count); |
| 76 | SkPaint::TextEncoding fEncoding; |
| 77 | } gRec[] = { |
| 78 | { uni_to_utf8, SkPaint::kUTF8_TextEncoding }, |
| 79 | { uni_to_utf16, SkPaint::kUTF16_TextEncoding }, |
| 80 | { uni_to_utf32, SkPaint::kUTF32_TextEncoding }, |
| 81 | }; |
| 82 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 83 | SkRandom rand; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 84 | SkPaint paint; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 85 | paint.setTypeface(SkTypeface::MakeDefault()); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 86 | SkTypeface* face = paint.getTypeface(); |
| 87 | |
| 88 | for (int i = 0; i < 1000; ++i) { |
| 89 | // generate some random text |
| 90 | for (int j = 0; j < NGLYPHS; ++j) { |
| 91 | src[j] = ' ' + j; |
| 92 | } |
| 93 | // inject some random chars, to sometimes abort early |
| 94 | src[rand.nextU() & 63] = rand.nextU() & 0xFFF; |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 95 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 96 | for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) { |
| 97 | paint.setTextEncoding(gRec[k].fEncoding); |
| 98 | |
| 99 | size_t len = gRec[k].fSeedTextProc(src, dst, 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 | uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS]; |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 102 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 103 | bool contains = paint.containsText(dst, len); |
| 104 | int nglyphs = paint.textToGlyphs(dst, len, glyphs0); |
| 105 | int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS); |
| 106 | int index = find_first_zero(glyphs1, NGLYPHS); |
| 107 | |
| 108 | REPORTER_ASSERT(reporter, NGLYPHS == nglyphs); |
| 109 | REPORTER_ASSERT(reporter, index == first); |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t))); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 111 | if (contains) { |
| 112 | REPORTER_ASSERT(reporter, NGLYPHS == first); |
| 113 | } else { |
| 114 | REPORTER_ASSERT(reporter, NGLYPHS > first); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 119 | |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 120 | // temparary api for bicubic, just be sure we can set/clear it |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 121 | DEF_TEST(Paint_filterQuality, reporter) { |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 122 | SkPaint p0, p1; |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 123 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality()); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 125 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 126 | static const SkFilterQuality gQualitys[] = { |
| 127 | kNone_SkFilterQuality, |
| 128 | kLow_SkFilterQuality, |
| 129 | kMedium_SkFilterQuality, |
| 130 | kHigh_SkFilterQuality |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 131 | }; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 132 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 133 | p0.setFilterQuality(gQualitys[i]); |
| 134 | REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality()); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 135 | p1 = p0; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 136 | REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality()); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 137 | |
| 138 | p0.reset(); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality()); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 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); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 147 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 148 | // set a few pointers |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 149 | SkLayerDrawLooper::Builder looperBuilder; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 150 | paint.setLooper(looperBuilder.detach()); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 151 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 152 | SkBlurMask::ConvertRadiusToSigma(1))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 153 | |
| 154 | // copy the paint using the copy constructor and check they are the same |
| 155 | SkPaint copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 157 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 158 | // copy the paint using the equal operator and check they are the same |
| 159 | copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 160 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 161 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 162 | // clean the paint and check they are back to their initial states |
| 163 | SkPaint cleanPaint; |
| 164 | paint.reset(); |
| 165 | copiedPaint.reset(); |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 166 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 167 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 168 | } |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 169 | |
| 170 | // found and fixed for webkit: mishandling when we hit recursion limit on |
| 171 | // mostly degenerate cubic flatness test |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 172 | DEF_TEST(Paint_regression_cubic, reporter) { |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 173 | SkPath path, stroke; |
| 174 | SkPaint paint; |
| 175 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 176 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 184 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 185 | 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 Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 196 | paint.getStrokeWidth() * miter : |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 197 | 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.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 204 | DEF_TEST(Paint_flattening, reporter) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 205 | const SkFilterQuality levels[] = { |
| 206 | kNone_SkFilterQuality, |
| 207 | kLow_SkFilterQuality, |
| 208 | kMedium_SkFilterQuality, |
| 209 | kHigh_SkFilterQuality, |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 210 | }; |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame^] | 211 | const SkFontHinting hinting[] = { |
| 212 | kNo_SkFontHinting, |
| 213 | kSlight_SkFontHinting, |
| 214 | kNormal_SkFontHinting, |
| 215 | kFull_SkFontHinting, |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 216 | }; |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 217 | const SkPaint::Cap caps[] = { |
| 218 | SkPaint::kButt_Cap, |
| 219 | SkPaint::kRound_Cap, |
| 220 | SkPaint::kSquare_Cap, |
| 221 | }; |
| 222 | const SkPaint::Join joins[] = { |
| 223 | SkPaint::kMiter_Join, |
| 224 | SkPaint::kRound_Join, |
| 225 | SkPaint::kBevel_Join, |
| 226 | }; |
| 227 | const SkPaint::TextEncoding encodings[] = { |
| 228 | SkPaint::kUTF8_TextEncoding, |
| 229 | SkPaint::kUTF16_TextEncoding, |
| 230 | SkPaint::kUTF32_TextEncoding, |
| 231 | SkPaint::kGlyphID_TextEncoding, |
| 232 | }; |
| 233 | const SkPaint::Style styles[] = { |
| 234 | SkPaint::kFill_Style, |
| 235 | SkPaint::kStroke_Style, |
| 236 | SkPaint::kStrokeAndFill_Style, |
| 237 | }; |
| 238 | |
| 239 | #define FOR_SETUP(index, array, setter) \ |
| 240 | for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \ |
| 241 | paint.setter(array[index]); \ |
| 242 | |
| 243 | SkPaint paint; |
| 244 | paint.setFlags(0x1234); |
| 245 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 246 | FOR_SETUP(i, levels, setFilterQuality) |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 247 | FOR_SETUP(j, hinting, setHinting) |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 248 | FOR_SETUP(l, caps, setStrokeCap) |
| 249 | FOR_SETUP(m, joins, setStrokeJoin) |
| 250 | FOR_SETUP(n, encodings, setTextEncoding) |
| 251 | FOR_SETUP(p, styles, setStyle) |
| 252 | |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 253 | SkBinaryWriteBuffer writer; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 254 | SkPaintPriv::Flatten(paint, writer); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 255 | |
mtklein | b4c899d | 2016-04-29 13:58:09 -0700 | [diff] [blame] | 256 | SkAutoMalloc buf(writer.bytesWritten()); |
| 257 | writer.writeToMemory(buf.get()); |
| 258 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
skia.committer@gmail.com | 667b98d | 2014-04-17 03:05:10 +0000 | [diff] [blame] | 259 | |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 260 | SkPaint paint2; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 261 | SkPaintPriv::Unflatten(&paint2, reader); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 262 | REPORTER_ASSERT(reporter, paint2 == paint); |
| 263 | |
Mike Reed | 6e24cd3 | 2018-10-27 14:39:00 +0000 | [diff] [blame] | 264 | }}}}}} |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 265 | #undef FOR_SETUP |
| 266 | |
| 267 | } |
| 268 | |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 269 | // 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] | 270 | DEF_TEST(Paint_regression_measureText, reporter) { |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 271 | |
| 272 | SkPaint paint; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 273 | paint.setTextSize(12.0f); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 274 | |
| 275 | SkRect r; |
| 276 | r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN); |
| 277 | |
| 278 | // test that the rect was reset |
reed | 99ae881 | 2014-08-26 11:30:01 -0700 | [diff] [blame] | 279 | paint.measureText("", 0, &r); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 280 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 281 | } |
| 282 | |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 283 | #define ASSERT(expr) REPORTER_ASSERT(r, expr) |
| 284 | |
mtklein | 610a015 | 2014-09-25 11:57:53 -0700 | [diff] [blame] | 285 | DEF_TEST(Paint_MoreFlattening, r) { |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 286 | SkPaint paint; |
| 287 | paint.setColor(0x00AABBCC); |
mtklein | ee902cd | 2014-09-22 11:40:33 -0700 | [diff] [blame] | 288 | paint.setTextScaleX(1.0f); // Default value, ignored. |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 289 | paint.setTextSize(19); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 290 | paint.setBlendMode(SkBlendMode::kModulate); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 291 | paint.setLooper(nullptr); // Default value, ignored. |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 292 | |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 293 | SkBinaryWriteBuffer writer; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 294 | SkPaintPriv::Flatten(paint, writer); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 295 | |
mtklein | b4c899d | 2016-04-29 13:58:09 -0700 | [diff] [blame] | 296 | SkAutoMalloc buf(writer.bytesWritten()); |
| 297 | writer.writeToMemory(buf.get()); |
| 298 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
| 299 | |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 300 | SkPaint other; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 301 | SkPaintPriv::Unflatten(&other, reader); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 302 | ASSERT(reader.offset() == writer.bytesWritten()); |
| 303 | |
| 304 | // No matter the encoding, these must always hold. |
| 305 | ASSERT(other.getColor() == paint.getColor()); |
| 306 | ASSERT(other.getTextScaleX() == paint.getTextScaleX()); |
| 307 | ASSERT(other.getTextSize() == paint.getTextSize()); |
| 308 | ASSERT(other.getLooper() == paint.getLooper()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 309 | ASSERT(other.getBlendMode() == paint.getBlendMode()); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 310 | } |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 311 | |
| 312 | DEF_TEST(Paint_getHash, r) { |
| 313 | // Try not to inspect the actual hash values in here. |
| 314 | // We might want to change the hash function. |
| 315 | |
| 316 | SkPaint paint; |
| 317 | const uint32_t defaultHash = paint.getHash(); |
| 318 | |
| 319 | // Check that some arbitrary field affects the hash. |
| 320 | paint.setColor(0xFF00FF00); |
| 321 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
| 322 | paint.setColor(SK_ColorBLACK); // Reset to default value. |
| 323 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
| 324 | |
| 325 | // SkTypeface is the first field we hash, so test it specially. |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 326 | paint.setTypeface(SkTypeface::MakeDefault()); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 327 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 328 | paint.setTypeface(nullptr); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 329 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
| 330 | |
| 331 | // This is part of fBitfields, the last field we hash. |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame^] | 332 | paint.setHinting(kSlight_SkFontHinting); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 333 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame^] | 334 | paint.setHinting(kNormal_SkFontHinting); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 335 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
| 336 | } |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 337 | |
| 338 | #include "SkColorMatrixFilter.h" |
| 339 | |
| 340 | DEF_TEST(Paint_nothingToDraw, r) { |
| 341 | SkPaint paint; |
| 342 | |
| 343 | REPORTER_ASSERT(r, !paint.nothingToDraw()); |
| 344 | paint.setAlpha(0); |
| 345 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 346 | |
| 347 | paint.setAlpha(0xFF); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 348 | paint.setBlendMode(SkBlendMode::kDst); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 349 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 350 | |
| 351 | paint.setAlpha(0); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 352 | paint.setBlendMode(SkBlendMode::kSrcOver); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 353 | |
| 354 | SkColorMatrix cm; |
| 355 | cm.setIdentity(); // does not change alpha |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 356 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat)); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 357 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 358 | |
| 359 | cm.postTranslate(0, 0, 0, 1); // wacks alpha |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 360 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat)); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 361 | REPORTER_ASSERT(r, !paint.nothingToDraw()); |
| 362 | } |