Skip to main content

vc_runtime/renderer/
mod.rs

1//! Image renderer abstraction.
2
3mod constants;
4mod dummy;
5mod python;
6
7#[cfg(feature = "comfy-renderer")]
8mod comfyui;
9
10// Ekspor konstanta publik
11pub use constants::OUTPUT_FILENAME;
12
13// Implementasi renderer tetap publik agar dapat diuji dan diekstensi
14pub use dummy::DummyRenderer;
15pub use python::PythonRenderer;
16
17#[cfg(feature = "comfy-renderer")]
18pub use comfyui::ComfyUIRenderer;
19
20// PythonConfig dikonsolidasi dari config::model
21pub use crate::config::model::PythonConfig;
22
23use anyhow::Result;
24use serde::{Deserialize, Serialize};
25use std::fmt;
26use std::path::{Path, PathBuf};
27use vc_shared::hashing::Sha256Digest; // <--- TAMBAHKAN PathBuf
28
29// ============================================================
30// RendererKind
31// ============================================================
32#[non_exhaustive]
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "lowercase")]
35pub enum RendererKind {
36    Dummy,
37    Python,
38    Comfy,
39}
40
41impl fmt::Display for RendererKind {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            RendererKind::Dummy => write!(f, "dummy"),
45            RendererKind::Python => write!(f, "python"),
46            RendererKind::Comfy => write!(f, "comfy"),
47        }
48    }
49}
50
51// ============================================================
52// RenderOutput
53// ============================================================
54#[non_exhaustive]
55#[derive(Debug, Clone)]
56pub struct RenderOutput {
57    pub image_path: PathBuf,
58    pub image_hash: Sha256Digest,
59    pub duration_ms: u64,
60    pub backend: RendererKind,
61    pub seed: u64,
62}
63
64// ============================================================
65// RenderParams
66// ============================================================
67#[non_exhaustive]
68#[derive(Debug, Clone)]
69pub struct RenderParams {
70    pub width: u32,
71    pub height: u32,
72    pub steps: u32,
73    pub cfg: f64,
74    pub backend_options: serde_json::Value,
75}
76
77impl RenderParams {
78    /// Create new RenderParams with default backend_options.
79    pub fn new(width: u32, height: u32, steps: u32, cfg: f64) -> Self {
80        Self {
81            width,
82            height,
83            steps,
84            cfg,
85            backend_options: serde_json::Value::Object(serde_json::Map::new()),
86        }
87    }
88
89    /// Set custom backend options.
90    pub fn with_backend_options(mut self, options: serde_json::Value) -> Self {
91        self.backend_options = options;
92        self
93    }
94}
95
96impl Default for RenderParams {
97    fn default() -> Self {
98        Self::new(1024, 1024, 30, 7.0)
99    }
100}
101
102// ============================================================
103// ImageRenderer Trait
104// ============================================================
105pub trait ImageRenderer: Send + Sync {
106    fn render(
107        &self,
108        prompt: &str,
109        negative_prompt: &str,
110        seed: u64,
111        output_dir: &Path,
112        params: &RenderParams,
113    ) -> Result<RenderOutput>;
114}