예제 #1
0
        public ISkiaGpuRenderSession BeginRenderingSession()
        {
            if (_currentSize != _data.Size || _surface == null)
            {
                _data.Texture?.Dispose();
                _surface?.Dispose();
                _surface      = null;
                _data.Texture = null;

                var game         = AvaloniaLocator.Current.GetService <IGame>();
                var gameSettings = game.Services.GetService <IGameSettingsService>().Settings;
                var device       = game.GraphicsDevice;

                StrideDispatcher.StrideThread.InvokeAsync(() =>
                {
                    _data.Texture = Texture.New2D(device, _data.Size.Width, _data.Size.Height, PixelFormat.B8G8R8A8_UNorm);
                }).Wait(1000);
                if (gameSettings.Configurations.Get <RenderingSettings>().ColorSpace == ColorSpace.Linear)
                {
                    _surface = SKSurface.Create(new SKImageInfo(_data.Size.Width, _data.Size.Height, SKColorType.Bgra8888, SKAlphaType.Premul, SKColorSpace.CreateSrgbLinear()));
                }
                else
                {
                    _surface = SKSurface.Create(new SKImageInfo(_data.Size.Width, _data.Size.Height, SKColorType.Bgra8888, SKAlphaType.Premul));
                }
                _currentSize = _data.Size;
            }
            return(new Session(_data, _surface, _options.WaitCopyTexture));
        }
예제 #2
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();
        }
예제 #3
0
 public override void Dispose()
 {
     surface?.Dispose();
     renderTarget?.Dispose();
     grContext?.Dispose();
     base.Dispose();
 }
예제 #4
0
        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);
            }
        }
예제 #5
0
        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;
            }
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
 private void InitBackSurf()
 {
     _backSurface?.Dispose();
     _backSurface = SKSurface.Create(GrContext, new GRBackendRenderTargetDesc()
     {
         Width       = Target.Size.Width,
         Height      = Target.Size.Height,
         SampleCount = 0,
         StencilBits = 0,
         Config      = GRPixelConfig.Rgba8888
     });
 }
예제 #8
0
        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;
            }
        }
예제 #9
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (designMode)
            {
                e.Graphics.Clear(BackColor);
                return;
            }

            base.OnPaint(e);

            MakeCurrent();

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

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

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

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

            // update the control
            surface.Canvas.Flush();
            SwapBuffers();
        }
예제 #10
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();
        }
예제 #11
0
        public void DrawSurface(CGContext ctx, CGRect viewBounds, SKImageInfo info, SKSurface surface)
        {
            surface.Canvas.Flush();
            surface.Dispose();

            // draw the image onto the context
            using (var dataProvider = new CGDataProvider(bitmapData, lastLength))
                using (var colorSpace = CGColorSpace.CreateDeviceRGB())
                    using (var image = new CGImage(info.Width, info.Height, BitsPerByte, info.BytesPerPixel * BitsPerByte, info.RowBytes, colorSpace, BitmapFlags, dataProvider, null, false, CGColorRenderingIntent.Default))
                    {
#if __IOS__ || __TVOS__
                        // we need to flip the image as we are mixing CoreGraphics and UIKit functions:
                        // https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html#//apple_ref/doc/uid/TP40010156-CH14-SW26
                        ctx.SaveState();
                        ctx.TranslateCTM(0, viewBounds.Height);
                        ctx.ScaleCTM(1, -1);
                        // draw the image
                        ctx.DrawImage(viewBounds, image);
                        ctx.RestoreState();
#elif __MACOS__
                        // draw the image
                        ctx.DrawImage(viewBounds, image);
#else
#error Plaform-specific code missing
#endif
                    }
        }
예제 #12
0
 public override void Dispose()
 {
     _canvas.Dispose();
     _surface.Dispose();
     _framebuffer.Dispose();
     base.Dispose();
 }
예제 #13
0
 public void Dispose()
 {
     if (_surface != null)
     {
         _surface.Dispose();
     }
 }
예제 #14
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();
        }
예제 #15
0
        public override void Dispose()
        {
            if (_skiaCanvas != null)
            {
                _skiaCanvas.Dispose();
                _skiaCanvas = null;
            }

            if (_surface != null)
            {
                _surface.Dispose();
                _surface = null;
            }

            if (_image != null)
            {
                _image.Dispose();
                _image = null;
            }

            if (_bitmap != null && _disposeBitmap)
            {
                _bitmap.Dispose();
                _bitmap = null;
            }

            _canvas = null;

            base.Dispose();
        }
예제 #16
0
 protected override void OnClosing(CancelEventArgs e)
 {
     renderTarget?.Dispose();
     contextOpenGL?.Dispose();
     surface?.Dispose();
     base.OnClosing(e);
 }
예제 #17
0
        public object EndRender()
        {
            var image = _surface.Snapshot();

            _surface.Dispose();
            return(image);
        }
예제 #18
0
        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
            };
        }
예제 #19
0
        protected override void OnResize(ResizeEventArgs e)
        {
            GL.Viewport(0, 0, e.Width, e.Height);
            _surface?.Dispose();
            _surface = null;
            var fbo          = GL.GetInteger(GetPName.FramebufferBinding);
            var stencilDepth = GL.GetInteger((GetPName)0x0D57);
            var samples      = GL.GetInteger((GetPName)0x80A9);
            var target       = new GRBackendRenderTarget(e.Width, e.Height,
                                                         samples, stencilDepth, new GRGlFramebufferInfo((uint)fbo, GRPixelConfig.Rgba8888.ToGlSizedFormat()));

            _surface = SKSurface.Create(GrContext, target, GRSurfaceOrigin.TopLeft,
                                        GRPixelConfig.Rgba8888.ToColorType());
            Canvas = _surface.Canvas;
            base.OnResize(e);
        }
예제 #20
0
        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();
        }
예제 #21
0
 public void Dispose()
 {
     _surface.Canvas.Flush();
     _surface.Dispose();
     _backendRenderTarget.Dispose();
     GrContext.Flush();
     _glSession.Dispose();
 }
예제 #22
0
        /// <summary>
        /// Called when the surface has been created
        /// </summary>
        /// <param name="backBuffer">The back buffer.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="stride">The stride.</param>
        protected override void OnSurfaceCreated(IntPtr backBuffer, int width, int height, int stride)
        {
            if (_surface != null)
            {
                _surface.Dispose();
            }

            _surface = SKSurface.Create(new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Premul), backBuffer, stride);
        }
예제 #23
0
        private void FreeDrawingObjects()
        {
            pix?.Dispose();
            pix = null;

            // SkiaSharp objects should only exist if the Pixbuf is set as well
            surface?.Dispose();
            surface = null;
        }
예제 #24
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();
        }
예제 #25
0
 //Dispose everything to not get memory exceptions
 protected override void OnClosing(CancelEventArgs e)
 {
     _rasterImg.Dispose();
     _surface.Dispose();
     foreach (var img in _textureImages)
     {
         img.Dispose();
     }
     base.OnClosing(e);
 }
예제 #26
0
        protected override bool OnDrawn(Context cr)
        {
            Stopwatch?sw = null;

            if (this.Log().IsEnabled(LogLevel.Trace))
            {
                sw = Stopwatch.StartNew();
                this.Log().Trace($"Render {renderCount++}");
            }

            var scaledWidth  = (int)(AllocatedWidth * _dpi);
            var scaledHeight = (int)(AllocatedHeight * _dpi);

            // reset the surfaces (skia/cairo) and bitmap if the size has changed
            if (_surface == null || scaledWidth != _bwidth || scaledHeight != _bheight)
            {
                _gtkSurface?.Dispose();
                _surface?.Dispose();
                _bitmap?.Dispose();

                var info = new SKImageInfo(scaledWidth, scaledHeight, _colorType, SKAlphaType.Premul);
                _bitmap  = new SKBitmap(info);
                _bwidth  = _bitmap.Width;
                _bheight = _bitmap.Height;
                var pixels = _bitmap.GetPixels(out _);
                _surface    = SKSurface.Create(info, pixels);
                _gtkSurface = new ImageSurface(pixels, Format.Argb32, _bwidth, _bheight, _bwidth * 4);
            }

            var canvas = _surface.Canvas;

            using (new SKAutoCanvasRestore(canvas, true))
            {
                canvas.Clear(BackgroundColor);
                canvas.Scale(_dpi);

                WUX.Window.Current.Compositor.Render(_surface);
            }

            _gtkSurface !.MarkDirty();
            cr.Save();
            cr.Scale(1 / _dpi, 1 / _dpi);
            cr.SetSourceSurface(_gtkSurface, 0, 0);
            cr.Paint();
            cr.Restore();

            if (this.Log().IsEnabled(LogLevel.Trace))
            {
                sw?.Stop();
                this.Log().Trace($"Frame: {sw?.Elapsed}");
            }

            return(true);
        }
예제 #27
0
        private void OnUnload(object sender, EventArgs e)
        {
            textureSurface.Dispose();
            textureSurface = null;

            renderSurface.Dispose();
            renderSurface = null;

            context.Dispose();
            context = null;
        }
예제 #28
0
        void RenderWithSkia()
        {
            int width  = glControl1.Width;
            int height = glControl1.Height;

            if (field == null || field.width != width || field.height != height)
            {
                field = new Starfield.Field(100_000, width, height);
            }
            field.StepForward();

            // Create a Skia surface using the OpenGL control
            SKColorType colorType     = SKColorType.Rgba8888;
            GRContext   contextOpenGL = GRContext.Create(GRBackend.OpenGL, GRGlInterface.CreateNativeGlInterface());

            GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer);
            GRGlFramebufferInfo glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());

            GL.GetInteger(GetPName.StencilBits, out var stencil);
            GRBackendRenderTarget renderTarget = new GRBackendRenderTarget(width, height, contextOpenGL.GetMaxSurfaceSampleCount(colorType), stencil, glInfo);
            SKSurface             surface      = SKSurface.Create(contextOpenGL, renderTarget, GRSurfaceOrigin.BottomLeft, colorType);
            SKCanvas canvas = surface.Canvas;

            // draw the starfield
            var paint = new SKPaint
            {
                Color       = SKColors.White,
                IsAntialias = true
            };

            canvas.Clear(SKColors.Black);
            foreach (Starfield.Star star in field.stars)
            {
                //canvas.DrawRect(star.X, star.Y, star.Size, star.Size, paint);
                canvas.DrawCircle(new SKPoint(star.X, star.Y), star.Size / 2, paint);
            }

            // Force a display
            surface.Canvas.Flush();
            glControl1.SwapBuffers();

            // dispose to prevent memory access violations while exiting
            renderTarget?.Dispose();
            contextOpenGL?.Dispose();
            canvas?.Dispose();
            surface?.Dispose();

            // update the FPS display
            renderCount += 1;
            double elapsedSeconds = (double)stopwatch.ElapsedMilliseconds / 1000;

            Text = string.Format("Rendered {0} frames in {1:0.00} seconds ({2:0.00} Hz)", renderCount, elapsedSeconds, renderCount / elapsedSeconds);
        }
예제 #29
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);
        }
예제 #30
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (designMode)
            {
                e.Graphics.Clear(BackColor);
                return;
            }

            base.OnPaint(e);

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

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

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

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

            // update the control
            surface.Canvas.Flush();
            SwapBuffers();
        }