xref: /aosp_15_r20/external/skia/gm/fatpathfill.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2012 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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathUtils.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSurface.h"
17 #include "tools/ToolUtils.h"
18 
19 #define ZOOM    32
20 #define SMALL_W 9
21 #define SMALL_H 3
22 #define REPEAT_LOOP 5
23 
new_surface(int width,int height)24 static sk_sp<SkSurface> new_surface(int width, int height) {
25     return SkSurfaces::Raster(SkImageInfo::MakeN32Premul(width, height));
26 }
27 
draw_pixel_centers(SkCanvas * canvas)28 static void draw_pixel_centers(SkCanvas* canvas) {
29     SkPaint paint;
30     paint.setColor(ToolUtils::color_to_565(0xFF0088FF));
31     paint.setAntiAlias(true);
32 
33     for (int y = 0; y < SMALL_H; ++y) {
34         for (int x = 0; x < SMALL_W; ++x) {
35             canvas->drawCircle(x + 0.5f, y + 0.5f, 1.5f / ZOOM, paint);
36         }
37     }
38 }
39 
draw_fatpath(SkCanvas * canvas,SkSurface * surface,const SkPath & path)40 static void draw_fatpath(SkCanvas* canvas, SkSurface* surface, const SkPath& path) {
41     SkPaint paint;
42 
43     surface->getCanvas()->clear(SK_ColorTRANSPARENT);
44     surface->getCanvas()->drawPath(path, paint);
45     surface->draw(canvas, 0, 0);
46 
47     paint.setAntiAlias(true);
48     paint.setColor(SK_ColorRED);
49     paint.setStyle(SkPaint::kStroke_Style);
50     canvas->drawPath(path, paint);
51 
52     draw_pixel_centers(canvas);
53 }
54 
DEF_SIMPLE_GM(fatpathfill,canvas,SMALL_W * ZOOM,SMALL_H * ZOOM * REPEAT_LOOP)55 DEF_SIMPLE_GM(fatpathfill, canvas,
56               SMALL_W * ZOOM,
57               SMALL_H * ZOOM * REPEAT_LOOP) {
58         auto surface(new_surface(SMALL_W, SMALL_H));
59 
60         canvas->scale(ZOOM, ZOOM);
61 
62         SkPaint paint;
63         paint.setStyle(SkPaint::kStroke_Style);
64         paint.setStrokeWidth(SK_Scalar1);
65 
66         for (int i = 0; i < REPEAT_LOOP; ++i) {
67             SkPath line, path;
68             line.moveTo(1, 2);
69             line.lineTo(SkIntToScalar(4 + i), 1);
70             skpathutils::FillPathWithPaint(line, paint, &path);
71             draw_fatpath(canvas, surface.get(), path);
72 
73             canvas->translate(0, SMALL_H);
74         }
75 }
76