blob: 789a725e11e499737edbeddd7f3358b8b33ec7a6 [file] [log] [blame]
reed@google.com3d407a12012-10-12 14:42:38 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
reed@google.com3d407a12012-10-12 14:42:38 +00008#include "SkBitmap.h"
reed@google.com3d407a12012-10-12 14:42:38 +00009#include "SkCanvas.h"
10#include "SkColorPriv.h"
tfarinaf168b862014-06-19 12:32:29 -070011#include "SkPaint.h"
reed@google.com3d407a12012-10-12 14:42:38 +000012#include "SkRandom.h"
13#include "SkString.h"
14
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000015static void draw_into_bitmap(const SkBitmap& bm) {
reed@google.com3d407a12012-10-12 14:42:38 +000016 const int w = bm.width();
17 const int h = bm.height();
18
19 SkCanvas canvas(bm);
20 SkPaint p;
21 p.setAntiAlias(true);
22 p.setColor(SK_ColorRED);
23 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
24 SkIntToScalar(SkMin32(w, h))*3/8, p);
25
26 SkRect r;
27 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
28 p.setStyle(SkPaint::kStroke_Style);
29 p.setStrokeWidth(SkIntToScalar(4));
30 p.setColor(SK_ColorBLUE);
31 canvas.drawRect(r, p);
32}
33
34/* Variants for bitmaprect
35 src : entire bitmap, subset, fractional subset
36 dst : same size as src, diff size
37 paint : filter-p
38 */
39
tfarinaf168b862014-06-19 12:32:29 -070040class BitmapRectBench : public Benchmark {
reed@google.com44699382013-10-31 17:28:30 +000041 SkBitmap fBitmap;
42 bool fSlightMatrix;
43 uint8_t fAlpha;
reed93a12152015-03-16 10:08:34 -070044 SkFilterQuality fFilterQuality;
reed@google.com44699382013-10-31 17:28:30 +000045 SkString fName;
46 SkRect fSrcR, fDstR;
47
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:
reed93a12152015-03-16 10:08:34 -070051 BitmapRectBench(U8CPU alpha, SkFilterQuality filterQuality,
reed@google.com44699382013-10-31 17:28:30 +000052 bool slightMatrix) {
reed@google.comb8b92ea2012-10-16 15:57:13 +000053 fAlpha = SkToU8(alpha);
reed93a12152015-03-16 10:08:34 -070054 fFilterQuality = filterQuality;
reed@google.com93182312013-01-15 20:21:19 +000055 fSlightMatrix = slightMatrix;
reed@google.comb8b92ea2012-10-16 15:57:13 +000056
reed6c225732014-06-09 19:52:07 -070057 fBitmap.setInfo(SkImageInfo::MakeN32Premul(kWidth, kHeight));
reed@google.com3d407a12012-10-12 14:42:38 +000058 }
59
60protected:
mtklein36352bf2015-03-25 18:17:31 -070061 const char* onGetName() override {
reed@google.com93182312013-01-15 20:21:19 +000062 fName.printf("bitmaprect_%02X_%sfilter_%s",
reed@google.com44699382013-10-31 17:28:30 +000063 fAlpha,
reed93a12152015-03-16 10:08:34 -070064 kNone_SkFilterQuality == fFilterQuality ? "no" : "",
reed@google.com93182312013-01-15 20:21:19 +000065 fSlightMatrix ? "trans" : "identity");
reed@google.com3d407a12012-10-12 14:42:38 +000066 return fName.c_str();
67 }
68
joshualitt8a6697a2015-09-30 12:11:07 -070069 void onDelayedSetup() override {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000070 fBitmap.allocPixels();
reed@google.com383a6972013-10-21 14:00:07 +000071 fBitmap.setAlphaType(kOpaque_SkAlphaType);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000072 fBitmap.eraseColor(SK_ColorBLACK);
73 draw_into_bitmap(fBitmap);
74
75 fSrcR.iset(0, 0, kWidth, kHeight);
76 fDstR.iset(0, 0, kWidth, kHeight);
77
78 if (fSlightMatrix) {
79 // want fractional translate
80 fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7);
81 // want enough to create a scale matrix, but not enough to scare
82 // off our sniffer which tries to see if the matrix is "effectively"
83 // translate-only.
84 fDstR.fRight += SK_Scalar1 / (kWidth * 60);
85 }
86 }
87
88
mtkleina1ebeb22015-10-01 09:43:39 -070089 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000090 SkRandom rand;
reed@google.com3d407a12012-10-12 14:42:38 +000091
92 SkPaint paint;
93 this->setupPaint(&paint);
reed93a12152015-03-16 10:08:34 -070094 paint.setFilterQuality(fFilterQuality);
reed@google.comb8b92ea2012-10-16 15:57:13 +000095 paint.setAlpha(fAlpha);
reed@google.com3d407a12012-10-12 14:42:38 +000096
commit-bot@chromium.org33614712013-12-03 18:17:16 +000097 for (int i = 0; i < loops; i++) {
reede47829b2015-08-06 10:02:53 -070098 canvas->drawBitmapRect(fBitmap, fSrcR, fDstR, &paint,
reed84984ef2015-07-17 07:09:43 -070099 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com3d407a12012-10-12 14:42:38 +0000100 }
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))