blob: f2f9e4c3958bff034cc5231caae929441a6b5399 [file] [log] [blame]
reed@google.com3d407a12012-10-12 14:42:38 +00001
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 drawIntoBitmap(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;
reed@google.comb8b92ea2012-10-16 15:57:13 +000044 uint8_t fAlpha;
reed@google.com3d407a12012-10-12 14:42:38 +000045 SkString fName;
46 SkRect fSrcR, fDstR;
47 enum { N = SkBENCHLOOP(300) };
48public:
reed@google.comb8b92ea2012-10-16 15:57:13 +000049 BitmapRectBench(void* param, U8CPU alpha, bool doFilter) : INHERITED(param) {
50 fAlpha = SkToU8(alpha);
51 fDoFilter = doFilter;
52
reed@google.com3d407a12012-10-12 14:42:38 +000053 const int w = 128;
54 const int h = 128;
55
56 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
57 fBitmap.allocPixels();
58 fBitmap.setIsOpaque(true);
59 fBitmap.eraseColor(SK_ColorBLACK);
60 drawIntoBitmap(fBitmap);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000061
robertphillips@google.com93f03322012-12-03 17:35:19 +000062 fSrcR.iset(0, 0, w, h);
63 fDstR.iset(0, 0, w, h);
reed@google.com3d407a12012-10-12 14:42:38 +000064 }
65
66protected:
67 virtual const char* onGetName() {
reed@google.comb8b92ea2012-10-16 15:57:13 +000068 fName.printf("bitmaprect_%02X_%sfilter", fAlpha, fDoFilter ? "" : "no");
reed@google.com3d407a12012-10-12 14:42:38 +000069 return fName.c_str();
70 }
71
72 virtual void onDraw(SkCanvas* canvas) {
reed@google.com3d407a12012-10-12 14:42:38 +000073 SkRandom rand;
74
75 SkPaint paint;
76 this->setupPaint(&paint);
77 paint.setFilterBitmap(fDoFilter);
reed@google.comb8b92ea2012-10-16 15:57:13 +000078 paint.setAlpha(fAlpha);
reed@google.com3d407a12012-10-12 14:42:38 +000079
80 for (int i = 0; i < N; i++) {
81 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint);
82 }
83 }
84
85private:
86 typedef SkBenchmark INHERITED;
87};
88
reed@google.comb8b92ea2012-10-16 15:57:13 +000089DEF_BENCH(return new BitmapRectBench(p, 0xFF, false))
90DEF_BENCH(return new BitmapRectBench(p, 0x80, false))
91DEF_BENCH(return new BitmapRectBench(p, 0xFF, true))
92DEF_BENCH(return new BitmapRectBench(p, 0x80, true))
reed@google.com3d407a12012-10-12 14:42:38 +000093