Added a few bench for some image filters
authorsugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 23 Apr 2013 14:20:45 +0000 (14:20 +0000)
committersugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 23 Apr 2013 14:20:45 +0000 (14:20 +0000)
Review URL: https://codereview.chromium.org/14234019

git-svn-id: http://skia.googlecode.com/svn/trunk@8824 2bbb7eff-a529-9590-31e7-b0007b416f81

bench/BlendBench.cpp [new file with mode: 0644]
bench/ColorFilterBench.cpp [new file with mode: 0644]
bench/DisplacementBench.cpp [new file with mode: 0644]
bench/MagnifierBench.cpp [new file with mode: 0644]
bench/MergeBench.cpp [new file with mode: 0644]
gyp/bench.gypi

diff --git a/bench/BlendBench.cpp b/bench/BlendBench.cpp
new file mode 100644 (file)
index 0000000..cbf5852
--- /dev/null
@@ -0,0 +1,197 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "SkBenchmark.h"
+#include "SkBitmapSource.h"
+#include "SkBlendImageFilter.h"
+#include "SkCanvas.h"
+#include "SkDevice.h"
+
+#define FILTER_WIDTH  256
+#define FILTER_HEIGHT 256
+
+class BlendBaseBench : public SkBenchmark {
+public:
+    BlendBaseBench(void* param) : INHERITED(param), fInitialized(false) {
+    }
+
+protected:
+    virtual void onPreDraw() SK_OVERRIDE {
+        if (!fInitialized) {
+            make_bitmap();
+            make_checkerboard();
+            fInitialized = true;
+        }
+    }
+
+    void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x) {
+        canvas->save();
+        canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), 0,
+            SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
+        canvas->drawBitmap(fBitmap, SkIntToScalar(x), 0, &paint);
+        canvas->restore();
+    }
+
+    void make_bitmap() {
+        fBitmap.setConfig(SkBitmap::kARGB_8888_Config, FILTER_WIDTH, FILTER_HEIGHT);
+        fBitmap.allocPixels();
+        SkDevice device(fBitmap);
+        SkCanvas canvas(&device);
+        canvas.clear(0x00000000);
+        SkPaint paint;
+        paint.setAntiAlias(true);
+        paint.setColor(0xD000D000);
+        paint.setTextSize(SkIntToScalar(96));
+        const char* str = "e";
+        canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
+    }
+
+    void make_checkerboard() {
+        fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, FILTER_WIDTH, FILTER_HEIGHT);
+        fCheckerboard.allocPixels();
+        SkDevice device(fCheckerboard);
+        SkCanvas canvas(&device);
+        canvas.clear(0x00000000);
+        SkPaint darkPaint;
+        darkPaint.setColor(0xFF404040);
+        SkPaint lightPaint;
+        lightPaint.setColor(0xFFA0A0A0);
+        for (int y = 0; y < FILTER_HEIGHT; y += 16) {
+          for (int x = 0; x < FILTER_WIDTH; x += 16) {
+            canvas.save();
+            canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
+            canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
+            canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
+            canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
+            canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
+            canvas.restore();
+          }
+        }
+    }
+
+    typedef SkBenchmark INHERITED;
+    SkBitmap fBitmap, fCheckerboard;
+    bool fInitialized;
+};
+
+class BlendNormalBench : public BlendBaseBench {
+public:
+    BlendNormalBench(void* param) : INHERITED(param) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return "blend_normal";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
+                (SkBlendImageFilter::kNormal_Mode, background)))->unref();
+        drawClippedBitmap(canvas, paint, 0);
+    }
+
+private:
+    typedef BlendBaseBench INHERITED;
+};
+
+class BlendMultiplyBench : public BlendBaseBench {
+public:
+    BlendMultiplyBench(void* param) : INHERITED(param) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return "blend_multiply";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
+                (SkBlendImageFilter::kMultiply_Mode, background)))->unref();
+        drawClippedBitmap(canvas, paint, 100);
+    }
+
+private:
+    typedef BlendBaseBench INHERITED;
+};
+
+class BlendScreenBench : public BlendBaseBench {
+public:
+    BlendScreenBench(void* param) : INHERITED(param) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return "blend_screen";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
+                (SkBlendImageFilter::kScreen_Mode, background)))->unref();
+        drawClippedBitmap(canvas, paint, 200);
+    }
+
+private:
+    typedef BlendBaseBench INHERITED;
+};
+
+class BlendDarkenBench : public BlendBaseBench {
+public:
+    BlendDarkenBench(void* param) : INHERITED(param) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return "blend_darken";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
+                (SkBlendImageFilter::kDarken_Mode, background)))->unref();
+        drawClippedBitmap(canvas, paint, 300);
+    }
+
+private:
+    typedef BlendBaseBench INHERITED;
+};
+
+class BlendLightenBench : public BlendBaseBench {
+public:
+    BlendLightenBench(void* param) : INHERITED(param) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return "blend_lighten";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter,
+                (SkBlendImageFilter::kLighten_Mode, background)))->unref();
+        drawClippedBitmap(canvas, paint, 400);
+    }
+
+private:
+    typedef BlendBaseBench INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+DEF_BENCH( return new BlendNormalBench(p); )
+DEF_BENCH( return new BlendMultiplyBench(p); )
+DEF_BENCH( return new BlendScreenBench(p); )
+DEF_BENCH( return new BlendDarkenBench(p); )
+DEF_BENCH( return new BlendLightenBench(p); )
+
diff --git a/bench/ColorFilterBench.cpp b/bench/ColorFilterBench.cpp
new file mode 100644 (file)
index 0000000..df5ab07
--- /dev/null
@@ -0,0 +1,336 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "SkBenchmark.h"
+#include "SkCanvas.h"
+#include "SkColorFilterImageFilter.h"
+#include "SkColorMatrixFilter.h"
+#include "SkTableColorFilter.h"
+
+#define FILTER_WIDTH_SMALL  SkIntToScalar(32)
+#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
+#define FILTER_WIDTH_LARGE  SkIntToScalar(256)
+#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
+
+class ColorFilterBaseBench : public SkBenchmark {
+
+public:
+    ColorFilterBaseBench(void* param, bool small) : INHERITED(param), fIsSmall(small) {
+    }
+
+protected:
+    SkRect getFilterRect() const {
+        return isSmall() ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
+                           SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
+    }
+
+    static SkImageFilter* make_brightness(float amount, SkImageFilter* input = NULL) {
+        SkScalar amount255 = SkScalarMul(SkFloatToScalar(amount), SkIntToScalar(255));
+        SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
+                                0, 1, 0, 0, amount255,
+                                0, 0, 1, 0, amount255,
+                                0, 0, 0, 1, 0 };
+        SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
+        return SkColorFilterImageFilter::Create(filter, input);
+    }
+
+    static SkImageFilter* make_grayscale(SkImageFilter* input = NULL) {
+        SkScalar matrix[20];
+        memset(matrix, 0, 20 * sizeof(SkScalar));
+        matrix[0] = matrix[5] = matrix[10] = SkFloatToScalar(0.2126f);
+        matrix[1] = matrix[6] = matrix[11] = SkFloatToScalar(0.7152f);
+        matrix[2] = matrix[7] = matrix[12] = SkFloatToScalar(0.0722f);
+        matrix[18] = SkFloatToScalar(1.0f);
+        SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
+        return SkColorFilterImageFilter::Create(filter, input);
+    }
+
+    static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) {
+        SkAutoTUnref<SkColorFilter> filter(
+            SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
+        return SkColorFilterImageFilter::Create(filter, input);
+    }
+
+    inline bool isSmall() const { return fIsSmall; }
+private:
+    bool fIsSmall;
+
+    typedef SkBenchmark INHERITED;
+};
+
+class ColorFilterDimBrightBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterDimBrightBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_dim_bright_small" : "colorfilter_dim_bright_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.4f) {
+            SkAutoTUnref<SkImageFilter> dim(make_brightness(-brightness));
+            SkAutoTUnref<SkImageFilter> bright(make_brightness(brightness, dim));
+            paint.setImageFilter(bright);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class ColorFilterBrightGrayBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterBrightGrayBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_bright_gray_small" : "colorfilter_bright_gray_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f));
+            SkAutoTUnref<SkImageFilter> grayscale(make_grayscale(brightness));
+            paint.setImageFilter(grayscale);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class ColorFilterGrayBrightBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterGrayBrightBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_gray_bright_small" : "colorfilter_gray_bright_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkImageFilter> grayscale(make_grayscale());
+            SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f, grayscale));
+            paint.setImageFilter(brightness);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class ColorFilterBlueBrightBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterBlueBrightBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_blue_bright_small" : "colorfilter_blue_bright_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkImageFilter> blue(make_mode_blue());
+            SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f, blue));
+            paint.setImageFilter(brightness);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class ColorFilterBrightBlueBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterBrightBlueBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_bright_blue_small" : "colorfilter_bright_blue_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f));
+            SkAutoTUnref<SkImageFilter> blue(make_mode_blue(brightness));
+            paint.setImageFilter(blue);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class ColorFilterBrightBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterBrightBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_bright_small" : "colorfilter_bright_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f));
+            paint.setImageFilter(brightness);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class ColorFilterBlueBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterBlueBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_blue_small" : "colorfilter_blue_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkImageFilter> blue(make_mode_blue());
+            paint.setImageFilter(blue);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class ColorFilterGrayBench : public ColorFilterBaseBench {
+
+public:
+    ColorFilterGrayBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "colorfilter_gray_small" : "colorfilter_gray_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkImageFilter> grayscale(make_grayscale());
+            paint.setImageFilter(grayscale);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+class TableColorFilterBench : public ColorFilterBaseBench {
+
+public:
+    TableColorFilterBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "table_colorfilter_small" : "table_colorfilter_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = getFilterRect();
+        SkPaint paint;
+        paint.setColor(SK_ColorRED);
+        {
+            SkAutoTUnref<SkColorFilter> table_filter(make_table_filter());
+            paint.setColorFilter(table_filter);
+            canvas->drawRect(r, paint);
+        }
+    }
+
+private:
+    static void fill_table_data(uint8_t table[]) {
+        for (int i = 0; i < 256; ++i) {
+            int n = i >> 5;
+            table[i] = (n << 5) | (n << 2) | (n >> 1);
+        }
+    }
+
+    static SkColorFilter* make_table_filter() {
+        uint8_t table[256]; fill_table_data(table);
+        return SkTableColorFilter::Create(table);
+    }
+
+    typedef ColorFilterBaseBench INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+DEF_BENCH( return new ColorFilterDimBrightBench(p, true); )
+DEF_BENCH( return new ColorFilterBrightGrayBench(p, true); )
+DEF_BENCH( return new ColorFilterGrayBrightBench(p, true); )
+DEF_BENCH( return new ColorFilterBlueBrightBench(p, true); )
+DEF_BENCH( return new ColorFilterBrightBlueBench(p, true); )
+DEF_BENCH( return new ColorFilterBrightBench(p, true); )
+DEF_BENCH( return new ColorFilterBlueBench(p, true); )
+DEF_BENCH( return new ColorFilterGrayBench(p, true); )
+DEF_BENCH( return new TableColorFilterBench(p, true); )
+
+DEF_BENCH( return new ColorFilterDimBrightBench(p, false); )
+DEF_BENCH( return new ColorFilterBrightGrayBench(p, false); )
+DEF_BENCH( return new ColorFilterGrayBrightBench(p, false); )
+DEF_BENCH( return new ColorFilterBlueBrightBench(p, false); )
+DEF_BENCH( return new ColorFilterBrightBlueBench(p, false); )
+DEF_BENCH( return new ColorFilterBrightBench(p, false); )
+DEF_BENCH( return new ColorFilterBlueBench(p, false); )
+DEF_BENCH( return new ColorFilterGrayBench(p, false); )
+DEF_BENCH( return new TableColorFilterBench(p, false); )
+
diff --git a/bench/DisplacementBench.cpp b/bench/DisplacementBench.cpp
new file mode 100644 (file)
index 0000000..32495cb
--- /dev/null
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "SkBenchmark.h"
+#include "SkBitmapSource.h"
+#include "SkCanvas.h"
+#include "SkDevice.h"
+#include "SkDisplacementMapEffect.h"
+
+#define FILTER_WIDTH_SMALL  SkIntToScalar(32)
+#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
+#define FILTER_WIDTH_LARGE  SkIntToScalar(256)
+#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
+
+class DisplacementBaseBench : public SkBenchmark {
+public:
+    DisplacementBaseBench(void* param, bool small) :
+        INHERITED(param), fInitialized(false), fIsSmall(small) {
+    }
+
+protected:
+    virtual void onPreDraw() SK_OVERRIDE {
+        if (!fInitialized) {
+            make_bitmap();
+            make_checkerboard();
+            fInitialized = true;
+        }
+    }
+
+    void make_bitmap() {
+        const int w = isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
+        const int h = isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
+        fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
+        fBitmap.allocPixels();
+        SkDevice device(fBitmap);
+        SkCanvas canvas(&device);
+        canvas.clear(0x00000000);
+        SkPaint paint;
+        paint.setAntiAlias(true);
+        paint.setColor(0xFF884422);
+        paint.setTextSize(SkIntToScalar(96));
+        const char* str = "g";
+        canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
+    }
+
+    void make_checkerboard() {
+        const int w = isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
+        const int h = isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
+        fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h);
+        fCheckerboard.allocPixels();
+        SkDevice device(fCheckerboard);
+        SkCanvas canvas(&device);
+        canvas.clear(0x00000000);
+        SkPaint darkPaint;
+        darkPaint.setColor(0xFF804020);
+        SkPaint lightPaint;
+        lightPaint.setColor(0xFF244484);
+        for (int y = 0; y < h; y += 16) {
+            for (int x = 0; x < w; x += 16) {
+                canvas.save();
+                canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
+                canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
+                canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
+                canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
+                canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
+                canvas.restore();
+            }
+        }
+    }
+
+    void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
+        canvas->save();
+        canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
+            SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
+        canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
+        canvas->restore();
+    }
+
+    inline bool isSmall() const { return fIsSmall; }
+
+    SkBitmap fBitmap, fCheckerboard;
+private:
+    bool fInitialized;
+    bool fIsSmall;
+    typedef SkBenchmark INHERITED;
+};
+
+class DisplacementZeroBench : public DisplacementBaseBench {
+public:
+    DisplacementZeroBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "displacement_zero_small" : "displacement_zero_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        // No displacement effect
+        paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
+            (SkDisplacementMapEffect::kR_ChannelSelectorType,
+             SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref();
+        drawClippedBitmap(canvas, 0, 0, paint);
+    }
+
+private:
+    typedef DisplacementBaseBench INHERITED;
+};
+
+class DisplacementAlphaBench : public DisplacementBaseBench {
+public:
+    DisplacementAlphaBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        // Displacement, with 1 alpha component (which isn't pre-multiplied)
+        paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
+            (SkDisplacementMapEffect::kB_ChannelSelectorType,
+             SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref();
+        drawClippedBitmap(canvas, 100, 0, paint);
+    }
+
+private:
+    typedef DisplacementBaseBench INHERITED;
+};
+
+class DisplacementFullBench : public DisplacementBaseBench {
+public:
+    DisplacementFullBench(void* param, bool small) : INHERITED(param, small) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return isSmall() ? "displacement_full_small" : "displacement_full_large";
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
+        // Displacement, with 2 non-alpha components
+        paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
+            (SkDisplacementMapEffect::kR_ChannelSelectorType,
+             SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref();
+        drawClippedBitmap(canvas, 200, 0, paint);
+    }
+
+private:
+    typedef DisplacementBaseBench INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+DEF_BENCH( return new DisplacementZeroBench(p, true); )
+DEF_BENCH( return new DisplacementAlphaBench(p, true); )
+DEF_BENCH( return new DisplacementFullBench(p, true); )
+DEF_BENCH( return new DisplacementZeroBench(p, false); )
+DEF_BENCH( return new DisplacementAlphaBench(p, false); )
+DEF_BENCH( return new DisplacementFullBench(p, false); )
+
diff --git a/bench/MagnifierBench.cpp b/bench/MagnifierBench.cpp
new file mode 100644 (file)
index 0000000..af507ac
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "SkBenchmark.h"
+#include "SkCanvas.h"
+#include "SkDevice.h"
+#include "SkMagnifierImageFilter.h"
+#include "SkRandom.h"
+
+#define FILTER_WIDTH_SMALL  SkIntToScalar(32)
+#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
+#define FILTER_WIDTH_LARGE  SkIntToScalar(256)
+#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
+
+class MagnifierBench : public SkBenchmark {
+public:
+    MagnifierBench(void* param, bool small) :
+        INHERITED(param), fIsSmall(small), fInitialized(false) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return fIsSmall ? "magnifier_small" : "magnifier_large";
+    }
+
+    virtual void onPreDraw() SK_OVERRIDE {
+        if (!fInitialized) {
+            make_checkerboard();
+            fInitialized = true;
+        }
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
+        const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
+        SkPaint paint;
+        paint.setImageFilter(
+            new SkMagnifierImageFilter(
+                SkRect::MakeXYWH(SkIntToScalar(w / 4),
+                                 SkIntToScalar(h / 4),
+                                 SkIntToScalar(w / 2),
+                                 SkIntToScalar(h / 2)), 100))->unref();
+        canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
+    }
+
+private:
+    void make_checkerboard() {
+        const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
+        const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
+        fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h);
+        fCheckerboard.allocPixels();
+        SkDevice device(fCheckerboard);
+        SkCanvas canvas(&device);
+        canvas.clear(0x00000000);
+        SkPaint darkPaint;
+        darkPaint.setColor(0xFF804020);
+        SkPaint lightPaint;
+        lightPaint.setColor(0xFF244484);
+        for (int y = 0; y < h; y += 16) {
+            for (int x = 0; x < w; x += 16) {
+                canvas.save();
+                canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
+                canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
+                canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
+                canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
+                canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
+                canvas.restore();
+            }
+        }
+    }
+
+    bool fIsSmall;
+    bool fInitialized;
+    SkBitmap fCheckerboard;
+    typedef SkBenchmark INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+DEF_BENCH( return new MagnifierBench(p, true); )
+DEF_BENCH( return new MagnifierBench(p, false); )
+
diff --git a/bench/MergeBench.cpp b/bench/MergeBench.cpp
new file mode 100644 (file)
index 0000000..d74c4c2
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "SkBenchmark.h"
+#include "SkBitmapSource.h"
+#include "SkBlurImageFilter.h"
+#include "SkCanvas.h"
+#include "SkDevice.h"
+#include "SkMergeImageFilter.h"
+
+#define FILTER_WIDTH_SMALL  SkIntToScalar(32)
+#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
+#define FILTER_WIDTH_LARGE  SkIntToScalar(256)
+#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
+
+class MergeBench : public SkBenchmark {
+public:
+    MergeBench(void* param, bool small) : INHERITED(param), fIsSmall(small), fInitialized(false) {
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return fIsSmall ? "merge_small" : "merge_large";
+    }
+
+    virtual void onPreDraw() SK_OVERRIDE {
+        if (!fInitialized) {
+            make_bitmap();
+            make_checkerboard();
+            fInitialized = true;
+        }
+    }
+
+    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
+                              SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
+        SkPaint paint;
+        paint.setImageFilter(mergeBitmaps())->unref();
+        canvas->drawRect(r, paint);
+    }
+
+private:
+    SkImageFilter* mergeBitmaps() {
+        SkImageFilter* first = new SkBitmapSource(fCheckerboard);
+        SkImageFilter* second = new SkBitmapSource(fBitmap);
+        SkAutoUnref aur0(first);
+        SkAutoUnref aur1(second);
+        return new SkMergeImageFilter(first, second);
+    }
+
+    void make_bitmap() {
+        fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
+        fBitmap.allocPixels();
+        SkDevice device(fBitmap);
+        SkCanvas canvas(&device);
+        canvas.clear(0x00000000);
+        SkPaint paint;
+        paint.setAntiAlias(true);
+        paint.setColor(0xFF884422);
+        paint.setTextSize(SkIntToScalar(96));
+        const char* str = "g";
+        canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
+    }
+
+    void make_checkerboard() {
+        fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
+        fCheckerboard.allocPixels();
+        SkDevice device(fCheckerboard);
+        SkCanvas canvas(&device);
+        canvas.clear(0x00000000);
+        SkPaint darkPaint;
+        darkPaint.setColor(0xFF804020);
+        SkPaint lightPaint;
+        lightPaint.setColor(0xFF244484);
+        for (int y = 0; y < 80; y += 16) {
+            for (int x = 0; x < 80; x += 16) {
+                canvas.save();
+                canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
+                canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
+                canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
+                canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
+                canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
+                canvas.restore();
+            }
+        }
+    }
+
+    bool fIsSmall;
+    bool fInitialized;
+    SkBitmap fBitmap, fCheckerboard;
+
+    typedef SkBenchmark INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+DEF_BENCH( return new MergeBench(p, true); )
+DEF_BENCH( return new MergeBench(p, false); )
+
index 74964c3..8bae9b7 100644 (file)
     '../bench/BicubicBench.cpp',
     '../bench/BitmapBench.cpp',
     '../bench/BitmapRectBench.cpp',
+    '../bench/BlendBench.cpp',
     '../bench/BlurBench.cpp',
     '../bench/BlurRectBench.cpp',
     '../bench/ChecksumBench.cpp',
     '../bench/ChartBench.cpp',
     '../bench/ChromeBench.cpp',
+    '../bench/ColorFilterBench.cpp',
     '../bench/DashBench.cpp',
     '../bench/DecodeBench.cpp',
     '../bench/DeferredCanvasBench.cpp',
     '../bench/DeferredSurfaceCopyBench.cpp',
+    '../bench/DisplacementBench.cpp',
     '../bench/FontScalerBench.cpp',
     '../bench/GradientBench.cpp',
     '../bench/GrMemoryPoolBench.cpp',
     '../bench/InterpBench.cpp',
     '../bench/LineBench.cpp',
+    '../bench/MagnifierBench.cpp',
     '../bench/MathBench.cpp',
     '../bench/Matrix44Bench.cpp',
     '../bench/MatrixBench.cpp',
     '../bench/MatrixConvolutionBench.cpp',
     '../bench/MemoryBench.cpp',
+    '../bench/MergeBench.cpp',
     '../bench/MorphologyBench.cpp',
     '../bench/MutexBench.cpp',
     '../bench/PathBench.cpp',