Skip to content

Commit 52ed82e

Browse files
authored
chore(🪨): Update graphite build (#3160)
1 parent f53e43f commit 52ed82e

15 files changed

+223
-164
lines changed

‎README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,8 @@ Again, since `eval`, `draw`, and `drawOffscreen` serialize the function's conten
155155
## Graphite
156156

157157
Skia has two backends: Ganesh and Graphite. Ganesh is the default backend.
158-
Currently, Graphite doesn't match Ganesh in terms of features, but we offer experimental support for it.
159-
If you want to tinker with Graphite, you can enable it by building Skia using `SK_GRAPHITE=1 yarn build-skia`.
160-
With this command, the Skia binary will work for both Ganesh and Graphite.
161-
The reason we do not currently ship this build by default is that it requires Android API Level 26 or above.
162-
163-
To enable Graphite in your app, follow these steps:
164-
* **iOS**: Install pods using `SK_GRAPHITE=1 pod install`.
165-
* **Android**: In [CMakeLists.txt](/packages/skia/android/CMakeLists.txt), use `set(SK_GRAPHITE ON)`.
158+
If you want to experiment with Graphite, you can enable it by building Skia with `SK_GRAPHITE=1 yarn build-skia`.
159+
Skia Graphite requires Android API Level 26 or above.
160+
161+
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.
166162

‎apps/example/app.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,14 @@
3030
"dist/assets",
3131
"dist/main.windows.bundle"
3232
]
33+
},
34+
"ios": {
35+
"metalAPIValidation": false
36+
},
37+
"macos": {
38+
"metalAPIValidation": false
39+
},
40+
"visionos": {
41+
"metalAPIValidation": false
3342
}
3443
}

‎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: 8ea98bfb08c756d04fa37803fcc4ff049d47ab42
2196+
react-native-skia: cde44b3bdb773204750a79d01ee74b85480287b1
21972197
react-native-slider: bb7eb4732940fab78217e1c096bb647d8b0d1cf3
21982198
React-NativeModulesApple: 88433b6946778bea9c153e27b671de15411bf225
21992199
React-perflogger: 9e8d3c0dc0194eb932162812a168aa5dc662f418

‎packages/skia/android/CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@ cmake_minimum_required(VERSION 3.4.1)
33

44
set (CMAKE_VERBOSE_MAKEFILE ON)
55
set (CMAKE_CXX_STANDARD 17)
6-
set(SK_GRAPHITE OFF)
6+
7+
# Import prebuilt SKIA libraries path first to check for Dawn libraries
8+
set (SKIA_LIBS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../libs/android/${ANDROID_ABI}")
9+
10+
# Check if Dawn libraries exist, if so enable SK_GRAPHITE
11+
if(EXISTS "${SKIA_LIBS_PATH}/libdawn_native_static.a")
12+
set(SK_GRAPHITE ON)
13+
message("-- SK_GRAPHITE: ON (Dawn libraries found)")
14+
else()
15+
set(SK_GRAPHITE OFF)
16+
message("-- SK_GRAPHITE: OFF (Dawn libraries not found)")
17+
endif()
18+
719
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")
820
set (PACKAGE_NAME "rnskia")
921
set (SKIA_LIB "skia")
@@ -31,9 +43,6 @@ message("-- LIBRN : " ${LIBRN_DIR})
3143

3244
link_directories(../libs/android/${ANDROID_ABI}/)
3345

34-
# Import prebuilt SKIA libraries
35-
set (SKIA_LIBS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../libs/android/${ANDROID_ABI}")
36-
3746
if(SK_GRAPHITE)
3847
add_definitions(-DSK_GRAPHITE)
3948
set(DAWN_NATIVE_LIB "libdawn_native_static")

‎packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
7575
#endif
7676
}
7777

78+
#if !defined(SK_GRAPHITE)
7879
sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &texInfo,
7980
int width, int height,
8081
bool mipMapped) override {
@@ -99,6 +100,7 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
99100
kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType,
100101
nullptr);
101102
}
103+
#endif
102104

103105
std::shared_ptr<RNSkVideo> createVideo(const std::string &url) override {
104106
auto jniVideo = _jniPlatformContext->createVideo(url);
@@ -171,6 +173,11 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
171173
#endif
172174
}
173175

176+
#if !defined(SK_GRAPHITE)
177+
GrDirectContext *getDirectContext() override {
178+
return OpenGLContext::getInstance().getDirectContext();
179+
}
180+
174181
const TextureInfo getTexture(sk_sp<SkImage> image) override {
175182
GrBackendTexture texture;
176183
if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
@@ -186,6 +193,7 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
186193
}
187194

188195
static TextureInfo getTextureInfo(const GrBackendTexture &texture) {
196+
189197
if (!texture.isValid()) {
190198
throw std::runtime_error("invalid backend texture");
191199
}
@@ -204,11 +212,6 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
204212
texInfo.glTarget = textureInfo.fTarget;
205213
return texInfo;
206214
}
207-
208-
#if !defined(SK_GRAPHITE)
209-
GrDirectContext *getDirectContext() override {
210-
return OpenGLContext::getInstance().getDirectContext();
211-
}
212215
#endif
213216

214217
sk_sp<SkFontMgr> createFontMgr() override {

‎packages/skia/apple/RNSkApplePlatformContext.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ class RNSkApplePlatformContext : public RNSkPlatformContext {
4343

4444
sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) override;
4545

46+
#if !defined(SK_GRAPHITE)
47+
GrDirectContext *getDirectContext() override;
48+
4649
sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &textureInfo,
4750
int width, int height,
4851
bool mipMapped) override;
4952

50-
uint64_t makeNativeBuffer(sk_sp<SkImage> image) override;
51-
5253
const TextureInfo getTexture(sk_sp<SkSurface> image) override;
5354

5455
const TextureInfo getTexture(sk_sp<SkImage> image) override;
56+
#endif
57+
58+
uint64_t makeNativeBuffer(sk_sp<SkImage> image) override;
5559

5660
void releaseNativeBuffer(uint64_t pointer) override;
5761

@@ -66,9 +70,7 @@ class RNSkApplePlatformContext : public RNSkPlatformContext {
6670

6771
void raiseError(const std::exception &err) override;
6872
sk_sp<SkSurface> makeOffscreenSurface(int width, int height) override;
69-
#if !defined(SK_GRAPHITE)
70-
GrDirectContext *getDirectContext() override;
71-
#endif
73+
7274
sk_sp<SkFontMgr> createFontMgr() override;
7375

7476
private:

‎packages/skia/apple/RNSkApplePlatformContext.mm

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,14 @@
156156
return reinterpret_cast<uint64_t>(pixelBuffer);
157157
}
158158

159+
#if !defined(SK_GRAPHITE)
160+
GrDirectContext *RNSkApplePlatformContext::getDirectContext() {
161+
return MetalContext::getInstance().getDirectContext();
162+
}
163+
159164
const TextureInfo RNSkApplePlatformContext::getTexture(sk_sp<SkImage> image) {
160-
GrBackendTexture texture;
161165
TextureInfo result;
166+
GrBackendTexture texture;
162167
if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
163168
throw std::runtime_error("Couldn't get backend texture");
164169
}
@@ -175,20 +180,43 @@
175180

176181
const TextureInfo
177182
RNSkApplePlatformContext::getTexture(sk_sp<SkSurface> surface) {
183+
TextureInfo result;
178184
GrBackendTexture texture = SkSurfaces::GetBackendTexture(
179185
surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead);
180-
TextureInfo result;
181186
if (!texture.isValid()) {
182187
throw std::runtime_error("Invalid backend texture");
183188
}
184189
GrMtlTextureInfo textureInfo;
185190
if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
186191
throw std::runtime_error("Couldn't get Metal texture info");
187192
}
188-
result.mtlTexture = textureInfo.fTexture.get();
193+
result.mtlTexture = textureInfo.fTexture.get();
189194
return result;
190195
}
191196

197+
sk_sp<SkImage> RNSkApplePlatformContext::makeImageFromNativeTexture(
198+
const TextureInfo &texInfo, int width, int height, bool mipMapped) {
199+
id<MTLTexture> mtlTexture = (__bridge id<MTLTexture>)(texInfo.mtlTexture);
200+
201+
SkColorType colorType = mtlPixelFormatToSkColorType(mtlTexture.pixelFormat);
202+
if (colorType == SkColorType::kUnknown_SkColorType) {
203+
throw std::runtime_error("Unsupported pixelFormat");
204+
}
205+
206+
GrMtlTextureInfo textureInfo;
207+
textureInfo.fTexture.retain((__bridge const void *)mtlTexture);
208+
209+
GrBackendTexture texture = GrBackendTextures::MakeMtl(
210+
width, height, mipMapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
211+
textureInfo);
212+
213+
return SkImages::BorrowTextureFrom(getDirectContext(), texture,
214+
kTopLeft_GrSurfaceOrigin, colorType,
215+
kPremul_SkAlphaType, nullptr);
216+
return nullptr;
217+
}
218+
#endif
219+
192220
std::shared_ptr<RNSkVideo>
193221
RNSkApplePlatformContext::createVideo(const std::string &url) {
194222
return std::make_shared<RNSkAppleVideo>(url, this);
@@ -227,27 +255,6 @@
227255
#endif
228256
}
229257

230-
sk_sp<SkImage> RNSkApplePlatformContext::makeImageFromNativeTexture(
231-
const TextureInfo &texInfo, int width, int height, bool mipMapped) {
232-
id<MTLTexture> mtlTexture = (__bridge id<MTLTexture>)(texInfo.mtlTexture);
233-
234-
SkColorType colorType = mtlPixelFormatToSkColorType(mtlTexture.pixelFormat);
235-
if (colorType == SkColorType::kUnknown_SkColorType) {
236-
throw std::runtime_error("Unsupported pixelFormat");
237-
}
238-
239-
GrMtlTextureInfo textureInfo;
240-
textureInfo.fTexture.retain((__bridge const void *)mtlTexture);
241-
242-
GrBackendTexture texture = GrBackendTextures::MakeMtl(
243-
width, height, mipMapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
244-
textureInfo);
245-
246-
return SkImages::BorrowTextureFrom(getDirectContext(), texture,
247-
kTopLeft_GrSurfaceOrigin, colorType,
248-
kPremul_SkAlphaType, nullptr);
249-
}
250-
251258
SkColorType RNSkApplePlatformContext::mtlPixelFormatToSkColorType(
252259
MTLPixelFormat pixelFormat) {
253260
switch (pixelFormat) {
@@ -280,12 +287,6 @@
280287
}
281288
}
282289

283-
#if !defined(SK_GRAPHITE)
284-
GrDirectContext *RNSkApplePlatformContext::getDirectContext() {
285-
return MetalContext::getInstance().getDirectContext();
286-
}
287-
#endif
288-
289290
sk_sp<SkFontMgr> RNSkApplePlatformContext::createFontMgr() {
290291
return SkFontMgr_New_CoreText(nullptr);
291292
}

‎packages/skia/apple/SkiaCVPixelBufferUtils.mm

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,12 @@
3030
#include <TargetConditionals.h>
3131
#if TARGET_RT_BIG_ENDIAN
3232
#define FourCC2Str(fourcc) \
33-
(const char[]) { \
34-
*((char *)&fourcc), *(((char *)&fourcc) + 1), *(((char *)&fourcc) + 2), \
35-
*(((char *)&fourcc) + 3), 0 \
36-
}
33+
(const char[]){*((char *)&fourcc), *(((char *)&fourcc) + 1), \
34+
*(((char *)&fourcc) + 2), *(((char *)&fourcc) + 3), 0}
3735
#else
3836
#define FourCC2Str(fourcc) \
39-
(const char[]) { \
40-
*(((char *)&fourcc) + 3), *(((char *)&fourcc) + 2), \
41-
*(((char *)&fourcc) + 1), *(((char *)&fourcc) + 0), 0 \
42-
}
37+
(const char[]){*(((char *)&fourcc) + 3), *(((char *)&fourcc) + 2), \
38+
*(((char *)&fourcc) + 1), *(((char *)&fourcc) + 0), 0}
4339
#endif
4440

4541
// pragma MARK: TextureHolder

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class DawnContext {
204204
DawnProcTable backendProcs = dawn::native::GetProcs();
205205
dawnProcSetProcs(&backendProcs);
206206
WGPUInstanceDescriptor desc{};
207-
desc.features.timedWaitAnyEnable = true;
207+
desc.capabilities.timedWaitAnyEnable = true;
208208
instance = std::make_unique<dawn::native::Instance>(&desc);
209209

210210
backendContext = DawnUtils::createDawnBackendContext(instance.get());

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ createDawnBackendContext(dawn::native::Instance *instance) {
3131
"use_tint_ir",
3232
};
3333
wgpu::DawnTogglesDescriptor togglesDesc;
34-
togglesDesc.enabledToggleCount = std::size(kToggles) - (useTintIR ? 0 : 1);
34+
togglesDesc.enabledToggleCount =
35+
(sizeof(kToggles) / sizeof(kToggles[0])) - (useTintIR ? 0 : 1);
3536
togglesDesc.enabledToggles = kToggles;
3637

3738
wgpu::RequestAdapterOptions options;
@@ -100,18 +101,6 @@ createDawnBackendContext(dawn::native::Instance *instance) {
100101
desc.requiredFeatureCount = features.size();
101102
desc.requiredFeatures = features.data();
102103
desc.nextInChain = &togglesDesc;
103-
desc.SetDeviceLostCallback(
104-
wgpu::CallbackMode::AllowSpontaneous,
105-
[](const wgpu::Device &, wgpu::DeviceLostReason reason,
106-
const char *message) {
107-
if (reason != wgpu::DeviceLostReason::Destroyed) {
108-
SK_ABORT("Device lost: %s\n", message);
109-
}
110-
});
111-
desc.SetUncapturedErrorCallback(
112-
[](const wgpu::Device &, wgpu::ErrorType, const char *message) {
113-
SkDebugf("Device error: %s\n", message);
114-
});
115104

116105
wgpu::Device device = adapter.CreateDevice(&desc);
117106
SkASSERT(device);

0 commit comments

Comments
 (0)