blob: 0db34b6f09a5730e05fc6926f13a1ff4704c5183 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.coma0f5d152009-06-22 17:38:10 +00008#include "Test.h"
9#include "SkPath.h"
10#include "SkPaint.h"
djsollen@google.comb44cd652011-12-01 17:09:21 +000011#include "SkLayerDrawLooper.h"
12#include "SkBlurMaskFilter.h"
13
reed@google.com25b3bd52013-05-22 13:55:54 +000014// temparary api for bicubic, just be sure we can set/clear it
15static 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.comb44cd652011-12-01 17:09:21 +000028static 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.comefbe8e92013-02-07 18:58:35 +000046 uint32_t paintGenID = paint.getGenerationID();
47 uint32_t copiedPaintGenID = copiedPaint.getGenerationID();
djsollen@google.comb44cd652011-12-01 17:09:21 +000048 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.coma0f5d152009-06-22 17:38:10 +000079
80// found and fixed for webkit: mishandling when we hit recursion limit on
81// mostly degenerate cubic flatness test
82static void regression_cubic(skiatest::Reporter* reporter) {
83 SkPath path, stroke;
84 SkPaint paint;
85
robertphillips@google.com6853e802012-04-16 15:50:18 +000086 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.comd6176b02012-08-23 18:14:13 +000094
reed@android.coma0f5d152009-06-22 17:38:10 +000095 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.com46348e22013-03-04 19:47:42 +0000114// found and fixed for android: not initializing rect for string's of length 0
115static 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.coma0f5d152009-06-22 17:38:10 +0000128static void TestPaint(skiatest::Reporter* reporter) {
129 // TODO add general paint tests
djsollen@google.comb44cd652011-12-01 17:09:21 +0000130 test_copy(reporter);
reed@android.coma0f5d152009-06-22 17:38:10 +0000131
132 // regression tests
133 regression_cubic(reporter);
djsollen@google.com46348e22013-03-04 19:47:42 +0000134 regression_measureText(reporter);
reed@google.com25b3bd52013-05-22 13:55:54 +0000135
136 test_bicubic(reporter);
reed@android.coma0f5d152009-06-22 17:38:10 +0000137}
138
139#include "TestClassDef.h"
140DEFINE_TESTCLASS("Paint", TestPaintClass, TestPaint)