Skip to content

Commit

Permalink
captureStream for WebGL content
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Jul 28, 2020
1 parent 6714c10 commit d4a27db
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
20 changes: 18 additions & 2 deletions components/canvas/webgl_thread.rs
Expand Up @@ -47,6 +47,7 @@ use euclid::default::Size2D;
use fnv::FnvHashMap;
use half::f16;
use pixels::{self, PixelFormat};
use servo_media::ServoMedia;
use sparkle::gl;
use sparkle::gl::GLint;
use sparkle::gl::GLuint;
Expand Down Expand Up @@ -800,7 +801,6 @@ impl WebGLThread {
completed_sender: WebGLSender<u64>,
_sent_time: u64,
) {
debug!("handle_swap_buffers()");
for context_id in context_ids {
let data = Self::make_current_if_needed_mut(
&self.device,
Expand Down Expand Up @@ -1069,7 +1069,7 @@ impl WebGLThread {
/// Gets the GLSL Version supported by a GLContext.
fn get_glsl_version(gl: &Gl) -> WebGLSLVersion {
let version = gl.get_string(gl::SHADING_LANGUAGE_VERSION);
// Fomat used by SHADING_LANGUAGE_VERSION query : major.minor[.release] [vendor info]
// Format used by SHADING_LANGUAGE_VERSION query : major.minor[.release] [vendor info]
let mut values = version.split(&['.', ' '][..]);
let major = values
.next()
Expand Down Expand Up @@ -2208,6 +2208,22 @@ impl WebGLImpl {
},
WebGLCommand::ReadBuffer(buffer) => gl.read_buffer(buffer),
WebGLCommand::DrawBuffers(ref buffers) => gl.draw_buffers(buffers),
WebGLCommand::PushCapturedStreamsData(ref captured_streams, size) => {
// XXX(ferjm) This is currently returning all 0's.
// I suspect this requires https://github.com/servo/servo/issues/2460
let pixels = gl.read_pixels(
0,
0,
size.width as gl::GLsizei,
size.height as gl::GLsizei,
gl::RGB,
gl::UNSIGNED_BYTE,
);
let media = ServoMedia::get().unwrap();
for stream in captured_streams.iter() {
media.push_stream_data(stream, pixels.clone());
}
},
}

// If debug asertions are enabled, then check the error state.
Expand Down
2 changes: 2 additions & 0 deletions components/canvas_traits/webgl.rs
Expand Up @@ -6,6 +6,7 @@ use euclid::default::{Rect, Size2D};
use ipc_channel::ipc::{IpcBytesReceiver, IpcBytesSender, IpcSharedMemory};
use pixels::PixelFormat;
use serde::{Deserialize, Serialize};
use servo_media::streams::registry::MediaStreamId;
use sparkle::gl;
use std::borrow::Cow;
use std::fmt;
Expand Down Expand Up @@ -511,6 +512,7 @@ pub enum WebGLCommand {
FramebufferTextureLayer(u32, u32, Option<WebGLTextureId>, i32, i32),
ReadBuffer(u32),
DrawBuffers(Vec<u32>),
PushCapturedStreamsData(Vec<MediaStreamId>, Size2D<u64>),
}

/// WebXR layer management
Expand Down
6 changes: 6 additions & 0 deletions components/script/dom/gpucanvascontext.rs
Expand Up @@ -19,6 +19,7 @@ use crate::dom::gpuswapchain::GPUSwapChain;
use crate::dom::htmlcanvaselement::{HTMLCanvasElement, LayoutCanvasRenderingContextHelpers};
use crate::dom::node::{document_from_node, Node, NodeDamage};
use arrayvec::ArrayVec;
use canvas_traits::webgl::WebGLCommand;
use dom_struct::dom_struct;
use euclid::default::Size2D;
use ipc_channel::ipc;
Expand Down Expand Up @@ -119,6 +120,11 @@ impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, GPUCanvasContext> {
unsafe fn canvas_data_source(self) -> HTMLCanvasDataSource {
(*self.unsafe_get()).layout_handle()
}

#[allow(unsafe_code)]
unsafe fn send_command(self, _: WebGLCommand) {
unimplemented!();
}
}

impl GPUCanvasContextMethods for GPUCanvasContext {
Expand Down
18 changes: 15 additions & 3 deletions components/script/dom/htmlcanvaselement.rs
Expand Up @@ -31,7 +31,7 @@ use crate::dom::webglrenderingcontext::WebGLRenderingContext;
use crate::script_runtime::JSContext;
use base64;
use canvas_traits::canvas::{CanvasId, CanvasMsg, FromScriptMsg};
use canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
use canvas_traits::webgl::{GLContextAttributes, WebGLCommand, WebGLVersion};
use dom_struct::dom_struct;
use euclid::default::{Rect, Size2D};
use html5ever::{LocalName, Prefix};
Expand Down Expand Up @@ -123,6 +123,8 @@ impl HTMLCanvasElement {
pub trait LayoutCanvasRenderingContextHelpers {
#[allow(unsafe_code)]
unsafe fn canvas_data_source(self) -> HTMLCanvasDataSource;
#[allow(unsafe_code)]
unsafe fn send_command(self, command: WebGLCommand);
}

pub trait LayoutHTMLCanvasElementHelpers {
Expand Down Expand Up @@ -164,10 +166,20 @@ impl LayoutHTMLCanvasElementHelpers for LayoutDom<'_, HTMLCanvasElement> {
HTMLCanvasDataSource::Image(Some(renderer))
},
Some(&CanvasContext::WebGL(ref context)) => {
context.to_layout().canvas_data_source()
let context = context.to_layout();
context.send_command(WebGLCommand::PushCapturedStreamsData(
self.get_captured_streams(),
Size2D::new(width.into(), height.into()),
));
context.canvas_data_source()
},
Some(&CanvasContext::WebGL2(ref context)) => {
context.to_layout().canvas_data_source()
let context = context.to_layout();
context.send_command(WebGLCommand::PushCapturedStreamsData(
self.get_captured_streams(),
Size2D::new(width.into(), height.into()),
));
context.canvas_data_source()
},
Some(&CanvasContext::WebGPU(ref context)) => {
context.to_layout().canvas_data_source()
Expand Down
6 changes: 6 additions & 0 deletions components/script/dom/webgl2renderingcontext.rs
Expand Up @@ -4469,4 +4469,10 @@ impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, WebGL2RenderingContex
let this = &*self.unsafe_get();
(*this.base.to_layout().unsafe_get()).layout_handle()
}

#[allow(unsafe_code)]
unsafe fn send_command(self, command: WebGLCommand) {
let this = &*self.unsafe_get();
(*this.base.to_layout().unsafe_get()).send_command(command);
}
}
5 changes: 5 additions & 0 deletions components/script/dom/webglrenderingcontext.rs
Expand Up @@ -4723,6 +4723,11 @@ impl LayoutCanvasRenderingContextHelpers for LayoutDom<'_, WebGLRenderingContext
unsafe fn canvas_data_source(self) -> HTMLCanvasDataSource {
(*self.unsafe_get()).layout_handle()
}

#[allow(unsafe_code)]
unsafe fn send_command(self, command: WebGLCommand) {
(*self.unsafe_get()).send_command(command);
}
}

#[derive(Default, JSTraceable, MallocSizeOf)]
Expand Down

0 comments on commit d4a27db

Please sign in to comment.