Skip to content

Commit

Permalink
Fix hiDPI scale factor for translations
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Jan 2, 2024
1 parent c8d3205 commit 3f81cce
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 0 additions & 2 deletions azul-core/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,6 @@ impl TessellatedGPUSvgNode {
}

/// Draw the vertex buffer to the texture with the given color and transform
///
/// Will resize the texture if necessary.
pub fn draw(
&self,
texture: &mut Texture,
Expand Down
16 changes: 12 additions & 4 deletions azul-core/src/ui_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2096,14 +2096,20 @@ impl ComputedTransform3D {
#[inline]
pub const fn new_scale(x: f32, y: f32, z: f32) -> Self {
Self::new(
x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, 1.0,
x, 0.0, 0.0, 0.0,
0.0, y, 0.0, 0.0,
0.0, 0.0, z, 0.0,
0.0, 0.0, 0.0, 1.0,
)
}

#[inline]
pub const fn new_translation(x: f32, y: f32, z: f32) -> Self {
Self::new(
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, x, y, z, 1.0,
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0,
)
}

Expand Down Expand Up @@ -2209,8 +2215,10 @@ impl ComputedTransform3D {
}

pub fn scale_for_dpi(&mut self, scale_factor: f32) {
let scale_matrix = Self::new_scale(scale_factor, scale_factor, 1.0);
*self = scale_matrix.then(&self);
// only scale the translation, don't scale anything else
self.m[3][0] *= scale_factor;
self.m[3][1] *= scale_factor;
self.m[3][2] *= scale_factor;
}

/// Computes the sum of two matrices while applying `other` AFTER the current matrix.
Expand Down
6 changes: 6 additions & 0 deletions azul-core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,12 @@ pub struct WindowInternal {
pub threads: BTreeMap<ThreadId, Thread>,
}

impl WindowInternal {
pub fn get_dpi_scale_factor(&self) -> DpiScaleFactor {
DpiScaleFactor { inner: FloatValue::new(self.current_window_state.size.get_hidpi_factor()) }
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct FullHitTest {
pub hovered_nodes: BTreeMap<DomId, HitTest>,
Expand Down
2 changes: 1 addition & 1 deletion azul-desktop/src/shell/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2633,7 +2633,7 @@ unsafe extern "system" fn WindowProc(
image_cache,
gl_context,
&mut resource_updates,
DpiScaleFactor { inner: FloatValue::new(internal.current_window_state.size.get_hidpi_factor()) },
internal.get_dpi_scale_factor(),
&crate::app::CALLBACKS,
fc_cache,
azul_layout::do_the_relayout,
Expand Down
16 changes: 11 additions & 5 deletions azul-desktop/src/wr_translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! (since webrender is a huge dependency) just to use the types. Only if you depend on
//! azul (not azul-core), you have to depend on webrender.
use azul_core::app_resources::DpiScaleFactor;
use webrender::render_api::{
RenderApi as WrRenderApi,
ResourceUpdate as WrResourceUpdate,
Expand Down Expand Up @@ -295,7 +296,7 @@ pub(crate) fn scroll_all_nodes(scroll_states: &ScrollStates, txn: &mut WrTransac
}

/// Synchronize transform / opacity keys
pub(crate) fn synchronize_gpu_values(layout_results: &[LayoutResult], txn: &mut WrTransaction) {
pub(crate) fn synchronize_gpu_values(layout_results: &[LayoutResult], dpi: &DpiScaleFactor, txn: &mut WrTransaction) {

use webrender::api::{
PropertyBindingKey as WrPropertyBindingKey,
Expand All @@ -306,8 +307,9 @@ pub(crate) fn synchronize_gpu_values(layout_results: &[LayoutResult], txn: &mut

let transforms = layout_results.iter().flat_map(|lr| {
lr.gpu_value_cache.transform_keys.iter().filter_map(|(nid, key)| {
let value = lr.gpu_value_cache.current_transform_values.get(nid)?;
Some((key, value.clone()))
let mut value = lr.gpu_value_cache.current_transform_values.get(nid).cloned()?;
value.scale_for_dpi(dpi.inner.get());
Some((key, value))
}).collect::<Vec<_>>().into_iter()
})
.map(|(k, v)| WrPropertyValue {
Expand Down Expand Up @@ -421,7 +423,7 @@ pub(crate) fn rebuild_display_list(
}

/// Generates a new frame for webrender
#[cfg(not(test))]
// #[cfg(not(test))]
pub(crate) fn generate_frame(
internal: &mut WindowInternal,
render_api: &mut WrRenderApi,
Expand All @@ -448,7 +450,11 @@ pub(crate) fn generate_frame(
txn.set_root_pipeline(wr_translate_pipeline_id(PipelineId(0, internal.document_id.id)));
txn.set_document_view(WrDeviceIntRect::from_origin_and_size(WrDeviceIntPoint::new(0, 0), framebuffer_size));
scroll_all_nodes(&mut internal.scroll_states, &mut txn);
synchronize_gpu_values(&internal.layout_results, &mut txn);
synchronize_gpu_values(
&internal.layout_results,
&internal.get_dpi_scale_factor(),
&mut txn
);

if !display_list_was_rebuilt {
txn.skip_scene_builder(); // avoid rebuilding the scene if DL hasn't changed
Expand Down

0 comments on commit 3f81cce

Please sign in to comment.