Truly independent web browser
Compositor: Export WebGL texture sources as RGBA5551
Shannon Booth4a65eb6ed12a
diff --git a/Services/Compositor/BitmapExport.cpp b/Services/Compositor/BitmapExport.cppindex 72be25f6c49..f783b1c72f5 100644--- a/Services/Compositor/BitmapExport.cpp+++ b/Services/Compositor/BitmapExport.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/ByteReader.h>#include <Compositor/BitmapExport.h>#include <Compositor/SkiaUtils.h>#include <LibGfx/Bitmap.h>@@ -56,13 +57,11 @@ static SkColorType export_format_to_skia_color_type(ExportFormat format)return SkColorType::kAlpha_8_SkColorType;case ExportFormat::RGB565:return SkColorType::kRGB_565_SkColorType;- case ExportFormat::RGBA5551:- dbgln("FIXME: Support conversion to RGBA5551.");- return SkColorType::kUnknown_SkColorType;case ExportFormat::RGBA4444:return SkColorType::kARGB_4444_SkColorType;+ case ExportFormat::RGBA5551:case ExportFormat::RGB888:- // This one needs to be converted manually because Skia has no valid 24-bit color type.+ // These need to be converted manually because Skia has no color type for them.VERIFY_NOT_REACHED();case ExportFormat::RGBA8888:return SkColorType::kRGBA_8888_SkColorType;@@ -71,6 +70,36 @@ static SkColorType export_format_to_skia_color_type(ExportFormat format)}}+static ErrorOr<void> draw_bitmap_into_pixels(Bitmap const& bitmap, ColorSpace const& color_space, SkColorType color_type, int flags, int width, int height, void* pixels, size_t pitch)+{+ auto image = sk_image_from_bitmap(bitmap, color_space);+ if (!image)+ return Error::from_string_literal("Failed to create a Skia image for this Bitmap");++ auto image_info = SkImageInfo::Make(+ width,+ height,+ color_type,+ flags & ExportFlags::PremultiplyAlpha ? SkAlphaType::kPremul_SkAlphaType : SkAlphaType::kUnpremul_SkAlphaType,+ SkColorSpace::MakeSRGB());+ auto surface = SkSurfaces::WrapPixels(image_info, pixels, pitch);+ if (!surface)+ return Error::from_string_literal("Failed to create a Skia surface for the exported pixels");+ auto* surface_canvas = surface->getCanvas();+ auto dst_rect = Gfx::to_skia_rect(Gfx::Rect { 0, 0, width, height });++ if (flags & ExportFlags::FlipY) {+ surface_canvas->translate(0, dst_rect.height());+ surface_canvas->scale(1, -1);+ }++ surface_canvas->drawImageRect(+ image.get(),+ dst_rect,+ Gfx::to_skia_sampling_options(Gfx::ScalingMode::NearestNeighbor));+ return {};+}+ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(Bitmap const& bitmap,ColorSpace const& color_space,@@ -113,34 +142,26 @@ ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(raw_buffer[buffer_offset + 2] = pixel.blue();}}- } else {- auto image = sk_image_from_bitmap(bitmap, color_space);- if (!image)- return Error::from_string_literal("Failed to create a Skia image for this Bitmap");-- auto skia_format = export_format_to_skia_color_type(format);- auto skia_color_space = SkColorSpace::MakeSRGB();-- auto image_info = SkImageInfo::Make(- width,- height,- skia_format,- flags & ExportFlags::PremultiplyAlpha ? SkAlphaType::kPremul_SkAlphaType : SkAlphaType::kUnpremul_SkAlphaType,- skia_color_space);- auto surface = SkSurfaces::WrapPixels(image_info, buffer.data(), buffer_pitch.value());- VERIFY(surface);- auto* surface_canvas = surface->getCanvas();- auto dst_rect = Gfx::to_skia_rect(Gfx::Rect { 0, 0, width, height });-- if (flags & ExportFlags::FlipY) {- surface_canvas->translate(0, dst_rect.height());- surface_canvas->scale(1, -1);- }+ } else if (format == ExportFormat::RGBA5551) {+ // 16 bit RGBA with 1 bit of alpha is not supported by Skia, so we draw into 32 bit RGBA and pack that.+ Checked<size_t> rgba_pitch = width;+ rgba_pitch *= 4;+ if (rgba_pitch.has_overflow() || Checked<size_t>::multiplication_would_overflow(rgba_pitch.value(), height))+ return Error::from_string_literal("Gfx::export_bitmap_to_byte_buffer size overflow");+ auto rgba_buffer = TRY(ByteBuffer::create_zeroed(rgba_pitch.value() * height));+ TRY(draw_bitmap_into_pixels(bitmap, color_space, SkColorType::kRGBA_8888_SkColorType, flags, width, height, rgba_buffer.data(), rgba_pitch.value()));- surface_canvas->drawImageRect(- image.get(),- dst_rect,- Gfx::to_skia_sampling_options(Gfx::ScalingMode::NearestNeighbor));+ for (auto y = 0; y < height; y++) {+ auto const* source_row = rgba_buffer.data() + (y * rgba_pitch.value());+ auto* destination_row = buffer.data() + (y * buffer_pitch.value());+ for (auto x = 0; x < width; x++) {+ auto const* source = source_row + (x * 4ull);+ u16 pixel = ((source[0] >> 3) << 11) | ((source[1] >> 3) << 6) | ((source[2] >> 3) << 1) | (source[3] >> 7);+ ByteReader::store(destination_row + (x * 2ull), pixel);+ }+ }+ } else {+ TRY(draw_bitmap_into_pixels(bitmap, color_space, export_format_to_skia_color_type(format), flags, width, height, buffer.data(), buffer_pitch.value()));}} else {VERIFY(buffer.is_empty());diff --git a/Tests/Compositor/TestBitmapExport.cpp b/Tests/Compositor/TestBitmapExport.cppindex b5b06d5f6be..518b71a124e 100644--- a/Tests/Compositor/TestBitmapExport.cpp+++ b/Tests/Compositor/TestBitmapExport.cpp@@ -150,6 +150,22 @@ TEST_CASE(export_bitmap_to_byte_buffer){ Premultiplied::Yes },{ 0x23, 0x01, 0x67, 0x45, 0xAB, 0x89, 0xEF, 0xCD },},+ {+ non_alpha_bitmap_formats,+ { Premultiplied::No, Premultiplied::Yes },+ { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF },+ Gfx::ExportFormat::RGBA5551,+ { Premultiplied::No, Premultiplied::Yes },+ { 0x01, 0xF8, 0xC1, 0x07, 0x3F, 0x00, 0xFF, 0xFF },+ },+ {+ alpha_bitmap_formats,+ { Premultiplied::No },+ { 0x7FFFFFFF, 0x80FFFFFF, 0xFEFFFFFF, 0xFFFFFFFF },+ Gfx::ExportFormat::RGBA5551,+ { Premultiplied::No },+ { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },+ },{all_bitmap_formats,{ Premultiplied::No, Premultiplied::Yes },diff --git a/Tests/LibWeb/Crash/HTML/webgl-tex-image-2d-rgba-5551.html b/Tests/LibWeb/Crash/HTML/webgl-tex-image-2d-rgba-5551.htmlnew file mode 100644index 00000000000..868e7b1d8b1--- /dev/null+++ b/Tests/LibWeb/Crash/HTML/webgl-tex-image-2d-rgba-5551.html@@ -0,0 +1,10 @@+<!DOCTYPE html>+<canvas id="source" width="1" height="1"></canvas>+<canvas id="destination" width="1" height="1"></canvas>+<script>+ document.getElementById("source").getContext("2d").fillRect(0, 0, 1, 1);+ const gl = document.getElementById("destination").getContext("webgl");+ gl.bindTexture(gl.TEXTURE_2D, gl.createTexture());+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_SHORT_5_5_5_1, document.getElementById("source"));+ gl.finish();+</script>