blob: b8c9cf2e84a5fdf1fa367456eccf7fb7d4ad1b67 [file] [log] [blame]
mike@reedtribe.org43c62b12012-07-03 02:44:02 +00001/*
2 * Copyright 2011 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Reed06d7c9d2020-08-26 12:56:51 -040012#include "include/core/SkPathBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkRect.h"
14#include "include/core/SkScalar.h"
15#include "include/core/SkTypes.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050016#include "src/base/SkRandom.h"
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000017
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000018static void test_hittest(SkCanvas* canvas, const SkPath& path) {
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000019 SkPaint paint;
20 SkRect r = path.getBounds();
rmistry@google.comd6176b02012-08-23 18:14:13 +000021
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000022 paint.setColor(SK_ColorRED);
23 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.comdbc5d282012-07-03 12:23:22 +000025 const SkScalar MARGIN = SkIntToScalar(4);
rmistry@google.comd6176b02012-08-23 18:14:13 +000026
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000027 paint.setColor(0x800000FF);
reed@google.comdbc5d282012-07-03 12:23:22 +000028 for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) {
29 for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) {
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000030 if (path.contains(x, y)) {
31 canvas->drawPoint(x, y, paint);
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000032 }
33 }
34 }
35}
36
Chris Daltonb7518a82020-05-26 12:14:30 -060037DEF_SIMPLE_GM_CAN_FAIL(hittestpath, canvas, errorMsg, 700, 460) {
Robert Phillips88d8aba2023-02-22 11:04:06 -050038 if (canvas->recordingContext() || canvas->recorder()) {
Chris Daltonb7518a82020-05-26 12:14:30 -060039 // GPU rasterization results vary greatly from platform to platform. We can't use them as
40 // an expected result for our internal SkPath::contains().
41 *errorMsg = "This test is for CPU configs only.";
42 return skiagm::DrawResult::kSkip;
43 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
Mike Reed06d7c9d2020-08-26 12:56:51 -040045 SkPathBuilder b;
Chris Daltonb7518a82020-05-26 12:14:30 -060046 SkRandom rand;
47
48 int scale = 300;
49 for (int i = 0; i < 4; ++i) {
50 // get the random values deterministically
51 SkScalar randoms[12];
Herb Derbyc37b3862022-06-21 09:49:17 -040052 for (int index = 0; index < (int) std::size(randoms); ++index) {
Chris Daltonb7518a82020-05-26 12:14:30 -060053 randoms[index] = rand.nextUScalar1();
mike@reedtribe.org43c62b12012-07-03 02:44:02 +000054 }
Mike Reed06d7c9d2020-08-26 12:56:51 -040055 b.lineTo(randoms[0] * scale, randoms[1] * scale)
56 .quadTo(randoms[2] * scale, randoms[3] * scale,
57 randoms[4] * scale, randoms[5] * scale)
58 .cubicTo(randoms[6] * scale, randoms[7] * scale,
59 randoms[8] * scale, randoms[9] * scale,
60 randoms[10] * scale, randoms[11] * scale);
Chris Daltonb7518a82020-05-26 12:14:30 -060061 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
Mike Reed06d7c9d2020-08-26 12:56:51 -040063 b.setFillType(SkPathFillType::kEvenOdd);
64 b.offset(SkIntToScalar(20), SkIntToScalar(20));
65
66 SkPath path = b.detach();
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
Chris Daltonb7518a82020-05-26 12:14:30 -060068 test_hittest(canvas, path);
mike@reedtribe.orgbad1b2f2012-07-11 01:51:33 +000069
Chris Daltonb7518a82020-05-26 12:14:30 -060070 canvas->translate(SkIntToScalar(scale), 0);
71 path.setFillType(SkPathFillType::kWinding);
rmistry@google.comd6176b02012-08-23 18:14:13 +000072
Chris Daltonb7518a82020-05-26 12:14:30 -060073 test_hittest(canvas, path);
74 return skiagm::DrawResult::kOk;
halcanary2a243382015-09-09 08:16:41 -070075}