blob: 1a42a49efd99d2d7cc5707da7a08389e98dd0190 [file] [log] [blame]
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001/*
2 * Copyright 2013 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 */
7
halcanary96fcdcc2015-08-27 07:41:13 -07008#include "SkTypes.h"
9
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000010#if SK_SUPPORT_GPU
11
12#include "GrContext.h"
fmalita3b444482015-11-19 07:28:50 -080013#include "GrPath.h"
bsalomon7bffcd22016-09-15 13:55:33 -070014#include "GrShape.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000015#include "SkBitmap.h"
16#include "SkCanvas.h"
17#include "SkColor.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000018#include "SkPaint.h"
fmalitafbe1c112015-11-18 20:12:56 -080019#include "SkPath.h"
20#include "SkDashPathEffect.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000021#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000022#include "SkRect.h"
reed69f6f002014-09-18 06:09:44 -070023#include "SkSurface.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000024#include "Test.h"
25
kkinnunen15302832015-12-01 04:35:26 -080026#include <initializer_list>
27
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000028static void test_drawPathEmpty(skiatest::Reporter*, SkCanvas* canvas) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000029 // Filling an empty path should not crash.
30 SkPaint paint;
fmalitafbe1c112015-11-18 20:12:56 -080031 SkRect emptyRect = SkRect::MakeEmpty();
32 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000033 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080034 canvas->drawOval(emptyRect, paint);
35 canvas->drawRect(emptyRect, paint);
36 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000037
38 // Stroking an empty path should not crash.
39 paint.setAntiAlias(true);
40 paint.setStyle(SkPaint::kStroke_Style);
41 paint.setColor(SK_ColorGRAY);
42 paint.setStrokeWidth(SkIntToScalar(20));
43 paint.setStrokeJoin(SkPaint::kRound_Join);
fmalitafbe1c112015-11-18 20:12:56 -080044 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000045 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080046 canvas->drawOval(emptyRect, paint);
47 canvas->drawRect(emptyRect, paint);
48 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000049}
50
fmalitafbe1c112015-11-18 20:12:56 -080051static void fill_and_stroke(SkCanvas* canvas, const SkPath& p1, const SkPath& p2,
reeda4393342016-03-18 11:22:57 -070052 sk_sp<SkPathEffect> effect) {
fmalitafbe1c112015-11-18 20:12:56 -080053 SkPaint paint;
54 paint.setAntiAlias(true);
55 paint.setPathEffect(effect);
56
57 canvas->drawPath(p1, paint);
58 canvas->drawPath(p2, paint);
59
60 paint.setStyle(SkPaint::kStroke_Style);
61 canvas->drawPath(p1, paint);
62 canvas->drawPath(p2, paint);
63}
64
65static void test_drawSameRectOvals(skiatest::Reporter*, SkCanvas* canvas) {
66 // Drawing ovals with similar bounds but different points order should not crash.
67
68 SkPath oval1, oval2;
69 const SkRect rect = SkRect::MakeWH(100, 50);
70 oval1.addOval(rect, SkPath::kCW_Direction);
71 oval2.addOval(rect, SkPath::kCCW_Direction);
72
73 fill_and_stroke(canvas, oval1, oval2, nullptr);
74
75 const SkScalar intervals[] = { 1, 1 };
reeda4393342016-03-18 11:22:57 -070076 fill_and_stroke(canvas, oval1, oval2, SkDashPathEffect::Make(intervals, 2, 0));
fmalitafbe1c112015-11-18 20:12:56 -080077}
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000078
bsalomon758586c2016-04-06 14:02:39 -070079DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GpuDrawPath, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -080080 for (auto& test_func : { &test_drawPathEmpty, &test_drawSameRectOvals }) {
81 for (auto& sampleCount : {0, 4, 16}) {
reed69f6f002014-09-18 06:09:44 -070082 SkImageInfo info = SkImageInfo::MakeN32Premul(255, 255);
reede8f30622016-03-23 18:59:25 -070083 auto surface(
bsalomon8b7451a2016-05-11 06:33:06 -070084 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info,
85 sampleCount, nullptr));
kkinnunenc11a5272015-11-19 09:37:02 -080086 if (!surface) {
87 continue;
88 }
kkinnunen15302832015-12-01 04:35:26 -080089 test_func(reporter, surface->getCanvas());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000090 }
91 }
92}
93
kkinnunen15302832015-12-01 04:35:26 -080094DEF_GPUTEST(GrPathKeys, reporter, /*factory*/) {
bsalomon7bffcd22016-09-15 13:55:33 -070095 SkPaint strokePaint;
96 strokePaint.setStyle(SkPaint::kStroke_Style);
97 strokePaint.setStrokeWidth(10.f);
98 GrStyle styles[] = {
99 GrStyle::SimpleFill(),
100 GrStyle::SimpleHairline(),
101 GrStyle(strokePaint)
102 };
fmalita3b444482015-11-19 07:28:50 -0800103
bsalomon7bffcd22016-09-15 13:55:33 -0700104 for (const GrStyle& style : styles) {
105 // Keys should not ignore conic weights.
106 SkPath path1, path2;
107 path1.setIsVolatile(true);
108 path2.setIsVolatile(true);
109 SkPoint p0 = SkPoint::Make(100, 0);
110 SkPoint p1 = SkPoint::Make(100, 100);
fmalita3b444482015-11-19 07:28:50 -0800111
bsalomon7bffcd22016-09-15 13:55:33 -0700112 path1.conicTo(p0, p1, .5f);
113 path2.conicTo(p0, p1, .7f);
114
115 bool isVolatile;
116 GrUniqueKey key1, key2;
117 // Even though the paths are marked volatile, they should have keys based on the path data
118 // because they have a small amount of data.
119 GrPath::ComputeKey(GrShape(path1, GrStyle::SimpleFill()), &key1, &isVolatile);
120 REPORTER_ASSERT(reporter, !isVolatile);
121 REPORTER_ASSERT(reporter, key1.isValid());
122 GrPath::ComputeKey(GrShape(path2, GrStyle::SimpleFill()), &key2, &isVolatile);
123 REPORTER_ASSERT(reporter, !isVolatile);
124 REPORTER_ASSERT(reporter, key1.isValid());
125 REPORTER_ASSERT(reporter, key1 != key2);
126
127 // Ensure that recreating the GrShape doesn't change the key.
128 {
129 GrUniqueKey tempKey;
130 GrPath::ComputeKey(GrShape(path1, GrStyle::SimpleFill()), &tempKey, &isVolatile);
131 REPORTER_ASSERT(reporter, key1 == tempKey);
132 }
133
134 // Try a large path that is too big to be keyed off its data.
135 SkPath path3;
136 SkPath path4;
137 for (int i = 0; i < 1000; ++i) {
138 SkScalar s = SkIntToScalar(i);
139 path3.conicTo(s, 3.f * s / 4, s + 1.f, s, 0.5f + s / 2000.f);
140 path4.conicTo(s, 3.f * s / 4, s + 1.f, s, 0.3f + s / 2000.f);
141 }
142
143 GrUniqueKey key3, key4;
144 // These aren't marked volatile and so should have keys
145 GrPath::ComputeKey(GrShape(path3, style), &key3, &isVolatile);
146 REPORTER_ASSERT(reporter, !isVolatile);
147 REPORTER_ASSERT(reporter, key3.isValid());
148 GrPath::ComputeKey(GrShape(path4, style), &key4, &isVolatile);
149 REPORTER_ASSERT(reporter, !isVolatile);
150 REPORTER_ASSERT(reporter, key4.isValid());
151 REPORTER_ASSERT(reporter, key3 != key4);
152
153 {
154 GrUniqueKey tempKey;
155 path3.setIsVolatile(true);
156 GrPath::ComputeKey(GrShape(path3, style), &key1, &isVolatile);
157 REPORTER_ASSERT(reporter, isVolatile);
158 REPORTER_ASSERT(reporter, !tempKey.isValid());
159 }
160 }
fmalita3b444482015-11-19 07:28:50 -0800161}
162
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000163#endif