Skip to content

Truly independent web browser

LibCore+LibMedia: Map a lent software frame's pages up front

Zaggy10245538771ef026
diff --git a/Libraries/LibCore/AnonymousBuffer.cpp b/Libraries/LibCore/AnonymousBuffer.cpp
index f2cd34ebc47..5a62a34275f 100644
--- a/Libraries/LibCore/AnonymousBuffer.cpp
+++ b/Libraries/LibCore/AnonymousBuffer.cpp
@@ -120,6 +120,19 @@ ErrorOr<void> AnonymousBuffer::validate_backing_size() const
return {};
}
+void AnonymousBuffer::populate_for_reading() const
+{
+ if (!is_valid() || size() == 0)
+ return;
+ auto* data = const_cast<void*>(m_impl->data());
+#if defined(AK_OS_LINUX) && defined(MADV_POPULATE_READ)
+ // Kernels before 5.14 refuse this advice.
+ (void)madvise(data, size(), MADV_POPULATE_READ);
+#else
+ (void)madvise(data, size(), MADV_WILLNEED);
+#endif
+}
+
ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> AnonymousBufferImpl::create(int fd, size_t size)
{
void* data = nullptr;
diff --git a/Libraries/LibCore/AnonymousBuffer.h b/Libraries/LibCore/AnonymousBuffer.h
index 09dfe9b5d9f..b729b4f5ec7 100644
--- a/Libraries/LibCore/AnonymousBuffer.h
+++ b/Libraries/LibCore/AnonymousBuffer.h
@@ -52,6 +52,9 @@ public:
// the backing store against shrinking where the peer could still shrink it.
ErrorOr<void> validate_backing_size() const;
+ // Worth calling only on a buffer about to be read in full.
+ void populate_for_reading() const;
+
int fd() const { return m_impl ? m_impl->fd() : -1; }
size_t size() const { return m_impl ? m_impl->size() : 0; }
diff --git a/Libraries/LibCore/AnonymousBufferWindows.cpp b/Libraries/LibCore/AnonymousBufferWindows.cpp
index 69a2a0e6697..680fe490b76 100644
--- a/Libraries/LibCore/AnonymousBufferWindows.cpp
+++ b/Libraries/LibCore/AnonymousBufferWindows.cpp
@@ -95,4 +95,12 @@ ErrorOr<void> AnonymousBuffer::validate_backing_size() const
return {};
}
+void AnonymousBuffer::populate_for_reading() const
+{
+ if (!is_valid() || size() == 0)
+ return;
+ WIN32_MEMORY_RANGE_ENTRY range { const_cast<void*>(m_impl->data()), size() };
+ (void)PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0);
+}
+
}
diff --git a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp
index 56e75f9d2cf..05736e6e08e 100644
--- a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp
+++ b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp
@@ -136,7 +136,10 @@ void VideoPresentationClientConnection::lend_frame(u64 edge_id, u64 lend_id, u32
dbgln("VideoPresentation: Dropping a lent frame that does not fit its storage: {}", frame_or_error.error());
return;
}
- edge_state->consumer->receive_frame(lend_id, seek_id, frame_or_error.release_value());
+ auto frame = frame_or_error.release_value();
+ // Every frame comes with a fresh mapping of its buffer, which is read in full to upload it.
+ frame->pixel_buffer().populate_for_reading();
+ edge_state->consumer->receive_frame(lend_id, seek_id, move(frame));
}
void VideoPresentationClientConnection::edge_status_changed(u64 edge_id, PipelineStatus status, u32 seek_id)