Truly independent web browser
Compositor: Upload video frames to WebGL as bitmaps
Aliaksandr Kalenik9d58e2c24c5e
diff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cppindex edbda19e4be..ccde3e910a0 100644--- a/Services/Compositor/CanvasHost.cpp+++ b/Services/Compositor/CanvasHost.cpp@@ -278,7 +278,7 @@ Vector<HostWebGLContext::UploadSource> CanvasHost::resolve_webgl_upload_sources(sources.unchecked_append(video_frame.release_nonnull());return;}- sources.unchecked_append(Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { 1, 1 })) } });+ sources.unchecked_append(HostWebGLContext::MissingVideoFrame {});});}return sources;diff --git a/Services/Compositor/HostWebGLContext.cpp b/Services/Compositor/HostWebGLContext.cppindex e49258a6f1c..da2706290c6 100644--- a/Services/Compositor/HostWebGLContext.cpp+++ b/Services/Compositor/HostWebGLContext.cpp@@ -88,14 +88,17 @@ static ErrorOr<HostWebGLContext::UploadSource const*> upload_source_at(Vector<Horeturn &sources[index];}-static ErrorOr<Gfx::DecodedImageFrame> decoded_frame_for_upload(HostWebGLContext::UploadSource const& source, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+static ErrorOr<BitmapInColorSpace> bitmap_for_upload(HostWebGLContext::UploadSource const& source, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){return source.visit(- [](Gfx::DecodedImageFrame const& frame) -> ErrorOr<Gfx::DecodedImageFrame> {- return frame;+ [](Gfx::DecodedImageFrame const& frame) -> ErrorOr<BitmapInColorSpace> {+ return BitmapInColorSpace { frame.bitmap_ref(), frame.color_space() };},- [&](NonnullRefPtr<Media::VideoFrame> const& video_frame) -> ErrorOr<Gfx::DecodedImageFrame> {- return decoded_image_frame_from_video_frame(*video_frame, skia_backend_context);+ [&](NonnullRefPtr<Media::VideoFrame> const& video_frame) -> ErrorOr<BitmapInColorSpace> {+ return bitmap_from_video_frame(*video_frame, skia_backend_context);+ },+ [](HostWebGLContext::MissingVideoFrame) -> ErrorOr<BitmapInColorSpace> {+ return BitmapInColorSpace { TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { 1, 1 })), {} };});}@@ -127,8 +130,8 @@ static ErrorOr<Gfx::BitmapExportResult> convert_bitmap_for_upload(Vector<HostWebtarget_height = destination_height;}- auto frame = TRY(decoded_frame_for_upload(source, skia_backend_context));- return Gfx::export_bitmap_to_byte_buffer(frame.bitmap(), frame.color_space(), export_format.value(), export_flags, target_width, target_height);+ auto [bitmap, color_space] = TRY(bitmap_for_upload(source, skia_backend_context));+ return Gfx::export_bitmap_to_byte_buffer(*bitmap, color_space, export_format.value(), export_flags, target_width, target_height);}static bool copy_can_write_internal_format(GLint internalformat)diff --git a/Services/Compositor/HostWebGLContext.h b/Services/Compositor/HostWebGLContext.hindex 88b6894dc3e..9476635c948 100644--- a/Services/Compositor/HostWebGLContext.h+++ b/Services/Compositor/HostWebGLContext.h@@ -41,7 +41,8 @@ public:static OwnPtr<HostWebGLContext> create(RefPtr<Gfx::SkiaBackendContext>, OpenGLContext::WebGLVersion, OpenGLContext::DrawingBufferOptions, Gfx::IntSize initial_size);- using UploadSource = Variant<Gfx::DecodedImageFrame, NonnullRefPtr<Media::VideoFrame>>;+ struct MissingVideoFrame { };+ using UploadSource = Variant<Gfx::DecodedImageFrame, NonnullRefPtr<Media::VideoFrame>, MissingVideoFrame>;ErrorOr<void> execute_commands(ReadonlyBytes, Vector<UploadSource> const&);void set_shared_command_buffer(Compositing::WebGL::WebGLSharedCommandBuffer shared_command_buffer) { m_shared_command_buffer = move(shared_command_buffer); }diff --git a/Services/Compositor/VideoFrameSkia.cpp b/Services/Compositor/VideoFrameSkia.cppindex 1012f7c41e3..4f5a83fb38e 100644--- a/Services/Compositor/VideoFrameSkia.cpp+++ b/Services/Compositor/VideoFrameSkia.cpp@@ -374,16 +374,16 @@ sk_sp<SkImage> sk_image_in_memory_from_video_frame(Media::VideoFrame const& framreturn nullptr;}-ErrorOr<Gfx::DecodedImageFrame> decoded_image_frame_from_video_frame(Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+ErrorOr<BitmapInColorSpace> bitmap_from_video_frame(Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){if (frame.surface() != nullptr && skia_backend_context) {auto bitmap = TRY(read_back_video_surface_through_gpu(frame, skia_backend_context));// The pixels are read back in sRGB.- return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { move(bitmap) } };+ return BitmapInColorSpace { move(bitmap), {} };}auto bitmap = TRY(frame.to_bitmap());- return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { move(bitmap) }, color_space_of(frame) };+ return BitmapInColorSpace { move(bitmap), color_space_of(frame) };}}diff --git a/Services/Compositor/VideoFrameSkia.h b/Services/Compositor/VideoFrameSkia.hindex c8760ea6410..1e5be3e3b3a 100644--- a/Services/Compositor/VideoFrameSkia.h+++ b/Services/Compositor/VideoFrameSkia.h@@ -9,7 +9,8 @@#include <AK/Error.h>#include <AK/RefPtr.h>#include <Compositor/Forward.h>-#include <LibGfx/DecodedImageFrame.h>+#include <LibGfx/Bitmap.h>+#include <LibGfx/ColorSpace.h>#include <LibGfx/Forward.h>#include <LibMediaCodecs/Color/CodingIndependentCodePoints.h>#include <LibMediaCodecs/Forward.h>@@ -44,8 +45,13 @@ sk_sp<SkImage> sk_image_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::S// read it, and the GPU, when there is one, reads back a surface that only it can read.sk_sp<SkImage> sk_image_in_memory_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&);+struct BitmapInColorSpace {+ NonnullRefPtr<Gfx::Bitmap const> bitmap;+ Gfx::ColorSpace color_space;+};+// Reads the frame's pixels into memory. A surface is drawn on the GPU and read back from there, since only the GPU// can read some surfaces.-ErrorOr<Gfx::DecodedImageFrame> decoded_image_frame_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&);+ErrorOr<BitmapInColorSpace> bitmap_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&);}