11// Copyright © SixtyFPS GmbH <info@slint.dev>
22// SPDX-License-Identifier: MIT
33use crate :: Color ;
4- use dioxus_native:: { CustomPaintCtx , CustomPaintSource , DeviceHandle , TextureHandle } ;
4+ use anyrender:: { PaintRef , PaintScene , RenderContext , ResourceId } ;
5+ use blitz_dom:: Widget ;
6+ use blitz_dom:: node:: ComputedStyles ;
7+ use dioxus_native:: DeviceHandle ;
8+ use peniko:: kurbo:: { Affine , Rect } ;
9+ use peniko:: { Fill , ImageBrush , ImageSampler } ;
510use std:: sync:: mpsc:: { Receiver , Sender , channel} ;
611use std:: time:: Instant ;
712use wgpu:: {
@@ -12,36 +17,69 @@ use wgpu::{
1217 TextureUsages , TextureViewDescriptor , VertexState ,
1318} ;
1419
15- pub struct DemoPaintSource {
20+ pub struct DemoWidget {
1621 state : DemoRendererState ,
1722 start_time : std:: time:: Instant ,
1823 tx : Sender < DemoMessage > ,
1924 rx : Receiver < DemoMessage > ,
2025 color : Color ,
2126}
2227
23- impl CustomPaintSource for DemoPaintSource {
24- fn resume ( & mut self , device_handle : & DeviceHandle ) {
25- // Extract device and queue from device_handle
26- let device = & device_handle. device ;
27- let queue = & device_handle. queue ;
28- let active_state = ActiveDemoRenderer :: new ( device, queue) ;
29- self . state = DemoRendererState :: Active ( Box :: new ( active_state) ) ;
28+ impl Widget for DemoWidget {
29+ fn connected ( & mut self ) { }
30+ fn disconnected ( & mut self ) { }
31+ fn can_create_surfaces ( & mut self , render_ctx : & mut dyn anyrender:: RenderContext ) {
32+ if let Some ( renderer_specific_context) = render_ctx. renderer_specific_context ( ) {
33+ if let Ok ( device_handle) = renderer_specific_context. downcast :: < DeviceHandle > ( ) {
34+ let active_state =
35+ ActiveDemoRenderer :: new ( & device_handle. device , & device_handle. queue ) ;
36+ self . state = DemoRendererState :: Active ( Box :: new ( active_state) ) ;
37+ } else {
38+ println ! ( "WARNING: Running WGPU example with non-wgpu rendering backend" ) ;
39+ }
40+ } else {
41+ println ! ( "WARNING: Rendering backend returned no context!" ) ;
42+ }
3043 }
31-
32- fn suspend ( & mut self ) {
44+ fn destroy_surfaces ( & mut self ) {
3345 self . state = DemoRendererState :: Suspended ;
3446 }
3547
36- fn render (
48+ fn handle_event ( & mut self , event : & blitz_traits:: events:: UiEvent ) {
49+ let _ = event;
50+ }
51+
52+ fn paint (
3753 & mut self ,
38- ctx : CustomPaintCtx < ' _ > ,
54+ render_ctx : & mut dyn anyrender:: RenderContext ,
55+ _styles : & ComputedStyles ,
3956 width : u32 ,
4057 height : u32 ,
4158 _scale : f64 ,
42- ) -> Option < TextureHandle > {
59+ ) -> anyrender:: Scene {
60+ let mut scene = anyrender:: Scene :: new ( ) ;
61+
62+ // if matches!(self.state, DemoRendererState::Suspended) {
63+ // self.can_create_surfaces(render_ctx);
64+ // }
65+
4366 self . process_messages ( ) ;
44- self . render ( ctx, width, height)
67+ if let Some ( resource_id) = self . render ( render_ctx, width, height) {
68+ scene. fill (
69+ Fill :: NonZero ,
70+ Affine :: IDENTITY ,
71+ PaintRef :: Resource ( ImageBrush {
72+ image : resource_id,
73+ sampler : ImageSampler :: default ( ) ,
74+ } ) ,
75+ None ,
76+ & Rect :: from_origin_size ( ( 0.0 , 0.0 ) , ( width as f64 , height as f64 ) ) ,
77+ ) ;
78+ } else {
79+ println ! ( "WARNING: render returned None" ) ;
80+ }
81+
82+ scene
4583 }
4684}
4785
@@ -58,7 +96,7 @@ enum DemoRendererState {
5896#[ derive( Clone ) ]
5997struct TextureAndHandle {
6098 texture : Texture ,
61- handle : TextureHandle ,
99+ handle : ResourceId ,
62100}
63101
64102struct ActiveDemoRenderer {
@@ -69,7 +107,7 @@ struct ActiveDemoRenderer {
69107 next_texture : Option < TextureAndHandle > ,
70108}
71109
72- impl DemoPaintSource {
110+ impl DemoWidget {
73111 pub fn new ( ) -> Self {
74112 let ( tx, rx) = channel ( ) ;
75113 Self :: with_channel ( tx, rx)
@@ -102,10 +140,10 @@ impl DemoPaintSource {
102140
103141 fn render (
104142 & mut self ,
105- ctx : CustomPaintCtx < ' _ > ,
143+ ctx : & mut dyn RenderContext ,
106144 width : u32 ,
107145 height : u32 ,
108- ) -> Option < TextureHandle > {
146+ ) -> Option < ResourceId > {
109147 if width == 0 || height == 0 {
110148 return None ;
111149 }
@@ -163,32 +201,34 @@ impl ActiveDemoRenderer {
163201
164202 pub ( crate ) fn render (
165203 & mut self ,
166- mut ctx : CustomPaintCtx < ' _ > ,
204+ ctx : & mut dyn RenderContext ,
167205 light : [ f32 ; 3 ] ,
168206 width : u32 ,
169207 height : u32 ,
170208 start_time : & Instant ,
171- ) -> Option < TextureHandle > {
209+ ) -> Option < ResourceId > {
172210 // If "next texture" size doesn't match specified size then unregister and drop texture
173211 if let Some ( next) = & self . next_texture
174212 && ( next. texture . width ( ) != width || next. texture . height ( ) != height)
175213 {
176- ctx. unregister_texture ( self . next_texture . take ( ) . unwrap ( ) . handle ) ;
214+ ctx. unregister_resource ( self . next_texture . take ( ) . unwrap ( ) . handle ) ;
177215 }
178216
179217 // If there is no "next texture" then create one and register it.
180218 let texture_and_handle = match & self . next_texture {
181219 Some ( next) => next,
182220 None => {
183221 let texture = create_texture ( & self . device , width, height) ;
184- let handle = ctx. register_texture ( texture. clone ( ) ) ;
222+ let handle = ctx
223+ . try_register_custom_resource ( Box :: new ( texture. clone ( ) ) )
224+ . unwrap ( ) ;
185225 self . next_texture = Some ( TextureAndHandle { texture, handle } ) ;
186226 self . next_texture . as_ref ( ) . unwrap ( )
187227 }
188228 } ;
189229
190230 let next_texture = & texture_and_handle. texture ;
191- let next_texture_handle = texture_and_handle. handle . clone ( ) ;
231+ let next_texture_handle = texture_and_handle. handle ;
192232
193233 let elapsed: f32 = start_time. elapsed ( ) . as_millis ( ) as f32 / 500. ;
194234 let [ light_red, light_green, light_blue] = light;
0 commit comments