vulkan: optimize shm handling
This commit is contained in:
parent
03c02be34c
commit
af80fada6c
14 changed files with 629 additions and 413 deletions
|
|
@ -4,25 +4,32 @@ use {
|
|||
clientmem::{ClientMem, ClientMemError, ClientMemOffset},
|
||||
format::{Format, ARGB8888},
|
||||
gfx_api::{GfxError, GfxFramebuffer, GfxImage, GfxTexture},
|
||||
ifs::wl_surface::WlSurface,
|
||||
leaks::Tracker,
|
||||
object::{Object, Version},
|
||||
rect::Rect,
|
||||
theme::Color,
|
||||
utils::{clonecell::CloneCell, errorfmt::ErrorFmt},
|
||||
utils::errorfmt::ErrorFmt,
|
||||
video::dmabuf::DmaBuf,
|
||||
wire::{wl_buffer::*, WlBufferId},
|
||||
},
|
||||
std::{
|
||||
cell::{Cell, RefCell},
|
||||
ops::Deref,
|
||||
rc::Rc,
|
||||
},
|
||||
thiserror::Error,
|
||||
};
|
||||
|
||||
pub enum WlBufferStorage {
|
||||
Shm { mem: ClientMemOffset, stride: i32 },
|
||||
Dmabuf(Rc<dyn GfxImage>),
|
||||
Shm {
|
||||
mem: ClientMemOffset,
|
||||
stride: i32,
|
||||
},
|
||||
Dmabuf {
|
||||
img: Rc<dyn GfxImage>,
|
||||
tex: Option<Rc<dyn GfxTexture>>,
|
||||
fb: Option<Rc<dyn GfxFramebuffer>>,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct WlBuffer {
|
||||
|
|
@ -35,8 +42,6 @@ pub struct WlBuffer {
|
|||
render_ctx_version: Cell<u32>,
|
||||
pub storage: RefCell<Option<WlBufferStorage>>,
|
||||
pub color: Option<Color>,
|
||||
pub texture: CloneCell<Option<Rc<dyn GfxTexture>>>,
|
||||
pub famebuffer: CloneCell<Option<Rc<dyn GfxFramebuffer>>>,
|
||||
width: i32,
|
||||
height: i32,
|
||||
pub tracker: Tracker<Self>,
|
||||
|
|
@ -65,11 +70,13 @@ impl WlBuffer {
|
|||
format,
|
||||
width,
|
||||
height,
|
||||
texture: CloneCell::new(None),
|
||||
famebuffer: Default::default(),
|
||||
dmabuf: Some(dmabuf),
|
||||
render_ctx_version: Cell::new(client.state.render_ctx_version.get()),
|
||||
storage: RefCell::new(Some(WlBufferStorage::Dmabuf(img.clone()))),
|
||||
storage: RefCell::new(Some(WlBufferStorage::Dmabuf {
|
||||
img: img.clone(),
|
||||
tex: None,
|
||||
fb: None,
|
||||
})),
|
||||
tracker: Default::default(),
|
||||
color: None,
|
||||
}
|
||||
|
|
@ -110,9 +117,7 @@ impl WlBuffer {
|
|||
storage: RefCell::new(Some(WlBufferStorage::Shm { mem, stride })),
|
||||
width,
|
||||
height,
|
||||
texture: CloneCell::new(None),
|
||||
tracker: Default::default(),
|
||||
famebuffer: Default::default(),
|
||||
color: None,
|
||||
})
|
||||
}
|
||||
|
|
@ -136,78 +141,110 @@ impl WlBuffer {
|
|||
storage: RefCell::new(None),
|
||||
width: 1,
|
||||
height: 1,
|
||||
texture: CloneCell::new(None),
|
||||
tracker: Default::default(),
|
||||
famebuffer: Default::default(),
|
||||
color: Some(Color::from_u32_rgba_premultiplied(r, g, b, a)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_gfx_context_change(&self) {
|
||||
pub fn handle_gfx_context_change(&self, surface: Option<&WlSurface>) {
|
||||
let ctx_version = self.client.state.render_ctx_version.get();
|
||||
if self.render_ctx_version.replace(ctx_version) == ctx_version {
|
||||
return;
|
||||
}
|
||||
let had_texture = self.texture.set(None).is_some();
|
||||
self.famebuffer.set(None);
|
||||
self.reset_storage_after_gfx_context_change();
|
||||
let had_texture = self.reset_gfx_objects(surface);
|
||||
if had_texture {
|
||||
self.update_texture_or_log();
|
||||
}
|
||||
}
|
||||
|
||||
fn reset_storage_after_gfx_context_change(&self) {
|
||||
let mut storage = self.storage.borrow_mut();
|
||||
if let Some(storage) = &mut *storage {
|
||||
if let WlBufferStorage::Shm { .. } = storage {
|
||||
return;
|
||||
if let Some(surface) = surface {
|
||||
self.update_texture_or_log(surface, None);
|
||||
}
|
||||
}
|
||||
*storage = None;
|
||||
let ctx = match self.client.state.render_ctx.get() {
|
||||
Some(ctx) => ctx,
|
||||
_ => return,
|
||||
}
|
||||
|
||||
fn reset_gfx_objects(&self, surface: Option<&WlSurface>) -> bool {
|
||||
let mut storage = self.storage.borrow_mut();
|
||||
let Some(s) = &mut *storage else {
|
||||
return false;
|
||||
};
|
||||
if let Some(dmabuf) = &self.dmabuf {
|
||||
let image = match ctx.dmabuf_img(dmabuf) {
|
||||
Ok(image) => image,
|
||||
Err(e) => {
|
||||
log::error!(
|
||||
"Cannot re-import wl_buffer after graphics context change: {}",
|
||||
ErrorFmt(e)
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
*storage = Some(WlBufferStorage::Dmabuf(image));
|
||||
let had_texture = match s {
|
||||
WlBufferStorage::Shm { .. } => {
|
||||
return match surface {
|
||||
Some(s) => s.shm_texture.take().is_some(),
|
||||
None => false,
|
||||
};
|
||||
}
|
||||
WlBufferStorage::Dmabuf { tex, .. } => tex.is_some(),
|
||||
};
|
||||
*storage = None;
|
||||
let Some(ctx) = self.client.state.render_ctx.get() else {
|
||||
return false;
|
||||
};
|
||||
let Some(dmabuf) = &self.dmabuf else {
|
||||
return false;
|
||||
};
|
||||
let img = match ctx.dmabuf_img(dmabuf) {
|
||||
Ok(image) => image,
|
||||
Err(e) => {
|
||||
log::error!(
|
||||
"Cannot re-import wl_buffer after graphics context change: {}",
|
||||
ErrorFmt(e)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
*storage = Some(WlBufferStorage::Dmabuf {
|
||||
img,
|
||||
tex: None,
|
||||
fb: None,
|
||||
});
|
||||
had_texture
|
||||
}
|
||||
|
||||
pub fn get_texture(&self, surface: &WlSurface) -> Option<Rc<dyn GfxTexture>> {
|
||||
match &*self.storage.borrow() {
|
||||
None => None,
|
||||
Some(s) => match s {
|
||||
WlBufferStorage::Shm { .. } => surface.shm_texture.get(),
|
||||
WlBufferStorage::Dmabuf { tex, .. } => tex.clone(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_texture_or_log(&self) {
|
||||
if let Err(e) = self.update_texture() {
|
||||
pub fn update_texture_or_log(&self, surface: &WlSurface, damage: Option<&[Rect]>) {
|
||||
if let Err(e) = self.update_texture(surface, damage) {
|
||||
log::warn!("Could not update texture: {}", ErrorFmt(e));
|
||||
}
|
||||
}
|
||||
|
||||
fn update_texture(&self) -> Result<(), WlBufferError> {
|
||||
let storage = self.storage.borrow_mut();
|
||||
let storage = match storage.deref() {
|
||||
fn update_texture(
|
||||
&self,
|
||||
surface: &WlSurface,
|
||||
damage: Option<&[Rect]>,
|
||||
) -> Result<(), WlBufferError> {
|
||||
let old_shm_texture = surface.shm_texture.take();
|
||||
let storage = &mut *self.storage.borrow_mut();
|
||||
let storage = match storage {
|
||||
Some(s) => s,
|
||||
_ => return Ok(()),
|
||||
};
|
||||
match storage {
|
||||
WlBufferStorage::Shm { mem, stride } => {
|
||||
let old = self.texture.take();
|
||||
if let Some(ctx) = self.client.state.render_ctx.get() {
|
||||
let tex = mem.access(|mem| {
|
||||
ctx.shmem_texture(old, mem, self.format, self.width, self.height, *stride)
|
||||
ctx.shmem_texture(
|
||||
old_shm_texture,
|
||||
mem,
|
||||
self.format,
|
||||
self.width,
|
||||
self.height,
|
||||
*stride,
|
||||
damage,
|
||||
)
|
||||
})??;
|
||||
self.texture.set(Some(tex));
|
||||
surface.shm_texture.set(Some(tex));
|
||||
}
|
||||
}
|
||||
WlBufferStorage::Dmabuf(img) => {
|
||||
if self.texture.is_none() {
|
||||
self.texture.set(Some(img.clone().to_texture()?));
|
||||
WlBufferStorage::Dmabuf { img, tex, .. } => {
|
||||
if tex.is_none() {
|
||||
*tex = Some(img.clone().to_texture()?);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -215,8 +252,8 @@ impl WlBuffer {
|
|||
}
|
||||
|
||||
pub fn update_framebuffer(&self) -> Result<(), WlBufferError> {
|
||||
let storage = self.storage.borrow_mut();
|
||||
let storage = match storage.deref() {
|
||||
let storage = &mut *self.storage.borrow_mut();
|
||||
let storage = match storage {
|
||||
Some(s) => s,
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
|
@ -224,9 +261,9 @@ impl WlBuffer {
|
|||
WlBufferStorage::Shm { .. } => {
|
||||
// nothing
|
||||
}
|
||||
WlBufferStorage::Dmabuf(img) => {
|
||||
if self.famebuffer.is_none() {
|
||||
self.famebuffer.set(Some(img.clone().to_framebuffer()?));
|
||||
WlBufferStorage::Dmabuf { img, fb, .. } => {
|
||||
if fb.is_none() {
|
||||
*fb = Some(img.clone().to_framebuffer()?);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@ use {
|
|||
cursor_user::{CursorUser, CursorUserId},
|
||||
drm_feedback::DrmFeedback,
|
||||
fixed::Fixed,
|
||||
gfx_api::{AcquireSync, BufferResv, BufferResvUser, ReleaseSync, SampleRect, SyncFile},
|
||||
gfx_api::{
|
||||
AcquireSync, BufferResv, BufferResvUser, GfxTexture, ReleaseSync, SampleRect, SyncFile,
|
||||
},
|
||||
ifs::{
|
||||
wl_buffer::WlBuffer,
|
||||
wl_callback::WlCallback,
|
||||
|
|
@ -261,6 +263,7 @@ pub struct WlSurface {
|
|||
pub buffer_abs_pos: Cell<Rect>,
|
||||
pub need_extents_update: Cell<bool>,
|
||||
pub buffer: CloneCell<Option<Rc<SurfaceBuffer>>>,
|
||||
pub shm_texture: CloneCell<Option<Rc<dyn GfxTexture>>>,
|
||||
pub buf_x: NumCell<i32>,
|
||||
pub buf_y: NumCell<i32>,
|
||||
pub children: RefCell<Option<Box<ParentData>>>,
|
||||
|
|
@ -393,7 +396,8 @@ struct PendingState {
|
|||
opaque_region: Option<Option<Rc<Region>>>,
|
||||
input_region: Option<Option<Rc<Region>>>,
|
||||
frame_request: Vec<Rc<WlCallback>>,
|
||||
damage: bool,
|
||||
damage_full: bool,
|
||||
damage: Vec<Rect>,
|
||||
presentation_feedback: Vec<Rc<WpPresentationFeedback>>,
|
||||
src_rect: Option<Option<[Fixed; 4]>>,
|
||||
dst_size: Option<Option<(i32, i32)>>,
|
||||
|
|
@ -465,7 +469,14 @@ impl PendingState {
|
|||
self.offset = (dx1 + dx2, dy1 + dy2);
|
||||
}
|
||||
self.frame_request.append(&mut next.frame_request);
|
||||
self.damage |= mem::take(&mut next.damage);
|
||||
if !self.damage_full {
|
||||
if self.damage.len() + next.damage.len() > MAX_DAMAGE {
|
||||
self.damage_full = true;
|
||||
self.damage.clear();
|
||||
} else {
|
||||
self.damage.append(&mut next.damage);
|
||||
}
|
||||
}
|
||||
mem::swap(
|
||||
&mut self.presentation_feedback,
|
||||
&mut next.presentation_feedback,
|
||||
|
|
@ -542,6 +553,7 @@ impl WlSurface {
|
|||
buffer_abs_pos: Cell::new(Default::default()),
|
||||
need_extents_update: Default::default(),
|
||||
buffer: Default::default(),
|
||||
shm_texture: Default::default(),
|
||||
buf_x: Default::default(),
|
||||
buf_y: Default::default(),
|
||||
children: Default::default(),
|
||||
|
|
@ -800,6 +812,8 @@ impl WlSurface {
|
|||
}
|
||||
}
|
||||
|
||||
const MAX_DAMAGE: usize = 32;
|
||||
|
||||
impl WlSurfaceRequestHandler for WlSurface {
|
||||
type Error = WlSurfaceError;
|
||||
|
||||
|
|
@ -819,6 +833,7 @@ impl WlSurfaceRequestHandler for WlSurface {
|
|||
*children = None;
|
||||
}
|
||||
self.buffer.set(None);
|
||||
self.shm_texture.take();
|
||||
if let Some(xwayland_serial) = self.xwayland_serial.get() {
|
||||
self.client
|
||||
.surfaces_by_xwayland_serial
|
||||
|
|
@ -852,7 +867,9 @@ impl WlSurfaceRequestHandler for WlSurface {
|
|||
}
|
||||
|
||||
fn damage(&self, _req: Damage, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.pending.borrow_mut().damage = true;
|
||||
let pending = &mut *self.pending.borrow_mut();
|
||||
pending.damage.clear();
|
||||
pending.damage_full = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -918,8 +935,19 @@ impl WlSurfaceRequestHandler for WlSurface {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn damage_buffer(&self, _req: DamageBuffer, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.pending.borrow_mut().damage = true;
|
||||
fn damage_buffer(&self, req: DamageBuffer, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
let pending = &mut *self.pending.borrow_mut();
|
||||
if !pending.damage_full {
|
||||
if pending.damage.len() >= MAX_DAMAGE || self.shm_texture.is_none() {
|
||||
pending.damage.clear();
|
||||
pending.damage_full = true;
|
||||
} else {
|
||||
let Some(rect) = Rect::new_sized(req.x, req.y, req.width, req.height) else {
|
||||
return Err(WlSurfaceError::InvalidRect);
|
||||
};
|
||||
pending.damage.push(rect);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -980,7 +1008,13 @@ impl WlSurface {
|
|||
old_raw_size = Some(buffer.buffer.rect);
|
||||
}
|
||||
if let Some(buffer) = buffer_change {
|
||||
buffer.update_texture_or_log();
|
||||
let damage = match pending.damage_full {
|
||||
true => None,
|
||||
false => Some(&pending.damage[..]),
|
||||
};
|
||||
buffer.update_texture_or_log(self, damage);
|
||||
pending.damage.clear();
|
||||
pending.damage_full = false;
|
||||
let (sync, release_sync) = match pending.explicit_sync {
|
||||
false => (AcquireSync::Implicit, ReleaseSync::Implicit),
|
||||
true => (AcquireSync::Unnecessary, ReleaseSync::Explicit),
|
||||
|
|
@ -998,6 +1032,7 @@ impl WlSurface {
|
|||
};
|
||||
self.buffer.set(Some(Rc::new(surface_buffer)));
|
||||
} else {
|
||||
self.shm_texture.take();
|
||||
self.buf_x.set(0);
|
||||
self.buf_y.set(0);
|
||||
for (_, cursor) in &self.cursors {
|
||||
|
|
@ -1669,6 +1704,8 @@ pub enum WlSurfaceError {
|
|||
MissingSyncPoints,
|
||||
#[error("No buffer is attached but acquire or release point is set")]
|
||||
UnexpectedSyncPoints,
|
||||
#[error("The supplied region is invalid")]
|
||||
InvalidRect,
|
||||
}
|
||||
efrom!(WlSurfaceError, ClientError);
|
||||
efrom!(WlSurfaceError, XdgSurfaceError);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue