ClientConnection.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <ImageDecoder/ClientConnection.h>
  28. #include <ImageDecoder/ImageDecoderClientEndpoint.h>
  29. #include <LibGfx/Bitmap.h>
  30. #include <LibGfx/ImageDecoder.h>
  31. namespace ImageDecoder {
  32. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  33. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  34. : IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), client_id)
  35. {
  36. s_connections.set(client_id, *this);
  37. }
  38. ClientConnection::~ClientConnection()
  39. {
  40. }
  41. void ClientConnection::die()
  42. {
  43. s_connections.remove(client_id());
  44. exit(0);
  45. }
  46. OwnPtr<Messages::ImageDecoderServer::GreetResponse> ClientConnection::handle(const Messages::ImageDecoderServer::Greet&)
  47. {
  48. return make<Messages::ImageDecoderServer::GreetResponse>();
  49. }
  50. OwnPtr<Messages::ImageDecoderServer::DecodeImageResponse> ClientConnection::handle(const Messages::ImageDecoderServer::DecodeImage& message)
  51. {
  52. auto encoded_buffer = message.data();
  53. if (!encoded_buffer.is_valid()) {
  54. dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid");
  55. return {};
  56. }
  57. auto decoder = Gfx::ImageDecoder::create(encoded_buffer.data<u8>(), encoded_buffer.size());
  58. if (!decoder->frame_count()) {
  59. dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode image from encoded data");
  60. return make<Messages::ImageDecoderServer::DecodeImageResponse>(false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {});
  61. }
  62. Vector<Gfx::ShareableBitmap> bitmaps;
  63. Vector<u32> durations;
  64. for (size_t i = 0; i < decoder->frame_count(); ++i) {
  65. // FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap().
  66. // Non-animated images can simply return 1 frame.
  67. Gfx::ImageFrameDescriptor frame;
  68. if (decoder->is_animated()) {
  69. frame = decoder->frame(i);
  70. } else {
  71. frame.image = decoder->bitmap();
  72. }
  73. if (frame.image)
  74. bitmaps.append(frame.image->to_shareable_bitmap());
  75. else
  76. bitmaps.append(Gfx::ShareableBitmap {});
  77. durations.append(frame.duration);
  78. }
  79. return make<Messages::ImageDecoderServer::DecodeImageResponse>(decoder->is_animated(), decoder->loop_count(), bitmaps, durations);
  80. }
  81. }