Bitmap.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Checked.h>
  7. #include <AK/LexicalPath.h>
  8. #include <AK/Memory.h>
  9. #include <AK/MemoryStream.h>
  10. #include <AK/Optional.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <AK/String.h>
  13. #include <LibGfx/BMPLoader.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/DDSLoader.h>
  16. #include <LibGfx/GIFLoader.h>
  17. #include <LibGfx/ICOLoader.h>
  18. #include <LibGfx/JPGLoader.h>
  19. #include <LibGfx/PBMLoader.h>
  20. #include <LibGfx/PGMLoader.h>
  21. #include <LibGfx/PNGLoader.h>
  22. #include <LibGfx/PPMLoader.h>
  23. #include <LibGfx/ShareableBitmap.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <sys/mman.h>
  27. #ifdef __serenity__
  28. # include <serenity.h>
  29. #endif
  30. namespace Gfx {
  31. struct BackingStore {
  32. void* data { nullptr };
  33. size_t pitch { 0 };
  34. size_t size_in_bytes { 0 };
  35. };
  36. size_t Bitmap::minimum_pitch(size_t physical_width, BitmapFormat format)
  37. {
  38. size_t element_size;
  39. switch (determine_storage_format(format)) {
  40. case StorageFormat::Indexed8:
  41. element_size = 1;
  42. break;
  43. case StorageFormat::BGRx8888:
  44. case StorageFormat::BGRA8888:
  45. case StorageFormat::RGBA8888:
  46. element_size = 4;
  47. break;
  48. default:
  49. VERIFY_NOT_REACHED();
  50. }
  51. return physical_width * element_size;
  52. }
  53. static bool size_would_overflow(BitmapFormat format, const IntSize& size, int scale_factor)
  54. {
  55. if (size.width() < 0 || size.height() < 0)
  56. return true;
  57. // This check is a bit arbitrary, but should protect us from most shenanigans:
  58. if (size.width() >= 32768 || size.height() >= 32768 || scale_factor < 1 || scale_factor > 4)
  59. return true;
  60. // In contrast, this check is absolutely necessary:
  61. size_t pitch = Bitmap::minimum_pitch(size.width() * scale_factor, format);
  62. return Checked<size_t>::multiplication_would_overflow(pitch, size.height() * scale_factor);
  63. }
  64. RefPtr<Bitmap> Bitmap::create(BitmapFormat format, const IntSize& size, int scale_factor)
  65. {
  66. auto backing_store = Bitmap::allocate_backing_store(format, size, scale_factor, Purgeable::No);
  67. if (!backing_store.has_value())
  68. return nullptr;
  69. return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::No, backing_store.value()));
  70. }
  71. RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const IntSize& size, int scale_factor)
  72. {
  73. auto backing_store = Bitmap::allocate_backing_store(format, size, scale_factor, Purgeable::Yes);
  74. if (!backing_store.has_value())
  75. return nullptr;
  76. return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::Yes, backing_store.value()));
  77. }
  78. #ifdef __serenity__
  79. RefPtr<Bitmap> Bitmap::create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
  80. {
  81. if (size_would_overflow(format, size, scale_factor))
  82. return nullptr;
  83. const auto pitch = minimum_pitch(size.width() * scale_factor, format);
  84. const auto data_size = size_in_bytes(pitch, size.height() * scale_factor);
  85. auto anon_fd = anon_create(round_up_to_power_of_two(data_size, PAGE_SIZE), O_CLOEXEC);
  86. if (anon_fd < 0)
  87. return nullptr;
  88. return Bitmap::create_with_anon_fd(format, anon_fd, size, scale_factor, {}, ShouldCloseAnonymousFile::No);
  89. }
  90. #endif
  91. Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, Purgeable purgeable, const BackingStore& backing_store)
  92. : m_size(size)
  93. , m_scale(scale_factor)
  94. , m_data(backing_store.data)
  95. , m_pitch(backing_store.pitch)
  96. , m_format(format)
  97. , m_purgeable(purgeable == Purgeable::Yes)
  98. {
  99. VERIFY(!m_size.is_empty());
  100. VERIFY(!size_would_overflow(format, size, scale_factor));
  101. VERIFY(m_data);
  102. VERIFY(backing_store.size_in_bytes == size_in_bytes());
  103. allocate_palette_from_format(format, {});
  104. m_needs_munmap = true;
  105. }
  106. RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data)
  107. {
  108. if (size_would_overflow(format, size, scale_factor))
  109. return nullptr;
  110. return adopt_ref(*new Bitmap(format, size, scale_factor, pitch, data));
  111. }
  112. RefPtr<Bitmap> Bitmap::load_from_file(String const& path, int scale_factor)
  113. {
  114. if (scale_factor > 1 && path.starts_with("/res/")) {
  115. LexicalPath lexical_path { path };
  116. StringBuilder highdpi_icon_path;
  117. highdpi_icon_path.append(lexical_path.dirname());
  118. highdpi_icon_path.append('/');
  119. highdpi_icon_path.append(lexical_path.title());
  120. highdpi_icon_path.appendff("-{}x.", scale_factor);
  121. highdpi_icon_path.append(lexical_path.extension());
  122. RefPtr<Bitmap> bmp;
  123. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  124. if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) \
  125. bmp = load_##Name(highdpi_icon_path.to_string());
  126. ENUMERATE_IMAGE_FORMATS
  127. #undef __ENUMERATE_IMAGE_FORMAT
  128. if (bmp) {
  129. VERIFY(bmp->width() % scale_factor == 0);
  130. VERIFY(bmp->height() % scale_factor == 0);
  131. bmp->m_size.set_width(bmp->width() / scale_factor);
  132. bmp->m_size.set_height(bmp->height() / scale_factor);
  133. bmp->m_scale = scale_factor;
  134. return bmp;
  135. }
  136. }
  137. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  138. if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) \
  139. return load_##Name(path);
  140. ENUMERATE_IMAGE_FORMATS
  141. #undef __ENUMERATE_IMAGE_FORMAT
  142. return nullptr;
  143. }
  144. Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data)
  145. : m_size(size)
  146. , m_scale(scale_factor)
  147. , m_data(data)
  148. , m_pitch(pitch)
  149. , m_format(format)
  150. {
  151. VERIFY(pitch >= minimum_pitch(size.width() * scale_factor, format));
  152. VERIFY(!size_would_overflow(format, size, scale_factor));
  153. // FIXME: assert that `data` is actually long enough!
  154. allocate_palette_from_format(format, {});
  155. }
  156. static bool check_size(const IntSize& size, int scale_factor, BitmapFormat format, unsigned actual_size)
  157. {
  158. // FIXME: Code duplication of size_in_bytes() and m_pitch
  159. unsigned expected_size_min = Bitmap::minimum_pitch(size.width() * scale_factor, format) * size.height() * scale_factor;
  160. unsigned expected_size_max = round_up_to_power_of_two(expected_size_min, PAGE_SIZE);
  161. if (expected_size_min > actual_size || actual_size > expected_size_max) {
  162. // Getting here is most likely an error.
  163. dbgln("Constructing a shared bitmap for format {} and size {} @ {}x, which demands {} bytes, which rounds up to at most {}.",
  164. static_cast<int>(format),
  165. size,
  166. scale_factor,
  167. expected_size_min,
  168. expected_size_max);
  169. dbgln("However, we were given {} bytes, which is outside this range?! Refusing cowardly.", actual_size);
  170. return false;
  171. }
  172. return true;
  173. }
  174. RefPtr<Bitmap> Bitmap::create_with_anon_fd(BitmapFormat format, int anon_fd, const IntSize& size, int scale_factor, const Vector<RGBA32>& palette, ShouldCloseAnonymousFile should_close_anon_fd)
  175. {
  176. void* data = nullptr;
  177. {
  178. // If ShouldCloseAnonymousFile::Yes, it's our responsibility to close 'anon_fd' no matter what.
  179. ScopeGuard close_guard = [&] {
  180. if (should_close_anon_fd == ShouldCloseAnonymousFile::Yes) {
  181. int rc = close(anon_fd);
  182. VERIFY(rc == 0);
  183. anon_fd = -1;
  184. }
  185. };
  186. if (size_would_overflow(format, size, scale_factor))
  187. return nullptr;
  188. const auto pitch = minimum_pitch(size.width() * scale_factor, format);
  189. const auto data_size_in_bytes = size_in_bytes(pitch, size.height() * scale_factor);
  190. data = mmap(nullptr, round_up_to_power_of_two(data_size_in_bytes, PAGE_SIZE), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, anon_fd, 0);
  191. if (data == MAP_FAILED) {
  192. perror("mmap");
  193. return nullptr;
  194. }
  195. }
  196. return adopt_ref(*new Bitmap(format, anon_fd, size, scale_factor, data, palette));
  197. }
  198. /// Read a bitmap as described by:
  199. /// - actual size
  200. /// - width
  201. /// - height
  202. /// - scale_factor
  203. /// - format
  204. /// - palette count
  205. /// - palette data (= palette count * BGRA8888)
  206. /// - image data (= actual size * u8)
  207. RefPtr<Bitmap> Bitmap::create_from_serialized_byte_buffer(ByteBuffer&& buffer)
  208. {
  209. InputMemoryStream stream { buffer };
  210. unsigned actual_size;
  211. unsigned width;
  212. unsigned height;
  213. unsigned scale_factor;
  214. BitmapFormat format;
  215. unsigned palette_size;
  216. Vector<RGBA32> palette;
  217. auto read = [&]<typename T>(T& value) {
  218. if (stream.read({ &value, sizeof(T) }) != sizeof(T))
  219. return false;
  220. return true;
  221. };
  222. if (!read(actual_size) || !read(width) || !read(height) || !read(scale_factor) || !read(format) || !read(palette_size))
  223. return nullptr;
  224. if (format > BitmapFormat::BGRA8888 || format < BitmapFormat::Indexed1)
  225. return nullptr;
  226. if (!check_size({ width, height }, scale_factor, format, actual_size))
  227. return {};
  228. palette.ensure_capacity(palette_size);
  229. for (size_t i = 0; i < palette_size; ++i) {
  230. if (!read(palette[i]))
  231. return {};
  232. }
  233. if (stream.remaining() < actual_size)
  234. return {};
  235. auto data = stream.bytes().slice(stream.offset(), actual_size);
  236. auto bitmap = Bitmap::create(format, { width, height }, scale_factor);
  237. if (!bitmap)
  238. return {};
  239. bitmap->m_palette = new RGBA32[palette_size];
  240. memcpy(bitmap->m_palette, palette.data(), palette_size * sizeof(RGBA32));
  241. data.copy_to({ bitmap->scanline(0), bitmap->size_in_bytes() });
  242. return bitmap;
  243. }
  244. ByteBuffer Bitmap::serialize_to_byte_buffer() const
  245. {
  246. auto buffer = ByteBuffer::create_uninitialized(5 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(RGBA32) * palette_size(m_format) + size_in_bytes());
  247. OutputMemoryStream stream { buffer };
  248. auto write = [&]<typename T>(T value) {
  249. if (stream.write({ &value, sizeof(T) }) != sizeof(T))
  250. return false;
  251. return true;
  252. };
  253. auto palette = palette_to_vector();
  254. if (!write(size_in_bytes()) || !write((unsigned)size().width()) || !write((unsigned)size().height()) || !write((unsigned)scale()) || !write(m_format) || !write((unsigned)palette.size()))
  255. return {};
  256. for (auto& p : palette) {
  257. if (!write(p))
  258. return {};
  259. }
  260. auto size = size_in_bytes();
  261. VERIFY(stream.remaining() == size);
  262. if (stream.write({ scanline(0), size }) != size)
  263. return {};
  264. return buffer;
  265. }
  266. Bitmap::Bitmap(BitmapFormat format, int anon_fd, const IntSize& size, int scale_factor, void* data, const Vector<RGBA32>& palette)
  267. : m_size(size)
  268. , m_scale(scale_factor)
  269. , m_data(data)
  270. , m_pitch(minimum_pitch(size.width() * scale_factor, format))
  271. , m_format(format)
  272. , m_needs_munmap(true)
  273. , m_purgeable(true)
  274. , m_anon_fd(anon_fd)
  275. {
  276. VERIFY(!is_indexed() || !palette.is_empty());
  277. VERIFY(!size_would_overflow(format, size, scale_factor));
  278. if (is_indexed(m_format))
  279. allocate_palette_from_format(m_format, palette);
  280. }
  281. RefPtr<Gfx::Bitmap> Bitmap::clone() const
  282. {
  283. RefPtr<Gfx::Bitmap> new_bitmap {};
  284. if (m_purgeable) {
  285. new_bitmap = Bitmap::create_purgeable(format(), size(), scale());
  286. } else {
  287. new_bitmap = Bitmap::create(format(), size(), scale());
  288. }
  289. if (!new_bitmap) {
  290. return nullptr;
  291. }
  292. VERIFY(size_in_bytes() == new_bitmap->size_in_bytes());
  293. memcpy(new_bitmap->scanline(0), scanline(0), size_in_bytes());
  294. return new_bitmap;
  295. }
  296. RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
  297. {
  298. auto new_bitmap = Gfx::Bitmap::create(this->format(), { height(), width() }, scale());
  299. if (!new_bitmap)
  300. return nullptr;
  301. auto w = this->physical_width();
  302. auto h = this->physical_height();
  303. for (int i = 0; i < w; i++) {
  304. for (int j = 0; j < h; j++) {
  305. Color color;
  306. if (rotation_direction == Gfx::RotationDirection::Left)
  307. color = this->get_pixel(w - i - 1, j);
  308. else
  309. color = this->get_pixel(i, h - j - 1);
  310. new_bitmap->set_pixel(j, i, color);
  311. }
  312. }
  313. return new_bitmap;
  314. }
  315. RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
  316. {
  317. auto new_bitmap = Gfx::Bitmap::create(this->format(), { width(), height() }, scale());
  318. if (!new_bitmap)
  319. return nullptr;
  320. auto w = this->physical_width();
  321. auto h = this->physical_height();
  322. for (int i = 0; i < w; i++) {
  323. for (int j = 0; j < h; j++) {
  324. Color color = this->get_pixel(i, j);
  325. if (orientation == Orientation::Vertical)
  326. new_bitmap->set_pixel(i, h - j - 1, color);
  327. else
  328. new_bitmap->set_pixel(w - i - 1, j, color);
  329. }
  330. }
  331. return new_bitmap;
  332. }
  333. RefPtr<Gfx::Bitmap> Bitmap::scaled(int sx, int sy) const
  334. {
  335. VERIFY(sx >= 0 && sy >= 0);
  336. if (sx == 1 && sy == 1)
  337. return this;
  338. auto new_bitmap = Gfx::Bitmap::create(format(), { width() * sx, height() * sy }, scale());
  339. if (!new_bitmap)
  340. return nullptr;
  341. auto old_width = physical_width();
  342. auto old_height = physical_height();
  343. for (int y = 0; y < old_height; y++) {
  344. for (int x = 0; x < old_width; x++) {
  345. auto color = get_pixel(x, y);
  346. auto base_x = x * sx;
  347. auto base_y = y * sy;
  348. for (int new_y = base_y; new_y < base_y + sy; new_y++) {
  349. for (int new_x = base_x; new_x < base_x + sx; new_x++) {
  350. new_bitmap->set_pixel(new_x, new_y, color);
  351. }
  352. }
  353. }
  354. }
  355. return new_bitmap;
  356. }
  357. // http://fourier.eng.hmc.edu/e161/lectures/resize/node3.html
  358. RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
  359. {
  360. VERIFY(sx >= 0.0f && sy >= 0.0f);
  361. if (floorf(sx) == sx && floorf(sy) == sy)
  362. return scaled(static_cast<int>(sx), static_cast<int>(sy));
  363. auto new_bitmap = Gfx::Bitmap::create(format(), { width() * sx, height() * sy }, scale());
  364. if (!new_bitmap)
  365. return nullptr;
  366. auto old_width = physical_width();
  367. auto old_height = physical_height();
  368. auto new_width = new_bitmap->physical_width();
  369. auto new_height = new_bitmap->physical_height();
  370. // The interpolation goes out of bounds on the bottom- and right-most edges.
  371. // We handle those in two specialized loops not only to make them faster, but
  372. // also to avoid four branch checks for every pixel.
  373. for (int y = 0; y < new_height - 1; y++) {
  374. for (int x = 0; x < new_width - 1; x++) {
  375. auto p = static_cast<float>(x) * static_cast<float>(old_width - 1) / static_cast<float>(new_width - 1);
  376. auto q = static_cast<float>(y) * static_cast<float>(old_height - 1) / static_cast<float>(new_height - 1);
  377. int i = floor(p);
  378. int j = floor(q);
  379. float u = p - static_cast<float>(i);
  380. float v = q - static_cast<float>(j);
  381. auto a = get_pixel(i, j);
  382. auto b = get_pixel(i + 1, j);
  383. auto c = get_pixel(i, j + 1);
  384. auto d = get_pixel(i + 1, j + 1);
  385. auto e = a.interpolate(b, u);
  386. auto f = c.interpolate(d, u);
  387. auto color = e.interpolate(f, v);
  388. new_bitmap->set_pixel(x, y, color);
  389. }
  390. }
  391. // Bottom strip (excluding last pixel)
  392. auto old_bottom_y = old_height - 1;
  393. auto new_bottom_y = new_height - 1;
  394. for (int x = 0; x < new_width - 1; x++) {
  395. auto p = static_cast<float>(x) * static_cast<float>(old_width - 1) / static_cast<float>(new_width - 1);
  396. int i = floor(p);
  397. float u = p - static_cast<float>(i);
  398. auto a = get_pixel(i, old_bottom_y);
  399. auto b = get_pixel(i + 1, old_bottom_y);
  400. auto color = a.interpolate(b, u);
  401. new_bitmap->set_pixel(x, new_bottom_y, color);
  402. }
  403. // Right strip (excluding last pixel)
  404. auto old_right_x = old_width - 1;
  405. auto new_right_x = new_width - 1;
  406. for (int y = 0; y < new_height - 1; y++) {
  407. auto q = static_cast<float>(y) * static_cast<float>(old_height - 1) / static_cast<float>(new_height - 1);
  408. int j = floor(q);
  409. float v = q - static_cast<float>(j);
  410. auto c = get_pixel(old_right_x, j);
  411. auto d = get_pixel(old_right_x, j + 1);
  412. auto color = c.interpolate(d, v);
  413. new_bitmap->set_pixel(new_right_x, y, color);
  414. }
  415. // Bottom-right pixel
  416. new_bitmap->set_pixel(new_width - 1, new_height - 1, get_pixel(physical_width() - 1, physical_height() - 1));
  417. return new_bitmap;
  418. }
  419. RefPtr<Gfx::Bitmap> Bitmap::cropped(Gfx::IntRect crop) const
  420. {
  421. auto new_bitmap = Gfx::Bitmap::create(format(), { crop.width(), crop.height() }, 1);
  422. if (!new_bitmap)
  423. return nullptr;
  424. for (int y = 0; y < crop.height(); ++y) {
  425. for (int x = 0; x < crop.width(); ++x) {
  426. int global_x = x + crop.left();
  427. int global_y = y + crop.top();
  428. if (global_x >= physical_width() || global_y >= physical_height() || global_x < 0 || global_y < 0) {
  429. new_bitmap->set_pixel(x, y, Gfx::Color::Black);
  430. } else {
  431. new_bitmap->set_pixel(x, y, get_pixel(global_x, global_y));
  432. }
  433. }
  434. }
  435. return new_bitmap;
  436. }
  437. #ifdef __serenity__
  438. RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anon_fd() const
  439. {
  440. if (m_anon_fd != -1)
  441. return *this;
  442. auto anon_fd = anon_create(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE), O_CLOEXEC);
  443. if (anon_fd < 0)
  444. return nullptr;
  445. auto bitmap = Bitmap::create_with_anon_fd(m_format, anon_fd, size(), scale(), palette_to_vector(), ShouldCloseAnonymousFile::No);
  446. if (!bitmap)
  447. return nullptr;
  448. memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
  449. return bitmap;
  450. }
  451. #endif
  452. Bitmap::~Bitmap()
  453. {
  454. if (m_needs_munmap) {
  455. int rc = munmap(m_data, size_in_bytes());
  456. VERIFY(rc == 0);
  457. }
  458. if (m_anon_fd != -1) {
  459. int rc = close(m_anon_fd);
  460. VERIFY(rc == 0);
  461. }
  462. m_data = nullptr;
  463. delete[] m_palette;
  464. }
  465. void Bitmap::set_mmap_name([[maybe_unused]] String const& name)
  466. {
  467. VERIFY(m_needs_munmap);
  468. #ifdef __serenity__
  469. ::set_mmap_name(m_data, size_in_bytes(), name.characters());
  470. #endif
  471. }
  472. void Bitmap::fill(Color color)
  473. {
  474. VERIFY(!is_indexed(m_format));
  475. for (int y = 0; y < physical_height(); ++y) {
  476. auto* scanline = this->scanline(y);
  477. fast_u32_fill(scanline, color.value(), physical_width());
  478. }
  479. }
  480. void Bitmap::set_volatile()
  481. {
  482. VERIFY(m_purgeable);
  483. if (m_volatile)
  484. return;
  485. #ifdef __serenity__
  486. int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
  487. if (rc < 0) {
  488. perror("madvise(MADV_SET_VOLATILE)");
  489. VERIFY_NOT_REACHED();
  490. }
  491. #endif
  492. m_volatile = true;
  493. }
  494. [[nodiscard]] bool Bitmap::set_nonvolatile()
  495. {
  496. VERIFY(m_purgeable);
  497. if (!m_volatile)
  498. return true;
  499. #ifdef __serenity__
  500. int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
  501. if (rc < 0) {
  502. perror("madvise(MADV_SET_NONVOLATILE)");
  503. VERIFY_NOT_REACHED();
  504. }
  505. #else
  506. int rc = 0;
  507. #endif
  508. m_volatile = false;
  509. return rc == 0;
  510. }
  511. #ifdef __serenity__
  512. ShareableBitmap Bitmap::to_shareable_bitmap() const
  513. {
  514. auto bitmap = to_bitmap_backed_by_anon_fd();
  515. if (!bitmap)
  516. return {};
  517. return ShareableBitmap(*bitmap);
  518. }
  519. #endif
  520. Optional<BackingStore> Bitmap::allocate_backing_store(BitmapFormat format, const IntSize& size, int scale_factor, [[maybe_unused]] Purgeable purgeable)
  521. {
  522. if (size_would_overflow(format, size, scale_factor))
  523. return {};
  524. const auto pitch = minimum_pitch(size.width() * scale_factor, format);
  525. const auto data_size_in_bytes = size_in_bytes(pitch, size.height() * scale_factor);
  526. int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
  527. if (purgeable == Purgeable::Yes)
  528. map_flags |= MAP_NORESERVE;
  529. #ifdef __serenity__
  530. void* data = mmap_with_name(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0, String::formatted("GraphicsBitmap [{}]", size).characters());
  531. #else
  532. void* data = mmap(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0);
  533. #endif
  534. if (data == MAP_FAILED) {
  535. perror("mmap");
  536. return {};
  537. }
  538. return { { data, pitch, data_size_in_bytes } };
  539. }
  540. void Bitmap::allocate_palette_from_format(BitmapFormat format, const Vector<RGBA32>& source_palette)
  541. {
  542. size_t size = palette_size(format);
  543. if (size == 0)
  544. return;
  545. m_palette = new RGBA32[size];
  546. if (!source_palette.is_empty()) {
  547. VERIFY(source_palette.size() == size);
  548. memcpy(m_palette, source_palette.data(), size * sizeof(RGBA32));
  549. }
  550. }
  551. Vector<RGBA32> Bitmap::palette_to_vector() const
  552. {
  553. Vector<RGBA32> vector;
  554. auto size = palette_size(m_format);
  555. vector.ensure_capacity(size);
  556. for (size_t i = 0; i < size; ++i)
  557. vector.unchecked_append(palette_color(i).value());
  558. return vector;
  559. }
  560. }