LibHTML: Mark image bitmaps outside the visible viewport as volatile

When the visible viewport rect changes, we walk the layout tree and
check where each LayoutImage is in relation to the viewport rect.
Images outside have their bitmaps marked as volatile.

Note that the bitmaps are managed by ImageDecoder objects. If a bitmap
is purged by the kernel while volatile, we construct a new ImageDecoder
next time we need pixels for the image.
This commit is contained in:
Andreas Kling 2019-12-18 20:57:18 +01:00
parent 7e068565bc
commit 54bd322881
Notes: sideshowbarker 2024-07-19 10:48:59 +09:00
5 changed files with 38 additions and 2 deletions

View File

@ -80,3 +80,17 @@ const GraphicsBitmap* HTMLImageElement::bitmap() const
return nullptr;
return m_image_decoder->bitmap();
}
void HTMLImageElement::set_volatile(Badge<LayoutDocument>, bool v)
{
if (!m_image_decoder)
return;
if (v) {
m_image_decoder->set_volatile();
return;
}
bool has_image = m_image_decoder->set_nonvolatile();
if (has_image)
return;
m_image_decoder = ImageDecoder::create(m_encoded_data.data(), m_encoded_data.size());
}

View File

@ -4,6 +4,8 @@
#include <LibDraw/ImageDecoder.h>
#include <LibHTML/DOM/HTMLElement.h>
class LayoutDocument;
class HTMLImageElement : public HTMLElement {
public:
HTMLImageElement(Document&, const String& tag_name);
@ -19,12 +21,13 @@ public:
const GraphicsBitmap* bitmap() const;
const ImageDecoder* image_decoder() const { return m_image_decoder; }
void set_volatile(Badge<LayoutDocument>, bool);
private:
void load_image(const String& src);
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
RefPtr<ImageDecoder> m_image_decoder;
mutable RefPtr<GraphicsBitmap> m_bitmap;
ByteBuffer m_encoded_data;
};

View File

@ -1,6 +1,7 @@
#include <LibHTML/DOM/Document.h>
#include <LibHTML/Frame.h>
#include <LibHTML/HtmlView.h>
#include <LibHTML/Layout/LayoutDocument.h>
Frame::Frame(HtmlView& html_view)
: m_html_view(html_view.make_weak_ptr())
@ -37,6 +38,9 @@ void Frame::set_viewport_rect(const Rect& rect)
if (m_viewport_rect == rect)
return;
m_viewport_rect = rect;
if (m_document && m_document->layout_node())
m_document->layout_node()->did_set_viewport_rect({}, rect);
}
void Frame::set_needs_display(const Rect& rect)

View File

@ -1,6 +1,7 @@
#include <LibHTML/Frame.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Frame.h>
#include <LibHTML/Layout/LayoutDocument.h>
#include <LibHTML/Layout/LayoutImage.h>
LayoutDocument::LayoutDocument(const Document& document, NonnullRefPtr<StyleProperties> style)
: LayoutBlock(&document, move(style))
@ -29,3 +30,15 @@ void LayoutDocument::layout()
});
rect().set_bottom(lowest_bottom);
}
void LayoutDocument::did_set_viewport_rect(Badge<Frame>, const Rect& a_viewport_rect)
{
FloatRect viewport_rect(a_viewport_rect.x(), a_viewport_rect.y(), a_viewport_rect.width(), a_viewport_rect.height());
for_each_in_subtree([&](auto& layout_node) {
if (is<LayoutImage>(layout_node)) {
auto& image = to<LayoutImage>(layout_node);
const_cast<HTMLImageElement&>(image.node()).set_volatile({}, !viewport_rect.intersects(image.rect()));
}
return IterationDecision::Continue;
});
}

View File

@ -15,6 +15,8 @@ public:
const LayoutRange& selection() const { return m_selection; }
LayoutRange& selection() { return m_selection; }
void did_set_viewport_rect(Badge<Frame>, const Rect&);
private:
LayoutRange m_selection;
};