blob: 79c6aaf5772a357f7cdd4e4eeedee50367bad313 [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;
44 SkString fName;
45 SkRect fSrcR, fDstR;
46 enum { N = SkBENCHLOOP(300) };
47public:
48 BitmapRectBench(void* param, bool doFilter) : INHERITED(param), fDoFilter(doFilter) {
49 const int w = 128;
50 const int h = 128;
51
52 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
53 fBitmap.allocPixels();
54 fBitmap.setIsOpaque(true);
55 fBitmap.eraseColor(SK_ColorBLACK);
56 drawIntoBitmap(fBitmap);
57
58 fSrcR.set(0, 0, w, h);
59 fDstR.set(0, 0, w, h);
60 }
61
62protected:
63 virtual const char* onGetName() {
64 fName.printf("bitmaprect_%sfilter", fDoFilter ? "" : "no");
65 return fName.c_str();
66 }
67
68 virtual void onDraw(SkCanvas* canvas) {
69 SkIPoint dim = this->getSize();
70 SkRandom rand;
71
72 SkPaint paint;
73 this->setupPaint(&paint);
74 paint.setFilterBitmap(fDoFilter);
75
76 for (int i = 0; i < N; i++) {
77 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint);
78 }
79 }
80
81private:
82 typedef SkBenchmark INHERITED;
83};
84
85static SkBenchmark* Fact0(void* p) { return new BitmapRectBench(p, false); }
86static SkBenchmark* Fact1(void* p) { return new BitmapRectBench(p, true); }
87
88static BenchRegistry gReg0(Fact0);
89static BenchRegistry gReg1(Fact1);