blob: 0a1336233613834269480a63748621db80a55f88 [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 */
Hal Canaryc640d0d2018-06-13 09:59:02 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkPaint.h"
13#include "include/core/SkString.h"
14#include "include/private/SkTo.h"
15#include "include/utils/SkRandom.h"
reed@google.com3d407a12012-10-12 14:42:38 +000016
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000017static void draw_into_bitmap(const SkBitmap& bm) {
reed@google.com3d407a12012-10-12 14:42:38 +000018 const int w = bm.width();
19 const int h = bm.height();
20
21 SkCanvas canvas(bm);
22 SkPaint p;
23 p.setAntiAlias(true);
24 p.setColor(SK_ColorRED);
25 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
26 SkIntToScalar(SkMin32(w, h))*3/8, p);
27
28 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -040029 r.setWH(SkIntToScalar(w), SkIntToScalar(h));
reed@google.com3d407a12012-10-12 14:42:38 +000030 p.setStyle(SkPaint::kStroke_Style);
31 p.setStrokeWidth(SkIntToScalar(4));
32 p.setColor(SK_ColorBLUE);
33 canvas.drawRect(r, p);
34}
35
36/* Variants for bitmaprect
37 src : entire bitmap, subset, fractional subset
38 dst : same size as src, diff size
39 paint : filter-p
40 */
41
tfarinaf168b862014-06-19 12:32:29 -070042class BitmapRectBench : public Benchmark {
reed@google.com44699382013-10-31 17:28:30 +000043 SkBitmap fBitmap;
44 bool fSlightMatrix;
45 uint8_t fAlpha;
reed93a12152015-03-16 10:08:34 -070046 SkFilterQuality fFilterQuality;
reed@google.com44699382013-10-31 17:28:30 +000047 SkString fName;
48 SkRect fSrcR, fDstR;
49
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000050 static const int kWidth = 128;
51 static const int kHeight = 128;
reed@google.com3d407a12012-10-12 14:42:38 +000052public:
reed93a12152015-03-16 10:08:34 -070053 BitmapRectBench(U8CPU alpha, SkFilterQuality filterQuality,
reed@google.com44699382013-10-31 17:28:30 +000054 bool slightMatrix) {
reed@google.comb8b92ea2012-10-16 15:57:13 +000055 fAlpha = SkToU8(alpha);
reed93a12152015-03-16 10:08:34 -070056 fFilterQuality = filterQuality;
reed@google.com93182312013-01-15 20:21:19 +000057 fSlightMatrix = slightMatrix;
reed@google.comb8b92ea2012-10-16 15:57:13 +000058
reed6c225732014-06-09 19:52:07 -070059 fBitmap.setInfo(SkImageInfo::MakeN32Premul(kWidth, kHeight));
reed@google.com3d407a12012-10-12 14:42:38 +000060 }
61
62protected:
mtklein36352bf2015-03-25 18:17:31 -070063 const char* onGetName() override {
reed@google.com93182312013-01-15 20:21:19 +000064 fName.printf("bitmaprect_%02X_%sfilter_%s",
reed@google.com44699382013-10-31 17:28:30 +000065 fAlpha,
reed93a12152015-03-16 10:08:34 -070066 kNone_SkFilterQuality == fFilterQuality ? "no" : "",
reed@google.com93182312013-01-15 20:21:19 +000067 fSlightMatrix ? "trans" : "identity");
reed@google.com3d407a12012-10-12 14:42:38 +000068 return fName.c_str();
69 }
70
joshualitt8a6697a2015-09-30 12:11:07 -070071 void onDelayedSetup() override {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000072 fBitmap.allocPixels();
reed@google.com383a6972013-10-21 14:00:07 +000073 fBitmap.setAlphaType(kOpaque_SkAlphaType);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000074 fBitmap.eraseColor(SK_ColorBLACK);
75 draw_into_bitmap(fBitmap);
76
Mike Reed92b33352019-08-24 19:39:13 -040077 fSrcR.setWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
78 fDstR.setWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000079
80 if (fSlightMatrix) {
81 // want fractional translate
82 fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7);
83 // want enough to create a scale matrix, but not enough to scare
84 // off our sniffer which tries to see if the matrix is "effectively"
85 // translate-only.
86 fDstR.fRight += SK_Scalar1 / (kWidth * 60);
87 }
88 }
89
90
mtkleina1ebeb22015-10-01 09:43:39 -070091 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000092 SkRandom rand;
reed@google.com3d407a12012-10-12 14:42:38 +000093
94 SkPaint paint;
95 this->setupPaint(&paint);
reed93a12152015-03-16 10:08:34 -070096 paint.setFilterQuality(fFilterQuality);
reed@google.comb8b92ea2012-10-16 15:57:13 +000097 paint.setAlpha(fAlpha);
reed@google.com3d407a12012-10-12 14:42:38 +000098
commit-bot@chromium.org33614712013-12-03 18:17:16 +000099 for (int i = 0; i < loops; i++) {
reede47829b2015-08-06 10:02:53 -0700100 canvas->drawBitmapRect(fBitmap, fSrcR, fDstR, &paint,
reed84984ef2015-07-17 07:09:43 -0700101 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com3d407a12012-10-12 14:42:38 +0000102 }
103 }
104
105private:
tfarinaf168b862014-06-19 12:32:29 -0700106 typedef Benchmark INHERITED;
reed@google.com3d407a12012-10-12 14:42:38 +0000107};
108
reed93a12152015-03-16 10:08:34 -0700109DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, false))
110DEF_BENCH(return new BitmapRectBench(0x80, kNone_SkFilterQuality, false))
111DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, false))
112DEF_BENCH(return new BitmapRectBench(0x80, kLow_SkFilterQuality, false))
reed@google.com93182312013-01-15 20:21:19 +0000113
reed93a12152015-03-16 10:08:34 -0700114DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, true))
115DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, true))