ShareableBitmap.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <LibGfx/Bitmap.h>
  27. #include <LibGfx/ShareableBitmap.h>
  28. #include <LibGfx/Size.h>
  29. #include <LibIPC/Decoder.h>
  30. #include <LibIPC/Encoder.h>
  31. #include <LibIPC/File.h>
  32. namespace Gfx {
  33. ShareableBitmap::ShareableBitmap(const Bitmap& bitmap)
  34. : m_bitmap(bitmap.to_bitmap_backed_by_anon_fd())
  35. {
  36. }
  37. ShareableBitmap::ShareableBitmap(NonnullRefPtr<Bitmap> bitmap, Tag)
  38. : m_bitmap(move(bitmap))
  39. {
  40. }
  41. }
  42. namespace IPC {
  43. bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
  44. {
  45. encoder << shareable_bitmap.is_valid();
  46. if (!shareable_bitmap.is_valid())
  47. return true;
  48. auto& bitmap = *shareable_bitmap.bitmap();
  49. encoder << IPC::File(bitmap.anon_fd());
  50. encoder << bitmap.size();
  51. encoder << bitmap.scale();
  52. encoder << (u32)bitmap.format();
  53. if (bitmap.is_indexed()) {
  54. auto palette = bitmap.palette_to_vector();
  55. encoder << palette;
  56. }
  57. return true;
  58. }
  59. bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
  60. {
  61. bool valid = false;
  62. if (!decoder.decode(valid))
  63. return false;
  64. if (!valid) {
  65. shareable_bitmap = {};
  66. return true;
  67. }
  68. IPC::File anon_file;
  69. if (!decoder.decode(anon_file))
  70. return false;
  71. Gfx::IntSize size;
  72. if (!decoder.decode(size))
  73. return false;
  74. u32 scale;
  75. if (!decoder.decode(scale))
  76. return false;
  77. u32 raw_bitmap_format;
  78. if (!decoder.decode(raw_bitmap_format))
  79. return false;
  80. if (!Gfx::is_valid_bitmap_format(raw_bitmap_format))
  81. return false;
  82. auto bitmap_format = (Gfx::BitmapFormat)raw_bitmap_format;
  83. Vector<Gfx::RGBA32> palette;
  84. if (Gfx::Bitmap::is_indexed(bitmap_format)) {
  85. if (!decoder.decode(palette))
  86. return false;
  87. }
  88. auto bitmap = Gfx::Bitmap::create_with_anon_fd(bitmap_format, anon_file.take_fd(), size, scale, palette, Gfx::Bitmap::ShouldCloseAnonymousFile::Yes);
  89. if (!bitmap)
  90. return false;
  91. shareable_bitmap = Gfx::ShareableBitmap { bitmap.release_nonnull(), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
  92. return true;
  93. }
  94. }