• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkSurface.h"
15 #include "include/utils/SkRandom.h"
16 
17 /**
18  * Draws a small set of small images multiple times each with no overlaps so that each image could
19  * be batched. This was originally added to detect regressions as TextureOp is refactored to
20  * use "dynamic state" for texture bindings. Everything is kept small as we're mostly interested in
21  * CPU overhead.
22  */
23 class ImageCycle : public Benchmark {
24 public:
25     /**
26      * imageCnt is the number of images and repeat cnt is how many times each image is drawn per
27      * logical "frame."
28      */
ImageCycle(int imageCnt,int repeatCnt)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 
isSuitableFor(Backend backend)33     bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
34 
35 protected:
onGetName()36     const char* onGetName() override { return fName.c_str(); }
37 
onPerCanvasPreDraw(SkCanvas * canvas)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;
42         fImages = std::make_unique<sk_sp<SkImage>[]>(fImageCnt);
43         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 
onPerCanvasPostDraw(SkCanvas *)56     void onPerCanvasPostDraw(SkCanvas*) override { fImages.reset(); }
57 
onDraw(int loops,SkCanvas * canvas)58     void onDraw(int loops, SkCanvas* canvas) override {
59         SkPaint paint;
60         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);
73                     canvas->drawImage(fImages[i].get(), x, imageYOffset + rowYOffset,
74                                       SkSamplingOptions(), &paint);
75                 }
76             }
77             // Prevent any batching between "frames".
78             if (auto surf = canvas->getSurface()) {
79                 surf->flushAndSubmit();
80             }
81         }
82     }
83 
84 private:
onGetSize()85     SkIPoint 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 
98 DEF_BENCH(return new ImageCycle(5, 10));
99