Bitmap.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2018-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/Checked.h>
  27. #include <AK/Memory.h>
  28. #include <AK/SharedBuffer.h>
  29. #include <AK/String.h>
  30. #include <LibGfx/Bitmap.h>
  31. #include <LibGfx/PNGLoader.h>
  32. #include <LibGfx/GIFLoader.h>
  33. #include <LibGfx/ShareableBitmap.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <sys/mman.h>
  38. #include <unistd.h>
  39. namespace Gfx {
  40. static bool size_would_overflow(BitmapFormat format, const Size& size)
  41. {
  42. if (size.width() < 0 || size.height() < 0)
  43. return true;
  44. return Checked<size_t>::multiplication_would_overflow(size.width(), size.height(), Bitmap::bpp_for_format(format));
  45. }
  46. RefPtr<Bitmap> Bitmap::create(BitmapFormat format, const Size& size)
  47. {
  48. if (size_would_overflow(format, size))
  49. return nullptr;
  50. return adopt(*new Bitmap(format, size, Purgeable::No));
  51. }
  52. RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const Size& size)
  53. {
  54. if (size_would_overflow(format, size))
  55. return nullptr;
  56. return adopt(*new Bitmap(format, size, Purgeable::Yes));
  57. }
  58. Bitmap::Bitmap(BitmapFormat format, const Size& size, Purgeable purgeable)
  59. : m_size(size)
  60. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  61. , m_format(format)
  62. , m_purgeable(purgeable == Purgeable::Yes)
  63. {
  64. ASSERT(!m_size.is_empty());
  65. ASSERT(!size_would_overflow(format, size));
  66. if (format == BitmapFormat::Indexed8)
  67. m_palette = new RGBA32[256];
  68. int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
  69. m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, map_flags, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
  70. ASSERT(m_data && m_data != (void*)-1);
  71. m_needs_munmap = true;
  72. }
  73. RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
  74. {
  75. if (size_would_overflow(format, size))
  76. return nullptr;
  77. return adopt(*new Bitmap(format, size, pitch, data));
  78. }
  79. RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
  80. {
  81. if(path.ends_with(".png"))
  82. return load_png(path);
  83. if(path.ends_with(".gif"))
  84. return load_gif(path);
  85. return nullptr;
  86. }
  87. Bitmap::Bitmap(BitmapFormat format, const Size& size, size_t pitch, RGBA32* data)
  88. : m_size(size)
  89. , m_data(data)
  90. , m_pitch(pitch)
  91. , m_format(format)
  92. {
  93. ASSERT(!size_would_overflow(format, size));
  94. if (format == BitmapFormat::Indexed8)
  95. m_palette = new RGBA32[256];
  96. }
  97. RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
  98. {
  99. if (size_would_overflow(format, size))
  100. return nullptr;
  101. return adopt(*new Bitmap(format, move(shared_buffer), size));
  102. }
  103. Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
  104. : m_size(size)
  105. , m_data((RGBA32*)shared_buffer->data())
  106. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  107. , m_format(format)
  108. , m_shared_buffer(move(shared_buffer))
  109. {
  110. ASSERT(format != BitmapFormat::Indexed8);
  111. ASSERT(!size_would_overflow(format, size));
  112. }
  113. RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
  114. {
  115. auto w = this->width();
  116. auto h = this->height();
  117. auto new_bitmap = Gfx::Bitmap::create(this->format(), { h, w });
  118. if (!new_bitmap)
  119. return nullptr;
  120. for (int i = 0; i < w; i++) {
  121. for (int j = 0; j < h; j++) {
  122. Color color;
  123. if (rotation_direction == Gfx::RotationDirection::Left)
  124. color = this->get_pixel(w - i - 1, j);
  125. else
  126. color = this->get_pixel(i, h - j - 1);
  127. new_bitmap->set_pixel(j, i, color);
  128. }
  129. }
  130. return new_bitmap;
  131. }
  132. RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
  133. {
  134. auto w = this->width();
  135. auto h = this->height();
  136. auto new_bitmap = Gfx::Bitmap::create(this->format(), { w, h });
  137. if (!new_bitmap)
  138. return nullptr;
  139. for (int i = 0; i < w; i++) {
  140. for (int j = 0; j < h; j++) {
  141. Color color = this->get_pixel(i, j);
  142. if (orientation == Orientation::Vertical)
  143. new_bitmap->set_pixel(i, h - j - 1, color);
  144. else
  145. new_bitmap->set_pixel(w - i - 1, j, color);
  146. }
  147. }
  148. return new_bitmap;
  149. }
  150. RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
  151. {
  152. if (m_shared_buffer)
  153. return *this;
  154. auto buffer = SharedBuffer::create_with_size(size_in_bytes());
  155. auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size);
  156. if (!bitmap)
  157. return nullptr;
  158. memcpy(buffer->data(), scanline(0), size_in_bytes());
  159. return bitmap;
  160. }
  161. Bitmap::~Bitmap()
  162. {
  163. if (m_needs_munmap) {
  164. int rc = munmap(m_data, size_in_bytes());
  165. ASSERT(rc == 0);
  166. }
  167. m_data = nullptr;
  168. delete[] m_palette;
  169. }
  170. void Bitmap::set_mmap_name(const StringView& name)
  171. {
  172. ASSERT(m_needs_munmap);
  173. ::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
  174. }
  175. void Bitmap::fill(Color color)
  176. {
  177. ASSERT(m_format == BitmapFormat::RGB32 || m_format == BitmapFormat::RGBA32);
  178. for (int y = 0; y < height(); ++y) {
  179. auto* scanline = this->scanline(y);
  180. fast_u32_fill(scanline, color.value(), width());
  181. }
  182. }
  183. void Bitmap::set_volatile()
  184. {
  185. ASSERT(m_purgeable);
  186. if (m_volatile)
  187. return;
  188. int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
  189. if (rc < 0) {
  190. perror("madvise(MADV_SET_VOLATILE)");
  191. ASSERT_NOT_REACHED();
  192. }
  193. m_volatile = true;
  194. }
  195. [[nodiscard]] bool Bitmap::set_nonvolatile()
  196. {
  197. ASSERT(m_purgeable);
  198. if (!m_volatile)
  199. return true;
  200. int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
  201. if (rc < 0) {
  202. perror("madvise(MADV_SET_NONVOLATILE)");
  203. ASSERT_NOT_REACHED();
  204. }
  205. m_volatile = false;
  206. return rc == 0;
  207. }
  208. int Bitmap::shbuf_id() const
  209. {
  210. return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
  211. }
  212. ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
  213. {
  214. auto bitmap = to_bitmap_backed_by_shared_buffer();
  215. if (!bitmap)
  216. return {};
  217. if (peer_pid > 0)
  218. bitmap->shared_buffer()->share_with(peer_pid);
  219. return ShareableBitmap(*bitmap);
  220. }
  221. }