Exemplo n.º 1
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();
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        protected override void OnRenderFrame(Rect rect)
        {
            // clear everything
            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

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

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

                // create the surface
                surface?.Dispose();
                surface = SKSurface.Create(context, 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();
            context.Flush();
        }
Exemplo n.º 5
0
        public void Run()
        {
            var targetInterval = TimeSpan.FromSeconds(1 / _config.Window.FPS);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            while (_running)
            {
                var startTime = stopwatch.Elapsed;

                _time = startTime.TotalSeconds;
                Update();

                var canvas = _skScreenSurface.Canvas;
                canvas.Clear();
                Draw(canvas);

                _skContext.Flush();
                _tkContext.SwapBuffers();

                _tkWindow.ProcessEvents();

                var endTime = stopwatch.Elapsed;

                var pauseTime = (startTime + targetInterval) - endTime;
                if (pauseTime > TimeSpan.Zero)
                {
                    Thread.Sleep(pauseTime);
                }
            }
        }
Exemplo n.º 6
0
        protected override void OnRenderFrame(Rect rect)
        {
            // clear everything
            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

            // create the SkiaSharp context
            if (context == null)
            {
                glInterface = GRGlInterface.Create();
                context     = GRContext.CreateGl(glInterface);
            }

            // get the new surface size
            var newSize = new SKSizeI((int)rect.Width, (int)rect.Height);

            // manage the drawing surface
            if (renderTarget == null || lastSize != newSize || !renderTarget.IsValid)
            {
                // create or update the dimensions
                lastSize = newSize;

                // read the info from the buffer
                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;
                }

                glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());

                // destroy the old surface
                surface?.Dispose();
                surface = null;
                canvas  = null;

                // re-create the render target
                renderTarget?.Dispose();
                renderTarget = new GRBackendRenderTarget(newSize.Width, newSize.Height, samples, stencil, glInfo);
            }

            // create the surface
            if (surface == null)
            {
                surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType);
                canvas  = surface.Canvas;
            }

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

            // update the control
            canvas.Flush();
            context.Flush();
        }
Exemplo n.º 7
0
 public void Render(Action <RICanvas> inside)
 {
     RenderTarget.Width  = Width;
     RenderTarget.Height = Height;
     Context.ResetContext();
     using (var surface = SKSurface.Create(Context, RenderTarget)) {
         var canvas = surface.Canvas;
         inside(new RICanvas(canvas, Scale));
         canvas.Flush();
     }
     Context.Flush();
     GL.Disable(EnableCap.Blend);
     GL.Disable(EnableCap.VertexProgramPointSize);
     GL.BindVertexArray(0);
     GL.FrontFace(FrontFaceDirection.Cw);
     GL.Enable(EnableCap.FramebufferSrgb);
     GL.ActiveTexture(TextureUnit.Texture0);
     GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
     GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, 0);
     GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0);
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
     GL.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
     GL.UseProgram(0);
     GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
     GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
     GL.DrawBuffer(DrawBufferMode.Back);
     GL.Enable(EnableCap.Dither);
     GL.DepthMask(true);
     GL.Enable(EnableCap.Multisample);
     GL.Disable(EnableCap.ScissorTest);
 }
Exemplo n.º 8
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();
                    }
                }
            }
        }
Exemplo n.º 9
0
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

            // create the render target
            if (renderTarget == null || lastSize != newSize || !renderTarget.IsValid)
            {
                // create or update the dimensions
                lastSize = newSize;

                // read the info from the buffer
                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;
                }
                glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());

                // destroy the old surface
                surface?.Dispose();
                surface = null;
                canvas  = null;

                // re-create the render target
                renderTarget?.Dispose();
                renderTarget = new GRBackendRenderTarget(newSize.Width, newSize.Height, samples, stencil, glInfo);
            }

            // create the surface
            if (surface == null)
            {
                surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType);
                canvas  = surface.Canvas;
            }

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

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

            OpenGLContext.FlushBuffer();
        }
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
        public virtual void Render()
        {
            if (glContext == null)
            {
                PrepareGLContexts();
            }

            EAGLContext.SetCurrentContext(glContext);

            // manage the drawing surface
            if (renderTarget == null || surface == null)
            {
                // 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;
                }
                Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_WIDTH, out var bufferWidth);
                Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_HEIGHT, out var bufferHeight);
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget(bufferWidth, bufferHeight, 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();

            // present the GL buffers
            glContext.PresentRenderBuffer(Gles.GL_RENDERBUFFER);
            EAGLContext.SetCurrentContext(null);
        }
Exemplo n.º 12
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.Size;
            var scaling = session.Scaling;

            if (size.Width <= 0 || size.Height <= 0 || scaling < 0)
            {
                throw new InvalidOperationException(
                          $"Can't create drawing context for surface with {size} size and {scaling} scaling");
            }

            gl.Viewport(0, 0, size.Width, size.Height);
            gl.ClearStencil(0);
            gl.ClearColor(0, 0, 0, 0);
            gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            lock (_grContext)
            {
                _grContext.ResetContext();

                GRBackendRenderTarget renderTarget =
                    new GRBackendRenderTarget(size.Width, size.Height, disp.SampleCount, disp.StencilSize,
                                              new GRGlFramebufferInfo((uint)fb, GRPixelConfig.Rgba8888.ToGlSizedFormat()));
                var surface = SKSurface.Create(_grContext, renderTarget,
                                               GRSurfaceOrigin.BottomLeft,
                                               GRPixelConfig.Rgba8888.ToColorType());

                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();
                    renderTarget.Dispose();
                    _grContext.Flush();
                    session.Dispose();
                })));
            }
        }
Exemplo n.º 13
0
        void GLSurfaceView.IRenderer.OnDrawFrame(IGL10 gl)
        {
            // create the surface
            using (var surface = SKSurface.Create(context, renderTarget))
            {
                // draw using SkiaSharp
                OnDrawFrame(surface, renderTarget);

                surface.Canvas.Flush();
            }

            // flush the SkiaSharp contents to GL
            context.Flush();
        }
Exemplo n.º 14
0
        void IMTKViewDelegate.Draw(MTKView view)
        {
            if (designMode)
            {
                return;
            }

            if (backendContext.Device == null || backendContext.Queue == null || CurrentDrawable?.Texture == null)
            {
                return;
            }

            CanvasSize = DrawableSize.ToSKSize();

            if (CanvasSize.Width <= 0 || CanvasSize.Height <= 0)
            {
                return;
            }

            // create the contexts if not done already
            context ??= GRContext.CreateMetal(backendContext);

            const SKColorType     colorType     = SKColorType.Bgra8888;
            const GRSurfaceOrigin surfaceOrigin = GRSurfaceOrigin.TopLeft;

            // create the render target
            var metalInfo = new GRMtlTextureInfo(CurrentDrawable.Texture);

            using var renderTarget = new GRBackendRenderTarget((int)CanvasSize.Width, (int)CanvasSize.Height, (int)SampleCount, metalInfo);

            // create the surface
            using var surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType);
            using var canvas  = surface.Canvas;

            // start drawing
            var e = new SKPaintMetalSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType);

            OnPaintSurface(e);

            // flush the SkiaSharp contents
            canvas.Flush();
            surface.Flush();
            context.Flush();

            // present
            using var commandBuffer = backendContext.Queue.CommandBuffer();
            commandBuffer.PresentDrawable(CurrentDrawable);
            commandBuffer.Commit();
        }
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
        private void DrawSkia(FrameEventArgs e)
        {
            var renderCanvas = renderSurface.Canvas;

            renderCanvas.Clear(SKColors.CornflowerBlue);

            using (var paint = new SKPaint {
                IsAntialias = true, TextSize = 100, TextAlign = SKTextAlign.Center
            })
            {
                renderCanvas.DrawText("Hello World!", renderDesc.Width / 2, 150, paint);
            }
            using (var paint = new SKPaint {
                IsAntialias = true, TextSize = 24, Typeface = SKTypeface.FromFamilyName(null, SKTypefaceStyle.Italic)
            })
            {
                renderCanvas.DrawText($"V-Sync: {VSync}", 16, 16 + paint.TextSize, paint);
                renderCanvas.DrawText($"FPS: {1f / e.Time:0}", 16, 16 + paint.TextSize + 8 + paint.TextSize, paint);
            }

            renderCanvas.DrawSurface(textureSurface, (renderDesc.Width - textureDesc.Width) / 2, 200);

            context.Flush();
        }
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 void OnDrawFrame(IGL10 gl)
        {
            GLES10.GlClear(GLES10.GlStencilBufferBit);

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

                surface.Canvas.Flush();
            }

            // flush the SkiaSharp contents to GL
            context.Flush();
        }
Exemplo n.º 19
0
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

            // manage the drawing surface
            var size = ConvertSizeToBacking(Bounds.Size);

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

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

            OpenGLContext.FlushBuffer();
        }
Exemplo n.º 20
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.º 21
0
        protected sealed override void OnDrawFrame()
        {
            if (glSurface != IntPtr.Zero)
            {
                Gles.glClear(Gles.GL_STENCIL_BUFFER_BIT);

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

                    surface.Canvas.Flush();
                }

                // flush the SkiaSharp contents to GL
                context.Flush();
            }
        }
Exemplo n.º 22
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.º 23
0
        protected override void OnRenderFrame(Rect rect)
        {
            // clear everything
            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

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

            // manage the drawing surface
            if (renderTarget == null || surface == null || renderTarget.Width != (int)rect.Width || renderTarget.Height != (int)rect.Height)
            {
                // 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)rect.Width, (int)rect.Height, samples, stencil, glInfo);

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

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

            // update the control
            surface.Canvas.Flush();
            context.Flush();
        }
Exemplo n.º 24
0
        protected sealed override void OnDrawFrame()
        {
            if (glSurface != IntPtr.Zero)
            {
                Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

                if (surface != null && renderTarget != null && context != null)
                {
                    using (new SKAutoCanvasRestore(surface.Canvas, true))
                    {
                        // draw using SkiaSharp
                        OnDrawFrame(new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType));
                    }

                    // flush the SkiaSharp contents to GL
                    surface.Canvas.Flush();
                    context.Flush();
                }
            }
        }
Exemplo n.º 25
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();
        }
Exemplo n.º 26
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.º 27
0
        protected override void OnRenderFrame()
        {
            // create the contexts if not done already
            if (grContext == null)
            {
                var glInterface = GRGlInterface.Create();
                grContext = GRContext.CreateGl(glInterface);
            }
            // manage the drawing surface
            var alloc = Allocation;
            var res   = (int)Math.Max(1.0, Gdk.Screen.Default.Resolution / 96.0);
            var w     = Math.Max(0, alloc.Width * res);
            var h     = Math.Max(0, alloc.Height * res);

            if (renderTarget == null || surface == null || renderTarget.Width != w || renderTarget.Height != h)
            {
                // 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(COLOR_TYPE);
                if (samples > maxSamples)
                {
                    samples = maxSamples;
                }
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, COLOR_TYPE.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget(w, h, samples, stencil, glInfo);
                // create the surface
                surface?.Dispose();
                surface = SKSurface.Create(grContext, renderTarget, SURFACE_ORIGIN, COLOR_TYPE);
            }
            using (new SKAutoCanvasRestore(surface.Canvas, true))
            {
                RenderFrame?.Invoke(this, surface);
            }
            // update the control
            surface.Canvas.Flush();
            grContext.Flush();
        }
Exemplo n.º 28
0
        public virtual void Render()
        {
            if (glContext == null)
            {
                PrepareGLContexts();
            }

            EAGLContext.SetCurrentContext(glContext);

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

                // 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();

            // present the GL buffers
            glContext.PresentRenderBuffer(Gles.GL_RENDERBUFFER);
            EAGLContext.SetCurrentContext(null);
        }
Exemplo n.º 29
0
        protected sealed override void OnDrawFrame()
        {
            if (glSurface != IntPtr.Zero)
            {
                Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

                if (surface != null && renderTarget != null && context != null)
                {
                    using (new SKAutoCanvasRestore(canvas, true))
                    {
                        // draw using SkiaSharp
#pragma warning disable CS0612 // Type or member is obsolete
                        OnDrawFrame(new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType, glInfo));
#pragma warning restore CS0612 // Type or member is obsolete
                    }

                    // flush the SkiaSharp contents to GL
                    canvas.Flush();
                    context.Flush();
                }
            }
        }
Exemplo n.º 30
0
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            var size = ConvertSizeToBacking(Bounds.Size);

            renderTarget.Width  = (int)size.Width;
            renderTarget.Height = (int)size.Height;

            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();

            GL.Flush();
        }