Flutter Engine
The Flutter Engine
ImageCycleBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 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 <memory>
9
10#include "bench/Benchmark.h"
11#include "bench/GpuTools.h"
16#include "src/base/SkRandom.h"
17
18
19/**
20 * Draws a small set of small images multiple times each with no overlaps so that each image could
21 * be batched. This was originally added to detect regressions as TextureOp is refactored to
22 * use "dynamic state" for texture bindings. Everything is kept small as we're mostly interested in
23 * CPU overhead.
24 */
25class ImageCycle : public Benchmark {
26public:
27 /**
28 * imageCnt is the number of images and repeat cnt is how many times each image is drawn per
29 * logical "frame."
30 */
31 ImageCycle(int imageCnt, int repeatCnt) : fImageCnt(imageCnt), fRepeatCnt(repeatCnt) {
32 fName.appendf("image_cycle_image_cnt_%d_repeat_cnt_%d", fImageCnt, fRepeatCnt);
33 }
34
36
37protected:
38 const char* onGetName() override { return fName.c_str(); }
39
40 void onPerCanvasPreDraw(SkCanvas* canvas) override {
41 auto ii = SkImageInfo::Make(kImageSize.fWidth, kImageSize.fHeight, kRGBA_8888_SkColorType,
42 kPremul_SkAlphaType, nullptr);
43 SkRandom random;
44 fImages = std::make_unique<sk_sp<SkImage>[]>(fImageCnt);
45 for (int i = 0; i < fImageCnt; ++i) {
46 auto surf = canvas->makeSurface(ii);
47 SkColor color = random.nextU();
48 surf->getCanvas()->clear(color);
50 paint.setColor(~color);
51 paint.setBlendMode(SkBlendMode::kSrc);
52 surf->getCanvas()->drawRect(
53 SkRect::MakeLTRB(1, 1, kImageSize.fWidth - 1, kImageSize.fHeight - 1), paint);
54 fImages[i] = surf->makeImageSnapshot();
55 }
56 }
57
58 void onPerCanvasPostDraw(SkCanvas*) override { fImages.reset(); }
59
60 void onDraw(int loops, SkCanvas* canvas) override {
62 paint.setAntiAlias(true);
63 static constexpr SkScalar kPad = 2;
64 // To avoid tripping up bounds tracking we position the draws such that all the
65 // draws of image 0 are above those of image 1, etc.
66 static const int imagesPerRow =
67 SkScalarFloorToInt(kDeviceSize.fWidth / (kImageSize.fWidth + kPad));
68 int rowsPerImage = SkScalarCeilToInt((SkScalar)fRepeatCnt / imagesPerRow);
69 for (int l = 0; l < loops; ++l) {
70 for (int r = 0; r < fRepeatCnt; ++r) {
71 for (int i = 0; i < fImageCnt; ++i) {
72 SkScalar imageYOffset = i * rowsPerImage * (kImageSize.fHeight + kPad);
73 SkScalar rowYOffset = (r / imagesPerRow) * (kImageSize.fHeight + kPad);
74 SkScalar x = (r % imagesPerRow) * (kImageSize.fWidth + kPad);
75 canvas->drawImage(fImages[i].get(), x, imageYOffset + rowYOffset,
77 }
78 }
79 // Prevent any batching between "frames".
81 }
82 }
83
84private:
85 SkISize onGetSize() override { return {kDeviceSize.fWidth, kDeviceSize.fHeight}; }
86
87 inline static constexpr SkISize kImageSize{4, 4};
88 inline static constexpr SkISize kDeviceSize{64, 64};
89
90 std::unique_ptr<sk_sp<SkImage>[]> fImages;
91 SkString fName;
92 int fImageCnt;
93 int fRepeatCnt;
94
95 using INHERITED = Benchmark;
96};
97
98DEF_BENCH(return new ImageCycle(5, 10));
const char * backend
DEF_BENCH(return new ImageCycle(5, 10))
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
uint32_t SkColor
Definition: SkColor.h:37
#define SkScalarCeilToInt(x)
Definition: SkScalar.h:36
#define SkScalarFloorToInt(x)
Definition: SkScalar.h:35
constexpr int kPad
void onDraw(int loops, SkCanvas *canvas) override
void onPerCanvasPostDraw(SkCanvas *) override
void onPerCanvasPreDraw(SkCanvas *canvas) override
bool isSuitableFor(Backend backend) override
ImageCycle(int imageCnt, int repeatCnt)
const char * onGetName() override
SkSurface * getSurface() const
Definition: SkCanvas.cpp:365
sk_sp< SkSurface > makeSurface(const SkImageInfo &info, const SkSurfaceProps *props=nullptr)
Definition: SkCanvas.cpp:1195
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
uint32_t nextU()
Definition: SkRandom.h:42
const char * c_str() const
Definition: SkString.h:133
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
double x
const myers::Point & get(const myers::Segment &)
void FlushAndSubmit(SkSurface *surface)
Definition: GpuTools.h:34
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkSize.h:16
int32_t fHeight
Definition: SkSize.h:18
int32_t fWidth
Definition: SkSize.h:17
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646