• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/utils/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 
onShortName()43     SkString onShortName() override {
44         return SkString("manycircles");
45     }
46 
onISize()47     SkISize onISize() override {
48         return SkISize::Make(kWidth, kHeight);
49     }
50 
onDraw(SkCanvas * canvas)51     void onDraw(SkCanvas* canvas) override {
52         SkRandom rand(1);
53         SkPaint paint;
54         paint.setAntiAlias(true);
55         int total = 10000;
56         while (total--) {
57             SkScalar x = rand.nextF() * kWidth - 100;
58             SkScalar y = rand.nextF() * kHeight - 100;
59             SkScalar w = rand.nextF() * 200;
60             SkRect circle = SkRect::MakeXYWH(x, y, w, w);
61             paint.setColor(gen_color(&rand));
62             canvas->drawOval(circle, paint);
63         }
64     }
65 
66 private:
67     using INHERITED = GM;
68 };
69 
70 //////////////////////////////////////////////////////////////////////////////
71 
72 class ManyRRectsGM : public GM {
73     // This GM attempts to flood Ganesh with more rrects than will fit in a single index buffer
74     // Stresses crbug.com/684112
75 public:
ManyRRectsGM()76     ManyRRectsGM() {
77         this->setBGColor(0xFFFFFFFF);
78     }
79 
80 protected:
81 
onShortName()82     SkString onShortName() override {
83         return SkString("manyrrects");
84     }
85 
onISize()86     SkISize onISize() override {
87         return SkISize::Make(800, 300);
88     }
89 
onDraw(SkCanvas * canvas)90     void onDraw(SkCanvas* canvas) override {
91         SkRandom rand(1);
92         SkPaint paint;
93         paint.setAntiAlias(true);
94         paint.setColor(SK_ColorBLUE);
95         int total = 7000;
96 
97         // Rectangle positioning variables
98         int x = 0;
99         int y = 0;
100         const int kXLimit = 700;
101         const int kYIncrement = 5;
102         const int kXIncrement = 5;
103 
104         SkRect rect = SkRect::MakeLTRB(0, 0, 4, 4);
105         SkRRect rrect = SkRRect::MakeRectXY(rect, 1, 1);
106         while (total--) {
107             canvas->save();
108             canvas->translate(x, y);
109             canvas->drawRRect(rrect, paint);
110             x += kXIncrement;
111             if (x > kXLimit) {
112                 x = 0;
113                 y += kYIncrement;
114             }
115             canvas->restore();
116         }
117     }
118 
119 private:
120     using INHERITED = GM;
121 };
122 
123 //////////////////////////////////////////////////////////////////////////////
124 
125 DEF_GM( return new ManyCirclesGM; )
126 DEF_GM( return new ManyRRectsGM; )
127 
128 }  // namespace skiagm
129