Flutter Engine
The Flutter Engine
AnimatedRectsSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 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"
11#include "include/core/SkRect.h"
14#include "src/base/SkRandom.h"
16#include "tools/viewer/Slide.h"
17
18// This slide draws a lot of overlapping rectangles which slide left.
19// It's adapted from the performance test at https://benchmarks.slaylines.io/
20static constexpr int kWidth = 1000;
21static constexpr int kHeight = 639;
22static constexpr int kNumRects = 32000;
23
24class AnimatedRects : public Slide {
25public:
27 fName = "animated-rects";
28 }
29
30protected:
31 void load(SkScalar, SkScalar) override {
32 for (int i = 0; i < kNumRects; ++i) {
33 fRect[i].x = fRand.nextF() * kWidth;
34 fRect[i].y = fRand.nextF() * kHeight;
35 fRect[i].size = 10.0 + fRand.nextF() * 40.0;
36 fRect[i].speed = 1.0 + fRand.nextF();
37 }
38
39 fStrokePaint.setAntiAlias(true);
40 fStrokePaint.setColor(SK_ColorBLACK);
41 fStrokePaint.setStyle(SkPaint::kStroke_Style);
42 fStrokePaint.setStrokeWidth(2.0);
43 fFillPaint.setAntiAlias(true);
45 fFillPaint.setColor(SK_ColorWHITE);
46 }
47
48 void draw(SkCanvas* canvas) override {
49 SkAutoCanvasRestore acr(canvas, /*doSave=*/true);
50 canvas->clipRect({0, 0, (float)kWidth, (float)kHeight});
51
52 for (int i = 0; i < kNumRects; ++i) {
53 const AnimatedRect& r = fRect[i];
54 canvas->drawRect(SkRect{r.x, r.y, r.x + r.size, r.y + r.size}, fStrokePaint);
55 canvas->drawRect(SkRect{r.x, r.y, r.x + r.size, r.y + r.size}, fFillPaint);
56 }
57 }
58
59 bool animate(double nanos) override {
60 float seconds = 1e-9 * nanos;
61 if (0.0f != fLastTime) {
62 float scale = (seconds - fLastTime) * 60;
63
64 for (int i = 0; i < kNumRects; ++i) {
65 AnimatedRect& r = fRect[i];
66 r.x -= r.speed * scale;
67 if (r.x + r.size < 0) {
68 r.x = kWidth + r.size;
69 }
70 }
71 }
72
73 fLastTime = seconds;
74 return true;
75 }
76
77private:
78 struct AnimatedRect {
79 float x, y, size, speed;
80 };
81
82 AnimatedRect fRect[kNumRects];
83 SkRandom fRand;
84 SkPaint fStrokePaint, fFillPaint;
85 float fLastTime = 0.0f;
86};
87
88//////////////////////////////////////////////////////////////////////////////
89
90DEF_SLIDE(return new AnimatedRects;)
static constexpr int kNumRects
static constexpr int kWidth
static constexpr int kHeight
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define DEF_SLIDE(code)
Definition: Slide.h:25
void load(SkScalar, SkScalar) override
bool animate(double nanos) override
void draw(SkCanvas *canvas) override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
float nextF()
Definition: SkRandom.h:55
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
float SkScalar
Definition: extension.cpp:12
double y
double x
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
const Scalar scale
constexpr float x() const
Definition: SkRect.h:720