blob: fad741aebc72d3cf6bda55088e8227a903b066ab [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
humper@google.comb0889472013-07-09 21:37:14 +000051 SkIPoint onGetSize() SK_OVERRIDE {
52 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:
91 virtual void doScaleImage() SK_OVERRIDE {
92 SkCanvas canvas( fOutputBitmap );
93 SkPaint paint;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000094
reed@google.com9cfc83c2013-07-22 17:18:18 +000095 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
commit-bot@chromium.org1b28f6f2014-03-06 17:16:37 +000096 fInputBitmap.notifyPixelsChanged();
humper@google.comb0889472013-07-09 21:37:14 +000097 canvas.drawBitmapMatrix( fInputBitmap, fMatrix, &paint );
98 }
99private:
100 typedef BitmapScaleBench INHERITED;
101};
102
mtklein@google.com410e6e82013-09-13 19:52:27 +0000103DEF_BENCH(return new BitmapFilterScaleBench(10, 90);)
104DEF_BENCH(return new BitmapFilterScaleBench(30, 90);)
105DEF_BENCH(return new BitmapFilterScaleBench(80, 90);)
106DEF_BENCH(return new BitmapFilterScaleBench(90, 90);)
107DEF_BENCH(return new BitmapFilterScaleBench(90, 80);)
108DEF_BENCH(return new BitmapFilterScaleBench(90, 30);)
109DEF_BENCH(return new BitmapFilterScaleBench(90, 10);)
110DEF_BENCH(return new BitmapFilterScaleBench(256, 64);)
111DEF_BENCH(return new BitmapFilterScaleBench(64, 256);)