Fix flickering (#9012)
See https://zed.dev/channel/gpui-536 Fixes https://github.com/zed-industries/zed/issues/9010 Fixes https://github.com/zed-industries/zed/issues/8883 Fixes https://github.com/zed-industries/zed/issues/8640 Fixes https://github.com/zed-industries/zed/issues/8598 Fixes https://github.com/zed-industries/zed/issues/8579 Fixes https://github.com/zed-industries/zed/issues/8363 Fixes https://github.com/zed-industries/zed/issues/8207 ### Problem After transitioning Zed to GPUI 2, we started noticing that interacting with the mouse on many UI elements would lead to a pretty annoying flicker. The main issue with the old approach was that hover state was calculated based on the previous frame. That is, when computing whether a given element was hovered in the current frame, we would use information about the same element in the previous frame. However, inspecting the previous frame tells us very little about what should be hovered in the current frame, as elements in the current frame may have changed significantly. ### Solution This pull request's main contribution is the introduction of a new `after_layout` phase when redrawing the window. The key idea is that we'll give every element a chance to register a hitbox (see `ElementContext::insert_hitbox`) before painting anything. Then, during the `paint` phase, elements can determine whether they're the topmost and draw their hover state accordingly. We are also removing the ability to give an arbitrary z-index to elements. Instead, we will follow the much simpler painter's algorithm. That is, an element that gets painted after will be drawn on top of an element that got painted earlier. Elements can still escape their current "stacking context" by using the new `ElementContext::defer_draw` method (see `Overlay` for an example). Elements drawn using this method will still be logically considered as being children of their original parent (for keybinding, focus and cache invalidation purposes) but their layout and paint passes will be deferred until the currently-drawn element is done. With these changes we also reworked geometry batching within the `Scene`. The new approach uses an AABB tree to determine geometry occlusion, which allows the GPU to render non-overlapping geometry in parallel. ### Performance Performance is slightly better than on `main` even though this new approach is more correct and we're maintaining an extra data structure (the AABB tree).  Release Notes: - Fixed a bug that was causing popovers to flicker. --------- Co-authored-by: Nathan Sobo <nathan@zed.dev> Co-authored-by: Thorsten <thorsten@zed.dev>
This commit is contained in:
parent
9afd78b35e
commit
4700d33728
74 changed files with 6434 additions and 6301 deletions
|
@ -107,212 +107,218 @@ fn paint_line(
|
|||
line_height: Pixels,
|
||||
decoration_runs: &[DecorationRun],
|
||||
wrap_boundaries: &[WrapBoundary],
|
||||
cx: &mut ElementContext<'_>,
|
||||
cx: &mut ElementContext,
|
||||
) -> Result<()> {
|
||||
let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
|
||||
let baseline_offset = point(px(0.), padding_top + layout.ascent);
|
||||
let mut decoration_runs = decoration_runs.iter();
|
||||
let mut wraps = wrap_boundaries.iter().peekable();
|
||||
let mut run_end = 0;
|
||||
let mut color = black();
|
||||
let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
|
||||
let mut current_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
|
||||
let mut current_background: Option<(Point<Pixels>, Hsla)> = None;
|
||||
let text_system = cx.text_system().clone();
|
||||
let mut glyph_origin = origin;
|
||||
let mut prev_glyph_position = Point::default();
|
||||
for (run_ix, run) in layout.runs.iter().enumerate() {
|
||||
let max_glyph_size = text_system.bounding_box(run.font_id, layout.font_size).size;
|
||||
let line_bounds = Bounds::new(origin, size(layout.width, line_height));
|
||||
cx.paint_layer(line_bounds, |cx| {
|
||||
let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
|
||||
let baseline_offset = point(px(0.), padding_top + layout.ascent);
|
||||
let mut decoration_runs = decoration_runs.iter();
|
||||
let mut wraps = wrap_boundaries.iter().peekable();
|
||||
let mut run_end = 0;
|
||||
let mut color = black();
|
||||
let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
|
||||
let mut current_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
|
||||
let mut current_background: Option<(Point<Pixels>, Hsla)> = None;
|
||||
let text_system = cx.text_system().clone();
|
||||
let mut glyph_origin = origin;
|
||||
let mut prev_glyph_position = Point::default();
|
||||
for (run_ix, run) in layout.runs.iter().enumerate() {
|
||||
let max_glyph_size = text_system.bounding_box(run.font_id, layout.font_size).size;
|
||||
|
||||
for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
|
||||
glyph_origin.x += glyph.position.x - prev_glyph_position.x;
|
||||
for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
|
||||
glyph_origin.x += glyph.position.x - prev_glyph_position.x;
|
||||
|
||||
if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
|
||||
wraps.next();
|
||||
if let Some((background_origin, background_color)) = current_background.as_mut() {
|
||||
if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
|
||||
wraps.next();
|
||||
if let Some((background_origin, background_color)) = current_background.as_mut()
|
||||
{
|
||||
cx.paint_quad(fill(
|
||||
Bounds {
|
||||
origin: *background_origin,
|
||||
size: size(glyph_origin.x - background_origin.x, line_height),
|
||||
},
|
||||
*background_color,
|
||||
));
|
||||
background_origin.x = origin.x;
|
||||
background_origin.y += line_height;
|
||||
}
|
||||
if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
|
||||
cx.paint_underline(
|
||||
*underline_origin,
|
||||
glyph_origin.x - underline_origin.x,
|
||||
underline_style,
|
||||
);
|
||||
underline_origin.x = origin.x;
|
||||
underline_origin.y += line_height;
|
||||
}
|
||||
if let Some((strikethrough_origin, strikethrough_style)) =
|
||||
current_strikethrough.as_mut()
|
||||
{
|
||||
cx.paint_strikethrough(
|
||||
*strikethrough_origin,
|
||||
glyph_origin.x - strikethrough_origin.x,
|
||||
strikethrough_style,
|
||||
);
|
||||
strikethrough_origin.x = origin.x;
|
||||
strikethrough_origin.y += line_height;
|
||||
}
|
||||
|
||||
glyph_origin.x = origin.x;
|
||||
glyph_origin.y += line_height;
|
||||
}
|
||||
prev_glyph_position = glyph.position;
|
||||
|
||||
let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
|
||||
let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
|
||||
let mut finished_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
|
||||
if glyph.index >= run_end {
|
||||
if let Some(style_run) = decoration_runs.next() {
|
||||
if let Some((_, background_color)) = &mut current_background {
|
||||
if style_run.background_color.as_ref() != Some(background_color) {
|
||||
finished_background = current_background.take();
|
||||
}
|
||||
}
|
||||
if let Some(run_background) = style_run.background_color {
|
||||
current_background.get_or_insert((
|
||||
point(glyph_origin.x, glyph_origin.y),
|
||||
run_background,
|
||||
));
|
||||
}
|
||||
|
||||
if let Some((_, underline_style)) = &mut current_underline {
|
||||
if style_run.underline.as_ref() != Some(underline_style) {
|
||||
finished_underline = current_underline.take();
|
||||
}
|
||||
}
|
||||
if let Some(run_underline) = style_run.underline.as_ref() {
|
||||
current_underline.get_or_insert((
|
||||
point(
|
||||
glyph_origin.x,
|
||||
glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
|
||||
),
|
||||
UnderlineStyle {
|
||||
color: Some(run_underline.color.unwrap_or(style_run.color)),
|
||||
thickness: run_underline.thickness,
|
||||
wavy: run_underline.wavy,
|
||||
},
|
||||
));
|
||||
}
|
||||
if let Some((_, strikethrough_style)) = &mut current_strikethrough {
|
||||
if style_run.strikethrough.as_ref() != Some(strikethrough_style) {
|
||||
finished_strikethrough = current_strikethrough.take();
|
||||
}
|
||||
}
|
||||
if let Some(run_strikethrough) = style_run.strikethrough.as_ref() {
|
||||
current_strikethrough.get_or_insert((
|
||||
point(
|
||||
glyph_origin.x,
|
||||
glyph_origin.y
|
||||
+ (((layout.ascent * 0.5) + baseline_offset.y) * 0.5),
|
||||
),
|
||||
StrikethroughStyle {
|
||||
color: Some(run_strikethrough.color.unwrap_or(style_run.color)),
|
||||
thickness: run_strikethrough.thickness,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
run_end += style_run.len as usize;
|
||||
color = style_run.color;
|
||||
} else {
|
||||
run_end = layout.len;
|
||||
finished_background = current_background.take();
|
||||
finished_underline = current_underline.take();
|
||||
finished_strikethrough = current_strikethrough.take();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((background_origin, background_color)) = finished_background {
|
||||
cx.paint_quad(fill(
|
||||
Bounds {
|
||||
origin: *background_origin,
|
||||
origin: background_origin,
|
||||
size: size(glyph_origin.x - background_origin.x, line_height),
|
||||
},
|
||||
*background_color,
|
||||
background_color,
|
||||
));
|
||||
background_origin.x = origin.x;
|
||||
background_origin.y += line_height;
|
||||
}
|
||||
if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
|
||||
|
||||
if let Some((underline_origin, underline_style)) = finished_underline {
|
||||
cx.paint_underline(
|
||||
*underline_origin,
|
||||
underline_origin,
|
||||
glyph_origin.x - underline_origin.x,
|
||||
underline_style,
|
||||
&underline_style,
|
||||
);
|
||||
underline_origin.x = origin.x;
|
||||
underline_origin.y += line_height;
|
||||
}
|
||||
if let Some((strikethrough_origin, strikethrough_style)) =
|
||||
current_strikethrough.as_mut()
|
||||
{
|
||||
|
||||
if let Some((strikethrough_origin, strikethrough_style)) = finished_strikethrough {
|
||||
cx.paint_strikethrough(
|
||||
*strikethrough_origin,
|
||||
strikethrough_origin,
|
||||
glyph_origin.x - strikethrough_origin.x,
|
||||
strikethrough_style,
|
||||
&strikethrough_style,
|
||||
);
|
||||
strikethrough_origin.x = origin.x;
|
||||
strikethrough_origin.y += line_height;
|
||||
}
|
||||
|
||||
glyph_origin.x = origin.x;
|
||||
glyph_origin.y += line_height;
|
||||
}
|
||||
prev_glyph_position = glyph.position;
|
||||
let max_glyph_bounds = Bounds {
|
||||
origin: glyph_origin,
|
||||
size: max_glyph_size,
|
||||
};
|
||||
|
||||
let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
|
||||
let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
|
||||
let mut finished_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
|
||||
if glyph.index >= run_end {
|
||||
if let Some(style_run) = decoration_runs.next() {
|
||||
if let Some((_, background_color)) = &mut current_background {
|
||||
if style_run.background_color.as_ref() != Some(background_color) {
|
||||
finished_background = current_background.take();
|
||||
}
|
||||
let content_mask = cx.content_mask();
|
||||
if max_glyph_bounds.intersects(&content_mask.bounds) {
|
||||
if glyph.is_emoji {
|
||||
cx.paint_emoji(
|
||||
glyph_origin + baseline_offset,
|
||||
run.font_id,
|
||||
glyph.id,
|
||||
layout.font_size,
|
||||
)?;
|
||||
} else {
|
||||
cx.paint_glyph(
|
||||
glyph_origin + baseline_offset,
|
||||
run.font_id,
|
||||
glyph.id,
|
||||
layout.font_size,
|
||||
color,
|
||||
)?;
|
||||
}
|
||||
if let Some(run_background) = style_run.background_color {
|
||||
current_background
|
||||
.get_or_insert((point(glyph_origin.x, glyph_origin.y), run_background));
|
||||
}
|
||||
|
||||
if let Some((_, underline_style)) = &mut current_underline {
|
||||
if style_run.underline.as_ref() != Some(underline_style) {
|
||||
finished_underline = current_underline.take();
|
||||
}
|
||||
}
|
||||
if let Some(run_underline) = style_run.underline.as_ref() {
|
||||
current_underline.get_or_insert((
|
||||
point(
|
||||
glyph_origin.x,
|
||||
glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
|
||||
),
|
||||
UnderlineStyle {
|
||||
color: Some(run_underline.color.unwrap_or(style_run.color)),
|
||||
thickness: run_underline.thickness,
|
||||
wavy: run_underline.wavy,
|
||||
},
|
||||
));
|
||||
}
|
||||
if let Some((_, strikethrough_style)) = &mut current_strikethrough {
|
||||
if style_run.strikethrough.as_ref() != Some(strikethrough_style) {
|
||||
finished_strikethrough = current_strikethrough.take();
|
||||
}
|
||||
}
|
||||
if let Some(run_strikethrough) = style_run.strikethrough.as_ref() {
|
||||
current_strikethrough.get_or_insert((
|
||||
point(
|
||||
glyph_origin.x,
|
||||
glyph_origin.y
|
||||
+ (((layout.ascent * 0.5) + baseline_offset.y) * 0.5),
|
||||
),
|
||||
StrikethroughStyle {
|
||||
color: Some(run_strikethrough.color.unwrap_or(style_run.color)),
|
||||
thickness: run_strikethrough.thickness,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
run_end += style_run.len as usize;
|
||||
color = style_run.color;
|
||||
} else {
|
||||
run_end = layout.len;
|
||||
finished_background = current_background.take();
|
||||
finished_underline = current_underline.take();
|
||||
finished_strikethrough = current_strikethrough.take();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((background_origin, background_color)) = finished_background {
|
||||
cx.paint_quad(fill(
|
||||
Bounds {
|
||||
origin: background_origin,
|
||||
size: size(glyph_origin.x - background_origin.x, line_height),
|
||||
},
|
||||
background_color,
|
||||
));
|
||||
}
|
||||
|
||||
if let Some((underline_origin, underline_style)) = finished_underline {
|
||||
cx.paint_underline(
|
||||
underline_origin,
|
||||
glyph_origin.x - underline_origin.x,
|
||||
&underline_style,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some((strikethrough_origin, strikethrough_style)) = finished_strikethrough {
|
||||
cx.paint_strikethrough(
|
||||
strikethrough_origin,
|
||||
glyph_origin.x - strikethrough_origin.x,
|
||||
&strikethrough_style,
|
||||
);
|
||||
}
|
||||
|
||||
let max_glyph_bounds = Bounds {
|
||||
origin: glyph_origin,
|
||||
size: max_glyph_size,
|
||||
};
|
||||
|
||||
let content_mask = cx.content_mask();
|
||||
if max_glyph_bounds.intersects(&content_mask.bounds) {
|
||||
if glyph.is_emoji {
|
||||
cx.paint_emoji(
|
||||
glyph_origin + baseline_offset,
|
||||
run.font_id,
|
||||
glyph.id,
|
||||
layout.font_size,
|
||||
)?;
|
||||
} else {
|
||||
cx.paint_glyph(
|
||||
glyph_origin + baseline_offset,
|
||||
run.font_id,
|
||||
glyph.id,
|
||||
layout.font_size,
|
||||
color,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut last_line_end_x = origin.x + layout.width;
|
||||
if let Some(boundary) = wrap_boundaries.last() {
|
||||
let run = &layout.runs[boundary.run_ix];
|
||||
let glyph = &run.glyphs[boundary.glyph_ix];
|
||||
last_line_end_x -= glyph.position.x;
|
||||
}
|
||||
let mut last_line_end_x = origin.x + layout.width;
|
||||
if let Some(boundary) = wrap_boundaries.last() {
|
||||
let run = &layout.runs[boundary.run_ix];
|
||||
let glyph = &run.glyphs[boundary.glyph_ix];
|
||||
last_line_end_x -= glyph.position.x;
|
||||
}
|
||||
|
||||
if let Some((background_origin, background_color)) = current_background.take() {
|
||||
cx.paint_quad(fill(
|
||||
Bounds {
|
||||
origin: background_origin,
|
||||
size: size(last_line_end_x - background_origin.x, line_height),
|
||||
},
|
||||
background_color,
|
||||
));
|
||||
}
|
||||
if let Some((background_origin, background_color)) = current_background.take() {
|
||||
cx.paint_quad(fill(
|
||||
Bounds {
|
||||
origin: background_origin,
|
||||
size: size(last_line_end_x - background_origin.x, line_height),
|
||||
},
|
||||
background_color,
|
||||
));
|
||||
}
|
||||
|
||||
if let Some((underline_start, underline_style)) = current_underline.take() {
|
||||
cx.paint_underline(
|
||||
underline_start,
|
||||
last_line_end_x - underline_start.x,
|
||||
&underline_style,
|
||||
);
|
||||
}
|
||||
if let Some((underline_start, underline_style)) = current_underline.take() {
|
||||
cx.paint_underline(
|
||||
underline_start,
|
||||
last_line_end_x - underline_start.x,
|
||||
&underline_style,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some((strikethrough_start, strikethrough_style)) = current_strikethrough.take() {
|
||||
cx.paint_strikethrough(
|
||||
strikethrough_start,
|
||||
last_line_end_x - strikethrough_start.x,
|
||||
&strikethrough_style,
|
||||
);
|
||||
}
|
||||
if let Some((strikethrough_start, strikethrough_style)) = current_strikethrough.take() {
|
||||
cx.paint_strikethrough(
|
||||
strikethrough_start,
|
||||
last_line_end_x - strikethrough_start.x,
|
||||
&strikethrough_style,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use crate::{px, EntityId, FontId, GlyphId, Pixels, PlatformTextSystem, Point, Size};
|
||||
use collections::{FxHashMap, FxHashSet};
|
||||
use crate::{px, FontId, GlyphId, Pixels, PlatformTextSystem, Point, Size};
|
||||
use collections::FxHashMap;
|
||||
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
borrow::Borrow,
|
||||
hash::{Hash, Hasher},
|
||||
ops::Range,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
|
@ -277,63 +278,71 @@ impl WrappedLineLayout {
|
|||
}
|
||||
|
||||
pub(crate) struct LineLayoutCache {
|
||||
view_stack: Mutex<Vec<EntityId>>,
|
||||
previous_frame: Mutex<FxHashMap<CacheKey, Arc<LineLayout>>>,
|
||||
current_frame: RwLock<FxHashMap<CacheKey, Arc<LineLayout>>>,
|
||||
previous_frame_wrapped: Mutex<FxHashMap<CacheKey, Arc<WrappedLineLayout>>>,
|
||||
current_frame_wrapped: RwLock<FxHashMap<CacheKey, Arc<WrappedLineLayout>>>,
|
||||
previous_frame: Mutex<FrameCache>,
|
||||
current_frame: RwLock<FrameCache>,
|
||||
platform_text_system: Arc<dyn PlatformTextSystem>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FrameCache {
|
||||
lines: FxHashMap<Arc<CacheKey>, Arc<LineLayout>>,
|
||||
wrapped_lines: FxHashMap<Arc<CacheKey>, Arc<WrappedLineLayout>>,
|
||||
used_lines: Vec<Arc<CacheKey>>,
|
||||
used_wrapped_lines: Vec<Arc<CacheKey>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct LineLayoutIndex {
|
||||
lines_index: usize,
|
||||
wrapped_lines_index: usize,
|
||||
}
|
||||
|
||||
impl LineLayoutCache {
|
||||
pub fn new(platform_text_system: Arc<dyn PlatformTextSystem>) -> Self {
|
||||
Self {
|
||||
view_stack: Mutex::default(),
|
||||
previous_frame: Mutex::default(),
|
||||
current_frame: RwLock::default(),
|
||||
previous_frame_wrapped: Mutex::default(),
|
||||
current_frame_wrapped: RwLock::default(),
|
||||
platform_text_system,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish_frame(&self, reused_views: &FxHashSet<EntityId>) {
|
||||
debug_assert_eq!(self.view_stack.lock().len(), 0);
|
||||
pub fn layout_index(&self) -> LineLayoutIndex {
|
||||
let frame = self.current_frame.read();
|
||||
LineLayoutIndex {
|
||||
lines_index: frame.used_lines.len(),
|
||||
wrapped_lines_index: frame.used_wrapped_lines.len(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reuse_layouts(&self, range: Range<LineLayoutIndex>) {
|
||||
let mut previous_frame = &mut *self.previous_frame.lock();
|
||||
let mut current_frame = &mut *self.current_frame.write();
|
||||
|
||||
for key in &previous_frame.used_lines[range.start.lines_index..range.end.lines_index] {
|
||||
if let Some((key, line)) = previous_frame.lines.remove_entry(key) {
|
||||
current_frame.lines.insert(key, line);
|
||||
}
|
||||
current_frame.used_lines.push(key.clone());
|
||||
}
|
||||
|
||||
for key in &previous_frame.used_wrapped_lines
|
||||
[range.start.wrapped_lines_index..range.end.wrapped_lines_index]
|
||||
{
|
||||
if let Some((key, line)) = previous_frame.wrapped_lines.remove_entry(key) {
|
||||
current_frame.wrapped_lines.insert(key, line);
|
||||
}
|
||||
current_frame.used_wrapped_lines.push(key.clone());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish_frame(&self) {
|
||||
let mut prev_frame = self.previous_frame.lock();
|
||||
let mut curr_frame = self.current_frame.write();
|
||||
for (key, layout) in prev_frame.drain() {
|
||||
if key
|
||||
.parent_view_id
|
||||
.map_or(false, |view_id| reused_views.contains(&view_id))
|
||||
{
|
||||
curr_frame.insert(key, layout);
|
||||
}
|
||||
}
|
||||
std::mem::swap(&mut *prev_frame, &mut *curr_frame);
|
||||
|
||||
let mut prev_frame_wrapped = self.previous_frame_wrapped.lock();
|
||||
let mut curr_frame_wrapped = self.current_frame_wrapped.write();
|
||||
for (key, layout) in prev_frame_wrapped.drain() {
|
||||
if key
|
||||
.parent_view_id
|
||||
.map_or(false, |view_id| reused_views.contains(&view_id))
|
||||
{
|
||||
curr_frame_wrapped.insert(key, layout);
|
||||
}
|
||||
}
|
||||
std::mem::swap(&mut *prev_frame_wrapped, &mut *curr_frame_wrapped);
|
||||
}
|
||||
|
||||
pub fn with_view<R>(&self, view_id: EntityId, f: impl FnOnce() -> R) -> R {
|
||||
self.view_stack.lock().push(view_id);
|
||||
let result = f();
|
||||
self.view_stack.lock().pop();
|
||||
result
|
||||
}
|
||||
|
||||
fn parent_view_id(&self) -> Option<EntityId> {
|
||||
self.view_stack.lock().last().copied()
|
||||
curr_frame.lines.clear();
|
||||
curr_frame.wrapped_lines.clear();
|
||||
curr_frame.used_lines.clear();
|
||||
curr_frame.used_wrapped_lines.clear();
|
||||
}
|
||||
|
||||
pub fn layout_wrapped_line(
|
||||
|
@ -348,19 +357,24 @@ impl LineLayoutCache {
|
|||
font_size,
|
||||
runs,
|
||||
wrap_width,
|
||||
parent_view_id: self.parent_view_id(),
|
||||
} as &dyn AsCacheKeyRef;
|
||||
|
||||
let current_frame = self.current_frame_wrapped.upgradable_read();
|
||||
if let Some(layout) = current_frame.get(key) {
|
||||
let current_frame = self.current_frame.upgradable_read();
|
||||
if let Some(layout) = current_frame.wrapped_lines.get(key) {
|
||||
return layout.clone();
|
||||
}
|
||||
|
||||
let mut current_frame = RwLockUpgradableReadGuard::upgrade(current_frame);
|
||||
if let Some((key, layout)) = self.previous_frame_wrapped.lock().remove_entry(key) {
|
||||
current_frame.insert(key, layout.clone());
|
||||
let previous_frame_entry = self.previous_frame.lock().wrapped_lines.remove_entry(key);
|
||||
if let Some((key, layout)) = previous_frame_entry {
|
||||
let mut current_frame = RwLockUpgradableReadGuard::upgrade(current_frame);
|
||||
current_frame
|
||||
.wrapped_lines
|
||||
.insert(key.clone(), layout.clone());
|
||||
current_frame.used_wrapped_lines.push(key);
|
||||
layout
|
||||
} else {
|
||||
drop(current_frame);
|
||||
|
||||
let unwrapped_layout = self.layout_line(text, font_size, runs);
|
||||
let wrap_boundaries = if let Some(wrap_width) = wrap_width {
|
||||
unwrapped_layout.compute_wrap_boundaries(text.as_ref(), wrap_width)
|
||||
|
@ -372,14 +386,19 @@ impl LineLayoutCache {
|
|||
wrap_boundaries,
|
||||
wrap_width,
|
||||
});
|
||||
let key = CacheKey {
|
||||
let key = Arc::new(CacheKey {
|
||||
text: text.into(),
|
||||
font_size,
|
||||
runs: SmallVec::from(runs),
|
||||
wrap_width,
|
||||
parent_view_id: self.parent_view_id(),
|
||||
};
|
||||
current_frame.insert(key, layout.clone());
|
||||
});
|
||||
|
||||
let mut current_frame = self.current_frame.write();
|
||||
current_frame
|
||||
.wrapped_lines
|
||||
.insert(key.clone(), layout.clone());
|
||||
current_frame.used_wrapped_lines.push(key);
|
||||
|
||||
layout
|
||||
}
|
||||
}
|
||||
|
@ -390,28 +409,28 @@ impl LineLayoutCache {
|
|||
font_size,
|
||||
runs,
|
||||
wrap_width: None,
|
||||
parent_view_id: self.parent_view_id(),
|
||||
} as &dyn AsCacheKeyRef;
|
||||
|
||||
let current_frame = self.current_frame.upgradable_read();
|
||||
if let Some(layout) = current_frame.get(key) {
|
||||
if let Some(layout) = current_frame.lines.get(key) {
|
||||
return layout.clone();
|
||||
}
|
||||
|
||||
let mut current_frame = RwLockUpgradableReadGuard::upgrade(current_frame);
|
||||
if let Some((key, layout)) = self.previous_frame.lock().remove_entry(key) {
|
||||
current_frame.insert(key, layout.clone());
|
||||
if let Some((key, layout)) = self.previous_frame.lock().lines.remove_entry(key) {
|
||||
current_frame.lines.insert(key.clone(), layout.clone());
|
||||
current_frame.used_lines.push(key);
|
||||
layout
|
||||
} else {
|
||||
let layout = Arc::new(self.platform_text_system.layout_line(text, font_size, runs));
|
||||
let key = CacheKey {
|
||||
let key = Arc::new(CacheKey {
|
||||
text: text.into(),
|
||||
font_size,
|
||||
runs: SmallVec::from(runs),
|
||||
wrap_width: None,
|
||||
parent_view_id: self.parent_view_id(),
|
||||
};
|
||||
current_frame.insert(key, layout.clone());
|
||||
});
|
||||
current_frame.lines.insert(key.clone(), layout.clone());
|
||||
current_frame.used_lines.push(key);
|
||||
layout
|
||||
}
|
||||
}
|
||||
|
@ -428,13 +447,12 @@ trait AsCacheKeyRef {
|
|||
fn as_cache_key_ref(&self) -> CacheKeyRef;
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq)]
|
||||
#[derive(Clone, Debug, Eq)]
|
||||
struct CacheKey {
|
||||
text: String,
|
||||
font_size: Pixels,
|
||||
runs: SmallVec<[FontRun; 1]>,
|
||||
wrap_width: Option<Pixels>,
|
||||
parent_view_id: Option<EntityId>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
|
@ -443,7 +461,6 @@ struct CacheKeyRef<'a> {
|
|||
font_size: Pixels,
|
||||
runs: &'a [FontRun],
|
||||
wrap_width: Option<Pixels>,
|
||||
parent_view_id: Option<EntityId>,
|
||||
}
|
||||
|
||||
impl<'a> PartialEq for (dyn AsCacheKeyRef + 'a) {
|
||||
|
@ -467,7 +484,6 @@ impl AsCacheKeyRef for CacheKey {
|
|||
font_size: self.font_size,
|
||||
runs: self.runs.as_slice(),
|
||||
wrap_width: self.wrap_width,
|
||||
parent_view_id: self.parent_view_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -484,9 +500,9 @@ impl Hash for CacheKey {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Borrow<dyn AsCacheKeyRef + 'a> for CacheKey {
|
||||
impl<'a> Borrow<dyn AsCacheKeyRef + 'a> for Arc<CacheKey> {
|
||||
fn borrow(&self) -> &(dyn AsCacheKeyRef + 'a) {
|
||||
self as &dyn AsCacheKeyRef
|
||||
self.as_ref() as &dyn AsCacheKeyRef
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue