Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dma heap changes. #574

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/libcamera_detect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ static void event_loop(LibcameraDetectApp &app)

StreamInfo info;
libcamera::Stream *stream = app.StillStream(&info);
const std::vector<libcamera::Span<uint8_t>> mem = app.Mmap(completed_request->buffers[stream]);
BufferReadSync r(app, completed_request->buffers[stream]);
const std::vector<libcamera::Span<uint8_t>> mem = r.Get();

// Generate a filename for the output and save it.
char filename[128];
Expand Down
3 changes: 2 additions & 1 deletion apps/libcamera_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ static void event_loop(LibcameraJpegApp &app)
Stream *stream = app.StillStream();
StreamInfo info = app.GetStreamInfo(stream);
CompletedRequestPtr &payload = std::get<CompletedRequestPtr>(msg.payload);
const std::vector<libcamera::Span<uint8_t>> mem = app.Mmap(payload->buffers[stream]);
BufferReadSync r(&app, payload->buffers[stream]);
const std::vector<libcamera::Span<uint8_t>> mem = r.Get();
jpeg_save(mem, info, payload->metadata, options->output, app.CameraModel(), options);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/libcamera_still.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ static void save_image(LibcameraStillApp &app, CompletedRequestPtr &payload, Str
{
StillOptions const *options = app.GetOptions();
StreamInfo info = app.GetStreamInfo(stream);
const std::vector<libcamera::Span<uint8_t>> mem = app.Mmap(payload->buffers[stream]);
BufferReadSync r(&app, payload->buffers[stream]);
const std::vector<libcamera::Span<uint8_t>> mem = r.Get();
if (stream == app.RawStream())
dng_save(mem, info, payload->metadata, filename, app.CameraModel(), options);
else if (options->encoding == "jpg")
Expand Down
77 changes: 77 additions & 0 deletions core/buffer_sync.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (C) 2023, Raspberry Pi Ltd
*
* buffer_sync.cpp - Buffer coherency handling
*/

#include <linux/dma-buf.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include "core/buffer_sync.hpp"
#include "core/libcamera_app.hpp"
#include "core/logging.hpp"

BufferWriteSync::BufferWriteSync(LibcameraApp *app, libcamera::FrameBuffer *fb)
: fb_(fb)
{
struct dma_buf_sync dma_sync {};
dma_sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;

auto it = app->mapped_buffers_.find(fb_);
if (it == app->mapped_buffers_.end())
{
LOG_ERROR("failed to find buffer in BufferWriteSync");
return;
}

int ret = ::ioctl(fb_->planes()[0].fd.get(), DMA_BUF_IOCTL_SYNC, &dma_sync);
if (ret)
{
LOG_ERROR("failed to lock-sync-write dma buf");
return;
}

planes_ = it->second;
}

BufferWriteSync::~BufferWriteSync()
{
struct dma_buf_sync dma_sync {};
dma_sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;

int ret = ::ioctl(fb_->planes()[0].fd.get(), DMA_BUF_IOCTL_SYNC, &dma_sync);
if (ret)
LOG_ERROR("failed to unlock-sync-write dma buf");
}

const std::vector<libcamera::Span<uint8_t>> &BufferWriteSync::Get() const
{
return planes_;
}

BufferReadSync::BufferReadSync(LibcameraApp *app, libcamera::FrameBuffer *fb)
{
auto it = app->mapped_buffers_.find(fb);
if (it == app->mapped_buffers_.end())
{
LOG_ERROR("failed to find buffer in BufferReadSync");
return;
}

// DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ happens when the request completes,
// so nothing to do here but cache the planes map.
planes_ = it->second;
}

BufferReadSync::~BufferReadSync()
{
// DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ happens when we resend the buffer
// in the next request, so nothing to do here.
}

const std::vector<libcamera::Span<uint8_t>> &BufferReadSync::Get() const
{
return planes_;
}
37 changes: 37 additions & 0 deletions core/buffer_sync.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (C) 2023 Raspberry Pi Ltd
*
* buffer_sync.hpp - Buffer coherency handling
*/

#pragma once

#include <libcamera/framebuffer.h>

class LibcameraApp;

class BufferWriteSync
{
public:
BufferWriteSync(LibcameraApp *app, libcamera::FrameBuffer *fb);
~BufferWriteSync();

const std::vector<libcamera::Span<uint8_t>> &Get() const;

private:
libcamera::FrameBuffer *fb_;
std::vector<libcamera::Span<uint8_t>> planes_;
};

class BufferReadSync
{
public:
BufferReadSync(LibcameraApp *app, libcamera::FrameBuffer *fb);
~BufferReadSync();

const std::vector<libcamera::Span<uint8_t>> &Get() const;

private:
std::vector<libcamera::Span<uint8_t>> planes_;
};
84 changes: 84 additions & 0 deletions core/dma_heaps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2023, Raspberry Pi Ltd
*
* dma_heaps.cpp - Helper class for dma-heap allocations.
*/

#include "dma_heaps.hpp"

#include <array>
#include <fcntl.h>
#include <linux/dma-buf.h>
#include <linux/dma-heap.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "core/logging.hpp"

namespace
{
/*
* /dev/dma-heap/vidbuf_cached sym links to either the system heap (Pi 5) or the
* CMA allocator (Pi 4 and below). If missing, fallback to the CMA allocator.
*/
const std::vector<const char *> heapNames
{
"/dev/dma_heap/vidbuf_cached",
"/dev/dma_heap/linux,cma",
};

} // namespace

DmaHeap::DmaHeap()
{
for (const char *name : heapNames)
{
int ret = ::open(name, O_RDWR | O_CLOEXEC, 0);
if (ret < 0)
{
LOG(2, "Failed to open " << name << ": " << ret);
continue;
}

dmaHeapHandle_ = libcamera::UniqueFD(ret);
break;
}

if (!dmaHeapHandle_.isValid())
LOG_ERROR("Could not open any dmaHeap device");
}

DmaHeap::~DmaHeap()
{
}

libcamera::UniqueFD DmaHeap::alloc(const char *name, std::size_t size) const
{
int ret;

if (!name)
return {};

struct dma_heap_allocation_data alloc = {};

alloc.len = size;
alloc.fd_flags = O_CLOEXEC | O_RDWR;

ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);
if (ret < 0)
{
LOG_ERROR("dmaHeap allocation failure for " << name);
return {};
}

libcamera::UniqueFD allocFd(alloc.fd);
ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);
if (ret < 0)
{
LOG_ERROR("dmaHeap naming failure for " << name);
return {};
}

return allocFd;
}
24 changes: 24 additions & 0 deletions core/dma_heaps.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2023, Raspberry Pi Ltd
*
* dma_heaps.h - Helper class for dma-heap allocations.
*/

#pragma once

#include <stddef.h>

#include <libcamera/base/unique_fd.h>

class DmaHeap
{
public:
DmaHeap();
~DmaHeap();
bool isValid() const { return dmaHeapHandle_.isValid(); }
libcamera::UniqueFD alloc(const char *name, std::size_t size) const;

private:
libcamera::UniqueFD dmaHeapHandle_;
};
Loading
Loading