Bitmap.cpp 5.2 KB

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