Merge commit 'e0067bdc75566629c9143818c8f3970c16c8825e'
[android/external-libkmsxx.git] / kms++ / src / extframebuffer.cpp
2 #include <cstring>
3 #include <stdexcept>
4 #include <sys/mman.h>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
8 #include <kms++/kms++.h>
10 using namespace std;
12 namespace kms
13 {
15 ExtFramebuffer::ExtFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format,
16                                vector<uint32_t> handles, vector<uint32_t> pitches, vector<uint32_t> offsets)
17         : Framebuffer(card, width, height)
18 {
19         m_format = format;
21         const PixelFormatInfo& format_info = get_pixel_format_info(format);
23         m_num_planes = format_info.num_planes;
25         for (int i = 0; i < format_info.num_planes; ++i) {
26                 FramebufferPlane& plane = m_planes[i];
28                 plane.handle = handles[i];
29                 plane.prime_fd = 0;
31                 plane.stride = pitches[i];
32                 plane.offset = offsets[i];
33                 plane.size = plane.stride * height;
34                 plane.map = 0;
35         }
37         uint32_t id;
38         int r = drmModeAddFB2(card.fd(), width, height, (uint32_t)format, handles.data(), pitches.data(), offsets.data(), &id, 0);
39         if (r)
40                 throw std::invalid_argument(string("Failed to create ExtFramebuffer: ") + strerror(r));
42         set_id(id);
43 }
45 ExtFramebuffer::ExtFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format,
46                                vector<int> fds, vector<uint32_t> pitches, vector<uint32_t> offsets)
47         : Framebuffer(card, width, height)
48 {
49         int r;
51         m_format = format;
53         const PixelFormatInfo& format_info = get_pixel_format_info(format);
55         m_num_planes = format_info.num_planes;
57         for (int i = 0; i < format_info.num_planes; ++i) {
58                 FramebufferPlane& plane = m_planes[i];
60                 plane.prime_fd = fds[i];
62                 r = drmPrimeFDToHandle(card.fd(), fds[i], &plane.handle);
63                 if (r)
64                         throw invalid_argument(string("drmPrimeFDToHandle: ") + strerror(errno));
66                 plane.stride = pitches[i];
67                 plane.offset = offsets[i];
68                 plane.size = plane.stride * height;
69                 plane.map = 0;
70         }
72         uint32_t id;
73         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
74         r = drmModeAddFB2(card.fd(), width, height, (uint32_t)format,
75                           bo_handles, pitches.data(), offsets.data(), &id, 0);
76         if (r)
77                 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
79         set_id(id);
80 }
82 ExtFramebuffer::~ExtFramebuffer()
83 {
84         drmModeRmFB(card().fd(), id());
85 }
87 uint8_t* ExtFramebuffer::map(unsigned plane)
88 {
89         FramebufferPlane& p = m_planes[plane];
91         if (!p.prime_fd)
92                 throw invalid_argument("cannot mmap non-dmabuf fb");
94         if (p.map)
95                 return p.map;
97         p.map = (uint8_t *)mmap(0, p.size, PROT_READ | PROT_WRITE, MAP_SHARED,
98                                           p.prime_fd, 0);
99         if (p.map == MAP_FAILED)
100                 throw invalid_argument(string("mmap failed: ") + strerror(errno));
102         return p.map;
105 int ExtFramebuffer::prime_fd(unsigned plane)
107         FramebufferPlane& p = m_planes[plane];
109         if (!p.prime_fd)
110                 throw invalid_argument("no primefb for non-dmabuf fb");
112         return p.prime_fd;