55use crate :: webgl_thread:: { WebGLThread , WebGLThreadInit } ;
66use canvas_traits:: webgl:: { WebGLContextId , WebGLMsg , WebGLSender , WebGLThreads } ;
77use canvas_traits:: webgl:: { WebVRRenderHandler , webgl_channel} ;
8+ use canvas_traits:: webgl:: SwapChainId ;
89use euclid:: default:: Size2D ;
910use fnv:: FnvHashMap ;
1011use gleam:: gl;
@@ -114,7 +115,7 @@ struct WebGLExternalImages {
114115 context : Rc < RefCell < Context > > ,
115116 webrender_gl : Rc < dyn gl:: Gl > ,
116117 swap_chains : SwapChains ,
117- locked_front_buffers : FnvHashMap < WebGLContextId , SurfaceTexture > ,
118+ locked_front_buffers : FnvHashMap < SwapChainId , SurfaceTexture > ,
118119 sendable : SendableWebGLExternalImages ,
119120}
120121
@@ -136,50 +137,36 @@ impl WebGLExternalImages {
136137 }
137138}
138139
139- impl WebrenderExternalImageApi for WebGLExternalImages {
140- fn lock ( & mut self , id : u64 ) -> ( u32 , Size2D < i32 > ) {
141- let id = WebGLContextId ( id) ;
142-
140+ impl WebGLExternalImages {
141+ fn lock_swap_chain ( & mut self , id : SwapChainId ) -> Option < ( u32 , Size2D < i32 > ) > {
143142 let mut swap_chains = self . swap_chains . lock ( ) ;
144- let mut front_buffer = None ;
145- if let Some ( ref mut swap_chain) = swap_chains. get_mut ( & id) {
146- front_buffer = swap_chain. pending_surface . take ( ) ;
147- if front_buffer. is_none ( ) {
148- println ! ( "*** no surface ready, presenting last one" ) ;
149- front_buffer = swap_chain. presented_surfaces . pop ( ) ;
150- }
151- }
143+ let swap_chain = swap_chains. get_mut ( & id) ?;
144+ let front_buffer = swap_chain. pending_surface . take ( ) . or_else ( || {
145+ println ! ( "*** no surface ready, presenting last one" ) ;
146+ swap_chain. presented_surfaces . pop ( )
147+ } ) ?;
152148
153- let ( mut gl_texture, mut size) = ( 0 , Size2D :: new ( 0 , 0 ) ) ;
154- if let Some ( front_buffer) = front_buffer {
155- let mut context = self . context . borrow_mut ( ) ;
156- size = front_buffer. size ( ) ;
157- let front_buffer_texture = self . device
158- . create_surface_texture ( & mut * context, front_buffer)
159- . unwrap ( ) ;
160- gl_texture = front_buffer_texture. gl_texture ( ) ;
161- self . locked_front_buffers . insert ( id, front_buffer_texture) ;
162- }
163-
164- ( gl_texture, size)
165- }
149+ let mut context = self . context . borrow_mut ( ) ;
150+ let size = front_buffer. size ( ) ;
151+ let front_buffer_texture = self . device
152+ . create_surface_texture ( & mut * context, front_buffer)
153+ . unwrap ( ) ;
154+ let gl_texture = front_buffer_texture. gl_texture ( ) ;
166155
167- fn unlock ( & mut self , id : u64 ) {
168- self . sendable . unlock ( id as usize ) ;
156+ self . locked_front_buffers . insert ( id, front_buffer_texture) ;
169157
170- let id = WebGLContextId ( id) ;
171- let locked_front_buffer = match self . locked_front_buffers . remove ( & id) {
172- None => return ,
173- Some ( locked_front_buffer) => locked_front_buffer,
174- } ;
158+ Some ( ( gl_texture, size) )
159+ }
175160
161+ fn unlock_swap_chain ( & mut self , id : SwapChainId ) -> Option < ( ) > {
162+ let locked_front_buffer = self . locked_front_buffers . remove ( & id) ?;
176163 let mut context = self . context . borrow_mut ( ) ;
177164 let locked_front_buffer = self . device
178165 . destroy_surface_texture ( & mut * context, locked_front_buffer)
179166 . unwrap ( ) ;
180167
181168 self . swap_chains . push_presented_surface ( id, locked_front_buffer) ;
182-
169+ Some ( ( ) )
183170 /*
184171 let mut front_buffer_slot = self.swap_chains.get(id);
185172 let mut locked_front_buffer_slot = front_buffer_slot.lock();
@@ -194,9 +181,21 @@ impl WebrenderExternalImageApi for WebGLExternalImages {
194181 }
195182}
196183
184+ impl WebrenderExternalImageApi for WebGLExternalImages {
185+ fn lock ( & mut self , id : u64 ) -> ( u32 , Size2D < i32 > ) {
186+ let id = SwapChainId :: Context ( WebGLContextId ( id) ) ;
187+ self . lock_swap_chain ( id) . unwrap_or_default ( )
188+ }
189+
190+ fn unlock ( & mut self , id : u64 ) {
191+ let id = SwapChainId :: Context ( WebGLContextId ( id) ) ;
192+ self . unlock_swap_chain ( id) ;
193+ }
194+ }
195+
197196#[ derive( Clone ) ]
198197pub struct SwapChains {
199- table : Arc < Mutex < FnvHashMap < WebGLContextId , SwapChain > > > ,
198+ table : Arc < Mutex < FnvHashMap < SwapChainId , SwapChain > > > ,
200199}
201200
202201pub ( crate ) struct SwapChain {
@@ -211,9 +210,9 @@ impl SwapChains {
211210 }
212211 }
213212
214- fn push_presented_surface ( & self , context_id : WebGLContextId , surface : Surface ) {
213+ fn push_presented_surface ( & self , swap_id : SwapChainId , surface : Surface ) {
215214 let mut table = self . lock ( ) ;
216- match table. entry ( context_id ) {
215+ match table. entry ( swap_id ) {
217216 Entry :: Vacant ( vacant_entry) => {
218217 vacant_entry. insert ( SwapChain {
219218 pending_surface : None ,
@@ -226,7 +225,7 @@ impl SwapChains {
226225 }
227226 }
228227
229- pub ( crate ) fn lock ( & self ) -> MutexGuard < FnvHashMap < WebGLContextId , SwapChain > > {
228+ pub ( crate ) fn lock ( & self ) -> MutexGuard < FnvHashMap < SwapChainId , SwapChain > > {
230229 self . table . lock ( ) . unwrap ( )
231230 }
232231}
0 commit comments