GraphicsBitmap.cpp 2.7 KB

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