bl_test_context_mt.cpp (4834B)
1 // This file is part of Blend2D project <https://blend2d.com> 2 // 3 // See blend2d.h or LICENSE.md for license and copyright information 4 // SPDX-License-Identifier: Zlib 5 6 #include <blend2d.h> 7 #include <cmath> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 #include "bl_test_context_baseapp.h" 13 #include "bl_test_context_utilities.h" 14 15 #include "../commons/imagediff.h" 16 17 namespace ContextTests { 18 19 class MTTestApp : public BaseTestApp { 20 public: 21 uint32_t failed_count {}; 22 uint32_t passed_count {}; 23 24 MTTestApp() : BaseTestApp() { 25 default_options.thread_count = 2u; 26 } 27 28 int help() { 29 using StringUtils::bool_to_string; 30 31 printf("Usage:\n"); 32 printf(" bl_test_context_mt [options] [--help for help]\n"); 33 printf("\n"); 34 35 printf("Purpose:\n"); 36 printf(" Multi-threaded rendering context tester is designed to verify whether both\n"); 37 printf(" single-threaded and multi-threaded rendering contexts yield pixel identical\n"); 38 printf(" output when used with the same input data.\n"); 39 printf("\n"); 40 41 print_common_options(default_options); 42 43 printf("Multithreading Options:\n"); 44 printf(" --flush-sync - Do occasional syncs between calls [default=%s]\n", bool_to_string(default_options.flush_sync)); 45 printf(" --thread-count=<uint> - Number of threads of MT context [default=%u]\n", default_options.thread_count); 46 printf("\n"); 47 48 print_commands(); 49 print_formats(); 50 print_comp_ops(); 51 print_opacity_ops(); 52 print_style_ids(); 53 print_style_ops(); 54 55 fflush(stdout); 56 return 0; 57 } 58 59 bool parseMTOptions(CmdLine cmd_line) { 60 options.flush_sync = cmd_line.has_arg("--flush-sync") || default_options.flush_sync; 61 options.thread_count = cmd_line.value_as_uint("--thread-count", default_options.thread_count); 62 63 return true; 64 } 65 66 int run(CmdLine cmd_line) { 67 print_app_info("Blend2D Multi-Threaded Rendering Context Tester", cmd_line.has_arg("--quiet")); 68 69 if (cmd_line.has_arg("--help")) 70 return help(); 71 72 if (!parse_common_options(cmd_line) || !parseMTOptions(cmd_line)) 73 return 1; 74 75 for (BLFormat format : test_cases.format_ids) { 76 ContextTester a_tester(test_cases, "st"); 77 ContextTester b_tester(test_cases, "mt"); 78 79 a_tester.set_flush_sync(options.flush_sync); 80 b_tester.set_flush_sync(options.flush_sync); 81 82 BLContextCreateInfo a_create_info {}; 83 BLContextCreateInfo b_create_info {}; 84 85 b_create_info.thread_count = options.thread_count; 86 87 if (a_tester.init(int(options.width), int(options.height), format, a_create_info) != BL_SUCCESS || 88 b_tester.init(int(options.width), int(options.height), format, b_create_info) != BL_SUCCESS) { 89 printf("Failed to initialize rendering contexts\n"); 90 return 1; 91 } 92 93 TestInfo info; 94 dispatch_runs([&](CommandId command_id, StyleId style_id, StyleOp style_op, CompOp comp_op, OpacityOp opacity_op) { 95 BLString s0; 96 s0.append_format("%s/%s", 97 StringUtils::style_id_to_string(style_id), 98 StringUtils::style_op_to_string(style_op)); 99 100 BLString s1; 101 s1.append_format("%s/%s", 102 StringUtils::comp_op_to_string(comp_op), 103 StringUtils::opacity_op_to_string(opacity_op)); 104 105 info.name.assign_format( 106 "%-21s | fmt=%-7s| style+api=%-30s| comp+op=%-20s| thread-count=%u", 107 StringUtils::command_id_to_string(command_id), 108 StringUtils::format_to_string(format), 109 s0.data(), 110 s1.data(), 111 options.thread_count); 112 113 info.id.assign_format("ctx-mt-%s-%s-%s-%s-%s-%s-%u", 114 StringUtils::format_to_string(format), 115 StringUtils::command_id_to_string(command_id), 116 StringUtils::style_id_to_string(style_id), 117 StringUtils::style_op_to_string(style_op), 118 StringUtils::comp_op_to_string(comp_op), 119 StringUtils::opacity_op_to_string(opacity_op), 120 options.thread_count); 121 122 if (!options.quiet) { 123 printf("Running [%s]\n", info.name.data()); 124 } 125 126 a_tester.set_options(comp_op, opacity_op, style_id, style_op); 127 b_tester.set_options(comp_op, opacity_op, style_id, style_op); 128 129 if (run_multiple(command_id, info, a_tester, b_tester, 0)) 130 passed_count++; 131 else 132 failed_count++; 133 }); 134 135 a_tester.reset(); 136 b_tester.reset(); 137 } 138 139 if (failed_count) { 140 printf("[FAILED] %u tests out of %u failed\n", failed_count, passed_count + failed_count); 141 return 1; 142 } 143 else { 144 printf("[PASSED] %u tests passed\n", passed_count); 145 return 0; 146 } 147 } 148 }; 149 150 } // {ContextTests} 151 152 int main(int argc, char* argv[]) { 153 BLRuntimeScope rt_scope; 154 ContextTests::MTTestApp app; 155 156 return app.run(CmdLine(argc, argv)); 157 }