ClientConnection.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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& message)
  48. {
  49. set_client_pid(message.client_pid());
  50. return make<Messages::ImageDecoderServer::GreetResponse>(client_id(), getpid());
  51. }
  52. OwnPtr<Messages::ImageDecoderServer::DecodeImageResponse> ClientConnection::handle(const Messages::ImageDecoderServer::DecodeImage& message)
  53. {
  54. auto encoded_buffer = message.data();
  55. if (!encoded_buffer.is_valid()) {
  56. #if IMAGE_DECODER_DEBUG
  57. dbgln("Encoded data is invalid");
  58. #endif
  59. return {};
  60. }
  61. auto decoder = Gfx::ImageDecoder::create(encoded_buffer.data<u8>(), encoded_buffer.size());
  62. if (!decoder->frame_count()) {
  63. #if IMAGE_DECODER_DEBUG
  64. dbgln("Could not decode image from encoded data");
  65. #endif
  66. return make<Messages::ImageDecoderServer::DecodeImageResponse>(false, 0, Vector<Gfx::ShareableBitmap> {}, Vector<u32> {});
  67. }
  68. Vector<Gfx::ShareableBitmap> bitmaps;
  69. Vector<u32> durations;
  70. for (size_t i = 0; i < decoder->frame_count(); ++i) {
  71. // FIXME: All image decoder plugins should be rewritten to return frame() instead of bitmap().
  72. // Non-animated images can simply return 1 frame.
  73. Gfx::ImageFrameDescriptor frame;
  74. if (decoder->is_animated()) {
  75. frame = decoder->frame(i);
  76. } else {
  77. frame.image = decoder->bitmap();
  78. }
  79. if (frame.image)
  80. bitmaps.append(frame.image->to_shareable_bitmap());
  81. else
  82. bitmaps.append(Gfx::ShareableBitmap {});
  83. durations.append(frame.duration);
  84. }
  85. return make<Messages::ImageDecoderServer::DecodeImageResponse>(decoder->is_animated(), decoder->loop_count(), bitmaps, durations);
  86. }
  87. }