private void InitSkia() { if (grContext == null) { var glInterface = GRGlInterface.Create(); grContext = GRContext.CreateGl(glInterface); } // define the surface properties // create or update the dimensions renderTarget?.Dispose(); GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer); var stencil = 0; 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(CurrentSize.X, CurrentSize.Y, 0, 0, glInfo); // create the surface surface?.Dispose(); surface = SKSurface.Create(grContext, renderTarget, SurfaceOrigin, ColorType); Paint = new SKPaint { Color = new SKColor(0, 128, 0), IsAntialias = true }; }
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(); }
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(); }
protected sealed override void CreateDrawingSurface() { if (glSurface == IntPtr.Zero) { // create the surface glSurface = Evas.evas_gl_surface_create(glEvas, glConfigPtr, surfaceSize.Width, surfaceSize.Height); // copy the native surface to the image Evas.evas_gl_native_surface_get(glEvas, glSurface, out var nativeSurface); Evas.evas_object_image_native_surface_set(evasImage, ref nativeSurface); // switch to the current OpenGL context Evas.evas_gl_make_current(glEvas, glSurface, glContext); // resize the viewport Gles.glViewport(0, 0, surfaceSize.Width, surfaceSize.Height); // create the interface using the function pointers provided by the EFL var glInterface = GRGlInterface.CreateEvas(glEvas); if (glInterface == null) { Log.Error("SKGLSurfaceView", "Unable to create GRGlInterface."); } if (!glInterface.Validate()) { Log.Error("SKGLSurfaceView", "The created GRGlInterface was not valid."); } context?.Dispose(); context = GRContext.CreateGl(glInterface); if (context == null) { Log.Error("SKGLSurfaceView", "Unable to create the GRContext."); } // create the render target 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; } glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat()); renderTarget = new GRBackendRenderTarget(surfaceSize.Width, surfaceSize.Height, samples, stencil, glInfo); // create the surface surface?.Dispose(); surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType); canvas = surface.Canvas; } }
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); }
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); }
public ISkiaGpuRenderSession BeginRenderingSession() { var glSession = _surface.BeginDraw(); bool success = false; try { var disp = glSession.Context; var gl = disp.GlInterface; gl.GetIntegerv(GL_FRAMEBUFFER_BINDING, out var fb); var size = glSession.Size; var colorType = SKColorType.Rgba8888; var scaling = glSession.Scaling; if (size.Width <= 0 || size.Height <= 0 || scaling < 0) { glSession.Dispose(); throw new InvalidOperationException( $"Can't create drawing context for surface with {size} size and {scaling} scaling"); } lock (_grContext) { _grContext.ResetContext(); var samples = disp.SampleCount; var maxSamples = _grContext.GetMaxSurfaceSampleCount(colorType); if (samples > maxSamples) { samples = maxSamples; } var glInfo = new GRGlFramebufferInfo((uint)fb, colorType.ToGlSizedFormat()); var renderTarget = new GRBackendRenderTarget(size.Width, size.Height, samples, disp.StencilSize, glInfo); var surface = SKSurface.Create(_grContext, renderTarget, glSession.IsYFlipped ? GRSurfaceOrigin.TopLeft : GRSurfaceOrigin.BottomLeft, colorType); success = true; return(new GlGpuSession(_grContext, renderTarget, surface, glSession)); } } finally { if (!success) { glSession.Dispose(); } } }
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(); }
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(); }
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(); }
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(); }
public GRBackendRenderTarget CreateRenderTarget(GRContext grContext) { GL.GetInteger(GetPName.FramebufferBinding, out int framebuffer); GL.GetInteger(GetPName.StencilBits, out int stencil); GL.GetInteger(GetPName.Samples, out int samples); int bufferWidth = 0; int bufferHeight = 0; GL.GetRenderbufferParameter(RenderbufferTarget.Renderbuffer, RenderbufferParameterName.RenderbufferWidth, out bufferWidth); GL.GetRenderbufferParameter(RenderbufferTarget.Renderbuffer, RenderbufferParameterName.RenderbufferHeight, out bufferHeight); var colorType = SKColorType.Rgba8888; var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat()); return(new GRBackendRenderTarget(Width, Height, grContext.GetMaxSurfaceSampleCount(colorType), stencil, glInfo)); }
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(); }
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); }
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(); }
protected void TkRender(TimeSpan delta) { if (designMode || disposed) { return; } if (this.Visibility != Visibility.Visible) { return; } var size = CreateSize(); if (size.Width <= 0 || size.Height <= 0) { return; } // create the contexts if not done already if (grContext == null) { var glInterface = GRGlInterface.Create(); grContext = GRContext.CreateGl(glInterface); } // get the new surface size var newSize = new SKSizeI(size.Width, size.Height); // manage the drawing surface if (renderTarget == null || lastSize != newSize || !renderTarget.IsValid) { // create or update the dimensions lastSize = newSize; 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; } 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(grContext, 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(); }
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(); }
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.Create(); grContext = GRContext.CreateGl(glInterface); } // get the new surface size var newSize = new SKSizeI(Width, Height); // manage the drawing surface if (renderTarget == null || lastSize != newSize || !renderTarget.IsValid) { // create or update the dimensions lastSize = newSize; 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; } 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(grContext, renderTarget, surfaceOrigin, colorType); canvas = surface.Canvas; } using (new SKAutoCanvasRestore(canvas, true)) { // start drawing #pragma warning disable CS0612 // Type or member is obsolete OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType, glInfo)); #pragma warning restore CS0612 // Type or member is obsolete } // update the control canvas.Flush(); SwapBuffers(); }
private void Render(int cornerCount = 100, int maxVisibleDistance = 200) { // create the field if needed or if the size changed if (field == null || field.width != glControl1.Width || field.height != glControl1.Height) { field = new IntroAnimation.Field(glControl1.Width, glControl1.Height, cornerCount); } // step the field forward in time field.StepForward(3); // 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(glControl1.Width, glControl1.Height, contextOpenGL.GetMaxSurfaceSampleCount(colorType), stencil, glInfo); SKSurface surface = SKSurface.Create(contextOpenGL, renderTarget, GRSurfaceOrigin.BottomLeft, colorType); SKCanvas canvas = surface.Canvas; // draw the stuff var bgColor = field.GetBackgroundColor(); canvas.Clear(new SKColor(bgColor.R, bgColor.G, bgColor.B)); // draw circles at every corner var paint = new SKPaint { Color = new SKColor(255, 255, 255), IsAntialias = true }; float radius = 2; for (int cornerIndex = 0; cornerIndex < field.corners.Length; cornerIndex++) { canvas.DrawCircle((float)field.corners[cornerIndex].X, (float)field.corners[cornerIndex].Y, radius, paint); } // draw lines between every corner and every other corner for (int i = 0; i < field.corners.Length; i++) { for (int j = 0; j < field.corners.Length; j++) { double distance = field.GetDistance(i, j); if (distance < maxVisibleDistance && distance != 0) { SKPoint pt1 = new SKPoint((float)field.corners[i].X, (float)field.corners[i].Y); SKPoint pt2 = new SKPoint((float)field.corners[j].X, (float)field.corners[j].Y); double distanceFraction = distance / maxVisibleDistance; byte alpha = (byte)(255 - distanceFraction * 256); var linePaint = new SKPaint { Color = new SKColor(255, 255, 255, alpha), IsAntialias = true }; canvas.DrawLine(pt1, pt2, linePaint); } } } // 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 Text = field.GetBenchmarkMessage(); }
protected sealed override void CreateDrawingSurface() { if (glSurface == IntPtr.Zero) { // create the surface glSurface = Evas.evas_gl_surface_create(glEvas, glConfigPtr, surfaceSize.Width, surfaceSize.Height); // copy the native surface to the image Evas.evas_gl_native_surface_get(glEvas, glSurface, out var nativeSurface); Evas.evas_object_image_native_surface_set(evasImage, ref nativeSurface); // switch to the current OpenGL context Evas.evas_gl_make_current(glEvas, glSurface, glContext); // resize the viewport Gles.glViewport(0, 0, surfaceSize.Width, surfaceSize.Height); // create the interface using the function pointers provided by the EFL var glInterface = GRGlInterface.CreateNativeEvasInterface(glEvas); context?.Dispose(); context = GRContext.Create(GRBackend.OpenGL, glInterface); // create the render target renderTarget?.Dispose(); Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out var framebuffer); Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out var stencil); var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat()); renderTarget = new GRBackendRenderTarget(surfaceSize.Width, surfaceSize.Height, context.GetMaxSurfaceSampleCount(colorType), stencil, glInfo); // create the surface surface?.Dispose(); surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType); } }
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(); }
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); }
private void glControl1_Paint(object sender, PaintEventArgs e) { Control senderControl = (Control)sender; if (context == null) { var glInterface = GRGlInterface.CreateNativeGlInterface(); context = GRContext.Create(GRBackend.OpenGL, glInterface); } if (renderTarget == null || surface == null || renderTarget.Width != senderControl.Width || renderTarget.Height != senderControl.Height) { renderTarget?.Dispose(); GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer); GL.GetInteger(GetPName.StencilBits, out var stencil); var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat()); renderTarget = new GRBackendRenderTarget(senderControl.Width, senderControl.Height, context.GetMaxSurfaceSampleCount(colorType), stencil, glInfo); surface?.Dispose(); surface = SKSurface.Create(context, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888); } figure.Render(surface.Canvas, figureSize, plotEngagedWithMouse); surface.Canvas.Flush(); glControl1.SwapBuffers(); }
private void Render(int lineCount) { Stopwatch stopwatch = Stopwatch.StartNew(); // set up the Skia surface using OpenGL 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 stencilBits); GRBackendRenderTarget renderTarget = new GRBackendRenderTarget( width: skglControl1.Width, height: skglControl1.Height, sampleCount: contextOpenGL.GetMaxSurfaceSampleCount(colorType), stencilBits: stencilBits, glInfo: glInfo); SKSurface surface = SKSurface.Create( context: contextOpenGL, renderTarget: renderTarget, origin: GRSurfaceOrigin.BottomLeft, colorType: colorType); // perform the drawing surface.Canvas.Clear(SKColor.Parse("#003366")); for (int i = 0; i < lineCount; i++) { var paint = new SKPaint { Color = new SKColor( red: (byte)rand.Next(255), green: (byte)rand.Next(255), blue: (byte)rand.Next(255), alpha: (byte)rand.Next(255)), StrokeWidth = rand.Next(1, 10), IsAntialias = true }; surface.Canvas.DrawLine( x0: rand.Next(skglControl1.Width), y0: rand.Next(skglControl1.Height), x1: rand.Next(skglControl1.Width), y1: rand.Next(skglControl1.Height), paint: paint); } // Force a display surface.Canvas.Flush(); skglControl1.SwapBuffers(); // prevent memory access violations by disposing before exiting renderTarget?.Dispose(); contextOpenGL?.Dispose(); surface?.Dispose(); stopwatch.Stop(); renderTimesMsec.Add(1000.0 * stopwatch.ElapsedTicks / Stopwatch.Frequency); double mean = renderTimesMsec.Sum() / renderTimesMsec.Count(); Debug.WriteLine($"Render {renderTimesMsec.Count:00} " + $"took {renderTimesMsec.Last():0.000} ms " + $"(running mean: {mean:0.000} ms)"); Application.DoEvents(); }
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); }