1 /*
2 * Copyright 2017 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/SkRRect.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "src/base/SkRandom.h"
18 #include "tools/ToolUtils.h"
19
20 namespace skiagm {
21
gen_color(SkRandom * rand)22 static SkColor gen_color(SkRandom* rand) {
23 SkScalar hsv[3];
24 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
25 hsv[1] = rand->nextRangeF(0.5f, 1.0f);
26 hsv[2] = rand->nextRangeF(0.5f, 1.0f);
27
28 return ToolUtils::color_to_565(SkHSVToColor(hsv));
29 }
30
31 class ManyCirclesGM : public GM {
32 // This GM attempts to flood Ganesh with more circles than will fit in a single index buffer
33 // Stresses crbug.com/688582.
34 public:
ManyCirclesGM()35 ManyCirclesGM() {
36 this->setBGColor(0xFFFFFFFF);
37 }
38
39 protected:
40 static const int kWidth = 800;
41 static const int kHeight = 600;
42
getName() const43 SkString getName() const override { return SkString("manycircles"); }
44
getISize()45 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
46
onDraw(SkCanvas * canvas)47 void onDraw(SkCanvas* canvas) override {
48 SkRandom rand(1);
49 SkPaint paint;
50 paint.setAntiAlias(true);
51 int total = 10000;
52 while (total--) {
53 SkScalar x = rand.nextF() * kWidth - 100;
54 SkScalar y = rand.nextF() * kHeight - 100;
55 SkScalar w = rand.nextF() * 200;
56 SkRect circle = SkRect::MakeXYWH(x, y, w, w);
57 paint.setColor(gen_color(&rand));
58 canvas->drawOval(circle, paint);
59 }
60 }
61
62 private:
63 using INHERITED = GM;
64 };
65
66 //////////////////////////////////////////////////////////////////////////////
67
68 class ManyRRectsGM : public GM {
69 // This GM attempts to flood Ganesh with more rrects than will fit in a single index buffer
70 // Stresses crbug.com/684112
71 public:
ManyRRectsGM()72 ManyRRectsGM() {
73 this->setBGColor(0xFFFFFFFF);
74 }
75
76 protected:
getName() const77 SkString getName() const override { return SkString("manyrrects"); }
78
getISize()79 SkISize getISize() override { return SkISize::Make(800, 300); }
80
onDraw(SkCanvas * canvas)81 void onDraw(SkCanvas* canvas) override {
82 SkRandom rand(1);
83 SkPaint paint;
84 paint.setAntiAlias(true);
85 paint.setColor(SK_ColorBLUE);
86 int total = 7000;
87
88 // Rectangle positioning variables
89 int x = 0;
90 int y = 0;
91 const int kXLimit = 700;
92 const int kYIncrement = 5;
93 const int kXIncrement = 5;
94
95 SkRect rect = SkRect::MakeLTRB(0, 0, 4, 4);
96 SkRRect rrect = SkRRect::MakeRectXY(rect, 1, 1);
97 while (total--) {
98 canvas->save();
99 canvas->translate(x, y);
100 canvas->drawRRect(rrect, paint);
101 x += kXIncrement;
102 if (x > kXLimit) {
103 x = 0;
104 y += kYIncrement;
105 }
106 canvas->restore();
107 }
108 }
109
110 private:
111 using INHERITED = GM;
112 };
113
114 //////////////////////////////////////////////////////////////////////////////
115
116 DEF_GM( return new ManyCirclesGM; )
117 DEF_GM( return new ManyRRectsGM; )
118
119 } // namespace skiagm
120