1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:37:35 +00:00

Lagom: Add LibGemini, LibGfx

They are dependencies of LibWeb and might be useful for
running test-web on GitHub actions one day.
This commit is contained in:
Nico Weber 2020-07-23 14:34:53 -04:00 committed by Andreas Kling
parent c4d9d5cc54
commit 3f45e9ab1e
8 changed files with 179 additions and 22 deletions

View file

@ -74,8 +74,13 @@ Bitmap::Bitmap(BitmapFormat format, const IntSize& size, Purgeable purgeable)
ASSERT(!m_size.is_empty());
ASSERT(!size_would_overflow(format, size));
allocate_palette_from_format(format, {});
#ifdef __serenity__
int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, map_flags, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
#else
int map_flags = (MAP_ANONYMOUS | MAP_PRIVATE);
m_data = (RGBA32*)mmap(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, map_flags, 0, 0);
#endif
ASSERT(m_data && m_data != (void*)-1);
m_needs_munmap = true;
}
@ -207,7 +212,11 @@ Bitmap::~Bitmap()
void Bitmap::set_mmap_name(const StringView& name)
{
ASSERT(m_needs_munmap);
#ifdef __serenity__
::set_mmap_name(m_data, size_in_bytes(), name.to_string().characters());
#else
(void)name;
#endif
}
void Bitmap::fill(Color color)
@ -224,11 +233,13 @@ void Bitmap::set_volatile()
ASSERT(m_purgeable);
if (m_volatile)
return;
#ifdef __serenity__
int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
if (rc < 0) {
perror("madvise(MADV_SET_VOLATILE)");
ASSERT_NOT_REACHED();
}
#endif
m_volatile = true;
}
@ -237,11 +248,15 @@ void Bitmap::set_volatile()
ASSERT(m_purgeable);
if (!m_volatile)
return true;
#ifdef __serenity__
int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
if (rc < 0) {
perror("madvise(MADV_SET_NONVOLATILE)");
ASSERT_NOT_REACHED();
}
#else
int rc = 0;
#endif
m_volatile = false;
return rc == 0;
}

View file

@ -213,7 +213,15 @@ RefPtr<Font> Font::load_from_file(const StringView& path)
bool Font::write_to_file(const StringView& path)
{
int fd = creat_with_path_length(path.characters_without_null_termination(), path.length(), 0644);
int fd;
#ifdef __serenity__
fd = creat_with_path_length(path.characters_without_null_termination(), path.length(), 0644);
#else
{
String null_terminated_path = path;
fd = creat(null_terminated_path.characters(), 0644);
}
#endif
if (fd < 0) {
perror("open");
return false;

View file

@ -432,7 +432,7 @@ bool load_gif_frame_descriptors(GIFLoadingContext& context)
if (sub_block_length == 0)
break;
u8 dummy;
u8 dummy = 0;
for (u16 i = 0; i < sub_block_length; ++i) {
stream >> dummy;
sub_block.append(dummy);

View file

@ -466,7 +466,7 @@ static inline bool is_valid_marker(const Marker marker)
static inline u16 read_be_word(BufferStream& stream)
{
u8 tmp1, tmp2;
u8 tmp1 = 0, tmp2 = 0;
stream >> tmp1 >> tmp2;
return ((u16)tmp1 << 8) | ((u16)tmp2);
}
@ -505,6 +505,8 @@ static bool read_start_of_scan(BufferStream& stream, JPGLoadingContext& context)
return false;
u8 component_count;
stream >> component_count;
if (stream.handle_read_failure())
return false;
if (component_count != context.component_count) {
dbg() << stream.offset()
<< String::format(": Unsupported number of components: %i!", component_count);
@ -515,6 +517,8 @@ static bool read_start_of_scan(BufferStream& stream, JPGLoadingContext& context)
ComponentSpec* component = nullptr;
u8 component_id;
stream >> component_id;
if (stream.handle_read_failure())
return false;
component_id += context.has_zero_based_ids ? 1 : 0;
if (component_id == context.components[0].id)
@ -530,16 +534,24 @@ static bool read_start_of_scan(BufferStream& stream, JPGLoadingContext& context)
u8 table_ids;
stream >> table_ids;
if (stream.handle_read_failure())
return false;
component->dc_destination_id = table_ids >> 4;
component->ac_destination_id = table_ids & 0x0F;
}
u8 spectral_selection_start;
stream >> spectral_selection_start;
if (stream.handle_read_failure())
return false;
u8 spectral_selection_end;
stream >> spectral_selection_end;
if (stream.handle_read_failure())
return false;
u8 successive_approximation;
stream >> successive_approximation;
if (stream.handle_read_failure())
return false;
// The three values should be fixed for baseline JPEGs utilizing sequential DCT.
if (spectral_selection_start != 0 || spectral_selection_end != 63 || successive_approximation != 0) {
dbg() << stream.offset() << ": ERROR! Start of Selection: " << spectral_selection_start
@ -574,6 +586,8 @@ static bool read_huffman_table(BufferStream& stream, JPGLoadingContext& context)
HuffmanTableSpec table;
u8 table_info;
stream >> table_info;
if (stream.handle_read_failure())
return false;
u8 table_type = table_info >> 4;
u8 table_destination_id = table_info & 0x0F;
if (table_type > 1) {
@ -593,6 +607,8 @@ static bool read_huffman_table(BufferStream& stream, JPGLoadingContext& context)
for (int i = 0; i < 16; i++) {
u8 count;
stream >> count;
if (stream.handle_read_failure())
return false;
total_codes += count;
table.code_counts[i] = count;
}
@ -601,11 +617,14 @@ static bool read_huffman_table(BufferStream& stream, JPGLoadingContext& context)
// Read symbols. Read X bytes, where X is the sum of the counts of codes read in the previous step.
for (u32 i = 0; i < total_codes; i++) {
u8 symbol;
u8 symbol = 0;
stream >> symbol;
table.symbols.append(symbol);
}
if (stream.handle_read_failure())
return false;
if (table_type == 0)
context.dc_tables.append(move(table));
else
@ -690,8 +709,10 @@ static bool read_start_of_frame(BufferStream& stream, JPGLoadingContext& context
context.has_zero_based_ids = component.id == 0;
component.id += context.has_zero_based_ids ? 1 : 0;
u8 subsample_factors;
u8 subsample_factors = 0;
stream >> subsample_factors;
if (stream.handle_read_failure())
return false;
component.hsample_factor = subsample_factors >> 4;
component.vsample_factor = subsample_factors & 0x0F;
@ -732,6 +753,8 @@ static bool read_quantization_table(BufferStream& stream, JPGLoadingContext& con
while (bytes_to_read > 0) {
u8 info_byte;
stream >> info_byte;
if (stream.handle_read_failure())
return false;
u8 element_unit_hint = info_byte >> 4;
if (element_unit_hint > 1) {
dbg() << stream.offset()
@ -746,12 +769,15 @@ static bool read_quantization_table(BufferStream& stream, JPGLoadingContext& con
u32* table = table_id == 0 ? context.luma_table : context.chroma_table;
for (int i = 0; i < 64; i++) {
if (element_unit_hint == 0) {
u8 tmp;
u8 tmp = 0;
stream >> tmp;
table[zigzag_map[i]] = tmp;
} else
table[zigzag_map[i]] = read_be_word(stream);
}
if (stream.handle_read_failure())
return false;
bytes_to_read -= 1 + (element_unit_hint == 0 ? 64 : 128);
}
if (bytes_to_read != 0) {
@ -1081,6 +1107,8 @@ static bool scan_huffman_stream(BufferStream& stream, JPGLoadingContext& context
u8 last_byte;
u8 current_byte;
stream >> current_byte;
if (stream.handle_read_failure())
return false;
for (;;) {
last_byte = current_byte;

View file

@ -32,13 +32,16 @@
#include <LibGfx/PNGLoader.h>
#include <LibM/math.h>
#include <fcntl.h>
#include <serenity.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef __serenity__
# include <serenity.h>
#endif
//#define PNG_DEBUG
namespace Gfx {
@ -747,7 +750,11 @@ static bool decode_png_bitmap(PNGLoadingContext& context)
return false;
}
context.decompression_buffer_size = destlen;
#ifdef __serenity__
context.decompression_buffer = (u8*)mmap_with_name(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "PNG decompression buffer");
#else
context.decompression_buffer = (u8*)mmap(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
#endif
ret = puff(context.decompression_buffer, &destlen, context.compressed_data.data() + 2, &srclen);
if (ret != 0) {