use anyhow::Error;
use fidl::endpoints::{create_endpoints, ClientEnd};
use fsysmem2::BufferCollectionTokenDuplicateRequest;
use zx::{self as zx, AsHandleRef};
use {fidl_fuchsia_sysmem2 as fsysmem2, fidl_fuchsia_ui_composition as fland};
pub struct BufferCollectionTokenPair {
pub export_token: fland::BufferCollectionExportToken,
pub import_token: fland::BufferCollectionImportToken,
}
impl BufferCollectionTokenPair {
pub fn new() -> BufferCollectionTokenPair {
let (raw_export_token, raw_import_token) = zx::EventPair::create();
BufferCollectionTokenPair {
export_token: fland::BufferCollectionExportToken { value: raw_export_token },
import_token: fland::BufferCollectionImportToken { value: raw_import_token },
}
}
}
pub fn duplicate_buffer_collection_import_token(
import_token: &fland::BufferCollectionImportToken,
) -> Result<fland::BufferCollectionImportToken, Error> {
let handle = import_token.value.as_handle_ref().duplicate(zx::Rights::SAME_RIGHTS)?;
Ok(fland::BufferCollectionImportToken { value: handle.into() })
}
pub async fn duplicate_buffer_collection_token(
token: &mut fsysmem2::BufferCollectionTokenProxy,
) -> Result<ClientEnd<fsysmem2::BufferCollectionTokenMarker>, Error> {
let (duplicate_token, duplicate_token_server_end) =
create_endpoints::<fsysmem2::BufferCollectionTokenMarker>();
token.duplicate(BufferCollectionTokenDuplicateRequest {
rights_attenuation_mask: Some(fidl::Rights::SAME_RIGHTS),
token_request: Some(duplicate_token_server_end),
..Default::default()
})?;
token.sync().await?;
Ok(duplicate_token)
}