Framebuffer: don't crash is drmModeGetFB() returns 0
[android/external-libkmsxx.git] / kms++ / src / framebuffer.cpp
1 #include <algorithm>
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 Framebuffer::Framebuffer(Card& card, uint32_t width, uint32_t height)
16         : DrmObject(card, DRM_MODE_OBJECT_FB), m_width(width), m_height(height)
17 {
18         card.m_framebuffers.push_back(this);
19 }
21 Framebuffer::Framebuffer(Card& card, uint32_t id)
22         : DrmObject(card, id, DRM_MODE_OBJECT_FB)
23 {
24         auto fb = drmModeGetFB(card.fd(), id);
26         if (fb) {
27                 m_width = fb->width;
28                 m_height = fb->height;
30                 drmModeFreeFB(fb);
31         } else {
32                 m_width = m_height = 0;
33         }
35         card.m_framebuffers.push_back(this);
36 }
38 void Framebuffer::flush()
39 {
40         drmModeClip clip { };
41         clip.x1 = clip.y1 = 0;
42         clip.x2 = width();
43         clip.y2 = height();
45         drmModeDirtyFB(card().fd(), id(), &clip, 1);
46 }
48 Framebuffer::~Framebuffer()
49 {
50         auto& fbs = card().m_framebuffers;
51         auto iter = find(fbs.begin(), fbs.end(), this);
52         card().m_framebuffers.erase(iter);
53 }
56 }