windows: Remove Send and Sync implementation of DirectWrite (#15263)

This PR uses the `AgileReference` provided by the `windows-rs` crate to
correctly implement `Send` and `Sync` for `DirectWrite`.

Release Notes:

- N/A
This commit is contained in:
张小白 2024-07-27 07:46:06 +08:00 committed by GitHub
parent a1bd7a1297
commit e6cd1cf22b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 59 additions and 29 deletions

View file

@ -13,6 +13,11 @@ use std::{
rc::{Rc, Weak},
sync::Arc,
};
#[cfg(target_os = "windows")]
use windows::Win32::{
Graphics::Imaging::{CLSID_WICImagingFactory, IWICImagingFactory},
System::Com::{CoCreateInstance, CLSCTX_INPROC_SERVER},
};
/// TestPlatform implements the Platform trait for use in tests.
pub(crate) struct TestPlatform {
@ -28,6 +33,8 @@ pub(crate) struct TestPlatform {
pub(crate) prompts: RefCell<TestPrompts>,
pub opened_url: RefCell<Option<String>>,
pub text_system: Arc<dyn PlatformTextSystem>,
#[cfg(target_os = "windows")]
bitmap_factory: std::mem::ManuallyDrop<IWICImagingFactory>,
weak: Weak<Self>,
}
@ -40,10 +47,14 @@ pub(crate) struct TestPrompts {
impl TestPlatform {
pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Rc<Self> {
#[cfg(target_os = "windows")]
unsafe {
let bitmap_factory = unsafe {
windows::Win32::System::Ole::OleInitialize(None)
.expect("unable to initialize Windows OLE");
}
std::mem::ManuallyDrop::new(
CoCreateInstance(&CLSID_WICImagingFactory, None, CLSCTX_INPROC_SERVER)
.expect("Error creating bitmap factory."),
)
};
#[cfg(target_os = "macos")]
let text_system = Arc::new(crate::platform::mac::MacTextSystem::new());
@ -52,7 +63,10 @@ impl TestPlatform {
let text_system = Arc::new(crate::platform::linux::CosmicTextSystem::new());
#[cfg(target_os = "windows")]
let text_system = Arc::new(crate::platform::windows::DirectWriteTextSystem::new().unwrap());
let text_system = Arc::new(
crate::platform::windows::DirectWriteTextSystem::new(&bitmap_factory)
.expect("Unable to initialize direct write."),
);
Rc::new_cyclic(|weak| TestPlatform {
background_executor: executor,
@ -66,6 +80,8 @@ impl TestPlatform {
current_primary_item: Mutex::new(None),
weak: weak.clone(),
opened_url: Default::default(),
#[cfg(target_os = "windows")]
bitmap_factory,
text_system,
})
}
@ -303,3 +319,13 @@ impl Platform for TestPlatform {
unimplemented!()
}
}
#[cfg(target_os = "windows")]
impl Drop for TestPlatform {
fn drop(&mut self) {
unsafe {
std::mem::ManuallyDrop::drop(&mut self.bitmap_factory);
windows::Win32::System::Ole::OleUninitialize();
}
}
}