aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/fatpathfill.cpp
blob: ba6fffee128068fc251a674a36586c3914bed685 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "SkCanvas.h"
#include "SkPath.h"
#include "SkSurface.h"

#define ZOOM    32
#define SMALL_W 9
#define SMALL_H 3
#define REPEAT_LOOP 5

static SkSurface* new_surface(int width, int height) {
    return SkSurface::NewRasterPMColor(width, height);
}

static void draw_pixel_centers(SkCanvas* canvas) {
    SkPaint paint;
    paint.setColor(0xFF0088FF);
    paint.setAntiAlias(true);

    for (int y = 0; y < SMALL_H; ++y) {
        for (int x = 0; x < SMALL_W; ++x) {
            canvas->drawCircle(x + 0.5f, y + 0.5f, 1.5f / ZOOM, paint);
        }
    }
}

static void draw_fatpath(SkCanvas* canvas, SkSurface* surface,
                         const SkPath paths[], int count) {
    SkPaint paint;

    surface->getCanvas()->clear(SK_ColorTRANSPARENT);
    for (int i = 0; i < count; ++i) {
        surface->getCanvas()->drawPath(paths[i], paint);
    }
    surface->draw(canvas, 0, 0, NULL);

    paint.setAntiAlias(true);
    paint.setColor(SK_ColorRED);
    paint.setStyle(SkPaint::kStroke_Style);
    for (int j = 0; j < count; ++j) {
        canvas->drawPath(paths[j], paint);
    }

    draw_pixel_centers(canvas);
}

class FatPathFillGM : public skiagm::GM {
public:
    FatPathFillGM() {}

protected:
    virtual uint32_t onGetFlags() const SK_OVERRIDE {
        return kSkipTiled_Flag;
    }

    virtual SkString onShortName() {
        return SkString("fatpathfill");
    }

    virtual SkISize onISize() {
        return SkISize::Make(SMALL_W * ZOOM, SMALL_H * ZOOM * REPEAT_LOOP);
    }

    virtual void onDraw(SkCanvas* canvas) {
        SkAutoTUnref<SkSurface> surface(new_surface(SMALL_W, SMALL_H));

        canvas->scale(ZOOM, ZOOM);

        SkPaint paint;
        paint.setStyle(SkPaint::kStroke_Style);
        paint.setStrokeWidth(SK_Scalar1);

        for (int i = 0; i < REPEAT_LOOP; ++i) {
            SkPath line, path;
            line.moveTo(SkIntToScalar(1), SkIntToScalar(2));
            line.lineTo(SkIntToScalar(4 + i), SkIntToScalar(1));
            paint.getFillPath(line, &path);
            draw_fatpath(canvas, surface, &path, 1);

            canvas->translate(0, SMALL_H);
        }
    }

private:
    typedef skiagm::GM INHERITED;
};

///////////////////////////////////////////////////////////////////////////////

DEF_GM(return new FatPathFillGM;)