blob: 16322c07dfe8009824b871b1eec00e2f83a5277d [file] [log] [blame]
Brian Salomon7eae3e02018-08-07 14:02:38 +00001/*
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
John Stilesfbd050b2020-08-03 13:21:46 -04008#include <memory>
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "bench/Benchmark.h"
Brian Salomon7eae3e02018-08-07 14:02:38 +000011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
13#include "include/core/SkImage.h"
14#include "include/core/SkSurface.h"
15#include "include/utils/SkRandom.h"
Brian Salomon7eae3e02018-08-07 14:02:38 +000016
17/**
18 * Draws a small set of small images multiple times each with no overlaps so that each image could
Robert Phillipsfbf02142021-09-01 16:31:34 -040019 * be batched. This was originally added to detect regressions as TextureOp is refactored to
Brian Salomon7eae3e02018-08-07 14:02:38 +000020 * use "dynamic state" for texture bindings. Everything is kept small as we're mostly interested in
21 * CPU overhead.
22 */
23class ImageCycle : public Benchmark {
24public:
25 /**
26 * imageCnt is the number of images and repeat cnt is how many times each image is drawn per
27 * logical "frame."
28 */
29 ImageCycle(int imageCnt, int repeatCnt) : fImageCnt(imageCnt), fRepeatCnt(repeatCnt) {
30 fName.appendf("image_cycle_image_cnt_%d_repeat_cnt_%d", fImageCnt, fRepeatCnt);
31 }
32
33 bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
34
35protected:
36 const char* onGetName() override { return fName.c_str(); }
37
38 void onPerCanvasPreDraw(SkCanvas* canvas) override {
39 auto ii = SkImageInfo::Make(kImageSize.fWidth, kImageSize.fHeight, kRGBA_8888_SkColorType,
40 kPremul_SkAlphaType, nullptr);
41 SkRandom random;
John Stilesfbd050b2020-08-03 13:21:46 -040042 fImages = std::make_unique<sk_sp<SkImage>[]>(fImageCnt);
Brian Salomon7eae3e02018-08-07 14:02:38 +000043 for (int i = 0; i < fImageCnt; ++i) {
44 auto surf = canvas->makeSurface(ii);
45 SkColor color = random.nextU();
46 surf->getCanvas()->clear(color);
47 SkPaint paint;
48 paint.setColor(~color);
49 paint.setBlendMode(SkBlendMode::kSrc);
50 surf->getCanvas()->drawRect(
51 SkRect::MakeLTRB(1, 1, kImageSize.fWidth - 1, kImageSize.fHeight - 1), paint);
52 fImages[i] = surf->makeImageSnapshot();
53 }
54 }
55
56 void onPerCanvasPostDraw(SkCanvas*) override { fImages.reset(); }
57
58 void onDraw(int loops, SkCanvas* canvas) override {
59 SkPaint paint;
Brian Salomon7eae3e02018-08-07 14:02:38 +000060 paint.setAntiAlias(true);
61 static constexpr SkScalar kPad = 2;
62 // To avoid tripping up bounds tracking we position the draws such that all the
63 // draws of image 0 are above those of image 1, etc.
64 static const int imagesPerRow =
65 SkScalarFloorToInt(kDeviceSize.fWidth / (kImageSize.fWidth + kPad));
66 int rowsPerImage = SkScalarCeilToInt((SkScalar)fRepeatCnt / imagesPerRow);
67 for (int l = 0; l < loops; ++l) {
68 for (int r = 0; r < fRepeatCnt; ++r) {
69 for (int i = 0; i < fImageCnt; ++i) {
70 SkScalar imageYOffset = i * rowsPerImage * (kImageSize.fHeight + kPad);
71 SkScalar rowYOffset = (r / imagesPerRow) * (kImageSize.fHeight + kPad);
72 SkScalar x = (r % imagesPerRow) * (kImageSize.fWidth + kPad);
Mike Reede02d7f82021-01-21 22:25:21 -050073 canvas->drawImage(fImages[i].get(), x, imageYOffset + rowYOffset,
74 SkSamplingOptions(), &paint);
Brian Salomon7eae3e02018-08-07 14:02:38 +000075 }
76 }
77 // Prevent any batching between "frames".
Mike Reeda2b3b9e2019-11-15 15:00:27 -050078 if (auto surf = canvas->getSurface()) {
Greg Daniel0a2464f2020-05-14 15:45:44 -040079 surf->flushAndSubmit();
Mike Reeda2b3b9e2019-11-15 15:00:27 -050080 }
Brian Salomon7eae3e02018-08-07 14:02:38 +000081 }
82 }
83
84private:
85 SkIPoint onGetSize() override { return {kDeviceSize.fWidth, kDeviceSize.fHeight}; }
86
87 static constexpr SkISize kImageSize{4, 4};
88 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
John Stiles7571f9e2020-09-02 22:42:33 -040095 using INHERITED = Benchmark;
Brian Salomon7eae3e02018-08-07 14:02:38 +000096};
97
98DEF_BENCH(return new ImageCycle(5, 10));