Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "Benchmark.h" |
| 9 | #if SK_SUPPORT_GPU |
| 10 | |
| 11 | #include "GrContextOptions.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkImage.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkSurface.h" |
| 16 | |
| 17 | class MultitextureImages : public Benchmark { |
| 18 | public: |
| 19 | MultitextureImages(int imageSize, int dstRectSize, bool disableMultitexturing) |
| 20 | : fImageSize(imageSize) |
| 21 | , fDstRectSize(dstRectSize) |
| 22 | , fDisableMultitexturing(disableMultitexturing) { |
| 23 | fName.appendf("multitexture_images_%dx%d_image_%dx%d_rect", imageSize, imageSize, |
| 24 | dstRectSize, dstRectSize); |
| 25 | if (disableMultitexturing) { |
| 26 | fName.append("_disable_multitexturing"); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; } |
| 31 | |
| 32 | protected: |
| 33 | const char* onGetName() override { return fName.c_str(); } |
| 34 | |
| 35 | void onPerCanvasPreDraw(SkCanvas* canvas) override { |
| 36 | auto ii = SkImageInfo::Make(fImageSize, fImageSize, kRGBA_8888_SkColorType, |
| 37 | kPremul_SkAlphaType, nullptr); |
| 38 | SkRandom random; |
| 39 | for (int i = 0; i < kNumImages; ++i) { |
| 40 | auto surf = canvas->makeSurface(ii); |
| 41 | SkColor color = random.nextU(); |
| 42 | surf->getCanvas()->clear(color); |
| 43 | SkPaint paint; |
| 44 | paint.setColor(~color); |
| 45 | paint.setBlendMode(SkBlendMode::kSrc); |
| 46 | surf->getCanvas()->drawRect(SkRect::MakeLTRB(3, 3, fImageSize - 3, fImageSize - 3), |
| 47 | paint); |
| 48 | fImages[i] = surf->makeImageSnapshot(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 53 | for (int i = 0; i < kNumImages; ++i) { |
| 54 | fImages[i].reset(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void onDraw(int loops, SkCanvas* canvas) override { |
| 59 | SkRect rect = SkRect::MakeWH(fDstRectSize, fDstRectSize); |
| 60 | SkPaint paint; |
| 61 | paint.setAlpha(0x40); |
| 62 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 63 | for (int i = 0; i < loops; i++) { |
| 64 | for (int j = 0; j < kNumImages; ++j) { |
| 65 | SkVector translate = this->translation(i * kNumImages + j); |
| 66 | canvas->drawImageRect(fImages[j].get(), rect.makeOffset(translate.fX, translate.fY), |
| 67 | &paint); |
| 68 | } |
| 69 | // Prevent any batching except without multitexturing since we're trying to measure |
| 70 | // drawing distinct images and just repeating images here to increase the workload for |
| 71 | // timing reasons. |
| 72 | canvas->flush(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void modifyGrContextOptions(GrContextOptions* options) override { |
| 77 | options->fDisableImageMultitexturing = fDisableMultitexturing; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | SkIPoint onGetSize() override { |
| 82 | // The rows and columns are spaced by kTranslate, but the images may overlap if they are |
| 83 | // larger than kTranslate and extend beyond the last row/column. |
| 84 | return SkIPoint::Make(kTranslate * (kNumColumns - 1) + fDstRectSize, |
| 85 | kTranslate * (kNumRows - 1) + fDstRectSize); |
| 86 | } |
| 87 | |
| 88 | SkVector translation(int i) const { |
| 89 | SkVector offset; |
| 90 | offset.fX = i % kNumColumns * kTranslate; |
| 91 | offset.fY = (i / kNumColumns) % kNumRows * kTranslate; |
| 92 | return offset; |
| 93 | } |
| 94 | |
| 95 | static const int kTranslate = 200; |
| 96 | static const int kNumColumns = 5; |
| 97 | static const int kNumRows = 5; |
| 98 | static const int kNumImages = 8; |
| 99 | |
| 100 | sk_sp<SkImage> fImages[kNumImages]; |
| 101 | SkString fName; |
| 102 | int fImageSize; |
| 103 | int fDstRectSize; |
| 104 | bool fDisableMultitexturing; |
| 105 | |
| 106 | typedef Benchmark INHERITED; |
| 107 | }; |
| 108 | |
| 109 | DEF_BENCH(return new MultitextureImages(128, 32, false)); |
| 110 | DEF_BENCH(return new MultitextureImages(128, 32, true)); |
| 111 | DEF_BENCH(return new MultitextureImages(128, 128, false)); |
| 112 | DEF_BENCH(return new MultitextureImages(128, 128, true)); |
| 113 | DEF_BENCH(return new MultitextureImages(128, 256, false)); |
| 114 | DEF_BENCH(return new MultitextureImages(128, 256, true)); |
| 115 | |
| 116 | DEF_BENCH(return new MultitextureImages(512, 32, false)); |
| 117 | DEF_BENCH(return new MultitextureImages(512, 32, true)); |
| 118 | DEF_BENCH(return new MultitextureImages(512, 128, false)); |
| 119 | DEF_BENCH(return new MultitextureImages(512, 128, true)); |
| 120 | DEF_BENCH(return new MultitextureImages(512, 256, false)); |
| 121 | DEF_BENCH(return new MultitextureImages(512, 256, true)); |
| 122 | DEF_BENCH(return new MultitextureImages(512, 512, false)); |
| 123 | DEF_BENCH(return new MultitextureImages(512, 512, true)); |
| 124 | |
| 125 | #endif |