コード例 #1
0
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            // create the contexts if not done already
            if (grContext == null)
            {
                var glInterface = GRGlInterface.CreateNativeGlInterface();
                grContext = GRContext.Create(GRBackend.OpenGL, glInterface);

                // get initial details
                renderTarget = CreateRenderTarget();
            }

            // update to the latest dimensions
            renderTarget = new GRBackendRenderTarget(Width, Height, renderTarget.SampleCount, renderTarget.StencilBits, renderTarget.GetGlFramebufferInfo());

            // create the surface
            using (var surface = SKSurface.Create(grContext, renderTarget, SKColorType.Rgba8888))
            {
                if (PaintSurface != null)
                {
                    PaintSurface.Invoke(surface);
                }

                // start drawing
                OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));

                surface.Canvas.Flush();
            }

            // update the control
            SwapBuffers();
        }
コード例 #2
0
        private static GRContext GenerateSkiaContext(NativeWindow nativeWindow)
        {
            var nativeContext = Program.GetNativeContext(nativeWindow);
            var glInterface   = GRGlInterface.AssembleGlInterface(nativeContext, (contextHandle, name) => Glfw.GetProcAddress(name));

            return(GRContext.Create(GRBackend.OpenGL, glInterface));
        }
コード例 #3
0
        public new void DrawInRect(GLKView view, CGRect rect)
        {
            if (designMode)
            {
                return;
            }

            if (context == null)
            {
                var glInterface = GRGlInterface.CreateNativeGlInterface();
                context = GRContext.Create(GRBackend.OpenGL, glInterface);

                // get the initial details
                renderTarget = SKGLDrawable.CreateRenderTarget();
            }

            // set the dimensions as they might have changed
            renderTarget.Width  = (int)DrawableWidth;
            renderTarget.Height = (int)DrawableHeight;

            // create the surface
            using (var surface = SKSurface.Create(context, renderTarget))
            {
                // draw on the surface
                DrawInSurface(surface, renderTarget);

                surface.Canvas.Flush();
            }

            // flush the SkiaSharp contents to GL
            context.Flush();
        }
コード例 #4
0
        public PlatformRenderInterface(ICustomSkiaGpu customSkiaGpu)
        {
            if (customSkiaGpu != null)
            {
                _customSkiaGpu = customSkiaGpu;

                GrContext = _customSkiaGpu.GrContext;

                return;
            }

            var gl = AvaloniaLocator.Current.GetService <IWindowingPlatformGlFeature>();

            if (gl != null)
            {
                var display = gl.ImmediateContext.Display;
                gl.ImmediateContext.MakeCurrent();
                using (var iface = display.Type == GlDisplayType.OpenGL2
                    ? GRGlInterface.AssembleGlInterface((_, proc) => display.GlInterface.GetProcAddress(proc))
                    : GRGlInterface.AssembleGlesInterface((_, proc) => display.GlInterface.GetProcAddress(proc)))
                {
                    GrContext = GRContext.Create(GRBackend.OpenGL, iface);
                }
                display.ClearContext();
            }
        }
コード例 #5
0
        public override void DrawInCGLContext(CGLContext glContext, CGLPixelFormat pixelFormat, double timeInterval, ref CVTimeStamp timeStamp)
        {
            CGLContext.CurrentContext = glContext;

            if (context == null)
            {
                // get the bits for SkiaSharp
                var glInterface = GRGlInterface.CreateNativeGlInterface();
                context = GRContext.Create(GRBackend.OpenGL, glInterface);
            }

            // create the surface
            renderTarget        = SKGLDrawable.CreateRenderTarget();
            renderTarget.Width  = (int)(Bounds.Width * ContentsScale);
            renderTarget.Height = (int)(Bounds.Height * ContentsScale);
            using (var surface = SKSurface.Create(context, renderTarget))
            {
                // draw on the surface
                DrawInSurface(surface, renderTarget);
                SKDelegate?.DrawInSurface(surface, renderTarget);

                surface.Canvas.Flush();
            }

            // flush the SkiaSharp context to the GL context
            context.Flush();

            base.DrawInCGLContext(glContext, pixelFormat, timeInterval, ref timeStamp);
        }
コード例 #6
0
        private void DrawGpuTextureWithDesc(Action <SKSurface, GRGlBackendTextureDesc> draw)
        {
            using (var ctx = CreateGlContext())
            {
                ctx.MakeCurrent();

                // create the texture
                var textureInfo = ctx.CreateTexture(new SKSizeI(100, 100));
                // this is a new field that was added to the struct
                textureInfo.Format = 0;
                var textureDesc = new GRGlBackendTextureDesc
                {
                    Width         = 100,
                    Height        = 100,
                    Config        = GRPixelConfig.Rgba8888,
                    Flags         = GRBackendTextureDescFlags.RenderTarget,
                    Origin        = GRSurfaceOrigin.BottomLeft,
                    SampleCount   = 0,
                    TextureHandle = textureInfo,
                };

                // create the surface
                using (var grContext = GRContext.Create(GRBackend.OpenGL))
                    using (var surface = SKSurface.CreateAsRenderTarget(grContext, textureDesc))
                    {
                        Assert.NotNull(surface);

                        draw(surface, textureDesc);
                    }

                // clean up
                ctx.DestroyTexture(textureInfo.Id);
            }
        }
コード例 #7
0
        protected override void OnLoad(EventArgs ee)
        {
            base.OnLoad(ee);
            var glInterface = GRGlInterface.CreateNativeGlInterface();

            Debug.Assert(glInterface.Validate());

            this._context = GRContext.Create(GRBackend.OpenGL, glInterface);
            Debug.Assert(this._context.Handle != IntPtr.Zero);
            this._renderTarget = CreateRenderTarget(_context);

            this.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
            };
            FocusedChanged += OnFocusedChanged;

            InitMouse();

            //    WindowState = WindowState.Fullscreen;
            CursorVisible      = false;
            this.CursorVisible = true;
        }
コード例 #8
0
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            Control senderControl = (Control)sender;

            if (context == null)
            {
                var glInterface = GRGlInterface.CreateNativeGlInterface();
                context = GRContext.Create(GRBackend.OpenGL, glInterface);
            }

            if (renderTarget == null || surface == null || renderTarget.Width != senderControl.Width || renderTarget.Height != senderControl.Height)
            {
                renderTarget?.Dispose();

                GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer);
                GL.GetInteger(GetPName.StencilBits, out var stencil);
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget(senderControl.Width, senderControl.Height, context.GetMaxSurfaceSampleCount(colorType), stencil, glInfo);
                surface?.Dispose();
                surface = SKSurface.Create(context, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
            }

            figure.Render(surface.Canvas, figureSize, plotEngagedWithMouse);

            surface.Canvas.Flush();
            glControl1.SwapBuffers();
        }
コード例 #9
0
        private void PrepareGLContexts()
        {
            // create GL context
            glContext = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
            EAGLContext.SetCurrentContext(glContext);

            // create render buffer
            Gles.glGenRenderbuffers(1, ref renderBuffer);
            Gles.glBindRenderbuffer(Gles.GL_RENDERBUFFER, renderBuffer);
            glContext.RenderBufferStorage(Gles.GL_RENDERBUFFER, this);

            // create frame buffer
            Gles.glGenFramebuffers(1, ref framebuffer);
            Gles.glBindFramebuffer(Gles.GL_FRAMEBUFFER, framebuffer);
            Gles.glFramebufferRenderbuffer(Gles.GL_FRAMEBUFFER, Gles.GL_COLOR_ATTACHMENT0, Gles.GL_RENDERBUFFER, renderBuffer);

            // get the bits for SkiaSharp
            var glInterface = GRGlInterface.CreateNativeGlInterface();

            context = GRContext.Create(GRBackend.OpenGL, glInterface);

            // finished
            EAGLContext.SetCurrentContext(null);

            recreateSurface = true;
        }
コード例 #10
0
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            //base.OnPaint(e);

            // create the contexts if not done already
            if (grContext == null)
            {
                var glInterface = GRGlInterface.CreateNativeGlInterface();
                grContext = GRContext.Create(GRBackend.OpenGL, glInterface);

                // get initial details
                renderTarget = CreateRenderTarget();
            }

            // update to the latest dimensions
            renderTarget.Width  = Width;
            renderTarget.Height = Height;

            // create the surface
            using (var surface = SKSurface.Create(grContext, renderTarget))
            {
                if (PaintSurface != null)
                {
                    PaintSurface.Invoke(surface);
                }

                // start drawing
                OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));

                surface.Canvas.Flush();
            }

            // update the control
            SwapBuffers();
        }
コード例 #11
0
        protected override void OnLoad(EventArgs ee)
        {
            base.OnLoad(ee);
            var glInterface = GRGlInterface.CreateNativeGlInterface();

            Debug.Assert(glInterface.Validate());

            this.context = GRContext.Create(GRBackend.OpenGL, glInterface);
            Debug.Assert(this.context.Handle != IntPtr.Zero);
            this.renderTarget = CreateRenderTarget();

            this.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
            };
            InputMouse                   = new InputMouse(this);
            InputMouse.MouseDown        += state => Engine.OnMouse(state);
            InputMouse.MouseUp          += state => Engine.OnMouse(state);
            InputMouse.MouseMove        += state => Engine.OnMouse(state);
            InputMouse.MousePressedMove += state => Engine.OnMouse(state);
            FocusedChanged              += OnFocusedChanged;

            //    WindowState = WindowState.Fullscreen;
            CursorVisible = false;
        }
コード例 #12
0
ファイル: GRContextTest.cs プロジェクト: jsntcy/SkiaSharp
        public void CreateSpecificContextIsValid()
        {
            using (var ctx = CreateGlContext()) {
                ctx.MakeCurrent();

                var glInterface = GRGlInterface.CreateNativeGlInterface();

                var grContext = GRContext.Create(GRBackend.OpenGL, glInterface);
            }
        }
コード例 #13
0
        public void CreateDefaultContextIsValid()
        {
            using (var ctx = CreateGlContext()) {
                ctx.MakeCurrent();

                var grContext = GRContext.Create(GRBackend.OpenGL);

                Assert.NotNull(grContext);
            }
        }
コード例 #14
0
        public override void PrepareOpenGL()
        {
            base.PrepareOpenGL();

            // create the context
            var glInterface = GRGlInterface.CreateNativeGlInterface();

            context = GRContext.Create(GRBackend.OpenGL, glInterface);

            renderTarget = SKGLDrawable.CreateRenderTarget();
        }
コード例 #15
0
ファイル: CanvasControl_OpenGL.cs プロジェクト: w8w8w8/dwsim5
        protected override void OnRenderFrame()
        {
            var rect = Allocation;

            // create the contexts if not done already
            if (grContext == null)
            {
                var glInterface = GRGlInterface.CreateNativeGlInterface();

                if (glInterface == null)
                {
                    Console.WriteLine("Error creating OpenGL ES interface. Check if you have OpenGL ES correctly installed and configured or change the PFD Renderer to 'Software (CPU)' on the Global Settings panel.", "Error Creating OpenGL ES interface");
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }
                else
                {
                    grContext = GRContext.Create(GRBackend.OpenGL, glInterface);
                }

                try
                {
                    renderTarget = CreateRenderTarget();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error creating OpenGL ES render target. Check if you have OpenGL ES correctly installed and configured or change the PFD Renderer to 'Software (CPU)' on the Global Settings panel.\nError message:\n" + ex.ToString());
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }
            }

            if (grContext != null)
            {
                // update to the latest dimensions
                renderTarget.Width  = rect.Width;
                renderTarget.Height = rect.Height;

                // create the surface
                using (var surface = SKSurface.Create(grContext, renderTarget))
                {
                    surface.Canvas.Clear(SKColors.White);

                    // start drawing
                    if (fsurface != null)
                    {
                        fsurface.UpdateSurface(surface);
                    }

                    // start drawing
                    OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));

                    surface.Canvas.Flush();
                }
            }
        }
コード例 #16
0
ファイル: SkiaGLControl.cs プロジェクト: Erguotou/SkiaSharp
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
            {
                return;
            }

            Reshape();
            grContext = GRContext.Create(GRBackend.OpenGL);
        }
コード例 #17
0
        void RenderWithSkia()
        {
            int width  = glControl1.Width;
            int height = glControl1.Height;

            if (field == null || field.width != width || field.height != height)
            {
                field = new Starfield.Field(100_000, width, height);
            }
            field.StepForward();

            // Create a Skia surface using the OpenGL control
            SKColorType colorType     = SKColorType.Rgba8888;
            GRContext   contextOpenGL = GRContext.Create(GRBackend.OpenGL, GRGlInterface.CreateNativeGlInterface());

            GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer);
            GRGlFramebufferInfo glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());

            GL.GetInteger(GetPName.StencilBits, out var stencil);
            GRBackendRenderTarget renderTarget = new GRBackendRenderTarget(width, height, contextOpenGL.GetMaxSurfaceSampleCount(colorType), stencil, glInfo);
            SKSurface             surface      = SKSurface.Create(contextOpenGL, renderTarget, GRSurfaceOrigin.BottomLeft, colorType);
            SKCanvas canvas = surface.Canvas;

            // draw the starfield
            var paint = new SKPaint
            {
                Color       = SKColors.White,
                IsAntialias = true
            };

            canvas.Clear(SKColors.Black);
            foreach (Starfield.Star star in field.stars)
            {
                //canvas.DrawRect(star.X, star.Y, star.Size, star.Size, paint);
                canvas.DrawCircle(new SKPoint(star.X, star.Y), star.Size / 2, paint);
            }

            // Force a display
            surface.Canvas.Flush();
            glControl1.SwapBuffers();

            // dispose to prevent memory access violations while exiting
            renderTarget?.Dispose();
            contextOpenGL?.Dispose();
            canvas?.Dispose();
            surface?.Dispose();

            // update the FPS display
            renderCount += 1;
            double elapsedSeconds = (double)stopwatch.ElapsedMilliseconds / 1000;

            Text = string.Format("Rendered {0} frames in {1:0.00} seconds ({2:0.00} Hz)", renderCount, elapsedSeconds, renderCount / elapsedSeconds);
        }
コード例 #18
0
        public PlatformRenderInterface()
        {
            var gl = AvaloniaLocator.Current.GetService <IWindowingPlatformGlFeature>();

            if (gl != null)
            {
                var display = gl.ImmediateContext.Display;
                var iface   = display.Type == GlDisplayType.OpenGL2
                    ? GRGlInterface.AssembleGlInterface((_, proc) => display.GlInterface.GetProcAddress(proc))
                    : GRGlInterface.AssembleGlesInterface((_, proc) => display.GlInterface.GetProcAddress(proc));
                gl.ImmediateContext.MakeCurrent();
                GrContext = GRContext.Create(GRBackend.OpenGL, iface);
            }
        }
コード例 #19
0
        private void RenderWithOpenGL(object sender, PaintEventArgs e)
        {
            Control sctl   = (Control)sender;
            int     width  = sctl.Width;
            int     height = sctl.Height;

            // setup the Skia surface using OpenGL

            GRContext contextOpenGL = GRContext.Create(GRBackend.OpenGL, GRGlInterface.CreateNativeGlInterface());

            GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer);
            GRGlFramebufferInfo glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());

            GL.GetInteger(GetPName.StencilBits, out var stencil);
            GRBackendRenderTarget renderTarget = new GRBackendRenderTarget(width, height, contextOpenGL.GetMaxSurfaceSampleCount(colorType), stencil, glInfo);

            SKSurface surface = SKSurface.Create(contextOpenGL, renderTarget, GRSurfaceOrigin.BottomLeft, colorType);

            // Draw some stuff on the canvas

            surface.Canvas.Clear(SKColor.Parse("#FFFFFF")); // adds about 3ms fullscreen

            byte alpha = 128;
            var  paint = new SKPaint();

            for (int i = 0; i < 1_000; i++)
            {
                float x1 = (float)(rand.NextDouble() * width);
                float x2 = (float)(rand.NextDouble() * width);
                float y1 = (float)(rand.NextDouble() * height);
                float y2 = (float)(rand.NextDouble() * height);

                paint.Color = new SKColor(
                    red: (byte)(rand.NextDouble() * 255),
                    green: (byte)(rand.NextDouble() * 255),
                    blue: (byte)(rand.NextDouble() * 255),
                    alpha: alpha
                    );

                surface.Canvas.DrawLine(x1, y1, x2, y2, paint);
            }

            surface.Canvas.Flush();
            glControl1.SwapBuffers();

            // prevent memory access violations by disposing before exiting
            renderTarget?.Dispose();
            contextOpenGL?.Dispose();
            surface?.Dispose();
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: Barinzaya/AutoCaption
        private void InitSkia()
        {
            _skInterface = GRGlInterface.CreateNativeGlInterface();
            if (_skInterface == null)
            {
                throw new Exception($"Failed to create SkiaSharp OpenGL interface.");
            }

            _skContext = GRContext.Create(GRBackend.OpenGL, _skInterface);
            if (_skContext == null)
            {
                throw new Exception($"Failed to create SkiaSharp OpenGL context.");
            }

            ResizeScreen();

            var fontName = _config.Text.Font;

            _skFont = SKTypeface.FromFile(fontName) ?? SKTypeface.FromFamilyName(fontName);

            _skFillPaint = new SKPaint()
            {
                BlendMode   = SKBlendMode.SrcOver,
                Color       = _config.Text.FillColor,
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                TextAlign   = _config.Text.Align,
                TextSize    = _config.Text.Size,
                Typeface    = _skFont,
            };

            if (_config.Text.StrokeWidth > 0)
            {
                _skStrokePaint = new SKPaint()
                {
                    BlendMode   = SKBlendMode.SrcOver,
                    Color       = _config.Text.StrokeColor,
                    IsAntialias = true,
                    StrokeCap   = _config.Text.StrokeCap,
                    StrokeJoin  = _config.Text.StrokeJoin,
                    StrokeMiter = _config.Text.StrokeMiter,
                    StrokeWidth = _config.Text.StrokeWidth,
                    Style       = SKPaintStyle.Stroke,
                    TextAlign   = _config.Text.Align,
                    TextSize    = _config.Text.Size,
                    Typeface    = _skFont,
                };
            }
        }
コード例 #21
0
ファイル: SKGLView.cs プロジェクト: miss-bug/SkiaSharp
        public new void DrawInRect(GLKView view, CGRect rect)
        {
            if (designMode)
            {
                return;
            }

            // create the contexts if not done already
            if (context == null)
            {
                var glInterface = GRGlInterface.CreateNativeGlInterface();
                context = GRContext.Create(GRBackend.OpenGL, glInterface);
            }

            // manage the drawing surface
            if (renderTarget == null || surface == null || renderTarget.Width != DrawableWidth || renderTarget.Height != DrawableHeight)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out var framebuffer);
                Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out var stencil);
                Gles.glGetIntegerv(Gles.GL_SAMPLES, out var samples);
                var maxSamples = context.GetMaxSurfaceSampleCount(colorType);
                if (samples > maxSamples)
                {
                    samples = maxSamples;
                }
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget((int)DrawableWidth, (int)DrawableHeight, samples, stencil, glInfo);

                // create the surface
                surface?.Dispose();
                surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType);
            }

            using (new SKAutoCanvasRestore(surface.Canvas, true))
            {
                // start drawing
                var e = new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType);
                OnPaintSurface(e);
#pragma warning disable CS0618 // Type or member is obsolete
                DrawInSurface(e.Surface, e.RenderTarget);
#pragma warning restore CS0618 // Type or member is obsolete
            }

            // flush the SkiaSharp contents to GL
            surface.Canvas.Flush();
            context.Flush();
        }
コード例 #22
0
        public Renderer()
        {
            GRGlInterface glInterface = GRGlInterface.CreateNativeGlInterface();

            Context = GRContext.Create(GRBackend.OpenGL, glInterface);

            RenderTarget = new GRBackendRenderTargetDesc
            {
                Config             = GRPixelConfig.Bgra8888,
                Origin             = GRSurfaceOrigin.BottomLeft,
                SampleCount        = 0,
                StencilBits        = 8,
                RenderTargetHandle = (IntPtr)0
            };
        }
コード例 #23
0
ファイル: Renderer.cs プロジェクト: emmauss/Ngco
        public Renderer()
        {
            var glInterface = GRGlInterface.CreateNativeGlInterface();

            Context = GRContext.Create(GRBackend.OpenGL, glInterface);

            GL.GetFramebufferAttachmentParameter(FramebufferTarget.Framebuffer, FramebufferAttachment.FrontLeft, FramebufferParameterName.FramebufferAttachmentStencilSize, out var stencilBits);

            RenderTarget = new GRBackendRenderTargetDesc {
                Config             = GRPixelConfig.Bgra8888,
                Origin             = GRSurfaceOrigin.BottomLeft,
                SampleCount        = 0,
                StencilBits        = stencilBits,
                RenderTargetHandle = (IntPtr)0
            };
        }
コード例 #24
0
        public void GpuSurfaceIsCreated()
        {
            using (var ctx = CreateGlContext()) {
                ctx.MakeCurrent();

                using (var grContext = GRContext.Create(GRBackend.OpenGL))
                    using (var surface = SKSurface.Create(grContext, true, new SKImageInfo(100, 100))) {
                        Assert.NotNull(surface);

                        var canvas = surface.Canvas;
                        Assert.NotNull(canvas);

                        canvas.Clear(SKColors.Transparent);
                    }
            }
        }
コード例 #25
0
        void Render()
        {
            // Create a Skia surface using the OpenGL control
            int         width         = glControl1.Width;
            int         height        = glControl1.Height;
            SKColorType colorType     = SKColorType.Rgba8888;
            GRContext   contextOpenGL = GRContext.Create(GRBackend.OpenGL, GRGlInterface.CreateNativeGlInterface());

            GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer);
            GRGlFramebufferInfo glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());

            GL.GetInteger(GetPName.StencilBits, out var stencil);
            GRBackendRenderTarget renderTarget = new GRBackendRenderTarget(width, height, contextOpenGL.GetMaxSurfaceSampleCount(colorType), stencil, glInfo);
            SKSurface             surface      = SKSurface.Create(contextOpenGL, renderTarget, GRSurfaceOrigin.BottomLeft, colorType);
            SKCanvas canvas = surface.Canvas;

            // draw some lines
            canvas.Clear(SKColor.Parse("#003366"));
            var paint = new SKPaint
            {
                Color       = new SKColor(255, 255, 255, 50),
                IsAntialias = true
            };

            for (int i = 0; i < 1_000; i++)
            {
                SKPoint ptA = new SKPoint(rand.Next(width), rand.Next(height));
                SKPoint ptB = new SKPoint(rand.Next(width), rand.Next(height));
                canvas.DrawLine(ptA, ptB, paint);
            }

            // Force a display
            surface.Canvas.Flush();
            glControl1.SwapBuffers();

            // dispose to prevent memory access violations while exiting
            renderTarget?.Dispose();
            contextOpenGL?.Dispose();
            canvas?.Dispose();
            surface?.Dispose();

            // update the FPS display
            renderCount += 1;
            double elapsedSeconds = (double)stopwatch.ElapsedMilliseconds / 1000;

            Text = string.Format("Rendered {0} frames in {1:0.00} seconds ({2:0.00} Hz)", renderCount, elapsedSeconds, renderCount / elapsedSeconds);
        }
コード例 #26
0
        public Window(string title, int w, int h)
        {
            const int x = SDL.SDL_WINDOWPOS_CENTERED;
            const int y = SDL.SDL_WINDOWPOS_CENTERED;

            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 0);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE);

            int kStencilBits = 8;  // Skia needs 8 stencil bits

            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, 8);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, 8);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DEPTH_SIZE, 0);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_STENCIL_SIZE, kStencilBits);

            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ACCELERATED_VISUAL, 1);

            _window = SDL.SDL_CreateWindow(title, x, y, w, h, WINDOW_FLAGS);

            // try and setup a GL context
            _glContext = SDL.SDL_GL_CreateContext(_window);
            if (_glContext == IntPtr.Zero)
            {
                throw new Exception("Unable to create OpenGL context.");
            }

            SDL.SDL_GL_MakeCurrent(_window, _glContext);

            var windowFormat = SDL.SDL_GetWindowPixelFormat(_window);
            int contextType;

            SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, out contextType);

            /*glViewport(0, 0, dw, dh);
             * glClearColor(1, 1, 1, 1);
             * glClearStencil(0);
             * glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);*/

            var grInterface = GRGlInterface.CreateNativeGlInterface();
            var grContext   = GRContext.Create(GRBackend.OpenGL, grInterface);

            _surface = SKSurface.Create(grContext, CreateRenderTarget());
        }
コード例 #27
0
        private void DrawGpuSurface(Action <SKSurface, SKImageInfo> draw)
        {
            using (var ctx = CreateGlContext())
            {
                ctx.MakeCurrent();

                var info = new SKImageInfo(100, 100);

                using (var grContext = GRContext.Create(GRBackend.OpenGL))
                    using (var surface = SKSurface.Create(grContext, true, info))
                    {
                        Assert.NotNull(surface);

                        draw(surface, info);
                    }
            }
        }
コード例 #28
0
        protected override void OnRenderFrame()
        {
            var rect = Allocation;

            // create the contexts if not done already
            if (grContext == null)
            {
                var glInterface = GRGlInterface.CreateNativeGlInterface();

                if (glInterface == null)
                {
                    throw new InvalidOperationException("Error creating OpenGL ES interface. Check if you have OpenGL ES correctly installed and configured or change the PFD Renderer to 'Software (CPU)' on the Global Settings panel.");
                }
                else
                {
                    grContext = GRContext.Create(GRBackend.OpenGL, glInterface);
                }

                try
                {
                    renderTarget = CreateRenderTarget();
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Error creating OpenGL ES render target. Check if you have OpenGL ES correctly installed and configured or change the PFD Renderer to 'Software (CPU)' on the Global Settings panel.", ex);
                }
            }

            if (grContext != null)
            {
                // update to the latest dimensions
                renderTarget = new GRBackendRenderTarget(rect.Width, rect.Height, renderTarget.SampleCount, renderTarget.StencilBits, renderTarget.GetGlFramebufferInfo());

                // create the surface
                using (var surface = SKSurface.Create(grContext, renderTarget, SKColorType.Rgba8888))
                {
                    if (PaintSurface != null)
                    {
                        PaintSurface.Invoke(surface);
                    }

                    surface.Canvas.Flush();
                }
            }
        }
コード例 #29
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            GlobalResources.OpenGLInterface = GRGlInterface.CreateNativeGlInterface();
            Debug.Assert(GlobalResources.OpenGLInterface.Validate());
            this.context = GRContext.Create(GRBackend.OpenGL, GlobalResources.OpenGLInterface);
            Debug.Assert(this.context.Handle != IntPtr.Zero);
            this.renderTarget = CreateRenderTarget(this);
            CursorVisible     = true;


            Location = new Point(Location.X, 0);
            _Present = new GamePresenter(this);
            StartGame();
            CurrentState = new GenericMenuState(StandardImageBackgroundSkia.GetMenuBackgroundDrawer(), this, new TitleMenuPopulator())
            {
                StateHeader = "BASeTris"
            };
        }
コード例 #30
0
ファイル: GlSkiaGpu.cs プロジェクト: yahiheb/Avalonia
        public GlSkiaGpu(IWindowingPlatformGlFeature gl, long?maxResourceBytes)
        {
            var context = gl.MainContext;

            using (context.MakeCurrent())
            {
                using (var iface = context.Version.Type == GlProfileType.OpenGL ?
                                   GRGlInterface.AssembleGlInterface((_, proc) => context.GlInterface.GetProcAddress(proc)) :
                                   GRGlInterface.AssembleGlesInterface((_, proc) => context.GlInterface.GetProcAddress(proc)))
                {
                    _grContext = GRContext.Create(GRBackend.OpenGL, iface);
                    if (maxResourceBytes.HasValue)
                    {
                        _grContext.GetResourceCacheLimits(out var maxResources, out _);
                        _grContext.SetResourceCacheLimits(maxResources, maxResourceBytes.Value);
                    }
                }
            }
        }