mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
LibWeb: Make requestAnimationFrame() work in multi-process mode
Since we don't have a direct connection to WindowServer, this is slighly more naive implementation than what we were doing for single-process mode. Basically, there's a global RequestAnimationFrameDriver object that has a 16ms single-shot timer. Whenever someone registers for a RAF callback, we start the timer (if needed). This is not ideal, but it's better than nothing. :^)
This commit is contained in:
parent
f21041861b
commit
7632cce5e5
2 changed files with 80 additions and 14 deletions
|
@ -19,6 +19,70 @@
|
||||||
|
|
||||||
namespace Web::DOM {
|
namespace Web::DOM {
|
||||||
|
|
||||||
|
class RequestAnimationFrameCallback : public RefCounted<RequestAnimationFrameCallback> {
|
||||||
|
public:
|
||||||
|
explicit RequestAnimationFrameCallback(i32 id, Function<void(i32)> handler)
|
||||||
|
: m_id(id)
|
||||||
|
, m_handler(move(handler))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
~RequestAnimationFrameCallback() { }
|
||||||
|
|
||||||
|
i32 id() const { return m_id; }
|
||||||
|
bool is_cancelled() const { return !m_handler; }
|
||||||
|
|
||||||
|
void cancel() { m_handler = nullptr; }
|
||||||
|
void invoke() { m_handler(m_id); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
i32 m_id { 0 };
|
||||||
|
Function<void(i32)> m_handler;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RequestAnimationFrameDriver {
|
||||||
|
RequestAnimationFrameDriver()
|
||||||
|
{
|
||||||
|
m_timer = Core::Timer::create_single_shot(16, [this] {
|
||||||
|
auto taken_callbacks = move(m_callbacks);
|
||||||
|
for (auto& it : taken_callbacks) {
|
||||||
|
if (!it.value->is_cancelled())
|
||||||
|
it.value->invoke();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRefPtr<RequestAnimationFrameCallback> add(Function<void(i32)> handler)
|
||||||
|
{
|
||||||
|
auto id = m_id_allocator.allocate();
|
||||||
|
auto callback = adopt_ref(*new RequestAnimationFrameCallback { id, move(handler) });
|
||||||
|
m_callbacks.set(id, callback);
|
||||||
|
if (!m_timer->is_active())
|
||||||
|
m_timer->start();
|
||||||
|
return callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool remove(i32 id)
|
||||||
|
{
|
||||||
|
auto it = m_callbacks.find(id);
|
||||||
|
if (it == m_callbacks.end())
|
||||||
|
return false;
|
||||||
|
m_callbacks.remove(it);
|
||||||
|
m_id_allocator.deallocate(id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_callbacks;
|
||||||
|
IDAllocator m_id_allocator;
|
||||||
|
RefPtr<Core::Timer> m_timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
static RequestAnimationFrameDriver& request_animation_frame_driver()
|
||||||
|
{
|
||||||
|
static RequestAnimationFrameDriver driver;
|
||||||
|
return driver;
|
||||||
|
}
|
||||||
|
|
||||||
NonnullRefPtr<Window> Window::create_with_document(Document& document)
|
NonnullRefPtr<Window> Window::create_with_document(Document& document)
|
||||||
{
|
{
|
||||||
return adopt_ref(*new Window(document));
|
return adopt_ref(*new Window(document));
|
||||||
|
@ -113,29 +177,27 @@ void Window::clear_interval(i32 timer_id)
|
||||||
m_timers.remove(timer_id);
|
m_timers.remove(timer_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 Window::request_animation_frame(JS::FunctionObject& callback)
|
i32 Window::request_animation_frame(JS::FunctionObject& js_callback)
|
||||||
{
|
{
|
||||||
// FIXME: This is extremely fake!
|
auto callback = request_animation_frame_driver().add([this, handle = JS::make_handle(&js_callback)](i32 id) mutable {
|
||||||
static double fake_timestamp = 0;
|
auto& function = *handle.cell();
|
||||||
|
|
||||||
i32 link_id = GUI::DisplayLink::register_callback([handle = make_handle(&callback)](i32 link_id) {
|
|
||||||
auto& function = const_cast<JS::FunctionObject&>(static_cast<JS::FunctionObject const&>(*handle.cell()));
|
|
||||||
auto& vm = function.vm();
|
auto& vm = function.vm();
|
||||||
fake_timestamp += 10;
|
(void)vm.call(function, JS::js_undefined(), JS::Value((double)Core::DateTime::now().timestamp()));
|
||||||
[[maybe_unused]] auto rc = vm.call(function, JS::js_undefined(), JS::Value(fake_timestamp));
|
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
vm.clear_exception();
|
vm.clear_exception();
|
||||||
GUI::DisplayLink::unregister_callback(link_id);
|
m_request_animation_frame_callbacks.remove(id);
|
||||||
});
|
});
|
||||||
|
m_request_animation_frame_callbacks.set(callback->id(), callback);
|
||||||
// FIXME: Don't hand out raw DisplayLink ID's to JavaScript!
|
return callback->id();
|
||||||
return link_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::cancel_animation_frame(i32 id)
|
void Window::cancel_animation_frame(i32 id)
|
||||||
{
|
{
|
||||||
// FIXME: We should not be passing untrusted numbers to DisplayLink::unregister_callback()!
|
auto it = m_request_animation_frame_callbacks.find(id);
|
||||||
GUI::DisplayLink::unregister_callback(id);
|
if (it == m_request_animation_frame_callbacks.end())
|
||||||
|
return;
|
||||||
|
it->value->cancel();
|
||||||
|
m_request_animation_frame_callbacks.remove(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href)
|
void Window::did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href)
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
namespace Web::DOM {
|
namespace Web::DOM {
|
||||||
|
|
||||||
|
class RequestAnimationFrameCallback;
|
||||||
|
|
||||||
class Window final
|
class Window final
|
||||||
: public RefCounted<Window>
|
: public RefCounted<Window>
|
||||||
, public EventTarget {
|
, public EventTarget {
|
||||||
|
@ -90,6 +92,8 @@ private:
|
||||||
NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
|
NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
|
||||||
NonnullRefPtr<CSS::Screen> m_screen;
|
NonnullRefPtr<CSS::Screen> m_screen;
|
||||||
RefPtr<Event> m_current_event;
|
RefPtr<Event> m_current_event;
|
||||||
|
|
||||||
|
HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_request_animation_frame_callbacks;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue