GraphicsBitmap.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "GraphicsBitmap.h"
  2. #include <sys/mman.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. RetainPtr<GraphicsBitmap> GraphicsBitmap::create(const Size& size)
  8. {
  9. return adopt(*new GraphicsBitmap(size));
  10. }
  11. GraphicsBitmap::GraphicsBitmap(const Size& size)
  12. : m_size(size)
  13. , m_pitch(size.width() * sizeof(RGBA32))
  14. {
  15. size_t size_in_bytes = size.area() * sizeof(RGBA32);
  16. m_data = (RGBA32*)mmap(nullptr, size_in_bytes, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  17. ASSERT(m_data && m_data != (void*)-1);
  18. set_mmap_name(m_data, size_in_bytes, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
  19. m_mmaped = true;
  20. }
  21. RetainPtr<GraphicsBitmap> GraphicsBitmap::create_wrapper(const Size& size, RGBA32* data)
  22. {
  23. return adopt(*new GraphicsBitmap(size, data));
  24. }
  25. RetainPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(const String& path, const Size& size)
  26. {
  27. int fd = open(path.characters(), O_RDONLY, 0644);
  28. if (fd < 0) {
  29. dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
  30. perror("open");
  31. return nullptr;
  32. }
  33. auto* mapped_data = (RGBA32*)mmap(nullptr, size.area() * 4, PROT_READ, MAP_SHARED, fd, 0);
  34. if (mapped_data == MAP_FAILED) {
  35. int rc = close(fd);
  36. ASSERT(rc == 0);
  37. return nullptr;
  38. }
  39. int rc = close(fd);
  40. ASSERT(rc == 0);
  41. auto bitmap = create_wrapper(size, mapped_data);
  42. bitmap->m_mmaped = true;
  43. return bitmap;
  44. }
  45. GraphicsBitmap::GraphicsBitmap(const Size& size, RGBA32* data)
  46. : m_size(size)
  47. , m_data(data)
  48. , m_pitch(size.width() * sizeof(RGBA32))
  49. {
  50. }
  51. RetainPtr<GraphicsBitmap> GraphicsBitmap::create_with_shared_buffer(int shared_buffer_id, const Size& size, RGBA32* data)
  52. {
  53. if (!data) {
  54. void* shared_buffer = get_shared_buffer(shared_buffer_id);
  55. if (!shared_buffer || shared_buffer == (void*)-1)
  56. return nullptr;
  57. data = (RGBA32*)shared_buffer;
  58. }
  59. return adopt(*new GraphicsBitmap(shared_buffer_id, size, data));
  60. }
  61. GraphicsBitmap::GraphicsBitmap(int shared_buffer_id, const Size& size, RGBA32* data)
  62. : m_size(size)
  63. , m_data(data)
  64. , m_pitch(size.width() * sizeof(RGBA32))
  65. , m_shared_buffer_id(shared_buffer_id)
  66. {
  67. }
  68. GraphicsBitmap::~GraphicsBitmap()
  69. {
  70. if (m_mmaped) {
  71. int rc = munmap(m_data, m_size.area() * 4);
  72. ASSERT(rc == 0);
  73. }
  74. if (m_shared_buffer_id != -1) {
  75. int rc = release_shared_buffer(m_shared_buffer_id);
  76. ASSERT(rc == 0);
  77. }
  78. m_data = nullptr;
  79. }