mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 18:32:28 -05:00
ImageDecoder: Use Core::AnonymousBuffer and Gfx::ShareableBitmap
...instead of sending shbufs back and forth. :^)
This commit is contained in:
parent
8a61aba1e5
commit
447e6da52c
4 changed files with 14 additions and 58 deletions
|
@ -24,7 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/SharedBuffer.h>
|
||||
#include <LibCore/AnonymousBuffer.h>
|
||||
#include <LibImageDecoderClient/Client.h>
|
||||
|
||||
namespace ImageDecoderClient {
|
||||
|
@ -51,38 +51,15 @@ RefPtr<Gfx::Bitmap> Client::decode_image(const ByteBuffer& encoded_data)
|
|||
if (encoded_data.is_empty())
|
||||
return nullptr;
|
||||
|
||||
auto encoded_buffer = SharedBuffer::create_with_size(encoded_data.size());
|
||||
if (!encoded_buffer) {
|
||||
dbgln("Could not allocate encoded shbuf");
|
||||
auto encoded_buffer = Core::AnonymousBuffer::create_with_size(encoded_data.size());
|
||||
if (!encoded_buffer.is_valid()) {
|
||||
dbgln("Could not allocate encoded buffer");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
memcpy(encoded_buffer->data<void>(), encoded_data.data(), encoded_data.size());
|
||||
|
||||
encoded_buffer->seal();
|
||||
encoded_buffer->share_with(server_pid());
|
||||
|
||||
auto response = send_sync<Messages::ImageDecoderServer::DecodeImage>(encoded_buffer->shbuf_id(), encoded_data.size());
|
||||
auto bitmap_format = (Gfx::BitmapFormat)response->bitmap_format();
|
||||
if (bitmap_format == Gfx::BitmapFormat::Invalid) {
|
||||
#ifdef IMAGE_DECODER_CLIENT_DEBUG
|
||||
dbgln("Response image was invalid");
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (response->size().is_empty()) {
|
||||
dbgln("Response image was empty");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto decoded_buffer = SharedBuffer::create_from_shbuf_id(response->decoded_shbuf_id());
|
||||
if (!decoded_buffer) {
|
||||
dbgln("Could not map decoded image shbuf_id={}", response->decoded_shbuf_id());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return Gfx::Bitmap::create_with_shared_buffer(bitmap_format, decoded_buffer.release_nonnull(), response->size(), response->palette());
|
||||
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
|
||||
auto response = send_sync<Messages::ImageDecoderServer::DecodeImage>(move(encoded_buffer));
|
||||
return response->bitmap().bitmap();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -60,43 +60,25 @@ OwnPtr<Messages::ImageDecoderServer::GreetResponse> ClientConnection::handle(con
|
|||
|
||||
OwnPtr<Messages::ImageDecoderServer::DecodeImageResponse> ClientConnection::handle(const Messages::ImageDecoderServer::DecodeImage& message)
|
||||
{
|
||||
auto encoded_buffer = SharedBuffer::create_from_shbuf_id(message.encoded_shbuf_id());
|
||||
if (!encoded_buffer) {
|
||||
auto encoded_buffer = message.data();
|
||||
if (!encoded_buffer.is_valid()) {
|
||||
#ifdef IMAGE_DECODER_DEBUG
|
||||
dbgln("Could not map encoded data buffer");
|
||||
dbgln("Encoded data is invalid");
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
if (message.encoded_size() > (size_t)encoded_buffer->size()) {
|
||||
#ifdef IMAGE_DECODER_DEBUG
|
||||
dbgln("Encoded buffer is smaller than encoded size");
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
#ifdef IMAGE_DECODER_DEBUG
|
||||
dbg() << "Trying to decode " << message.encoded_size() << " bytes of image(?) data in shbuf_id=" << message.encoded_shbuf_id() << " (shbuf size: " << encoded_buffer->size() << ")";
|
||||
#endif
|
||||
|
||||
auto decoder = Gfx::ImageDecoder::create(encoded_buffer->data<u8>(), message.encoded_size());
|
||||
auto decoder = Gfx::ImageDecoder::create(encoded_buffer.data<u8>(), encoded_buffer.size());
|
||||
auto bitmap = decoder->bitmap();
|
||||
|
||||
if (!bitmap) {
|
||||
#ifdef IMAGE_DECODER_DEBUG
|
||||
dbgln("Could not decode image from encoded data");
|
||||
#endif
|
||||
return make<Messages::ImageDecoderServer::DecodeImageResponse>(-1, Gfx::IntSize(), (i32)Gfx::BitmapFormat::Invalid, Vector<u32>());
|
||||
return make<Messages::ImageDecoderServer::DecodeImageResponse>(Gfx::ShareableBitmap());
|
||||
}
|
||||
|
||||
// FIXME: We should fix ShareableBitmap so you can send it in responses as well as requests..
|
||||
m_shareable_bitmap = bitmap->to_bitmap_backed_by_shared_buffer();
|
||||
m_shareable_bitmap->shared_buffer()->share_with(client_pid());
|
||||
Vector<u32> palette;
|
||||
if (m_shareable_bitmap->is_indexed()) {
|
||||
palette = m_shareable_bitmap->palette_to_vector();
|
||||
}
|
||||
return make<Messages::ImageDecoderServer::DecodeImageResponse>(m_shareable_bitmap->shbuf_id(), m_shareable_bitmap->size(), (i32)m_shareable_bitmap->format(), palette);
|
||||
return make<Messages::ImageDecoderServer::DecodeImageResponse>(bitmap->to_shareable_bitmap());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -49,8 +49,6 @@ public:
|
|||
private:
|
||||
virtual OwnPtr<Messages::ImageDecoderServer::GreetResponse> handle(const Messages::ImageDecoderServer::Greet&) override;
|
||||
virtual OwnPtr<Messages::ImageDecoderServer::DecodeImageResponse> handle(const Messages::ImageDecoderServer::DecodeImage&) override;
|
||||
|
||||
RefPtr<Gfx::Bitmap> m_shareable_bitmap;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,5 @@ endpoint ImageDecoderServer = 7001
|
|||
{
|
||||
Greet(i32 client_pid) => (i32 client_id, i32 server_pid)
|
||||
|
||||
DecodeImage(i32 encoded_shbuf_id, u32 encoded_size) => (i32 decoded_shbuf_id, Gfx::IntSize size, i32 bitmap_format, Vector<u32> palette)
|
||||
|
||||
DecodeImage(Core::AnonymousBuffer data) => (Gfx::ShareableBitmap bitmap)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue