BitmapRectBench.cpp revision c289743864e2ab926a95e617a5cd1d29b26d1825
1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SkBenchmark.h"
9#include "SkBitmap.h"
10#include "SkPaint.h"
11#include "SkCanvas.h"
12#include "SkColorPriv.h"
13#include "SkRandom.h"
14#include "SkString.h"
15
16static void draw_into_bitmap(const SkBitmap& bm) {
17    const int w = bm.width();
18    const int h = bm.height();
19
20    SkCanvas canvas(bm);
21    SkPaint p;
22    p.setAntiAlias(true);
23    p.setColor(SK_ColorRED);
24    canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
25                      SkIntToScalar(SkMin32(w, h))*3/8, p);
26
27    SkRect r;
28    r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
29    p.setStyle(SkPaint::kStroke_Style);
30    p.setStrokeWidth(SkIntToScalar(4));
31    p.setColor(SK_ColorBLUE);
32    canvas.drawRect(r, p);
33}
34
35/*  Variants for bitmaprect
36    src : entire bitmap, subset, fractional subset
37    dst : same size as src, diff size
38    paint : filter-p
39 */
40
41class BitmapRectBench : public SkBenchmark {
42    SkBitmap    fBitmap;
43    bool        fDoFilter;
44    bool        fSlightMatrix;
45    uint8_t     fAlpha;
46    SkString    fName;
47    SkRect      fSrcR, fDstR;
48    static const int kWidth = 128;
49    static const int kHeight = 128;
50public:
51    BitmapRectBench(void* param, U8CPU alpha, bool doFilter, bool slightMatrix) : INHERITED(param) {
52        fAlpha = SkToU8(alpha);
53        fDoFilter = doFilter;
54        fSlightMatrix = slightMatrix;
55
56        fBitmap.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight);
57    }
58
59protected:
60    virtual const char* onGetName() SK_OVERRIDE {
61        fName.printf("bitmaprect_%02X_%sfilter_%s",
62                     fAlpha, fDoFilter ? "" : "no",
63                     fSlightMatrix ? "trans" : "identity");
64        return fName.c_str();
65    }
66
67    virtual void onPreDraw() SK_OVERRIDE {
68        fBitmap.allocPixels();
69        fBitmap.setIsOpaque(true);
70        fBitmap.eraseColor(SK_ColorBLACK);
71        draw_into_bitmap(fBitmap);
72
73        fSrcR.iset(0, 0, kWidth, kHeight);
74        fDstR.iset(0, 0, kWidth, kHeight);
75
76        if (fSlightMatrix) {
77            // want fractional translate
78            fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7);
79            // want enough to create a scale matrix, but not enough to scare
80            // off our sniffer which tries to see if the matrix is "effectively"
81            // translate-only.
82            fDstR.fRight += SK_Scalar1 / (kWidth * 60);
83        }
84    }
85
86
87    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
88        SkRandom rand;
89
90        SkPaint paint;
91        this->setupPaint(&paint);
92        paint.setFilterBitmap(fDoFilter);
93        paint.setAlpha(fAlpha);
94
95        for (int i = 0; i < this->getLoops(); i++) {
96            canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint);
97        }
98    }
99
100private:
101    typedef SkBenchmark INHERITED;
102};
103
104DEF_BENCH(return new BitmapRectBench(p, 0xFF, false, false))
105DEF_BENCH(return new BitmapRectBench(p, 0x80, false, false))
106DEF_BENCH(return new BitmapRectBench(p, 0xFF, true, false))
107DEF_BENCH(return new BitmapRectBench(p, 0x80, true, false))
108
109DEF_BENCH(return new BitmapRectBench(p, 0xFF, false, true))
110DEF_BENCH(return new BitmapRectBench(p, 0xFF, true, true))
111