diff --git a/include/utils/dma_alloc.h b/include/utils/dma_alloc.h index 01fbf1d..6d1abd9 100644 --- a/include/utils/dma_alloc.h +++ b/include/utils/dma_alloc.h @@ -35,4 +35,8 @@ DmaBufferPtr DmaAlloc(size_t size); void DmaSyncStart(DmaBuffer* buf); void DmaSyncEnd(DmaBuffer* buf); +// Sync DMA-BUF fd for CPU access (when only fd is available on Frame) +void DmaSyncStartFd(int fd); +void DmaSyncEndFd(int fd); + } // namespace rk3588 diff --git a/plugins/alarm/actions/snapshot_action.cpp b/plugins/alarm/actions/snapshot_action.cpp index 59b46c5..e977c27 100644 --- a/plugins/alarm/actions/snapshot_action.cpp +++ b/plugins/alarm/actions/snapshot_action.cpp @@ -8,6 +8,8 @@ #include #include +#include "utils/dma_alloc.h" + #if defined(RK3588_ENABLE_FFMPEG) extern "C" { #include @@ -207,7 +209,10 @@ void SnapshotAction::Execute(AlarmEvent& event, std::shared_ptr frame) { return; } + const int dma_fd = frame->dma_fd; + if (dma_fd >= 0) DmaSyncStartFd(dma_fd); auto jpeg_data = EncodeJpeg(frame); + if (dma_fd >= 0) DmaSyncEndFd(dma_fd); if (jpeg_data.empty()) { std::cerr << "[SnapshotAction] failed to encode JPEG\n"; return; diff --git a/plugins/alarm/alarm_node.cpp b/plugins/alarm/alarm_node.cpp index 01535c8..812ec41 100644 --- a/plugins/alarm/alarm_node.cpp +++ b/plugins/alarm/alarm_node.cpp @@ -16,6 +16,7 @@ #include "actions/http_action.h" #include "actions/snapshot_action.h" #include "actions/clip_action.h" +#include "utils/dma_alloc.h" namespace rk3588 { @@ -31,6 +32,15 @@ private: if (src->width <= 0 || src->height <= 0) return nullptr; if (!src->data) return nullptr; + const int dma_fd = src->dma_fd; + if (dma_fd >= 0) DmaSyncStartFd(dma_fd); + struct SyncGuard { + int fd; + ~SyncGuard() { + if (fd >= 0) DmaSyncEndFd(fd); + } + } guard{dma_fd}; + auto out = std::make_shared(); out->width = src->width; out->height = src->height; diff --git a/src/utils/dma_alloc.cpp b/src/utils/dma_alloc.cpp index 7326ff6..042b7dd 100644 --- a/src/utils/dma_alloc.cpp +++ b/src/utils/dma_alloc.cpp @@ -260,4 +260,26 @@ void DmaSyncEnd(DmaBuffer* buf) { #endif } +void DmaSyncStartFd(int fd) { +#if defined(__linux__) + if (fd < 0) return; + dma_buf_sync sync{}; + sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE; + ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync); +#else + (void)fd; +#endif +} + +void DmaSyncEndFd(int fd) { +#if defined(__linux__) + if (fd < 0) return; + dma_buf_sync sync{}; + sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE; + ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync); +#else + (void)fd; +#endif +} + } // namespace rk3588