@@ -29,6 +29,7 @@ public unsafe partial class X11View : ViewLifetimeBase
2929 private const int ButtonRelease = 5 ;
3030 private const int MotionNotify = 6 ;
3131 private const int DestroyNotify = 17 ;
32+ private const int ConfigureNotify = 22 ;
3233 private const int ClientMessage = 33 ;
3334
3435 private const long KeyPressMask = 1L << 0 ;
@@ -148,6 +149,24 @@ private struct XDestroyWindowEvent
148149 public nuint window ;
149150 }
150151
152+ [ StructLayout ( LayoutKind . Sequential ) ]
153+ private struct XConfigureEvent
154+ {
155+ public int type ;
156+ public nuint serial ;
157+ public int send_event ;
158+ public IntPtr display ;
159+ public nuint @event ;
160+ public nuint window ;
161+ public int x ;
162+ public int y ;
163+ public int width ;
164+ public int height ;
165+ public int border_width ;
166+ public nuint above ;
167+ public int override_redirect ;
168+ }
169+
151170 [ StructLayout ( LayoutKind . Explicit ) ]
152171 private struct XClientMessageData
153172 {
@@ -179,6 +198,7 @@ private struct XEvent
179198 [ FieldOffset ( 0 ) ] public XButtonEvent xbutton ;
180199 [ FieldOffset ( 0 ) ] public XMotionEvent xmotion ;
181200 [ FieldOffset ( 0 ) ] public XClientMessageEvent xclient ;
201+ [ FieldOffset ( 0 ) ] public XConfigureEvent xconfigure ;
182202 [ FieldOffset ( 0 ) ] public XDestroyWindowEvent xdestroywindow ;
183203 }
184204
@@ -328,8 +348,8 @@ private static partial int XChangeProperty(
328348 private static partial int XFree ( IntPtr data ) ;
329349
330350 private readonly string _title ;
331- private readonly int _width ;
332- private readonly int _height ;
351+ private int _width ;
352+ private int _height ;
333353 private readonly float _dpi ;
334354 private readonly string ? _requestedDisplayName ;
335355 private readonly bool _borderless ;
@@ -495,23 +515,7 @@ protected override void OnCloseCore()
495515 _lvDisplay = null ;
496516 }
497517
498- if ( _xImage != IntPtr . Zero )
499- {
500- DestroyXImage ( ) ;
501- }
502-
503- if ( _frameBuffer != null )
504- {
505- NativeMemory . Free ( _frameBuffer ) ;
506- _frameBuffer = null ;
507- }
508-
509- if ( _drawBuffer != null )
510- {
511- NativeMemory . Free ( _drawBuffer ) ;
512- _drawBuffer = null ;
513- _drawBufferByteSize = 0 ;
514- }
518+ ReleaseBuffers ( ) ;
515519
516520 if ( _gc != IntPtr . Zero && _display != IntPtr . Zero )
517521 {
@@ -610,26 +614,7 @@ private void InitializeWindow()
610614 throw new InvalidOperationException ( "X11 图形上下文创建失败。" ) ;
611615 }
612616
613- AllocateBuffers ( ) ;
614-
615- var visual = XDefaultVisual ( _display , _screen ) ;
616- var depth = XDefaultDepth ( _display , _screen ) ;
617- _xImage = XCreateImage (
618- _display ,
619- visual ,
620- ( uint ) depth ,
621- ZPixmap ,
622- 0 ,
623- ( IntPtr ) _frameBuffer ,
624- ( uint ) _width ,
625- ( uint ) _height ,
626- 32 ,
627- _width * sizeof ( uint ) ) ;
628-
629- if ( _xImage == IntPtr . Zero )
630- {
631- throw new InvalidOperationException ( "X11 图像缓冲创建失败。" ) ;
632- }
617+ ResizeSurface ( _width , _height ) ;
633618
634619 XMapWindow ( _display , _window ) ;
635620 XFlush ( _display ) ;
@@ -700,20 +685,94 @@ private void InitializeLvgl()
700685
701686 private void AllocateBuffers ( )
702687 {
703- _frameBuffer = ( uint * ) NativeMemory . AllocZeroed ( ( nuint ) ( _width * _height ) , ( nuint ) sizeof ( uint ) ) ;
688+ _frameBuffer = ( uint * ) NativeMemory . AllocZeroed ( ( nuint ) DisplayBufferSizeHelper . GetPixelCount ( _width , _height ) , ( nuint ) sizeof ( uint ) ) ;
704689 if ( _frameBuffer == null )
705690 {
706691 throw new OutOfMemoryException ( "X11 framebuffer 分配失败。" ) ;
707692 }
708693
709- _drawBufferByteSize = checked ( ( uint ) ( _width * _height * sizeof ( ushort ) ) ) ;
694+ _drawBufferByteSize = DisplayBufferSizeHelper . GetRgb565DrawBufferByteSize ( _width , _height ) ;
710695 _drawBuffer = ( byte * ) NativeMemory . AllocZeroed ( ( nuint ) _drawBufferByteSize ) ;
711696 if ( _drawBuffer == null )
712697 {
713698 throw new OutOfMemoryException ( "LVGL draw buffer 分配失败。" ) ;
714699 }
715700 }
716701
702+ private void CreateXImage ( )
703+ {
704+ var visual = XDefaultVisual ( _display , _screen ) ;
705+ var depth = XDefaultDepth ( _display , _screen ) ;
706+ _xImage = XCreateImage (
707+ _display ,
708+ visual ,
709+ ( uint ) depth ,
710+ ZPixmap ,
711+ 0 ,
712+ ( IntPtr ) _frameBuffer ,
713+ ( uint ) _width ,
714+ ( uint ) _height ,
715+ 32 ,
716+ _width * sizeof ( uint ) ) ;
717+
718+ if ( _xImage == IntPtr . Zero )
719+ {
720+ throw new InvalidOperationException ( "X11 图像缓冲创建失败。" ) ;
721+ }
722+ }
723+
724+ private void ReleaseBuffers ( )
725+ {
726+ if ( _xImage != IntPtr . Zero )
727+ {
728+ DestroyXImage ( ) ;
729+ }
730+
731+ if ( _frameBuffer != null )
732+ {
733+ NativeMemory . Free ( _frameBuffer ) ;
734+ _frameBuffer = null ;
735+ }
736+
737+ if ( _drawBuffer != null )
738+ {
739+ NativeMemory . Free ( _drawBuffer ) ;
740+ _drawBuffer = null ;
741+ _drawBufferByteSize = 0 ;
742+ }
743+ }
744+
745+ private void ResizeSurface ( int width , int height )
746+ {
747+ if ( width <= 0 || height <= 0 )
748+ {
749+ return ;
750+ }
751+
752+ if ( width == _width && height == _height && _frameBuffer != null && _drawBuffer != null && _xImage != IntPtr . Zero )
753+ {
754+ return ;
755+ }
756+
757+ ReleaseBuffers ( ) ;
758+
759+ _width = width ;
760+ _height = height ;
761+ AllocateBuffers ( ) ;
762+ CreateXImage ( ) ;
763+
764+ if ( _lvDisplay != null )
765+ {
766+ lv_display_set_resolution ( _lvDisplay , _width , _height ) ;
767+ lv_display_set_buffers ( _lvDisplay , _drawBuffer , null , _drawBufferByteSize , LV_DISPLAY_RENDER_MODE_FULL ) ;
768+ }
769+
770+ if ( RootObject != null )
771+ {
772+ lv_obj_invalidate ( RootObject ) ;
773+ }
774+ }
775+
717776 private void PollEvents ( )
718777 {
719778 while ( XPending ( _display ) > 0 )
@@ -787,6 +846,9 @@ private void PollEvents()
787846 _running = false ;
788847 }
789848 break ;
849+ case ConfigureNotify :
850+ ResizeSurface ( ev . xconfigure . width , ev . xconfigure . height ) ;
851+ break ;
790852 case DestroyNotify :
791853 _running = false ;
792854 break ;
0 commit comments