<commit_before>﻿using System;
using System.Runtime.InteropServices;
using Avalonia.OpenGL;
using Avalonia.OpenGL.Angle;
using Windows.UI.Composition;
using Windows.UI.Composition.Interop;
using WinRT;

namespace Avalonia.Win32
{
    public class CompositionEglGlPlatformSurface : EglGlPlatformSurfaceBase
    {
        private readonly EglDisplay _display;
        private readonly EglContext _context;
        private readonly IEglWindowGlPlatformSurfaceInfo _info;
        private ICompositionDrawingSurfaceInterop _surfaceInterop;

        public CompositionEglGlPlatformSurface(EglContext context, IEglWindowGlPlatformSurfaceInfo info) : base()
        {
            _display = context.Display;
            _context = context;
            _info = info;
        }

        public void AttachToCompositionTree(IntPtr hwnd)
        {
            _surfaceInterop = CompositionHost.Instance.Initialize(hwnd);
        }

        public override IGlPlatformSurfaceRenderTarget CreateGlRenderTarget()
        {            
            return new CompositionRenderTarget(_display, _context, _surfaceInterop, _info);
        }

        class CompositionRenderTarget : EglPlatformSurfaceRenderTargetBase
        {
            private readonly EglDisplay _display;
            private readonly EglContext _context;            
            private readonly IEglWindowGlPlatformSurfaceInfo _info;
            private PixelSize _initialSize;
            private readonly ICompositionDrawingSurfaceInterop _surfaceInterop;

            public CompositionRenderTarget(EglDisplay display, EglContext context, ICompositionDrawingSurfaceInterop interopSurface, IEglWindowGlPlatformSurfaceInfo info) : base(display, context)
            {
                _display = display;
                _context = context;                
                _surfaceInterop = interopSurface;
                _info = info;
                _initialSize = info.Size;
            }

            public override bool IsCorrupted => _initialSize != _info.Size;

            public override IGlPlatformSurfaceRenderingSession BeginDraw()
            {
                var l = _context.Lock();

                var iid = Guid.Parse("6f15aaf2-d208-4e89-9ab4-489535d34f9c");
                var updateRect = new RECT { right = _info.Size.Width, bottom = _info.Size.Height };
                var offset = new POINT();

                _surfaceInterop.Resize(new POINT { X = _info.Size.Width, Y = _info.Size.Height });
                _surfaceInterop.BeginDraw(
                    ref updateRect,
                    ref iid,
                    out IntPtr texture, ref offset);

                var surface = (_display as AngleWin32EglDisplay).WrapDirect3D11Texture(texture);

                var restoreContext = _context.MakeCurrent(surface);

                return new Session(_display, _context, null, _info, l, restoreContext, () => { }, true);
            }

            public override void Dispose()
            {
                _surfaceInterop.EndDraw();
                base.Dispose();
            }
        }

        class Session : IGlPlatformSurfaceRenderingSession
        {
            private readonly EglContext _context;
            private readonly EglSurface _glSurface;
            private readonly EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo _info;
            private readonly EglDisplay _display;
            private readonly IDisposable _lock;
            private readonly IDisposable _restoreContext;
            private readonly Action _onFinish;


            public Session(EglDisplay display, EglContext context,
                EglSurface glSurface, EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo info,
                IDisposable @lock, IDisposable restoreContext, Action onFinish, bool isYFlipped)
            {
                IsYFlipped = isYFlipped;
                _context = context;
                _display = display;
                _glSurface = glSurface;
                _info = info;
                _lock = @lock;
                _restoreContext = restoreContext;
                _onFinish = onFinish;
            }

            public void Dispose()
            {
                _context.GlInterface.Flush();
                _display.EglInterface.WaitGL();
                _glSurface?.SwapBuffers();
                _display.EglInterface.WaitClient();
                _display.EglInterface.WaitGL();
                _display.EglInterface.WaitNative(EglConsts.EGL_CORE_NATIVE_ENGINE);
                _restoreContext.Dispose();
                _lock.Dispose();
                _onFinish?.Invoke();
            }

            public IGlContext Context => _context;
            public PixelSize Size => _info.Size;
            public double Scaling => _info.Scaling;
            public bool IsYFlipped { get; }
        }
    }



    class CompositionHost
    {
        internal enum DISPATCHERQUEUE_THREAD_APARTMENTTYPE
        {
            DQTAT_COM_NONE = 0,
            DQTAT_COM_ASTA = 1,
            DQTAT_COM_STA = 2<commit_msg>pass in wrapped surface to session.<commit_after>﻿using System;
using System.Runtime.InteropServices;
using Avalonia.OpenGL;
using Avalonia.OpenGL.Angle;
using Windows.UI.Composition;
using Windows.UI.Composition.Interop;
using WinRT;

namespace Avalonia.Win32
{
    public class CompositionEglGlPlatformSurface : EglGlPlatformSurfaceBase
    {
        private readonly EglDisplay _display;
        private readonly EglContext _context;
        private readonly IEglWindowGlPlatformSurfaceInfo _info;
        private ICompositionDrawingSurfaceInterop _surfaceInterop;

        public CompositionEglGlPlatformSurface(EglContext context, IEglWindowGlPlatformSurfaceInfo info) : base()
        {
            _display = context.Display;
            _context = context;
            _info = info;
        }

        public void AttachToCompositionTree(IntPtr hwnd)
        {
            _surfaceInterop = CompositionHost.Instance.Initialize(hwnd);
        }

        public override IGlPlatformSurfaceRenderTarget CreateGlRenderTarget()
        {            
            return new CompositionRenderTarget(_display, _context, _surfaceInterop, _info);
        }

        class CompositionRenderTarget : EglPlatformSurfaceRenderTargetBase
        {
            private readonly EglDisplay _display;
            private readonly EglContext _context;            
            private readonly IEglWindowGlPlatformSurfaceInfo _info;
            private PixelSize _initialSize;
            private readonly ICompositionDrawingSurfaceInterop _surfaceInterop;

            public CompositionRenderTarget(EglDisplay display, EglContext context, ICompositionDrawingSurfaceInterop interopSurface, IEglWindowGlPlatformSurfaceInfo info) : base(display, context)
            {
                _display = display;
                _context = context;                
                _surfaceInterop = interopSurface;
                _info = info;
                _initialSize = info.Size;
            }

            public override bool IsCorrupted => _initialSize != _info.Size;

            public override IGlPlatformSurfaceRenderingSession BeginDraw()
            {
                var l = _context.Lock();

                var iid = Guid.Parse("6f15aaf2-d208-4e89-9ab4-489535d34f9c");
                var updateRect = new RECT { right = _info.Size.Width, bottom = _info.Size.Height };
                var offset = new POINT();

                _surfaceInterop.Resize(new POINT { X = _info.Size.Width, Y = _info.Size.Height });
                _surfaceInterop.BeginDraw(
                    ref updateRect,
                    ref iid,
                    out IntPtr texture, ref offset);

                var surface = (_display as AngleWin32EglDisplay).WrapDirect3D11Texture(texture);

                var restoreContext = _context.MakeCurrent(surface);

                return new Session(_display, _context, surface, _info, l, restoreContext, () => { }, true);
            }

            public override void Dispose()
            {
                _surfaceInterop.EndDraw();
                base.Dispose();
            }
        }

        class Session : IGlPlatformSurfaceRenderingSession
        {
            private readonly EglContext _context;
            private readonly EglSurface _glSurface;
            private readonly EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo _info;
            private readonly EglDisplay _display;
            private readonly IDisposable _lock;
            private readonly IDisposable _restoreContext;
            private readonly Action _onFinish;


            public Session(EglDisplay display, EglContext context,
                EglSurface glSurface, EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo info,
                IDisposable @lock, IDisposable restoreContext, Action onFinish, bool isYFlipped)
            {
                IsYFlipped = isYFlipped;
                _context = context;
                _display = display;
                _glSurface = glSurface;
                _info = info;
                _lock = @lock;
                _restoreContext = restoreContext;
                _onFinish = onFinish;
            }

            public void Dispose()
            {
                _context.GlInterface.Flush();
                _display.EglInterface.WaitGL();
                _glSurface.SwapBuffers();
                _display.EglInterface.WaitClient();
                _display.EglInterface.WaitGL();
                _display.EglInterface.WaitNative(EglConsts.EGL_CORE_NATIVE_ENGINE);
                _restoreContext.Dispose();
                _lock.Dispose();
                _onFinish?.Invoke();
            }

            public IGlContext Context => _context;
            public PixelSize Size => _info.Size;
            public double Scaling => _info.Scaling;
            public bool IsYFlipped { get; }
        }
    }



    class CompositionHost
    {
        internal enum DISPATCHERQUEUE_THREAD_APARTMENTTYPE
        {
            DQTAT_COM_NONE = 0,
            DQTAT_COM_ASTA = 1,
            DQTAT_COM_STA = 2
{'fraction_non_alphanumeric': 0.06028368794326241, 'fraction_numerical': 0.006107171000788022, 'mean_word_length': 1.8868353710548764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552406', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2471, 'n_words': 556}