Skip to content
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
137 changes: 136 additions & 1 deletion crates/quercus-ofx-plugin/cbits/quercus_test_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,128 @@ static OfxStatus mainEntryInteractEffect(const char *action, const void *handle,
return mainEntry(action, handle, inArgs, outArgs);
}

/* ---------- mask 变体:可选 mask clip fixture ----------
*
* filter 上下文,clip:Source、Mask(OfxImageClipPropOptional=1 且
* OfxImageClipPropIsMask=1)、Output。确定性行为(文档化契约):
* - Mask 已连接(OfxImageClipPropConnected=1):输出 RGB = Source RGB
* × Mask 红通道,alpha = Source alpha;
* - Mask 未连接:Source 原样透传(输出 == Source)。
* 其余 action 与 CPU 插件相同。 */
static OfxStatus actionDescribeMask(const void *handle)
{
OfxStatus st = actionDescribe(handle, 0);
if (st != kOfxStatOK)
return st;

const char *comps[] = { kOfxImageComponentRGBA, kOfxImageComponentRGB, kOfxImageComponentAlpha };
OfxPropertySetHandle clipProps = NULL;
st = g_imageEffectSuite->clipDefine((OfxImageEffectHandle)handle, "Mask", &clipProps);
if (st != kOfxStatOK)
return st;
propSetStringN(clipProps, kOfxImageEffectPropSupportedComponents, 3, comps);
propSetString(clipProps, kOfxPropLabel, 0, "Mask");
g_propSuite->propSetInt(clipProps, kOfxImageClipPropOptional, 0, 1);
g_propSuite->propSetInt(clipProps, kOfxImageClipPropIsMask, 0, 1);
return kOfxStatOK;
}

/* 取 clip 的当前图像基址/行跨度/边界;失败返回非 OK。 */
static OfxStatus getClipPixels(OfxImageClipHandle clip, double time, OfxPropertySetHandle *image,
float **data, int *rowBytes, int bounds[4])
{
void *ptr = NULL;
OfxStatus st = g_imageEffectSuite->clipGetImage(clip, time, NULL, image);
if (st != kOfxStatOK)
return st;
propGetPointer(*image, kOfxImagePropData, 0, &ptr);
propGetInt(*image, kOfxImagePropRowBytes, 0, rowBytes);
propGetIntN(*image, kOfxImagePropBounds, 4, bounds);
*data = (float *)ptr;
return kOfxStatOK;
}

static OfxStatus actionRenderMask(OfxImageEffectHandle inst, OfxPropertySetHandle inArgs)
{
double time = 0.0;
propGetDouble(inArgs, kOfxPropTime, 0, &time);

OfxImageClipHandle outClip = NULL, srcClip = NULL, maskClip = NULL;
OfxPropertySetHandle outProps = NULL, srcProps = NULL, maskProps = NULL;
OfxStatus st = g_imageEffectSuite->clipGetHandle(inst, "Output", &outClip, &outProps);
if (st != kOfxStatOK)
return st;
st = g_imageEffectSuite->clipGetHandle(inst, "Source", &srcClip, &srcProps);
if (st != kOfxStatOK)
return st;
st = g_imageEffectSuite->clipGetHandle(inst, "Mask", &maskClip, &maskProps);
if (st != kOfxStatOK)
return st;

int maskConnected = 0;
propGetInt(maskProps, kOfxImageClipPropConnected, 0, &maskConnected);

OfxPropertySetHandle outImg = NULL, srcImg = NULL, maskImg = NULL;
float *out = NULL, *src = NULL, *mask = NULL;
int outRow = 0, srcRow = 0, maskRow = 0;
int outB[4] = { 0, 0, 0, 0 }, srcB[4] = { 0, 0, 0, 0 }, maskB[4] = { 0, 0, 0, 0 };

st = getClipPixels(outClip, time, &outImg, &out, &outRow, outB);
if (st != kOfxStatOK)
return st;
st = getClipPixels(srcClip, time, &srcImg, &src, &srcRow, srcB);
if (st == kOfxStatOK && maskConnected) {
st = getClipPixels(maskClip, time, &maskImg, &mask, &maskRow, maskB);
}
if (st == kOfxStatOK && out && src) {
int w = outB[2] - outB[0];
int h = outB[3] - outB[1];
int sw = srcB[2] - srcB[0];
int sh = srcB[3] - srcB[1];
if (sw < w) w = sw;
if (sh < h) h = sh;
if (mask) {
int mw = maskB[2] - maskB[0];
int mh = maskB[3] - maskB[1];
if (mw < w) w = mw;
if (mh < h) h = mh;
}
for (int y = 0; y < h; y++) {
float *orow = (float *)((char *)out + (size_t)y * (size_t)outRow);
const float *srow = (const float *)((const char *)src + (size_t)y * (size_t)srcRow);
const float *mrow =
mask ? (const float *)((const char *)mask + (size_t)y * (size_t)maskRow) : NULL;
for (int x = 0; x < w; x++) {
float k = mrow ? mrow[x * 4 + 0] : 1.0f;
orow[x * 4 + 0] = srow[x * 4 + 0] * k;
orow[x * 4 + 1] = srow[x * 4 + 1] * k;
orow[x * 4 + 2] = srow[x * 4 + 2] * k;
orow[x * 4 + 3] = srow[x * 4 + 3];
}
}
}

if (maskImg)
g_imageEffectSuite->clipReleaseImage(maskImg);
if (srcImg)
g_imageEffectSuite->clipReleaseImage(srcImg);
g_imageEffectSuite->clipReleaseImage(outImg);
return st;
}

static OfxStatus mainEntryMask(const char *action, const void *handle,
OfxPropertySetHandle inArgs, OfxPropertySetHandle outArgs)
{
if (strcmp(action, kOfxActionDescribe) == 0 ||
strcmp(action, kOfxImageEffectActionDescribeInContext) == 0) {
return actionDescribeMask(handle);
}
if (strcmp(action, kOfxImageEffectActionRender) == 0) {
return actionRenderMask((OfxImageEffectHandle)handle, inArgs);
}
return mainEntry(action, handle, inArgs, outArgs);
}

/* ---------- 导出 ---------- */

static const OfxPlugin test_plugin = {
Expand Down Expand Up @@ -925,9 +1047,20 @@ static const OfxPlugin test_plugin_slow = {
/* mainEntry */ mainEntrySlow,
};

/* 可选 mask clip fixture(Resolve 21 适用性报告:可选 OFX mask 输入)。 */
static const OfxPlugin test_plugin_mask = {
/* pluginApi */ kOfxImageEffectPluginApi,
/* apiVersion */ kOfxImageEffectPluginApiVersion,
/* pluginIdentifier */ "org.quercus.test-plugin.mask",
/* pluginVersionMajor */ 1,
/* pluginVersionMinor */ 0,
/* setHost */ setHost,
/* mainEntry */ mainEntryMask,
};

OfxExport int OfxGetNumberOfPlugins(void)
{
return 5;
return 6;
}

OfxExport OfxPlugin *OfxGetPlugin(int nth)
Expand All @@ -942,5 +1075,7 @@ OfxExport OfxPlugin *OfxGetPlugin(int nth)
return (OfxPlugin *)&test_plugin_interact;
if (nth == 4)
return (OfxPlugin *)&test_plugin_slow;
if (nth == 5)
return (OfxPlugin *)&test_plugin_mask;
return NULL;
}
8 changes: 4 additions & 4 deletions crates/quercus-ofx-plugin/src/host/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn load_bundle_loads_fixture_plugins_and_dedupes() {
};
let cache = test_cache();
cache.load_bundle(&bundle);
assert_eq!(cache.count(), 5, "测试插件导出 5 个效果");
assert_eq!(cache.count(), 6, "测试插件导出 6 个效果");
assert!(cache.find("org.quercus.test-plugin").is_some());
assert_eq!(
cache
Expand All @@ -449,7 +449,7 @@ fn load_bundle_loads_fixture_plugins_and_dedupes() {

// 同一 bundle 二次加载 → 路径去重(直接 dlclose 返回)。
cache.load_bundle(&bundle);
assert_eq!(cache.count(), 5);
assert_eq!(cache.count(), 6);
assert_eq!(
cache
.binaries
Expand Down Expand Up @@ -479,10 +479,10 @@ fn load_bundle_ignores_duplicate_plugin_identifiers() {
};
let cache = test_cache();
cache.load_bundle(&bundle1);
assert_eq!(cache.count(), 5);
assert_eq!(cache.count(), 6);
// 第二个 bundle 的 identifier 全部重复 → 忽略,但二进制登记两份。
cache.load_bundle(&bundle2);
assert_eq!(cache.count(), 5);
assert_eq!(cache.count(), 6);
assert_eq!(
cache
.binaries
Expand Down
153 changes: 153 additions & 0 deletions crates/quercus-ofx-plugin/tests/mask_clip_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Quercus Video Editor - Non-Linear Video Editor
// Copyright (C) 2026 Quercus 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/>.

//! Optional mask clip fixture (`org.quercus.test-plugin.mask`): descriptor flags and the
//! `OfxImageClipPropConnected` lifecycle (attach, clear, reattach) driven by synthetic textures.
//!
//! Scope: fixture and connection-level coverage only (no GPU, no third-party plugin). The
//! fixture's documented behaviour is: mask connected → output RGB = Source RGB × Mask red,
//! alpha from Source; mask absent → Source passes through. Render-driver mask mapping and
//! CPU/GL mask rendering belong to the follow-up mask integration work.

mod common;

use std::sync::Arc;

use quercus_ofx_plugin::handle::RefBox;
use quercus_ofx_plugin::host::Host;
use quercus_ofx_plugin::instance::Instance;
use quercus_ofx_plugin::property::Value;
use quercus_ofx_plugin::render::{Frame, Texture, VideoParams, PIXEL_FORMAT_F32};

const MASK_PLUGIN_ID: &str = "org.quercus.test-plugin.mask";
const PLAIN_PLUGIN_ID: &str = "org.quercus.test-plugin";

fn create_instance(id: &str) -> Option<Arc<RefBox<Instance>>> {
let Some(dir) = common::test_plugin_scan_dir() else {
common::skip("最小测试插件未构建");
return None;
};
let host = Host::global();
let _ = host.cache.scan();
host.cache.scan_path(&dir).ok()?;
host.create_instance(id, None).ok()
}

/// A synthetic CPU texture (no GPU involved).
fn synthetic_texture(w: i32, h: i32) -> Texture {
let mut frame = Frame::new();
frame.set_video_params(VideoParams {
width: w,
height: h,
format: PIXEL_FORMAT_F32,
..Default::default()
});
assert!(frame.allocate(), "合成帧应可分配");
Texture::wrap_frame(frame)
}

fn int_prop(clip: &quercus_ofx_plugin::clip::ClipInstance, name: &str) -> Option<i32> {
match clip.props.get(name, 0)? {
Value::Int(v) => Some(v),
_ => None,
}
}

/// The Mask clip is optional and flagged as a mask; Source/Output are neither.
#[test]
fn mask_clip_descriptor_flags() {
common::with_host(|| {
let Some(inst) = create_instance(MASK_PLUGIN_ID) else {
Host::global().shutdown();
return;
};
let mask = inst.value.clips.iter().find(|c| c.name == "Mask");
let mask = mask.expect("fixture 应暴露 Mask clip");
assert_eq!(int_prop(mask, "OfxImageClipPropOptional"), Some(1));
assert_eq!(int_prop(mask, "OfxImageClipPropIsMask"), Some(1));
for name in ["Source", "Output"] {
let clip = inst.value.clips.iter().find(|c| c.name == name).unwrap();
assert_eq!(int_prop(clip, "OfxImageClipPropIsMask"), Some(0), "{name}");
}
Host::global().shutdown();
});
}

/// With no texture the mask is disconnected; attach → connected; clear → disconnected;
/// reattach → connected again.
#[test]
fn mask_clip_connection_lifecycle() {
common::with_host(|| {
let Some(inst) = create_instance(MASK_PLUGIN_ID) else {
Host::global().shutdown();
return;
};
let mask = inst
.value
.clips
.iter()
.find(|c| c.name == "Mask")
.expect("fixture 应暴露 Mask clip");
let connected = || int_prop(mask, "OfxImageClipPropConnected");

assert_eq!(connected(), Some(0), "初始应未连接");
assert!(mask.input_texture_value().is_none());

mask.set_input_texture(Some(synthetic_texture(4, 4)), 0.0);
assert_eq!(connected(), Some(1), "挂接纹理后应已连接");
assert!(mask.input_texture_value().is_some());

mask.set_input_texture(None, 0.0);
assert_eq!(connected(), Some(0), "清除后应回到未连接");
assert!(mask.input_texture_value().is_none());

mask.set_input_texture(Some(synthetic_texture(8, 2)), 0.0);
assert_eq!(connected(), Some(1), "重新挂接后应再次连接");
Host::global().shutdown();
});
}

/// Mask connection state is per-clip: toggling the mask never touches Source, and the
/// existing plain test plugin keeps exposing no Mask clip.
#[test]
fn mask_connection_is_independent_and_plain_plugin_unchanged() {
common::with_host(|| {
let Some(inst) = create_instance(MASK_PLUGIN_ID) else {
Host::global().shutdown();
return;
};
let source = inst
.value
.clips
.iter()
.find(|c| c.name == "Source")
.unwrap();
let mask = inst.value.clips.iter().find(|c| c.name == "Mask").unwrap();
mask.set_input_texture(Some(synthetic_texture(4, 4)), 0.0);
assert_eq!(int_prop(source, "OfxImageClipPropConnected"), Some(0));
assert_eq!(int_prop(mask, "OfxImageClipPropConnected"), Some(1));
Host::global().shutdown();

let Some(plain) = create_instance(PLAIN_PLUGIN_ID) else {
Host::global().shutdown();
return;
};
let mut names: Vec<_> = plain.value.clips.iter().map(|c| c.name.as_str()).collect();
names.sort_unstable();
assert_eq!(names, ["Output", "Source"]);
Host::global().shutdown();
});
}
Loading