blob: c47dcdc94770a4b232d65b7c3ec57461bc4d9032 [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"
sugoi@google.com580a1722013-04-23 14:20:45 +00009#include "SkCanvas.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000010#include "SkMagnifierImageFilter.h"
11#include "SkRandom.h"
12
sugoi@google.comca425d92013-04-23 14:37:38 +000013#define FILTER_WIDTH_SMALL 32
14#define FILTER_HEIGHT_SMALL 32
15#define FILTER_WIDTH_LARGE 256
16#define FILTER_HEIGHT_LARGE 256
sugoi@google.com580a1722013-04-23 14:20:45 +000017
tfarinaf168b862014-06-19 12:32:29 -070018class MagnifierBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000019public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 MagnifierBench(bool small) :
21 fIsSmall(small), fInitialized(false) {
sugoi@google.com580a1722013-04-23 14:20:45 +000022 }
23
24protected:
25 virtual const char* onGetName() SK_OVERRIDE {
26 return fIsSmall ? "magnifier_small" : "magnifier_large";
27 }
28
29 virtual void onPreDraw() SK_OVERRIDE {
30 if (!fInitialized) {
31 make_checkerboard();
32 fInitialized = true;
33 }
34 }
35
commit-bot@chromium.org33614712013-12-03 18:17:16 +000036 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +000037 const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
38 const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
39 SkPaint paint;
40 paint.setImageFilter(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000041 SkMagnifierImageFilter::Create(
sugoi@google.com580a1722013-04-23 14:20:45 +000042 SkRect::MakeXYWH(SkIntToScalar(w / 4),
43 SkIntToScalar(h / 4),
44 SkIntToScalar(w / 2),
45 SkIntToScalar(h / 2)), 100))->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +000046
commit-bot@chromium.org33614712013-12-03 18:17:16 +000047 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000048 canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
49 }
sugoi@google.com580a1722013-04-23 14:20:45 +000050 }
51
52private:
53 void make_checkerboard() {
54 const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
55 const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
reed6c225732014-06-09 19:52:07 -070056 fCheckerboard.allocN32Pixels(w, h);
57 SkCanvas canvas(fCheckerboard);
sugoi@google.com580a1722013-04-23 14:20:45 +000058 canvas.clear(0x00000000);
59 SkPaint darkPaint;
60 darkPaint.setColor(0xFF804020);
61 SkPaint lightPaint;
62 lightPaint.setColor(0xFF244484);
63 for (int y = 0; y < h; y += 16) {
64 for (int x = 0; x < w; x += 16) {
65 canvas.save();
66 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
67 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
68 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
69 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
70 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
71 canvas.restore();
72 }
73 }
74 }
75
76 bool fIsSmall;
77 bool fInitialized;
78 SkBitmap fCheckerboard;
tfarinaf168b862014-06-19 12:32:29 -070079 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000080};
81
82///////////////////////////////////////////////////////////////////////////////
83
mtklein@google.com410e6e82013-09-13 19:52:27 +000084DEF_BENCH( return new MagnifierBench(true); )
85DEF_BENCH( return new MagnifierBench(false); )