-
-
Notifications
You must be signed in to change notification settings - Fork 969
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ImageDecoder+LibGfx: Collate decoded bitmaps before sending over IPC
There is an issue where gifs with many frames cannot be loaded, as each bitmap is sent over IPC using a separate file descriptor, and there is limit on the maximum number of descriptors per IPC message. Thus, trying to load gifs with more than 64 frames (the current limit) causes the image decoder process to die. This commit introduces the BitmapSequence class, which is a thin wrapper around the type Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> and provides an IPC encode/decode routine that collates all bitmap data into a single buffer so that only a single file descriptor is required per IPC transfer, even if multiple frames are being sent.
- Loading branch information
Showing
7 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (c) 2024, Zachary Huang <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <LibCore/AnonymousBuffer.h> | ||
#include <LibGfx/Bitmap.h> | ||
#include <LibGfx/BitmapSequence.h> | ||
#include <LibGfx/Size.h> | ||
#include <LibIPC/Decoder.h> | ||
#include <LibIPC/Encoder.h> | ||
#include <LibIPC/File.h> | ||
|
||
namespace Gfx { | ||
|
||
BitmapSequence::BitmapSequence(Vector<Optional<NonnullRefPtr<Bitmap>>> bitmaps) | ||
: m_bitmaps(move(bitmaps)) | ||
{ | ||
} | ||
|
||
} | ||
|
||
namespace IPC { | ||
|
||
template<> | ||
ErrorOr<void> encode(Encoder& encoder, Gfx::BitmapSequence const& bitmap_sequence) | ||
{ | ||
Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> bitmaps = bitmap_sequence.bitmaps(); | ||
|
||
TRY(encoder.encode_size(bitmaps.size())); | ||
|
||
size_t total_buffer_size = 0; | ||
|
||
for (auto const& bitmap_option : bitmaps) { | ||
TRY(encoder.encode(bitmap_option.has_value())); | ||
|
||
if (bitmap_option.has_value()) { | ||
auto bitmap = bitmap_option.value(); | ||
TRY(encoder.encode(static_cast<u32>(bitmap->format()))); | ||
TRY(encoder.encode(static_cast<u32>(bitmap->alpha_type()))); | ||
TRY(encoder.encode(bitmap->size_in_bytes())); | ||
TRY(encoder.encode(bitmap->size())); | ||
|
||
total_buffer_size += bitmap_option.value()->size_in_bytes(); | ||
} | ||
} | ||
|
||
// collate all of the bitmap data into one contiguous buffer | ||
auto collated_buffer = TRY(Core::AnonymousBuffer::create_with_size(total_buffer_size)); | ||
|
||
auto* write_pointer = collated_buffer.data<u8>(); | ||
for (auto const& bitmap_option : bitmaps) { | ||
if (bitmap_option.has_value()) { | ||
auto bitmap = bitmap_option.value(); | ||
memcpy(write_pointer, bitmap->scanline(0), bitmap->size_in_bytes()); | ||
write_pointer += bitmap->size_in_bytes(); | ||
} | ||
} | ||
|
||
TRY(encoder.encode(collated_buffer)); | ||
|
||
return {}; | ||
} | ||
|
||
template<> | ||
ErrorOr<Gfx::BitmapSequence> decode(Decoder& decoder) | ||
{ | ||
// a struct to temporarily store bitmap fields before the buffer data is decoded | ||
struct BitmapMetadata { | ||
Gfx::BitmapFormat format; | ||
Gfx::AlphaType alpha_type; | ||
Gfx::IntSize size; | ||
size_t size_in_bytes; | ||
}; | ||
|
||
Vector<Optional<BitmapMetadata>> bitmaps_metadata; | ||
|
||
size_t num_bitmaps = TRY(decoder.decode_size()); | ||
|
||
for (size_t i = 0; i < num_bitmaps; i++) { | ||
auto valid = TRY(decoder.decode<bool>()); | ||
|
||
if (valid) { | ||
auto raw_bitmap_format = TRY(decoder.decode<u32>()); | ||
if (!Gfx::is_valid_bitmap_format(raw_bitmap_format)) | ||
return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence format"); | ||
auto format = static_cast<Gfx::BitmapFormat>(raw_bitmap_format); | ||
|
||
auto raw_alpha_type = TRY(decoder.decode<u32>()); | ||
if (!Gfx::is_valid_alpha_type(raw_alpha_type)) | ||
return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence alpha type"); | ||
auto alpha_type = static_cast<Gfx::AlphaType>(raw_alpha_type); | ||
|
||
auto size_in_bytes = TRY(decoder.decode<size_t>()); | ||
auto size = TRY(decoder.decode<Gfx::IntSize>()); | ||
|
||
bitmaps_metadata.append(BitmapMetadata { format, alpha_type, size, size_in_bytes }); | ||
} else { | ||
bitmaps_metadata.append({}); | ||
} | ||
} | ||
|
||
auto collated_buffer = TRY(decoder.decode<Core::AnonymousBuffer>()); | ||
auto* read_pointer = collated_buffer.data<u8>(); | ||
|
||
Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> bitmaps; | ||
|
||
// sequentially read each valid bitmap's data from the collated buffer | ||
for (auto metadata_option : bitmaps_metadata) { | ||
if (metadata_option.has_value()) { | ||
auto metadata = metadata_option.value(); | ||
size_t size_in_bytes = metadata.size_in_bytes; | ||
|
||
auto buffer = TRY(Core::AnonymousBuffer::create_with_size(size_in_bytes)); | ||
memcpy(buffer.data<u8>(), read_pointer, size_in_bytes); | ||
read_pointer += size_in_bytes; | ||
|
||
auto bitmap = TRY(Gfx::Bitmap::create_with_anonymous_buffer(metadata.format, metadata.alpha_type, move(buffer), metadata.size)); | ||
bitmaps.append(bitmap); | ||
} else { | ||
bitmaps.append({}); | ||
} | ||
} | ||
|
||
return Gfx::BitmapSequence { bitmaps }; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2024, Zachary Huang <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <AK/RefPtr.h> | ||
#include <LibGfx/Bitmap.h> | ||
#include <LibGfx/Size.h> | ||
#include <LibIPC/Forward.h> | ||
|
||
namespace Gfx { | ||
|
||
class BitmapSequence { | ||
public: | ||
BitmapSequence() = default; | ||
|
||
BitmapSequence(Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>>); | ||
|
||
Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> bitmaps() const { return m_bitmaps; } | ||
|
||
private: | ||
// friend class Bitmap; | ||
|
||
Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> m_bitmaps; | ||
}; | ||
|
||
} | ||
|
||
namespace IPC { | ||
|
||
template<> | ||
ErrorOr<void> encode(Encoder&, Gfx::BitmapSequence const&); | ||
|
||
template<> | ||
ErrorOr<Gfx::BitmapSequence> decode(Decoder&); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#include <LibGfx/ShareableBitmap.h> | ||
#include <LibGfx/BitmapSequence.h> | ||
|
||
endpoint ImageDecoderClient | ||
{ | ||
did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> bitmaps, Vector<u32> durations, Gfx::FloatPoint scale) =| | ||
did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmaps, Vector<u32> durations, Gfx::FloatPoint scale) =| | ||
did_fail_to_decode_image(i64 image_id, String error_message) =| | ||
} |