blob: e309d5162cd3154919f234866e44d38f9ed96b53 [file] [log] [blame]
humper@google.comb0889472013-07-09 21:37:14 +00001/*
2 * Copyright 2013 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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
9#include "SkBlurMask.h"
humper@google.comb0889472013-07-09 21:37:14 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkShader.h"
14#include "SkString.h"
humper@google.comb0889472013-07-09 21:37:14 +000015
tfarinaf168b862014-06-19 12:32:29 -070016class BitmapScaleBench: public Benchmark {
humper@google.comb0889472013-07-09 21:37:14 +000017 int fLoopCount;
18 int fInputSize;
19 int fOutputSize;
20 SkString fName;
21
22public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000023 BitmapScaleBench( int is, int os) {
humper@google.comb0889472013-07-09 21:37:14 +000024 fInputSize = is;
25 fOutputSize = os;
26
humper@google.com523521c2013-07-11 20:28:30 +000027 fLoopCount = 20;
humper@google.comb0889472013-07-09 21:37:14 +000028 }
29
30protected:
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000031
humper@google.comb0889472013-07-09 21:37:14 +000032 SkBitmap fInputBitmap, fOutputBitmap;
33 SkMatrix fMatrix;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000034
mtkleinf0599002015-07-13 06:18:39 -070035 const char* onGetName() override {
humper@google.comb0889472013-07-09 21:37:14 +000036 return fName.c_str();
37 }
38
39 int inputSize() const {
40 return fInputSize;
41 }
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000042
humper@google.comb0889472013-07-09 21:37:14 +000043 int outputSize() const {
44 return fOutputSize;
45 }
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000046
humper@google.comb0889472013-07-09 21:37:14 +000047 float scale() const {
48 return float(outputSize())/inputSize();
49 }
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000050
mtklein36352bf2015-03-25 18:17:31 -070051 SkIPoint onGetSize() override {
humper@google.comb0889472013-07-09 21:37:14 +000052 return SkIPoint::Make( fOutputSize, fOutputSize );
53 }
54
55 void setName(const char * name) {
56 fName.printf( "bitmap_scale_%s_%d_%d", name, fInputSize, fOutputSize );
57 }
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000058
joshualitt8a6697a2015-09-30 12:11:07 -070059 void onDelayedSetup() override {
reed6c225732014-06-09 19:52:07 -070060 fInputBitmap.allocN32Pixels(fInputSize, fInputSize, true);
humper@google.comb0889472013-07-09 21:37:14 +000061 fInputBitmap.eraseColor(SK_ColorWHITE);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000062
reed6c225732014-06-09 19:52:07 -070063 fOutputBitmap.allocN32Pixels(fOutputSize, fOutputSize, true);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000064
humper@google.comb0889472013-07-09 21:37:14 +000065 fMatrix.setScale( scale(), scale() );
66 }
67
mtkleina1ebeb22015-10-01 09:43:39 -070068 void onDraw(int loops, SkCanvas*) override {
humper@google.comb0889472013-07-09 21:37:14 +000069 SkPaint paint;
70 this->setupPaint(&paint);
71
72 preBenchSetup();
73
commit-bot@chromium.org33614712013-12-03 18:17:16 +000074 for (int i = 0; i < loops; i++) {
humper@google.comb0889472013-07-09 21:37:14 +000075 doScaleImage();
76 }
77 }
78
79 virtual void doScaleImage() = 0;
80 virtual void preBenchSetup() {}
81private:
tfarinaf168b862014-06-19 12:32:29 -070082 typedef Benchmark INHERITED;
humper@google.comb0889472013-07-09 21:37:14 +000083};
84
85class BitmapFilterScaleBench: public BitmapScaleBench {
86 public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000087 BitmapFilterScaleBench( int is, int os) : INHERITED(is, os) {
humper@google.comb0889472013-07-09 21:37:14 +000088 setName( "filter" );
89 }
90protected:
mtklein36352bf2015-03-25 18:17:31 -070091 void doScaleImage() override {
humper@google.comb0889472013-07-09 21:37:14 +000092 SkCanvas canvas( fOutputBitmap );
93 SkPaint paint;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000094
reed93a12152015-03-16 10:08:34 -070095 paint.setFilterQuality(kHigh_SkFilterQuality);
commit-bot@chromium.org1b28f6f2014-03-06 17:16:37 +000096 fInputBitmap.notifyPixelsChanged();
Florin Malitac54d8db2014-12-10 12:02:16 -050097 canvas.concat(fMatrix);
98 canvas.drawBitmap(fInputBitmap, 0, 0, &paint );
humper@google.comb0889472013-07-09 21:37:14 +000099 }
100private:
101 typedef BitmapScaleBench INHERITED;
102};
103
mtklein@google.com410e6e82013-09-13 19:52:27 +0000104DEF_BENCH(return new BitmapFilterScaleBench(10, 90);)
105DEF_BENCH(return new BitmapFilterScaleBench(30, 90);)
106DEF_BENCH(return new BitmapFilterScaleBench(80, 90);)
107DEF_BENCH(return new BitmapFilterScaleBench(90, 90);)
108DEF_BENCH(return new BitmapFilterScaleBench(90, 80);)
109DEF_BENCH(return new BitmapFilterScaleBench(90, 30);)
110DEF_BENCH(return new BitmapFilterScaleBench(90, 10);)
111DEF_BENCH(return new BitmapFilterScaleBench(256, 64);)
112DEF_BENCH(return new BitmapFilterScaleBench(64, 256);)
reeda36c7102016-01-14 10:10:56 -0800113
114///////////////////////////////////////////////////////////////////////////////////////////////
115
116#include "SkBitmapScaler.h"
117
118class PixmapScalerBench: public Benchmark {
119 SkBitmapScaler::ResizeMethod fMethod;
120 SkString fName;
121 SkBitmap fSrc, fDst;
122
123public:
124 PixmapScalerBench(SkBitmapScaler::ResizeMethod method, const char suffix[]) : fMethod(method) {
125 fName.printf("pixmapscaler_%s", suffix);
126 }
mtkleinbc161d62016-01-19 12:42:49 -0800127
reeda36c7102016-01-14 10:10:56 -0800128protected:
129 const char* onGetName() override {
130 return fName.c_str();
131 }
mtkleinbc161d62016-01-19 12:42:49 -0800132
reeda36c7102016-01-14 10:10:56 -0800133 SkIPoint onGetSize() override { return{ 100, 100 }; }
134
135 bool isSuitableFor(Backend backend) override {
136 return backend == kNonRendering_Backend;
137 }
138
139 void onDelayedSetup() override {
140 fSrc.allocN32Pixels(640, 480);
mtkleinbc161d62016-01-19 12:42:49 -0800141 fSrc.eraseColor(SK_ColorWHITE);
reeda36c7102016-01-14 10:10:56 -0800142 fDst.allocN32Pixels(300, 250);
143 }
mtkleinbc161d62016-01-19 12:42:49 -0800144
reeda36c7102016-01-14 10:10:56 -0800145 void onDraw(int loops, SkCanvas*) override {
146 SkPixmap src, dst;
147 fSrc.peekPixels(&src);
148 fDst.peekPixels(&dst);
149 for (int i = 0; i < loops * 16; i++) {
150 SkBitmapScaler::Resize(dst, src, fMethod);
151 }
152 }
153
154private:
155 typedef Benchmark INHERITED;
156};
157DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_LANCZOS3, "lanczos"); )
158DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_MITCHELL, "mitchell"); )
159DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_HAMMING, "hamming"); )
160DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_TRIANGLE, "triangle"); )
161DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_BOX, "box"); )