44
55using System ;
66using System . IO . MemoryMappedFiles ;
7- using System . Runtime . InteropServices ;
87using System . Windows ;
98using System . Windows . Controls ;
109using System . Windows . Interop ;
11- using System . Windows . Media ;
1210using System . Windows . Threading ;
1311using Rect = CefSharp . Structs . Rect ;
1412
@@ -20,28 +18,8 @@ namespace CefSharp.Wpf.Rendering.Experimental
2018 /// when the size increases
2119 /// </summary>
2220 /// <seealso cref="CefSharp.Wpf.IRenderHandler" />
23- public class IncreaseBufferInteropRenderHandler : IRenderHandler
21+ public class IncreaseBufferInteropRenderHandler : AbstractRenderHandler
2422 {
25- [ DllImport ( "kernel32.dll" , EntryPoint = "CopyMemory" , SetLastError = false ) ]
26- private static extern void CopyMemory ( IntPtr dest , IntPtr src , uint count ) ;
27-
28- /// <summary>
29- /// The pixel format
30- /// </summary>
31- private static readonly PixelFormat PixelFormat = PixelFormats . Pbgra32 ;
32- private static int BytesPerPixel = PixelFormat . BitsPerPixel / 8 ;
33-
34- private object lockObject = new object ( ) ;
35-
36- private Size viewSize ;
37- private Size popupSize ;
38- private DispatcherPriority dispatcherPriority ;
39-
40- private MemoryMappedFile viewMemoryMappedFile ;
41- private MemoryMappedFile popupMemoryMappedFile ;
42- private MemoryMappedViewAccessor viewMemoryMappedViewAccessor ;
43- private MemoryMappedViewAccessor popupMemoryMappedViewAccessor ;
44-
4523 /// <summary>
4624 /// Initializes a new instance of the <see cref="InteropBitmapRenderHandler"/> class.
4725 /// </summary>
@@ -51,46 +29,8 @@ public IncreaseBufferInteropRenderHandler(DispatcherPriority dispatcherPriority
5129 this . dispatcherPriority = dispatcherPriority ;
5230 }
5331
54- /// <summary>
55- /// Dispose
56- /// </summary>
57- public void Dispose ( )
58- {
59- ReleaseMemoryMappedView ( ref popupMemoryMappedFile , ref popupMemoryMappedViewAccessor ) ;
60- ReleaseMemoryMappedView ( ref viewMemoryMappedFile , ref viewMemoryMappedViewAccessor ) ;
61- }
62-
63- /// <summary>
64- /// Called when an element has been rendered to the shared texture handle.
65- /// This method is only called when <see cref="IWindowInfo.SharedTextureEnabled"/> is set to true
66- /// </summary>
67- /// <param name="isPopup">indicates whether the element is the view or the popup widget.</param>
68- /// <param name="dirtyRect">contains the set of rectangles in pixel coordinates that need to be repainted</param>
69- /// <param name="sharedHandle">is the handle for a D3D11 Texture2D that can be accessed via ID3D11Device using the OpenSharedResource method.</param>
70- void IRenderHandler . OnAcceleratedPaint ( bool isPopup , Rect dirtyRect , IntPtr sharedHandle )
71- {
72- //NOT USED
73- }
74-
75- void IRenderHandler . OnPaint ( bool isPopup , Rect dirtyRect , IntPtr buffer , int width , int height , Image image )
76- {
77- if ( isPopup )
78- {
79- CreateOrUpdateBitmap ( isPopup , dirtyRect , buffer , width , height , image , ref popupSize , ref popupMemoryMappedFile , ref popupMemoryMappedViewAccessor ) ;
80- }
81- else
82- {
83- CreateOrUpdateBitmap ( isPopup , dirtyRect , buffer , width , height , image , ref viewSize , ref viewMemoryMappedFile , ref viewMemoryMappedViewAccessor ) ;
84- }
85- }
86-
87- private void CreateOrUpdateBitmap ( bool isPopup , Rect dirtyRect , IntPtr buffer , int width , int height , Image image , ref Size currentSize , ref MemoryMappedFile mappedFile , ref MemoryMappedViewAccessor viewAccessor )
32+ protected override void CreateOrUpdateBitmap ( bool isPopup , Rect dirtyRect , IntPtr buffer , int width , int height , Image image , ref Size currentSize , ref MemoryMappedFile mappedFile , ref MemoryMappedViewAccessor viewAccessor )
8833 {
89- if ( image . Dispatcher . HasShutdownStarted )
90- {
91- return ;
92- }
93-
9434 var createNewBitmap = false ;
9535
9636 lock ( lockObject )
@@ -146,33 +86,15 @@ private void CreateOrUpdateBitmap(bool isPopup, Rect dirtyRect, IntPtr buffer, i
14686 var bitmap = ( InteropBitmap ) Imaging . CreateBitmapSourceFromMemorySection ( backBufferHandle . DangerousGetHandle ( ) , width , height , PixelFormat , stride , 0 ) ;
14787 image . Source = bitmap ;
14888 }
149- else
89+ else if ( image . Source != null )
15090 {
151- if ( image . Source != null )
152- {
153- var sourceRect = new Int32Rect ( dirtyRect . X , dirtyRect . Y , dirtyRect . Width , dirtyRect . Height ) ;
154- var bitmap = ( InteropBitmap ) image . Source ;
155- bitmap . Invalidate ( sourceRect ) ;
156- }
91+ var sourceRect = new Int32Rect ( dirtyRect . X , dirtyRect . Y , dirtyRect . Width , dirtyRect . Height ) ;
92+ var bitmap = ( InteropBitmap ) image . Source ;
93+ bitmap . Invalidate ( sourceRect ) ;
15794 }
15895 }
15996 } ) , dispatcherPriority ) ;
16097 }
16198 }
162-
163- private void ReleaseMemoryMappedView ( ref MemoryMappedFile mappedFile , ref MemoryMappedViewAccessor stream )
164- {
165- if ( stream != null )
166- {
167- stream . Dispose ( ) ;
168- stream = null ;
169- }
170-
171- if ( mappedFile != null )
172- {
173- mappedFile . Dispose ( ) ;
174- mappedFile = null ;
175- }
176- }
17799 }
178100}
0 commit comments