ClientConnection.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/Badge.h>
  27. #include <ImageDecoder/ClientConnection.h>
  28. #include <ImageDecoder/ImageDecoderClientEndpoint.h>
  29. #include <LibGfx/Bitmap.h>
  30. #include <LibGfx/ImageDecoder.h>
  31. #include <LibGfx/SystemTheme.h>
  32. namespace ImageDecoder {
  33. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  34. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  35. : IPC::ClientConnection<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket), client_id)
  36. {
  37. s_connections.set(client_id, *this);
  38. }
  39. ClientConnection::~ClientConnection()
  40. {
  41. }
  42. void ClientConnection::die()
  43. {
  44. s_connections.remove(client_id());
  45. exit(0);
  46. }
  47. OwnPtr<Messages::ImageDecoderServer::GreetResponse> ClientConnection::handle(const Messages::ImageDecoderServer::Greet&)
  48. {
  49. return make<Messages::ImageDecoderServer::GreetResponse>(client_id());
  50. }
  51. OwnPtr<Messages::ImageDecoderServer::DecodeImageResponse> ClientConnection::handle(const Messages::ImageDecoderServer::DecodeImage& message)
  52. {
  53. auto encoded_buffer = message.data();
  54. if (!encoded_buffer.is_valid()) {
  55. #if IMAGE_DECODER_DEBUG
  56. dbgln("Encoded data is invalid");
  57. #endif
  58. return {};
  59. }
  60. auto decoder = Gfx::ImageDecoder::create(encoded_buffer.data<u8>(), encoded_buffer.size());
  61. if (!decoder->frame_count()) {
  62. #if IMAGE_DECODER_DEBUG
  63. dbgln("Could not decode image from encoded data");
  64. #endif
  65. return make<Messages::ImageDecoderServer::DecodeImageResponse>(false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {});
  66. }
  67. Vector<Gfx::ShareableBitmap> bitmaps;
  68. Vector<u32> durations;
  69. for (size_t i = 0; i < decoder->frame_count(); ++i) {
  70. // FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap().
  71. // Non-animated images can simply return 1 frame.
  72. Gfx::ImageFrameDescriptor frame;
  73. if (decoder->is_animated()) {
  74. frame = decoder->frame(i);
  75. } else {
  76. frame.image = decoder->bitmap();
  77. }
  78. if (frame.image)
  79. bitmaps.append(frame.image->to_shareable_bitmap());
  80. else
  81. bitmaps.append(Gfx::ShareableBitmap {});
  82. durations.append(frame.duration);
  83. }
  84. return make<Messages::ImageDecoderServer::DecodeImageResponse>(decoder->is_animated(), decoder->loop_count(), bitmaps, durations);
  85. }
  86. }