GraphicsBitmap.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <AK/MappedFile.h>
  2. #include <LibDraw/GraphicsBitmap.h>
  3. #include <LibDraw/PNGLoader.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <sys/mman.h>
  8. #include <unistd.h>
  9. NonnullRefPtr<GraphicsBitmap> GraphicsBitmap::create(Format format, const Size& size)
  10. {
  11. return adopt(*new GraphicsBitmap(format, size));
  12. }
  13. GraphicsBitmap::GraphicsBitmap(Format format, const Size& size)
  14. : m_size(size)
  15. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  16. , m_format(format)
  17. {
  18. if (format == Format::Indexed8)
  19. m_palette = new RGBA32[256];
  20. m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
  21. ASSERT(m_data && m_data != (void*)-1);
  22. m_needs_munmap = true;
  23. }
  24. NonnullRefPtr<GraphicsBitmap> GraphicsBitmap::create_wrapper(Format format, const Size& size, size_t pitch, RGBA32* data)
  25. {
  26. return adopt(*new GraphicsBitmap(format, size, pitch, data));
  27. }
  28. RefPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(const StringView& path)
  29. {
  30. return load_png(path);
  31. }
  32. RefPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(Format format, const StringView& path, const Size& size)
  33. {
  34. MappedFile mapped_file(path);
  35. if (!mapped_file.is_valid())
  36. return nullptr;
  37. return adopt(*new GraphicsBitmap(format, size, move(mapped_file)));
  38. }
  39. GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, size_t pitch, RGBA32* data)
  40. : m_size(size)
  41. , m_data(data)
  42. , m_pitch(pitch)
  43. , m_format(format)
  44. {
  45. if (format == Format::Indexed8)
  46. m_palette = new RGBA32[256];
  47. }
  48. GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, MappedFile&& mapped_file)
  49. : m_size(size)
  50. , m_data((RGBA32*)mapped_file.data())
  51. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  52. , m_format(format)
  53. , m_mapped_file(move(mapped_file))
  54. {
  55. ASSERT(format != Format::Indexed8);
  56. }
  57. NonnullRefPtr<GraphicsBitmap> GraphicsBitmap::create_with_shared_buffer(Format format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
  58. {
  59. return adopt(*new GraphicsBitmap(format, move(shared_buffer), size));
  60. }
  61. GraphicsBitmap::GraphicsBitmap(Format format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
  62. : m_size(size)
  63. , m_data((RGBA32*)shared_buffer->data())
  64. , m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
  65. , m_format(format)
  66. , m_shared_buffer(move(shared_buffer))
  67. {
  68. ASSERT(format != Format::Indexed8);
  69. }
  70. GraphicsBitmap::~GraphicsBitmap()
  71. {
  72. if (m_needs_munmap) {
  73. int rc = munmap(m_data, size_in_bytes());
  74. ASSERT(rc == 0);
  75. }
  76. m_data = nullptr;
  77. delete[] m_palette;
  78. }
  79. void GraphicsBitmap::set_mmap_name(const StringView& name)
  80. {
  81. ASSERT(m_needs_munmap);
  82. ::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
  83. }
  84. void GraphicsBitmap::fill(Color color)
  85. {
  86. ASSERT(m_format == GraphicsBitmap::Format::RGB32 || m_format == GraphicsBitmap::Format::RGBA32);
  87. for (int y = 0; y < height(); ++y) {
  88. auto* scanline = this->scanline(y);
  89. fast_u32_fill(scanline, color.value(), width());
  90. }
  91. }