Bitmap.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/BMPLoader.h>
  32. #include <LibGfx/GIFLoader.h>
  33. #include <LibGfx/PBMLoader.h>
  34. #include <LibGfx/PNGLoader.h>
  35. #include <LibGfx/ShareableBitmap.h>
  36. #include <fcntl.h>
  37. #include <stdio.h>
  38. #include <sys/mman.h>
  39. namespace Gfx {
  40. static bool size_would_overflow(BitmapFormat format, const IntSize& 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 IntSize& 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 IntSize& 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 IntSize& 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. allocate_palette_from_format(format);
  67. int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
  68. 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());
  69. ASSERT(m_data && m_data != (void*)-1);
  70. m_needs_munmap = true;
  71. }
  72. RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const IntSize& size, size_t pitch, RGBA32* data)
  73. {
  74. if (size_would_overflow(format, size))
  75. return nullptr;
  76. return adopt(*new Bitmap(format, size, pitch, data));
  77. }
  78. RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
  79. {
  80. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  81. if (path.ends_with(Ext)) \
  82. return load_##Name(path);
  83. ENUMERATE_IMAGE_FORMATS
  84. #undef __ENUMERATE_IMAGE_FORMAT
  85. return nullptr;
  86. }
  87. Bitmap::Bitmap(BitmapFormat format, const IntSize& 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. allocate_palette_from_format(format);
  95. }
  96. RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size)
  97. {
  98. if (size_would_overflow(format, size))
  99. return nullptr;
  100. return adopt(*new Bitmap(format, move(shared_buffer), size));
  101. }
  102. Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size)
  103. : m_size(size)
  104. , m_data((RGBA32*)shared_buffer->data())
  105. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  106. , m_format(format)
  107. , m_shared_buffer(move(shared_buffer))
  108. {
  109. ASSERT(!is_indexed(format));
  110. ASSERT(!size_would_overflow(format, size));
  111. }
  112. RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
  113. {
  114. auto w = this->width();
  115. auto h = this->height();
  116. auto new_bitmap = Gfx::Bitmap::create(this->format(), { h, w });
  117. if (!new_bitmap)
  118. return nullptr;
  119. for (int i = 0; i < w; i++) {
  120. for (int j = 0; j < h; j++) {
  121. Color color;
  122. if (rotation_direction == Gfx::RotationDirection::Left)
  123. color = this->get_pixel(w - i - 1, j);
  124. else
  125. color = this->get_pixel(i, h - j - 1);
  126. new_bitmap->set_pixel(j, i, color);
  127. }
  128. }
  129. return new_bitmap;
  130. }
  131. RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
  132. {
  133. auto w = this->width();
  134. auto h = this->height();
  135. auto new_bitmap = Gfx::Bitmap::create(this->format(), { w, h });
  136. if (!new_bitmap)
  137. return nullptr;
  138. for (int i = 0; i < w; i++) {
  139. for (int j = 0; j < h; j++) {
  140. Color color = this->get_pixel(i, j);
  141. if (orientation == Orientation::Vertical)
  142. new_bitmap->set_pixel(i, h - j - 1, color);
  143. else
  144. new_bitmap->set_pixel(w - i - 1, j, color);
  145. }
  146. }
  147. return new_bitmap;
  148. }
  149. RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
  150. {
  151. if (m_shared_buffer)
  152. return *this;
  153. auto buffer = SharedBuffer::create_with_size(size_in_bytes());
  154. auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size);
  155. if (!bitmap)
  156. return nullptr;
  157. memcpy(buffer->data(), scanline(0), size_in_bytes());
  158. return bitmap;
  159. }
  160. Bitmap::~Bitmap()
  161. {
  162. if (m_needs_munmap) {
  163. int rc = munmap(m_data, size_in_bytes());
  164. ASSERT(rc == 0);
  165. }
  166. m_data = nullptr;
  167. delete[] m_palette;
  168. }
  169. void Bitmap::set_mmap_name(const StringView& name)
  170. {
  171. ASSERT(m_needs_munmap);
  172. ::set_mmap_name(m_data, size_in_bytes(), name.to_string().characters());
  173. }
  174. void Bitmap::fill(Color color)
  175. {
  176. ASSERT(!is_indexed(m_format));
  177. for (int y = 0; y < height(); ++y) {
  178. auto* scanline = this->scanline(y);
  179. fast_u32_fill(scanline, color.value(), width());
  180. }
  181. }
  182. void Bitmap::set_volatile()
  183. {
  184. ASSERT(m_purgeable);
  185. if (m_volatile)
  186. return;
  187. int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
  188. if (rc < 0) {
  189. perror("madvise(MADV_SET_VOLATILE)");
  190. ASSERT_NOT_REACHED();
  191. }
  192. m_volatile = true;
  193. }
  194. [[nodiscard]] bool Bitmap::set_nonvolatile()
  195. {
  196. ASSERT(m_purgeable);
  197. if (!m_volatile)
  198. return true;
  199. int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
  200. if (rc < 0) {
  201. perror("madvise(MADV_SET_NONVOLATILE)");
  202. ASSERT_NOT_REACHED();
  203. }
  204. m_volatile = false;
  205. return rc == 0;
  206. }
  207. int Bitmap::shbuf_id() const
  208. {
  209. return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
  210. }
  211. ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
  212. {
  213. auto bitmap = to_bitmap_backed_by_shared_buffer();
  214. if (!bitmap)
  215. return {};
  216. if (peer_pid > 0)
  217. bitmap->shared_buffer()->share_with(peer_pid);
  218. return ShareableBitmap(*bitmap);
  219. }
  220. void Bitmap::allocate_palette_from_format(BitmapFormat format)
  221. {
  222. if (format == BitmapFormat::Indexed1) {
  223. m_palette = new RGBA32[2];
  224. } else if (format == BitmapFormat::Indexed2) {
  225. m_palette = new RGBA32[4];
  226. } else if (format == BitmapFormat::Indexed4) {
  227. m_palette = new RGBA32[16];
  228. } else if (format == BitmapFormat::Indexed8) {
  229. m_palette = new RGBA32[256];
  230. }
  231. }
  232. }