From 86186b1cbbd531843b13f592924da0e774149f29 Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Sun, 7 Feb 2016 10:27:47 +0200 Subject: add MappedBuffer & co --- libkmstest/kmstest.h | 5 ++- libkmstest/mappedbuffer.cpp | 67 ++++++++++++++++++++++++++++++++ libkmstest/mappedbuffer.h | 95 +++++++++++++++++++++++++++++++++++++++++++++ libkmstest/testpat.cpp | 19 ++++++--- 4 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 libkmstest/mappedbuffer.cpp create mode 100644 libkmstest/mappedbuffer.h (limited to 'libkmstest') diff --git a/libkmstest/kmstest.h b/libkmstest/kmstest.h index 530c160..0482072 100644 --- a/libkmstest/kmstest.h +++ b/libkmstest/kmstest.h @@ -2,8 +2,11 @@ namespace kms { +class MappedBuffer; class DumbFramebuffer; void draw_color_bar(kms::DumbFramebuffer& buf, int old_xpos, int xpos, int width); -void draw_test_pattern(DumbFramebuffer& fb); + +void draw_test_pattern(MappedBuffer& fb); +void draw_test_pattern(DumbFramebuffer &fb); } diff --git a/libkmstest/mappedbuffer.cpp b/libkmstest/mappedbuffer.cpp new file mode 100644 index 0000000..40d5f37 --- /dev/null +++ b/libkmstest/mappedbuffer.cpp @@ -0,0 +1,67 @@ +#include + +#include "mappedbuffer.h" + +using namespace std; + +namespace kms { + +struct FormatPlaneInfo +{ + uint8_t bitspp; /* bits per (macro) pixel */ + uint8_t xsub; + uint8_t ysub; +}; + +struct FormatInfo +{ + uint8_t num_planes; + struct FormatPlaneInfo planes[4]; +}; + +static const map format_info_array = { + /* YUV packed */ + { PixelFormat::UYVY, { 1, { { 32, 2, 1 } }, } }, + { PixelFormat::YUYV, { 1, { { 32, 2, 1 } }, } }, + { PixelFormat::YVYU, { 1, { { 32, 2, 1 } }, } }, + { PixelFormat::VYUY, { 1, { { 32, 2, 1 } }, } }, + /* YUV semi-planar */ + { PixelFormat::NV12, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } }, + { PixelFormat::NV21, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } }, + /* RGB16 */ + { PixelFormat::RGB565, { 1, { { 16, 1, 1 } }, } }, + /* RGB32 */ + { PixelFormat::XRGB8888, { 1, { { 32, 1, 1 } }, } }, + { PixelFormat::XBGR8888, { 1, { { 32, 1, 1 } }, } }, + { PixelFormat::ARGB8888, { 1, { { 32, 1, 1 } }, } }, + { PixelFormat::ABGR8888, { 1, { { 32, 1, 1 } }, } }, +}; + +MappedCPUBuffer::MappedCPUBuffer(uint32_t width, uint32_t height, PixelFormat format) + : m_width(width), m_height(height), m_format(format) +{ + const FormatInfo& format_info = format_info_array.at(m_format); + + m_num_planes = format_info.num_planes; + + for (unsigned i = 0; i < format_info.num_planes; ++i) { + const FormatPlaneInfo& pi = format_info.planes[i]; + FramebufferPlane& plane = m_planes[i]; + + plane.stride = width * pi.bitspp / 8 / pi.xsub; + plane.size = plane.stride * height/ pi.ysub; + plane.offset = 0; + plane.map = new uint8_t[plane.size]; + } +} + +MappedCPUBuffer::~MappedCPUBuffer() +{ + for (unsigned i = 0; i < m_num_planes; ++i) { + FramebufferPlane& plane = m_planes[i]; + + delete plane.map; + } +} + +} diff --git a/libkmstest/mappedbuffer.h b/libkmstest/mappedbuffer.h new file mode 100644 index 0000000..7c1215c --- /dev/null +++ b/libkmstest/mappedbuffer.h @@ -0,0 +1,95 @@ +#pragma once + +#include "kms++.h" + +namespace kms +{ + +class MappedBuffer { +public: + MappedBuffer() + { + } + + virtual ~MappedBuffer() + { + } + + virtual uint32_t width() const = 0; + virtual uint32_t height() const = 0; + + virtual PixelFormat format() const = 0; + virtual unsigned num_planes() const = 0; + + virtual uint32_t stride(unsigned plane) const = 0; + virtual uint32_t size(unsigned plane) const = 0; + virtual uint32_t offset(unsigned plane) const = 0; + virtual uint8_t* map(unsigned plane) = 0; +}; + +class MappedDumbBuffer : public MappedBuffer { +public: + MappedDumbBuffer(DumbFramebuffer& dumbfb) + : m_fb(dumbfb) + { + + } + + virtual ~MappedDumbBuffer() + { + + } + + uint32_t width() const { return m_fb.width(); } + uint32_t height() const { return m_fb.height(); } + + PixelFormat format() const { return m_fb.format(); } + unsigned num_planes() const { return m_fb.num_planes(); } + + uint32_t stride(unsigned plane) const { return m_fb.stride(plane); } + uint32_t size(unsigned plane) const { return m_fb.size(plane); } + uint32_t offset(unsigned plane) const { return m_fb.offset(plane); } + uint8_t* map(unsigned plane) { return m_fb.map(plane); } + +private: + DumbFramebuffer& m_fb; +}; + +class MappedCPUBuffer : public MappedBuffer { +public: + MappedCPUBuffer(uint32_t width, uint32_t height, PixelFormat format); + + virtual ~MappedCPUBuffer(); + + MappedCPUBuffer(const MappedCPUBuffer& other) = delete; + MappedCPUBuffer& operator=(const MappedCPUBuffer& other) = delete; + + uint32_t width() const { return m_width; } + uint32_t height() const { return m_height; } + + PixelFormat format() const { return m_format; } + unsigned num_planes() const { return m_num_planes; } + + uint32_t stride(unsigned plane) const { return m_planes[plane].stride; } + uint32_t size(unsigned plane) const { return m_planes[plane].size; } + uint32_t offset(unsigned plane) const { return m_planes[plane].offset; } + uint8_t* map(unsigned plane) { return m_planes[plane].map; } + + +private: + struct FramebufferPlane { + uint32_t size; + uint32_t stride; + uint32_t offset; + uint8_t *map; + }; + + uint32_t m_width; + uint32_t m_height; + PixelFormat m_format; + + unsigned m_num_planes; + struct FramebufferPlane m_planes[4]; +}; + +} diff --git a/libkmstest/testpat.cpp b/libkmstest/testpat.cpp index 007860f..33a4ec1 100644 --- a/libkmstest/testpat.cpp +++ b/libkmstest/testpat.cpp @@ -13,12 +13,13 @@ #include "kms++.h" #include "test.h" +#include "mappedbuffer.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) namespace kms { -static void draw_rgb_pixel(DumbFramebuffer& buf, unsigned x, unsigned y, RGB color) +static void draw_rgb_pixel(MappedBuffer& buf, unsigned x, unsigned y, RGB color) { switch (buf.format()) { case PixelFormat::XRGB8888: @@ -46,7 +47,7 @@ static void draw_rgb_pixel(DumbFramebuffer& buf, unsigned x, unsigned y, RGB col } } -static void draw_yuv422_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2) +static void draw_yuv422_macropixel(MappedBuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2) { ASSERT((x & 1) == 0); @@ -91,7 +92,7 @@ static void draw_yuv422_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y, } } -static void draw_yuv420_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y, +static void draw_yuv420_macropixel(MappedBuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4) { ASSERT((x & 1) == 0); @@ -133,7 +134,7 @@ static void draw_yuv420_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y, } } -static RGB get_test_pattern_pixel(DumbFramebuffer& fb, unsigned x, unsigned y) +static RGB get_test_pattern_pixel(MappedBuffer& fb, unsigned x, unsigned y) { const unsigned w = fb.width(); const unsigned h = fb.height(); @@ -214,7 +215,7 @@ static RGB get_test_pattern_pixel(DumbFramebuffer& fb, unsigned x, unsigned y) } } -static void draw_test_pattern_impl(DumbFramebuffer& fb) +static void draw_test_pattern_impl(MappedBuffer& fb) { unsigned x, y; unsigned w = fb.width(); @@ -266,7 +267,13 @@ static void draw_test_pattern_impl(DumbFramebuffer& fb) } } -void draw_test_pattern(DumbFramebuffer& fb) +void draw_test_pattern(DumbFramebuffer &fb) +{ + MappedDumbBuffer mfb(fb); + draw_test_pattern(mfb); +} + +void draw_test_pattern(MappedBuffer &fb) { #ifdef DRAW_PERF_PRINT using namespace std::chrono; -- cgit v1.2.3