1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:38:11 +00:00

LibHTML: Use ImageLoader for <img> elements to defer bitmap decoding

We now wait until the pixels are actually needed before fully decoding
images in <img> elements.

This needs some more work and is currently a bit memory-wasteful since
we'll hang on to the raw image data forever.
This commit is contained in:
Andreas Kling 2019-10-15 21:53:08 +02:00
parent b4c0ea89d5
commit 18fa662eb2
3 changed files with 18 additions and 7 deletions

View file

@ -29,7 +29,8 @@ void HTMLImageElement::load_image(const String& src)
return; return;
} }
m_bitmap = load_png_from_memory(data.data(), data.size()); m_image_data = data;
m_image_loader = ImageLoader::create(m_image_data.data(), m_image_data.size());
document().update_layout(); document().update_layout();
}); });
} }
@ -41,8 +42,8 @@ int HTMLImageElement::preferred_width() const
if (ok) if (ok)
return width; return width;
if (m_bitmap) if (m_image_loader)
return m_bitmap->width(); return m_image_loader->width();
return 0; return 0;
} }
@ -54,8 +55,8 @@ int HTMLImageElement::preferred_height() const
if (ok) if (ok)
return height; return height;
if (m_bitmap) if (m_image_loader)
return m_bitmap->height(); return m_image_loader->height();
return 0; return 0;
} }
@ -71,5 +72,7 @@ RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* p
const GraphicsBitmap* HTMLImageElement::bitmap() const const GraphicsBitmap* HTMLImageElement::bitmap() const
{ {
return m_bitmap; if (!m_image_loader)
return nullptr;
return m_image_loader->bitmap();
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <LibDraw/GraphicsBitmap.h> #include <LibDraw/GraphicsBitmap.h>
#include <LibDraw/ImageLoader.h>
#include <LibHTML/DOM/HTMLElement.h> #include <LibHTML/DOM/HTMLElement.h>
class HTMLImageElement : public HTMLElement { class HTMLImageElement : public HTMLElement {
@ -16,11 +17,14 @@ public:
int preferred_height() const; int preferred_height() const;
const GraphicsBitmap* bitmap() const; const GraphicsBitmap* bitmap() const;
const ImageLoader* image_loader() const { return m_image_loader; }
private: private:
void load_image(const String& src); void load_image(const String& src);
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override; virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
RefPtr<ImageLoader> m_image_loader;
mutable RefPtr<GraphicsBitmap> m_bitmap; mutable RefPtr<GraphicsBitmap> m_bitmap;
ByteBuffer m_image_data;
}; };

View file

@ -37,6 +37,10 @@ void LayoutImage::render(RenderingContext& context)
if (!is_visible()) if (!is_visible())
return; return;
// FIXME: This should be done at a different level. Also rect() does not include padding etc!
if (!context.viewport_rect().intersects(rect()))
return;
if (renders_as_alt_text()) { if (renders_as_alt_text()) {
context.painter().set_font(Font::default_font()); context.painter().set_font(Font::default_font());
StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2); StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2);
@ -52,5 +56,5 @@ void LayoutImage::render(RenderingContext& context)
bool LayoutImage::renders_as_alt_text() const bool LayoutImage::renders_as_alt_text() const
{ {
return !node().bitmap(); return !node().image_loader();
} }