Skip to content

Commit

Permalink
Remove NodeId usage from ChromiumSenderThread
Browse files Browse the repository at this point in the history
  • Loading branch information
noituri authored and wkozyra95 committed Feb 2, 2024
1 parent b1987a8 commit 024a76a
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 132 deletions.
5 changes: 5 additions & 0 deletions compositor_render/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ pub enum SceneError {
#[error("Instance of web renderer \"{0}\" does not exist. You have to register it first before using it in the scene definition.")]
WebRendererNotFound(RendererId),

#[error(
"Instance of web renderer \"{0}\" was used more than once. Only one component can use specific web renderer at the time."
)]
WebRendererUsageNotExclusive(RendererId),

#[error("Invalid parameter passed to \"{1}\" shader.")]
ShaderNodeParametersValidationError(#[source] ParametersValidationError, RendererId),

Expand Down
31 changes: 31 additions & 0 deletions compositor_render/src/scene/validation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashSet;

use crate::RendererId;

use super::{Component, ComponentId, OutputScene, SceneError};

impl Component {
Expand Down Expand Up @@ -32,6 +34,7 @@ impl Component {

pub(super) fn validate_scene_update(outputs: &[OutputScene]) -> Result<(), SceneError> {
validate_component_ids_uniqueness(outputs)?;
validate_web_renderer_ids_uniqueness(outputs)?;
Ok(())
}

Expand Down Expand Up @@ -61,3 +64,31 @@ fn validate_component_ids_uniqueness(outputs: &[OutputScene]) -> Result<(), Scen
.iter()
.try_for_each(|output| visit(&output.root, &mut ids))
}

fn validate_web_renderer_ids_uniqueness(outputs: &[OutputScene]) -> Result<(), SceneError> {
let mut web_renderer_ids: HashSet<&RendererId> = HashSet::new();

fn visit<'a>(
component: &'a Component,
ids: &mut HashSet<&'a RendererId>,
) -> Result<(), SceneError> {
if let Component::WebView(web_view) = component {
let instance_id = &web_view.instance_id;
if ids.contains(instance_id) {
return Err(SceneError::WebRendererUsageNotExclusive(
instance_id.clone(),
));
}
ids.insert(instance_id);
}

component
.children()
.into_iter()
.try_for_each(|c| visit(c, ids))
}

outputs
.iter()
.try_for_each(|output| visit(&output.root, &mut web_renderer_ids))
}
3 changes: 1 addition & 2 deletions compositor_render/src/state/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ impl RenderNode {
pub(super) fn new_web_renderer_node(
ctx: &RenderCtx,
inputs: Vec<NodeId>,
node_id: &NodeId,
web_renderer: Arc<WebRenderer>,
) -> Self {
let resolution = web_renderer.resolution();
let node = InnerRenderNode::Web(WebRendererNode::new(node_id, web_renderer));
let node = InnerRenderNode::Web(WebRendererNode::new(web_renderer));
let mut output = NodeTexture::new();
output.ensure_size(ctx.wgpu_ctx, resolution);

Expand Down
3 changes: 1 addition & 2 deletions compositor_render/src/state/render_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ impl RenderGraph {
new_nodes.insert(node_id, node);
}
scene::NodeParams::Web(web_renderer) => {
let node =
RenderNode::new_web_renderer_node(ctx, input_pads, &node_id, web_renderer);
let node = RenderNode::new_web_renderer_node(ctx, input_pads, web_renderer);
new_nodes.insert(node_id, node);
}
scene::NodeParams::Image(image) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use log::error;

use crate::wgpu::texture::NodeTexture;

use super::{browser_client::BrowserClient, chromium_sender_thread::ChromiumSenderThread};
use super::{
browser_client::BrowserClient, chromium_sender_thread::ChromiumSenderThread, WebRendererSpec,
};

#[derive(Debug)]
pub(super) struct ChromiumSender {
Expand All @@ -27,13 +29,13 @@ impl Drop for ChromiumSender {
}

impl ChromiumSender {
pub fn new(ctx: &RegisterCtx, url: String, browser_client: BrowserClient) -> Self {
pub fn new(ctx: &RegisterCtx, spec: &WebRendererSpec, browser_client: BrowserClient) -> Self {
let (message_sender, message_receiver) = crossbeam_channel::unbounded();
let (unmap_signal_sender, unmap_signal_receiver) = crossbeam_channel::bounded(0);

ChromiumSenderThread::new(
ctx,
url,
spec,
browser_client,
message_receiver,
unmap_signal_sender,
Expand All @@ -48,47 +50,37 @@ impl ChromiumSender {

pub fn embed_sources(
&self,
node_id: NodeId,
sources: &[(&NodeId, &NodeTexture)],
) -> Result<(), ChromiumSenderError> {
let resolutions = sources
.iter()
.map(|(_, texture)| texture.resolution())
.collect();
self.message_sender
.send(ChromiumSenderMessage::EmbedSources {
node_id,
resolutions,
})
.send(ChromiumSenderMessage::EmbedSources { resolutions })
.map_err(|_| ChromiumSenderError::MessageChannelDisconnected)
}

pub fn ensure_shared_memory(
&self,
node_id: NodeId,
sources: &[(&NodeId, &NodeTexture)],
) -> Result<(), ChromiumSenderError> {
let resolutions = sources
.iter()
.map(|(_, texture)| texture.resolution())
.collect();
self.message_sender
.send(ChromiumSenderMessage::EnsureSharedMemory {
node_id,
resolutions,
})
.send(ChromiumSenderMessage::EnsureSharedMemory { resolutions })
.map_err(|_| ChromiumSenderError::MessageChannelDisconnected)
}

pub fn update_shared_memory(
&self,
node_id: NodeId,
source_idx: usize,
buffer: Arc<wgpu::Buffer>,
size: wgpu::Extent3d,
) -> Result<(), ChromiumSenderError> {
let info = UpdateSharedMemoryInfo {
node_id,
source_idx,
buffer,
size,
Expand Down Expand Up @@ -118,11 +110,9 @@ impl ChromiumSender {

pub(super) enum ChromiumSenderMessage {
EmbedSources {
node_id: NodeId,
resolutions: Vec<Option<Resolution>>,
},
EnsureSharedMemory {
node_id: NodeId,
resolutions: Vec<Option<Resolution>>,
},
UpdateSharedMemory(UpdateSharedMemoryInfo),
Expand All @@ -133,7 +123,6 @@ pub(super) enum ChromiumSenderMessage {
}

pub(super) struct UpdateSharedMemoryInfo {
pub node_id: NodeId,
pub source_idx: usize,
pub buffer: Arc<wgpu::Buffer>,
pub size: wgpu::Extent3d,
Expand Down
Loading

0 comments on commit 024a76a

Please sign in to comment.