Skip to content

Commit

Permalink
Add linux kernel clang format.
Browse files Browse the repository at this point in the history
Disable Autoformat like the kernel guidelines suggest.
Add instructions to generate `compile_commands.json`.
Modernize cpp code.
Fix incorrect version in test.
Add instructions to build python module.
Fix Makefile to use virtual env if activated.
Rename `acquire_framebuffer_cb` to `acquire_framebuffer_handler` for consistency.
  • Loading branch information
raldone01 committed Jun 17, 2023
1 parent eb83c2e commit c31f212
Show file tree
Hide file tree
Showing 15 changed files with 1,006 additions and 231 deletions.
687 changes: 687 additions & 0 deletions .clang-format

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ tmp/
checkpatch.pl
const_structs.checkpatch
spelling.txt

__pycache__/
evdienv/
.cache/
compile_commands.json
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"[cpp]": {
"editor.formatOnType": false,
"editor.formatOnPaste": false,
"editor.formatOnSave": false
},
"[c]": {
"editor.formatOnType": false,
"editor.formatOnPaste": false,
"editor.formatOnSave": false
}
}
43 changes: 25 additions & 18 deletions pyevdi/Buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
// Copyright (c) 2022 DisplayLink (UK) Ltd.
#include "Buffer.h"
#include "../library/evdi_lib.h"
#include <cstdlib>
#include <cstdio>

#include "../library/evdi_lib.h"
#include "Buffer.h"

int Buffer::numerator = 0;

Buffer::Buffer(evdi_mode mode, evdi_handle evdiHandle)
{
int id = numerator++;
int id = numerator++;

this->evdiHandle = evdiHandle;
int stride = mode.width;
int pitch_mask = 63;
this->evdiHandle = evdiHandle;
int stride = mode.width;
int pitch_mask = 63;

stride += pitch_mask;
stride &= ~pitch_mask;
stride *= 4;

buffer.id = id;
buffer.width = mode.width;
buffer.height = mode.height;
buffer.stride = stride;
buffer.rect_count = 16;
buffer.rects = reinterpret_cast<evdi_rect*>(
calloc(buffer.rect_count, sizeof(struct evdi_rect)));
buffer.buffer = calloc(mode.width*mode.width, mode.bits_per_pixel/8);
buffer.id = id;
buffer.width = mode.width;
buffer.height = mode.height;
buffer.stride = stride;
buffer.rect_count = 16;
buffer.rects = reinterpret_cast<evdi_rect *>(
calloc(buffer.rect_count, sizeof(struct evdi_rect)));
rects_span = std::span<evdi_rect>(buffer.rects, buffer.rect_count);
bytes_per_pixel = mode.bits_per_pixel / 8;
buffer_size = mode.width * mode.height * bytes_per_pixel;
buffer.buffer = calloc(1, buffer_size);
buffer_span =
std::span<uint32_t>(reinterpret_cast<uint32_t *>(buffer.buffer),
buffer_size / sizeof(uint32_t));

evdi_register_buffer(evdiHandle, buffer);
evdi_register_buffer(evdiHandle, buffer);
}

Buffer::~Buffer()
{
evdi_unregister_buffer(evdiHandle, buffer.id);
free(buffer.buffer);
free(buffer.rects);
evdi_unregister_buffer(evdiHandle, buffer.id);
free(buffer.buffer);
free(buffer.rects);
}
22 changes: 14 additions & 8 deletions pyevdi/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
#define BUFFER_H

#include "../library/evdi_lib.h"
#include <cstddef>
#include <memory>
#include <cstddef>
#include <span>

class Buffer : public std::enable_shared_from_this<Buffer> {
static int numerator;
evdi_handle evdiHandle;

class Buffer : public std::enable_shared_from_this<Buffer>
{
static int numerator;
evdi_handle evdiHandle;
public:
evdi_buffer buffer;
Buffer(evdi_mode mode, evdi_handle evdiHandle);
~Buffer();
public:
evdi_buffer buffer;
size_t buffer_size;
std::span<evdi_rect> rects_span;
std::span<uint32_t> buffer_span;
size_t bytes_per_pixel;
Buffer(evdi_mode mode, evdi_handle evdiHandle);
~Buffer();
};

#endif
187 changes: 93 additions & 94 deletions pyevdi/Card.cpp
Original file line number Diff line number Diff line change
@@ -1,167 +1,166 @@
// Copyright (c) 2022 DisplayLink (UK) Ltd.
#include <format>

#include "../library/evdi_lib.h"
#include <pybind11/pybind11.h>
#include "Card.h"
#include "Buffer.h"

namespace py = pybind11;


void default_update_ready_handler(int buffer_to_be_updated, void *user_data)
{
Card* card_ptr = reinterpret_cast<Card*>(user_data);
assert(buffer_to_be_updated == card_ptr->buffer_requested->buffer.id);
card_ptr->grab_pixels();
Card *card_ptr = reinterpret_cast<Card *>(user_data);
assert(buffer_to_be_updated == card_ptr->buffer_requested->buffer.id);
card_ptr->grab_pixels();
}

void card_C_mode_handler(struct evdi_mode mode, void *user_data)
{
py::module logging = py::module::import("logging");
logging.attr("info")("Got mode_changed signal.");
Card* card = reinterpret_cast<Card*>(user_data);
py::module logging = py::module::import("logging");
logging.attr("info")("Got mode_changed signal.");
Card *card = reinterpret_cast<Card *>(user_data);

assert(card);
assert(card);

card->setMode(mode);
card->makeBuffers(2);
card->setMode(mode);
card->makeBuffers(2);

if(card->m_modeHandler != nullptr){
card->m_modeHandler(mode);
}
if (!card->mode_handler.is_none()) {
card->mode_handler(mode);
}

card->request_update();
card->request_update();
}

void Card::setMode(struct evdi_mode mode)
{
this->mode = mode;
this->mode = mode;
}

void Card::makeBuffers(int count)
{
clearBuffers();
for(int i=0; i<count; i++){
buffers.emplace_back(new Buffer(mode, evdiHandle));
}
clearBuffers();
for (int i = 0; i < count; i++) {
buffers.emplace_back(new Buffer(mode, evdiHandle));
}
}

void Card::clearBuffers()
{
buffer_requested.reset();
buffers.clear();
buffer_requested.reset();
buffers.clear();
}

void dpms_handler(int dpms_mode, void* /*user_data*/){
py::module logging = py::module::import("logging");
logging.attr("info")("Got dpms signal." + std::to_string(dpms_mode));
void dpms_handler(int dpms_mode, void * /*user_data*/)
{
py::module logging = py::module::import("logging");
logging.attr("info")(std::format("Got dpms signal: \"{}\"", dpms_mode));
}

Card::Card(int device) :
evdiHandle(evdi_open(device))
Card::Card(int device)
: evdiHandle(evdi_open(device))
{
if(evdiHandle==nullptr)
{
throw py::value_error("Card /dev/dri/card" + std::to_string(device) + "does not exists!");
}

memset(&eventContext, 0, sizeof(eventContext));
if (evdiHandle == nullptr) {
throw py::value_error(std::format(
"Failed to open card \"/dev/dri/card{}\"", device));
}

m_modeHandler = nullptr;
acquire_framebuffer_cb = nullptr;
memset(&eventContext, 0, sizeof(eventContext));

eventContext.mode_changed_handler = &card_C_mode_handler;
eventContext.update_ready_handler = &default_update_ready_handler;
eventContext.dpms_handler=dpms_handler;
eventContext.user_data = this;
eventContext.mode_changed_handler = &card_C_mode_handler;
eventContext.update_ready_handler = &default_update_ready_handler;
eventContext.dpms_handler = dpms_handler;
eventContext.user_data = this;

memset(&mode, 0, sizeof(mode));
memset(&mode, 0, sizeof(mode));
}

Card::~Card()
{
close();
close();
}

void Card::close()
{
if(evdiHandle != nullptr)
{
clearBuffers();
evdi_close(evdiHandle);
}
evdiHandle = nullptr;
if (evdiHandle != nullptr) {
clearBuffers();
evdi_close(evdiHandle);
}
evdiHandle = nullptr;
}

void Card::connect(const char *edid, const unsigned int edid_length,
const uint32_t pixel_area_limit, const uint32_t pixel_per_second_limit)
const uint32_t pixel_area_limit,
const uint32_t pixel_per_second_limit)
{
evdi_connect2(evdiHandle, reinterpret_cast<const unsigned char *>(edid),
edid_length, pixel_area_limit, pixel_per_second_limit);
evdi_connect2(evdiHandle, reinterpret_cast<const unsigned char *>(edid),
edid_length, pixel_area_limit, pixel_per_second_limit);
}

void Card::disconnect()
{
evdi_disconnect(evdiHandle);
evdi_disconnect(evdiHandle);
}

struct evdi_mode Card::getMode() const
{
return mode;
return mode;
}

void Card::handle_events(int waiting_time)
{
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
int fd = evdi_get_event_ready(evdiHandle);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = waiting_time*1000;

request_update();

if(select(fd + 1, &rfds, NULL, NULL, &tv)){
evdi_handle_events(evdiHandle, &eventContext);
}
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
int fd = evdi_get_event_ready(evdiHandle);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = waiting_time * 1000;

request_update();

if (select(fd + 1, &rfds, NULL, NULL, &tv)) {
evdi_handle_events(evdiHandle, &eventContext);
}
}

void Card::request_update()
{
if(buffer_requested){
return;
}

for(auto &i : buffers){
if(i.use_count() == 1)
{
buffer_requested = i;
break;
}
}

if(!buffer_requested){
return;
}

bool update_ready = evdi_request_update(evdiHandle, buffer_requested->buffer.id);

if(update_ready){
grab_pixels();
}
if (buffer_requested) {
return;
}

for (auto &i : buffers) {
if (i.use_count() == 1) {
buffer_requested = i;
break;
}
}

if (!buffer_requested) {
return;
}

bool update_ready =
evdi_request_update(evdiHandle, buffer_requested->buffer.id);

if (update_ready) {
grab_pixels();
}
}

void Card::grab_pixels()
{
if(!buffer_requested){
return;
}
if (!buffer_requested) {
return;
}

evdi_grab_pixels(evdiHandle, buffer_requested->buffer.rects, &buffer_requested->buffer.rect_count);
evdi_grab_pixels(evdiHandle, buffer_requested->buffer.rects,
&buffer_requested->buffer.rect_count);

if(acquire_framebuffer_cb)
acquire_framebuffer_cb(std::move(buffer_requested));
buffer_requested = nullptr;
if (acquire_framebuffer_handler)
acquire_framebuffer_handler(std::move(buffer_requested));
buffer_requested = nullptr;

request_update();
request_update();
}
Loading

0 comments on commit c31f212

Please sign in to comment.