|
| 1 | +/** |
| 2 | + * FreeRDP: A Remote Desktop Protocol Implementation |
| 3 | + * primitives benchmarking tool |
| 4 | + * |
| 5 | + * Copyright 2025 Armin Novak <[email protected]> |
| 6 | + * Copyright 2025 Thincast Technologies GmbH |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | + |
| 23 | +#include <winpr/crypto.h> |
| 24 | +#include <winpr/sysinfo.h> |
| 25 | +#include <freerdp/primitives.h> |
| 26 | + |
| 27 | +typedef struct |
| 28 | +{ |
| 29 | + BYTE* channels[3]; |
| 30 | + UINT32 steps[3]; |
| 31 | + prim_size_t roi; |
| 32 | + BYTE* outputBuffer; |
| 33 | + UINT32 outputStride; |
| 34 | + UINT32 testedFormat; |
| 35 | +} primitives_YUV_benchmark; |
| 36 | + |
| 37 | +static void primitives_YUV_benchmark_free(primitives_YUV_benchmark* bench) |
| 38 | +{ |
| 39 | + if (!bench) |
| 40 | + return; |
| 41 | + |
| 42 | + free(bench->outputBuffer); |
| 43 | + |
| 44 | + for (size_t i = 0; i < 3; i++) |
| 45 | + free(bench->channels[i]); |
| 46 | + |
| 47 | + const primitives_YUV_benchmark empty = { 0 }; |
| 48 | + *bench = empty; |
| 49 | +} |
| 50 | + |
| 51 | +static primitives_YUV_benchmark primitives_YUV_benchmark_init(void) |
| 52 | +{ |
| 53 | + primitives_YUV_benchmark ret = { 0 }; |
| 54 | + ret.roi.width = 3840 * 4; |
| 55 | + ret.roi.height = 2160 * 4; |
| 56 | + ret.outputStride = ret.roi.width * 4; |
| 57 | + ret.testedFormat = PIXEL_FORMAT_BGRA32; |
| 58 | + |
| 59 | + ret.outputBuffer = calloc(ret.outputStride, ret.roi.height); |
| 60 | + if (!ret.outputBuffer) |
| 61 | + goto fail; |
| 62 | + |
| 63 | + for (size_t i = 0; i < 3; i++) |
| 64 | + { |
| 65 | + BYTE* buf = ret.channels[i] = calloc(ret.roi.width, ret.roi.height); |
| 66 | + if (!buf) |
| 67 | + goto fail; |
| 68 | + |
| 69 | + winpr_RAND(buf, 1ull * ret.roi.width * ret.roi.height); |
| 70 | + ret.steps[i] = ret.roi.width; |
| 71 | + } |
| 72 | + |
| 73 | + return ret; |
| 74 | + |
| 75 | +fail: |
| 76 | + primitives_YUV_benchmark_free(&ret); |
| 77 | + return ret; |
| 78 | +} |
| 79 | + |
| 80 | +static const char* print_time(UINT64 t, char* buffer, size_t size) |
| 81 | +{ |
| 82 | + (void)_snprintf(buffer, size, "%u.%03u.%03u.%03u", (unsigned)(t / 1000000000ull), |
| 83 | + (unsigned)((t / 1000000ull) % 1000), (unsigned)((t / 1000ull) % 1000), |
| 84 | + (unsigned)((t) % 1000)); |
| 85 | + return buffer; |
| 86 | +} |
| 87 | + |
| 88 | +static BOOL primitives_YUV420_benchmark_run(primitives_YUV_benchmark* bench, primitives_t* prims) |
| 89 | +{ |
| 90 | + const BYTE* channels[3] = { 0 }; |
| 91 | + |
| 92 | + for (size_t i = 0; i < 3; i++) |
| 93 | + channels[i] = bench->channels[i]; |
| 94 | + |
| 95 | + for (size_t x = 0; x < 10; x++) |
| 96 | + { |
| 97 | + const UINT64 start = winpr_GetTickCount64NS(); |
| 98 | + pstatus_t status = |
| 99 | + prims->YUV420ToRGB_8u_P3AC4R(channels, bench->steps, bench->outputBuffer, |
| 100 | + bench->outputStride, bench->testedFormat, &bench->roi); |
| 101 | + const UINT64 end = winpr_GetTickCount64NS(); |
| 102 | + if (status != PRIMITIVES_SUCCESS) |
| 103 | + { |
| 104 | + (void)fprintf(stderr, "Running YUV420ToRGB_8u_P3AC4R failed\n"); |
| 105 | + return FALSE; |
| 106 | + } |
| 107 | + const UINT64 diff = end - start; |
| 108 | + char buffer[32] = { 0 }; |
| 109 | + printf("[%" PRIuz "] YUV420ToRGB_8u_P3AC4R %" PRIu32 "x%" PRIu32 " took %sns\n", x, |
| 110 | + bench->roi.width, bench->roi.height, print_time(diff, buffer, sizeof(buffer))); |
| 111 | + } |
| 112 | + |
| 113 | + return TRUE; |
| 114 | +} |
| 115 | + |
| 116 | +static BOOL primitives_YUV444_benchmark_run(primitives_YUV_benchmark* bench, primitives_t* prims) |
| 117 | +{ |
| 118 | + const BYTE* channels[3] = { 0 }; |
| 119 | + |
| 120 | + for (size_t i = 0; i < 3; i++) |
| 121 | + channels[i] = bench->channels[i]; |
| 122 | + |
| 123 | + for (size_t x = 0; x < 10; x++) |
| 124 | + { |
| 125 | + const UINT64 start = winpr_GetTickCount64NS(); |
| 126 | + pstatus_t status = |
| 127 | + prims->YUV444ToRGB_8u_P3AC4R(channels, bench->steps, bench->outputBuffer, |
| 128 | + bench->outputStride, bench->testedFormat, &bench->roi); |
| 129 | + const UINT64 end = winpr_GetTickCount64NS(); |
| 130 | + if (status != PRIMITIVES_SUCCESS) |
| 131 | + { |
| 132 | + (void)fprintf(stderr, "Running YUV444ToRGB_8u_P3AC4R failed\n"); |
| 133 | + return FALSE; |
| 134 | + } |
| 135 | + const UINT64 diff = end - start; |
| 136 | + char buffer[32] = { 0 }; |
| 137 | + printf("[%" PRIuz "] YUV444ToRGB_8u_P3AC4R %" PRIu32 "x%" PRIu32 " took %sns\n", x, |
| 138 | + bench->roi.width, bench->roi.height, print_time(diff, buffer, sizeof(buffer))); |
| 139 | + } |
| 140 | + |
| 141 | + return TRUE; |
| 142 | +} |
| 143 | + |
| 144 | +int main(int argc, char* argv[]) |
| 145 | +{ |
| 146 | + WINPR_UNUSED(argc); |
| 147 | + WINPR_UNUSED(argv); |
| 148 | + primitives_YUV_benchmark bench = primitives_YUV_benchmark_init(); |
| 149 | + primitives_t* opt = primitives_get(); |
| 150 | + primitives_t* generic = primitives_get_generic(); |
| 151 | + if (!generic || !opt) |
| 152 | + { |
| 153 | + (void)fprintf(stderr, "failed to get primitives: generic=%p, opt=%p\n", (void*)generic, |
| 154 | + (void*)opt); |
| 155 | + goto fail; |
| 156 | + } |
| 157 | + |
| 158 | + printf("Running YUV420 -> RGB benchmark on generic implementation:\n"); |
| 159 | + if (!primitives_YUV420_benchmark_run(&bench, generic)) |
| 160 | + { |
| 161 | + (void)fprintf(stderr, "YUV420 -> RGB benchmark failed\n"); |
| 162 | + goto fail; |
| 163 | + } |
| 164 | + printf("\n"); |
| 165 | + |
| 166 | + printf("Running YUV420 benchmark on optimized implementation:\n"); |
| 167 | + if (!primitives_YUV420_benchmark_run(&bench, opt)) |
| 168 | + { |
| 169 | + (void)fprintf(stderr, "YUV420 benchmark failed\n"); |
| 170 | + goto fail; |
| 171 | + } |
| 172 | + printf("\n"); |
| 173 | + |
| 174 | + printf("Running YUV444 -> RGB benchmark on generic implementation:\n"); |
| 175 | + if (!primitives_YUV444_benchmark_run(&bench, generic)) |
| 176 | + { |
| 177 | + (void)fprintf(stderr, "YUV444 -> RGB benchmark failed\n"); |
| 178 | + goto fail; |
| 179 | + } |
| 180 | + printf("\n"); |
| 181 | + |
| 182 | + printf("Running YUV444 -> RGB benchmark on optimized implementation:\n"); |
| 183 | + if (!primitives_YUV444_benchmark_run(&bench, opt)) |
| 184 | + { |
| 185 | + (void)fprintf(stderr, "YUV444 -> RGB benchmark failed\n"); |
| 186 | + goto fail; |
| 187 | + } |
| 188 | + printf("\n"); |
| 189 | +fail: |
| 190 | + primitives_YUV_benchmark_free(&bench); |
| 191 | + return 0; |
| 192 | +} |
0 commit comments