Skip to content

Commit

Permalink
rutabaga: add support for virgl's resource_map2
Browse files Browse the repository at this point in the history
Plumb virgl's resource_map2 into rutabaga to simplify BO mappings.

Signed-off-by: Sergio Lopez <[email protected]>
  • Loading branch information
slp committed Jun 5, 2024
1 parent fda7002 commit 8ff6263
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ endif
ifeq ($(GPU),1)
FEATURE_FLAGS += --features gpu
endif
ifeq ($(VIRGL_RESOURCE_MAP2),1)
FEATURE_FLAGS += --features virgl_resource_map2
endif
ifeq ($(BLK),1)
FEATURE_FLAGS += --features blk
endif
Expand Down
1 change: 1 addition & 0 deletions src/libkrun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ blk = []
efi = [ "blk", "net" ]
gpu = []
snd = []
virgl_resource_map2 = []

[dependencies]
crossbeam-channel = "0.5"
Expand Down
1 change: 1 addition & 0 deletions src/rutabaga_gfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gfxstream_stub = []
gpu = []
virgl_renderer = []
virgl_renderer_next = []
virgl_resource_map2 = []
minigbm = []
# To try out Vulkano, delete the following line and uncomment the line in "dependencies". Vulkano
# features are just a prototype and not integrated yet into the ChromeOS build system.
Expand Down
10 changes: 10 additions & 0 deletions src/rutabaga_gfx/src/generated/virgl_renderer_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ extern "C" {
out_size: *mut u64,
) -> ::std::os::raw::c_int;
}
#[cfg(feature = "virgl_resource_map2")]
extern "C" {
pub fn virgl_renderer_resource_map2(
res_handle: u32,
map: *const ::std::os::raw::c_void,
size: u64,
prot: i32,
flags: i32,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn virgl_renderer_resource_unmap(res_handle: u32) -> ::std::os::raw::c_int;
}
Expand Down
40 changes: 40 additions & 0 deletions src/rutabaga_gfx/src/rutabaga_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ pub trait RutabagaComponent {
Err(RutabagaError::Unsupported)
}

/// Implementations must map the blob resource on success, on the specified address and
/// honoring prot and flags.
fn resource_map(
&self,
_resource_id: u32,
_addr: u64,
_size: u64,
_prot: i32,
_flags: i32,
) -> RutabagaResult<()> {
Err(RutabagaError::Unsupported)
}

/// Implementations must map the blob resource on success. This is typically done by
/// glMapBufferRange(...) or vkMapMemory.
fn map(&self, _resource_id: u32) -> RutabagaResult<RutabagaMapping> {
Expand Down Expand Up @@ -748,6 +761,33 @@ impl Rutabaga {
Ok(())
}

pub fn resource_map(
&mut self,
resource_id: u32,
addr: u64,
size: u64,
prot: i32,
flags: i32,
) -> RutabagaResult<()> {
let resource = self
.resources
.get_mut(&resource_id)
.ok_or(RutabagaError::InvalidResourceId)?;

let component_type = calculate_component(resource.component_mask)?;
if component_type == RutabagaComponentType::CrossDomain {
resource.mapping = None;
return Ok(());
}

let component = self
.components
.get(&component_type)
.ok_or(RutabagaError::InvalidComponent)?;

component.resource_map(resource_id, addr, size, prot, flags)
}

/// Returns a memory mapping of the blob resource.
pub fn map(&mut self, resource_id: u32) -> RutabagaResult<RutabagaMapping> {
let resource = self
Expand Down
29 changes: 29 additions & 0 deletions src/rutabaga_gfx/src/virgl_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,35 @@ impl RutabagaComponent for VirglRenderer {
Err(RutabagaError::Unsupported)
}

fn resource_map(
&self,
_resource_id: u32,
_addr: u64,
_size: u64,
_prot: i32,
_flags: i32,
) -> RutabagaResult<()> {
#[cfg(feature = "virgl_resource_map2")]
{
let ret = unsafe {
virgl_renderer_resource_map2(
_resource_id,
_addr as *mut libc::c_void,
_size,
_prot,
_flags,
)
};
if ret != 0 {
return Err(RutabagaError::MappingFailed(ret));
}

Ok(())
}
#[cfg(not(feature = "virgl_resource_map2"))]
Err(RutabagaError::Unsupported)
}

fn map(&self, resource_id: u32) -> RutabagaResult<RutabagaMapping> {
#[cfg(feature = "virgl_renderer_next")]
{
Expand Down

0 comments on commit 8ff6263

Please sign in to comment.