Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix(android): dynamic pixel density change #2679

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/skia/android/cpp/jni/JniPlatformContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ using TSelf = jni::local_ref<JniPlatformContext::jhybriddata>;
void JniPlatformContext::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", JniPlatformContext::initHybrid),
makeNativeMethod("getPixelDensity",
JniPlatformContext::getPixelDensity),
makeNativeMethod("setPixelDensity",
JniPlatformContext::setPixelDensity),
});
}

Expand All @@ -71,6 +75,14 @@ TSelf JniPlatformContext::initHybrid(jni::alias_ref<jhybridobject> jThis,
return makeCxxInstance(jThis, pixelDensity);
}

float JniPlatformContext::getPixelDensity() {
return _pixelDensity;
}

void JniPlatformContext::setPixelDensity(float pixelDensity) {
_pixelDensity=pixelDensity;
}

jni::global_ref<jobject>
JniPlatformContext::createVideo(const std::string &url) {
jni::Environment::ensureCurrentThreadIsAttached();
Expand Down
5 changes: 3 additions & 2 deletions packages/skia/android/cpp/jni/include/JniPlatformContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class JniPlatformContext : public jni::HybridClass<JniPlatformContext> {

static void registerNatives();

float getPixelDensity();
void setPixelDensity(float pixelDensity);

void performStreamOperation(
const std::string &sourceUri,
const std::function<void(std::unique_ptr<SkStreamAsset>)> &op);
Expand All @@ -37,8 +40,6 @@ class JniPlatformContext : public jni::HybridClass<JniPlatformContext> {

void runTaskOnMainThread(std::function<void()> task);

float getPixelDensity() { return _pixelDensity; }

sk_sp<SkImage> takeScreenshotFromViewTag(size_t tag);

jni::global_ref<jobject> createVideo(const std::string &url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {

~RNSkAndroidPlatformContext() {}

float getPixelDensity() override {
return _jniPlatformContext->getPixelDensity();
}

void performStreamOperation(
const std::string &sourceUri,
const std::function<void(std::unique_ptr<SkStreamAsset>)> &op) override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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
Expand Down Expand Up @@ -99,6 +101,15 @@ public byte[] getJniStreamFromSource(String sourceUri) throws IOException {
return null;
}

void onResume() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be "onConfigurationChanged"?

_isPaused = false;
Log.i(TAG, "Resume");

float density = mContext.getResources().getDisplayMetrics().density;
//Log.d(TAG, "density=" + Float.toString(density));
setPixelDensity(density);
}

@Override
protected void finalize() throws Throwable {
mHybridData.resetNative();
Expand All @@ -107,4 +118,6 @@ 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);
}
2 changes: 1 addition & 1 deletion packages/skia/cpp/rnskia/RNSkPlatformContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class RNSkPlatformContext {
/**
* @return Current scale factor for pixels
*/
float getPixelDensity() { return _pixelDensity; }
virtual float getPixelDensity() { return _pixelDensity; }

private:
float _pixelDensity;
Expand Down
Loading