xref: /aosp_15_r20/external/skia/gm/daa.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 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/SkFont.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPathBuilder.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkTypes.h"
16 #include "tools/fonts/FontToolUtils.h"
17 
18 // This GM shows off a flaw in delta-based rasterizers (DAA, CCPR, etc.).
19 // See also the bottom of dashing4 and skia:6886.
20 
21 static const int K = 49;
22 
23 DEF_SIMPLE_GM(daa, canvas, K+350, 5*K) {
24     SkPaint paint;
25     paint.setAntiAlias(true);
26     SkFont font = ToolUtils::DefaultPortableFont();
27 
28     {
29         paint.setColor(SK_ColorBLACK);
30         canvas->drawString("Should be a green square with no red showing through.",
31                            K*1.5f, K*0.5f, font, paint);
32 
33         paint.setColor(SK_ColorRED);
34         canvas->drawRect({0,0,K,K}, paint);
35 
36         SkPoint tri1[] = {{0,0},{K,K},{0,K},{0,0}};
37         SkPoint tri2[] = {{0,0},{K,K},{K,0},{0,0}};
38         SkPath path = SkPathBuilder().addPolygon(tri1, std::size(tri1), false)
39                                      .addPolygon(tri2, std::size(tri2), false)
40                                      .detach();
41 
42         paint.setColor(SK_ColorGREEN);
43         canvas->drawPath(path, paint);
44     }
45 
46     canvas->translate(0,K);
47     {
48         paint.setColor(SK_ColorBLACK);
49         canvas->drawString("Adjacent rects, two draws.  Blue then green, no red?",
50                            K*1.5f, K*0.5f, font, paint);
51 
52         paint.setColor(SK_ColorRED);
53         canvas->drawRect({0,0,K,K}, paint);
54 
55         {
56             SkPath path = SkPath::Polygon({{0,0},{0,K},{K*0.5f,K},{K*0.5f,0}}, false);
57             paint.setColor(SK_ColorBLUE);
58             canvas->drawPath(path, paint);
59         }
60 
61         {
62             SkPath path = SkPath::Polygon({{K*0.5f,0},{K*0.5f,K},{K,K},{K,0}}, false);
63             paint.setColor(SK_ColorGREEN);
64             canvas->drawPath(path, paint);
65         }
66     }
67 
68     canvas->translate(0,K);
69     {
70         paint.setColor(SK_ColorBLACK);
71         canvas->drawString("Adjacent rects, wound together.  All green?",
72                            K*1.5f, K*0.5f, font, paint);
73 
74         paint.setColor(SK_ColorRED);
75         canvas->drawRect({0,0,K,K}, paint);
76 
77         {
78             SkPath path = SkPathBuilder().addPolygon({{0,0},{0,K},{K*0.5f,K},{K*0.5f,0}}, false)
79                                          .addPolygon({{K*0.5f,0},{K*0.5f,K},{K,K},{K,0}}, false)
80                                          .detach();
81 
82             paint.setColor(SK_ColorGREEN);
83             canvas->drawPath(path, paint);
84         }
85     }
86 
87     canvas->translate(0,K);
88     {
89         paint.setColor(SK_ColorBLACK);
90         canvas->drawString("Adjacent rects, wound opposite.  All green?",
91                            K*1.5f, K*0.5f, font, paint);
92 
93         paint.setColor(SK_ColorRED);
94         canvas->drawRect({0,0,K,K}, paint);
95 
96         {
97             SkPath path = SkPathBuilder().addPolygon({{0,0},{0,K},{K*0.5f,K},{K*0.5f,0}}, false)
98                                          .addPolygon({{K*0.5f,0},{K,0},{K,K},{K*0.5f,K}}, false)
99                                          .detach();
100 
101             paint.setColor(SK_ColorGREEN);
102             canvas->drawPath(path, paint);
103         }
104     }
105 
106     canvas->translate(0,K);
107     {
108         paint.setColor(SK_ColorBLACK);
109         canvas->drawString("One poly, wound opposite.  All green?",
110                            K*1.5f, K*0.5f, font, paint);
111 
112         paint.setColor(SK_ColorRED);
113         canvas->drawRect({0,0,K,K}, paint);
114 
115         SkPath path = SkPath::Polygon({{K*0.5f,0},{0,0},{0,K},{K*0.5f,K},
116                                        {K*0.5f,0},{K,0},{K,K},{K*0.5f,K}},
117                                       false);
118 
119         paint.setColor(SK_ColorGREEN);
120         canvas->drawPath(path, paint);
121     }
122 }
123