1/*
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 "Benchmark.h"
9#include "SkBlurMask.h"
10#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkShader.h"
14#include "SkString.h"
15
16class BitmapScaleBench: public Benchmark {
17    int         fLoopCount;
18    int         fInputSize;
19    int         fOutputSize;
20    SkString    fName;
21
22public:
23    BitmapScaleBench( int is, int os)  {
24        fInputSize = is;
25        fOutputSize = os;
26
27        fLoopCount = 20;
28    }
29
30protected:
31
32    SkBitmap fInputBitmap, fOutputBitmap;
33    SkMatrix fMatrix;
34
35    virtual const char* onGetName() {
36        return fName.c_str();
37    }
38
39    int inputSize() const {
40        return fInputSize;
41    }
42
43    int outputSize() const {
44        return fOutputSize;
45    }
46
47    float scale() const {
48        return float(outputSize())/inputSize();
49    }
50
51    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    }
58
59    virtual void onPreDraw() {
60        fInputBitmap.allocN32Pixels(fInputSize, fInputSize, true);
61        fInputBitmap.eraseColor(SK_ColorWHITE);
62
63        fOutputBitmap.allocN32Pixels(fOutputSize, fOutputSize, true);
64
65        fMatrix.setScale( scale(), scale() );
66    }
67
68    virtual void onDraw(const int loops, SkCanvas*) {
69        SkPaint paint;
70        this->setupPaint(&paint);
71
72        preBenchSetup();
73
74        for (int i = 0; i < loops; i++) {
75            doScaleImage();
76        }
77    }
78
79    virtual void doScaleImage() = 0;
80    virtual void preBenchSetup() {}
81private:
82    typedef Benchmark INHERITED;
83};
84
85class BitmapFilterScaleBench: public BitmapScaleBench {
86 public:
87    BitmapFilterScaleBench( int is, int os) : INHERITED(is, os) {
88        setName( "filter" );
89    }
90protected:
91    virtual void doScaleImage() SK_OVERRIDE {
92        SkCanvas canvas( fOutputBitmap );
93        SkPaint paint;
94
95        paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
96        fInputBitmap.notifyPixelsChanged();
97        canvas.drawBitmapMatrix( fInputBitmap, fMatrix, &paint );
98    }
99private:
100    typedef BitmapScaleBench INHERITED;
101};
102
103DEF_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);)
112