| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkPaint.h" |
| djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 11 | #include "SkLayerDrawLooper.h" |
| 12 | #include "SkBlurMaskFilter.h" |
| 13 | |
| reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 14 | // temparary api for bicubic, just be sure we can set/clear it |
| 15 | static void test_bicubic(skiatest::Reporter* reporter) { |
| 16 | SkPaint p0; |
| 17 | REPORTER_ASSERT(reporter, 0 == (p0.getFlags() & SkPaint::kBicubicFilterBitmap_Flag)); |
| 18 | p0.setFlags(p0.getFlags() | SkPaint::kBicubicFilterBitmap_Flag); |
| 19 | REPORTER_ASSERT(reporter, 0 != (p0.getFlags() & SkPaint::kBicubicFilterBitmap_Flag)); |
| 20 | SkPaint p1(p0); |
| 21 | REPORTER_ASSERT(reporter, 0 != (p1.getFlags() & SkPaint::kBicubicFilterBitmap_Flag)); |
| 22 | p0.reset(); |
| 23 | REPORTER_ASSERT(reporter, 0 == (p0.getFlags() & SkPaint::kBicubicFilterBitmap_Flag)); |
| 24 | p0 = p1; |
| 25 | p0.setFlags(p0.getFlags() | SkPaint::kBicubicFilterBitmap_Flag); |
| 26 | } |
| 27 | |
| djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 28 | static void test_copy(skiatest::Reporter* reporter) { |
| 29 | SkPaint paint; |
| 30 | // set a few member variables |
| 31 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 32 | paint.setTextAlign(SkPaint::kLeft_Align); |
| 33 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 34 | // set a few pointers |
| 35 | SkLayerDrawLooper* looper = new SkLayerDrawLooper(); |
| 36 | paint.setLooper(looper)->unref(); |
| 37 | SkMaskFilter* mask = SkBlurMaskFilter::Create(1, SkBlurMaskFilter::kNormal_BlurStyle); |
| 38 | paint.setMaskFilter(mask)->unref(); |
| 39 | |
| 40 | // copy the paint using the copy constructor and check they are the same |
| 41 | SkPaint copiedPaint = paint; |
| 42 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
| 43 | |
| 44 | #ifdef SK_BUILD_FOR_ANDROID |
| 45 | // the copy constructor should preserve the Generation ID |
| djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 46 | uint32_t paintGenID = paint.getGenerationID(); |
| 47 | uint32_t copiedPaintGenID = copiedPaint.getGenerationID(); |
| djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID); |
| 49 | REPORTER_ASSERT(reporter, !memcmp(&paint, &copiedPaint, sizeof(paint))); |
| 50 | #endif |
| 51 | |
| 52 | // copy the paint using the equal operator and check they are the same |
| 53 | copiedPaint = paint; |
| 54 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
| 55 | |
| 56 | #ifdef SK_BUILD_FOR_ANDROID |
| 57 | // the equals operator should increment the Generation ID |
| 58 | REPORTER_ASSERT(reporter, paint.getGenerationID() == paintGenID); |
| 59 | REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID); |
| 60 | copiedPaintGenID = copiedPaint.getGenerationID(); // reset to the new value |
| 61 | REPORTER_ASSERT(reporter, memcmp(&paint, &copiedPaint, sizeof(paint))); |
| 62 | #endif |
| 63 | |
| 64 | // clean the paint and check they are back to their initial states |
| 65 | SkPaint cleanPaint; |
| 66 | paint.reset(); |
| 67 | copiedPaint.reset(); |
| 68 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 69 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
| 70 | |
| 71 | #ifdef SK_BUILD_FOR_ANDROID |
| 72 | // the reset function should increment the Generation ID |
| 73 | REPORTER_ASSERT(reporter, paint.getGenerationID() != paintGenID); |
| 74 | REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID); |
| 75 | REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &paint, sizeof(cleanPaint))); |
| 76 | REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &copiedPaint, sizeof(cleanPaint))); |
| 77 | #endif |
| 78 | } |
| reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 79 | |
| 80 | // found and fixed for webkit: mishandling when we hit recursion limit on |
| 81 | // mostly degenerate cubic flatness test |
| 82 | static void regression_cubic(skiatest::Reporter* reporter) { |
| 83 | SkPath path, stroke; |
| 84 | SkPaint paint; |
| 85 | |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 86 | path.moveTo(SkFloatToScalar(460.2881309415525f), |
| 87 | SkFloatToScalar(303.250847066498f)); |
| 88 | path.cubicTo(SkFloatToScalar(463.36378422175284f), |
| 89 | SkFloatToScalar(302.1169735073363f), |
| 90 | SkFloatToScalar(456.32239330810046f), |
| 91 | SkFloatToScalar(304.720354932878f), |
| 92 | SkFloatToScalar(453.15255460013304f), |
| 93 | SkFloatToScalar(305.788586869862f)); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 94 | |
| reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 95 | SkRect fillR, strokeR; |
| 96 | fillR = path.getBounds(); |
| 97 | |
| 98 | paint.setStyle(SkPaint::kStroke_Style); |
| 99 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 100 | paint.getFillPath(path, &stroke); |
| 101 | strokeR = stroke.getBounds(); |
| 102 | |
| 103 | SkRect maxR = fillR; |
| 104 | SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter()); |
| 105 | SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ? |
| 106 | SkScalarMul(paint.getStrokeWidth(), miter) : |
| 107 | paint.getStrokeWidth(); |
| 108 | maxR.inset(-inset, -inset); |
| 109 | |
| 110 | // test that our stroke didn't explode |
| 111 | REPORTER_ASSERT(reporter, maxR.contains(strokeR)); |
| 112 | } |
| 113 | |
| djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 114 | // found and fixed for android: not initializing rect for string's of length 0 |
| 115 | static void regression_measureText(skiatest::Reporter* reporter) { |
| 116 | |
| 117 | SkPaint paint; |
| 118 | paint.setTextSize(SkFloatToScalar(12.0f)); |
| 119 | |
| 120 | SkRect r; |
| 121 | r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN); |
| 122 | |
| 123 | // test that the rect was reset |
| 124 | paint.measureText("", 0, &r, SkFloatToScalar(1.0f)); |
| 125 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 126 | } |
| 127 | |
| reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 128 | static void TestPaint(skiatest::Reporter* reporter) { |
| 129 | // TODO add general paint tests |
| djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 130 | test_copy(reporter); |
| reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 131 | |
| 132 | // regression tests |
| 133 | regression_cubic(reporter); |
| djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 134 | regression_measureText(reporter); |
| reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 135 | |
| 136 | test_bicubic(reporter); |
| reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | #include "TestClassDef.h" |
| 140 | DEFINE_TESTCLASS("Paint", TestPaintClass, TestPaint) |