Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Add watermark for free plan

Richie McIlroy24b6c2aae257
diff --git a/Cargo.lock b/Cargo.lock
index 76e36339c..82de4f205 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -713,6 +713,19 @@ dependencies = [
"alloc-stdlib",
]
+[[package]]
+name = "build-time"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1219c19fc29b7bfd74b7968b420aff5bc951cf517800176e795d6b2300dd382"
+dependencies = [
+ "chrono",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.90",
+]
+
[[package]]
name = "built"
version = "0.7.4"
@@ -942,6 +955,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"bezier_easing",
+ "build-time",
"bytemuck",
"cap-flags",
"cap-project",
diff --git a/apps/desktop/src/routes/(window-chrome)/(main).tsx b/apps/desktop/src/routes/(window-chrome)/(main).tsx
index aa203ec54..f751ad4e6 100644
--- a/apps/desktop/src/routes/(window-chrome)/(main).tsx
+++ b/apps/desktop/src/routes/(window-chrome)/(main).tsx
@@ -54,11 +54,6 @@ const getAuth = cache(async () => {
const value = await authStore.get();
const local = import.meta.env.VITE_LOCAL_MODE === "true";
- if (!value) {
- if (local) return;
- return redirect("/signin");
- }
-
const res = await apiClient.desktop.getUserPlan({
headers: await protectedHeaders(),
});
diff --git a/crates/rendering/Cargo.toml b/crates/rendering/Cargo.toml
index 5e59df415..f32fb2f30 100644
--- a/crates/rendering/Cargo.toml
+++ b/crates/rendering/Cargo.toml
@@ -6,20 +6,20 @@ edition = "2021"
[dependencies]
anyhow.workspace = true
bytemuck = { version = "1.7", features = ["derive"] }
-futures-intrusive = "0.5.0"
-serde = "1.0.209"
-specta.workspace = true
-wgpu.workspace = true
-image = "0.25.2"
-cap-project = { path = "../project" }
cap-flags = { path = "../flags" }
+cap-project = { path = "../project" }
cap-recording = { path = "../recording" }
ffmpeg-hw-device = { path = "../ffmpeg-hw-device" }
tokio.workspace = true
ffmpeg.workspace = true
ffmpeg-sys-next.workspace = true
futures = "0.3.30"
+futures-intrusive = "0.5.0"
+image = "0.25.2"
+serde = "1.0.209"
+specta.workspace = true
thiserror.workspace = true
+wgpu.workspace = true
bezier_easing = "0.1.1"
[target.'cfg(unix)'.dependencies]
@@ -27,3 +27,6 @@ nix = { version = "0.29.0", features = ["fs"] }
[dev-dependencies]
pretty_assertions = "1.4.1"
+
+[build-dependencies]
+build-time = "0.1"
diff --git a/crates/rendering/assets/watermark.png b/crates/rendering/assets/watermark.png
new file mode 100644
index 000000000..da3bc994b
Binary files /dev/null and b/crates/rendering/assets/watermark.png differ
diff --git a/crates/rendering/assets/watermark.svg b/crates/rendering/assets/watermark.svg
new file mode 100644
index 000000000..30d5b30b6
--- /dev/null
+++ b/crates/rendering/assets/watermark.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 103 40" class="w-20 sm:w-24 h-auto"><rect width="39.5" height="39.5" x="0.25" y="0.25" fill="#fff" rx="7.75"></rect><rect width="39.5" height="39.5" x="0.25" y="0.25" stroke="#E7EAF0" stroke-width="0.5" rx="7.75"></rect><path fill="#4785FF" d="M20 36c8.837 0 16-7.163 16-16 0-8.836-7.163-16-16-16-8.836 0-16 7.164-16 16 0 8.837 7.164 16 16 16z"></path><path fill="#ADC9FF" d="M20 33c7.18 0 13-5.82 13-13S27.18 7 20 7 7 12.82 7 20s5.82 13 13 13z"></path><path fill="#fff" d="M20 30c5.523 0 10-4.477 10-10s-4.477-10-10-10-10 4.477-10 10 4.477 10 10 10z"></path><path fill="#ffffff" d="M58.416 30.448c-5.404 0-9.212-3.864-9.212-10.36 0-6.384 3.668-10.416 9.268-10.416 5.068 0 7.784 2.66 8.624 7.168l-3.808.196c-.476-2.604-2.072-4.2-4.816-4.2-3.388 0-5.488 2.828-5.488 7.252 0 4.48 2.156 7.196 5.46 7.196 2.94 0 4.508-1.708 4.956-4.564l3.808.196c-.784 4.676-3.752 7.532-8.792 7.532zm16.23-.112c-3.137 0-5.209-1.484-5.209-4.088 0-2.576 1.596-3.948 4.872-4.592l4.956-.98c0-2.1-.98-3.192-2.856-3.192-1.764 0-2.716.812-3.052 2.324l-3.668-.168c.588-3.136 2.996-4.928 6.72-4.928 4.256 0 6.44 2.24 6.44 6.216v5.432c0 .812.28 1.036.84 1.036h.476V30c-.224.056-.812.112-1.288.112-1.624 0-2.828-.588-3.136-2.436-.728 1.596-2.632 2.66-5.096 2.66zm.727-2.604c2.38 0 3.892-1.512 3.892-3.78v-.84l-3.864.784c-1.596.308-2.24.98-2.24 2.016 0 1.176.784 1.82 2.212 1.82zM86.874 34.2V15.048h3.444l.056 2.212c.868-1.652 2.52-2.548 4.48-2.548 4.256 0 6.356 3.5 6.356 7.812s-2.128 7.812-6.384 7.812c-1.904 0-3.556-.924-4.368-2.38V34.2h-3.584zm7.112-6.776c2.184 0 3.5-1.82 3.5-4.9s-1.316-4.9-3.5-4.9-3.528 1.652-3.528 4.9 1.316 4.9 3.528 4.9z"></path></svg>
\ No newline at end of file
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index 18f21128e..e6f8aa882 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -4,9 +4,9 @@ use cap_flags::FLAGS;
use cap_project::{
AspectRatio, BackgroundSource, CameraXPosition, CameraYPosition, Content, Crop,
CursorAnimationStyle, CursorClickEvent, CursorData, CursorEvents, CursorMoveEvent,
- ProjectConfiguration, RecordingMeta, ZoomSegment, FAST_SMOOTHING_SAMPLES,
- FAST_VELOCITY_THRESHOLD, REGULAR_SMOOTHING_SAMPLES, REGULAR_VELOCITY_THRESHOLD,
- SLOW_SMOOTHING_SAMPLES, SLOW_VELOCITY_THRESHOLD, XY,
+ ProjectConfiguration, RecordingMeta, FAST_SMOOTHING_SAMPLES, FAST_VELOCITY_THRESHOLD,
+ REGULAR_SMOOTHING_SAMPLES, REGULAR_VELOCITY_THRESHOLD, SLOW_SMOOTHING_SAMPLES,
+ SLOW_VELOCITY_THRESHOLD, XY,
};
use cap_recording::segmented_actor::RecordingSegment;
use core::f64;
@@ -293,6 +293,10 @@ pub struct RenderVideoConstants {
gradient_or_color_pipeline: GradientOrColorPipeline,
pub cursor_textures: HashMap<String, wgpu::Texture>,
cursor_pipeline: CursorPipeline,
+ watermark_pipeline: WatermarkPipeline,
+ watermark_texture: wgpu::Texture,
+ watermark_bind_group: wgpu::BindGroup,
+ watermark_dimensions: (u32, u32), // Add watermark dimensions
}
impl RenderVideoConstants {
@@ -315,19 +319,113 @@ impl RenderVideoConstants {
// Pass project_path to load_cursor_textures
let cursor_textures = Self::load_cursor_textures(&device, &queue, meta);
-
let cursor_pipeline = CursorPipeline::new(&device);
+ let watermark_pipeline = WatermarkPipeline::new(&device);
+ let composite_video_frame_pipeline = CompositeVideoFramePipeline::new(&device);
+ let gradient_or_color_pipeline = GradientOrColorPipeline::new(&device);
+
+ // Create watermark texture from embedded logo
+ let watermark_bytes = include_bytes!("../assets/watermark.png");
+ println!(
+ "Loading watermark texture from embedded bytes: {} bytes",
+ watermark_bytes.len()
+ );
+
+ let watermark_image = image::load_from_memory(watermark_bytes).unwrap();
+ let watermark_rgba = watermark_image.to_rgba8();
+ let dimensions = watermark_image.dimensions();
+ println!(
+ "Loaded watermark image dimensions: {}x{}",
+ dimensions.0, dimensions.1
+ );
+
+ let watermark_texture = device.create_texture(&wgpu::TextureDescriptor {
+ label: Some("WatermarkTexture"),
+ size: wgpu::Extent3d {
+ width: dimensions.0,
+ height: dimensions.1,
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Rgba8UnormSrgb,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
+ view_formats: &[],
+ });
+
+ queue.write_texture(
+ wgpu::ImageCopyTexture {
+ texture: &watermark_texture,
+ mip_level: 0,
+ origin: wgpu::Origin3d::ZERO,
+ aspect: wgpu::TextureAspect::All,
+ },
+ &watermark_rgba,
+ wgpu::ImageDataLayout {
+ offset: 0,
+ bytes_per_row: Some(4 * dimensions.0),
+ rows_per_image: Some(dimensions.1),
+ },
+ wgpu::Extent3d {
+ width: dimensions.0,
+ height: dimensions.1,
+ depth_or_array_layers: 1,
+ },
+ );
+ println!("Watermark texture created and written to GPU");
+
+ let watermark_view = watermark_texture.create_view(&wgpu::TextureViewDescriptor::default());
+ let watermark_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
+ address_mode_u: wgpu::AddressMode::ClampToEdge,
+ address_mode_v: wgpu::AddressMode::ClampToEdge,
+ address_mode_w: wgpu::AddressMode::ClampToEdge,
+ mag_filter: wgpu::FilterMode::Linear,
+ min_filter: wgpu::FilterMode::Linear,
+ mipmap_filter: wgpu::FilterMode::Linear,
+ ..Default::default()
+ });
+
+ let watermark_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("WatermarkUniformBuffer"),
+ size: std::mem::size_of::<WatermarkUniforms>() as u64,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+
+ let watermark_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("WatermarkBindGroup"),
+ layout: &watermark_pipeline.bind_group_layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(&watermark_view),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::Sampler(&watermark_sampler),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: watermark_uniform_buffer.as_entire_binding(),
+ },
+ ],
+ });
Ok(Self {
- composite_video_frame_pipeline: CompositeVideoFramePipeline::new(&device),
- gradient_or_color_pipeline: GradientOrColorPipeline::new(&device),
_instance: instance,
_adapter: adapter,
- queue,
device,
+ queue,
options,
+ composite_video_frame_pipeline,
+ gradient_or_color_pipeline,
cursor_textures,
cursor_pipeline,
+ watermark_pipeline,
+ watermark_texture,
+ watermark_bind_group,
+ watermark_dimensions: dimensions,
})
}
@@ -975,6 +1073,113 @@ pub async fn produce_frame(
// Now submit the encoder
constants.queue.submit(std::iter::once(encoder.finish()));
+ // Add watermark if not upgraded - moved to after all other rendering
+ if uniforms.project.background.inset <= 0 {
+ // Calculate watermark width as 10% of frame width, but no larger than 200px
+ let frame_width = uniforms.output_size.0 as f32;
+ let target_width = (frame_width * 0.10).min(200.0);
+ let aspect_ratio =
+ constants.watermark_dimensions.1 as f32 / constants.watermark_dimensions.0 as f32;
+
+ // Increase padding from edges
+ let padding = frame_width * 0.02; // 2% of frame width for padding
+
+ let watermark_uniforms = WatermarkUniforms {
+ output_size: [uniforms.output_size.0 as f32, uniforms.output_size.1 as f32],
+ watermark_size: [target_width, target_width * aspect_ratio],
+ position: [
+ padding,
+ uniforms.output_size.1 as f32 - (target_width * aspect_ratio) - padding,
+ ],
+ opacity: 0.5, // Reduced opacity from 0.8 to 0.5
+ is_upgraded: 0.0,
+ };
+
+ println!(
+ "Rendering watermark at position: {:?}, size: {:?}, output_size: {:?}",
+ watermark_uniforms.position,
+ watermark_uniforms.watermark_size,
+ watermark_uniforms.output_size
+ );
+
+ let watermark_buffer =
+ constants
+ .device
+ .create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: Some("Watermark Uniforms Buffer"),
+ contents: bytemuck::cast_slice(&[watermark_uniforms]),
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ });
+
+ let watermark_bind_group = constants
+ .device
+ .create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("WatermarkBindGroup"),
+ layout: &constants.watermark_pipeline.bind_group_layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(
+ &constants
+ .watermark_texture
+ .create_view(&wgpu::TextureViewDescriptor::default()),
+ ),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::Sampler(&constants.device.create_sampler(
+ &wgpu::SamplerDescriptor {
+ address_mode_u: wgpu::AddressMode::ClampToEdge,
+ address_mode_v: wgpu::AddressMode::ClampToEdge,
+ address_mode_w: wgpu::AddressMode::ClampToEdge,
+ mag_filter: wgpu::FilterMode::Linear,
+ min_filter: wgpu::FilterMode::Linear,
+ mipmap_filter: wgpu::FilterMode::Linear,
+ anisotropy_clamp: 16, // Add anisotropic filtering
+ ..Default::default()
+ },
+ )),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: watermark_buffer.as_entire_binding(),
+ },
+ ],
+ });
+
+ let mut watermark_encoder =
+ constants
+ .device
+ .create_command_encoder(&wgpu::CommandEncoderDescriptor {
+ label: Some("WatermarkEncoder"),
+ });
+
+ {
+ let mut render_pass =
+ watermark_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("WatermarkRenderPass"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view: get_either(texture_views, !output_is_left),
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Load,
+ store: wgpu::StoreOp::Store,
+ },
+ })],
+ depth_stencil_attachment: None,
+ timestamp_writes: None,
+ occlusion_query_set: None,
+ });
+
+ render_pass.set_pipeline(&constants.watermark_pipeline.render_pipeline);
+ render_pass.set_bind_group(0, &watermark_bind_group, &[]);
+ render_pass.draw(0..6, 0..1); // Using 6 vertices for two triangles
+ println!("Drew watermark with 6 vertices");
+ }
+
+ constants.queue.submit(Some(watermark_encoder.finish()));
+ }
+
let output_texture_size = wgpu::Extent3d {
width: uniforms.output_size.0,
height: uniforms.output_size.1,
@@ -1764,7 +1969,7 @@ impl CursorPipeline {
format: wgpu::TextureFormat::Rgba8UnormSrgb,
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent {
- src_factor: wgpu::BlendFactor::SrcAlpha,
+ src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
@@ -1923,3 +2128,125 @@ impl<T> Mul<f64> for Coord<T> {
}
}
}
+
+struct WatermarkPipeline {
+ bind_group_layout: wgpu::BindGroupLayout,
+ render_pipeline: wgpu::RenderPipeline,
+}
+
+impl WatermarkPipeline {
+ pub fn new(device: &wgpu::Device) -> Self {
+ let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("WatermarkBindGroupLayout"),
+ entries: &[
+ wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
+ },
+ wgpu::BindGroupLayoutEntry {
+ binding: 1,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+ count: None,
+ },
+ wgpu::BindGroupLayoutEntry {
+ binding: 2,
+ visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ },
+ ],
+ });
+
+ let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some("WatermarkShader"),
+ source: wgpu::ShaderSource::Wgsl(include_str!("shaders/watermark.wgsl").into()),
+ });
+
+ let empty_constants: HashMap<String, f64> = HashMap::new();
+
+ let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("WatermarkPipeline"),
+ layout: Some(
+ &device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("WatermarkPipelineLayout"),
+ bind_group_layouts: &[&bind_group_layout],
+ push_constant_ranges: &[],
+ }),
+ ),
+ vertex: wgpu::VertexState {
+ module: &shader,
+ entry_point: "vs_main",
+ buffers: &[],
+ compilation_options: wgpu::PipelineCompilationOptions {
+ constants: &empty_constants,
+ zero_initialize_workgroup_memory: false,
+ vertex_pulling_transform: false,
+ },
+ },
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: "fs_main",
+ targets: &[Some(wgpu::ColorTargetState {
+ format: wgpu::TextureFormat::Rgba8UnormSrgb,
+ blend: Some(wgpu::BlendState {
+ color: wgpu::BlendComponent {
+ src_factor: wgpu::BlendFactor::One,
+ dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
+ operation: wgpu::BlendOperation::Add,
+ },
+ alpha: wgpu::BlendComponent {
+ src_factor: wgpu::BlendFactor::One,
+ dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
+ operation: wgpu::BlendOperation::Add,
+ },
+ }),
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ compilation_options: wgpu::PipelineCompilationOptions {
+ constants: &empty_constants,
+ zero_initialize_workgroup_memory: false,
+ vertex_pulling_transform: false,
+ },
+ }),
+ primitive: wgpu::PrimitiveState {
+ topology: wgpu::PrimitiveTopology::TriangleStrip,
+ strip_index_format: None,
+ front_face: wgpu::FrontFace::Ccw,
+ cull_mode: None,
+ unclipped_depth: false,
+ polygon_mode: wgpu::PolygonMode::Fill,
+ conservative: false,
+ },
+ depth_stencil: None,
+ multisample: wgpu::MultisampleState::default(),
+ multiview: None,
+ cache: None,
+ });
+
+ Self {
+ bind_group_layout,
+ render_pipeline,
+ }
+ }
+}
+
+#[repr(C)]
+#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
+struct WatermarkUniforms {
+ output_size: [f32; 2],
+ watermark_size: [f32; 2],
+ position: [f32; 2],
+ opacity: f32,
+ is_upgraded: f32,
+}
diff --git a/crates/rendering/src/shaders/watermark.wgsl b/crates/rendering/src/shaders/watermark.wgsl
new file mode 100644
index 000000000..425f3f882
--- /dev/null
+++ b/crates/rendering/src/shaders/watermark.wgsl
@@ -0,0 +1,53 @@
+struct VertexOutput {
+ @builtin(position) position: vec4<f32>,
+ @location(0) uv: vec2<f32>,
+};
+
+struct Uniforms {
+ output_size: vec2<f32>,
+ watermark_size: vec2<f32>,
+ position: vec2<f32>,
+ opacity: f32,
+ is_upgraded: f32,
+};
+
+@group(0) @binding(0) var watermark_texture: texture_2d<f32>;
+@group(0) @binding(1) var watermark_sampler: sampler;
+@group(0) @binding(2) var<uniform> uniforms: Uniforms;
+
+@vertex
+fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
+ var out: VertexOutput;
+
+ // Define vertices for two triangles forming a quad
+ var positions = array<vec2<f32>, 6>(
+ vec2<f32>(0.0, 0.0), // First triangle
+ vec2<f32>(1.0, 0.0),
+ vec2<f32>(0.0, 1.0),
+ vec2<f32>(0.0, 1.0), // Second triangle
+ vec2<f32>(1.0, 0.0),
+ vec2<f32>(1.0, 1.0)
+ );
+
+ let uv = positions[vertex_index];
+
+ // Calculate position in screen space
+ let size = uniforms.watermark_size;
+ let pos = uniforms.position;
+ let screen_pos = pos + (uv * size);
+
+ // Convert to clip space (-1 to 1)
+ let clip_pos = (screen_pos / uniforms.output_size) * 2.0 - 1.0;
+ out.position = vec4<f32>(clip_pos.x, -clip_pos.y, 0.0, 1.0);
+ out.uv = uv;
+
+ return out;
+}
+
+@fragment
+fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
+ let color = textureSample(watermark_texture, watermark_sampler, in.uv);
+ // Ensure the watermark is visible by using premultiplied alpha
+ let alpha = color.a * uniforms.opacity;
+ return vec4<f32>(color.rgb * alpha, alpha);
+}
\ No newline at end of file