blob: 42083fe08f2f3788d82688934dd7700d6614c29c [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"
13#include "GrContextFactory.h"
fmalita3b444482015-11-19 07:28:50 -080014#include "GrPath.h"
15#include "GrStrokeInfo.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000016#include "SkBitmap.h"
17#include "SkCanvas.h"
18#include "SkColor.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000019#include "SkPaint.h"
fmalitafbe1c112015-11-18 20:12:56 -080020#include "SkPath.h"
21#include "SkDashPathEffect.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000022#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000023#include "SkRect.h"
reed69f6f002014-09-18 06:09:44 -070024#include "SkSurface.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000025#include "Test.h"
26
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000027static void test_drawPathEmpty(skiatest::Reporter*, SkCanvas* canvas) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000028 // Filling an empty path should not crash.
29 SkPaint paint;
fmalitafbe1c112015-11-18 20:12:56 -080030 SkRect emptyRect = SkRect::MakeEmpty();
31 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000032 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080033 canvas->drawOval(emptyRect, paint);
34 canvas->drawRect(emptyRect, paint);
35 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000036
37 // Stroking an empty path should not crash.
38 paint.setAntiAlias(true);
39 paint.setStyle(SkPaint::kStroke_Style);
40 paint.setColor(SK_ColorGRAY);
41 paint.setStrokeWidth(SkIntToScalar(20));
42 paint.setStrokeJoin(SkPaint::kRound_Join);
fmalitafbe1c112015-11-18 20:12:56 -080043 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000044 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080045 canvas->drawOval(emptyRect, paint);
46 canvas->drawRect(emptyRect, paint);
47 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000048}
49
fmalitafbe1c112015-11-18 20:12:56 -080050static void fill_and_stroke(SkCanvas* canvas, const SkPath& p1, const SkPath& p2,
51 SkPathEffect* effect) {
52 SkPaint paint;
53 paint.setAntiAlias(true);
54 paint.setPathEffect(effect);
55
56 canvas->drawPath(p1, paint);
57 canvas->drawPath(p2, paint);
58
59 paint.setStyle(SkPaint::kStroke_Style);
60 canvas->drawPath(p1, paint);
61 canvas->drawPath(p2, paint);
62}
63
64static void test_drawSameRectOvals(skiatest::Reporter*, SkCanvas* canvas) {
65 // Drawing ovals with similar bounds but different points order should not crash.
66
67 SkPath oval1, oval2;
68 const SkRect rect = SkRect::MakeWH(100, 50);
69 oval1.addOval(rect, SkPath::kCW_Direction);
70 oval2.addOval(rect, SkPath::kCCW_Direction);
71
72 fill_and_stroke(canvas, oval1, oval2, nullptr);
73
74 const SkScalar intervals[] = { 1, 1 };
75 SkAutoTUnref<SkPathEffect> dashEffect(SkDashPathEffect::Create(intervals, 2, 0));
76 fill_and_stroke(canvas, oval1, oval2, dashEffect);
77}
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000078
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000079DEF_GPUTEST(GpuDrawPath, reporter, factory) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000080 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
81 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
82
83 GrContext* grContext = factory->get(glType);
halcanary96fcdcc2015-08-27 07:41:13 -070084 if (nullptr == grContext) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000085 continue;
86 }
87 static const int sampleCounts[] = { 0, 4, 16 };
88
89 for (size_t i = 0; i < SK_ARRAY_COUNT(sampleCounts); ++i) {
reed69f6f002014-09-18 06:09:44 -070090 SkImageInfo info = SkImageInfo::MakeN32Premul(255, 255);
91
bsalomonafe30052015-01-16 07:32:33 -080092 SkAutoTUnref<SkSurface> surface(
93 SkSurface::NewRenderTarget(grContext, SkSurface::kNo_Budgeted, info,
halcanary96fcdcc2015-08-27 07:41:13 -070094 sampleCounts[i], nullptr));
kkinnunenc11a5272015-11-19 09:37:02 -080095 if (!surface) {
96 continue;
97 }
reed69f6f002014-09-18 06:09:44 -070098 test_drawPathEmpty(reporter, surface->getCanvas());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000099 }
100 }
101}
102
fmalitafbe1c112015-11-18 20:12:56 -0800103DEF_GPUTEST(GpuDrawPathSameRectOvals, reporter, factory) {
104 GrContext* grContext = factory->get(GrContextFactory::kNVPR_GLContextType);
105 if (!grContext) {
106 return;
107 }
108
109 SkAutoTUnref<SkSurface> surface(
110 SkSurface::NewRenderTarget(grContext, SkSurface::kNo_Budgeted,
111 SkImageInfo::MakeN32Premul(255, 255), 4));
112 test_drawSameRectOvals(reporter, surface->getCanvas());
113}
114
fmalita3b444482015-11-19 07:28:50 -0800115DEF_TEST(GrPathKeys, reporter) {
116 // Keys should not ignore conic weights.
117 SkPath path1, path2;
118 path1.setIsVolatile(true);
119 path2.setIsVolatile(true);
120 SkPoint p0 = SkPoint::Make(100, 0);
121 SkPoint p1 = SkPoint::Make(100, 100);
122
123 path1.conicTo(p0, p1, .5f);
124 path2.conicTo(p0, p1, .7f);
125
126 bool isVolatile;
127 GrUniqueKey key1, key2;
128 GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle);
129 GrPath::ComputeKey(path1, stroke, &key1, &isVolatile);
130 GrPath::ComputeKey(path2, stroke, &key2, &isVolatile);
131
132 // https://bugs.chromium.org/p/skia/issues/detail?id=4580
133 // REPORTER_ASSERT(reporter, key1 != key2);
134}
135
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000136#endif