Bitmap.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/MappedFile.h>
  27. #include <LibGfx/Bitmap.h>
  28. #include <LibGfx/PNGLoader.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <sys/mman.h>
  33. #include <unistd.h>
  34. namespace Gfx {
  35. NonnullRefPtr<Bitmap> Bitmap::create(Format format, const Size& size)
  36. {
  37. return adopt(*new Bitmap(format, size, Purgeable::No));
  38. }
  39. NonnullRefPtr<Bitmap> Bitmap::create_purgeable(Format format, const Size& size)
  40. {
  41. return adopt(*new Bitmap(format, size, Purgeable::Yes));
  42. }
  43. Bitmap::Bitmap(Format format, const Size& size, Purgeable purgeable)
  44. : m_size(size)
  45. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  46. , m_format(format)
  47. , m_purgeable(purgeable == Purgeable::Yes)
  48. {
  49. if (format == Format::Indexed8)
  50. m_palette = new RGBA32[256];
  51. int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
  52. 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());
  53. ASSERT(m_data && m_data != (void*)-1);
  54. m_needs_munmap = true;
  55. }
  56. NonnullRefPtr<Bitmap> Bitmap::create_wrapper(Format format, const Size& size, size_t pitch, RGBA32* data)
  57. {
  58. return adopt(*new Bitmap(format, size, pitch, data));
  59. }
  60. RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
  61. {
  62. return load_png(path);
  63. }
  64. RefPtr<Bitmap> Bitmap::load_from_file(Format format, const StringView& path, const Size& size)
  65. {
  66. MappedFile mapped_file(path);
  67. if (!mapped_file.is_valid())
  68. return nullptr;
  69. return adopt(*new Bitmap(format, size, move(mapped_file)));
  70. }
  71. Bitmap::Bitmap(Format format, const Size& size, size_t pitch, RGBA32* data)
  72. : m_size(size)
  73. , m_data(data)
  74. , m_pitch(pitch)
  75. , m_format(format)
  76. {
  77. if (format == Format::Indexed8)
  78. m_palette = new RGBA32[256];
  79. }
  80. Bitmap::Bitmap(Format format, const Size& size, MappedFile&& mapped_file)
  81. : m_size(size)
  82. , m_data((RGBA32*)mapped_file.data())
  83. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  84. , m_format(format)
  85. , m_mapped_file(move(mapped_file))
  86. {
  87. ASSERT(format != Format::Indexed8);
  88. }
  89. NonnullRefPtr<Bitmap> Bitmap::create_with_shared_buffer(Format format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
  90. {
  91. return adopt(*new Bitmap(format, move(shared_buffer), size));
  92. }
  93. Bitmap::Bitmap(Format format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
  94. : m_size(size)
  95. , m_data((RGBA32*)shared_buffer->data())
  96. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  97. , m_format(format)
  98. , m_shared_buffer(move(shared_buffer))
  99. {
  100. ASSERT(format != Format::Indexed8);
  101. }
  102. NonnullRefPtr<Bitmap> Bitmap::to_shareable_bitmap() const
  103. {
  104. if (m_shared_buffer)
  105. return *this;
  106. auto buffer = SharedBuffer::create_with_size(size_in_bytes());
  107. auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size);
  108. memcpy(buffer->data(), scanline(0), size_in_bytes());
  109. return bitmap;
  110. }
  111. Bitmap::~Bitmap()
  112. {
  113. if (m_needs_munmap) {
  114. int rc = munmap(m_data, size_in_bytes());
  115. ASSERT(rc == 0);
  116. }
  117. m_data = nullptr;
  118. delete[] m_palette;
  119. }
  120. void Bitmap::set_mmap_name(const StringView& name)
  121. {
  122. ASSERT(m_needs_munmap);
  123. ::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
  124. }
  125. void Bitmap::fill(Color color)
  126. {
  127. ASSERT(m_format == Bitmap::Format::RGB32 || m_format == Bitmap::Format::RGBA32);
  128. for (int y = 0; y < height(); ++y) {
  129. auto* scanline = this->scanline(y);
  130. fast_u32_fill(scanline, color.value(), width());
  131. }
  132. }
  133. void Bitmap::set_volatile()
  134. {
  135. ASSERT(m_purgeable);
  136. if (m_volatile)
  137. return;
  138. int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
  139. if (rc < 0) {
  140. perror("madvise(MADV_SET_VOLATILE)");
  141. ASSERT_NOT_REACHED();
  142. }
  143. m_volatile = true;
  144. }
  145. [[nodiscard]] bool Bitmap::set_nonvolatile()
  146. {
  147. ASSERT(m_purgeable);
  148. if (!m_volatile)
  149. return true;
  150. int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
  151. if (rc < 0) {
  152. perror("madvise(MADV_SET_NONVOLATILE)");
  153. ASSERT_NOT_REACHED();
  154. }
  155. m_volatile = false;
  156. return rc == 0;
  157. }
  158. }