GraphicsBitmap.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "GraphicsBitmap.h"
  2. #ifdef KERNEL
  3. #include <Kernel/Process.h>
  4. #include <Kernel/MemoryManager.h>
  5. #include <WindowServer/WSMessageLoop.h>
  6. #endif
  7. #ifdef USERLAND
  8. #include <sys/mman.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <errno.h>
  12. #include <stdio.h>
  13. #endif
  14. #ifdef KERNEL
  15. RetainPtr<GraphicsBitmap> GraphicsBitmap::create(Process& process, const Size& size)
  16. {
  17. return adopt(*new GraphicsBitmap(process, size));
  18. }
  19. GraphicsBitmap::GraphicsBitmap(Process& process, const Size& size)
  20. : m_size(size)
  21. , m_pitch(size.width() * sizeof(RGBA32))
  22. , m_client_process(process.make_weak_ptr())
  23. {
  24. InterruptDisabler disabler;
  25. size_t size_in_bytes = size.width() * size.height() * sizeof(RGBA32);
  26. auto vmo = VMObject::create_anonymous(size_in_bytes);
  27. m_client_region = process.allocate_region_with_vmo(LinearAddress(), size_in_bytes, vmo.copy_ref(), 0, "GraphicsBitmap (client)", true, true);
  28. m_client_region->set_shared(true);
  29. m_client_region->set_is_bitmap(true);
  30. m_client_region->commit();
  31. auto& server = WSMessageLoop::the().server_process();
  32. m_server_region = server.allocate_region_with_vmo(LinearAddress(), size_in_bytes, move(vmo), 0, "GraphicsBitmap (server)", true, false);
  33. m_server_region->set_shared(true);
  34. m_server_region->set_is_bitmap(true);
  35. m_data = (RGBA32*)m_server_region->laddr().as_ptr();
  36. }
  37. #endif
  38. RetainPtr<GraphicsBitmap> GraphicsBitmap::create_wrapper(const Size& size, RGBA32* data)
  39. {
  40. return adopt(*new GraphicsBitmap(size, data));
  41. }
  42. RetainPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(const String& path, const Size& size)
  43. {
  44. #ifdef USERLAND
  45. int fd = open(path.characters(), O_RDONLY, 0644);
  46. if (fd < 0) {
  47. dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
  48. perror("open");
  49. return nullptr;
  50. }
  51. auto* mapped_file = (RGBA32*)mmap(nullptr, size.area() * 4, PROT_READ, MAP_SHARED, fd, 0);
  52. if (mapped_file == MAP_FAILED) {
  53. int rc = close(fd);
  54. ASSERT(rc == 0);
  55. return nullptr;
  56. }
  57. #else
  58. int error;
  59. auto descriptor = VFS::the().open(path, error, 0, 0, *VFS::the().root_inode());
  60. if (!descriptor) {
  61. kprintf("Failed to load GraphicsBitmap from file (%s)\n", path.characters());
  62. ASSERT_NOT_REACHED();
  63. }
  64. auto* region = WSMessageLoop::the().server_process().allocate_file_backed_region(LinearAddress(), size.area() * 4, descriptor->inode(), ".rgb file", /*readable*/true, /*writable*/false);
  65. auto* mapped_file = (RGBA32*)region->laddr().get();
  66. #endif
  67. #ifdef USERLAND
  68. int rc = close(fd);
  69. ASSERT(rc == 0);
  70. #endif
  71. auto bitmap = create_wrapper(size, mapped_file);
  72. #ifdef KERNEL
  73. bitmap->m_server_region = region;
  74. #else
  75. bitmap->m_mmaped = true;
  76. #endif
  77. return bitmap;
  78. }
  79. GraphicsBitmap::GraphicsBitmap(const Size& size, RGBA32* data)
  80. : m_size(size)
  81. , m_data(data)
  82. , m_pitch(size.width() * sizeof(RGBA32))
  83. {
  84. }
  85. GraphicsBitmap::~GraphicsBitmap()
  86. {
  87. #ifdef KERNEL
  88. if (m_client_region && m_client_process)
  89. m_client_process->deallocate_region(*m_client_region);
  90. if (m_server_region)
  91. WSMessageLoop::the().server_process().deallocate_region(*m_server_region);
  92. #else
  93. if (m_mmaped) {
  94. int rc = munmap(m_data, m_size.area() * 4);
  95. ASSERT(rc == 0);
  96. }
  97. #endif
  98. m_data = nullptr;
  99. }