diff --git a/CPF.Windows/WindowImpl.cs b/CPF.Windows/WindowImpl.cs index 400b7b2..1a02deb 100644 --- a/CPF.Windows/WindowImpl.cs +++ b/CPF.Windows/WindowImpl.cs @@ -807,6 +807,7 @@ namespace CPF.Windows } break; case WindowsMessage.WM_DESTROY: + Closed?.Invoke(); if (handle != IntPtr.Zero) { try @@ -857,7 +858,6 @@ namespace CPF.Windows RenderBitmap = null; } handle = IntPtr.Zero; - Closed?.Invoke(); foreach (var item in invokeQueue) { item.SendOrPostCallback(item.Data); diff --git a/CPF/OpenGL/IGlContext.cs b/CPF/OpenGL/IGlContext.cs index 3ad27da..d1b82dc 100644 --- a/CPF/OpenGL/IGlContext.cs +++ b/CPF/OpenGL/IGlContext.cs @@ -12,7 +12,7 @@ namespace CPF.OpenGL /// 用来获取和保存Skia创建的GRContext /// IDisposable GRContext { get; set; } - //void MakeCurrent(); + void MakeCurrent(); //void SwapBuffers(); //void Dispose(); //public abstract GRGlTextureInfo CreateTexture(SKSizeI textureSize); @@ -35,7 +35,7 @@ namespace CPF.OpenGL public static class OpenglEx { static bool loaded; - private static void Load(IGlContext context) + public static void Load(IGlContext context) { if (!loaded) { diff --git a/CPF/UIElement.cs b/CPF/UIElement.cs index 51b616a..4057dac 100644 --- a/CPF/UIElement.cs +++ b/CPF/UIElement.cs @@ -3046,7 +3046,7 @@ namespace CPF /// 子级,一般自定义组件的时候使用 /// [NotCpfProperty] - internal protected virtual UIElementCollection Children + internal protected UIElementCollection Children { get { diff --git a/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1.csproj index 0967888..df73f42 100644 --- a/ConsoleApp1/ConsoleApp1.csproj +++ b/ConsoleApp1/ConsoleApp1.csproj @@ -1,7 +1,7 @@  - netcoreapp3.0;net40;net5;net6;net8 + netcoreapp3.0;net40;net5;net6 WinExe @@ -102,11 +102,12 @@ - + --> + + Net4 false diff --git a/ConsoleApp1/GLView.cs b/ConsoleApp1/GLView.cs new file mode 100644 index 0000000..717174b --- /dev/null +++ b/ConsoleApp1/GLView.cs @@ -0,0 +1,174 @@ +#if !NET40&&!NETCOREAPP3_0 +using CPF; +using CPF.Animation; +using CPF.Charts; +using CPF.Controls; +using CPF.Drawing; +using CPF.Shapes; +using CPF.Skia; +using CPF.Styling; +using CPF.Svg; +using CPF.OpenGL; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using SkiaSharp; +using Silk.NET.OpenGLES; +using static System.Net.Mime.MediaTypeNames; + +namespace ConsoleApp1 +{ + [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式 + public class GLView : UIElement + { + GL _gl; + public uint Id { get; set; } + + public uint ColorBuffer { get; set; } + + public uint DepthRenderBuffer { get; set; } + Size oldSize; + SKImage image; + + uint vao; + uint shaderProgram; + //IGlContext context; + protected unsafe override void OnRender(DrawingContext dc) + { + var size1 = ActualSize; + if (size1.Width <= 0 || size1.Height <= 0 || DesignMode) + { + return; + } + + var size = new PixelSize((int)Math.Round(size1.Width * Root.RenderScaling), (int)Math.Round(size1.Height * Root.RenderScaling)); + var skia = dc as SkiaDrawingContext; + var gl = skia.GlContext; + //context = gl; + OpenglEx.Load(gl); + if (_gl == null) + { + _gl = GL.GetApi(gl.GetProcAddress); + Id = _gl.GenFramebuffer(); + ColorBuffer = _gl.GenTexture(); + DepthRenderBuffer = _gl.GenRenderbuffer(); + + _gl.BindTexture(GLEnum.Texture2D, ColorBuffer); + + _gl.TexParameter(GLEnum.Texture2D, GLEnum.TextureMinFilter, (int)GLEnum.Linear); + _gl.TexParameter(GLEnum.Texture2D, GLEnum.TextureMagFilter, (int)GLEnum.Linear); + + _gl.BindTexture(GLEnum.Texture2D, 0); + + + _gl.ClearColor(0.2f, 0.3f, 0.3f, 1.0f); + float[] vertices = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; + + _gl.GenBuffers(1, out uint vbo); + _gl.BindBuffer(GLEnum.ArrayBuffer, vbo); + _gl.BufferData(GLEnum.ArrayBuffer, (nuint)vertices.Length * sizeof(float), vertices, GLEnum.StaticDraw); + + _gl.GenVertexArrays(1, out vao); + _gl.BindVertexArray(vao); + _gl.VertexAttribPointer(0, 3, GLEnum.Float, false, 3 * sizeof(float), null); + _gl.EnableVertexAttribArray(0); + + string vertexShaderSource = @"#version 330 core + layout (location = 0) in vec3 aPos; + void main() + { + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + } + "; + + string fragmentShaderSource = @"#version 330 core + out vec4 FragColor; + void main() + { + FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + } + "; + + uint vertexShader = _gl.CreateShader(GLEnum.VertexShader); + _gl.ShaderSource(vertexShader, vertexShaderSource); + _gl.CompileShader(vertexShader); + + uint fragmentShader = _gl.CreateShader(GLEnum.FragmentShader); + _gl.ShaderSource(fragmentShader, fragmentShaderSource); + _gl.CompileShader(fragmentShader); + + shaderProgram = _gl.CreateProgram(); + _gl.AttachShader(shaderProgram, vertexShader); + _gl.AttachShader(shaderProgram, fragmentShader); + _gl.LinkProgram(shaderProgram); + + _gl.DeleteShader(vertexShader); + _gl.DeleteShader(fragmentShader); + } + if (size1 != oldSize) + { + oldSize = size1; + + _gl.BindTexture(GLEnum.Texture2D, ColorBuffer); + _gl.TexImage2D(GLEnum.Texture2D, 0, (int)GLEnum.Rgba, (uint)size.Width, (uint)size.Height, 0, GLEnum.Rgb, GLEnum.UnsignedByte, null); + _gl.BindTexture(GLEnum.Texture2D, 0); + + _gl.BindRenderbuffer(GLEnum.Renderbuffer, DepthRenderBuffer); + _gl.RenderbufferStorage(GLEnum.Renderbuffer, GLEnum.Depth32fStencil8, (uint)size.Width, (uint)size.Height); + + _gl.BindRenderbuffer(GLEnum.Renderbuffer, 0); + + _gl.BindFramebuffer(GLEnum.Framebuffer, Id); + + _gl.FramebufferTexture2D(GLEnum.Framebuffer, GLEnum.ColorAttachment0, GLEnum.Texture2D, ColorBuffer, 0); + + _gl.FramebufferRenderbuffer(GLEnum.Framebuffer, GLEnum.DepthStencilAttachment, GLEnum.Renderbuffer, DepthRenderBuffer); + _gl.BindFramebuffer(GLEnum.Framebuffer, 0); + + + GRBackendTexture backendTexture = new GRBackendTexture((int)(size.Width / Root.RenderScaling), (int)(size.Height / Root.RenderScaling), false, new GRGlTextureInfo(0x0DE1, (uint)ColorBuffer, SKColorType.Rgba8888.ToGlSizedFormat())); + + image = SKImage.FromTexture((GRContext)skia.GlContext.GRContext, backendTexture, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888); + } + + _gl.BindFramebuffer(FramebufferTarget.Framebuffer, Id); + var vp = new float[4]; + gl.GetFloatv(GlConsts.GL_VIEWPORT, vp); + _gl.Viewport(0, 0, (uint)size.Width, (uint)size.Height); + OnGlRender(gl, size); + _gl.Viewport((int)vp[0], (int)vp[1], (uint)vp[2], (uint)vp[3]); + skia.SKCanvas.DrawImage(image, 0, 0); + base.OnRender(dc); + } + + Random random = new Random(); + protected void OnGlRender(IGlContext gl, PixelSize viewPort) + { + //_gl.ClearColor(0.5f, 0, 0, 0.5f); + //_gl.Clear(ClearBufferMask.ColorBufferBit); + _gl.UseProgram(shaderProgram); + _gl.BindVertexArray(vao); + _gl.DrawArrays(GLEnum.Triangles, 0, 3); + } + + protected override void Dispose(bool disposing) + { + if (_gl != null) + { + OpenglEx.DeleteFramebuffers(null, 1, new int[] { (int)Id }); + OpenglEx.DeleteTextures(null, 1, new int[] { (int)ColorBuffer }); + OpenglEx.DeleteRenderbuffers(null, 1, new int[] { (int)DepthRenderBuffer }); + //_gl.DeleteFramebuffer(Id); + //_gl.DeleteTexture(ColorBuffer); + //_gl.DeleteRenderbuffer(DepthRenderBuffer); + } + base.Dispose(disposing); + } + } +} +#endif \ No newline at end of file diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index b137afa..760b9f2 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -38,7 +38,7 @@ namespace ConsoleApp1 #if Net4 (OperatingSystemType.Windows, new WindowsPlatform(), new CPF.GDIPlus.GDIPlusDrawingFactory { ClearType = true }) #else - (OperatingSystemType.Windows, new WindowsPlatform(false), new SkiaDrawingFactory { }) + (OperatingSystemType.Windows, new WindowsPlatform(false), new SkiaDrawingFactory { UseGPU=true }) , (OperatingSystemType.OSX, new CPF.Mac.MacPlatform(), new SkiaDrawingFactory { UseGPU = false }) , (OperatingSystemType.Linux, new CPF.Linux.LinuxPlatform(), new SkiaDrawingFactory { UseGPU = false }) #endif @@ -87,9 +87,21 @@ namespace ConsoleApp1 ////Thread.Sleep(10000); ////Application.AllowDeveloperTool = false; ////Application.DisablePopupClose = true; + - //Console.SetOut(new tr()); - //Console.WriteLine("123"); + //data aa = new data(); + //aa.test.test.test.Name = "11111"; + //model.Test1.test = aa; + + //var test1 = new TextBlock + //{ + // [nameof(TextBlock.Text)] = new CPF.Obx(a => a.Test1.test.test.test.test.Name), + //}; + //test1.DataContext=model; + + //aa = new data(); + //aa.test.test.test.Name = "666666"; + //model.Test1.test = aa; Application.Run(new Window2 { DataContext = model, CommandContext = model }); //Application.Run(new Window @@ -139,7 +151,7 @@ namespace ConsoleApp1 } class tr : TextWriter { - public override Encoding Encoding => Encoding.Unicode; + public override Encoding Encoding => Encoding.Unicode; public override void Write(string value) { diff --git a/ConsoleApp1/Window2.cs b/ConsoleApp1/Window2.cs index c26d849..06f3d34 100644 --- a/ConsoleApp1/Window2.cs +++ b/ConsoleApp1/Window2.cs @@ -738,15 +738,17 @@ namespace ConsoleApp1 MarginTop = 450, Height = 58, Width = 121, - },//#if !Net4 - //new CPF.Skia.GlView - //{ - // MarginRight = 56, - // MarginTop = 44, - // Height = 132, - // Width = 151, - //}, - //#endif + }, + #if !Net4&&!NETCOREAPP3_0 + new GLView + { + MarginRight = 56, + MarginTop = 44, + Height = 132, + Width = 151, + [nameof(GLView.DoubleClick)]=new CommandDescribe((s,e)=>{ s.Dispose(); }), + }, + #endif new Button { Commands = @@ -2444,14 +2446,14 @@ new TabItemTemplate{ BindingMode.OneWay), Name = "hmbb" }, - new TextBox - { - Width = 130, - Height= 60, - Background =Color.Gray, - [nameof(TextBox.Text)]= new Obx(a => a.Test1.test.test.test.test.Name, - BindingMode.OneWayToSource), - }, + //new TextBox + //{ + // Width = 130, + // Height= 60, + // Background =Color.Gray, + // [nameof(TextBox.Text)]= new Obx(a => a.Test1.test.test.test.test.Name, + // BindingMode.OneWayToSource), + //}, new Button { Content="创建对象",