Exemplo n.º 1
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);
            }

            QuickPlot.Render.RandomLines(surface);

            surface.Canvas.Flush();
            glControl1.SwapBuffers();
            // glControl1.Invalidate();
        }
Exemplo n.º 2
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;
        }
Exemplo n.º 3
0
 private void CreateContext()
 {
     if (context == null)
     {
         var glInterface = GRGlInterface.CreateNativeGlInterface();
         context = GRContext.Create(GRBackend.OpenGL, glInterface);
     }
 }
Exemplo n.º 4
0
        public override void PrepareOpenGL()
        {
            base.PrepareOpenGL();

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

            context = GRContext.Create(GRBackend.OpenGL, glInterface);
        }
Exemplo n.º 5
0
        private GRContext GenerateSkiaContext(NativeWindow nativeWindow)
        {
            var glInterface = GRGlInterface.CreateNativeGlInterface();

            if (glInterface == null)
            {
                throw new PlatformNotSupportedException();
            }
            return(GRContext.CreateGl(glInterface));
        }
Exemplo n.º 6
0
        public void CreateSpecificContextIsValid()
        {
            using (var ctx = CreateGlContext()) {
                ctx.MakeCurrent();

                var glInterface = GRGlInterface.CreateNativeGlInterface();

                var grContext = GRContext.Create(GRBackend.OpenGL, glInterface);
            }
        }
Exemplo n.º 7
0
        public void CreateDefaultInterfaceIsValid()
        {
            using (var ctx = CreateGlContext()) {
                ctx.MakeCurrent();

                var glInterface = GRGlInterface.CreateNativeGlInterface();

                Assert.NotNull(glInterface);
                Assert.True(glInterface.Validate());
            }
        }
Exemplo n.º 8
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)
                {
                    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();
                }
            }
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
            }

            // manage the drawing surface
            var surfaceWidth  = (int)(Bounds.Width * ContentsScale);
            var surfaceHeight = (int)(Bounds.Height * ContentsScale);

            if (renderTarget == null || surface == null || renderTarget.Width != surfaceWidth || renderTarget.Height != surfaceHeight)
            {
                // 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(surfaceWidth, surfaceHeight, 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);
                SKDelegate?.DrawInSurface(e.Surface, e.RenderTarget);
#pragma warning restore CS0618 // Type or member is obsolete
            }

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

            base.DrawInCGLContext(glContext, pixelFormat, timeInterval, ref timeStamp);
        }
Exemplo n.º 11
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();
        }
Exemplo n.º 12
0
        public void CreateSpecificContextIsValid()
        {
            using (var ctx = CreateGlContext()) {
                ctx.MakeCurrent();

                var glInterface = GRGlInterface.CreateNativeGlInterface();

                Assert.True(glInterface.Validate());

                var grContext = GRContext.CreateGl(glInterface);

                Assert.NotNull(grContext);
            }
        }
Exemplo n.º 13
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (designMode)
            {
                e.Graphics.Clear(BackColor);
                return;
            }

            base.OnPaint(e);

            MakeCurrent();

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

            // manage the drawing surface
            if (renderTarget == null || surface == null || renderTarget.Width != Width || renderTarget.Height != Height)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer);
                GL.GetInteger(GetPName.StencilBits, out var stencil);
                GL.GetInteger(GetPName.Samples, out var samples);
                var maxSamples = grContext.GetMaxSurfaceSampleCount(colorType);
                if (samples > maxSamples)
                {
                    samples = maxSamples;
                }
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget(Width, Height, samples, stencil, glInfo);

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

            using (new SKAutoCanvasRestore(surface.Canvas, true))
            {
                // start drawing
                OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType));
            }

            // update the control
            surface.Canvas.Flush();
            SwapBuffers();
        }
Exemplo n.º 14
0
        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,
                };
            }
        }
Exemplo n.º 15
0
        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();
        }
Exemplo n.º 16
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
            };
        }
Exemplo n.º 17
0
        public void OnDrawFrame(IGL10 gl)
        {
            GLES10.GlClear(GLES10.GlColorBufferBit | GLES10.GlDepthBufferBit | GLES10.GlStencilBufferBit);

            // 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 != surfaceWidth || renderTarget.Height != surfaceHeight)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                var buffer = new int[3];
                GLES20.GlGetIntegerv(GLES20.GlFramebufferBinding, buffer, 0);
                GLES20.GlGetIntegerv(GLES20.GlStencilBits, buffer, 1);
                GLES20.GlGetIntegerv(GLES20.GlSamples, buffer, 2);
                var samples    = buffer[2];
                var maxSamples = context.GetMaxSurfaceSampleCount(colorType);
                if (samples > maxSamples)
                {
                    samples = maxSamples;
                }
                var glInfo = new GRGlFramebufferInfo((uint)buffer[0], colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget(surfaceWidth, surfaceHeight, samples, buffer[1], 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
                OnDrawFrame(e.Surface, e.RenderTarget);
#pragma warning restore CS0618 // Type or member is obsolete
            }

            // flush the SkiaSharp contents to GL
            surface.Canvas.Flush();
            context.Flush();
        }
Exemplo n.º 18
0
        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
            };
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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());
        }
Exemplo n.º 21
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();
                }
            }
        }
Exemplo n.º 22
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);
            }

            // manage the drawing surface
            var surfaceWidth  = (int)(Bounds.Width * ContentsScale);
            var surfaceHeight = (int)(Bounds.Height * ContentsScale);

            if (renderTarget == null || surface == null || renderTarget.Width != surfaceWidth || renderTarget.Height != surfaceHeight)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                renderTarget = SKGLDrawable.CreateRenderTarget(surfaceWidth, surfaceHeight);

                // create the surface
                surface?.Dispose();
                surface = SKSurface.Create(context, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
            }

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

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

            base.DrawInCGLContext(glContext, pixelFormat, timeInterval, ref timeStamp);
        }
Exemplo n.º 23
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"
            };
        }
Exemplo n.º 24
0
        public new void DrawInRect(GLKView view, CGRect rect)
        {
#if false
            if (designMode)
            {
                return;
            }
#endif

            // 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 int framebuffer);
                Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out int stencil);
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                _renderTarget = new GRBackendRenderTarget((int)DrawableWidth, (int)DrawableHeight, _context.GetMaxSurfaceSampleCount(colorType), 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);
                PaintSurface(_surface);
            }

            // flush the SkiaSharp contents to GL
            _surface.Canvas.Flush();
            _context.Flush();
        }
Exemplo n.º 25
0
        private void Form1_Load(object sender, EventArgs e)
        {
            SKColorType colorType = SKColorType.Rgba8888;

            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 stencilBits);
            renderTarget = new GRBackendRenderTarget(
                width: skglControl1.Width,
                height: skglControl1.Height,
                sampleCount: contextOpenGL.GetMaxSurfaceSampleCount(colorType),
                stencilBits: stencilBits,
                glInfo: glInfo);
            surface = SKSurface.Create(
                context: contextOpenGL,
                renderTarget: renderTarget,
                origin: GRSurfaceOrigin.BottomLeft,
                colorType: colorType);
        }
Exemplo n.º 26
0
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.MakeCurrent();

            //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))
            {
                surface.Canvas.Clear(SKColors.White);

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

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

                surface.Canvas.Flush();
            }

            // update the control
            SwapBuffers();
        }
Exemplo n.º 27
0
        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();
                renderTarget = SKGLDrawable.CreateRenderTarget((int)DrawableWidth, (int)DrawableHeight);

                // create the surface
                surface?.Dispose();
                surface = SKSurface.Create(context, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
            }

            using (new SKAutoCanvasRestore(surface.Canvas, true))
            {
                // start drawing
                var e = new SKPaintGLSurfaceEventArgs(surface, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
                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();
        }
Exemplo n.º 28
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (designMode)
            {
                e.Graphics.Clear(BackColor);
                return;
            }

            base.OnPaint(e);

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

            // manage the drawing surface
            if (renderTarget == null || surface == null || renderTarget.Width != Width || renderTarget.Height != Height)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                renderTarget = SKGLDrawable.CreateRenderTarget(Width, Height);

                // create the surface
                surface?.Dispose();
                surface = SKSurface.Create(grContext, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
            }

            using (new SKAutoCanvasRestore(surface.Canvas, true))
            {
                // start drawing
                OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888));
            }

            // update the control
            surface.Canvas.Flush();
            SwapBuffers();
        }
Exemplo n.º 29
0
        void GLSurfaceView.IRenderer.OnSurfaceCreated(IGL10 gl, EGLConfig config)
        {
            FreeContext();

            // get the config
            var egl  = EGLContext.EGL.JavaCast <IEGL10>();
            var disp = egl.EglGetCurrentDisplay();

            // stencil buffers
            int[] stencilbuffers = new int[1];
            egl.EglGetConfigAttrib(disp, config, EGL10.EglStencilSize, stencilbuffers);

            // samples
            int[] samples = new int[1];
            egl.EglGetConfigAttrib(disp, config, EGL10.EglSamples, samples);

            // get the frame buffer
            int[] framebuffers = new int[1];
            gl.GlGetIntegerv(GLES20.GlFramebufferBinding, framebuffers, 0);

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

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

            // create the render target
            renderTarget = new GRBackendRenderTargetDesc
            {
                Width              = 0,    // set later
                Height             = 0,    // set later
                Config             = GRPixelConfig.Rgba8888,
                Origin             = GRSurfaceOrigin.TopLeft,
                SampleCount        = samples[0],
                StencilBits        = stencilbuffers[0],
                RenderTargetHandle = (IntPtr)framebuffers[0],
            };
        }
Exemplo n.º 30
0
        public void OnDrawFrame(IGL10 gl)
        {
            GLES10.GlClear(GLES10.GlColorBufferBit | GLES10.GlDepthBufferBit | GLES10.GlStencilBufferBit);

            // 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 != surfaceWidth || renderTarget.Height != surfaceHeight)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                renderTarget = SKGLDrawable.CreateRenderTarget(surfaceWidth, surfaceHeight);

                // create the surface
                surface?.Dispose();
                surface = SKSurface.Create(context, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
            }

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

            // flush the SkiaSharp contents to GL
            surface.Canvas.Flush();
            context.Flush();
        }