Skip to content

Commit

Permalink
Add starboardized CPU detection to WebP (youtube#835)
Browse files Browse the repository at this point in the history
* Add Starboardized CPU detection to WebP

b/290060548
  • Loading branch information
kaidokert committed Jul 10, 2023
1 parent a2755fb commit 4e1e814
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
28 changes: 28 additions & 0 deletions third_party/libwebp/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ libwebp_lib("libwebp_dsp_dec_common") {
":libwebp_dsp_dec_sse2",
":libwebp_dsp_dec_sse41",
]
if (is_starboard) {
deps += [ "//starboard:starboard_headers_only" ]
}
}

libwebp_lib("libwebp_dsp_dec_msa") {
Expand Down Expand Up @@ -87,6 +90,31 @@ libwebp_lib("libwebp_utils_enc") {

config("libwebp_direct_config") {
include_dirs = [ "." ]
if (is_starboard) {
if (current_cpu == "x64" || current_cpu == "x86") {
cflags = [
"-msse2",
"-msse4.1",
]
defines = [
"WEBP_HAVE_SSE2",
"WEBP_HAVE_SSE41",
]
}

# This crashes on x86/Atom, enable on 64-bit only.
if (current_cpu == "x64") {
cflags += [
"-mavx",
"-mavx2",
]
defines += [ "WEBP_HAVE_AVX2" ]
}
if (current_cpu == "arm" || current_cpu == "arm64") {
defines = [ "WEBP_HAVE_NEON" ]
cflags = [ "-mfpu=neon" ]
}
}
}

group("libwebp") {
Expand Down
47 changes: 47 additions & 0 deletions third_party/libwebp/src/dsp/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,51 @@

#include "src/dsp/dsp.h"

#if defined(STARBOARD)

#include "starboard/cpu_features.h"
#include "starboard/log.h"
#include "starboard/system.h"

bool warn_if_not_enabled(const char* name, bool value) {
if (!value) {
SbLogFormatF("LibWebP optimization not enabled: %s\n", name);
SbLogFlush();
}
return value;
}

static int StarboardGetCPUInfo(CPUFeature feature) {
SbCPUFeatures features;
if (SbCPUFeaturesGet(&features)) {
switch (feature) {
case kSSE2:
return warn_if_not_enabled("sse2", features.x86.has_sse2);
case kSSE3:
return warn_if_not_enabled("sse3", features.x86.has_sse3);
case kSSE4_1:
return warn_if_not_enabled("sse41", features.x86.has_sse41);
case kAVX:
return warn_if_not_enabled("avx", features.x86.has_avx);
case kAVX2:
return warn_if_not_enabled("avx2", features.x86.has_avx2);
case kNEON: {
return warn_if_not_enabled("neon", features.arm.has_neon);
}
default:
return 0;
}
} else {
SbLogFormatF("LibWebP CPU feature detection failed\n");
SbLogFlush();
}
return 0;
}

VP8CPUInfo VP8GetCPUInfo = StarboardGetCPUInfo;

#else // STARBOARD

#if defined(WEBP_HAVE_NEON_RTCD)
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -220,3 +265,5 @@ VP8CPUInfo VP8GetCPUInfo = mipsCPUInfo;
#else
VP8CPUInfo VP8GetCPUInfo = NULL;
#endif

#endif // STARBOARD

0 comments on commit 4e1e814

Please sign in to comment.