blob: 39e332ff687a4cc80b3e033f52090e058c4943f6 [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#include "GrContext.h"
fmalita3b444482015-11-19 07:28:50 -080011#include "GrPath.h"
bsalomon7bffcd22016-09-15 13:55:33 -070012#include "GrShape.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000013#include "SkBitmap.h"
14#include "SkCanvas.h"
15#include "SkColor.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000016#include "SkPaint.h"
fmalitafbe1c112015-11-18 20:12:56 -080017#include "SkPath.h"
18#include "SkDashPathEffect.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000019#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "SkRect.h"
reed69f6f002014-09-18 06:09:44 -070021#include "SkSurface.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000022#include "Test.h"
23
kkinnunen15302832015-12-01 04:35:26 -080024#include <initializer_list>
25
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000026static void test_drawPathEmpty(skiatest::Reporter*, SkCanvas* canvas) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000027 // Filling an empty path should not crash.
28 SkPaint paint;
fmalitafbe1c112015-11-18 20:12:56 -080029 SkRect emptyRect = SkRect::MakeEmpty();
30 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000031 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080032 canvas->drawOval(emptyRect, paint);
33 canvas->drawRect(emptyRect, paint);
34 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000035
36 // Stroking an empty path should not crash.
37 paint.setAntiAlias(true);
38 paint.setStyle(SkPaint::kStroke_Style);
39 paint.setColor(SK_ColorGRAY);
40 paint.setStrokeWidth(SkIntToScalar(20));
41 paint.setStrokeJoin(SkPaint::kRound_Join);
fmalitafbe1c112015-11-18 20:12:56 -080042 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000043 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080044 canvas->drawOval(emptyRect, paint);
45 canvas->drawRect(emptyRect, paint);
46 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000047}
48
fmalitafbe1c112015-11-18 20:12:56 -080049static void fill_and_stroke(SkCanvas* canvas, const SkPath& p1, const SkPath& p2,
reeda4393342016-03-18 11:22:57 -070050 sk_sp<SkPathEffect> effect) {
fmalitafbe1c112015-11-18 20:12:56 -080051 SkPaint paint;
52 paint.setAntiAlias(true);
53 paint.setPathEffect(effect);
54
55 canvas->drawPath(p1, paint);
56 canvas->drawPath(p2, paint);
57
58 paint.setStyle(SkPaint::kStroke_Style);
59 canvas->drawPath(p1, paint);
60 canvas->drawPath(p2, paint);
61}
62
63static void test_drawSameRectOvals(skiatest::Reporter*, SkCanvas* canvas) {
64 // Drawing ovals with similar bounds but different points order should not crash.
65
66 SkPath oval1, oval2;
67 const SkRect rect = SkRect::MakeWH(100, 50);
68 oval1.addOval(rect, SkPath::kCW_Direction);
69 oval2.addOval(rect, SkPath::kCCW_Direction);
70
71 fill_and_stroke(canvas, oval1, oval2, nullptr);
72
73 const SkScalar intervals[] = { 1, 1 };
reeda4393342016-03-18 11:22:57 -070074 fill_and_stroke(canvas, oval1, oval2, SkDashPathEffect::Make(intervals, 2, 0));
fmalitafbe1c112015-11-18 20:12:56 -080075}
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000076
bsalomon758586c2016-04-06 14:02:39 -070077DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GpuDrawPath, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -080078 for (auto& test_func : { &test_drawPathEmpty, &test_drawSameRectOvals }) {
Brian Salomonbdecacf2018-02-02 20:32:49 -050079 for (auto& sampleCount : {1, 4, 16}) {
reed69f6f002014-09-18 06:09:44 -070080 SkImageInfo info = SkImageInfo::MakeN32Premul(255, 255);
reede8f30622016-03-23 18:59:25 -070081 auto surface(
bsalomon8b7451a2016-05-11 06:33:06 -070082 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info,
83 sampleCount, nullptr));
kkinnunenc11a5272015-11-19 09:37:02 -080084 if (!surface) {
85 continue;
86 }
kkinnunen15302832015-12-01 04:35:26 -080087 test_func(reporter, surface->getCanvas());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000088 }
89 }
90}
91
Brian Salomondcfca432017-11-15 15:48:03 -050092DEF_GPUTEST(GrPathKeys, reporter, /* options */) {
bsalomon7bffcd22016-09-15 13:55:33 -070093 SkPaint strokePaint;
94 strokePaint.setStyle(SkPaint::kStroke_Style);
95 strokePaint.setStrokeWidth(10.f);
96 GrStyle styles[] = {
97 GrStyle::SimpleFill(),
98 GrStyle::SimpleHairline(),
99 GrStyle(strokePaint)
100 };
fmalita3b444482015-11-19 07:28:50 -0800101
bsalomon7bffcd22016-09-15 13:55:33 -0700102 for (const GrStyle& style : styles) {
103 // Keys should not ignore conic weights.
104 SkPath path1, path2;
bsalomon7bffcd22016-09-15 13:55:33 -0700105 SkPoint p0 = SkPoint::Make(100, 0);
106 SkPoint p1 = SkPoint::Make(100, 100);
fmalita3b444482015-11-19 07:28:50 -0800107
bsalomon7bffcd22016-09-15 13:55:33 -0700108 path1.conicTo(p0, p1, .5f);
109 path2.conicTo(p0, p1, .7f);
110
bsalomon7bffcd22016-09-15 13:55:33 -0700111 GrUniqueKey key1, key2;
bsalomonaa840642016-09-23 12:09:16 -0700112 // We expect these small paths to be keyed based on their data.
113 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -0700114 GrPath::ComputeKey(GrShape(path1, GrStyle::SimpleFill()), &key1, &isVolatile);
115 REPORTER_ASSERT(reporter, !isVolatile);
116 REPORTER_ASSERT(reporter, key1.isValid());
117 GrPath::ComputeKey(GrShape(path2, GrStyle::SimpleFill()), &key2, &isVolatile);
118 REPORTER_ASSERT(reporter, !isVolatile);
119 REPORTER_ASSERT(reporter, key1.isValid());
120 REPORTER_ASSERT(reporter, key1 != key2);
bsalomonaa840642016-09-23 12:09:16 -0700121 {
122 GrUniqueKey tempKey;
123 path1.setIsVolatile(true);
124 GrPath::ComputeKey(GrShape(path1, style), &key1, &isVolatile);
125 REPORTER_ASSERT(reporter, isVolatile);
126 REPORTER_ASSERT(reporter, !tempKey.isValid());
127 }
bsalomon7bffcd22016-09-15 13:55:33 -0700128
129 // Ensure that recreating the GrShape doesn't change the key.
130 {
131 GrUniqueKey tempKey;
bsalomonaa840642016-09-23 12:09:16 -0700132 GrPath::ComputeKey(GrShape(path2, GrStyle::SimpleFill()), &tempKey, &isVolatile);
133 REPORTER_ASSERT(reporter, key2 == tempKey);
bsalomon7bffcd22016-09-15 13:55:33 -0700134 }
135
136 // Try a large path that is too big to be keyed off its data.
137 SkPath path3;
138 SkPath path4;
139 for (int i = 0; i < 1000; ++i) {
140 SkScalar s = SkIntToScalar(i);
141 path3.conicTo(s, 3.f * s / 4, s + 1.f, s, 0.5f + s / 2000.f);
142 path4.conicTo(s, 3.f * s / 4, s + 1.f, s, 0.3f + s / 2000.f);
143 }
144
145 GrUniqueKey key3, key4;
146 // These aren't marked volatile and so should have keys
147 GrPath::ComputeKey(GrShape(path3, style), &key3, &isVolatile);
148 REPORTER_ASSERT(reporter, !isVolatile);
149 REPORTER_ASSERT(reporter, key3.isValid());
150 GrPath::ComputeKey(GrShape(path4, style), &key4, &isVolatile);
151 REPORTER_ASSERT(reporter, !isVolatile);
152 REPORTER_ASSERT(reporter, key4.isValid());
153 REPORTER_ASSERT(reporter, key3 != key4);
154
155 {
156 GrUniqueKey tempKey;
157 path3.setIsVolatile(true);
158 GrPath::ComputeKey(GrShape(path3, style), &key1, &isVolatile);
159 REPORTER_ASSERT(reporter, isVolatile);
160 REPORTER_ASSERT(reporter, !tempKey.isValid());
161 }
162 }
fmalita3b444482015-11-19 07:28:50 -0800163}