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 Dispose() { surface?.Dispose(); renderTarget?.Dispose(); grContext?.Dispose(); base.Dispose(); }
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 ResizeScreen() { _skScreenSurface?.Dispose(); _skScreenSurface = null; _skScreenRenderTarget?.Dispose(); _skScreenRenderTarget = null; var screenRenderTargetInfo = new GRGlFramebufferInfo() { Format = (int)PixelInternalFormat.Rgba8, FramebufferObjectId = 0, }; var screenSize = _tkWindow.ClientSize; _skScreenRenderTarget = new GRBackendRenderTarget(screenSize.Width, screenSize.Height, 1, 0, screenRenderTargetInfo); if (_skScreenRenderTarget == null) { throw new Exception($"Failed to create SkiaSharp screen render target."); } _skScreenSurface = SKSurface.Create(_skContext, _skScreenRenderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888); if (_skScreenSurface == null) { throw new Exception($"Failed to create SkiaSharp screen surface."); } foreach (var caption in _captions) { caption.Lines = null; } }
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(); renderTarget = SKGLDrawable.CreateRenderTarget(surfaceSize.Width, surfaceSize.Height); // create the surface surface?.Dispose(); surface = SKSurface.Create(context, renderTarget, GRSurfaceOrigin.BottomLeft, SKImageInfo.PlatformColorType); } }
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; } }
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 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(); }
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.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(); }
protected override void OnClosing(CancelEventArgs e) { renderTarget?.Dispose(); contextOpenGL?.Dispose(); surface?.Dispose(); base.OnClosing(e); }
private void UnoGLDrawingArea_Render(object o, RenderArgs args) { args.Context.MakeCurrent(); // create the contexts if not done already if (_grContext == null) { _grContext = TryBuildGRContext(); } // manage the drawing surface var res = (int)Math.Max(1.0, Screen.Resolution / 96.0); var w = Math.Max(0, AllocatedWidth * res); var h = Math.Max(0, AllocatedHeight * res); if (_renderTarget == null || _surface == null || _renderTarget.Width != w || _renderTarget.Height != h) { // create or update the dimensions _renderTarget?.Dispose(); var(framebuffer, stencil, samples) = GetGLBuffers(); var maxSamples = _grContext.GetMaxSurfaceSampleCount(colorType); if (samples > maxSamples) { samples = maxSamples; } var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat()); _renderTarget = new GRBackendRenderTarget(w + GuardBand, h + GuardBand, samples, stencil, glInfo); // create the surface _surface?.Dispose(); _surface = SKSurface.Create(_grContext, _renderTarget, surfaceOrigin, colorType); if (this.Log().IsEnabled(LogLevel.Trace)) { this.Log().Trace($"Recreate render surface {w}x{h} colorType:{colorType} sample:{samples}"); } } GLClear(); var canvas = _surface.Canvas; using (new SKAutoCanvasRestore(canvas, true)) { canvas.Clear(BackgroundColor); canvas.Translate(new SKPoint(0, GuardBand)); WUX.Window.Current.Compositor.Render(_surface); } // update the control canvas.Flush(); GLFlush(); }
public void Dispose() { _surface.Canvas.Flush(); _surface.Dispose(); _backendRenderTarget.Dispose(); GrContext.Flush(); _glSession.Dispose(); }
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(); }
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); }
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 void Dispose() { lock (_vulkanSession.Display.Lock) { SkSurface.Canvas.Flush(); SkSurface.Dispose(); _backendRenderTarget.Dispose(); GrContext.Flush(); _vulkanSession.Dispose(); } }
protected override void OnPaint(PaintEventArgs e) { if (designMode) { e.Graphics.Clear(BackColor); return; } base.OnPaint(e); // create the contexts if not done already if (grContext == null) { var glInterface = GRGlInterface.CreateNativeGlInterface(); grContext = GRContext.Create(GRBackend.OpenGL, glInterface); } // manage the drawing surface if (renderTarget == null || surface == null || renderTarget.Width != Width || renderTarget.Height != Height) { // create or update the dimensions renderTarget?.Dispose(); renderTarget = SKGLDrawable.CreateRenderTarget(Width, Height); // create the surface surface?.Dispose(); surface = SKSurface.Create(grContext, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888); } using (new SKAutoCanvasRestore(surface.Canvas, true)) { // start drawing OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888)); } // update the control surface.Canvas.Flush(); SwapBuffers(); }
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(); }))); } }
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); }
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(); }
public void OnDrawFrame(IGL10 gl) { GLES10.GlClear(GLES10.GlColorBufferBit | GLES10.GlDepthBufferBit | GLES10.GlStencilBufferBit); // create the contexts if not done already if (context == null) { var glInterface = GRGlInterface.CreateNativeGlInterface(); context = GRContext.Create(GRBackend.OpenGL, glInterface); } // manage the drawing surface if (renderTarget == null || surface == null || renderTarget.Width != surfaceWidth || renderTarget.Height != surfaceHeight) { // create or update the dimensions renderTarget?.Dispose(); renderTarget = SKGLDrawable.CreateRenderTarget(surfaceWidth, surfaceHeight); // create the surface surface?.Dispose(); surface = SKSurface.Create(context, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888); } using (new SKAutoCanvasRestore(surface.Canvas, true)) { // start drawing var e = new SKPaintGLSurfaceEventArgs(surface, renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888); OnPaintSurface(e); #pragma warning disable CS0618 // Type or member is obsolete OnDrawFrame(e.Surface, e.RenderTarget); #pragma warning restore CS0618 // Type or member is obsolete } // flush the SkiaSharp contents to GL surface.Canvas.Flush(); context.Flush(); }
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(); }
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); }
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 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); }
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 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(); }
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(); }
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); }