blob: 3702a7cc3b03ee45e2c4673efa7b73b43155a2bc [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"
14#include "SkBitmap.h"
15#include "SkCanvas.h"
16#include "SkColor.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000017#include "SkPaint.h"
fmalitafbe1c112015-11-18 20:12:56 -080018#include "SkPath.h"
19#include "SkDashPathEffect.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000020#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "SkRect.h"
reed69f6f002014-09-18 06:09:44 -070022#include "SkSurface.h"
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000023#include "Test.h"
24
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000025static void test_drawPathEmpty(skiatest::Reporter*, SkCanvas* canvas) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000026 // Filling an empty path should not crash.
27 SkPaint paint;
fmalitafbe1c112015-11-18 20:12:56 -080028 SkRect emptyRect = SkRect::MakeEmpty();
29 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000030 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080031 canvas->drawOval(emptyRect, paint);
32 canvas->drawRect(emptyRect, paint);
33 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000034
35 // Stroking an empty path should not crash.
36 paint.setAntiAlias(true);
37 paint.setStyle(SkPaint::kStroke_Style);
38 paint.setColor(SK_ColorGRAY);
39 paint.setStrokeWidth(SkIntToScalar(20));
40 paint.setStrokeJoin(SkPaint::kRound_Join);
fmalitafbe1c112015-11-18 20:12:56 -080041 canvas->drawRect(emptyRect, paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000042 canvas->drawPath(SkPath(), paint);
fmalitafbe1c112015-11-18 20:12:56 -080043 canvas->drawOval(emptyRect, paint);
44 canvas->drawRect(emptyRect, paint);
45 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000046}
47
fmalitafbe1c112015-11-18 20:12:56 -080048static void fill_and_stroke(SkCanvas* canvas, const SkPath& p1, const SkPath& p2,
49 SkPathEffect* effect) {
50 SkPaint paint;
51 paint.setAntiAlias(true);
52 paint.setPathEffect(effect);
53
54 canvas->drawPath(p1, paint);
55 canvas->drawPath(p2, paint);
56
57 paint.setStyle(SkPaint::kStroke_Style);
58 canvas->drawPath(p1, paint);
59 canvas->drawPath(p2, paint);
60}
61
62static void test_drawSameRectOvals(skiatest::Reporter*, SkCanvas* canvas) {
63 // Drawing ovals with similar bounds but different points order should not crash.
64
65 SkPath oval1, oval2;
66 const SkRect rect = SkRect::MakeWH(100, 50);
67 oval1.addOval(rect, SkPath::kCW_Direction);
68 oval2.addOval(rect, SkPath::kCCW_Direction);
69
70 fill_and_stroke(canvas, oval1, oval2, nullptr);
71
72 const SkScalar intervals[] = { 1, 1 };
73 SkAutoTUnref<SkPathEffect> dashEffect(SkDashPathEffect::Create(intervals, 2, 0));
74 fill_and_stroke(canvas, oval1, oval2, dashEffect);
75}
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000076
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000077DEF_GPUTEST(GpuDrawPath, reporter, factory) {
fmalitafbe1c112015-11-18 20:12:56 -080078 // https://bugs.chromium.org/p/skia/issues/detail?id=4581
stephana1ac3f402015-11-18 18:35:56 -080079 return;
80
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000081 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
82 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
83
84 GrContext* grContext = factory->get(glType);
halcanary96fcdcc2015-08-27 07:41:13 -070085 if (nullptr == grContext) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000086 continue;
87 }
88 static const int sampleCounts[] = { 0, 4, 16 };
89
90 for (size_t i = 0; i < SK_ARRAY_COUNT(sampleCounts); ++i) {
reed69f6f002014-09-18 06:09:44 -070091 SkImageInfo info = SkImageInfo::MakeN32Premul(255, 255);
92
bsalomonafe30052015-01-16 07:32:33 -080093 SkAutoTUnref<SkSurface> surface(
94 SkSurface::NewRenderTarget(grContext, SkSurface::kNo_Budgeted, info,
halcanary96fcdcc2015-08-27 07:41:13 -070095 sampleCounts[i], nullptr));
reed69f6f002014-09-18 06:09:44 -070096 test_drawPathEmpty(reporter, surface->getCanvas());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +000097 }
98 }
99}
100
fmalitafbe1c112015-11-18 20:12:56 -0800101DEF_GPUTEST(GpuDrawPathSameRectOvals, reporter, factory) {
102 GrContext* grContext = factory->get(GrContextFactory::kNVPR_GLContextType);
103 if (!grContext) {
104 return;
105 }
106
107 SkAutoTUnref<SkSurface> surface(
108 SkSurface::NewRenderTarget(grContext, SkSurface::kNo_Budgeted,
109 SkImageInfo::MakeN32Premul(255, 255), 4));
110 test_drawSameRectOvals(reporter, surface->getCanvas());
111}
112
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000113#endif