Truly independent web browser
Compositor: Upload video frames to WebGL textures on the GPU
Zaggy1024a174a7ba0960
diff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cppindex 4e293880c65..c6041ae3deb 100644--- a/Services/Compositor/CanvasHost.cpp+++ b/Services/Compositor/CanvasHost.cpp@@ -220,41 +220,31 @@ void CanvasHost::forget_video_sink(Media::VideoSinkHandle handle)m_video_frame_images.remove(handle);}-Vector<Gfx::DecodedImageFrame> CanvasHost::resolve_webgl_image_source_frames(Vector<Compositing::WebGL::ImageSourceFrame> const& frames) const+Vector<HostWebGLContext::UploadSource> CanvasHost::resolve_webgl_upload_sources(Vector<Compositing::WebGL::ImageSourceFrame> const& frames) const{- Vector<Gfx::DecodedImageFrame> resolved_frames;- resolved_frames.ensure_capacity(frames.size());+ Vector<HostWebGLContext::UploadSource> sources;+ sources.ensure_capacity(frames.size());for (auto const& frame : frames) {- auto resolved_frame = frame.visit(- [](Gfx::DecodedImageFrame const& decoded_frame) -> Optional<Gfx::DecodedImageFrame> {- return decoded_frame;+ frame.visit(+ [&](Gfx::DecodedImageFrame const& decoded_frame) {+ sources.unchecked_append(decoded_frame);},- [&](Compositing::CapturedVideoFrame const& captured_frame) -> Optional<Gfx::DecodedImageFrame> {- auto video_frame = m_video_frame_holds.held_frame(captured_frame.sink_handle, captured_frame.generation);- if (!video_frame)- return {};- auto bitmap_or_error = video_frame->to_bitmap();- if (bitmap_or_error.is_error()) {- dbgln("Could not convert video frame to bitmap: {}", bitmap_or_error.release_error());- return {};+ [&](Compositing::CapturedVideoFrame const& captured_frame) {+ if (auto video_frame = m_video_frame_holds.held_frame(captured_frame.sink_handle, captured_frame.generation)) {+ sources.unchecked_append(video_frame.release_nonnull());+ return;}- auto color_space = Gfx::ColorSpace {};- if (auto color_space_result = Gfx::ColorSpace::from_cicp(video_frame->cicp()); !color_space_result.is_error())- color_space = color_space_result.release_value();- return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { bitmap_or_error.release_value() }, move(color_space) };+ sources.unchecked_append(Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { 1, 1 })) } });});- if (!resolved_frame.has_value())- resolved_frame = Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { 1, 1 })) } };- resolved_frames.unchecked_append(resolved_frame.release_value());}- return resolved_frames;+ return sources;}void CanvasHost::execute_webgl_commands(Compositing::CanvasId canvas_id, ReadonlyBytes commands, Vector<Compositing::WebGL::ImageSourceFrame> const& frames){auto* context = this->context(canvas_id);VERIFY(context);- MUST(as_webgl(*context).execute_commands(commands, resolve_webgl_image_source_frames(frames)));+ MUST(as_webgl(*context).execute_commands(commands, resolve_webgl_upload_sources(frames)));}void CanvasHost::set_webgl_shared_command_buffer(Compositing::CanvasId canvas_id, Compositing::WebGL::WebGLSharedCommandBuffer shared_command_buffer)@@ -276,7 +266,7 @@ bool CanvasHost::execute_webgl_commands_from_shared_buffer(Compositing::CanvasId// WebContent can rewrite shared bytes while they execute, so a malformed stream is// a misbehaving peer rather than a Compositor bug; report instead of crashing.- if (webgl_context.execute_commands(*commands, resolve_webgl_image_source_frames(frames)).is_error())+ if (webgl_context.execute_commands(*commands, resolve_webgl_upload_sources(frames)).is_error())return false;webgl_context.store_executed_flush_sequence_number(flush_sequence_number);return true;diff --git a/Services/Compositor/CanvasHost.h b/Services/Compositor/CanvasHost.hindex 2e795349e8b..3b357b786e9 100644--- a/Services/Compositor/CanvasHost.h+++ b/Services/Compositor/CanvasHost.h@@ -19,6 +19,7 @@#include <AK/String.h>#include <AK/Variant.h>#include <AK/Vector.h>+#include <Compositor/HostWebGLContext.h>#include <LibCompositing/DisplayList/DisplayListResourceIds.h>#include <LibCompositing/Forward.h>#include <LibCompositing/Types.h>@@ -44,7 +45,6 @@ class CanvasSurfaceRegistry;namespace Compositor {-class HostWebGLContext;class VideoFrameHolds;class CanvasHost {@@ -96,7 +96,7 @@ private:static HostWebGLContext& as_webgl(Context&);void present_canvas_2d_context(Compositing::CanvasId, Canvas2DContext&);sk_sp<SkImage> held_video_frame_image(Media::VideoSinkHandle, u64 generation);- Vector<Gfx::DecodedImageFrame> resolve_webgl_image_source_frames(Vector<Compositing::WebGL::ImageSourceFrame> const&) const;+ Vector<HostWebGLContext::UploadSource> resolve_webgl_upload_sources(Vector<Compositing::WebGL::ImageSourceFrame> const&) const;RefPtr<Gfx::SkiaBackendContext> m_skia_backend_context;Compositing::CanvasSurfaceRegistry& m_canvas_surface_registry;diff --git a/Services/Compositor/HostWebGLContext.cpp b/Services/Compositor/HostWebGLContext.cppindex 429e86bd6aa..d679ca95cce 100644--- a/Services/Compositor/HostWebGLContext.cpp+++ b/Services/Compositor/HostWebGLContext.cpp@@ -6,6 +6,7 @@#include <AK/Vector.h>#include <Compositor/HostWebGLContext.h>+#include <Compositor/VideoFrameSkia.h>#include <Compositor/WebGLCommandReplayer.h>#include <LibCompositing/WebGL/TextureUpload.h>#include <LibCompositing/WebGL/WebGLCommandList.h>@@ -15,6 +16,9 @@#include <LibGfx/PaintingSurface.h>#include <LibGfx/ShareableBitmap.h>#include <LibGfx/SkiaBackendContext.h>+#include <LibMediaCodecs/VideoFrame.h>++#include <core/SkImage.h>namespace Compositor {@@ -22,8 +26,9 @@ using namespace Compositing::WebGL;static constexpr GLsizei max_webgl_string_list_entries = 16384;-HostWebGLContext::HostWebGLContext(NonnullOwnPtr<OpenGLContext> gl_context)+HostWebGLContext::HostWebGLContext(NonnullOwnPtr<OpenGLContext> gl_context, RefPtr<Gfx::SkiaBackendContext> skia_backend_context): m_gl_context(move(gl_context))+ , m_skia_backend_context(move(skia_backend_context)){}@@ -36,7 +41,7 @@ OwnPtr<HostWebGLContext> HostWebGLContext::create(RefPtr<Gfx::SkiaBackendContextif (!gl_context)return {};gl_context->set_size(initial_size);- return adopt_own(*new HostWebGLContext(gl_context.release_nonnull()));+ return adopt_own(*new HostWebGLContext(gl_context.release_nonnull(), move(skia_backend_context)));}Optional<ReadonlyBytes> HostWebGLContext::shared_command_buffer_range(u64 offset, u64 size_in_bytes) const@@ -51,7 +56,7 @@ Optional<ReadonlyBytes> HostWebGLContext::shared_command_buffer_range(u64 offsetreturn data_region.slice(offset, size_in_bytes);}-ErrorOr<void> HostWebGLContext::execute_commands(ReadonlyBytes bytes, Vector<Gfx::DecodedImageFrame> const& bitmaps)+ErrorOr<void> HostWebGLContext::execute_commands(ReadonlyBytes bytes, Vector<UploadSource> const& sources){m_gl_context->make_current();clear_drawing_buffer_if_needed();@@ -63,23 +68,40 @@ ErrorOr<void> HostWebGLContext::execute_commands(ReadonlyBytes bytes, Vector<Gfxm_gl_context->read_pixels_robust_angle(command.x, command.y, command.width, command.height, command.format, command.type, 0, nullptr, nullptr, nullptr, reinterpret_cast<void*>(static_cast<uintptr_t>(command.offset)));return {};} else if constexpr (IsSame<Command, Commands::TexImage2DFromBitmap>) {- return tex_image2d_from_bitmap(command, bitmaps);+ return tex_image2d_from_bitmap(command, sources);} else if constexpr (IsSame<Command, Commands::TexSubImage2DFromBitmap>) {- return tex_sub_image2d_from_bitmap(command, bitmaps);+ return tex_sub_image2d_from_bitmap(command, sources);} else if constexpr (IsSame<Command, Commands::TexImage3DFromBitmap>) {- return tex_image3d_from_bitmap(command, bitmaps);+ return tex_image3d_from_bitmap(command, sources);} else if constexpr (IsSame<Command, Commands::TexSubImage3DFromBitmap>) {- return tex_sub_image3d_from_bitmap(command, bitmaps);+ return tex_sub_image3d_from_bitmap(command, sources);} else {return replay_webgl_command(*m_gl_context, m_objects, command, payload);}});}-static ErrorOr<Gfx::BitmapExportResult> convert_bitmap_for_upload(Vector<Gfx::DecodedImageFrame> const& bitmaps, u32 bitmap_index, GLenum format, GLenum type, bool has_explicit_destination_size, GLsizei destination_width, GLsizei destination_height, bool flip_y, bool premultiply_alpha)+static ErrorOr<HostWebGLContext::UploadSource const*> upload_source_at(Vector<HostWebGLContext::UploadSource> const& sources, u32 index){- if (bitmap_index >= bitmaps.size())+ if (index >= sources.size())return Error::from_string_literal("WebGL image upload references an out-of-range bitmap");+ return &sources[index];+}++static ErrorOr<Gfx::DecodedImageFrame> decoded_frame_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;+ },+ [&](NonnullRefPtr<Media::VideoFrame> const& video_frame) -> ErrorOr<Gfx::DecodedImageFrame> {+ return decoded_image_frame_from_video_frame(*video_frame, skia_backend_context);+ });+}++static ErrorOr<Gfx::BitmapExportResult> convert_bitmap_for_upload(Vector<HostWebGLContext::UploadSource> const& sources, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context, u32 bitmap_index, GLenum format, GLenum type, bool has_explicit_destination_size, GLsizei destination_width, GLsizei destination_height, bool flip_y, bool premultiply_alpha)+{+ auto const& source = *TRY(upload_source_at(sources, bitmap_index));auto export_format = texture_export_format(format, type);if (!export_format.has_value())@@ -105,34 +127,160 @@ static ErrorOr<Gfx::BitmapExportResult> convert_bitmap_for_upload(Vector<Gfx::Detarget_height = destination_height;}- auto const& frame = bitmaps[bitmap_index];+ 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);}-ErrorOr<void> HostWebGLContext::tex_image2d_from_bitmap(Commands::TexImage2DFromBitmap const& command, Vector<Gfx::DecodedImageFrame> const& bitmaps)+static bool copy_can_write_internal_format(GLint internalformat)+{+ switch (internalformat) {+ case GL_ALPHA:+ case GL_LUMINANCE:+ case GL_LUMINANCE_ALPHA:+ case GL_RGB:+ case GL_RGBA:+ case GL_SRGB_EXT:+ case GL_SRGB_ALPHA_EXT:+ case GL_R8:+ case GL_RG8:+ case GL_RGB8:+ case GL_RGBA8:+ case GL_SRGB8:+ case GL_SRGB8_ALPHA8:+ case GL_RGB565:+ case GL_RGBA4:+ case GL_RGB5_A1:+ case GL_RGB10_A2:+ case GL_RGB9_E5:+ case GL_R11F_G11F_B10F:+ case GL_R16F:+ case GL_RG16F:+ case GL_RGB16F:+ case GL_RGBA16F:+ case GL_R32F:+ case GL_RG32F:+ case GL_RGB32F:+ case GL_RGBA32F:+ return true;+ default:+ return false;+ }+}++static bool format_is_integer(GLenum format)+{+ return format == GL_RED_INTEGER || format == GL_RG_INTEGER || format == GL_RGB_INTEGER || format == GL_RGBA_INTEGER;+}++static Optional<Gfx::IntSize> upload_size(Media::VideoFrame const& frame, bool has_explicit_destination_size, GLsizei destination_width, GLsizei destination_height)+{+ if (!has_explicit_destination_size)+ return Gfx::IntSize { static_cast<int>(frame.width()), static_cast<int>(frame.height()) };+ if (destination_width < 0 || destination_height < 0)+ return {};+ return Gfx::IntSize { destination_width, destination_height };+}++class PixelUnpackBufferUnbinder {+public:+ explicit PixelUnpackBufferUnbinder(OpenGLContext& gl_context)+ : m_gl_context(gl_context)+ {+ if (m_gl_context.webgl_version() != WebGLVersion::WebGL2)+ return;+ m_gl_context.get_integerv_robust_angle(GL_PIXEL_UNPACK_BUFFER_BINDING, 1, nullptr, &m_bound_buffer);+ m_gl_context.bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);+ }++ ~PixelUnpackBufferUnbinder()+ {+ if (m_gl_context.webgl_version() == WebGLVersion::WebGL2)+ m_gl_context.bind_buffer(GL_PIXEL_UNPACK_BUFFER, m_bound_buffer);+ }++private:+ OpenGLContext& m_gl_context;+ GLint m_bound_buffer { 0 };+};++// The level is defined by the same call that an upload of pixels would make, so that WebGL sees the same errors.+bool HostWebGLContext::define_texture_image_from_video_frame(Commands::TexImage2DFromBitmap const& command, Media::VideoFrame const& frame)+{+ if (!m_gl_context->can_copy_images_into_textures() || !copy_can_write_internal_format(command.internalformat))+ return false;+ auto size = upload_size(frame, command.has_explicit_destination_size, command.destination_width, command.destination_height);+ if (!size.has_value())+ return false;+ auto image = sk_image_from_video_frame(frame, m_skia_backend_context);+ if (!image)+ return false;++ m_gl_context->stash_errors();+ {+ PixelUnpackBufferUnbinder unbinder { *m_gl_context };+ m_gl_context->tex_image2d_robust_angle(command.target, command.level, command.internalformat, size->width(), size->height(), 0, command.format, command.type, 0, nullptr);+ }+ if (m_gl_context->stash_errors() || size->is_empty())+ return true;+ if (!m_gl_context->copy_image_into_bound_texture(*image, frame.size().to_type<int>(), *size, command.target, command.level, 0, 0, command.flip_y, command.premultiply_alpha))+ return false;+ m_gl_context->discard_errors();+ return true;+}++bool HostWebGLContext::update_texture_image_from_video_frame(Commands::TexSubImage2DFromBitmap const& command, Media::VideoFrame const& frame){- auto converted = TRY(convert_bitmap_for_upload(bitmaps, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));+ if (!m_gl_context->can_copy_images_into_textures() || format_is_integer(command.format))+ return false;+ auto size = upload_size(frame, command.has_explicit_destination_size, command.destination_width, command.destination_height);+ if (!size.has_value())+ return false;+ auto image = sk_image_from_video_frame(frame, m_skia_backend_context);+ if (!image)+ return false;++ m_gl_context->stash_errors();+ {+ PixelUnpackBufferUnbinder unbinder { *m_gl_context };+ m_gl_context->tex_sub_image2d_robust_angle(command.target, command.level, command.xoffset, command.yoffset, 0, 0, command.format, command.type, 0, nullptr);+ }+ if (m_gl_context->stash_errors() || size->is_empty())+ return true;+ if (!m_gl_context->copy_image_into_bound_texture(*image, frame.size().to_type<int>(), *size, command.target, command.level, command.xoffset, command.yoffset, command.flip_y, command.premultiply_alpha))+ return false;+ // A region beyond the level is the caller's error, as it would be for an upload of pixels.+ m_gl_context->stash_errors();+ return true;+}++ErrorOr<void> HostWebGLContext::tex_image2d_from_bitmap(Commands::TexImage2DFromBitmap const& command, Vector<UploadSource> const& sources)+{+ if (auto const* video_frame = TRY(upload_source_at(sources, command.bitmap_index))->get_pointer<NonnullRefPtr<Media::VideoFrame>>(); video_frame && define_texture_image_from_video_frame(command, **video_frame))+ return {};+ auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));m_gl_context->tex_image2d_robust_angle(command.target, command.level, command.internalformat, converted.width, converted.height, 0, command.format, command.type, converted.buffer.size(), converted.buffer.data());return {};}-ErrorOr<void> HostWebGLContext::tex_sub_image2d_from_bitmap(Commands::TexSubImage2DFromBitmap const& command, Vector<Gfx::DecodedImageFrame> const& bitmaps)+ErrorOr<void> HostWebGLContext::tex_sub_image2d_from_bitmap(Commands::TexSubImage2DFromBitmap const& command, Vector<UploadSource> const& sources){- auto converted = TRY(convert_bitmap_for_upload(bitmaps, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));+ if (auto const* video_frame = TRY(upload_source_at(sources, command.bitmap_index))->get_pointer<NonnullRefPtr<Media::VideoFrame>>(); video_frame && update_texture_image_from_video_frame(command, **video_frame))+ return {};+ auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));m_gl_context->tex_sub_image2d_robust_angle(command.target, command.level, command.xoffset, command.yoffset, converted.width, converted.height, command.format, command.type, converted.buffer.size(), converted.buffer.data());return {};}-ErrorOr<void> HostWebGLContext::tex_image3d_from_bitmap(Commands::TexImage3DFromBitmap const& command, Vector<Gfx::DecodedImageFrame> const& bitmaps)+ErrorOr<void> HostWebGLContext::tex_image3d_from_bitmap(Commands::TexImage3DFromBitmap const& command, Vector<UploadSource> const& sources){- auto converted = TRY(convert_bitmap_for_upload(bitmaps, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));+ auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));m_gl_context->tex_image3d_robust_angle(command.target, command.level, command.internalformat, converted.width, converted.height, command.depth, 0, command.format, command.type, converted.buffer.size(), converted.buffer.data());return {};}-ErrorOr<void> HostWebGLContext::tex_sub_image3d_from_bitmap(Commands::TexSubImage3DFromBitmap const& command, Vector<Gfx::DecodedImageFrame> const& bitmaps)+ErrorOr<void> HostWebGLContext::tex_sub_image3d_from_bitmap(Commands::TexSubImage3DFromBitmap const& command, Vector<UploadSource> const& sources){- auto converted = TRY(convert_bitmap_for_upload(bitmaps, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));+ auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));m_gl_context->tex_sub_image3d_robust_angle(command.target, command.level, command.xoffset, command.yoffset, command.zoffset, converted.width, converted.height, command.depth, command.format, command.type, converted.buffer.size(), converted.buffer.data());return {};}diff --git a/Services/Compositor/HostWebGLContext.h b/Services/Compositor/HostWebGLContext.hindex bd1a181b935..67333d2db11 100644--- a/Services/Compositor/HostWebGLContext.h+++ b/Services/Compositor/HostWebGLContext.h@@ -11,14 +11,17 @@#include <AK/NonnullOwnPtr.h>#include <AK/OwnPtr.h>#include <AK/RefPtr.h>+#include <AK/Variant.h>#include <Compositor/OpenGLContext.h>#include <Compositor/WebGLObjectMap.h>#include <LibCompositing/DisplayList/DisplayListResourceIds.h>#include <LibCompositing/WebGL/Types.h>#include <LibCompositing/WebGL/WebGLSharedCommandBuffer.h>#include <LibCore/AnonymousBuffer.h>+#include <LibGfx/DecodedImageFrame.h>#include <LibGfx/Forward.h>#include <LibGfx/Size.h>+#include <LibMediaCodecs/Forward.h>namespace Compositing::WebGL::Commands {@@ -37,7 +40,8 @@ public:static OwnPtr<HostWebGLContext> create(RefPtr<Gfx::SkiaBackendContext>, OpenGLContext::WebGLVersion, OpenGLContext::DrawingBufferOptions, Gfx::IntSize initial_size);- ErrorOr<void> execute_commands(ReadonlyBytes, Vector<Gfx::DecodedImageFrame> const& bitmaps);+ using UploadSource = Variant<Gfx::DecodedImageFrame, NonnullRefPtr<Media::VideoFrame>>;+ 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); }Optional<ReadonlyBytes> shared_command_buffer_range(u64 offset, u64 size_in_bytes) const;@@ -53,16 +57,20 @@ public:OpenGLContext& gl_context() { return *m_gl_context; }private:- explicit HostWebGLContext(NonnullOwnPtr<OpenGLContext>);+ HostWebGLContext(NonnullOwnPtr<OpenGLContext>, RefPtr<Gfx::SkiaBackendContext>);ErrorOr<void> set_drawing_buffer_size(int width, int height);void clear_drawing_buffer_if_needed();- ErrorOr<void> tex_image2d_from_bitmap(Compositing::WebGL::Commands::TexImage2DFromBitmap const&, Vector<Gfx::DecodedImageFrame> const& bitmaps);- ErrorOr<void> tex_sub_image2d_from_bitmap(Compositing::WebGL::Commands::TexSubImage2DFromBitmap const&, Vector<Gfx::DecodedImageFrame> const& bitmaps);- ErrorOr<void> tex_image3d_from_bitmap(Compositing::WebGL::Commands::TexImage3DFromBitmap const&, Vector<Gfx::DecodedImageFrame> const& bitmaps);- ErrorOr<void> tex_sub_image3d_from_bitmap(Compositing::WebGL::Commands::TexSubImage3DFromBitmap const&, Vector<Gfx::DecodedImageFrame> const& bitmaps);+ ErrorOr<void> tex_image2d_from_bitmap(Compositing::WebGL::Commands::TexImage2DFromBitmap const&, Vector<UploadSource> const&);+ ErrorOr<void> tex_sub_image2d_from_bitmap(Compositing::WebGL::Commands::TexSubImage2DFromBitmap const&, Vector<UploadSource> const&);+ ErrorOr<void> tex_image3d_from_bitmap(Compositing::WebGL::Commands::TexImage3DFromBitmap const&, Vector<UploadSource> const&);+ ErrorOr<void> tex_sub_image3d_from_bitmap(Compositing::WebGL::Commands::TexSubImage3DFromBitmap const&, Vector<UploadSource> const&);++ bool define_texture_image_from_video_frame(Compositing::WebGL::Commands::TexImage2DFromBitmap const&, Media::VideoFrame const&);+ bool update_texture_image_from_video_frame(Compositing::WebGL::Commands::TexSubImage2DFromBitmap const&, Media::VideoFrame const&);NonnullOwnPtr<OpenGLContext> m_gl_context;+ RefPtr<Gfx::SkiaBackendContext> m_skia_backend_context;WebGLObjectMap m_objects;Compositing::WebGL::WebGLSharedCommandBuffer m_shared_command_buffer;bool m_needs_clear_before_next_frame { false };diff --git a/Services/Compositor/OpenGLContext.cpp b/Services/Compositor/OpenGLContext.cppindex 16020ae3521..00708d8bbb9 100644--- a/Services/Compositor/OpenGLContext.cpp+++ b/Services/Compositor/OpenGLContext.cpp@@ -31,6 +31,11 @@ extern "C" {# include <LibGfx/VulkanImage.h>#endif#include <Compositor/OpenGLContext.h>+#include <LibGfx/SkiaBackendContext.h>+#include <LibGfx/SkiaUtils.h>++#include <core/SkCanvas.h>+#include <core/SkImage.h>// Enable WebGL if we're on macOS and can use Metal, if Linux can use ANGLE's OpenGL backend for CPU-painting tests,// if Windows can use ANGLE's Direct3D 11 backend, or if we can use shareable Vulkan images.@@ -75,6 +80,9 @@ struct OpenGLContext::Impl {// before the latest present may still be reading on the GPU. The CPU painting surface is a copy made when// presenting, so it only uses the first.Array<ColorBuffer, 3> color_buffers {};+ // What Skia draws for an upload into one of the page's textures, at the size of the latest upload.+ ColorBuffer upload_color_buffer {};+ Gfx::IntSize upload_color_buffer_size {};size_t drawing_color_buffer_index { 0 };Optional<size_t> presented_color_buffer_index {};@@ -120,6 +128,8 @@ void OpenGLContext::free_surface_resources()for (auto& color_buffer : m_impl->color_buffers)free_color_buffer(color_buffer);+ free_color_buffer(m_impl->upload_color_buffer);+ m_impl->upload_color_buffer_size = {};m_impl->drawing_color_buffer_index = 0;m_impl->presented_color_buffer_index.clear();#endif@@ -378,16 +388,16 @@ static void with_texture_bound(GLenum target, GLuint texture, Callback callback)#endif#ifdef AK_OS_MACOS-void OpenGLContext::allocate_iosurface_color_buffer(ColorBuffer& color_buffer)+void OpenGLContext::allocate_iosurface_color_buffer(ColorBuffer& color_buffer, Gfx::IntSize size, Gfx::PaintingSurface::Origin origin){- color_buffer.shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::create(m_size));- color_buffer.painting_surface = Gfx::PaintingSurface::create_from_shared_image_buffer(*color_buffer.shared_image_buffer, *m_skia_backend_context, Gfx::PaintingSurface::Origin::BottomLeft);+ color_buffer.shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::create(size));+ color_buffer.painting_surface = Gfx::PaintingSurface::create_from_shared_image_buffer(*color_buffer.shared_image_buffer, *m_skia_backend_context, origin);EGLint const surface_attributes[] = {EGL_WIDTH,- m_size.width(),+ size.width(),EGL_HEIGHT,- m_size.height(),+ size.height(),EGL_IOSURFACE_PLANE_ANGLE,0,EGL_TEXTURE_TARGET,@@ -414,7 +424,7 @@ void OpenGLContext::allocate_iosurface_color_buffer(ColorBuffer& color_buffer)#endif#ifdef USE_VULKAN_DMABUF_IMAGES-bool OpenGLContext::allocate_vkimage_color_buffer(ColorBuffer& color_buffer)+bool OpenGLContext::allocate_vkimage_color_buffer(ColorBuffer& color_buffer, Gfx::IntSize size, Gfx::PaintingSurface::Origin origin){VERIFY(m_skia_backend_context);@@ -447,19 +457,19 @@ bool OpenGLContext::allocate_vkimage_color_buffer(ColorBuffer& color_buffer)}}- auto vulkan_image_or_error = Gfx::create_shared_vulkan_image(m_skia_backend_context->vulkan_context(), m_size.width(), m_size.height(), vulkan_format, renderable_modifiers);+ auto vulkan_image_or_error = Gfx::create_shared_vulkan_image(m_skia_backend_context->vulkan_context(), size.width(), size.height(), vulkan_format, renderable_modifiers);if (vulkan_image_or_error.is_error()) {dbgln("Compositor: create_shared_vulkan_image failed ({}); falling back to a CPU painting surface", vulkan_image_or_error.error());return false;}auto vulkan_image = vulkan_image_or_error.release_value();- color_buffer.painting_surface = Gfx::PaintingSurface::create_from_vkimage(*m_skia_backend_context, vulkan_image, Gfx::PaintingSurface::Origin::BottomLeft);+ color_buffer.painting_surface = Gfx::PaintingSurface::create_from_vkimage(*m_skia_backend_context, vulkan_image, origin);EGLAttrib attribs[] = {EGL_WIDTH,- m_size.width(),+ size.width(),EGL_HEIGHT,- m_size.height(),+ size.height(),EGL_LINUX_DRM_FOURCC_EXT,drm_format,EGL_DMA_BUF_PLANE0_FD_EXT,@@ -601,13 +611,13 @@ void OpenGLContext::allocate_painting_surface_if_needed()if (m_impl->uses_cpu_painting_surface) {allocate_cpu_painting_surface(color_buffer);} else {- allocate_iosurface_color_buffer(color_buffer);+ allocate_iosurface_color_buffer(color_buffer, m_size, Gfx::PaintingSurface::Origin::BottomLeft);}# elif defined(USE_VULKAN_DMABUF_IMAGES)# if defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)if (m_impl->uses_cpu_painting_surface) {allocate_cpu_painting_surface(color_buffer);- } else if (!allocate_vkimage_color_buffer(color_buffer)) {+ } else if (!allocate_vkimage_color_buffer(color_buffer, m_size, Gfx::PaintingSurface::Origin::BottomLeft)) {// This driver advertises the DMABUF EGL extensions but can't actually allocate a Vulkan/DMABUF painting surface// (unsupported format, modifier, or image import). Fall back to a CPU painting surface.m_impl->uses_cpu_painting_surface = true;@@ -615,7 +625,7 @@ void OpenGLContext::allocate_painting_surface_if_needed()allocate_cpu_painting_surface(color_buffer);}# else- (void)allocate_vkimage_color_buffer(color_buffer);+ (void)allocate_vkimage_color_buffer(color_buffer, m_size, Gfx::PaintingSurface::Origin::BottomLeft);# endif# elif (defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)) || defined(AK_OS_WINDOWS)allocate_cpu_painting_surface(color_buffer);@@ -726,13 +736,18 @@ void OpenGLContext::present_drawing_buffer([[maybe_unused]] bool preserve_drawin}#ifdef ENABLE_WEBGL-bool OpenGLContext::allocate_color_buffer([[maybe_unused]] ColorBuffer& color_buffer)+bool OpenGLContext::allocate_color_buffer(ColorBuffer& color_buffer)+{+ return allocate_shared_color_buffer(color_buffer, m_size, Gfx::PaintingSurface::Origin::BottomLeft);+}++bool OpenGLContext::allocate_shared_color_buffer([[maybe_unused]] ColorBuffer& color_buffer, [[maybe_unused]] Gfx::IntSize size, [[maybe_unused]] Gfx::PaintingSurface::Origin origin){# if defined(AK_OS_MACOS)- allocate_iosurface_color_buffer(color_buffer);+ allocate_iosurface_color_buffer(color_buffer, size, origin);return true;# elif defined(USE_VULKAN_DMABUF_IMAGES)- return allocate_vkimage_color_buffer(color_buffer);+ return allocate_vkimage_color_buffer(color_buffer, size, origin);# elsereturn false;# endif@@ -765,6 +780,89 @@ bool OpenGLContext::supports_copy_texture()}#endif+GLenum OpenGLContext::get_error()+{+ if (!m_stashed_errors.is_empty())+ return m_stashed_errors.take_first();+ return GLFunctions::get_error();+}++bool OpenGLContext::stash_errors()+{+ auto stashed_any_errors = false;+ for (auto error = GLFunctions::get_error(); error != GL_NO_ERROR; error = GLFunctions::get_error()) {+ if (!m_stashed_errors.contains_slow(error))+ m_stashed_errors.append(error);+ stashed_any_errors = true;+ }+ return stashed_any_errors;+}++void OpenGLContext::discard_errors()+{+ for (auto error = GLFunctions::get_error(); error != GL_NO_ERROR; error = GLFunctions::get_error())+ dbgln("Compositor: Discarding GL error {:#x} raised by the Compositor's own work", error);+}++bool OpenGLContext::can_copy_images_into_textures()+{+#if defined(AK_OS_MACOS) || defined(USE_VULKAN_DMABUF_IMAGES)+ return m_skia_backend_context && !m_impl->uses_cpu_painting_surface && supports_copy_texture();+#else+ return false;+#endif+}++#ifdef ENABLE_WEBGL+static Optional<GLenum> texture_binding_for_copy_destination(GLenum target)+{+ switch (target) {+ case GL_TEXTURE_2D:+ return GL_TEXTURE_BINDING_2D;+ case GL_TEXTURE_CUBE_MAP_POSITIVE_X:+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:+ case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:+ case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:+ return GL_TEXTURE_BINDING_CUBE_MAP;+ default:+ return {};+ }+}+#endif++bool OpenGLContext::copy_image_into_bound_texture([[maybe_unused]] SkImage const& image, [[maybe_unused]] Gfx::IntSize source_size, [[maybe_unused]] Gfx::IntSize size, [[maybe_unused]] GLenum target, [[maybe_unused]] GLint level, [[maybe_unused]] GLint xoffset, [[maybe_unused]] GLint yoffset, [[maybe_unused]] bool flip_y, [[maybe_unused]] bool premultiply_alpha)+{+#ifdef ENABLE_WEBGL+ auto destination_binding = texture_binding_for_copy_destination(target);+ if (!destination_binding.has_value() || size.is_empty() || !can_copy_images_into_textures())+ return false;++ auto& upload_color_buffer = m_impl->upload_color_buffer;+ if (m_impl->upload_color_buffer_size != size) {+ free_color_buffer(upload_color_buffer);+ m_impl->upload_color_buffer_size = {};+ if (!allocate_shared_color_buffer(upload_color_buffer, size, Gfx::PaintingSurface::Origin::TopLeft))+ return false;+ m_impl->upload_color_buffer_size = size;+ }++ auto& canvas = upload_color_buffer.painting_surface->canvas();+ canvas.clear(SK_ColorTRANSPARENT);+ canvas.drawImageRect(&image, SkRect::MakeIWH(source_size.width(), source_size.height()), SkRect::MakeIWH(size.width(), size.height()), Gfx::to_skia_sampling_options(Gfx::ScalingMode::Bilinear), nullptr, SkCanvas::kStrict_SrcRectConstraint);+ m_skia_backend_context->flush_and_submit(*upload_color_buffer.painting_surface);++ eglMakeCurrent(m_impl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_impl->context);+ GLint destination_texture = 0;+ glGetIntegerv(destination_binding.value(), &destination_texture);+ glCopySubTextureCHROMIUM(upload_color_buffer.texture, 0, target, destination_texture, level, xoffset, yoffset, 0, 0, size.width(), size.height(), flip_y, premultiply_alpha, GL_FALSE);+ return true;+#else+ return false;+#endif+}+RefPtr<Gfx::PaintingSurface> OpenGLContext::drawing_buffer_surface(){return m_impl->color_buffers[m_impl->drawing_color_buffer_index].painting_surface;diff --git a/Services/Compositor/OpenGLContext.h b/Services/Compositor/OpenGLContext.hindex ad2341d501a..2ec88c379c7 100644--- a/Services/Compositor/OpenGLContext.h+++ b/Services/Compositor/OpenGLContext.h@@ -13,8 +13,11 @@#include <AK/Vector.h>#include <LibCompositing/WebGL/GLFunctions.h>#include <LibGfx/Forward.h>+#include <LibGfx/PaintingSurface.h>#include <LibGfx/Size.h>+class SkImage;+#if defined(AK_OS_MACOS) || (defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)) || defined(AK_OS_WINDOWS)# define ENABLE_WEBGL_CPU_PAINTING_SURFACE#endif@@ -61,6 +64,16 @@ public:Vector<String> get_supported_opengl_extensions();+ // Keeps the errors WebGL has yet to collect apart from those of the Compositor's own GL work.+ GLenum get_error();+ bool stash_errors();+ void discard_errors();++ WebGLVersion webgl_version() const { return m_webgl_version; }+ bool can_copy_images_into_textures();+ // The source size is the part of the image to copy, which can be smaller than the image.+ bool copy_image_into_bound_texture(SkImage const&, Gfx::IntSize source_size, Gfx::IntSize, GLenum target, GLint level, GLint xoffset, GLint yoffset, bool flip_y, bool premultiply_alpha);+private:struct ColorBuffer;@@ -70,18 +83,20 @@ private:NonnullOwnPtr<Impl> m_impl;WebGLVersion m_webgl_version;[[maybe_unused]] DrawingBufferOptions m_drawing_buffer_options;+ Vector<GLenum> m_stashed_errors;void free_surface_resources();void free_color_buffer(ColorBuffer&);bool allocate_color_buffer(ColorBuffer&);+ bool allocate_shared_color_buffer(ColorBuffer&, Gfx::IntSize, Gfx::PaintingSurface::Origin);void attach_color_buffer_to_default_framebuffer(ColorBuffer const&);u32 color_buffer_texture_target() const;bool supports_copy_texture();#if defined(AK_OS_MACOS)- void allocate_iosurface_color_buffer(ColorBuffer&);+ void allocate_iosurface_color_buffer(ColorBuffer&, Gfx::IntSize, Gfx::PaintingSurface::Origin);#endif#if defined(USE_VULKAN_DMABUF_IMAGES)- bool allocate_vkimage_color_buffer(ColorBuffer&);+ bool allocate_vkimage_color_buffer(ColorBuffer&, Gfx::IntSize, Gfx::PaintingSurface::Origin);#endif#if defined(ENABLE_WEBGL_CPU_PAINTING_SURFACE)void allocate_cpu_painting_surface(ColorBuffer&);diff --git a/Services/Compositor/VideoFrameSkia.cpp b/Services/Compositor/VideoFrameSkia.cppindex 35f8d414f00..28911e75f21 100644--- a/Services/Compositor/VideoFrameSkia.cpp+++ b/Services/Compositor/VideoFrameSkia.cpp@@ -7,12 +7,14 @@#include <Compositor/VideoFrameSkia.h>#include <LibGfx/Bitmap.h>#include <LibGfx/ColorSpace.h>+#include <LibGfx/PaintingSurface.h>#include <LibGfx/SkiaBackendContext.h>#include <LibGfx/SkiaUtils.h>#include <LibGfx/YUVData.h>#include <LibMediaCodecs/VideoFrame.h>#include <LibMediaCodecs/VideoSurface.h>+#include <core/SkCanvas.h>#include <core/SkImage.h>#include <core/SkYUVAInfo.h>#include <core/SkYUVAPixmaps.h>@@ -195,4 +197,27 @@ sk_sp<SkImage> sk_image_from_video_frame(Media::VideoFrame const& frame, RefPtr<return image;}+ErrorOr<Gfx::DecodedImageFrame> decoded_image_frame_from_video_frame(Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+{+ if (frame.surface() != nullptr && skia_backend_context) {+ auto image = sk_image_from_video_frame(frame, skia_backend_context);+ if (!image)+ return Error::from_string_literal("Could not make an image of the video frame's surface");+ auto size = frame.size().to_type<int>();+ auto surface = Gfx::PaintingSurface::create_with_size(size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context);+ auto rect = SkRect::MakeIWH(size.width(), size.height());+ surface->canvas().drawImageRect(image.get(), rect, rect, Gfx::to_skia_sampling_options(Gfx::ScalingMode::NearestNeighbor), nullptr, SkCanvas::kStrict_SrcRectConstraint);+ auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, size));+ // The pixels are read back in sRGB.+ surface->read_into_bitmap(*bitmap);+ return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { move(bitmap) } };+ }++ auto bitmap = TRY(frame.to_bitmap());+ auto color_space = Gfx::ColorSpace {};+ if (auto color_space_result = Gfx::ColorSpace::from_cicp(frame.cicp()); !color_space_result.is_error())+ color_space = color_space_result.release_value();+ return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { move(bitmap) }, move(color_space) };+}+}diff --git a/Services/Compositor/VideoFrameSkia.h b/Services/Compositor/VideoFrameSkia.hindex 653a9309772..400ddf5c189 100644--- a/Services/Compositor/VideoFrameSkia.h+++ b/Services/Compositor/VideoFrameSkia.h@@ -6,7 +6,9 @@#pragma once+#include <AK/Error.h>#include <AK/RefPtr.h>+#include <LibGfx/DecodedImageFrame.h>#include <LibGfx/Forward.h>#include <LibMediaCodecs/Color/CodingIndependentCodePoints.h>#include <LibMediaCodecs/Forward.h>@@ -33,4 +35,8 @@ sk_sp<SkImage> sk_image_from_video_surface(Media::VideoSurface const&, Media::Co// Makes an image of the frame's pixels, on the GPU when there is a backend context, and in memory otherwise.sk_sp<SkImage> sk_image_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&);+// 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&);+}