blob: d3a7574e04f817ce7d258d4713329d4396a8d722 [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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com3d407a12012-10-12 14:42:38 +00009#include "SkBitmap.h"
reed@google.com3d407a12012-10-12 14:42:38 +000010#include "SkCanvas.h"
11#include "SkColorPriv.h"
tfarinaf168b862014-06-19 12:32:29 -070012#include "SkPaint.h"
reed@google.com3d407a12012-10-12 14:42:38 +000013#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
tfarinaf168b862014-06-19 12:32:29 -070041class BitmapRectBench : public Benchmark {
reed@google.com44699382013-10-31 17:28:30 +000042 SkBitmap fBitmap;
43 bool fSlightMatrix;
44 uint8_t fAlpha;
reed93a12152015-03-16 10:08:34 -070045 SkFilterQuality fFilterQuality;
reed@google.com44699382013-10-31 17:28:30 +000046 SkString fName;
47 SkRect fSrcR, fDstR;
48
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000049 static const int kWidth = 128;
50 static const int kHeight = 128;
reed@google.com3d407a12012-10-12 14:42:38 +000051public:
reed93a12152015-03-16 10:08:34 -070052 BitmapRectBench(U8CPU alpha, SkFilterQuality filterQuality,
reed@google.com44699382013-10-31 17:28:30 +000053 bool slightMatrix) {
reed@google.comb8b92ea2012-10-16 15:57:13 +000054 fAlpha = SkToU8(alpha);
reed93a12152015-03-16 10:08:34 -070055 fFilterQuality = filterQuality;
reed@google.com93182312013-01-15 20:21:19 +000056 fSlightMatrix = slightMatrix;
reed@google.comb8b92ea2012-10-16 15:57:13 +000057
reed6c225732014-06-09 19:52:07 -070058 fBitmap.setInfo(SkImageInfo::MakeN32Premul(kWidth, kHeight));
reed@google.com3d407a12012-10-12 14:42:38 +000059 }
60
61protected:
mtklein36352bf2015-03-25 18:17:31 -070062 const char* onGetName() override {
reed@google.com93182312013-01-15 20:21:19 +000063 fName.printf("bitmaprect_%02X_%sfilter_%s",
reed@google.com44699382013-10-31 17:28:30 +000064 fAlpha,
reed93a12152015-03-16 10:08:34 -070065 kNone_SkFilterQuality == fFilterQuality ? "no" : "",
reed@google.com93182312013-01-15 20:21:19 +000066 fSlightMatrix ? "trans" : "identity");
reed@google.com3d407a12012-10-12 14:42:38 +000067 return fName.c_str();
68 }
69
mtklein36352bf2015-03-25 18:17:31 -070070 void onPreDraw() override {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000071 fBitmap.allocPixels();
reed@google.com383a6972013-10-21 14:00:07 +000072 fBitmap.setAlphaType(kOpaque_SkAlphaType);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000073 fBitmap.eraseColor(SK_ColorBLACK);
74 draw_into_bitmap(fBitmap);
75
76 fSrcR.iset(0, 0, kWidth, kHeight);
77 fDstR.iset(0, 0, kWidth, kHeight);
78
79 if (fSlightMatrix) {
80 // want fractional translate
81 fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7);
82 // want enough to create a scale matrix, but not enough to scare
83 // off our sniffer which tries to see if the matrix is "effectively"
84 // translate-only.
85 fDstR.fRight += SK_Scalar1 / (kWidth * 60);
86 }
87 }
88
89
mtklein36352bf2015-03-25 18:17:31 -070090 void onDraw(const int loops, SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000091 SkRandom rand;
reed@google.com3d407a12012-10-12 14:42:38 +000092
93 SkPaint paint;
94 this->setupPaint(&paint);
reed93a12152015-03-16 10:08:34 -070095 paint.setFilterQuality(fFilterQuality);
reed@google.comb8b92ea2012-10-16 15:57:13 +000096 paint.setAlpha(fAlpha);
reed@google.com3d407a12012-10-12 14:42:38 +000097
commit-bot@chromium.org33614712013-12-03 18:17:16 +000098 for (int i = 0; i < loops; i++) {
reed@google.com3d407a12012-10-12 14:42:38 +000099 canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint);
100 }
101 }
102
103private:
tfarinaf168b862014-06-19 12:32:29 -0700104 typedef Benchmark INHERITED;
reed@google.com3d407a12012-10-12 14:42:38 +0000105};
106
reed93a12152015-03-16 10:08:34 -0700107DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, false))
108DEF_BENCH(return new BitmapRectBench(0x80, kNone_SkFilterQuality, false))
109DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, false))
110DEF_BENCH(return new BitmapRectBench(0x80, kLow_SkFilterQuality, false))
reed@google.com93182312013-01-15 20:21:19 +0000111
reed93a12152015-03-16 10:08:34 -0700112DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, true))
113DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, true))