From 7cbf5fd420d995641c87778104680529eba7e422 Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 18 Jul 2017 17:03:12 -0300 Subject: [PATCH] Add some simple benchmarks for color blending functions --- .gitmodules | 3 ++ CMakeLists.txt | 3 +- cmake/FindBenchmarks.cmake | 26 +++++++++++++ src/CMakeLists.txt | 6 ++- src/doc/blend_benchmark.cpp | 74 +++++++++++++++++++++++++++++++++++++ third_party/CMakeLists.txt | 5 +++ third_party/benchmark | 1 + 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 cmake/FindBenchmarks.cmake create mode 100644 src/doc/blend_benchmark.cpp create mode 160000 third_party/benchmark diff --git a/.gitmodules b/.gitmodules index 899232a6d..31b87b00f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -51,3 +51,6 @@ [submodule "third_party/json11"] path = third_party/json11 url = https://github.com/aseprite/json11.git +[submodule "third_party/benchmark"] + path = third_party/benchmark + url = https://github.com/aseprite/benchmark.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 3411278ff..4a6aa3280 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,7 +73,8 @@ option(ENABLE_MEMLEAK "Enable memory-leaks detector (only for developers)" o option(ENABLE_UPDATER "Enable automatic check for updates" on) option(ENABLE_SCRIPTING "Compile with scripting support" on) option(ENABLE_WEBSERVER "Enable support to run a webserver (for HTML5 gamedev)" off) -option(ENABLE_TESTS "Enable the unit tests" off) +option(ENABLE_TESTS "Compile unit tests" off) +option(ENABLE_BENCHMARKS "Compile benchmarks" off) option(ENABLE_TRIAL_MODE "Compile the trial version" off) option(ENABLE_STEAM "Compile with Steam library" off) option(FULLSCREEN_PLATFORM "Enable fullscreen by default" off) diff --git a/cmake/FindBenchmarks.cmake b/cmake/FindBenchmarks.cmake new file mode 100644 index 000000000..1cc8b8c47 --- /dev/null +++ b/cmake/FindBenchmarks.cmake @@ -0,0 +1,26 @@ +# Copyright (C) 2017 David Capello +# Find benchmarks + +function(find_benchmarks dir dependencies) + file(GLOB benchmarks ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/*_benchmark.cpp) + list(REMOVE_AT ARGV 0) + + foreach(benchmarksourcefile ${benchmarks}) + get_filename_component(benchmarkname ${benchmarksourcefile} NAME_WE) + + add_executable(${benchmarkname} ${benchmarksourcefile}) + + if(MSVC) + # Fix problem compiling gen from a Visual Studio solution + set_target_properties(${benchmarkname} + PROPERTIES LINK_FLAGS -ENTRY:"mainCRTStartup") + endif() + + target_link_libraries(${benchmarkname} benchmark ${ARGV} ${PLATFORM_LIBS}) + + if(extra_definitions) + set_target_properties(${benchmarkname} + PROPERTIES COMPILE_FLAGS ${extra_definitions}) + endif() + endforeach() +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 65a760ff3..d65f4df9d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -189,7 +189,6 @@ install(DIRECTORY ../data if(ENABLE_TESTS) include(FindTests) - find_tests(gfx gfx-lib) find_tests(doc doc-lib) find_tests(render render-lib) @@ -199,3 +198,8 @@ if(ENABLE_TESTS) find_tests(app app-lib) find_tests(. app-lib) endif() + +if(ENABLE_BENCHMARKS) + include(FindBenchmarks) + find_benchmarks(doc doc-lib) +endif() diff --git a/src/doc/blend_benchmark.cpp b/src/doc/blend_benchmark.cpp new file mode 100644 index 000000000..7ad9d03ba --- /dev/null +++ b/src/doc/blend_benchmark.cpp @@ -0,0 +1,74 @@ +// Aseprite Document Library +// Copyright (c) 2017 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "doc/blend_funcs.h" + +#include + +namespace doc { + color_t rgba_blender_multiply(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_screen(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_overlay(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_darken(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_lighten(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_color_dodge(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_color_burn(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_hard_light(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_soft_light(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_difference(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_exclusion(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_hsl_hue(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_hsl_saturation(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_hsl_color(color_t backdrop, color_t src, int opacity); + color_t rgba_blender_hsl_luminosity(color_t backdrop, color_t src, int opacity); +} + +using namespace doc; + +static void CustomArguments(benchmark::internal::Benchmark* b) { + b ->Args({ int(rgba(200, 128, 64, 255)), int(rgba(32, 128, 200, 255)), 255 }) + ->Args({ int(rgba(200, 128, 64, 255)), int(rgba(32, 128, 200, 255)), 128 }) + ->Args({ int(rgba(200, 128, 64, 255)), int(rgba(32, 128, 200, 255)), 0 }) + ->Args({ int(rgba(200, 128, 64, 128)), int(rgba(32, 128, 200, 128)), 255 }) + ->Args({ int(rgba(200, 128, 64, 128)), int(rgba(32, 128, 200, 128)), 128 }) + ->Args({ int(rgba(200, 128, 64, 128)), int(rgba(32, 128, 200, 128)), 0 }) + ->Args({ int(rgba(200, 128, 64, 128)), int(rgba(32, 128, 200, 0)), 255 }); +} + +template +void BM_Rgba(benchmark::State& state) { + color_t a = color_t(state.range(0)); + color_t b = color_t(state.range(1)); + int opacity = state.range(2); + BlendFunc func = F; + while (state.KeepRunning()) { + color_t c = func(a, b, opacity); + c = func(c, b, opacity); + } +} + +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_normal)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_multiply)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_screen)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_overlay)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_darken)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_lighten)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_color_dodge)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_color_burn)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_hard_light)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_soft_light)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_difference)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_exclusion)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_hsl_hue)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_hsl_saturation)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_hsl_color)->Apply(CustomArguments); +BENCHMARK_TEMPLATE(BM_Rgba, rgba_blender_hsl_luminosity)->Apply(CustomArguments); + +BENCHMARK_MAIN(); diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index 4a82bff5e..a6c62aeaf 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -119,3 +119,8 @@ set(ENABLE_CNG OFF CACHE BOOL "Enable the use of CNG(Crypto Next Generation)") add_subdirectory(libarchive) target_include_directories(archive_static INTERFACE $) + +# benchmark +if(ENABLE_BENCHMARKS) + add_subdirectory(benchmark) +endif() diff --git a/third_party/benchmark b/third_party/benchmark new file mode 160000 index 000000000..710c2b89d --- /dev/null +++ b/third_party/benchmark @@ -0,0 +1 @@ +Subproject commit 710c2b89d8c6e839e51ac148b4e840ce2c009dbb