xref: /aosp_15_r20/external/skia/gm/hardstop_gradients_many.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google LLC
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 /*
9  * This GM presents different gradients with an increasing number of
10  * hardstops, from 1 to 100.
11  */
12 
13 #include "gm/gm.h"
14 #include "include/core/SkCanvas.h"
15 #include "include/core/SkColor.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkTileMode.h"
25 #include "include/effects/SkGradientShader.h"
26 
27 #include <vector>
28 
29 const int kWidth  = 1000;
30 const int kHeight = 2000;
31 const int kNumRows = 100;
32 const int kCellHeight = kHeight / kNumRows;
33 const int kPadHeight = 1;
34 const int kRectHeight = kCellHeight - (2 * kPadHeight);
35 
36 class HardstopGradientsManyGM : public skiagm::GM {
37 public:
HardstopGradientsManyGM()38     HardstopGradientsManyGM() {}
39 
40 protected:
getName() const41     SkString getName() const override { return SkString("hardstop_gradients_many"); }
42 
getISize()43     SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
44 
onDraw(SkCanvas * canvas)45     void onDraw(SkCanvas* canvas) override {
46         static constexpr SkPoint points[] = {
47             SkPoint::Make(0,      kRectHeight / 2),
48             SkPoint::Make(kWidth, kRectHeight / 2),
49         };
50 
51         std::vector<SkColor> colors;
52         std::vector<SkScalar> positions;
53 
54         for (int row = 1; row <= kNumRows; ++row) {
55             // Assemble a gradient containing a blue-to-white blend, repeated N times per row.
56             colors.push_back(SK_ColorBLUE);
57             colors.push_back(SK_ColorWHITE);
58 
59             positions = {0.0f};
60             for (int pos = 1; pos < row; ++pos) {
61                 float place = SkScalar(pos) / SkScalar(row);
62                 positions.push_back(place);
63                 positions.push_back(place);
64             }
65             positions.push_back(1.0f);
66             SkASSERT(positions.size() == colors.size());
67 
68             // Draw it.
69             auto shader = SkGradientShader::MakeLinear(points,
70                                                        colors.data(),
71                                                        positions.data(),
72                                                        colors.size(),
73                                                        SkTileMode::kClamp,
74                                                        /*flags=*/0,
75                                                        /*localMatrix=*/nullptr);
76             SkPaint paint;
77             paint.setShader(shader);
78             canvas->drawRect(SkRect::MakeXYWH(0, kPadHeight, kWidth, kRectHeight), paint);
79 
80             canvas->translate(0, kCellHeight);
81         }
82     }
83 
84 private:
85     using INHERITED = skiagm::GM;
86 };
87 
88 DEF_GM(return new HardstopGradientsManyGM;)
89