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" 12 #include "include/core/SkCanvas.h" 13 #include "include/core/SkColorSpace.h" 14 #include "include/core/SkImage.h" 15 #include "include/core/SkSurface.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 */ 25 class ImageCycle : public Benchmark { 26 public: 27 /** 28 * imageCnt is the number of images and repeat cnt is how many times each image is drawn per 29 * logical "frame." 30 */ ImageCycle(int imageCnt,int repeatCnt)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 isSuitableFor(Backend backend)35 bool isSuitableFor(Backend backend) override { return Backend::kGanesh == backend; } 36 37 protected: onGetName()38 const char* onGetName() override { return fName.c_str(); } 39 onPerCanvasPreDraw(SkCanvas * canvas)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); 49 SkPaint paint; 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 onPerCanvasPostDraw(SkCanvas *)58 void onPerCanvasPostDraw(SkCanvas*) override { fImages.reset(); } 59 onDraw(int loops,SkCanvas * canvas)60 void onDraw(int loops, SkCanvas* canvas) override { 61 SkPaint paint; 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, 76 SkSamplingOptions(), &paint); 77 } 78 } 79 // Prevent any batching between "frames". 80 skgpu::FlushAndSubmit(canvas->getSurface()); 81 } 82 } 83 84 private: onGetSize()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 98 DEF_BENCH(return new ImageCycle(5, 10)); 99