From e40b36f2eeb3dd33d2fbe4386b8c9b45ed337a6a Mon Sep 17 00:00:00 2001 From: Alexander Keda Date: Mon, 7 Oct 2024 15:23:40 +0300 Subject: [PATCH] Platform context: get/set pixel density methods, set on resume --- packages/skia/android/cpp/jni/JniPlatformContext.cpp | 12 ++++++++++++ .../android/cpp/jni/include/JniPlatformContext.h | 5 +++-- .../cpp/rnskia-android/RNSkAndroidPlatformContext.h | 4 ++++ .../shopify/reactnative/skia/PlatformContext.java | 11 ++++++++++- packages/skia/cpp/rnskia/RNSkPlatformContext.h | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/skia/android/cpp/jni/JniPlatformContext.cpp b/packages/skia/android/cpp/jni/JniPlatformContext.cpp index ba3e70e0c9..b6d53925d9 100644 --- a/packages/skia/android/cpp/jni/JniPlatformContext.cpp +++ b/packages/skia/android/cpp/jni/JniPlatformContext.cpp @@ -63,6 +63,10 @@ using TSelf = jni::local_ref; void JniPlatformContext::registerNatives() { registerHybrid({ makeNativeMethod("initHybrid", JniPlatformContext::initHybrid), + makeNativeMethod("getPixelDensity", + JniPlatformContext::getPixelDensity), + makeNativeMethod("setPixelDensity", + JniPlatformContext::setPixelDensity), makeNativeMethod("notifyDrawLoop", JniPlatformContext::notifyDrawLoopExternal), makeNativeMethod("notifyTaskReady", @@ -75,6 +79,14 @@ TSelf JniPlatformContext::initHybrid(jni::alias_ref jThis, return makeCxxInstance(jThis, pixelDensity); } +float JniPlatformContext::getPixelDensity() { + return _pixelDensity; +} + +void JniPlatformContext::setPixelDensity(float pixelDensity) { + _pixelDensity=pixelDensity; +} + jni::global_ref JniPlatformContext::createVideo(const std::string &url) { jni::Environment::ensureCurrentThreadIsAttached(); diff --git a/packages/skia/android/cpp/jni/include/JniPlatformContext.h b/packages/skia/android/cpp/jni/include/JniPlatformContext.h index 634e891d07..8e7f6197d1 100644 --- a/packages/skia/android/cpp/jni/include/JniPlatformContext.h +++ b/packages/skia/android/cpp/jni/include/JniPlatformContext.h @@ -27,6 +27,9 @@ class JniPlatformContext : public jni::HybridClass { static void registerNatives(); + float getPixelDensity(); + void setPixelDensity(float pixelDensity); + void performStreamOperation( const std::string &sourceUri, const std::function)> &op); @@ -42,8 +45,6 @@ class JniPlatformContext : public jni::HybridClass { void runTaskOnMainThread(std::function task); - float getPixelDensity() { return _pixelDensity; } - sk_sp takeScreenshotFromViewTag(size_t tag); void setOnNotifyDrawLoop(const std::function &callback) { diff --git a/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h b/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h index 176f7943d4..c9accda666 100644 --- a/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +++ b/packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h @@ -40,6 +40,10 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext { ~RNSkAndroidPlatformContext() { stopDrawLoop(); } + float getPixelDensity() override { + return _jniPlatformContext->getPixelDensity(); + } + void performStreamOperation( const std::string &sourceUri, const std::function)> &op) override { diff --git a/packages/skia/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java b/packages/skia/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java index 0dfdfd13d6..cc51f809cf 100644 --- a/packages/skia/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java +++ b/packages/skia/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java @@ -35,7 +35,9 @@ public class PlatformContext { public PlatformContext(ReactContext reactContext) { mContext = reactContext; - mHybridData = initHybrid(reactContext.getResources().getDisplayMetrics().density); + float density = mContext.getResources().getDisplayMetrics().density; + //Log.d(TAG, "density=" + Float.toString(density)); + mHybridData = initHybrid(density); } @DoNotStrip @@ -171,6 +173,11 @@ void onPause() { void onResume() { _isPaused = false; Log.i(TAG, "Resume"); + + float density = mContext.getResources().getDisplayMetrics().density; + //Log.d(TAG, "density=" + Float.toString(density)); + setPixelDensity(density); + if(_drawLoopActive) { // Restart draw loop mainHandler.post(new Runnable() { @@ -190,6 +197,8 @@ protected void finalize() throws Throwable { // Private c++ native methods private native HybridData initHybrid(float pixelDensity); + private native float getPixelDensity(); + private native void setPixelDensity(float pixelDensity); private native void notifyDrawLoop(); private native void notifyTaskReady(); } \ No newline at end of file diff --git a/packages/skia/cpp/rnskia/RNSkPlatformContext.h b/packages/skia/cpp/rnskia/RNSkPlatformContext.h index 0ad2b668e2..a9d788faef 100644 --- a/packages/skia/cpp/rnskia/RNSkPlatformContext.h +++ b/packages/skia/cpp/rnskia/RNSkPlatformContext.h @@ -186,7 +186,7 @@ class RNSkPlatformContext { /** * @return Current scale factor for pixels */ - float getPixelDensity() { return _pixelDensity; } + virtual float getPixelDensity() { return _pixelDensity; } /** * Starts (if not started) a loop that will call back on display sync