Files
oak-editor/app/render/ipc/sharedmemoryregion.cpp
T
Mike-Solar 2aa921b215 fix: bug-fix sweep across node, audio, render, plugin subsystems
Node core:
- MathNode/TrigonometryNode combo strings realigned with Operation enums
- mathbase scalar/vector operand pick no longer uses bitwise type checks
- NodeSetPositionAndDependenciesRecursively moves dependencies again
- RemoveAllKeyframes undo actually restores keyframes
- NodeGroup GetInputName null-deref guard, passthrough ids use input id
- NodeValueTable::Has is an exact type match; tag fallback only for
  empty tags; kStrCombo/kPushButton get data type names
- delete_all_keyframes no longer loops forever on unparented keyframes;
  keyframe-load failures propagate; rational interpolation falls back to
  double; OpacityEffect no longer leaks its internal MathNode

Audio/footage:
- AudioVisualWaveform: GetSummaryFromTime underflow OOB read, TrimIn
  prepend length bookkeeping, OverwriteSums source channel indexing
- PanNode inserts the pan value into the sample job (keyframed pan
  works); OutputParamsChanged is emitted on device change; PortAudio
  device indices are validated before Pa_GetDeviceInfo
- Footage: AdjustTimeByLoopMode no longer hangs/UBs on degenerate
  lengths, GetStreamIndex bounds-checked, CheckFootage clears stale
  state on missing files, failed probes are not cached,
  FootageDescription::Load requires its own root element

Render/track:
- ViewerOutput pushes the tagged samples value; TrackList disconnects
  the track-height lambda; GetTrackFromReference validity check
- RenderManager dummy backend: null-initialized threads, guarded
  decoder-cache/timer paths; Renderer::Destroy releases color cache
  shaders/textures; unknown dynamic backends no longer alias to oakgl
- SharedMemoryRegion POSIX attach validates segment size; ReadMessage
  skips blank lines instead of failing; GC counter clamped;
  IsRenderingCustomRange implemented; TimeOffsetNode gets a true
  inverse OutputTimeAdjustment; zero-speed clips return the held frame

Plugin/nodes:
- OliveClip: stored default region of definition is honored, on-demand
  images are cached; OliveHost sets host identity properties and logs
  instead of showing modal dialogs offscreen; Plugin.h dead decls gone
- DespillNode guards graph-less use with Rec.709 fallback; description
  typos fixed (despill, swirl); mosaic applies when only one axis
  matches; Windows-only Project filename separator test fixed
2026-07-17 08:26:48 +08:00

231 lines
5.3 KiB
C++

/***
Oak - Non-Linear Video Editor
Copyright (C) 2026 Oak Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "sharedmemoryregion.h"
#include <QtGlobal>
#if defined(Q_OS_WIN)
#include <windows.h>
#else
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#endif
namespace olive
{
namespace ipc
{
SharedMemoryRegion::SharedMemoryRegion()
: size_(0)
, data_(nullptr)
, mode_(kAttach)
#if defined(Q_OS_WIN)
, handle_(nullptr)
#else
, fd_(-1)
#endif
{
}
SharedMemoryRegion::~SharedMemoryRegion()
{
Close();
}
QString SharedMemoryRegion::MakeKey(qint64 owner_pid, int worker_index)
{
return QStringLiteral("olive-rw-%1-%2").arg(owner_pid).arg(worker_index);
}
#if defined(Q_OS_WIN)
bool SharedMemoryRegion::Open(const QString &key, size_t size, Mode mode)
{
Close();
key_ = key;
size_ = size;
mode_ = mode;
// Windows global mapping names live in the Local\ namespace by default for the session.
const QString mapping_name = QStringLiteral("Local\\") + key;
const std::wstring wname = mapping_name.toStdWString();
if (mode == kCreate) {
const DWORD size_high =
static_cast<DWORD>((quint64(size) >> 32) & 0xFFFFFFFF);
const DWORD size_low = static_cast<DWORD>(quint64(size) & 0xFFFFFFFF);
handle_ = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr,
PAGE_READWRITE, size_high, size_low,
wname.c_str());
if (!handle_) {
error_ = QStringLiteral("CreateFileMapping failed: %1")
.arg(GetLastError());
return false;
}
if (GetLastError() == ERROR_ALREADY_EXISTS) {
error_ =
QStringLiteral("Shared memory key already exists: %1").arg(key);
CloseHandle(handle_);
handle_ = nullptr;
return false;
}
} else {
handle_ = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, wname.c_str());
if (!handle_) {
error_ =
QStringLiteral("OpenFileMapping failed: %1").arg(GetLastError());
return false;
}
}
data_ = MapViewOfFile(handle_, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (!data_) {
error_ = QStringLiteral("MapViewOfFile failed: %1").arg(GetLastError());
CloseHandle(handle_);
handle_ = nullptr;
return false;
}
if (mode == kCreate) {
memset(data_, 0, size);
}
return true;
}
void SharedMemoryRegion::Close()
{
if (data_) {
UnmapViewOfFile(data_);
data_ = nullptr;
}
if (handle_) {
CloseHandle(handle_);
handle_ = nullptr;
}
size_ = 0;
}
#else // POSIX
bool SharedMemoryRegion::Open(const QString &key, size_t size, Mode mode)
{
Close();
key_ = key;
size_ = size;
mode_ = mode;
// POSIX shared memory names must start with a single slash and contain no others.
shm_name_ = QStringLiteral("/") + QString(key).replace('/', '_');
const QByteArray name_bytes = shm_name_.toUtf8();
int oflag = O_RDWR;
if (mode == kCreate) {
oflag |= O_CREAT | O_EXCL;
// Clear any stale segment left by a crashed previous run with the same name.
shm_unlink(name_bytes.constData());
}
fd_ = shm_open(name_bytes.constData(), oflag, 0600);
if (fd_ < 0) {
error_ = QStringLiteral("shm_open(%1) failed: %2")
.arg(shm_name_, QString::fromUtf8(strerror(errno)));
return false;
}
if (mode == kCreate) {
if (ftruncate(fd_, off_t(size)) != 0) {
error_ = QStringLiteral("ftruncate failed: %1")
.arg(QString::fromUtf8(strerror(errno)));
::close(fd_);
fd_ = -1;
shm_unlink(name_bytes.constData());
return false;
}
} else {
// mmap() succeeds even beyond the real segment size and only faults
// (SIGBUS) on access, so verify the segment is large enough up front.
struct stat st;
if (fstat(fd_, &st) != 0) {
error_ = QStringLiteral("fstat failed: %1")
.arg(QString::fromUtf8(strerror(errno)));
::close(fd_);
fd_ = -1;
return false;
}
if (st.st_size < off_t(size)) {
error_ = QStringLiteral(
"shared memory segment is %1 bytes, smaller than the requested %2")
.arg(qint64(st.st_size))
.arg(qint64(size));
::close(fd_);
fd_ = -1;
return false;
}
}
data_ = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
if (data_ == MAP_FAILED) {
error_ = QStringLiteral("mmap failed: %1")
.arg(QString::fromUtf8(strerror(errno)));
data_ = nullptr;
::close(fd_);
fd_ = -1;
if (mode == kCreate) {
shm_unlink(name_bytes.constData());
}
return false;
}
if (mode == kCreate) {
memset(data_, 0, size);
}
return true;
}
void SharedMemoryRegion::Close()
{
if (data_) {
munmap(data_, size_);
data_ = nullptr;
}
if (fd_ >= 0) {
::close(fd_);
fd_ = -1;
}
if (mode_ == kCreate && !shm_name_.isEmpty()) {
// Only the owner unlinks, so the name is freed once both sides have unmapped.
shm_unlink(shm_name_.toUtf8().constData());
shm_name_.clear();
}
size_ = 0;
}
#endif
} // namespace ipc
} // namespace olive