blob: 104cb24bd90e51c6d00dd28ece3a1f29e7f69380 [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
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000016static void draw_into_bitmap(const SkBitmap& bm) {
reed@google.com3d407a12012-10-12 14:42:38 +000017 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.com93182312013-01-15 20:21:19 +000044 bool fSlightMatrix;
reed@google.comb8b92ea2012-10-16 15:57:13 +000045 uint8_t fAlpha;
reed@google.com3d407a12012-10-12 14:42:38 +000046 SkString fName;
47 SkRect fSrcR, fDstR;
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000048 static const int kWidth = 128;
49 static const int kHeight = 128;
reed@google.com3d407a12012-10-12 14:42:38 +000050public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000051 BitmapRectBench(U8CPU alpha, bool doFilter, bool slightMatrix) {
reed@google.comb8b92ea2012-10-16 15:57:13 +000052 fAlpha = SkToU8(alpha);
53 fDoFilter = doFilter;
reed@google.com93182312013-01-15 20:21:19 +000054 fSlightMatrix = slightMatrix;
reed@google.comb8b92ea2012-10-16 15:57:13 +000055
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000056 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight);
reed@google.com3d407a12012-10-12 14:42:38 +000057 }
58
59protected:
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000060 virtual const char* onGetName() SK_OVERRIDE {
reed@google.com93182312013-01-15 20:21:19 +000061 fName.printf("bitmaprect_%02X_%sfilter_%s",
62 fAlpha, fDoFilter ? "" : "no",
63 fSlightMatrix ? "trans" : "identity");
reed@google.com3d407a12012-10-12 14:42:38 +000064 return fName.c_str();
65 }
66
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000067 virtual void onPreDraw() SK_OVERRIDE {
68 fBitmap.allocPixels();
reed@google.com383a6972013-10-21 14:00:07 +000069 fBitmap.setAlphaType(kOpaque_SkAlphaType);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000070 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 {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000088 SkRandom rand;
reed@google.com3d407a12012-10-12 14:42:38 +000089
90 SkPaint paint;
91 this->setupPaint(&paint);
92 paint.setFilterBitmap(fDoFilter);
reed@google.comb8b92ea2012-10-16 15:57:13 +000093 paint.setAlpha(fAlpha);
reed@google.com3d407a12012-10-12 14:42:38 +000094
mtklein@google.comc2897432013-09-10 19:23:38 +000095 for (int i = 0; i < this->getLoops(); i++) {
reed@google.com3d407a12012-10-12 14:42:38 +000096 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint);
97 }
98 }
99
100private:
101 typedef SkBenchmark INHERITED;
102};
103
mtklein@google.com410e6e82013-09-13 19:52:27 +0000104DEF_BENCH(return new BitmapRectBench(0xFF, false, false))
105DEF_BENCH(return new BitmapRectBench(0x80, false, false))
106DEF_BENCH(return new BitmapRectBench(0xFF, true, false))
107DEF_BENCH(return new BitmapRectBench(0x80, true, false))
reed@google.com93182312013-01-15 20:21:19 +0000108
mtklein@google.com410e6e82013-09-13 19:52:27 +0000109DEF_BENCH(return new BitmapRectBench(0xFF, false, true))
110DEF_BENCH(return new BitmapRectBench(0xFF, true, true))