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