Bitmap.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/BMPLoader.h>
  31. #include <LibGfx/Bitmap.h>
  32. #include <LibGfx/GIFLoader.h>
  33. #include <LibGfx/ICOLoader.h>
  34. #include <LibGfx/JPGLoader.h>
  35. #include <LibGfx/PBMLoader.h>
  36. #include <LibGfx/PGMLoader.h>
  37. #include <LibGfx/PNGLoader.h>
  38. #include <LibGfx/PPMLoader.h>
  39. #include <LibGfx/ShareableBitmap.h>
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <sys/mman.h>
  43. namespace Gfx {
  44. static bool size_would_overflow(BitmapFormat format, const IntSize& size)
  45. {
  46. if (size.width() < 0 || size.height() < 0)
  47. return true;
  48. return Checked<size_t>::multiplication_would_overflow(size.width(), size.height(), Bitmap::bpp_for_format(format));
  49. }
  50. RefPtr<Bitmap> Bitmap::create(BitmapFormat format, const IntSize& size)
  51. {
  52. if (size_would_overflow(format, size))
  53. return nullptr;
  54. return adopt(*new Bitmap(format, size, Purgeable::No));
  55. }
  56. RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const IntSize& size)
  57. {
  58. if (size_would_overflow(format, size))
  59. return nullptr;
  60. return adopt(*new Bitmap(format, size, Purgeable::Yes));
  61. }
  62. Bitmap::Bitmap(BitmapFormat format, const IntSize& size, Purgeable purgeable)
  63. : m_size(size)
  64. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  65. , m_format(format)
  66. , m_purgeable(purgeable == Purgeable::Yes)
  67. {
  68. ASSERT(!m_size.is_empty());
  69. ASSERT(!size_would_overflow(format, size));
  70. allocate_palette_from_format(format, {});
  71. #ifdef __serenity__
  72. int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
  73. 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());
  74. #else
  75. int map_flags = (MAP_ANONYMOUS | MAP_PRIVATE);
  76. m_data = (RGBA32*)mmap(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, map_flags, 0, 0);
  77. #endif
  78. ASSERT(m_data && m_data != (void*)-1);
  79. m_needs_munmap = true;
  80. }
  81. RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const IntSize& size, size_t pitch, RGBA32* data)
  82. {
  83. if (size_would_overflow(format, size))
  84. return nullptr;
  85. return adopt(*new Bitmap(format, size, pitch, data));
  86. }
  87. RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
  88. {
  89. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  90. if (path.ends_with(Ext)) \
  91. return load_##Name(path);
  92. ENUMERATE_IMAGE_FORMATS
  93. #undef __ENUMERATE_IMAGE_FORMAT
  94. return nullptr;
  95. }
  96. Bitmap::Bitmap(BitmapFormat format, const IntSize& size, size_t pitch, RGBA32* data)
  97. : m_size(size)
  98. , m_data(data)
  99. , m_pitch(pitch)
  100. , m_format(format)
  101. {
  102. ASSERT(!size_would_overflow(format, size));
  103. allocate_palette_from_format(format, {});
  104. }
  105. RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size)
  106. {
  107. if (size_would_overflow(format, size))
  108. return nullptr;
  109. return adopt(*new Bitmap(format, move(shared_buffer), size, {}));
  110. }
  111. RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size, const Vector<RGBA32>& palette)
  112. {
  113. if (size_would_overflow(format, size))
  114. return nullptr;
  115. return adopt(*new Bitmap(format, move(shared_buffer), size, palette));
  116. }
  117. Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size, const Vector<RGBA32>& palette)
  118. : m_size(size)
  119. , m_data((RGBA32*)shared_buffer->data())
  120. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  121. , m_format(format)
  122. , m_shared_buffer(move(shared_buffer))
  123. {
  124. ASSERT(!is_indexed() || !palette.is_empty());
  125. ASSERT(!size_would_overflow(format, size));
  126. if (is_indexed(m_format))
  127. allocate_palette_from_format(m_format, palette);
  128. }
  129. RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
  130. {
  131. auto w = this->width();
  132. auto h = this->height();
  133. auto new_bitmap = Gfx::Bitmap::create(this->format(), { h, w });
  134. if (!new_bitmap)
  135. return nullptr;
  136. for (int i = 0; i < w; i++) {
  137. for (int j = 0; j < h; j++) {
  138. Color color;
  139. if (rotation_direction == Gfx::RotationDirection::Left)
  140. color = this->get_pixel(w - i - 1, j);
  141. else
  142. color = this->get_pixel(i, h - j - 1);
  143. new_bitmap->set_pixel(j, i, color);
  144. }
  145. }
  146. return new_bitmap;
  147. }
  148. RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
  149. {
  150. auto w = this->width();
  151. auto h = this->height();
  152. auto new_bitmap = Gfx::Bitmap::create(this->format(), { w, h });
  153. if (!new_bitmap)
  154. return nullptr;
  155. for (int i = 0; i < w; i++) {
  156. for (int j = 0; j < h; j++) {
  157. Color color = this->get_pixel(i, j);
  158. if (orientation == Orientation::Vertical)
  159. new_bitmap->set_pixel(i, h - j - 1, color);
  160. else
  161. new_bitmap->set_pixel(w - i - 1, j, color);
  162. }
  163. }
  164. return new_bitmap;
  165. }
  166. RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
  167. {
  168. if (m_shared_buffer)
  169. return *this;
  170. auto buffer = SharedBuffer::create_with_size(size_in_bytes());
  171. auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size, palette_to_vector());
  172. if (!bitmap)
  173. return nullptr;
  174. memcpy(buffer->data(), scanline(0), size_in_bytes());
  175. return bitmap;
  176. }
  177. Bitmap::~Bitmap()
  178. {
  179. if (m_needs_munmap) {
  180. int rc = munmap(m_data, size_in_bytes());
  181. ASSERT(rc == 0);
  182. }
  183. m_data = nullptr;
  184. delete[] m_palette;
  185. }
  186. void Bitmap::set_mmap_name(const StringView& name)
  187. {
  188. ASSERT(m_needs_munmap);
  189. #ifdef __serenity__
  190. ::set_mmap_name(m_data, size_in_bytes(), name.to_string().characters());
  191. #else
  192. (void)name;
  193. #endif
  194. }
  195. void Bitmap::fill(Color color)
  196. {
  197. ASSERT(!is_indexed(m_format));
  198. for (int y = 0; y < height(); ++y) {
  199. auto* scanline = this->scanline(y);
  200. fast_u32_fill(scanline, color.value(), width());
  201. }
  202. }
  203. void Bitmap::set_volatile()
  204. {
  205. ASSERT(m_purgeable);
  206. if (m_volatile)
  207. return;
  208. #ifdef __serenity__
  209. int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
  210. if (rc < 0) {
  211. perror("madvise(MADV_SET_VOLATILE)");
  212. ASSERT_NOT_REACHED();
  213. }
  214. #endif
  215. m_volatile = true;
  216. }
  217. [[nodiscard]] bool Bitmap::set_nonvolatile()
  218. {
  219. ASSERT(m_purgeable);
  220. if (!m_volatile)
  221. return true;
  222. #ifdef __serenity__
  223. int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
  224. if (rc < 0) {
  225. perror("madvise(MADV_SET_NONVOLATILE)");
  226. ASSERT_NOT_REACHED();
  227. }
  228. #else
  229. int rc = 0;
  230. #endif
  231. m_volatile = false;
  232. return rc == 0;
  233. }
  234. int Bitmap::shbuf_id() const
  235. {
  236. return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
  237. }
  238. ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
  239. {
  240. auto bitmap = to_bitmap_backed_by_shared_buffer();
  241. if (!bitmap)
  242. return {};
  243. if (peer_pid > 0)
  244. bitmap->shared_buffer()->share_with(peer_pid);
  245. return ShareableBitmap(*bitmap);
  246. }
  247. void Bitmap::allocate_palette_from_format(BitmapFormat format, const Vector<RGBA32>& source_palette)
  248. {
  249. size_t size = palette_size(format);
  250. if (size == 0)
  251. return;
  252. m_palette = new RGBA32[size];
  253. if (!source_palette.is_empty()) {
  254. ASSERT(source_palette.size() == size);
  255. memcpy(m_palette, source_palette.data(), size * sizeof(RGBA32));
  256. }
  257. }
  258. Vector<RGBA32> Bitmap::palette_to_vector() const
  259. {
  260. Vector<RGBA32> vector;
  261. auto size = palette_size(m_format);
  262. vector.ensure_capacity(size);
  263. for (size_t i = 0; i < size; ++i)
  264. vector.unchecked_append(palette_color(i).value());
  265. return vector;
  266. }
  267. }