OHOS: Use native API to get most of the information needed for starting servoshell. (#37392)

Uses the native ohos-api crates to get the required information for
starting servoshell.
This increases the minimum API version requirement to API-14.

Testing: Tested on device.

---------

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
Co-authored-by: Jonathan Schwender <55576758+jschwe@users.noreply.github.com>
This commit is contained in:
Narfinger 2025-06-13 11:38:56 +02:00 committed by GitHub
parent 7a801f0ef5
commit 099fd10317
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 95 additions and 47 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell;
use std::ffi::{CString, c_char};
use std::fs;
use std::os::raw::c_void;
use std::path::PathBuf;
@ -9,7 +10,9 @@ use std::ptr::NonNull;
use std::rc::Rc;
use dpi::PhysicalSize;
use log::{debug, info, warn};
use log::{debug, error, info, warn};
use ohos_abilitykit_sys::runtime::application_context;
use ohos_window_manager_sys::display_manager;
use raw_window_handle::{
DisplayHandle, OhosDisplayHandle, OhosNdkWindowHandle, RawDisplayHandle, RawWindowHandle,
WindowHandle,
@ -23,6 +26,46 @@ use crate::egl::ohos::InitOpts;
use crate::egl::ohos::resources::ResourceReaderInstance;
use crate::prefs::{ArgumentParsingResult, parse_command_line_arguments};
#[derive(Debug)]
struct NativeValues {
cache_dir: String,
display_density: f32,
device_type: ohos_deviceinfo::OhosDeviceType,
os_full_name: String,
}
/// Gets the resource and cache directory from the native c methods.
fn get_native_values() -> NativeValues {
let cache_dir = {
const BUFFER_SIZE: i32 = 100;
let mut buffer: Vec<u8> = Vec::with_capacity(BUFFER_SIZE as usize);
let mut write_length = 0;
unsafe {
application_context::OH_AbilityRuntime_ApplicationContextGetCacheDir(
buffer.as_mut_ptr().cast(),
BUFFER_SIZE,
&mut write_length,
)
.expect("Call to cache dir failed");
buffer.set_len(write_length as usize);
String::from_utf8(buffer).expect("UTF-8")
}
};
let display_density = unsafe {
let mut density: f32 = 0_f32;
display_manager::OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(&mut density)
.expect("Could not get displaydensity");
density
};
NativeValues {
cache_dir,
display_density,
device_type: ohos_deviceinfo::get_device_type(),
os_full_name: String::from(ohos_deviceinfo::get_os_full_name().unwrap_or("Undefined")),
}
}
/// Initialize Servo. At that point, we need a valid GL context.
/// In the future, this will be done in multiple steps.
pub fn init(
@ -34,6 +77,12 @@ pub fn init(
) -> Result<Rc<RunningAppState>, &'static str> {
info!("Entered simpleservo init function");
crate::init_crypto();
let native_values = get_native_values();
info!("Device Type {:?}", native_values.device_type);
info!("OS Full Name {:?}", native_values.os_full_name);
info!("ResourceDir {:?}", options.resource_dir);
let resource_dir = PathBuf::from(&options.resource_dir).join("servo");
debug!("Resources are located at: {:?}", resource_dir);
resources::set(Box::new(ResourceReaderInstance::new(resource_dir.clone())));
@ -49,7 +98,7 @@ pub fn init(
);
debug!("Servo commandline args: {:?}", args);
let config_dir = PathBuf::from(&options.cache_dir).join("servo");
let config_dir = PathBuf::from(&native_values.cache_dir).join("servo");
debug!("Configs are located at: {:?}", config_dir);
let _ = crate::prefs::DEFAULT_CONFIG_DIR
.set(config_dir.clone())
@ -133,7 +182,7 @@ pub fn init(
let app_state = RunningAppState::new(
Some(options.url),
options.display_density as f32,
native_values.display_density as f32,
rendering_context,
servo,
window_callbacks,