From 769387c54a2507bced932ba58ea7020acc0ef9cd Mon Sep 17 00:00:00 2001 From: James Zern Date: Fri, 21 Oct 2022 15:37:12 -0700 Subject: [PATCH 1/2] cpu.c,cosmetics: fix a typo VP8DecGetCPUInfo -> VP8GetCPUInfo Change-Id: Ic59d23a2964a881b853db62b3617117bf10ec66d --- src/dsp/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsp/cpu.c b/src/dsp/cpu.c index a4ba7f2c..62de73f7 100644 --- a/src/dsp/cpu.c +++ b/src/dsp/cpu.c @@ -212,7 +212,7 @@ VP8CPUInfo VP8GetCPUInfo = wasmCPUInfo; #elif defined(WEBP_HAVE_NEON) // In most cases this function doesn't check for NEON support (it's assumed by // the configuration), but enables turning off NEON at runtime, for testing -// purposes, by setting VP8DecGetCPUInfo = NULL. +// purposes, by setting VP8GetCPUInfo = NULL. static int armCPUInfo(CPUFeature feature) { if (feature != kNEON) return 0; #if defined(__linux__) && defined(WEBP_HAVE_NEON_RTCD) From 979c0ebbcd95c06acc8b46a693e544b859cee0de Mon Sep 17 00:00:00 2001 From: James Zern Date: Fri, 21 Oct 2022 19:04:53 -0700 Subject: [PATCH 2/2] sharpyuv: add SharpYuvGetCPUInfo This gives a similar structure to libwebp and fixes a bug where passing NULL to SharpYuvInit() would unconditionally set optimized function pointers. SharpYuvInit() is left as an undocumented public function and SharpYuvGetCPUInfo is kept private to serialize updates to the pointer. Change-Id: Id72fbf3ba5b396367510e3bcd1ee2e4e11b95b8c --- Android.mk | 1 + CMakeLists.txt | 8 ++++++-- Makefile.vc | 1 + build.gradle | 1 + makefile.unix | 4 +++- sharpyuv/Makefile.am | 2 ++ sharpyuv/sharpyuv.c | 29 +++++++++++++---------------- sharpyuv/sharpyuv_cpu.c | 14 ++++++++++++++ sharpyuv/sharpyuv_cpu.h | 22 ++++++++++++++++++++++ sharpyuv/sharpyuv_dsp.c | 17 +++++++++-------- sharpyuv/sharpyuv_dsp.h | 3 +-- 11 files changed, 73 insertions(+), 29 deletions(-) create mode 100644 sharpyuv/sharpyuv_cpu.c create mode 100644 sharpyuv/sharpyuv_cpu.h diff --git a/Android.mk b/Android.mk index 85ba5c6b..9215c901 100644 --- a/Android.mk +++ b/Android.mk @@ -35,6 +35,7 @@ endif sharpyuv_srcs := \ sharpyuv/sharpyuv.c \ + sharpyuv/sharpyuv_cpu.c \ sharpyuv/sharpyuv_csp.c \ sharpyuv/sharpyuv_dsp.c \ sharpyuv/sharpyuv_gamma.c \ diff --git a/CMakeLists.txt b/CMakeLists.txt index c0ef1022..85c6abba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,7 @@ if(WEBP_BUILD_WEBP_JS) endif() set(SHARPYUV_DEP_LIBRARIES) +set(SHARPYUV_DEP_INCLUDE_DIRS) set(WEBP_DEP_LIBRARIES) set(WEBP_DEP_INCLUDE_DIRS) @@ -151,9 +152,12 @@ if(ANDROID) ${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c) list(APPEND INSTALLED_LIBRARIES cpufeatures-webp) target_link_libraries(cpufeatures-webp dl) + set(SHARPYUV_DEP_LIBRARIES ${SHARPYUV_DEP_LIBRARIES} cpufeatures-webp) set(WEBP_DEP_LIBRARIES ${WEBP_DEP_LIBRARIES} cpufeatures-webp) - set(WEBP_DEP_INCLUDE_DIRS ${WEBP_DEP_INCLUDE_DIRS} - ${ANDROID_NDK}/sources/android/cpufeatures) + set(cpufeatures_include_dir ${ANDROID_NDK}/sources/android/cpufeatures) + set(SHARPYUV_DEP_INCLUDE_DIRS ${SHARPYUV_DEP_INCLUDE_DIRS} + ${cpufeatures_include_dir}) + set(WEBP_DEP_INCLUDE_DIRS ${WEBP_DEP_INCLUDE_DIRS} ${cpufeatures_include_dir}) add_definitions(-DHAVE_CPU_FEATURES_H=1) set(HAVE_CPU_FEATURES_H 1) else() diff --git a/Makefile.vc b/Makefile.vc index 5881adc4..8e3aed45 100644 --- a/Makefile.vc +++ b/Makefile.vc @@ -180,6 +180,7 @@ CFLAGS = $(CFLAGS) /D_UNICODE /DUNICODE SHARPYUV_OBJS = \ $(DIROBJ)\sharpyuv\sharpyuv.obj \ + $(DIROBJ)\sharpyuv\sharpyuv_cpu.obj \ $(DIROBJ)\sharpyuv\sharpyuv_csp.obj \ $(DIROBJ)\sharpyuv\sharpyuv_dsp.obj \ $(DIROBJ)\sharpyuv\sharpyuv_gamma.obj \ diff --git a/build.gradle b/build.gradle index 13202a85..c25314c0 100644 --- a/build.gradle +++ b/build.gradle @@ -107,6 +107,7 @@ model { source { srcDir "sharpyuv" include "sharpyuv.c" + include "sharpyuv_cpu.c" include "sharpyuv_csp.c" include "sharpyuv_dsp.c" include "sharpyuv_gamma.c" diff --git a/makefile.unix b/makefile.unix index 854f3342..8348d108 100644 --- a/makefile.unix +++ b/makefile.unix @@ -128,6 +128,7 @@ ANIM_UTIL_OBJS = \ SHARPYUV_OBJS = \ sharpyuv/sharpyuv.o \ + sharpyuv/sharpyuv_cpu.o \ sharpyuv/sharpyuv_csp.o \ sharpyuv/sharpyuv_dsp.o \ sharpyuv/sharpyuv_gamma.o \ @@ -308,7 +309,8 @@ HDRS_INSTALLED = \ SHARPYUV_HDRS_INSTALLED = \ sharpyuv/sharpyuv.h \ - sharpyuv/sharpyuv_csp.h + sharpyuv/sharpyuv_cpu.h \ + sharpyuv/sharpyuv_csp.h \ HDRS = \ src/dec/alphai_dec.h \ diff --git a/sharpyuv/Makefile.am b/sharpyuv/Makefile.am index 48801e06..b6b46758 100644 --- a/sharpyuv/Makefile.am +++ b/sharpyuv/Makefile.am @@ -11,6 +11,7 @@ libsharpyuvinclude_HEADERS = libsharpyuvinclude_HEADERS += sharpyuv.h libsharpyuvinclude_HEADERS += sharpyuv_csp.h noinst_HEADERS = +noinst_HEADERS += ../src/dsp/cpu.c noinst_HEADERS += ../src/dsp/cpu.h noinst_HEADERS += ../src/webp/types.h @@ -25,6 +26,7 @@ libsharpyuv_neon_la_CPPFLAGS = $(libsharpyuv_la_CPPFLAGS) libsharpyuv_neon_la_CFLAGS = $(AM_CFLAGS) $(NEON_FLAGS) libsharpyuv_la_SOURCES = +libsharpyuv_la_SOURCES += sharpyuv_cpu.c sharpyuv_cpu.h libsharpyuv_la_SOURCES += sharpyuv_csp.c sharpyuv_csp.h libsharpyuv_la_SOURCES += sharpyuv_dsp.c sharpyuv_dsp.h libsharpyuv_la_SOURCES += sharpyuv_gamma.c sharpyuv_gamma.h diff --git a/sharpyuv/sharpyuv.c b/sharpyuv/sharpyuv.c index 5ff02225..6585041e 100644 --- a/sharpyuv/sharpyuv.c +++ b/sharpyuv/sharpyuv.c @@ -20,7 +20,7 @@ #include #include "src/webp/types.h" -#include "src/dsp/cpu.h" +#include "sharpyuv/sharpyuv_cpu.h" #include "sharpyuv/sharpyuv_dsp.h" #include "sharpyuv/sharpyuv_gamma.h" @@ -431,26 +431,23 @@ static int DoSharpArgbToYuv(const uint8_t* r_ptr, const uint8_t* g_ptr, #endif // defined(WEBP_USE_THREAD) && !defined(_WIN32) // Hidden exported init function. -// By default SharpYuvConvert calls it with NULL. If needed, users can declare -// it as extern and call it with a VP8CPUInfo function. +// By default SharpYuvConvert calls it with SharpYuvGetCPUInfo. If needed, +// users can declare it as extern and call it with an alternate VP8CPUInfo +// function. SHARPYUV_EXTERN void SharpYuvInit(VP8CPUInfo cpu_info_func); void SharpYuvInit(VP8CPUInfo cpu_info_func) { static volatile VP8CPUInfo sharpyuv_last_cpuinfo_used = (VP8CPUInfo)&sharpyuv_last_cpuinfo_used; LOCK_ACCESS; - { - const int initialized = - (sharpyuv_last_cpuinfo_used != (VP8CPUInfo)&sharpyuv_last_cpuinfo_used); - if (cpu_info_func == NULL && initialized) UNLOCK_ACCESS_AND_RETURN; - if (sharpyuv_last_cpuinfo_used == cpu_info_func) UNLOCK_ACCESS_AND_RETURN; - - SharpYuvInitDsp(cpu_info_func); - if (!initialized) { - SharpYuvInitGammaTables(); - } - - sharpyuv_last_cpuinfo_used = cpu_info_func; + SharpYuvGetCPUInfo = cpu_info_func; + if (sharpyuv_last_cpuinfo_used == SharpYuvGetCPUInfo) { + UNLOCK_ACCESS_AND_RETURN; } + + SharpYuvInitDsp(); + SharpYuvInitGammaTables(); + + sharpyuv_last_cpuinfo_used = SharpYuvGetCPUInfo; UNLOCK_ACCESS_AND_RETURN; } @@ -487,7 +484,7 @@ int SharpYuvConvert(const void* r_ptr, const void* g_ptr, // Stride should be even for uint16_t buffers. return 0; } - SharpYuvInit(NULL); + SharpYuvInit(SharpYuvGetCPUInfo); // Add scaling factor to go from rgb_bit_depth to yuv_bit_depth, to the // rgb->yuv conversion matrix. diff --git a/sharpyuv/sharpyuv_cpu.c b/sharpyuv/sharpyuv_cpu.c new file mode 100644 index 00000000..29425a0c --- /dev/null +++ b/sharpyuv/sharpyuv_cpu.c @@ -0,0 +1,14 @@ +// Copyright 2022 Google Inc. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the COPYING file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +// ----------------------------------------------------------------------------- +// +#include "sharpyuv/sharpyuv_cpu.h" + +// Include src/dsp/cpu.c to create SharpYuvGetCPUInfo from VP8GetCPUInfo. The +// function pointer is renamed in sharpyuv_cpu.h. +#include "src/dsp/cpu.c" diff --git a/sharpyuv/sharpyuv_cpu.h b/sharpyuv/sharpyuv_cpu.h new file mode 100644 index 00000000..176ca3eb --- /dev/null +++ b/sharpyuv/sharpyuv_cpu.h @@ -0,0 +1,22 @@ +// Copyright 2022 Google Inc. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the COPYING file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +// ----------------------------------------------------------------------------- +// +#ifndef WEBP_SHARPYUV_SHARPYUV_CPU_H_ +#define WEBP_SHARPYUV_SHARPYUV_CPU_H_ + +#include "sharpyuv/sharpyuv.h" + +// Avoid exporting SharpYuvGetCPUInfo in shared object / DLL builds. +// SharpYuvInit() replaces the use of the function pointer. +#undef WEBP_EXTERN +#define WEBP_EXTERN extern +#define VP8GetCPUInfo SharpYuvGetCPUInfo +#include "src/dsp/cpu.h" + +#endif // WEBP_SHARPYUV_SHARPYUV_CPU_H_ diff --git a/sharpyuv/sharpyuv_dsp.c b/sharpyuv/sharpyuv_dsp.c index 956fa7ce..31c272c4 100644 --- a/sharpyuv/sharpyuv_dsp.c +++ b/sharpyuv/sharpyuv_dsp.c @@ -16,7 +16,7 @@ #include #include -#include "src/dsp/cpu.h" +#include "sharpyuv/sharpyuv_cpu.h" //----------------------------------------------------------------------------- @@ -75,23 +75,24 @@ void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len, extern void InitSharpYuvSSE2(void); extern void InitSharpYuvNEON(void); -void SharpYuvInitDsp(VP8CPUInfo cpu_info_func) { - (void)cpu_info_func; - +void SharpYuvInitDsp(void) { #if !WEBP_NEON_OMIT_C_CODE SharpYuvUpdateY = SharpYuvUpdateY_C; SharpYuvUpdateRGB = SharpYuvUpdateRGB_C; SharpYuvFilterRow = SharpYuvFilterRow_C; #endif + if (SharpYuvGetCPUInfo != NULL) { #if defined(WEBP_HAVE_SSE2) - if (cpu_info_func == NULL || cpu_info_func(kSSE2)) { - InitSharpYuvSSE2(); - } + if (SharpYuvGetCPUInfo(kSSE2)) { + InitSharpYuvSSE2(); + } #endif // WEBP_HAVE_SSE2 + } #if defined(WEBP_HAVE_NEON) - if (WEBP_NEON_OMIT_C_CODE || cpu_info_func == NULL || cpu_info_func(kNEON)) { + if (WEBP_NEON_OMIT_C_CODE || + (SharpYuvGetCPUInfo != NULL && SharpYuvGetCPUInfo(kNEON))) { InitSharpYuvNEON(); } #endif // WEBP_HAVE_NEON diff --git a/sharpyuv/sharpyuv_dsp.h b/sharpyuv/sharpyuv_dsp.h index c7755b97..18e8de38 100644 --- a/sharpyuv/sharpyuv_dsp.h +++ b/sharpyuv/sharpyuv_dsp.h @@ -12,7 +12,6 @@ #ifndef WEBP_SHARPYUV_SHARPYUV_DSP_H_ #define WEBP_SHARPYUV_SHARPYUV_DSP_H_ -#include "src/dsp/cpu.h" #include "src/webp/types.h" extern uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref, @@ -23,6 +22,6 @@ extern void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len, const uint16_t* best_y, uint16_t* out, int bit_depth); -void SharpYuvInitDsp(VP8CPUInfo cpu_info_func); +void SharpYuvInitDsp(void); #endif // WEBP_SHARPYUV_SHARPYUV_DSP_H_