Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 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 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "bench/Benchmark.h" |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkImage.h" |
| 14 | #include "include/core/SkSurface.h" |
| 15 | #include "include/utils/SkRandom.h" |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 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 GrTextureOp 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 | */ |
| 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 | |
| 35 | protected: |
| 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 Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 42 | fImages = std::make_unique<sk_sp<SkImage>[]>(fImageCnt); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 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 | |
| 56 | void onPerCanvasPostDraw(SkCanvas*) override { fImages.reset(); } |
| 57 | |
| 58 | void onDraw(int loops, SkCanvas* canvas) override { |
| 59 | SkPaint paint; |
| 60 | paint.setFilterQuality(kNone_SkFilterQuality); |
| 61 | paint.setAntiAlias(true); |
| 62 | static constexpr SkScalar kPad = 2; |
| 63 | // To avoid tripping up bounds tracking we position the draws such that all the |
| 64 | // draws of image 0 are above those of image 1, etc. |
| 65 | static const int imagesPerRow = |
| 66 | SkScalarFloorToInt(kDeviceSize.fWidth / (kImageSize.fWidth + kPad)); |
| 67 | int rowsPerImage = SkScalarCeilToInt((SkScalar)fRepeatCnt / imagesPerRow); |
| 68 | for (int l = 0; l < loops; ++l) { |
| 69 | for (int r = 0; r < fRepeatCnt; ++r) { |
| 70 | for (int i = 0; i < fImageCnt; ++i) { |
| 71 | SkScalar imageYOffset = i * rowsPerImage * (kImageSize.fHeight + kPad); |
| 72 | SkScalar rowYOffset = (r / imagesPerRow) * (kImageSize.fHeight + kPad); |
| 73 | SkScalar x = (r % imagesPerRow) * (kImageSize.fWidth + kPad); |
| 74 | canvas->drawImage(fImages[i].get(), x, imageYOffset + rowYOffset, &paint); |
| 75 | } |
| 76 | } |
| 77 | // Prevent any batching between "frames". |
Mike Reed | a2b3b9e | 2019-11-15 15:00:27 -0500 | [diff] [blame] | 78 | if (auto surf = canvas->getSurface()) { |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 79 | surf->flushAndSubmit(); |
Mike Reed | a2b3b9e | 2019-11-15 15:00:27 -0500 | [diff] [blame] | 80 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | private: |
| 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 Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 95 | using INHERITED = Benchmark; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | DEF_BENCH(return new ImageCycle(5, 10)); |