| 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" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkPaint.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkString.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "src/base/SkRandom.h" |
| 15 |
#include "tools/timer/TimeUtils.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/ |
| 20 |
static constexpr int kWidth = 1000; |
| 21 |
static constexpr int kHeight = 639; |
| 22 |
static constexpr int kNumRects = 32000; |
| 23 |
|
| 24 |
class AnimatedRects : public Slide { |
| 25 |
public: |
| 26 |
AnimatedRects() { |
| 27 |
fName = "animated-rects"; |
| 28 |
} |
| 29 |
|
| 30 |
protected: |
| 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); |
| 44 |
fFillPaint.setStyle(SkPaint::kFill_Style); |
| 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 |
|
| 77 |
private: |
| 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 |
|
| 90 |
DEF_SLIDE(return new AnimatedRects;) |