Skip to content

How to use a VelloWindowRenderer with layershellev? #52

@bitbloxhub

Description

@bitbloxhub

This is my current attempt:

use std::sync::Arc;

use anyrender::WindowRenderer;
use anyrender_vello::VelloWindowRenderer;
use blitz::traits::shell::Viewport;
use blitz_dom::DocumentConfig;
use blitz_html::HtmlDocument;
use blitz_paint::paint_scene;
use layershellev::keyboard::{KeyCode, PhysicalKey};
use layershellev::reexport::*;
use layershellev::*;

fn main() {
	let ev = WindowState::<()>::new("Hello")
		.with_active()
		.with_size((0, 400))
		.with_layer(Layer::Top)
		.with_margin((20, 20, 100, 20))
		.with_anchor(Anchor::Bottom | Anchor::Left | Anchor::Right)
		.with_keyboard_interacivity(KeyboardInteractivity::Exclusive)
		.with_exclusive_zone(-1)
		.build()
		.unwrap();

	let mut document = HtmlDocument::from_html(
		r#"
		    <!DOCTYPE html>
    <html>
    <head>
        <style type="text/css">
            html, body {
                height: 100%;
                margin: 0;
            }

            body {
                display: grid;
                place-items: center;
            }
        </style>
    </head>
    <body>
        <svg viewBox="0 0 368 106" version="1.1" xmlns="http://www.w3.org/2000/svg" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
            <g>
                <path d="M131.548,97.488L131.548,8.369L144.939,8.369C150.903,8.369 155.656,8.831 159.196,9.755C162.774,10.678 165.795,12.236 168.258,14.43C170.759,16.7 172.741,19.528 174.203,22.915C175.703,26.339 176.454,29.802 176.454,33.304C176.454,39.692 174.01,45.098 169.123,49.523C173.856,51.139 177.589,53.967 180.321,58.008C183.091,62.01 184.477,66.666 184.477,71.976C184.477,78.941 182.014,84.828 177.089,89.638C174.126,92.601 170.797,94.66 167.103,95.814C163.063,96.93 158.003,97.488 151.923,97.488L131.548,97.488ZM144.997,46.637L149.21,46.637C154.213,46.637 157.878,45.531 160.206,43.318C162.534,41.106 163.698,37.845 163.698,33.535C163.698,29.341 162.505,26.156 160.119,23.982C157.734,21.808 154.27,20.721 149.73,20.721L144.997,20.721L144.997,46.637ZM144.997,84.847L153.308,84.847C159.388,84.847 163.852,83.654 166.699,81.269C169.701,78.691 171.201,75.42 171.201,71.456C171.201,67.608 169.758,64.376 166.872,61.76C164.063,59.181 159.042,57.892 151.808,57.892L144.997,57.892L144.997,84.847Z" style="fill-rule:nonzero;"/>
                <rect x="202.173" y="0" width="12.987" height="97.488" style="fill-rule:nonzero;"/>
                <path d="M247.806,41.269L247.806,97.488L234.819,97.488L234.819,41.269L247.806,41.269ZM232.857,17.893C232.857,15.623 233.684,13.66 235.338,12.006C236.993,10.351 238.975,9.524 241.284,9.524C243.631,9.524 245.632,10.351 247.286,12.006C248.941,13.622 249.768,15.603 249.768,17.951C249.768,20.298 248.941,22.299 247.286,23.953C245.67,25.608 243.689,26.435 241.341,26.435C238.994,26.435 236.993,25.608 235.338,23.953C233.684,22.299 232.857,20.279 232.857,17.893Z" style="fill-rule:nonzero;"/>
                <path d="M285.856,53.39L285.856,97.488L272.869,97.488L272.869,53.39L267.328,53.39L267.328,41.269L272.869,41.269L272.869,20.663L285.856,20.663L285.856,41.269L295.957,41.269L295.957,53.39L285.856,53.39Z" style="fill-rule:nonzero;"/>
                <path d="M331.64,85.251L365.059,85.251L365.059,97.488L305.897,97.488L342.318,53.39L313.631,53.39L313.631,41.269L368.003,41.269L331.64,85.251Z" style="fill-rule:nonzero;"/>
            </g>
            <g>
                <g>
                    <circle cx="53" cy="53" r="53" style="fill:rgb(1,99,63);"/>
                    <circle cx="53" cy="53" r="45.773" style="fill:rgb(0,118,114);"/>
                    <circle cx="53" cy="53" r="38.545" style="fill:rgb(62,149,147);"/>
                    <circle cx="53" cy="53" r="31.318" style="fill:rgb(252,176,64);"/>
                    <circle cx="53" cy="53" r="24.091" style="fill:rgb(233,86,41);"/>
                    <circle cx="53" cy="53" r="16.864" style="fill:rgb(230,29,50);"/>
                </g>
                <g>
                    <path d="M39.759,90.287C39.549,90.287 39.338,90.241 39.137,90.144C38.49,89.83 38.177,89.087 38.404,88.405L49.211,55.986L38.33,55.986C37.853,55.986 37.407,55.747 37.141,55.35C36.875,54.953 36.826,54.448 37.011,54.008L51.303,19.707C51.524,19.174 52.045,18.826 52.622,18.826L66.2,18.826C66.684,18.826 67.136,19.072 67.399,19.478C67.663,19.886 67.702,20.397 67.504,20.839L56.257,45.982L66.914,45.982C67.439,45.982 67.922,46.27 68.172,46.73C68.422,47.192 68.398,47.754 68.11,48.193L40.955,89.64C40.682,90.057 40.228,90.287 39.759,90.287Z" style="fill:rgb(244,232,210);fill-rule:nonzero;"/>
                </g>
            </g>
        </svg>
    </body>
    </html>"#,
		DocumentConfig {
			viewport: Some(Viewport::new(
				0,
				0,
				1.0,
				blitz::traits::shell::ColorScheme::Dark,
			)),
			..Default::default()
		},
	);

	let mut renderer = VelloWindowRenderer::new();
	renderer.resume(
		Arc::new(ev.main_window()) as Arc<dyn anyrender::WindowHandle>,
		0,
		0,
	);

	ev.running(move |event, _ev, _index| match event {
		LayerShellEvent::InitRequest => ReturnData::RequestBind,
		LayerShellEvent::BindProvide(_globals, _qh) => {
			ReturnData::RequestCompositor
		}
		LayerShellEvent::RequestMessages(DispatchMessage::RequestRefresh {
			width,
			height,
			scale_float: _,
			is_created: _,
		}) => {
			document.set_viewport(Viewport::new(
				*width,
				*height,
				1.0,
				blitz::traits::shell::ColorScheme::Dark,
			));
			document.resolve(0.0);
			renderer.render(|scene| {
				paint_scene(scene, document.as_ref(), 1.0, *width, *height, 0, 0);
			});
			// println!("{:?}", );
			ReturnData::None
		}
		LayerShellEvent::RequestMessages(DispatchMessage::KeyboardInput { event, .. }) => {
			if let PhysicalKey::Code(KeyCode::Escape) = event.physical_key {
				ReturnData::RequestExit
			} else {
				ReturnData::None
			}
		}
		_ => ReturnData::None,
	})
	.unwrap();
}

It worked before by using render_to_buffer and VelloCpuImageRenderer, but I want proper GPU acceleration.
Currently my compile errror is this:

    Checking keyboard2mouse v0.1.0 (/mnt/wd-blue-2tb/home/jonahgam/keyboard2mouse)
error[E0597]: `ev` does not live long enough
   --> src/main.rs:80:12
    |
 14 |     let ev = WindowState::<()>::new("Hello")
    |         -- binding `ev` declared here
...
 80 |         Arc::new(ev.main_window()) as Arc<dyn anyrender::WindowHandle>,
    |         ---------^^---------------------------------------------------
    |         |        |
    |         |        borrowed value does not live long enough
    |         coercion requires that `ev` is borrowed for `'static`
...
119 | }
    | - `ev` dropped here while still borrowed
    |
    = note: due to object lifetime defaults, `Arc<dyn anyrender::WindowHandle>` actually means `Arc<(dyn anyrender::WindowHandle + 'static)>`

error[E0505]: cannot move out of `ev` because it is borrowed
  --> src/main.rs:85:2
   |
14 |     let ev = WindowState::<()>::new("Hello")
   |         -- binding `ev` declared here
...
80 |         Arc::new(ev.main_window()) as Arc<dyn anyrender::WindowHandle>,
   |         --------------------------------------------------------------
   |         |        |
   |         |        borrow of `ev` occurs here
   |         coercion requires that `ev` is borrowed for `'static`
...
85 |     ev.running(move |event, _ev, _index| match event {
   |     ^^ move out of `ev` occurs here
   |
   = note: due to object lifetime defaults, `Arc<dyn anyrender::WindowHandle>` actually means `Arc<(dyn anyrender::WindowHandle + 'static)>`

Some errors have detailed explanations: E0505, E0597.
For more information about an error, try `rustc --explain E0505`.
error: could not compile `keyboard2mouse` (bin "keyboard2mouse") due to 2 previous errors

There's probably a really simple solution, but I can't figure out what that is.

Additional links:

@Decodetalkers also bringing you in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions