| Jim Van Verth | e549a05 | 2017-02-21 17:55:13 -0500 | [diff] [blame] | 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 |  | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" | 
| Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" | 
 | 10 | #include "include/core/SkColor.h" | 
 | 11 | #include "include/core/SkPaint.h" | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkRRect.h" | 
 | 13 | #include "include/core/SkRect.h" | 
| Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkScalar.h" | 
 | 15 | #include "include/core/SkSize.h" | 
 | 16 | #include "include/core/SkString.h" | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/utils/SkRandom.h" | 
 | 18 | #include "tools/ToolUtils.h" | 
| Jim Van Verth | e549a05 | 2017-02-21 17:55:13 -0500 | [diff] [blame] | 19 |  | 
 | 20 | namespace skiagm { | 
 | 21 |  | 
 | 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 |  | 
| Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 28 |     return ToolUtils::color_to_565(SkHSVToColor(hsv)); | 
| Jim Van Verth | e549a05 | 2017-02-21 17:55:13 -0500 | [diff] [blame] | 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: | 
 | 35 |     ManyCirclesGM() { | 
 | 36 |         this->setBGColor(0xFFFFFFFF); | 
 | 37 |     } | 
 | 38 |  | 
 | 39 | protected: | 
 | 40 |     static const int kWidth = 800; | 
 | 41 |     static const int kHeight = 600; | 
 | 42 |  | 
 | 43 |     SkString onShortName() override { | 
 | 44 |         return SkString("manycircles"); | 
 | 45 |     } | 
 | 46 |  | 
 | 47 |     SkISize onISize() override { | 
 | 48 |         return SkISize::Make(kWidth, kHeight); | 
 | 49 |     } | 
 | 50 |  | 
 | 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 |     typedef GM INHERITED; | 
 | 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: | 
 | 76 |     ManyRRectsGM() { | 
 | 77 |         this->setBGColor(0xFFFFFFFF); | 
 | 78 |     } | 
 | 79 |  | 
 | 80 | protected: | 
 | 81 |  | 
 | 82 |     SkString onShortName() override { | 
 | 83 |         return SkString("manyrrects"); | 
 | 84 |     } | 
 | 85 |  | 
 | 86 |     SkISize onISize() override { | 
 | 87 |         return SkISize::Make(800, 300); | 
 | 88 |     } | 
 | 89 |  | 
 | 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 |     typedef GM INHERITED; | 
 | 121 | }; | 
 | 122 |  | 
 | 123 | ////////////////////////////////////////////////////////////////////////////// | 
 | 124 |  | 
 | 125 | DEF_GM( return new ManyCirclesGM; ) | 
 | 126 | DEF_GM( return new ManyRRectsGM; ) | 
 | 127 |  | 
 | 128 | } |