11fe834dd68fcb57a4297c9d0a08cfbb06732559
[platform/upstream/libSkiaSharp.git] / gm / hittestpath.cpp
1 /*
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
8 #include "gm.h"
9 #include "SkCanvas.h"
10 #include "SkCullPoints.h"
11 #include "SkRandom.h"
12
13 static void test_hittest(SkCanvas* canvas, const SkPath& path) {
14     SkPaint paint;
15     SkRect r = path.getBounds();
16
17     paint.setColor(SK_ColorRED);
18     canvas->drawPath(path, paint);
19
20     const SkScalar MARGIN = SkIntToScalar(4);
21
22     paint.setColor(0x800000FF);
23     for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) {
24         for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) {
25             if (path.contains(x, y)) {
26                 canvas->drawPoint(x, y, paint);
27             }
28         }
29     }
30 }
31
32 class HitTestPathGM : public skiagm::GM {
33 public:
34     HitTestPathGM () {}
35
36 protected:
37
38     SkString onShortName() SK_OVERRIDE {
39         return SkString("hittestpath");
40     }
41
42     SkISize onISize() SK_OVERRIDE { return SkISize::Make(700, 460); }
43
44     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
45         SkPath path;
46         SkRandom rand;
47
48         int scale = 300;
49         for (int i = 0; i < 4; ++i) {
50             path.lineTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
51             path.quadTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
52                         rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
53             path.cubicTo(rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
54                          rand.nextUScalar1() * scale, rand.nextUScalar1() * scale,
55                          rand.nextUScalar1() * scale, rand.nextUScalar1() * scale);
56         }
57
58         path.setFillType(SkPath::kEvenOdd_FillType);
59         path.offset(SkIntToScalar(20), SkIntToScalar(20));
60
61         test_hittest(canvas, path);
62
63         canvas->translate(SkIntToScalar(scale), 0);
64         path.setFillType(SkPath::kWinding_FillType);
65
66         test_hittest(canvas, path);
67     }
68
69 private:
70     typedef GM INHERITED;
71 };
72
73 //////////////////////////////////////////////////////////////////////////////
74
75 static skiagm::GM* MyFactory(void*) { return new HitTestPathGM; }
76 static skiagm::GMRegistry reg(MyFactory);