Skip to content

Commit 4106e96

Browse files
authored
chore(🗿): improve automatic graphite detection (#3575)
1 parent 2d9811d commit 4106e96

File tree

4 files changed

+59
-58
lines changed

4 files changed

+59
-58
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ For detailed information on library development, building, testing, and contribu
2323
## Graphite
2424

2525
Skia has two backends: Ganesh and Graphite. Ganesh is the default backend.
26-
If you want to experiment with Graphite, you can enable it by building Skia with `SK_GRAPHITE=1 yarn build-skia`.
26+
If you want to experiment with Graphite, you can enable it by building Skia with `SK_GRAPHITE=1 yarn build-skia`.
2727
Skia Graphite requires Android API Level 26 or above.
2828

29-
React Native Skia automatically detects if the Dawn WebGPU implementation library files are available. If they are present, it will automatically assume that Graphite is enabled.
29+
React Native Skia automatically detects Graphite via a marker file included in the prebuilt libs.
3030

‎packages/skia/android/CMakeLists.txt‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ set (SKIA_LIBS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../libs/android/${ANDROID_ABI}"
1111
add_library(skia STATIC IMPORTED)
1212
set_property(TARGET skia PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libskia.a")
1313

14-
# Check if Graphite symbols are available in libskia using nm
14+
# Check if Graphite is available
15+
# Detection method priority:
16+
# 1. SK_GRAPHITE environment variable (explicit override, fastest)
17+
# 2. Marker file in libs directory (set during Skia build)
18+
# 3. Fall back to nm symbol detection (slow on some CI systems)
1519
set(SK_GRAPHITE_AVAILABLE OFF)
1620

17-
if(EXISTS "${SKIA_LIBS_PATH}/libskia.a")
18-
# Look for specific Dawn function symbols that indicate Graphite support
19-
# Check each symbol individually for better reliability
20-
set(DAWN_SYMBOLS "dawn::" "wgpu" "_ZN4dawn" "DawnDevice" "dawn_native")
21-
22-
foreach(SYMBOL ${DAWN_SYMBOLS})
23-
execute_process(
24-
COMMAND sh -c "nm '${SKIA_LIBS_PATH}/libskia.a' 2>/dev/null | grep '${SYMBOL}'"
25-
OUTPUT_VARIABLE NM_OUTPUT
26-
ERROR_QUIET
27-
RESULT_VARIABLE NM_RESULT
28-
)
29-
30-
if(NM_RESULT EQUAL 0 AND NOT "${NM_OUTPUT}" STREQUAL "")
31-
set(SK_GRAPHITE_AVAILABLE ON)
32-
break()
33-
endif()
34-
endforeach()
21+
if(DEFINED ENV{SK_GRAPHITE})
22+
# Explicit override via environment variable
23+
if($ENV{SK_GRAPHITE})
24+
set(SK_GRAPHITE_AVAILABLE ON)
25+
message("-- SK_GRAPHITE detection: using environment variable (ON)")
26+
else()
27+
message("-- SK_GRAPHITE detection: using environment variable (OFF)")
28+
endif()
29+
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../libs/android/graphite.enabled")
30+
# Marker file indicates Graphite-enabled build
31+
set(SK_GRAPHITE_AVAILABLE ON)
32+
message("-- SK_GRAPHITE detection: marker file found")
33+
else()
34+
message("-- SK_GRAPHITE detection: no marker file, assuming OFF")
3535
endif()
3636

3737
if(SK_GRAPHITE_AVAILABLE)
3838
set(SK_GRAPHITE ON)
39-
message("-- SK_GRAPHITE: ON (Graphite symbols found in libskia)")
39+
message("-- SK_GRAPHITE: ON")
4040
else()
4141
set(SK_GRAPHITE OFF)
42-
message("-- SK_GRAPHITE: OFF (Graphite symbols not found in libskia)")
42+
message("-- SK_GRAPHITE: OFF")
4343
endif()
4444

4545
string(APPEND CMAKE_CXX_FLAGS " -DSK_BUILD_FOR_ANDROID -DSK_DISABLE_LEGACY_SHAPER_FACTORY -DSK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID")

‎packages/skia/react-native-skia.podspec‎

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,29 @@ require "json"
44

55
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
66

7-
# Check if Graphite symbols are available in libskia
8-
libskia_path = File.join(__dir__, "libs/apple/libskia.xcframework")
7+
# Check if Graphite is available
8+
# Detection method priority:
9+
# 1. SK_GRAPHITE environment variable (explicit override, fastest)
10+
# 2. Marker file in libs directory (set during Skia build)
11+
# 3. Default to OFF (no slow nm symbol detection)
912
use_graphite = false
1013

11-
if File.exist?(libskia_path)
12-
# Look for any arm64 or x86_64 framework inside the xcframework
13-
framework_paths = Dir.glob(File.join(libskia_path, "**/libskia.framework/libskia"))
14-
15-
# Also try looking for static libraries if frameworks aren't found
16-
if framework_paths.empty?
17-
framework_paths = Dir.glob(File.join(libskia_path, "**/libskia.a"))
18-
end
19-
20-
framework_paths.each do |framework_path|
21-
if File.exist?(framework_path)
22-
# Look for specific Dawn function symbols that indicate Graphite support
23-
dawn_symbols = [
24-
'dawn::',
25-
'wgpu',
26-
'_ZN4dawn',
27-
'DawnDevice',
28-
'dawn_native'
29-
]
30-
31-
dawn_symbols.each do |symbol|
32-
nm_output = `nm "#{framework_path}" 2>/dev/null | grep "#{symbol}"`
33-
if $?.success? && !nm_output.empty?
34-
use_graphite = true
35-
break
36-
end
37-
end
38-
39-
break if use_graphite
40-
end
41-
end
14+
if ENV['SK_GRAPHITE']
15+
# Explicit override via environment variable
16+
use_graphite = ENV['SK_GRAPHITE'] == '1' || ENV['SK_GRAPHITE'].downcase == 'true'
17+
puts "-- SK_GRAPHITE detection: using environment variable (#{use_graphite ? 'ON' : 'OFF'})"
18+
elsif File.exist?(File.join(__dir__, "libs/apple/graphite.enabled"))
19+
# Marker file indicates Graphite-enabled build
20+
use_graphite = true
21+
puts "-- SK_GRAPHITE detection: marker file found"
22+
else
23+
puts "-- SK_GRAPHITE detection: no marker file, assuming OFF"
4224
end
4325

4426
if use_graphite
45-
puts "SK_GRAPHITE: ON (Graphite symbols found in libskia)"
27+
puts "-- SK_GRAPHITE: ON"
4628
else
47-
puts "SK_GRAPHITE: OFF (Graphite symbols not found in libskia)"
29+
puts "-- SK_GRAPHITE: OFF"
4830
end
4931

5032
# Set preprocessor definitions based on GRAPHITE flag

‎packages/skia/scripts/install-skia.mjs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,25 @@ const main = async () => {
487487
});
488488
}
489489

490+
// Create or remove Graphite marker files based on build type
491+
const androidMarkerFile = path.join(androidDir, "graphite.enabled");
492+
const appleMarkerFile = path.join(appleDir, "graphite.enabled");
493+
494+
if (GRAPHITE) {
495+
// Create marker files for Graphite builds
496+
fs.writeFileSync(androidMarkerFile, "");
497+
fs.writeFileSync(appleMarkerFile, "");
498+
console.log("✓ Created Graphite marker files");
499+
} else {
500+
// Ensure marker files don't exist for non-Graphite builds
501+
if (fs.existsSync(androidMarkerFile)) {
502+
fs.unlinkSync(androidMarkerFile);
503+
}
504+
if (fs.existsSync(appleMarkerFile)) {
505+
fs.unlinkSync(appleMarkerFile);
506+
}
507+
}
508+
490509
// Copy Graphite headers if using Graphite
491510
if (GRAPHITE) {
492511
console.log("📦 Copying Graphite headers...");

0 commit comments

Comments
 (0)