@@ -2,7 +2,7 @@ use core::fmt;
2
2
3
3
use anyhow:: { Context , Result } ;
4
4
use ironrdp_acceptor:: DesktopSize ;
5
- use ironrdp_core :: { Encode , WriteCursor } ;
5
+ use ironrdp_pdu :: encode_vec ;
6
6
use ironrdp_pdu:: fast_path:: UpdateCode ;
7
7
use ironrdp_pdu:: geometry:: ExclusiveRectangle ;
8
8
use ironrdp_pdu:: pointer:: { ColorPointerAttribute , Point16 , PointerAttribute , PointerPositionAttribute } ;
@@ -30,7 +30,6 @@ pub(crate) struct UpdateEncoder {
30
30
desktop_size : DesktopSize ,
31
31
// FIXME: draw updates on the framebuffer
32
32
framebuffer : Option < Framebuffer > ,
33
- pdu_encoder : PduEncoder ,
34
33
bitmap_updater : BitmapUpdater ,
35
34
}
36
35
@@ -44,7 +43,6 @@ impl fmt::Debug for UpdateEncoder {
44
43
45
44
impl UpdateEncoder {
46
45
pub ( crate ) fn new ( desktop_size : DesktopSize , surface_flags : CmdFlags , remotefx : Option < ( EntropyBits , u8 ) > ) -> Self {
47
- let pdu_encoder = PduEncoder :: new ( ) ;
48
46
let bitmap_updater = if !surface_flags. contains ( CmdFlags :: SET_SURFACE_BITS ) {
49
47
BitmapUpdater :: Bitmap ( BitmapHandler :: new ( ) )
50
48
} else if remotefx. is_some ( ) {
@@ -57,7 +55,6 @@ impl UpdateEncoder {
57
55
Self {
58
56
desktop_size,
59
57
framebuffer : None ,
60
- pdu_encoder,
61
58
bitmap_updater,
62
59
}
63
60
}
@@ -66,7 +63,8 @@ impl UpdateEncoder {
66
63
self . desktop_size = size;
67
64
}
68
65
69
- pub ( crate ) fn rgba_pointer ( & mut self , ptr : RGBAPointer ) -> Result < UpdateFragmenter < ' _ > > {
66
+ #[ allow( clippy:: unused_self) ]
67
+ pub ( crate ) fn rgba_pointer ( & mut self , ptr : RGBAPointer ) -> Result < UpdateFragmenter > {
70
68
let xor_mask = ptr. data ;
71
69
72
70
let hot_spot = Point16 {
@@ -85,11 +83,11 @@ impl UpdateEncoder {
85
83
xor_bpp : 32 ,
86
84
color_pointer,
87
85
} ;
88
- let buf = self . pdu_encoder . encode ( ptr) ?;
89
- Ok ( UpdateFragmenter :: new ( UpdateCode :: NewPointer , buf) )
86
+ Ok ( UpdateFragmenter :: new ( UpdateCode :: NewPointer , encode_vec ( & ptr) ?) )
90
87
}
91
88
92
- pub ( crate ) fn color_pointer ( & mut self , ptr : ColorPointer ) -> Result < UpdateFragmenter < ' _ > > {
89
+ #[ allow( clippy:: unused_self) ]
90
+ pub ( crate ) fn color_pointer ( & mut self , ptr : ColorPointer ) -> Result < UpdateFragmenter > {
93
91
let hot_spot = Point16 {
94
92
x : ptr. hot_x ,
95
93
y : ptr. hot_y ,
@@ -102,27 +100,26 @@ impl UpdateEncoder {
102
100
xor_mask : & ptr. xor_mask ,
103
101
and_mask : & ptr. and_mask ,
104
102
} ;
105
- let buf = self . pdu_encoder . encode ( ptr) ?;
106
- Ok ( UpdateFragmenter :: new ( UpdateCode :: ColorPointer , buf) )
103
+ Ok ( UpdateFragmenter :: new ( UpdateCode :: ColorPointer , encode_vec ( & ptr) ?) )
107
104
}
108
105
109
106
#[ allow( clippy:: unused_self) ]
110
- pub ( crate ) fn default_pointer ( & mut self ) -> Result < UpdateFragmenter < ' _ > > {
111
- Ok ( UpdateFragmenter :: new ( UpdateCode :: DefaultPointer , & [ ] ) )
107
+ pub ( crate ) fn default_pointer ( & mut self ) -> Result < UpdateFragmenter > {
108
+ Ok ( UpdateFragmenter :: new ( UpdateCode :: DefaultPointer , vec ! [ ] ) )
112
109
}
113
110
114
111
#[ allow( clippy:: unused_self) ]
115
- pub ( crate ) fn hide_pointer ( & mut self ) -> Result < UpdateFragmenter < ' _ > > {
116
- Ok ( UpdateFragmenter :: new ( UpdateCode :: HiddenPointer , & [ ] ) )
112
+ pub ( crate ) fn hide_pointer ( & mut self ) -> Result < UpdateFragmenter > {
113
+ Ok ( UpdateFragmenter :: new ( UpdateCode :: HiddenPointer , vec ! [ ] ) )
117
114
}
118
115
119
- pub ( crate ) fn pointer_position ( & mut self , pos : PointerPositionAttribute ) -> Result < UpdateFragmenter < ' _ > > {
120
- let buf = self . pdu_encoder . encode ( pos ) ? ;
121
- Ok ( UpdateFragmenter :: new ( UpdateCode :: PositionPointer , buf ) )
116
+ # [ allow ( clippy :: unused_self ) ]
117
+ pub ( crate ) fn pointer_position ( & mut self , pos : PointerPositionAttribute ) -> Result < UpdateFragmenter > {
118
+ Ok ( UpdateFragmenter :: new ( UpdateCode :: PositionPointer , encode_vec ( & pos ) ? ) )
122
119
}
123
120
124
- pub ( crate ) fn bitmap ( & mut self , bitmap : BitmapUpdate ) -> Result < UpdateFragmenter < ' _ > > {
125
- let res = self . bitmap_updater . handle ( & bitmap, & mut self . pdu_encoder ) ;
121
+ pub ( crate ) fn bitmap ( & mut self , bitmap : BitmapUpdate ) -> Result < UpdateFragmenter > {
122
+ let res = self . bitmap_updater . handle ( & bitmap) ;
126
123
if bitmap. x == 0
127
124
&& bitmap. y == 0
128
125
&& bitmap. width . get ( ) == self . desktop_size . width
@@ -135,10 +132,6 @@ impl UpdateEncoder {
135
132
}
136
133
res
137
134
}
138
-
139
- pub ( crate ) fn fragmenter_from_owned ( & self , res : UpdateFragmenterOwned ) -> UpdateFragmenter < ' _ > {
140
- UpdateFragmenter :: from_owned ( res, & self . pdu_encoder . buffer )
141
- }
142
135
}
143
136
144
137
#[ derive( Debug ) ]
@@ -149,30 +142,30 @@ enum BitmapUpdater {
149
142
}
150
143
151
144
impl BitmapUpdater {
152
- fn handle < ' a > ( & mut self , bitmap : & BitmapUpdate , encoder : & ' a mut PduEncoder ) -> Result < UpdateFragmenter < ' a > > {
145
+ fn handle ( & mut self , bitmap : & BitmapUpdate ) -> Result < UpdateFragmenter > {
153
146
match self {
154
- Self :: None ( up) => up. handle ( bitmap, encoder ) ,
155
- Self :: Bitmap ( up) => up. handle ( bitmap, encoder ) ,
156
- Self :: RemoteFx ( up) => up. handle ( bitmap, encoder ) ,
147
+ Self :: None ( up) => up. handle ( bitmap) ,
148
+ Self :: Bitmap ( up) => up. handle ( bitmap) ,
149
+ Self :: RemoteFx ( up) => up. handle ( bitmap) ,
157
150
}
158
151
}
159
152
}
160
153
161
154
trait BitmapUpdateHandler {
162
- fn handle < ' a > ( & mut self , bitmap : & BitmapUpdate , encoder : & ' a mut PduEncoder ) -> Result < UpdateFragmenter < ' a > > ;
155
+ fn handle ( & mut self , bitmap : & BitmapUpdate ) -> Result < UpdateFragmenter > ;
163
156
}
164
157
165
158
#[ derive( Debug ) ]
166
159
struct NoneHandler ;
167
160
168
161
impl BitmapUpdateHandler for NoneHandler {
169
- fn handle < ' a > ( & mut self , bitmap : & BitmapUpdate , encoder : & ' a mut PduEncoder ) -> Result < UpdateFragmenter < ' a > > {
162
+ fn handle ( & mut self , bitmap : & BitmapUpdate ) -> Result < UpdateFragmenter > {
170
163
let stride = usize:: from ( bitmap. format . bytes_per_pixel ( ) ) * usize:: from ( bitmap. width . get ( ) ) ;
171
164
let mut data = Vec :: with_capacity ( stride * usize:: from ( bitmap. height . get ( ) ) ) ;
172
165
for row in bitmap. data . chunks ( bitmap. stride ) . rev ( ) {
173
166
data. extend_from_slice ( & row[ ..stride] ) ;
174
167
}
175
- encoder . set_surface ( bitmap, CodecId :: None as u8 , & data)
168
+ set_surface ( bitmap, CodecId :: None as u8 , & data)
176
169
}
177
170
}
178
171
@@ -195,13 +188,14 @@ impl BitmapHandler {
195
188
}
196
189
197
190
impl BitmapUpdateHandler for BitmapHandler {
198
- fn handle < ' a > ( & mut self , bitmap : & BitmapUpdate , encoder : & ' a mut PduEncoder ) -> Result < UpdateFragmenter < ' a > > {
191
+ fn handle ( & mut self , bitmap : & BitmapUpdate ) -> Result < UpdateFragmenter > {
192
+ let mut buffer = vec ! [ 0 ; bitmap. data. len( ) * 2 ] ; // TODO: estimate bitmap encoded size
199
193
let len = loop {
200
- match self . bitmap . encode ( bitmap, encoder . buffer . as_mut_slice ( ) ) {
194
+ match self . bitmap . encode ( bitmap, buffer. as_mut_slice ( ) ) {
201
195
Err ( e) => match e. kind ( ) {
202
196
ironrdp_core:: EncodeErrorKind :: NotEnoughBytes { .. } => {
203
- encoder . buffer . resize ( encoder . buffer . len ( ) * 2 , 0 ) ;
204
- debug ! ( "encoder buffer resized to: {}" , encoder . buffer. len( ) * 2 ) ;
197
+ buffer. resize ( buffer. len ( ) * 2 , 0 ) ;
198
+ debug ! ( "encoder buffer resized to: {}" , buffer. len( ) * 2 ) ;
205
199
}
206
200
207
201
_ => Err ( e) . context ( "bitmap encode error" ) ?,
@@ -210,7 +204,8 @@ impl BitmapUpdateHandler for BitmapHandler {
210
204
}
211
205
} ;
212
206
213
- Ok ( UpdateFragmenter :: new ( UpdateCode :: Bitmap , & encoder. buffer [ ..len] ) )
207
+ buffer. truncate ( len) ;
208
+ Ok ( UpdateFragmenter :: new ( UpdateCode :: Bitmap , buffer) )
214
209
}
215
210
}
216
211
@@ -230,7 +225,7 @@ impl RemoteFxHandler {
230
225
}
231
226
232
227
impl BitmapUpdateHandler for RemoteFxHandler {
233
- fn handle < ' a > ( & mut self , bitmap : & BitmapUpdate , encoder : & ' a mut PduEncoder ) -> Result < UpdateFragmenter < ' a > > {
228
+ fn handle ( & mut self , bitmap : & BitmapUpdate ) -> Result < UpdateFragmenter > {
234
229
let mut buffer = vec ! [ 0 ; bitmap. data. len( ) ] ;
235
230
let len = loop {
236
231
match self . remotefx . encode ( bitmap, buffer. as_mut_slice ( ) ) {
@@ -246,59 +241,29 @@ impl BitmapUpdateHandler for RemoteFxHandler {
246
241
}
247
242
} ;
248
243
249
- encoder . set_surface ( bitmap, self . codec_id , & buffer[ ..len] )
244
+ set_surface ( bitmap, self . codec_id , & buffer[ ..len] )
250
245
}
251
246
}
252
247
253
- struct PduEncoder {
254
- buffer : Vec < u8 > ,
255
- }
256
-
257
- impl PduEncoder {
258
- fn new ( ) -> Self {
259
- Self { buffer : vec ! [ 0 ; 16384 ] }
260
- }
261
-
262
- fn encode ( & mut self , pdu : impl Encode ) -> Result < & [ u8 ] > {
263
- let pos = loop {
264
- let mut cursor = WriteCursor :: new ( self . buffer . as_mut_slice ( ) ) ;
265
- match pdu. encode ( & mut cursor) {
266
- Err ( e) => match e. kind ( ) {
267
- ironrdp_core:: EncodeErrorKind :: NotEnoughBytes { .. } => {
268
- self . buffer . resize ( self . buffer . len ( ) * 2 , 0 ) ;
269
- debug ! ( "encoder buffer resized to: {}" , self . buffer. len( ) * 2 ) ;
270
- }
271
-
272
- _ => Err ( e) . context ( "PDU encode error" ) ?,
273
- } ,
274
- Ok ( ( ) ) => break cursor. pos ( ) ,
275
- }
276
- } ;
277
-
278
- Ok ( & self . buffer [ ..pos] )
279
- }
280
-
281
- fn set_surface ( & mut self , bitmap : & BitmapUpdate , codec_id : u8 , data : & [ u8 ] ) -> Result < UpdateFragmenter < ' _ > > {
282
- let destination = ExclusiveRectangle {
283
- left : bitmap. x ,
284
- top : bitmap. y ,
285
- right : bitmap. x + bitmap. width . get ( ) ,
286
- bottom : bitmap. y + bitmap. height . get ( ) ,
287
- } ;
288
- let extended_bitmap_data = ExtendedBitmapDataPdu {
289
- bpp : bitmap. format . bytes_per_pixel ( ) * 8 ,
290
- width : bitmap. width . get ( ) ,
291
- height : bitmap. height . get ( ) ,
292
- codec_id,
293
- header : None ,
294
- data,
295
- } ;
296
- let pdu = SurfaceBitsPdu {
297
- destination,
298
- extended_bitmap_data,
299
- } ;
300
- let cmd = SurfaceCommand :: SetSurfaceBits ( pdu) ;
301
- let buf = self . encode ( cmd) ?;
302
- Ok ( UpdateFragmenter :: new ( UpdateCode :: SurfaceCommands , buf) )
303
- }
248
+ fn set_surface ( bitmap : & BitmapUpdate , codec_id : u8 , data : & [ u8 ] ) -> Result < UpdateFragmenter > {
249
+ let destination = ExclusiveRectangle {
250
+ left : bitmap. x ,
251
+ top : bitmap. y ,
252
+ right : bitmap. x + bitmap. width . get ( ) ,
253
+ bottom : bitmap. y + bitmap. height . get ( ) ,
254
+ } ;
255
+ let extended_bitmap_data = ExtendedBitmapDataPdu {
256
+ bpp : bitmap. format . bytes_per_pixel ( ) * 8 ,
257
+ width : bitmap. width . get ( ) ,
258
+ height : bitmap. height . get ( ) ,
259
+ codec_id,
260
+ header : None ,
261
+ data,
262
+ } ;
263
+ let pdu = SurfaceBitsPdu {
264
+ destination,
265
+ extended_bitmap_data,
266
+ } ;
267
+ let cmd = SurfaceCommand :: SetSurfaceBits ( pdu) ;
268
+ Ok ( UpdateFragmenter :: new ( UpdateCode :: SurfaceCommands , encode_vec ( & cmd) ?) )
304
269
}
0 commit comments