Skip to content

Commit

Permalink
[wpe] Fix device scale according Android DisplayMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
zhani committed Jun 5, 2024
1 parent 5f5071c commit 535b5e5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tools/mediaplayer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {

dependencies {
implementation project(':wpe')
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.appcompat:appcompat:1.7.0'

modules {
module("org.jetbrains.kotlin:kotlin-stdlib-jdk7") {
Expand Down
33 changes: 21 additions & 12 deletions wpe/src/main/cpp/Browser/Page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ class JNIPageCache final : public JNI::TypedClass<JNIPage> {
const JNI::Method<void()> m_onExitFullscreenMode;
// NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)

static jlong nativeInit(
JNIEnv* env, jobject obj, jlong wkWebContextPtr, jint width, jint height, jboolean headless);
static jlong nativeInit(JNIEnv* env, jobject obj, jlong wkWebContextPtr, jint width, jint height,
jfloat deviceScale, jboolean headless);
static void nativeClose(JNIEnv* env, jobject obj, jlong pagePtr) noexcept;
static void nativeDestroy(JNIEnv* env, jobject obj, jlong pagePtr) noexcept;
static void nativeLoadUrl(JNIEnv* env, jobject obj, jlong pagePtr, jstring url) noexcept;
Expand Down Expand Up @@ -168,7 +168,8 @@ JNIPageCache::JNIPageCache()
, m_onEnterFullscreenMode(getMethod<void()>("onEnterFullscreenMode"))
, m_onExitFullscreenMode(getMethod<void()>("onExitFullscreenMode"))
{
registerNativeMethods(JNI::NativeMethod<jlong(jlong, jint, jint, jboolean)>("nativeInit", JNIPageCache::nativeInit),
registerNativeMethods(
JNI::NativeMethod<jlong(jlong, jint, jint, jfloat, jboolean)>("nativeInit", JNIPageCache::nativeInit),
JNI::NativeMethod<void(jlong)>("nativeClose", JNIPageCache::nativeClose),
JNI::NativeMethod<void(jlong)>("nativeDestroy", JNIPageCache::nativeDestroy),
JNI::NativeMethod<void(jlong, jstring)>("nativeLoadUrl", JNIPageCache::nativeLoadUrl),
Expand All @@ -192,13 +193,13 @@ JNIPageCache::JNIPageCache()
}

jlong JNIPageCache::nativeInit(
JNIEnv* env, jobject obj, jlong wkWebContextPtr, jint width, jint height, jboolean headless)
JNIEnv* env, jobject obj, jlong wkWebContextPtr, jint width, jint height, jfloat deviceScale, jboolean headless)
{
Logging::logDebug("Page::nativeInit(%p, %d, %d) [tid %d]", obj, width, height, gettid());
Logging::logDebug("Page::nativeInit(%p, %d, %d, [density %f] [tid %d]", obj, width, height, deviceScale, gettid());
auto* wkWebContext = reinterpret_cast<WKWebContext*>(wkWebContextPtr); // NOLINT(performance-no-int-to-ptr)
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Page* page
= new Page(env, reinterpret_cast<JNIPage>(obj), wkWebContext, width, height, static_cast<bool>(headless));
Page* page = new Page(
env, reinterpret_cast<JNIPage>(obj), wkWebContext, width, height, deviceScale, static_cast<bool>(headless));
return reinterpret_cast<jlong>(page);
}

Expand Down Expand Up @@ -282,11 +283,14 @@ void JNIPageCache::nativeSurfaceChanged(
Logging::logDebug("Page::nativeSurfaceChanged(%d, %d, %d) [tid %d]", format, width, height, gettid());
Page* page = reinterpret_cast<Page*>(pagePtr); // NOLINT(performance-no-int-to-ptr)
if ((page != nullptr) && (page->m_viewBackend != nullptr) && page->m_renderer) {
const uint32_t uWidth = std::max(0, width);
const uint32_t uHeight = std::max(0, height);
const uint32_t physicalWidth = std::max(0, width);
const uint32_t physicalHeight = std::max(0, height);
const uint32_t logicalWidth = std::floor(static_cast<float>(physicalWidth) / page->deviceScale());
const uint32_t logicalHeight = std::floor(static_cast<float>(physicalHeight) / page->deviceScale());

wpe_view_backend_dispatch_set_size(
WPEAndroidViewBackend_getWPEViewBackend(page->m_viewBackend), uWidth, uHeight);
page->m_renderer->onSurfaceChanged(format, uWidth, uHeight);
WPEAndroidViewBackend_getWPEViewBackend(page->m_viewBackend), logicalWidth, logicalHeight);
page->m_renderer->onSurfaceChanged(format, physicalWidth, physicalHeight);
}
}

Expand Down Expand Up @@ -390,10 +394,12 @@ void JNIPageCache::nativeRequestExitFullscreenMode(JNIEnv* /*env*/, jobject /*ob

void Page::configureJNIMappings() { getJNIPageCache(); }

Page::Page(JNIEnv* env, JNIPage jniPage, WKWebContext* wkWebContext, int width, int height, bool headless)
Page::Page(
JNIEnv* env, JNIPage jniPage, WKWebContext* wkWebContext, int width, int height, float deviceScale, bool headless)
: m_pageJavaInstance(JNI::createTypedProtectedRef(env, jniPage, true))
, m_inputMethodContext(this)
, m_isHeadless(headless)
, m_deviceScale(deviceScale)
{
const uint32_t uWidth = std::max(0, width);
const uint32_t uHeight = std::max(0, height);
Expand Down Expand Up @@ -427,6 +433,9 @@ Page::Page(JNIEnv* env, JNIPage jniPage, WKWebContext* wkWebContext, int width,
wpeBackend, reinterpret_cast<wpe_view_backend_fullscreen_handler>(JNIPageCache::onFullscreenRequest), this);

WPEAndroidViewBackend_setCommitBufferHandler(m_viewBackend, this, handleCommitBuffer);

wpe_view_backend_dispatch_set_device_scale_factor(
WPEAndroidViewBackend_getWPEViewBackend(m_viewBackend), deviceScale);
}

void Page::close() noexcept
Expand Down
5 changes: 4 additions & 1 deletion wpe/src/main/cpp/Browser/Page.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Page final : public InputMethodContextObserver {

void close() noexcept;

float deviceScale() const noexcept { return m_deviceScale; }
WebKitWebView* webView() const noexcept { return m_webView; }

void onInputMethodContextIn() noexcept override;
Expand All @@ -56,7 +57,8 @@ class Page final : public InputMethodContextObserver {
private:
friend class JNIPageCache;

Page(JNIEnv* env, JNIPage jniPage, WKWebContext* wkWebContext, int width, int height, bool headless);
Page(JNIEnv* env, JNIPage jniPage, WKWebContext* wkWebContext, int width, int height, float deviceScale,
bool headless);

JNI::ProtectedType<JNIPage> m_pageJavaInstance;
InputMethodContext m_inputMethodContext;
Expand All @@ -67,4 +69,5 @@ class Page final : public InputMethodContextObserver {
std::vector<gulong> m_signalHandlers;
bool m_isFullscreenRequested = false;
bool m_isHeadless = false;
float m_deviceScale;
};
7 changes: 5 additions & 2 deletions wpe/src/main/java/com/wpe/wpe/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
Expand Down Expand Up @@ -62,7 +63,7 @@ public final class Page {
protected long nativePtr = 0;
public long getNativePtr() { return nativePtr; }

private native long nativeInit(long nativeContextPtr, int width, int height, boolean headless);
private native long nativeInit(long nativeContextPtr, int width, int height, float deviceScale, boolean headless);
private native void nativeClose(long nativePtr);
private native void nativeDestroy(long nativePtr);
private native void nativeLoadUrl(long nativePtr, @NonNull String url);
Expand Down Expand Up @@ -109,7 +110,9 @@ public Page(@NonNull WPEView wpeView, @NonNull WKWebContext context, boolean hea
height = kHeadlessHeight;
}

nativePtr = nativeInit(context.getNativePtr(), width, height, headless);
DisplayMetrics displayMetrics = context.getApplicationContext().getResources().getDisplayMetrics();

nativePtr = nativeInit(context.getNativePtr(), width, height, displayMetrics.density, headless);

Context ctx = wpeView.getContext();
surfaceView = new PageSurfaceView(ctx);
Expand Down

0 comments on commit 535b5e5

Please sign in to comment.