2 #include <cstring>
3 #include <stdexcept>
4 #include <sys/mman.h>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <drm_fourcc.h>
10 #include <drm.h>
11 #include <drm_mode.h>
13 #include <kms++/kms++.h>
14 #include <kms++/omap/omapkms++.h>
16 extern "C" {
17 #include <omap_drmif.h>
18 }
20 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
21 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
22 #define PAGE_SIZE 4096
24 using namespace std;
26 namespace kms
27 {
29 OmapFramebuffer::OmapFramebuffer(OmapCard& card, uint32_t width, uint32_t height, const string& fourcc, Flags flags)
30 : OmapFramebuffer(card, width, height, FourCCToPixelFormat(fourcc), flags)
31 {
32 }
34 OmapFramebuffer::OmapFramebuffer(OmapCard& card, uint32_t width, uint32_t height, PixelFormat format, Flags flags)
35 :MappedFramebuffer(card, width, height), m_omap_card(card), m_format(format)
36 {
37 Create(flags);
38 }
40 OmapFramebuffer::~OmapFramebuffer()
41 {
42 Destroy();
43 }
45 void OmapFramebuffer::Create(Flags buffer_flags)
46 {
47 const PixelFormatInfo& format_info = get_pixel_format_info(m_format);
49 m_num_planes = format_info.num_planes;
51 for (int i = 0; i < format_info.num_planes; ++i) {
52 const PixelFormatPlaneInfo& pi = format_info.planes[i];
53 FramebufferPlane& plane = m_planes[i];
55 uint32_t flags = OMAP_BO_SCANOUT | OMAP_BO_WC;
57 struct omap_bo* bo;
59 uint32_t stride;
61 if (!(buffer_flags & Flags::Tiled)) {
62 stride = width() * pi.bitspp / 8;
64 uint32_t size = stride * height() / pi.ysub;
66 bo = omap_bo_new(m_omap_card.dev(), size, flags);
67 if (!bo)
68 throw invalid_argument(string("omap_bo_new failed: ") + strerror(errno));
69 } else {
70 unsigned bitspertiler;
72 switch (m_format) {
73 case PixelFormat::NV12:
74 bitspertiler = i == 0 ? 8 : 16; break;
75 case PixelFormat::YUYV:
76 case PixelFormat::UYVY:
77 bitspertiler = 32; break;
78 case PixelFormat::ARGB8888:
79 case PixelFormat::XRGB8888:
80 bitspertiler = 32; break;
81 case PixelFormat::RGB565:
82 bitspertiler = 16; break;
83 default:
84 throw invalid_argument("unimplemented format");
85 }
87 switch (bitspertiler) {
88 case 8: flags |= OMAP_BO_TILED_8; break;
89 case 16: flags |= OMAP_BO_TILED_16; break;
90 case 32: flags |= OMAP_BO_TILED_32; break;
91 default:
92 throw invalid_argument("bad bitspertiler");
93 }
95 uint32_t width_tiler = width() * pi.bitspp / bitspertiler;
97 bo = omap_bo_new_tiled(m_omap_card.dev(), width_tiler, height(), flags);
98 if (!bo)
99 throw invalid_argument(string("omap_bo_new_tiled failed: ") + strerror(errno));
101 stride = round_up(width() * pi.bitspp / 8, PAGE_SIZE);
102 }
104 plane.omap_bo = bo;
105 plane.handle = omap_bo_handle(bo);
106 plane.stride = stride;
107 plane.size = omap_bo_size(bo);
108 plane.offset = 0;
109 plane.map = 0;
110 plane.prime_fd = -1;
111 }
113 /* create framebuffer object */
114 uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
115 uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
116 uint32_t offsets[4] = { m_planes[0].offset, m_planes[1].offset };
117 uint32_t id;
118 int r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
119 bo_handles, pitches, offsets, &id, 0);
120 if (r)
121 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
123 set_id(id);
124 }
126 void OmapFramebuffer::Destroy()
127 {
128 /* delete framebuffer */
129 drmModeRmFB(card().fd(), id());
131 for (uint i = 0; i < m_num_planes; ++i) {
132 FramebufferPlane& plane = m_planes[i];
134 /* unmap buffer */
135 if (plane.map)
136 munmap(plane.map, plane.size);
138 omap_bo_del(plane.omap_bo);
140 if (plane.prime_fd >= 0)
141 ::close(plane.prime_fd);
142 }
143 }
145 uint8_t* OmapFramebuffer::map(unsigned plane)
146 {
147 FramebufferPlane& p = m_planes[plane];
149 if (p.map)
150 return p.map;
152 p.map = (uint8_t*)omap_bo_map(p.omap_bo);
153 if (p.map == MAP_FAILED)
154 throw invalid_argument(string("mmap failed: ") + strerror(errno));
156 return p.map;
157 }
159 int OmapFramebuffer::prime_fd(unsigned int plane)
160 {
161 FramebufferPlane& p = m_planes[plane];
163 if (p.prime_fd >= 0)
164 return p.prime_fd;
166 int fd = omap_bo_dmabuf(p.omap_bo);
167 if (fd < 0)
168 throw std::runtime_error("omap_bo_dmabuf failed\n");
170 p.prime_fd = fd;
172 return fd;
173 }
175 }