libkmsxx: Add include of <cerrno.h> to various files
[android/external-libkmsxx.git] / kms++ / src / extframebuffer.cpp
2 #include <cstring>
3 #include <cerrno>
5 #include <stdexcept>
6 #include <sys/mman.h>
7 #include <xf86drm.h>
8 #include <xf86drmMode.h>
10 #include <kms++/kms++.h>
12 using namespace std;
14 namespace kms
15 {
17 ExtFramebuffer::ExtFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format,
18                                vector<uint32_t> handles, vector<uint32_t> pitches, vector<uint32_t> offsets)
19         : Framebuffer(card, width, height)
20 {
21         m_format = format;
23         const PixelFormatInfo& format_info = get_pixel_format_info(format);
25         m_num_planes = format_info.num_planes;
27         for (int i = 0; i < format_info.num_planes; ++i) {
28                 FramebufferPlane& plane = m_planes[i];
30                 plane.handle = handles[i];
31                 plane.prime_fd = 0;
33                 plane.stride = pitches[i];
34                 plane.offset = offsets[i];
35                 plane.size = plane.stride * height;
36                 plane.map = 0;
37         }
39         uint32_t id;
40         int r = drmModeAddFB2(card.fd(), width, height, (uint32_t)format, handles.data(), pitches.data(), offsets.data(), &id, 0);
41         if (r)
42                 throw std::invalid_argument(string("Failed to create ExtFramebuffer: ") + strerror(r));
44         set_id(id);
45 }
47 ExtFramebuffer::ExtFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format,
48                                vector<int> fds, vector<uint32_t> pitches, vector<uint32_t> offsets)
49         : Framebuffer(card, width, height)
50 {
51         int r;
53         m_format = format;
55         const PixelFormatInfo& format_info = get_pixel_format_info(format);
57         m_num_planes = format_info.num_planes;
59         for (int i = 0; i < format_info.num_planes; ++i) {
60                 FramebufferPlane& plane = m_planes[i];
62                 plane.prime_fd = fds[i];
64                 r = drmPrimeFDToHandle(card.fd(), fds[i], &plane.handle);
65                 if (r)
66                         throw invalid_argument(string("drmPrimeFDToHandle: ") + strerror(errno));
68                 plane.stride = pitches[i];
69                 plane.offset = offsets[i];
70                 plane.size = plane.stride * height;
71                 plane.map = 0;
72         }
74         uint32_t id;
75         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
76         r = drmModeAddFB2(card.fd(), width, height, (uint32_t)format,
77                           bo_handles, pitches.data(), offsets.data(), &id, 0);
78         if (r)
79                 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
81         set_id(id);
82 }
84 ExtFramebuffer::~ExtFramebuffer()
85 {
86         drmModeRmFB(card().fd(), id());
87 }
89 uint8_t* ExtFramebuffer::map(unsigned plane)
90 {
91         FramebufferPlane& p = m_planes[plane];
93         if (!p.prime_fd)
94                 throw invalid_argument("cannot mmap non-dmabuf fb");
96         if (p.map)
97                 return p.map;
99         p.map = (uint8_t *)mmap(0, p.size, PROT_READ | PROT_WRITE, MAP_SHARED,
100                                           p.prime_fd, 0);
101         if (p.map == MAP_FAILED)
102                 throw invalid_argument(string("mmap failed: ") + strerror(errno));
104         return p.map;
107 int ExtFramebuffer::prime_fd(unsigned plane)
109         FramebufferPlane& p = m_planes[plane];
111         if (!p.prime_fd)
112                 throw invalid_argument("no primefb for non-dmabuf fb");
114         return p.prime_fd;