Skip to content

Commit 8e3e8a9

Browse files
Dorinda Basseystefano-garzarella
authored andcommitted
virtio_gpu.rs: expose component_type for specific backend
- Added `component_type` field to `RutabagaVirtioGpu` to retain the selected backend type (e.g., VirglRenderer, Gfxstream). - Refactored `configure_rutabaga_builder` to return both the builder and the selected component type. - Updated `new_gpu` test helper to accept a `RutabagaComponentType` argument, allowing tests to be parameterized by GPU backend. This prepares the codebase for backend-specific logic, such as validating behavior based on the component used. Signed-off-by: Dorinda Bassey <[email protected]>
1 parent 852751b commit 8e3e8a9

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

vhost-device-gpu/src/virtio_gpu.rs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ pub struct RutabagaVirtioGpu {
328328
pub(crate) resources: BTreeMap<u32, VirtioGpuResource>,
329329
pub(crate) fence_state: Arc<Mutex<FenceState>>,
330330
pub(crate) scanouts: [Option<VirtioGpuScanout>; VIRTIO_GPU_MAX_SCANOUTS as usize],
331+
pub(crate) component_type: RutabagaComponentType,
331332
}
332333

333334
const READ_RESOURCE_BYTES_PER_PIXEL: u32 = 4;
@@ -385,27 +386,33 @@ impl RutabagaVirtioGpu {
385386
})
386387
}
387388

388-
fn configure_rutabaga_builder(gpu_config: &GpuConfig) -> RutabagaBuilder {
389+
fn configure_rutabaga_builder(
390+
gpu_config: &GpuConfig,
391+
) -> (RutabagaBuilder, RutabagaComponentType) {
389392
let component = match gpu_config.gpu_mode() {
390393
GpuMode::VirglRenderer => RutabagaComponentType::VirglRenderer,
391394
#[cfg(feature = "gfxstream")]
392395
GpuMode::Gfxstream => RutabagaComponentType::Gfxstream,
393396
};
394397

395-
RutabagaBuilder::new(component, gpu_config.capsets().bits())
398+
let builder = RutabagaBuilder::new(component, gpu_config.capsets().bits())
396399
.set_use_egl(gpu_config.flags().use_egl)
397400
.set_use_glx(gpu_config.flags().use_glx)
398401
.set_use_gles(gpu_config.flags().use_gles)
399402
.set_use_surfaceless(gpu_config.flags().use_surfaceless)
400403
// Since vhost-user-gpu is out-of-process this is the only type of blob resource that
401404
// could work, so this is always enabled
402-
.set_use_external_blob(true)
405+
.set_use_external_blob(true);
406+
407+
(builder, component)
403408
}
404409

405410
pub fn new(queue_ctl: &VringRwLock, gpu_config: &GpuConfig, gpu_backend: GpuBackend) -> Self {
406411
let fence_state = Arc::new(Mutex::new(FenceState::default()));
407412
let fence = Self::create_fence_handler(queue_ctl.clone(), fence_state.clone());
408-
let rutabaga = Self::configure_rutabaga_builder(gpu_config)
413+
let (builder, component_type) = Self::configure_rutabaga_builder(gpu_config);
414+
415+
let rutabaga = builder
409416
.build(fence, None)
410417
.expect("Rutabaga initialization failed!");
411418

@@ -415,6 +422,7 @@ impl RutabagaVirtioGpu {
415422
resources: BTreeMap::default(),
416423
fence_state,
417424
scanouts: Default::default(),
425+
component_type,
418426
}
419427
}
420428

@@ -941,21 +949,31 @@ mod tests {
941949
GpuBackend::from_stream(backend)
942950
}
943951

944-
fn new_gpu() -> RutabagaVirtioGpu {
945-
let config = GpuConfig::new(
946-
GpuMode::VirglRenderer,
947-
Some(GpuCapset::VIRGL | GpuCapset::VIRGL2),
948-
GpuFlags::default(),
949-
)
950-
.unwrap();
951-
let builder = RutabagaVirtioGpu::configure_rutabaga_builder(&config);
952+
fn new_gpu(component_type: RutabagaComponentType) -> RutabagaVirtioGpu {
953+
let (gpu_mode, capsets) = match component_type {
954+
RutabagaComponentType::VirglRenderer => (
955+
GpuMode::VirglRenderer,
956+
Some(GpuCapset::VIRGL | GpuCapset::VIRGL2),
957+
),
958+
#[cfg(feature = "gfxstream")]
959+
RutabagaComponentType::Gfxstream => {
960+
(GpuMode::Gfxstream, Some(GpuCapset::GFXSTREAM_GLES))
961+
}
962+
_ => panic!("Unsupported component type for test"),
963+
};
964+
965+
let config = GpuConfig::new(gpu_mode, capsets, GpuFlags::default()).unwrap();
966+
967+
let (builder, actual_component_type) =
968+
RutabagaVirtioGpu::configure_rutabaga_builder(&config);
952969
let rutabaga = builder.build(RutabagaHandler::new(|_| {}), None).unwrap();
953970
RutabagaVirtioGpu {
954971
rutabaga,
955972
gpu_backend: dummy_gpu_backend(),
956973
resources: BTreeMap::default(),
957974
fence_state: Arc::new(Mutex::new(FenceState::default())),
958975
scanouts: Default::default(),
976+
component_type: actual_component_type,
959977
}
960978
}
961979

@@ -999,7 +1017,7 @@ mod tests {
9991017
rusty_fork_test! {
10001018
#[test]
10011019
fn test_update_cursor_fails() {
1002-
let mut virtio_gpu = new_gpu();
1020+
let mut virtio_gpu = new_gpu(RutabagaComponentType::VirglRenderer);
10031021

10041022
let cursor_pos = VhostUserGpuCursorPos {
10051023
scanout_id: 1,
@@ -1033,7 +1051,7 @@ mod tests {
10331051

10341052
#[test]
10351053
fn test_move_cursor_fails() {
1036-
let mut virtio_gpu = new_gpu();
1054+
let mut virtio_gpu = new_gpu(RutabagaComponentType::VirglRenderer);
10371055
let cursor_pos = VhostUserGpuCursorPos {
10381056
scanout_id: 1,
10391057
x: 123,
@@ -1051,7 +1069,7 @@ mod tests {
10511069

10521070
#[test]
10531071
fn test_process_fence() {
1054-
let mut virtio_gpu = new_gpu();
1072+
let mut virtio_gpu = new_gpu(RutabagaComponentType::VirglRenderer);
10551073
let fence = RutabagaFence {
10561074
flags: 0,
10571075
fence_id: 0,
@@ -1076,13 +1094,13 @@ mod tests {
10761094

10771095
#[test]
10781096
fn test_event_poll() {
1079-
let virtio_gpu = new_gpu();
1097+
let virtio_gpu = new_gpu(RutabagaComponentType::VirglRenderer);
10801098
virtio_gpu.event_poll();
10811099
}
10821100

10831101
#[test]
10841102
fn test_create_and_unref_resources() {
1085-
let mut virtio_gpu = new_gpu();
1103+
let mut virtio_gpu = new_gpu(RutabagaComponentType::VirglRenderer);
10861104

10871105
// No resources exists, cannot unref anything:
10881106
assert!(virtio_gpu.resources.is_empty());
@@ -1102,7 +1120,7 @@ mod tests {
11021120

11031121
#[test]
11041122
fn test_gpu_capset() {
1105-
let virtio_gpu = new_gpu();
1123+
let virtio_gpu = new_gpu(RutabagaComponentType::VirglRenderer);
11061124

11071125
let capset_info = virtio_gpu.get_capset_info(0);
11081126
assert_matches!(capset_info, Ok(OkCapsetInfo { .. }));
@@ -1117,7 +1135,7 @@ mod tests {
11171135

11181136
#[test]
11191137
fn test_gpu_submit_command_fails() {
1120-
let mut virtio_gpu = new_gpu();
1138+
let mut virtio_gpu = new_gpu(RutabagaComponentType::VirglRenderer);
11211139
let mut cmd_buf = [0; 10];
11221140
let fence_ids: Vec<u64> = Vec::with_capacity(0);
11231141
virtio_gpu

0 commit comments

Comments
 (0)