blob: 926111d12db5936621372be0d62675edba5fd1ea [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
8#include "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkShader.h"
13#include "SkString.h"
14#include "SkBlurMask.h"
15
16class BitmapScaleBench: public SkBenchmark {
17 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() {
reed@google.com383a6972013-10-21 14:00:07 +000060 fInputBitmap.setConfig(SkBitmap::kARGB_8888_Config,
61 fInputSize, fInputSize, 0, kOpaque_SkAlphaType);
humper@google.comb0889472013-07-09 21:37:14 +000062 fInputBitmap.allocPixels();
63 fInputBitmap.eraseColor(SK_ColorWHITE);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000064
reed@google.com383a6972013-10-21 14:00:07 +000065 fOutputBitmap.setConfig(SkBitmap::kARGB_8888_Config,
66 fOutputSize, fOutputSize, 0, kOpaque_SkAlphaType);
humper@google.comb0889472013-07-09 21:37:14 +000067 fOutputBitmap.allocPixels();
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000068
humper@google.comb0889472013-07-09 21:37:14 +000069 fMatrix.setScale( scale(), scale() );
70 }
71
commit-bot@chromium.org33614712013-12-03 18:17:16 +000072 virtual void onDraw(const int loops, SkCanvas*) {
humper@google.comb0889472013-07-09 21:37:14 +000073 SkPaint paint;
74 this->setupPaint(&paint);
75
76 preBenchSetup();
77
commit-bot@chromium.org33614712013-12-03 18:17:16 +000078 for (int i = 0; i < loops; i++) {
humper@google.comb0889472013-07-09 21:37:14 +000079 doScaleImage();
80 }
81 }
82
83 virtual void doScaleImage() = 0;
84 virtual void preBenchSetup() {}
85private:
86 typedef SkBenchmark INHERITED;
87};
88
89class BitmapFilterScaleBench: public BitmapScaleBench {
90 public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000091 BitmapFilterScaleBench( int is, int os) : INHERITED(is, os) {
humper@google.comb0889472013-07-09 21:37:14 +000092 setName( "filter" );
93 }
94protected:
95 virtual void doScaleImage() SK_OVERRIDE {
96 SkCanvas canvas( fOutputBitmap );
97 SkPaint paint;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000098
reed@google.com9cfc83c2013-07-22 17:18:18 +000099 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
humper@google.comb0889472013-07-09 21:37:14 +0000100 canvas.drawBitmapMatrix( fInputBitmap, fMatrix, &paint );
101 }
102private:
103 typedef BitmapScaleBench INHERITED;
104};
105
mtklein@google.com410e6e82013-09-13 19:52:27 +0000106DEF_BENCH(return new BitmapFilterScaleBench(10, 90);)
107DEF_BENCH(return new BitmapFilterScaleBench(30, 90);)
108DEF_BENCH(return new BitmapFilterScaleBench(80, 90);)
109DEF_BENCH(return new BitmapFilterScaleBench(90, 90);)
110DEF_BENCH(return new BitmapFilterScaleBench(90, 80);)
111DEF_BENCH(return new BitmapFilterScaleBench(90, 30);)
112DEF_BENCH(return new BitmapFilterScaleBench(90, 10);)
113DEF_BENCH(return new BitmapFilterScaleBench(256, 64);)
114DEF_BENCH(return new BitmapFilterScaleBench(64, 256);)