Skip to content

Commit 711459d

Browse files
authored
chore(🪨): Update Graphite build (#3170)
1 parent cd6749c commit 711459d

File tree

11 files changed

+205
-72
lines changed

11 files changed

+205
-72
lines changed

‎.github/workflows/build-skia-graphite.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ jobs:
6464
./externals/skia/out/android/arm/libskparagraph.a
6565
./externals/skia/out/android/arm/libskunicode_core.a
6666
./externals/skia/out/android/arm/libskunicode_icu.a
67-
./externals/skia/out/android/arm/libdawn_native_static.a
68-
./externals/skia/out/android/arm/libdawn_platform_static.a
69-
./externals/skia/out/android/arm/libdawn_proc_static.a
7067
7168
- name: Upload artifacts - Android arm64 (Graphite)
7269
if: github.ref == 'refs/heads/main'
@@ -82,9 +79,6 @@ jobs:
8279
./externals/skia/out/android/arm64/libskparagraph.a
8380
./externals/skia/out/android/arm64/libskunicode_core.a
8481
./externals/skia/out/android/arm64/libskunicode_icu.a
85-
./externals/skia/out/android/arm64/libdawn_native_static.a
86-
./externals/skia/out/android/arm64/libdawn_platform_static.a
87-
./externals/skia/out/android/arm64/libdawn_proc_static.a
8882
8983
- name: Upload artifacts - Android x86 (Graphite)
9084
if: github.ref == 'refs/heads/main'
@@ -100,9 +94,6 @@ jobs:
10094
./externals/skia/out/android/x86/libskparagraph.a
10195
./externals/skia/out/android/x86/libskunicode_core.a
10296
./externals/skia/out/android/x86/libskunicode_icu.a
103-
./externals/skia/out/android/x86/libdawn_native_static.a
104-
./externals/skia/out/android/x86/libdawn_platform_static.a
105-
./externals/skia/out/android/x86/libdawn_proc_static.a
10697
10798
- name: Upload artifacts - Android x64 (Graphite)
10899
if: github.ref == 'refs/heads/main'
@@ -118,9 +109,6 @@ jobs:
118109
./externals/skia/out/android/x64/libskparagraph.a
119110
./externals/skia/out/android/x64/libskunicode_core.a
120111
./externals/skia/out/android/x64/libskunicode_icu.a
121-
./externals/skia/out/android/x64/libdawn_native_static.a
122-
./externals/skia/out/android/x64/libdawn_platform_static.a
123-
./externals/skia/out/android/x64/libdawn_proc_static.a
124112
125113
- name: Upload artifacts - Apple xcframeworks (Graphite)
126114
if: github.ref == 'refs/heads/main'

‎apps/example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ SPEC CHECKSUMS:
21932193
React-Mapbuffer: 3c11cee7737609275c7b66bd0b1de475f094cedf
21942194
React-microtasksnativemodule: 843f352b32aacbe13a9c750190d34df44c3e6c2c
21952195
react-native-safe-area-context: 0f14bce545abcdfbff79ce2e3c78c109f0be283e
2196-
react-native-skia: cde44b3bdb773204750a79d01ee74b85480287b1
2196+
react-native-skia: 87d730aeeb02c54280ad5e42b443cc110a2e43ce
21972197
react-native-slider: bb7eb4732940fab78217e1c096bb647d8b0d1cf3
21982198
React-NativeModulesApple: 88433b6946778bea9c153e27b671de15411bf225
21992199
React-perflogger: 9e8d3c0dc0194eb932162812a168aa5dc662f418

‎packages/skia/android/CMakeLists.txt

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,50 @@ cmake_minimum_required(VERSION 3.4.1)
44
set (CMAKE_VERBOSE_MAKEFILE ON)
55
set (CMAKE_CXX_STANDARD 17)
66

7-
# Import prebuilt SKIA libraries path first to check for Dawn libraries
7+
# Import prebuilt SKIA libraries path
88
set (SKIA_LIBS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../libs/android/${ANDROID_ABI}")
99

10-
# Check if Dawn libraries exist, if so enable SK_GRAPHITE
11-
if(EXISTS "${SKIA_LIBS_PATH}/libdawn_native_static.a")
10+
# Import libskia first so we can check for symbols
11+
add_library(skia STATIC IMPORTED)
12+
set_property(TARGET skia PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libskia.a")
13+
14+
# Check if Graphite symbols are available in libskia using nm/objdump
15+
set(SK_GRAPHITE_AVAILABLE OFF)
16+
17+
if(EXISTS "${SKIA_LIBS_PATH}/libskia.a")
18+
# Look for specific Dawn function symbols that indicate Graphite support
19+
execute_process(
20+
COMMAND nm "${SKIA_LIBS_PATH}/libskia.a"
21+
COMMAND grep "dawn::\\|wgpu\\|_ZN4dawn\\|DawnDevice\\|dawn_native"
22+
OUTPUT_VARIABLE NM_OUTPUT
23+
ERROR_QUIET
24+
RESULT_VARIABLE NM_RESULT
25+
)
26+
27+
if(NM_RESULT EQUAL 0 AND NOT "${NM_OUTPUT}" STREQUAL "")
28+
set(SK_GRAPHITE_AVAILABLE ON)
29+
else()
30+
# Fallback to objdump if nm doesn't work
31+
execute_process(
32+
COMMAND objdump -t "${SKIA_LIBS_PATH}/libskia.a"
33+
COMMAND grep "dawn::\\|wgpu\\|_ZN4dawn\\|DawnDevice\\|dawn_native"
34+
OUTPUT_VARIABLE OBJDUMP_OUTPUT
35+
ERROR_QUIET
36+
RESULT_VARIABLE OBJDUMP_RESULT
37+
)
38+
39+
if(OBJDUMP_RESULT EQUAL 0 AND NOT "${OBJDUMP_OUTPUT}" STREQUAL "")
40+
set(SK_GRAPHITE_AVAILABLE ON)
41+
endif()
42+
endif()
43+
endif()
44+
45+
if(SK_GRAPHITE_AVAILABLE)
1246
set(SK_GRAPHITE ON)
13-
message("-- SK_GRAPHITE: ON (Dawn libraries found)")
47+
message("-- SK_GRAPHITE: ON (Graphite symbols found in libskia)")
1448
else()
1549
set(SK_GRAPHITE OFF)
16-
message("-- SK_GRAPHITE: OFF (Dawn libraries not found)")
50+
message("-- SK_GRAPHITE: OFF (Graphite symbols not found in libskia)")
1751
endif()
1852

1953
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSK_BUILD_FOR_ANDROID -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")
@@ -45,21 +79,10 @@ link_directories(../libs/android/${ANDROID_ABI}/)
4579

4680
if(SK_GRAPHITE)
4781
add_definitions(-DSK_GRAPHITE)
48-
set(DAWN_NATIVE_LIB "libdawn_native_static")
49-
set(DAWN_PLATFORM_LIB "libdawn_platform_static")
50-
set(DAWN_PROC_LIB "libdawn_proc_static")
5182
set(BACKEND_SOURCES
5283
#TODO: is this source needed to be added?
5384
"${PROJECT_SOURCE_DIR}/../cpp/rnskia/DawnWindowContext.cpp"
5485
)
55-
add_library(libdawn_native_static STATIC IMPORTED)
56-
set_property(TARGET libdawn_native_static PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libdawn_native_static.a")
57-
58-
add_library(libdawn_platform_static STATIC IMPORTED)
59-
set_property(TARGET libdawn_platform_static PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libdawn_platform_static.a")
60-
61-
add_library(libdawn_proc_static STATIC IMPORTED)
62-
set_property(TARGET libdawn_proc_static PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libdawn_proc_static.a")
6386
else()
6487
add_definitions(-DSK_GL -DSK_GANESH)
6588
set(BACKEND_SOURCES
@@ -131,9 +154,6 @@ target_include_directories(
131154
${libfbjni_include_DIRS}
132155
)
133156

134-
add_library(skia STATIC IMPORTED)
135-
set_property(TARGET skia PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libskia.a")
136-
137157
add_library(svg STATIC IMPORTED)
138158
set_property(TARGET svg PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libsvg.a")
139159

@@ -307,9 +327,6 @@ endif()
307327
if(SK_GRAPHITE)
308328
target_link_libraries(${PACKAGE_NAME}
309329
${COMMON_LIBS}
310-
${DAWN_NATIVE_LIB}
311-
${DAWN_PLATFORM_LIB}
312-
${DAWN_PROC_LIB}
313330
)
314331
else()
315332
target_link_libraries(${PACKAGE_NAME}

‎packages/skia/apple/MetalWindowContext.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
_layer.drawableSize = CGSizeMake(width, height);
2626
BOOL supportsWideColor = NO;
2727
if (@available(iOS 10.0, *)) {
28-
supportsWideColor = [UIScreen mainScreen].traitCollection.displayGamut == UIDisplayGamutP3;
28+
supportsWideColor =
29+
[UIScreen mainScreen].traitCollection.displayGamut == UIDisplayGamutP3;
2930
}
3031
if (supportsWideColor) {
31-
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
32+
CGColorSpaceRef colorSpace =
33+
CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
3234
_layer.colorspace = colorSpace;
3335
CGColorSpaceRelease(colorSpace);
3436
}

‎packages/skia/apple/RNSkApplePlatformContext.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
191191
throw std::runtime_error("Couldn't get Metal texture info");
192192
}
193-
result.mtlTexture = textureInfo.fTexture.get();
193+
result.mtlTexture = textureInfo.fTexture.get();
194194
return result;
195195
}
196196

‎packages/skia/cpp/api/JsiSkImageFactory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ class JsiSkImageFactory : public JsiSkHostObject {
9393
}
9494
if (count > 4 && arguments[4].isObject() &&
9595
arguments[4].asObject(runtime).isHostObject(runtime)) {
96-
auto jsiImage = arguments[4].asObject(runtime).asHostObject<JsiSkImage>(
97-
runtime);
96+
auto jsiImage =
97+
arguments[4].asObject(runtime).asHostObject<JsiSkImage>(runtime);
9898
jsiImage->setObject(image);
9999
return jsi::Value(runtime, arguments[4]);
100100
}

‎packages/skia/cpp/api/JsiSkSurface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class JsiSkSurface : public JsiSkWrappingSkPtrHostObject<SkSurface> {
7676
DawnContext::getInstance().submitRecording(recording.get());
7777
#endif
7878
if (count > 1 && arguments[1].isObject()) {
79-
auto jsiImage = arguments[1].asObject(runtime).asHostObject<JsiSkImage>(runtime);
79+
auto jsiImage =
80+
arguments[1].asObject(runtime).asHostObject<JsiSkImage>(runtime);
8081
jsiImage->setObject(image);
8182
return jsi::Value(runtime, arguments[1]);
8283
}

‎packages/skia/cpp/rnskia/DawnContext.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,16 @@ class DawnContext {
221221
}
222222
}
223223

224-
void tick() { backendContext.fTick(backendContext.fInstance); }
224+
~DawnContext() {
225+
backendContext.fDevice = nullptr;
226+
tick();
227+
}
228+
229+
void tick() {
230+
if (backendContext.fTick) {
231+
backendContext.fTick(backendContext.fInstance);
232+
}
233+
}
225234

226235
skgpu::graphite::Recorder *getRecorder() {
227236
static thread_local skgpu::graphite::RecorderOptions recorderOptions;

‎packages/skia/cpp/rnskia/DawnUtils.h

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "dawn/dawn_proc.h"
66
#include "dawn/native/DawnNative.h"
77

8+
#include "RNSkLog.h"
89
#include "include/core/SkColorType.h"
910
#include "include/gpu/graphite/dawn/DawnBackendContext.h"
1011

@@ -25,23 +26,29 @@ createDawnBackendContext(dawn::native::Instance *instance) {
2526

2627
auto useTintIR = false;
2728
static constexpr const char *kToggles[] = {
29+
#if !defined(SK_DEBUG)
30+
"skip_validation",
31+
#endif
32+
"disable_lazy_clear_for_mapped_at_creation_buffer",
2833
"allow_unsafe_apis",
2934
"use_user_defined_labels_in_backend",
3035
"disable_robustness",
3136
"use_tint_ir",
3237
};
3338
wgpu::DawnTogglesDescriptor togglesDesc;
34-
togglesDesc.enabledToggleCount =
35-
(sizeof(kToggles) / sizeof(kToggles[0])) - (useTintIR ? 0 : 1);
39+
togglesDesc.enabledToggleCount = std::size(kToggles) - (useTintIR ? 0 : 1);
3640
togglesDesc.enabledToggles = kToggles;
3741

42+
dawn::native::Adapter matchedAdaptor;
43+
3844
wgpu::RequestAdapterOptions options;
3945
#ifdef __APPLE__
4046
constexpr auto kDefaultBackendType = wgpu::BackendType::Metal;
4147
#elif __ANDROID__
4248
constexpr auto kDefaultBackendType = wgpu::BackendType::Vulkan;
4349
#endif
4450
options.backendType = kDefaultBackendType;
51+
options.featureLevel = wgpu::FeatureLevel::Core;
4552
options.nextInChain = &togglesDesc;
4653

4754
std::vector<dawn::native::Adapter> adapters =
@@ -50,7 +57,78 @@ createDawnBackendContext(dawn::native::Instance *instance) {
5057
throw std::runtime_error("No matching adapter found");
5158
}
5259

53-
wgpu::Adapter adapter = adapters[0].Get();
60+
// Sort adapters by adapterType(DiscreteGPU, IntegratedGPU, CPU) and
61+
// backendType(Metal, Vulkan, OpenGL, OpenGLES, WebGPU).
62+
std::sort(adapters.begin(), adapters.end(),
63+
[](dawn::native::Adapter a, dawn::native::Adapter b) {
64+
wgpu::Adapter wgpuA = a.Get();
65+
wgpu::Adapter wgpuB = b.Get();
66+
wgpu::AdapterInfo infoA;
67+
wgpu::AdapterInfo infoB;
68+
wgpuA.GetInfo(&infoA);
69+
wgpuB.GetInfo(&infoB);
70+
return std::tuple(infoA.adapterType, infoA.backendType) <
71+
std::tuple(infoB.adapterType, infoB.backendType);
72+
});
73+
74+
for (const auto &adapter : adapters) {
75+
wgpu::Adapter wgpuAdapter = adapter.Get();
76+
wgpu::AdapterInfo props;
77+
wgpuAdapter.GetInfo(&props);
78+
if (kDefaultBackendType == props.backendType) {
79+
matchedAdaptor = adapter;
80+
break;
81+
}
82+
}
83+
84+
if (!matchedAdaptor) {
85+
throw std::runtime_error("No matching adapter found");
86+
}
87+
88+
wgpu::Adapter adapter = matchedAdaptor.Get();
89+
90+
// Log selected adapter info
91+
wgpu::AdapterInfo adapterInfo;
92+
adapter.GetInfo(&adapterInfo);
93+
std::string deviceName =
94+
adapterInfo.device.data
95+
? std::string(adapterInfo.device.data, adapterInfo.device.length)
96+
: "Unknown";
97+
std::string description = adapterInfo.description.data
98+
? std::string(adapterInfo.description.data,
99+
adapterInfo.description.length)
100+
: "Unknown";
101+
102+
std::string backendName;
103+
switch (adapterInfo.backendType) {
104+
case wgpu::BackendType::Metal:
105+
backendName = "Metal";
106+
break;
107+
case wgpu::BackendType::Vulkan:
108+
backendName = "Vulkan";
109+
break;
110+
case wgpu::BackendType::OpenGL:
111+
backendName = "OpenGL";
112+
break;
113+
case wgpu::BackendType::OpenGLES:
114+
backendName = "OpenGLES";
115+
break;
116+
case wgpu::BackendType::WebGPU:
117+
backendName = "WebGPU";
118+
break;
119+
case wgpu::BackendType::Null:
120+
backendName = "Null";
121+
break;
122+
default:
123+
backendName = "Undefined (" +
124+
std::to_string(static_cast<int>(adapterInfo.backendType)) +
125+
")";
126+
break;
127+
}
128+
129+
RNSkia::RNSkLogger::logToConsole(
130+
"Selected Dawn adapter - Backend: %s, Device: %s, Description: %s",
131+
backendName.c_str(), deviceName.c_str(), description.c_str());
54132

55133
std::vector<wgpu::FeatureName> features;
56134
if (adapter.HasFeature(wgpu::FeatureName::MSAARenderToSingleSampled)) {
@@ -86,6 +164,15 @@ createDawnBackendContext(dawn::native::Instance *instance) {
86164
if (adapter.HasFeature(wgpu::FeatureName::DawnPartialLoadResolveTexture)) {
87165
features.push_back(wgpu::FeatureName::DawnPartialLoadResolveTexture);
88166
}
167+
if (adapter.HasFeature(wgpu::FeatureName::TimestampQuery)) {
168+
features.push_back(wgpu::FeatureName::TimestampQuery);
169+
}
170+
if (adapter.HasFeature(wgpu::FeatureName::DawnTexelCopyBufferRowAlignment)) {
171+
features.push_back(wgpu::FeatureName::DawnTexelCopyBufferRowAlignment);
172+
}
173+
if (adapter.HasFeature(wgpu::FeatureName::ImplicitDeviceSynchronization)) {
174+
features.push_back(wgpu::FeatureName::ImplicitDeviceSynchronization);
175+
}
89176
#ifdef __APPLE__
90177
if (adapter.HasFeature(wgpu::FeatureName::SharedTextureMemoryIOSurface)) {
91178
features.push_back(wgpu::FeatureName::SharedTextureMemoryIOSurface);
@@ -101,8 +188,23 @@ createDawnBackendContext(dawn::native::Instance *instance) {
101188
desc.requiredFeatureCount = features.size();
102189
desc.requiredFeatures = features.data();
103190
desc.nextInChain = &togglesDesc;
104-
105-
wgpu::Device device = adapter.CreateDevice(&desc);
191+
desc.SetDeviceLostCallback(
192+
wgpu::CallbackMode::AllowSpontaneous,
193+
[](const wgpu::Device &, wgpu::DeviceLostReason reason,
194+
wgpu::StringView message) {
195+
if (reason != wgpu::DeviceLostReason::Destroyed) {
196+
SK_ABORT("Device lost: %.*s\n", static_cast<int>(message.length),
197+
message.data);
198+
}
199+
});
200+
desc.SetUncapturedErrorCallback(
201+
[](const wgpu::Device &, wgpu::ErrorType, wgpu::StringView message) {
202+
SkDebugf("Device error: %.*s\n", static_cast<int>(message.length),
203+
message.data);
204+
});
205+
206+
wgpu::Device device =
207+
wgpu::Device::Acquire(matchedAdaptor.CreateDevice(&desc));
106208
SkASSERT(device);
107209

108210
skgpu::graphite::DawnBackendContext backendContext;

0 commit comments

Comments
 (0)