diff options
author | Tomi Valkeinen <tomi.valkeinen@iki.fi> | 2015-10-02 21:27:43 +0300 |
---|---|---|
committer | Tomi Valkeinen <tomi.valkeinen@iki.fi> | 2015-10-03 16:59:19 +0300 |
commit | 89bfd9d567ced945bf2f4b3a927426c1ad2ee762 (patch) | |
tree | c6d3e5450196f11bb5c58ce1c7c92956ff8c1fb7 /libkms++ | |
parent | c77756c48d6f38c8b40a39fd53f610c296530adf (diff) |
Add DumbFramebuffer
Move the current Framebuffer to DumbFramebuffer, and make a simple
Framebuffer as its super class.
Diffstat (limited to 'libkms++')
-rw-r--r-- | libkms++/decls.h | 1 | ||||
-rw-r--r-- | libkms++/dumbframebuffer.cpp | 167 | ||||
-rw-r--r-- | libkms++/dumbframebuffer.h | 39 | ||||
-rw-r--r-- | libkms++/framebuffer.cpp | 153 | ||||
-rw-r--r-- | libkms++/framebuffer.h | 30 | ||||
-rw-r--r-- | libkms++/kms++.h | 1 |
6 files changed, 224 insertions, 167 deletions
diff --git a/libkms++/decls.h b/libkms++/decls.h index c2cf09f..1415f1f 100644 --- a/libkms++/decls.h +++ b/libkms++/decls.h @@ -8,6 +8,7 @@ class Connector; class Crtc; class Encoder; class Framebuffer; +class DumbFramebuffer; class DrmObject; class Plane; class Property; diff --git a/libkms++/dumbframebuffer.cpp b/libkms++/dumbframebuffer.cpp new file mode 100644 index 0000000..8e77604 --- /dev/null +++ b/libkms++/dumbframebuffer.cpp @@ -0,0 +1,167 @@ + +#include <cstring> +#include <stdexcept> +#include <sys/mman.h> +#include <xf86drm.h> +#include <xf86drmMode.h> +#include <drm_fourcc.h> +#include <drm.h> +#include <drm_mode.h> + +#include "kms++.h" + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +using namespace std; + +namespace kms +{ + +DumbFramebuffer::DumbFramebuffer(Card &card, uint32_t width, uint32_t height, const char* fourcc) + :Framebuffer(card, width, height) +{ + uint32_t a, b, c, d; + a = fourcc[0]; + b = fourcc[1]; + c = fourcc[2]; + d = fourcc[3]; + + uint32_t code = ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24)); + + Create(width, height, code); +} + +DumbFramebuffer::~DumbFramebuffer() +{ + Destroy(); +} + +void DumbFramebuffer::print_short() const +{ + printf("DumbFramebuffer %d\n", id()); +} + +struct FormatPlaneInfo +{ + uint8_t bitspp; /* bits per (macro) pixel */ + uint8_t xsub; + uint8_t ysub; +}; + +struct FormatInfo +{ + uint32_t format; + uint8_t num_planes; + struct FormatPlaneInfo planes[4]; +}; + +static const FormatInfo format_info_array[] = { + /* YUV packed */ + { DRM_FORMAT_UYVY, 1, { { 32, 2, 1 } }, }, + { DRM_FORMAT_YUYV, 1, { { 32, 2, 1 } }, }, + /* YUV semi-planar */ + { DRM_FORMAT_NV12, 2, { { 8, 1, 1, }, { 16, 2, 2 } }, }, + /* RGB16 */ + { DRM_FORMAT_RGB565, 1, { { 16, 1, 1 } }, }, + /* RGB32 */ + { DRM_FORMAT_XRGB8888, 1, { { 32, 1, 1 } }, }, +}; + +static const FormatInfo& find_format(uint32_t format) +{ + for (uint i = 0; i < ARRAY_SIZE(format_info_array); ++i) { + if (format == format_info_array[i].format) + return format_info_array[i]; + } + + throw std::invalid_argument("foo"); +} + +void DumbFramebuffer::Create(uint32_t width, uint32_t height, uint32_t format) +{ + int r; + + m_format = format; + + const FormatInfo& format_info = find_format(format); + + m_num_planes = format_info.num_planes; + + for (int i = 0; i < format_info.num_planes; ++i) { + const FormatPlaneInfo& pi = format_info.planes[i]; + FramebufferPlane& plane = m_planes[i]; + + /* create dumb buffer */ + struct drm_mode_create_dumb creq = drm_mode_create_dumb(); + creq.width = width / pi.xsub; + creq.height = height / pi.ysub; + creq.bpp = pi.bitspp; + r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq); + if (r) + throw std::invalid_argument("foo"); + + plane.handle = creq.handle; + plane.stride = creq.pitch; + plane.size = creq.height * creq.pitch; + + /* + printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n", + i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size); + */ + + /* prepare buffer for memory mapping */ + struct drm_mode_map_dumb mreq = drm_mode_map_dumb(); + mreq.handle = plane.handle; + r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq); + if (r) + throw std::invalid_argument("foo"); + + /* perform actual memory mapping */ + m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED, + card().fd(), mreq.offset); + if (plane.map == MAP_FAILED) + throw std::invalid_argument("foo"); + + /* clear the framebuffer to 0 */ + memset(plane.map, 0, plane.size); + } + + /* create framebuffer object for the dumb-buffer */ + uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle }; + uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride }; + uint32_t offsets[4] = { 0 }; + uint32_t id; + r = drmModeAddFB2(card().fd(), width, height, format, + bo_handles, pitches, offsets, &id, 0); + if (r) + throw std::invalid_argument("foo"); + + m_id = id; +} + +void DumbFramebuffer::Destroy() +{ + /* delete framebuffer */ + drmModeRmFB(card().fd(), id()); + + for (uint i = 0; i < m_num_planes; ++i) { + FramebufferPlane& plane = m_planes[i]; + + /* unmap buffer */ + munmap(plane.map, plane.size); + + /* delete dumb buffer */ + struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb(); + dreq.handle = plane.handle; + drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq); + + } +} + +void DumbFramebuffer::clear() +{ + for (unsigned i = 0; i < m_num_planes; ++i) + memset(m_planes[i].map, 0, m_planes[i].size); +} + +} diff --git a/libkms++/dumbframebuffer.h b/libkms++/dumbframebuffer.h new file mode 100644 index 0000000..e7cafb6 --- /dev/null +++ b/libkms++/dumbframebuffer.h @@ -0,0 +1,39 @@ +#pragma once + +#include "framebuffer.h" + +namespace kms +{ +class DumbFramebuffer : public Framebuffer +{ +public: + DumbFramebuffer(Card& card, uint32_t width, uint32_t height, const char* fourcc); + virtual ~DumbFramebuffer(); + + void print_short() const; + + uint32_t format() const { return m_format; } + + uint8_t* map(unsigned plane) const { return m_planes[plane].map; } + uint32_t stride(unsigned plane) const { return m_planes[plane].stride; } + uint32_t size(unsigned plane) const { return m_planes[plane].size; } + + void clear(); + +private: + struct FramebufferPlane { + uint32_t handle; + uint32_t size; + uint32_t stride; + uint8_t *map; + }; + + void Create(uint32_t width, uint32_t height, uint32_t format); + void Destroy(); + + unsigned m_num_planes; + struct FramebufferPlane m_planes[4]; + + uint32_t m_format; +}; +} diff --git a/libkms++/framebuffer.cpp b/libkms++/framebuffer.cpp index a37c512..bc1fd51 100644 --- a/libkms++/framebuffer.cpp +++ b/libkms++/framebuffer.cpp @@ -1,166 +1,35 @@ - #include <cstring> #include <stdexcept> #include <sys/mman.h> #include <xf86drm.h> #include <xf86drmMode.h> -#include <drm_fourcc.h> -#include <drm.h> -#include <drm_mode.h> #include "kms++.h" -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +using namespace std; namespace kms { -Framebuffer::Framebuffer(Card &card, uint32_t width, uint32_t height, const char* fourcc) - :DrmObject(card, DRM_MODE_OBJECT_FB) -{ - uint32_t a, b, c, d; - a = fourcc[0]; - b = fourcc[1]; - c = fourcc[2]; - d = fourcc[3]; - - uint32_t code = ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24)); - - Create(width, height, code); -} - -Framebuffer::~Framebuffer() -{ - Destroy(); -} - -void Framebuffer::print_short() const -{ - printf("Framebuffer %d\n", id()); -} - -struct FormatPlaneInfo -{ - uint8_t bitspp; /* bits per (macro) pixel */ - uint8_t xsub; - uint8_t ysub; -}; - -struct FormatInfo -{ - uint32_t format; - uint8_t num_planes; - struct FormatPlaneInfo planes[4]; -}; - -static const FormatInfo format_info_array[] = { - /* YUV packed */ - { DRM_FORMAT_UYVY, 1, { { 32, 2, 1 } }, }, - { DRM_FORMAT_YUYV, 1, { { 32, 2, 1 } }, }, - /* YUV semi-planar */ - { DRM_FORMAT_NV12, 2, { { 8, 1, 1, }, { 16, 2, 2 } }, }, - /* RGB16 */ - { DRM_FORMAT_RGB565, 1, { { 16, 1, 1 } }, }, - /* RGB32 */ - { DRM_FORMAT_XRGB8888, 1, { { 32, 1, 1 } }, }, -}; - -static const FormatInfo& find_format(uint32_t format) +Framebuffer::Framebuffer(Card& card, int width, int height) + : DrmObject(card, DRM_MODE_OBJECT_FB), m_width(width), m_height(height) { - for (uint i = 0; i < ARRAY_SIZE(format_info_array); ++i) { - if (format == format_info_array[i].format) - return format_info_array[i]; - } - - throw std::invalid_argument("foo"); } -void Framebuffer::Create(uint32_t width, uint32_t height, uint32_t format) +Framebuffer::Framebuffer(Card& card, uint32_t id) + : DrmObject(card, id, DRM_MODE_OBJECT_FB) { - int r; - - m_width = width; - m_height = height; - m_format = format; - - const FormatInfo& format_info = find_format(format); - - m_num_planes = format_info.num_planes; - - for (int i = 0; i < format_info.num_planes; ++i) { - const FormatPlaneInfo& pi = format_info.planes[i]; - FramebufferPlane& plane = m_planes[i]; - - /* create dumb buffer */ - struct drm_mode_create_dumb creq = drm_mode_create_dumb(); - creq.width = m_width / pi.xsub; - creq.height = m_height / pi.ysub; - creq.bpp = pi.bitspp; - r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq); - if (r) - throw std::invalid_argument("foo"); - - plane.handle = creq.handle; - plane.stride = creq.pitch; - plane.size = creq.height * creq.pitch; - - /* - printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n", - i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size); - */ - - /* prepare buffer for memory mapping */ - struct drm_mode_map_dumb mreq = drm_mode_map_dumb(); - mreq.handle = plane.handle; - r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq); - if (r) - throw std::invalid_argument("foo"); - - /* perform actual memory mapping */ - m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED, - card().fd(), mreq.offset); - if (plane.map == MAP_FAILED) - throw std::invalid_argument("foo"); + auto fb = drmModeGetFB(card.fd(), id); - /* clear the framebuffer to 0 */ - memset(plane.map, 0, plane.size); - } + m_width = fb->width; + m_height = fb->height; - /* create framebuffer object for the dumb-buffer */ - uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle }; - uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride }; - uint32_t offsets[4] = { 0 }; - uint32_t id; - r = drmModeAddFB2(card().fd(), m_width, m_height, format, - bo_handles, pitches, offsets, &id, 0); - if (r) - throw std::invalid_argument("foo"); - - m_id = id; + drmModeFreeFB(fb); } -void Framebuffer::Destroy() +void Framebuffer::print_short() const { - /* delete framebuffer */ - drmModeRmFB(card().fd(), id()); - - for (uint i = 0; i < m_num_planes; ++i) { - FramebufferPlane& plane = m_planes[i]; - - /* unmap buffer */ - munmap(plane.map, plane.size); - - /* delete dumb buffer */ - struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb(); - dreq.handle = plane.handle; - drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq); - - } + printf("Framebuffer %d\n", id()); } -void Framebuffer::clear() -{ - for (unsigned i = 0; i < m_num_planes; ++i) - memset(m_planes[i].map, 0, m_planes[i].size); -} } diff --git a/libkms++/framebuffer.h b/libkms++/framebuffer.h index 2710528..33349d8 100644 --- a/libkms++/framebuffer.h +++ b/libkms++/framebuffer.h @@ -4,41 +4,21 @@ namespace kms { - class Framebuffer : public DrmObject { public: - Framebuffer(Card& card, uint32_t width, uint32_t height, const char* fourcc); - virtual ~Framebuffer(); - - void print_short() const; + Framebuffer(Card& card, uint32_t id); + virtual ~Framebuffer() { } uint32_t width() const { return m_width; } uint32_t height() const { return m_height; } - uint32_t format() const { return m_format; } - - uint8_t* map(unsigned plane) const { return m_planes[plane].map; } - uint32_t stride(unsigned plane) const { return m_planes[plane].stride; } - uint32_t size(unsigned plane) const { return m_planes[plane].size; } - void clear(); + virtual void print_short() const; +protected: + Framebuffer(Card& card, int width, int height); private: - struct FramebufferPlane { - uint32_t handle; - uint32_t size; - uint32_t stride; - uint8_t *map; - }; - - void Create(uint32_t width, uint32_t height, uint32_t format); - void Destroy(); - - unsigned m_num_planes; - struct FramebufferPlane m_planes[4]; - uint32_t m_width; uint32_t m_height; - uint32_t m_format; }; } diff --git a/libkms++/kms++.h b/libkms++/kms++.h index 7d6788d..c446a9e 100644 --- a/libkms++/kms++.h +++ b/libkms++/kms++.h @@ -6,6 +6,7 @@ #include "crtc.h" #include "encoder.h" #include "framebuffer.h" +#include "dumbframebuffer.h" #include "plane.h" #include "property.h" #include "pipeline.h" |