From 52f2e44c3d709ce9be9cc61c3d0f1ae438b52b01 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Sat, 7 Dec 2024 18:08:05 +0100 Subject: [PATCH] =?UTF-8?q?chore(=F0=9F=96=BC=EF=B8=8F):=20minor=20refacto?= =?UTF-8?q?ring=20in=20platform=20context=20(#2795)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RNSkAndroidPlatformContext.h | 54 +++++++------------ packages/skia/cpp/api/JsiSkImage.h | 4 +- packages/skia/cpp/api/JsiSkImageFactory.h | 5 +- packages/skia/cpp/api/JsiSkSurface.h | 4 +- packages/skia/cpp/api/JsiTextureInfo.h | 53 ++++++++++++++++++ .../skia/cpp/rnskia/RNSkPlatformContext.h | 23 ++++---- .../ios/RNSkia-iOS/RNSkiOSPlatformContext.h | 9 ++-- .../ios/RNSkia-iOS/RNSkiOSPlatformContext.mm | 35 ++++++------ 8 files changed, 113 insertions(+), 74 deletions(-) create mode 100644 packages/skia/cpp/api/JsiTextureInfo.h diff --git a/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h b/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h index 70ba6157c5..1f9c3446ba 100644 --- a/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +++ b/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h @@ -20,7 +20,6 @@ #include "MainThreadDispatcher.h" #include "RNSkAndroidVideo.h" #include "RNSkPlatformContext.h" - #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdocumentation" @@ -78,26 +77,15 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext { #endif } - sk_sp makeImageFromNativeTexture(jsi::Runtime &runtime, - jsi::Value jsiTextureInfo, + sk_sp makeImageFromNativeTexture(const TextureInfo &texInfo, int width, int height, bool mipMapped) override { - if (!jsiTextureInfo.isObject()) { - throw new std::runtime_error("Invalid textureInfo"); - } - auto jsiTextureInfoObj = jsiTextureInfo.asObject(runtime); - GrGLTextureInfo textureInfo; - textureInfo.fTarget = - (GrGLenum)jsiTextureInfoObj.getProperty(runtime, "fTarget").asNumber(); - textureInfo.fID = - (GrGLuint)jsiTextureInfoObj.getProperty(runtime, "fID").asNumber(); - textureInfo.fFormat = - (GrGLenum)jsiTextureInfoObj.getProperty(runtime, "fFormat").asNumber(); + textureInfo.fTarget = (GrGLenum)texInfo.glTarget; + textureInfo.fID = (GrGLuint)texInfo.glID; + textureInfo.fFormat = (GrGLenum)texInfo.glFormat; textureInfo.fProtected = - jsiTextureInfoObj.getProperty(runtime, "fProtected").asBool() - ? skgpu::Protected::kYes - : skgpu::Protected::kNo; + texInfo.glProtected ? skgpu::Protected::kYes : skgpu::Protected::kNo; OpenGLContext::getInstance().makeCurrent(); if (glIsTexture(textureInfo.fID) == GL_FALSE) { @@ -185,42 +173,38 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext { #endif } - jsi::Value getTexture(jsi::Runtime &runtime, sk_sp image) override { + const TextureInfo getTexture(sk_sp image) override { GrBackendTexture texture; if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) { - return jsi::Value::null(); + throw std::runtime_error("Couldn't get backend texture from image."); } - return getJSITextureInfo(runtime, texture); + return getJSITextureInfo(texture); } - jsi::Value getTexture(jsi::Runtime &runtime, - sk_sp surface) override { + const TextureInfo getTexture(sk_sp surface) override { GrBackendTexture texture = SkSurfaces::GetBackendTexture( surface.get(), SkSurface::BackendHandleAccess::kFlushRead); - return getJSITextureInfo(runtime, texture); + return getJSITextureInfo(texture); } - static jsi::Value getJSITextureInfo(jsi::Runtime &runtime, - const GrBackendTexture &texture) { + static const TextureInfo getJSITextureInfo(const GrBackendTexture &texture) { if (!texture.isValid()) { - return jsi::Value::null(); + throw std::runtime_error("invalid backend texture"); } GrGLTextureInfo textureInfo; if (!GrBackendTextures::GetGLTextureInfo(texture, &textureInfo)) { - return jsi::Value::null(); + throw std::runtime_error("couldn't get OpenGL texture"); } OpenGLContext::getInstance().makeCurrent(); glFlush(); - jsi::Object jsiTextureInfo = jsi::Object(runtime); - jsiTextureInfo.setProperty(runtime, "fTarget", (int)textureInfo.fTarget); - jsiTextureInfo.setProperty(runtime, "fFormat", (int)textureInfo.fFormat); - jsiTextureInfo.setProperty(runtime, "fID", (int)textureInfo.fID); - jsiTextureInfo.setProperty(runtime, "fProtected", - (bool)textureInfo.fProtected); - - return jsiTextureInfo; + TextureInfo texInfo; + texInfo.glProtected = textureInfo.isProtected(); + texInfo.glID = textureInfo.fID; + texInfo.glFormat = textureInfo.fFormat; + texInfo.glTarget = textureInfo.fTarget; + return texInfo; } #if !defined(SK_GRAPHITE) diff --git a/packages/skia/cpp/api/JsiSkImage.h b/packages/skia/cpp/api/JsiSkImage.h index 192a43cad0..23061e6417 100644 --- a/packages/skia/cpp/api/JsiSkImage.h +++ b/packages/skia/cpp/api/JsiSkImage.h @@ -10,6 +10,7 @@ #include "JsiSkShader.h" #include "third_party/base64.h" +#include "JsiTextureInfo.h" #include "RNSkTypedArray.h" #if defined(SK_GRAPHITE) @@ -220,7 +221,8 @@ class JsiSkImage : public JsiSkWrappingSkPtrHostObject { if (!image->isTextureBacked()) { return jsi::Value::null(); } - return getContext()->getTexture(runtime, image); + auto texInfo = getContext()->getTexture(image); + return JsiTextureInfo::toValue(runtime, texInfo); } EXPORT_JSI_API_TYPENAME(JsiSkImage, Image) diff --git a/packages/skia/cpp/api/JsiSkImageFactory.h b/packages/skia/cpp/api/JsiSkImageFactory.h index 8382b02057..76a358c6bd 100644 --- a/packages/skia/cpp/api/JsiSkImageFactory.h +++ b/packages/skia/cpp/api/JsiSkImageFactory.h @@ -79,9 +79,10 @@ class JsiSkImageFactory : public JsiSkHostObject { } JSI_HOST_FUNCTION(MakeImageFromNativeTextureUnstable) { + auto texInfo = JsiTextureInfo::fromValue(runtime, arguments[0]); auto image = getContext()->makeImageFromNativeTexture( - runtime, jsi::Value(runtime, arguments[0]), arguments[1].asNumber(), - arguments[2].asNumber(), count > 3 && arguments[3].asBool()); + texInfo, arguments[1].asNumber(), arguments[2].asNumber(), + count > 3 && arguments[3].asBool()); if (image == nullptr) { throw std::runtime_error("Failed to convert native texture to SkImage!"); } diff --git a/packages/skia/cpp/api/JsiSkSurface.h b/packages/skia/cpp/api/JsiSkSurface.h index 5915217258..b4a95aac22 100644 --- a/packages/skia/cpp/api/JsiSkSurface.h +++ b/packages/skia/cpp/api/JsiSkSurface.h @@ -6,6 +6,7 @@ #include #include "JsiSkHostObjects.h" +#include "JsiTextureInfo.h" #include "JsiSkCanvas.h" #include "JsiSkImage.h" @@ -79,7 +80,8 @@ class JsiSkSurface : public JsiSkWrappingSkPtrHostObject { } JSI_HOST_FUNCTION(getNativeTextureUnstable) { - return getContext()->getTexture(runtime, getObject()); + auto texInfo = getContext()->getTexture(getObject()); + return JsiTextureInfo::toValue(runtime, texInfo); } JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkSurface, width), diff --git a/packages/skia/cpp/api/JsiTextureInfo.h b/packages/skia/cpp/api/JsiTextureInfo.h new file mode 100644 index 0000000000..762e080c3d --- /dev/null +++ b/packages/skia/cpp/api/JsiTextureInfo.h @@ -0,0 +1,53 @@ +#pragma once + +#include + +#include "RNSkPlatformContext.h" + +namespace jsi = facebook::jsi; +namespace react = facebook::react; + +namespace RNSkia { + +namespace JsiTextureInfo { + +inline jsi::Value toValue(jsi::Runtime &runtime, const TextureInfo &texInfo) { + jsi::Object textureInfo(runtime); + textureInfo.setProperty( + runtime, "mtlTexture", + jsi::BigInt::fromUint64(runtime, + reinterpret_cast(texInfo.mtlTexture))); + textureInfo.setProperty(runtime, "glTarget", + static_cast(texInfo.glTarget)); + textureInfo.setProperty(runtime, "glID", static_cast(texInfo.glID)); + textureInfo.setProperty(runtime, "glFormat", + static_cast(texInfo.glFormat)); + textureInfo.setProperty(runtime, "glProtected", + static_cast(texInfo.glProtected)); + return textureInfo; +} + +inline TextureInfo fromValue(jsi::Runtime &runtime, const jsi::Value &value) { + auto object = value.getObject(runtime); + TextureInfo texInfo; + if (object.hasProperty(runtime, "mtlTexture")) { + texInfo.mtlTexture = + reinterpret_cast(object.getProperty(runtime, "mtlTexture") + .asBigInt(runtime) + .asUint64(runtime)); + } + if (object.hasProperty(runtime, "glID")) { + texInfo.glTarget = static_cast( + object.getProperty(runtime, "glTarget").asNumber()); + texInfo.glID = static_cast( + object.getProperty(runtime, "glID").asNumber()); + texInfo.glFormat = static_cast( + object.getProperty(runtime, "glFormat").asNumber()); + texInfo.glProtected = + object.getProperty(runtime, "glProtected").asNumber() != 0; + } + return texInfo; +} + +} // namespace JsiTextureInfo +} // namespace RNSkia diff --git a/packages/skia/cpp/rnskia/RNSkPlatformContext.h b/packages/skia/cpp/rnskia/RNSkPlatformContext.h index 6b7b99c183..c124209ad4 100644 --- a/packages/skia/cpp/rnskia/RNSkPlatformContext.h +++ b/packages/skia/cpp/rnskia/RNSkPlatformContext.h @@ -23,8 +23,6 @@ #pragma clang diagnostic pop -#include - #include namespace RNSkia { @@ -32,6 +30,14 @@ namespace RNSkia { namespace jsi = facebook::jsi; namespace react = facebook::react; +struct TextureInfo { + const void *mtlTexture = nullptr; + unsigned int glTarget = 0; + unsigned int glID = 0; + unsigned int glFormat = 0; + bool glProtected = false; +}; + class RNSkPlatformContext { public: /** @@ -100,10 +106,9 @@ class RNSkPlatformContext { */ virtual sk_sp makeImageFromNativeBuffer(void *buffer) = 0; - virtual sk_sp makeImageFromNativeTexture(jsi::Runtime &runtime, - jsi::Value textureInfo, - int width, int height, - bool mipMapped) = 0; + virtual sk_sp + makeImageFromNativeTexture(const TextureInfo &textureInfo, int width, + int height, bool mipMapped) = 0; #if !defined(SK_GRAPHITE) virtual GrDirectContext *getDirectContext() = 0; @@ -113,11 +118,9 @@ class RNSkPlatformContext { virtual uint64_t makeNativeBuffer(sk_sp image) = 0; - virtual jsi::Value getTexture(jsi::Runtime &runtime, - sk_sp image) = 0; + virtual const TextureInfo getTexture(sk_sp image) = 0; - virtual jsi::Value getTexture(jsi::Runtime &runtime, - sk_sp image) = 0; + virtual const TextureInfo getTexture(sk_sp image) = 0; virtual std::shared_ptr createVideo(const std::string &url) = 0; diff --git a/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.h b/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.h index 275ebe7523..2839dde033 100644 --- a/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +++ b/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.h @@ -42,16 +42,15 @@ class RNSkiOSPlatformContext : public RNSkPlatformContext { sk_sp makeImageFromNativeBuffer(void *buffer) override; - sk_sp makeImageFromNativeTexture(jsi::Runtime &runtime, - jsi::Value textureInfo, int width, - int height, + sk_sp makeImageFromNativeTexture(const TextureInfo &textureInfo, + int width, int height, bool mipMapped) override; uint64_t makeNativeBuffer(sk_sp image) override; - jsi::Value getTexture(jsi::Runtime &runtime, sk_sp image) override; + const TextureInfo getTexture(sk_sp image) override; - jsi::Value getTexture(jsi::Runtime &runtime, sk_sp image) override; + const TextureInfo getTexture(sk_sp image) override; void releaseNativeBuffer(uint64_t pointer) override; diff --git a/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm b/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm index 748fcbdc7f..6bbcea0486 100644 --- a/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +++ b/packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm @@ -156,36 +156,36 @@ return reinterpret_cast(pixelBuffer); } -jsi::Value RNSkiOSPlatformContext::getTexture(jsi::Runtime &runtime, - sk_sp image) { +const TextureInfo RNSkiOSPlatformContext::getTexture(sk_sp image) { GrBackendTexture texture; + TextureInfo result; if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) { - return jsi::Value::null(); + throw std::runtime_error("Couldn't get backend texture"); } if (!texture.isValid()) { - return jsi::Value::null(); + throw std::runtime_error("Invalid backend texture"); } GrMtlTextureInfo textureInfo; if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) { - return jsi::Value::null(); + throw std::runtime_error("Couldn't get Metal texture info"); } - auto pointer = reinterpret_cast(textureInfo.fTexture.get()); - return jsi::BigInt::fromUint64(runtime, pointer); + result.mtlTexture = textureInfo.fTexture.get(); + return result; } -jsi::Value RNSkiOSPlatformContext::getTexture(jsi::Runtime &runtime, - sk_sp surface) { +const TextureInfo RNSkiOSPlatformContext::getTexture(sk_sp surface) { GrBackendTexture texture = SkSurfaces::GetBackendTexture( surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead); + TextureInfo result; if (!texture.isValid()) { - return jsi::Value::null(); + throw std::runtime_error("Invalid backend texture"); } GrMtlTextureInfo textureInfo; if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) { - return jsi::Value::null(); + throw std::runtime_error("Couldn't get Metal texture info"); } - auto pointer = reinterpret_cast(textureInfo.fTexture.get()); - return jsi::BigInt::fromUint64(runtime, pointer); + result.mtlTexture = textureInfo.fTexture.get(); + return result; } std::shared_ptr @@ -226,13 +226,8 @@ } sk_sp RNSkiOSPlatformContext::makeImageFromNativeTexture( - jsi::Runtime &runtime, jsi::Value jsiTextureInfo, int width, int height, - bool mipMapped) { - if (!jsiTextureInfo.isBigInt()) { - throw std::runtime_error("Invalid textureInfo"); - } - auto pointer = (void *)jsiTextureInfo.asBigInt(runtime).asUint64(runtime); - id mtlTexture = (__bridge id)(pointer); + const TextureInfo &texInfo, int width, int height, bool mipMapped) { + id mtlTexture = (__bridge id)(texInfo.mtlTexture); SkColorType colorType = mtlPixelFormatToSkColorType(mtlTexture.pixelFormat); if (colorType == SkColorType::kUnknown_SkColorType) {