Beispiel #1
0
        public void 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 render target
            renderTarget = new GRBackendRenderTargetDesc
            {
                Width              = 0,    // set later
                Height             = 0,    // set later
                Config             = GRPixelConfig.Rgba8888,
                Origin             = GRSurfaceOrigin.BottomLeft,
                SampleCount        = samples[0],
                StencilBits        = stencilbuffers[0],
                RenderTargetHandle = (IntPtr)framebuffers[0],
            };

            CreateContext();
        }
Beispiel #2
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();
        }
Beispiel #3
0
        public virtual void Render()
        {
            if (glContext == null)
            {
                PrepareGLContexts();
            }

            EAGLContext.SetCurrentContext(glContext);

            // create the surface
            if (renderTarget.Width == 0 || renderTarget.Height == 0)
            {
                renderTarget = SKGLDrawable.CreateRenderTarget();
            }
            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();

            // present the GL buffers
            glContext.PresentRenderBuffer(Gles.GL_RENDERBUFFER);
            EAGLContext.SetCurrentContext(null);
        }
Beispiel #4
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);
        }
Beispiel #5
0
            public override void DrawInSurface(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
            {
                base.DrawInSurface(surface, renderTarget);

                // the control is being repainted, let the user know
                controller.OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));
            }
 public virtual void DrawInSurface(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
     if (PaintSurface != null)
     {
         PaintSurface.Invoke(this, new SKPaintGLSurfaceEventArgs(surface, renderTarget));
     }
 }
Beispiel #7
0
        public override void DrawRect(CoreGraphics.CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            if (grContext != null)
            {
                var sampleCount = grContext.GetRecommendedSampleCount(GRPixelConfig.Rgba8888, 96.0f);

                var desc = new GRBackendRenderTargetDesc
                {
                    Width              = (int)Bounds.Width,
                    Height             = (int)Bounds.Height,
                    Config             = GRPixelConfig.Rgba8888,
                    Origin             = GRSurfaceOrigin.TopLeft,
                    SampleCount        = sampleCount,
                    StencilBits        = 0,
                    RenderTargetHandle = IntPtr.Zero,
                };

                using (var surface = SKSurface.Create(grContext, desc))
                {
                    var skcanvas = surface.Canvas;

                    sample.Method(skcanvas, (int)Bounds.Width, (int)Bounds.Height);

                    skcanvas.Flush();
                }

                GL.Flush();
            }
        }
        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();
        }
Beispiel #9
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (grContext != null)
            {
                var desc = new GRBackendRenderTargetDesc
                {
                    Width              = Width,
                    Height             = Height,
                    Config             = GRPixelConfig.Bgra8888,
                    Origin             = GRSurfaceOrigin.TopLeft,
                    SampleCount        = 1,
                    StencilBits        = 0,
                    RenderTargetHandle = IntPtr.Zero,
                };

                using (var surface = SKSurface.Create(grContext, desc))
                {
                    var skcanvas = surface.Canvas;

                    sample.Method(skcanvas, Width, Height);

                    skcanvas.Flush();
                }

                SwapBuffers();
            }
        }
Beispiel #10
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;
        }
 public SKPaintGLSurfaceEventArgs(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
     Surface             = surface;
     rtDesc              = renderTarget;
     BackendRenderTarget = new GRBackendRenderTarget(GRBackend.OpenGL, renderTarget);
     ColorType           = renderTarget.Config.ToColorType();
     Origin              = renderTarget.Origin;
 }
Beispiel #12
0
        static void Main(string[] args)
        {
            a.Add(new BezierPiece(new SKPoint(80, 80), new SKPoint(-80, 150)));
            a.Add(new BezierPiece(new SKPoint(280, 80), new SKPoint(380, 180)));
            a.Add(new BezierPiece(new SKPoint(680, 80), new SKPoint(580, 10)));

            using (window = new NativeWindow(800, 600, "BezierSharp", Monitor.None, Window.None))
            {
                Glfw.MakeContextCurrent(window);
                GRGlInterface             glInterface = GRGlInterface.AssembleGlInterface(Glfw.CurrentContext, (contextHandle, name) => Glfw.GetProcAddress(name));
                GRContext                 context     = GRContext.CreateGl(glInterface);
                GRBackendRenderTargetDesc backendRenderTargetDescription = new GRBackendRenderTargetDesc
                {
                    Config             = GRPixelConfig.Rgba8888,
                    Height             = 600,
                    Width              = 800,
                    Origin             = GRSurfaceOrigin.TopLeft,
                    RenderTargetHandle = new IntPtr(0),
                    SampleCount        = 0,
                    StencilBits        = 8
                };


                using (var surface = SKSurface.Create(context, backendRenderTargetDescription))
                {
                    var canvas = surface.Canvas;
                    int height = canvas.DeviceClipBounds.Height;
                    // Main application loop
                    while (!window.IsClosing)
                    {
                        AjustCurve(ref a, height);
                        canvas.Clear(SKColors.White);
                        using (var paint = new SKPaint {
                            Color = SKColors.Blue
                        })
                        {
                            paint.StrokeWidth = 3;
                            paint.Style       = SKPaintStyle.Stroke;
                            paint.IsAntialias = true;

                            a.Draw(ref canvas, paint, SKPathFillType.Winding);
                        }

                        // OpenGL rendering
                        // Implement any timing for flow control, etc (see Glfw.GetTime())
                        canvas.Flush();
                        context.Flush();
                        // Swap the front/back buffers
                        window.SwapBuffers();

                        // Poll native operating system events (must be called or OS will think application is hanging)
                        Glfw.PollEvents();
                    }
                }
            }
        }
Beispiel #13
0
 public SKPaintGLSurfaceEventArgs(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
     Surface             = surface;
     rtDesc              = renderTarget;
     BackendRenderTarget = new GRBackendRenderTarget(GRBackend.OpenGL, renderTarget);
     ColorType           = renderTarget.Config.ToColorType();
     Origin              = renderTarget.Origin;
     Info    = new SKImageInfo(renderTarget.Width, renderTarget.Height, ColorType);
     RawInfo = Info;
 }
Beispiel #14
0
        private void ViewOnPaintSurface(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
        {
            var canvas = surface.Canvas;

            canvas.Clear(new SKColor(50, 50, 50));

            Engine?.Step(1, 1);
            Engine?.Draw(canvas);

            //    _view.Invalidate();
        }
        public override void PrepareOpenGL()
        {
            base.PrepareOpenGL();

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

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

            renderTarget = SKGLDrawable.CreateRenderTarget();
        }
Beispiel #16
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();
                }
            }
        }
Beispiel #17
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
            };
        }
Beispiel #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
            };
        }
Beispiel #19
0
        public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer)
        {
            var session = _surface.BeginDraw();
            var disp    = session.Display;
            var gl      = disp.GlInterface;

            gl.GetIntegerv(GL_FRAMEBUFFER_BINDING, out var fb);

            var size    = session.PixelSize;
            var scaling = session.Scaling;

            GRBackendRenderTargetDesc desc = new GRBackendRenderTargetDesc
            {
                Width              = size.Width,
                Height             = size.Height,
                SampleCount        = disp.SampleCount,
                StencilBits        = disp.StencilSize,
                Config             = GRPixelConfig.Rgba8888,
                Origin             = GRSurfaceOrigin.BottomLeft,
                RenderTargetHandle = new IntPtr(fb)
            };

            gl.Viewport(0, 0, desc.Width, desc.Height);
            gl.ClearStencil(0);
            gl.ClearColor(0, 0, 0, 0);
            gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            var surface = SKSurface.Create(_grContext, desc);

            var nfo = new DrawingContextImpl.CreateInfo
            {
                GrContext               = _grContext,
                Canvas                  = surface.Canvas,
                Dpi                     = SkiaPlatform.DefaultDpi * scaling,
                VisualBrushRenderer     = visualBrushRenderer,
                DisableTextLcdRendering = true
            };

            return(new DrawingContextImpl(nfo, Disposable.Create(() =>
            {
                surface.Canvas.Flush();
                surface.Dispose();
                session.Dispose();
            })));
        }
Beispiel #20
0
        public SKGLSurfaceView(EvasObject parent)
            : base(parent)
        {
            glConfig = new Evas.Config()
            {
                color_format     = Evas.ColorFormat.RGBA_8888,
                depth_bits       = Evas.DepthBits.BIT_24,
                stencil_bits     = Evas.StencilBits.BIT_8,
                options_bits     = Evas.OptionsBits.NONE,
                multisample_bits = Evas.MultisampleBits.HIGH,
            };

            var isBgra = SKImageInfo.PlatformColorType == SKColorType.Bgra8888;

            renderTarget = new GRBackendRenderTargetDesc
            {
                Config = isBgra ? GRPixelConfig.Bgra8888 : GRPixelConfig.Rgba8888,
                Origin = GRSurfaceOrigin.BottomLeft,
            };
        }
Beispiel #21
0
        private void OnPaint(object sender, SKPaintGLSurfaceEventArgs e)
        {
            //clear screen
            GRBackendRenderTargetDesc viewInfo = e.RenderTarget;
            SKSurface surface = e.Surface;
            SKCanvas  canvas  = surface.Canvas;


            //scale from pixels to xamarin units (64 units to each centimeter)
            int    skiaPixels   = viewInfo.Width;
            double xamarinUnits = this.canvasView.Width;
            float  scaleFactor  = (float)(skiaPixels / xamarinUnits);

            canvas.Scale(scaleFactor);


            //clear screen
            canvas.Clear(SKColors.White);

            uvGraph.DrawGraph(e);
        }
Beispiel #22
0
        protected override void OnRenderFrame(Rect rect)
        {
            base.OnRenderFrame(rect);

            if (designMode)
            {
                return;
            }

            if (!isVisible)
            {
                return;
            }

            // create the SkiaSharp context
            if (context == null)
            {
                var glInterface = GRGlInterface.CreateNativeAngleInterface();
                context = GRContext.Create(GRBackend.OpenGL, glInterface);

                renderTarget = SKGLDrawable.CreateRenderTarget();
            }

            // set the size
            renderTarget.Width  = (int)rect.Width;
            renderTarget.Height = (int)rect.Height;

            // create the surface
            using (var surface = SKSurface.Create(context, renderTarget))
            {
                // draw to the SkiaSharp surface
                OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));

                // flush the canvas
                surface.Canvas.Flush();
            }

            // flush the SkiaSharp context to the GL context
            context.Flush();
        }
        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],
            };
        }
Beispiel #24
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);

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

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

            // create the surface
            using (var surface = SKSurface.Create(grContext, renderTarget))
            {
                // start drawing
                OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));

                surface.Canvas.Flush();
            }

            // update the control
            SwapBuffers();
        }
Beispiel #25
0
 public virtual void DrawInSurface(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
 }
 protected abstract void OnDrawFrame(SKSurface surface, GRBackendRenderTargetDesc renderTarget);
Beispiel #27
0
 protected override void OnDrawFrame(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
     surfaceView.skRenderer?.OnDrawFrame(surface, renderTarget);
 }
 protected virtual void OnDrawFrame(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
 }
Beispiel #29
0
 public void OnDrawFrame(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
     controller.OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));
 }
Beispiel #30
0
 public SKPaintGLSurfaceEventArgs(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
 {
     Surface      = surface;
     RenderTarget = renderTarget;
 }