2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapSource.h"
11 #include "SkMergeImageFilter.h"
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)
18 class MergeBench : public Benchmark {
20 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
23 const char* onGetName() override {
24 return fIsSmall ? "merge_small" : "merge_large";
27 void onPreDraw() override {
35 void onDraw(const int loops, SkCanvas* canvas) override {
36 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
37 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
39 paint.setImageFilter(mergeBitmaps())->unref();
40 for (int i = 0; i < loops; i++) {
41 canvas->drawRect(r, paint);
46 SkImageFilter* mergeBitmaps() {
47 SkImageFilter* first = SkBitmapSource::Create(fCheckerboard);
48 SkImageFilter* second = SkBitmapSource::Create(fBitmap);
49 SkAutoUnref aur0(first);
50 SkAutoUnref aur1(second);
51 return SkMergeImageFilter::Create(first, second);
55 fBitmap.allocN32Pixels(80, 80);
56 SkCanvas canvas(fBitmap);
57 canvas.clear(0x00000000);
59 paint.setAntiAlias(true);
60 paint.setColor(0xFF884422);
61 paint.setTextSize(SkIntToScalar(96));
62 const char* str = "g";
63 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
66 void make_checkerboard() {
67 fCheckerboard.allocN32Pixels(80, 80);
68 SkCanvas canvas(fCheckerboard);
69 canvas.clear(0x00000000);
71 darkPaint.setColor(0xFF804020);
73 lightPaint.setColor(0xFF244484);
74 for (int y = 0; y < 80; y += 16) {
75 for (int x = 0; x < 80; x += 16) {
77 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
78 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
79 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
80 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
81 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
89 SkBitmap fBitmap, fCheckerboard;
91 typedef Benchmark INHERITED;
94 ///////////////////////////////////////////////////////////////////////////////
96 DEF_BENCH( return new MergeBench(true); )
97 DEF_BENCH( return new MergeBench(false); )