blob: f71e10ff6a0ac46ca12e76a077f909d1ed145b94 [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
humper@google.comb0889472013-07-09 21:37:14 +000035 virtual const char* onGetName() {
36 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
humper@google.comb0889472013-07-09 21:37:14 +000059 virtual void onPreDraw() {
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
commit-bot@chromium.org33614712013-12-03 18:17:16 +000068 virtual void onDraw(const int loops, SkCanvas*) {
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);)