1 #include <algorithm>
2 #include <cstring>
3 #include <stdexcept>
4 #include <sys/mman.h>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
7 
8 #include <kms++/kms++.h>
9 
10 using namespace std;
11 
12 namespace kms
13 {
Framebuffer(Card & card,uint32_t width,uint32_t height)14 Framebuffer::Framebuffer(Card& card, uint32_t width, uint32_t height)
15 	: DrmObject(card, DRM_MODE_OBJECT_FB), m_width(width), m_height(height)
16 {
17 	card.m_framebuffers.push_back(this);
18 }
19 
Framebuffer(Card & card,uint32_t id)20 Framebuffer::Framebuffer(Card& card, uint32_t id)
21 	: DrmObject(card, id, DRM_MODE_OBJECT_FB)
22 {
23 	auto fb = drmModeGetFB(card.fd(), id);
24 
25 	if (fb) {
26 		m_width = fb->width;
27 		m_height = fb->height;
28 
29 		drmModeFreeFB(fb);
30 	} else {
31 		m_width = m_height = 0;
32 	}
33 
34 	card.m_framebuffers.push_back(this);
35 }
36 
flush()37 void Framebuffer::flush()
38 {
39 	drmModeClip clip{};
40 	clip.x1 = clip.y1 = 0;
41 	clip.x2 = width();
42 	clip.y2 = height();
43 
44 	drmModeDirtyFB(card().fd(), id(), &clip, 1);
45 }
46 
~Framebuffer()47 Framebuffer::~Framebuffer()
48 {
49 	auto& fbs = card().m_framebuffers;
50 	auto iter = find(fbs.begin(), fbs.end(), this);
51 	card().m_framebuffers.erase(iter);
52 }
53 
54 } // namespace kms
55