blob: 6403aa6de73116b3e2b5f3737acd451c01f4cf67 [file] [log] [blame]
sugoi@google.com580a1722013-04-23 14:20:45 +00001/*
2 * Copyright 2013 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 */
reed8c0c7b02014-06-27 05:49:53 -07007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
Florin Malitaab244f02017-05-03 19:16:58 +00009#include "SkBitmap.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000010#include "SkCanvas.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000011#include "SkMagnifierImageFilter.h"
12#include "SkRandom.h"
13
sugoi@google.comca425d92013-04-23 14:37:38 +000014#define FILTER_WIDTH_SMALL 32
15#define FILTER_HEIGHT_SMALL 32
16#define FILTER_WIDTH_LARGE 256
17#define FILTER_HEIGHT_LARGE 256
sugoi@google.com580a1722013-04-23 14:20:45 +000018
tfarinaf168b862014-06-19 12:32:29 -070019class MagnifierBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000020public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000021 MagnifierBench(bool small) :
22 fIsSmall(small), fInitialized(false) {
sugoi@google.com580a1722013-04-23 14:20:45 +000023 }
24
25protected:
mtklein36352bf2015-03-25 18:17:31 -070026 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000027 return fIsSmall ? "magnifier_small" : "magnifier_large";
28 }
29
joshualitt8a6697a2015-09-30 12:11:07 -070030 void onDelayedSetup() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000031 if (!fInitialized) {
32 make_checkerboard();
33 fInitialized = true;
34 }
35 }
36
mtkleina1ebeb22015-10-01 09:43:39 -070037 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +000038 const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
39 const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
40 SkPaint paint;
41 paint.setImageFilter(
robertphillips11171f32016-04-07 07:34:15 -070042 SkMagnifierImageFilter::Make(
sugoi@google.com580a1722013-04-23 14:20:45 +000043 SkRect::MakeXYWH(SkIntToScalar(w / 4),
44 SkIntToScalar(h / 4),
45 SkIntToScalar(w / 2),
robertphillips11171f32016-04-07 07:34:15 -070046 SkIntToScalar(h / 2)), 100, nullptr));
mtklein@google.comc2897432013-09-10 19:23:38 +000047
commit-bot@chromium.org33614712013-12-03 18:17:16 +000048 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000049 canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
50 }
sugoi@google.com580a1722013-04-23 14:20:45 +000051 }
52
53private:
54 void make_checkerboard() {
55 const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
56 const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070057 fCheckerboard.allocN32Pixels(w, h);
58 SkCanvas canvas(fCheckerboard);
sugoi@google.com580a1722013-04-23 14:20:45 +000059 canvas.clear(0x00000000);
60 SkPaint darkPaint;
61 darkPaint.setColor(0xFF804020);
62 SkPaint lightPaint;
63 lightPaint.setColor(0xFF244484);
64 for (int y = 0; y < h; y += 16) {
65 for (int x = 0; x < w; x += 16) {
66 canvas.save();
67 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
68 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
69 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
70 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
71 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
72 canvas.restore();
73 }
74 }
75 }
76
77 bool fIsSmall;
78 bool fInitialized;
79 SkBitmap fCheckerboard;
tfarinaf168b862014-06-19 12:32:29 -070080 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000081};
82
83///////////////////////////////////////////////////////////////////////////////
84
mtklein@google.com410e6e82013-09-13 19:52:27 +000085DEF_BENCH( return new MagnifierBench(true); )
86DEF_BENCH( return new MagnifierBench(false); )