ClientConnection.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <ImageDecoder/ClientConnection.h>
  8. #include <ImageDecoder/ImageDecoderClientEndpoint.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/ImageDecoder.h>
  11. namespace ImageDecoder {
  12. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  13. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  14. : IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), client_id)
  15. {
  16. s_connections.set(client_id, *this);
  17. }
  18. ClientConnection::~ClientConnection()
  19. {
  20. }
  21. void ClientConnection::die()
  22. {
  23. s_connections.remove(client_id());
  24. exit(0);
  25. }
  26. Messages::ImageDecoderServer::GreetResponse ClientConnection::handle(const Messages::ImageDecoderServer::Greet&)
  27. {
  28. return {};
  29. }
  30. Messages::ImageDecoderServer::DecodeImageResponse ClientConnection::handle(const Messages::ImageDecoderServer::DecodeImage& message)
  31. {
  32. auto encoded_buffer = message.data();
  33. if (!encoded_buffer.is_valid()) {
  34. dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
  35. return nullptr;
  36. }
  37. auto decoder = Gfx::ImageDecoder::create(encoded_buffer.data<u8>(), encoded_buffer.size());
  38. if (!decoder->frame_count()) {
  39. dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
  40. return { false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {} };
  41. }
  42. Vector<Gfx::ShareableBitmap> bitmaps;
  43. Vector<u32> durations;
  44. for (size_t i = 0; i < decoder->frame_count(); ++i) {
  45. // FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap().
  46. // Non-animated images can simply return 1 frame.
  47. Gfx::ImageFrameDescriptor frame;
  48. if (decoder->is_animated()) {
  49. frame = decoder->frame(i);
  50. } else {
  51. frame.image = decoder->bitmap();
  52. }
  53. if (frame.image)
  54. bitmaps.append(frame.image->to_shareable_bitmap());
  55. else
  56. bitmaps.append(Gfx::ShareableBitmap {});
  57. durations.append(frame.duration);
  58. }
  59. return { decoder->is_animated(), decoder->loop_count(), bitmaps, durations };
  60. }
  61. }