blob: 3639c954a2176331140454295b5a44df0f57d56c [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 */
7#include "SkBenchmark.h"
8#include "SkBitmapSource.h"
sugoi@google.com580a1722013-04-23 14:20:45 +00009#include "SkCanvas.h"
10#include "SkDevice.h"
11#include "SkMergeImageFilter.h"
12
13#define FILTER_WIDTH_SMALL SkIntToScalar(32)
14#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
15#define FILTER_WIDTH_LARGE SkIntToScalar(256)
16#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
17
18class MergeBench : public SkBenchmark {
19public:
20 MergeBench(void* param, bool small) : INHERITED(param), fIsSmall(small), fInitialized(false) {
21 }
22
23protected:
24 virtual const char* onGetName() SK_OVERRIDE {
25 return fIsSmall ? "merge_small" : "merge_large";
26 }
27
28 virtual void onPreDraw() SK_OVERRIDE {
29 if (!fInitialized) {
30 make_bitmap();
31 make_checkerboard();
32 fInitialized = true;
33 }
34 }
35
36 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
37 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
38 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
39 SkPaint paint;
40 paint.setImageFilter(mergeBitmaps())->unref();
41 canvas->drawRect(r, paint);
42 }
43
44private:
45 SkImageFilter* mergeBitmaps() {
46 SkImageFilter* first = new SkBitmapSource(fCheckerboard);
47 SkImageFilter* second = new SkBitmapSource(fBitmap);
48 SkAutoUnref aur0(first);
49 SkAutoUnref aur1(second);
50 return new SkMergeImageFilter(first, second);
51 }
52
53 void make_bitmap() {
54 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
55 fBitmap.allocPixels();
robertphillips@google.com9b051a32013-08-20 20:06:40 +000056 SkDevice device(fBitmap);
sugoi@google.com580a1722013-04-23 14:20:45 +000057 SkCanvas canvas(&device);
58 canvas.clear(0x00000000);
59 SkPaint paint;
60 paint.setAntiAlias(true);
61 paint.setColor(0xFF884422);
62 paint.setTextSize(SkIntToScalar(96));
63 const char* str = "g";
64 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
65 }
66
67 void make_checkerboard() {
68 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
69 fCheckerboard.allocPixels();
robertphillips@google.com9b051a32013-08-20 20:06:40 +000070 SkDevice device(fCheckerboard);
sugoi@google.com580a1722013-04-23 14:20:45 +000071 SkCanvas canvas(&device);
72 canvas.clear(0x00000000);
73 SkPaint darkPaint;
74 darkPaint.setColor(0xFF804020);
75 SkPaint lightPaint;
76 lightPaint.setColor(0xFF244484);
77 for (int y = 0; y < 80; y += 16) {
78 for (int x = 0; x < 80; x += 16) {
79 canvas.save();
80 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
81 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
82 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
83 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
84 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
85 canvas.restore();
86 }
87 }
88 }
89
90 bool fIsSmall;
91 bool fInitialized;
92 SkBitmap fBitmap, fCheckerboard;
93
94 typedef SkBenchmark INHERITED;
95};
96
97///////////////////////////////////////////////////////////////////////////////
98
99DEF_BENCH( return new MergeBench(p, true); )
100DEF_BENCH( return new MergeBench(p, false); )