Ejemplo n.º 1
0
        private GRBackendRenderTargetDesc CreateRenderTarget()
        {
            int framebuffer, stencil, samples;

            Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out framebuffer);
            Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out stencil);
            Gles.glGetIntegerv(Gles.GL_SAMPLES, out samples);

            int bufferWidth, bufferHeight;

            SDL.SDL_GL_GetDrawableSize(_window, out bufferWidth, out bufferHeight);
            Width  = bufferWidth;
            Height = bufferHeight;

            return(new GRBackendRenderTargetDesc
            {
                Width = bufferWidth,
                Height = bufferHeight,
                Config = GRPixelConfig.Rgba8888,
                Origin = GRSurfaceOrigin.BottomLeft,
                SampleCount = samples,
                StencilBits = stencil,
                RenderTargetHandle = (IntPtr)framebuffer,
            });
        }
Ejemplo n.º 2
0
        private void PrepareGLContexts()
        {
            // create GL context
            glContext = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
            EAGLContext.SetCurrentContext(glContext);

            // create render buffer
            Gles.glGenRenderbuffers(1, ref renderBuffer);
            Gles.glBindRenderbuffer(Gles.GL_RENDERBUFFER, renderBuffer);
            glContext.RenderBufferStorage(Gles.GL_RENDERBUFFER, this);

            // create frame buffer
            Gles.glGenFramebuffers(1, ref framebuffer);
            Gles.glBindFramebuffer(Gles.GL_FRAMEBUFFER, framebuffer);
            Gles.glFramebufferRenderbuffer(Gles.GL_FRAMEBUFFER, Gles.GL_COLOR_ATTACHMENT0, Gles.GL_RENDERBUFFER, renderBuffer);

            // get the bits for SkiaSharp
            var glInterface = GRGlInterface.CreateNativeGlInterface();

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

            // finished
            EAGLContext.SetCurrentContext(null);

            recreateSurface = true;
        }
Ejemplo n.º 3
0
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            if (fsurface == null)
            {
                return;
            }

            var size = ConvertSizeToBacking(Bounds.Size);

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

            Gles.glClear(Gles.GL_STENCIL_BUFFER_BIT);

            using (var surface = SKSurface.Create(context, renderTarget))
            {
                fsurface.UpdateSurface(surface);

                // draw on the surface
                DrawInSurface(surface, renderTarget);

                surface.Canvas.Flush();
            }

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

            OpenGLContext.FlushBuffer();
        }
Ejemplo n.º 4
0
        protected override void OnRenderFrame(Rect rect)
        {
            // clear everything
            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

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

            // 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();
        }
Ejemplo n.º 5
0
        private void CreateSurface()
        {
            if (glSurface == IntPtr.Zero)
            {
                // create the surface
                glSurface = Evas.evas_gl_surface_create(glEvas, glConfigPtr, renderTarget.Width, renderTarget.Height);

                // copy the native surface to the image
                Evas.NativeSurfaceOpenGL nativeSurface;
                Evas.evas_gl_native_surface_get(glEvas, glSurface, out 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, renderTarget.Width, renderTarget.Height);

                // initialize the Skia's context
                CreateContext();

                // copy the properties of the current surface
                var currentRenderTarget = SKGLDrawable.CreateRenderTarget();
                renderTarget.SampleCount        = currentRenderTarget.SampleCount;
                renderTarget.StencilBits        = currentRenderTarget.StencilBits;
                renderTarget.RenderTargetHandle = currentRenderTarget.RenderTargetHandle;
            }
        }
Ejemplo n.º 6
0
        public static GRBackendRenderTargetDesc CreateRenderTarget()
        {
            int framebuffer, stencil, samples;

            Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out framebuffer);
            Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out stencil);
            Gles.glGetIntegerv(Gles.GL_SAMPLES, out samples);

            int bufferWidth  = 0;
            int bufferHeight = 0;

#if __IOS__ || __TVOS__
            Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_WIDTH, out bufferWidth);
            Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_HEIGHT, out bufferHeight);
#endif

            return(new GRBackendRenderTargetDesc
            {
                Width = bufferWidth,
                Height = bufferHeight,
                Config = GRPixelConfig.Rgba8888,
                Origin = GRSurfaceOrigin.BottomLeft,
                SampleCount = samples,
                StencilBits = stencil,
                RenderTargetHandle = (IntPtr)framebuffer,
            });
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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();
        }
Ejemplo n.º 9
0
        public static GRBackendRenderTarget CreateRenderTarget()
        {
            Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out int framebuffer);
            Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out int stencil);
            Gles.glGetIntegerv(Gles.GL_SAMPLES, out int samples);

            int bufferWidth  = 0;
            int bufferHeight = 0;

            return(new GRBackendRenderTarget(bufferWidth, bufferHeight, samples, stencil, new GRGlFramebufferInfo((uint)framebuffer, GRPixelConfig.Rgba8888.ToGlSizedFormat())));
        }
Ejemplo n.º 10
0
        private void CreateSurface(SwapChainPanel panel, int rbWidth, int rbHeight, GlesBackingOption backingOption, GlesMultisampling multisampling, GlesRenderTarget renderTarget)
        {
            if (panel == null)
            {
                throw new ArgumentNullException(nameof(panel), "SwapChainPanel parameter is invalid");
            }

            if (rbWidth == 0 || rbHeight == 0)
            {
                rbWidth  = 320;
                rbHeight = 480;
            }

            // Create a temporary surface so that we can make the context current
            int[] surfaceAttributes =
            {
                Egl.EGL_WIDTH,                               rbWidth,
                Egl.EGL_HEIGHT,                              rbHeight,
                Egl.EGL_FIXED_SIZE_ANGLE,                    Egl.EGL_TRUE,

                // Egl.EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER is part of the same optimization as Egl.EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER (see above).
                // If you have compilation issues with it then please update your Visual Studio templates.
                Egl.EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER, Egl.EGL_TRUE,
                Egl.EGL_NONE
            };

            eglSurface = Egl.eglCreateWindowSurface(eglDisplay, eglConfig, panel, surfaceAttributes);
            if (eglSurface == Egl.EGL_NO_SURFACE)
            {
                throw new Exception("Failed to create EGL surface");
            }

            // Set up swap behavior.
            int swapBehavior = backingOption == GlesBackingOption.Retained ? Egl.EGL_BUFFER_PRESERVED : Egl.EGL_BUFFER_DESTROYED;

            if (Egl.eglSurfaceAttrib(eglDisplay, eglSurface, Egl.EGL_SWAP_BEHAVIOR, swapBehavior) == Egl.EGL_FALSE)
            {
                Debug.WriteLine("Unable to set up backbuffer swap behavior, app may experience graphical glitches!");
            }

            if (multisampling == GlesMultisampling.FourTimes)
            {
                Gles.glRenderbufferStorageMultisampleANGLE((uint)renderTarget, 4, Gles.GL_BGRA8_EXT, rbWidth, rbHeight);
            }
            else
            {
                Gles.glRenderbufferStorage((uint)renderTarget, Gles.GL_BGRA8_EXT, rbWidth, rbHeight);
            }

            if (CurrentContext == this)
            {
                Egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
            }
        }
Ejemplo n.º 11
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;
            }
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        public bool SwapBuffers(GlesRenderTarget renderTarget, int width, int height)
        {
            int curFB;

            Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out curFB);
            Gles.glBindFramebuffer(Gles.GL_DRAW_FRAMEBUFFER_ANGLE, 0);
            Gles.glBindFramebuffer(Gles.GL_READ_FRAMEBUFFER_ANGLE, (uint)curFB);
            Gles.glBlitFramebufferANGLE(0, 0, width, height, 0, 0, width, height, Gles.GL_COLOR_BUFFER_BIT, Gles.GL_NEAREST);
            Gles.glBindFramebuffer(Gles.GL_DRAW_FRAMEBUFFER_ANGLE, (uint)curFB);
            Gles.glBindFramebuffer(Gles.GL_READ_FRAMEBUFFER_ANGLE, (uint)curFB);

            return(Egl.eglSwapBuffers(eglDisplay, eglSurface) == Egl.EGL_TRUE);
        }
Ejemplo n.º 14
0
        private void ResizeGLContexts()
        {
            // nuke old buffers
            Gles.glDeleteRenderbuffers(1, ref renderBuffer);

            // re-create render buffer
            Gles.glGenRenderbuffers(1, ref renderBuffer);
            Gles.glBindRenderbuffer(Gles.GL_RENDERBUFFER, renderBuffer);
            glContext.RenderBufferStorage(Gles.GL_RENDERBUFFER, this);

            // re-link
            Gles.glFramebufferRenderbuffer(Gles.GL_FRAMEBUFFER, Gles.GL_COLOR_ATTACHMENT0, Gles.GL_RENDERBUFFER, renderBuffer);
        }
Ejemplo n.º 15
0
        public virtual void Render()
        {
            if (glContext == null)
            {
                PrepareGLContexts();
            }

            EAGLContext.SetCurrentContext(glContext);

            // manage the drawing surface
            if (renderTarget == null || surface == null)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out var framebuffer);
                Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out var stencil);
                Gles.glGetIntegerv(Gles.GL_SAMPLES, out var samples);
                var maxSamples = context.GetMaxSurfaceSampleCount(colorType);
                if (samples > maxSamples)
                {
                    samples = maxSamples;
                }
                Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_WIDTH, out var bufferWidth);
                Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_HEIGHT, out var bufferHeight);
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget(bufferWidth, bufferHeight, samples, stencil, glInfo);

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

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

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

            // present the GL buffers
            glContext.PresentRenderBuffer(Gles.GL_RENDERBUFFER);
            EAGLContext.SetCurrentContext(null);
        }
Ejemplo n.º 16
0
        public new void DrawInRect(GLKView view, CGRect rect)
        {
            if (designMode)
            {
                return;
            }

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

            // manage the drawing surface
            if (renderTarget == null || surface == null || renderTarget.Width != DrawableWidth || renderTarget.Height != DrawableHeight)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out var framebuffer);
                Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out var stencil);
                Gles.glGetIntegerv(Gles.GL_SAMPLES, out var samples);
                var maxSamples = context.GetMaxSurfaceSampleCount(colorType);
                if (samples > maxSamples)
                {
                    samples = maxSamples;
                }
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget((int)DrawableWidth, (int)DrawableHeight, samples, stencil, glInfo);

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

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

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

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

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

            if (renderTarget == null || surface == null || renderTarget.Width != size.Width || renderTarget.Height != size.Height)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out var framebuffer);
                Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out var stencil);
                Gles.glGetIntegerv(Gles.GL_SAMPLES, out var samples);
                var maxSamples = context.GetMaxSurfaceSampleCount(colorType);
                if (samples > maxSamples)
                {
                    samples = maxSamples;
                }
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget((int)size.Width, (int)size.Height, samples, stencil, glInfo);

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

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

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

            OpenGLContext.FlushBuffer();
        }
Ejemplo n.º 18
0
        protected sealed override void OnDrawFrame()
        {
            if (glSurface != IntPtr.Zero)
            {
                Gles.glClear(Gles.GL_STENCIL_BUFFER_BIT);

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

                    surface.Canvas.Flush();
                }

                // flush the SkiaSharp contents to GL
                context.Flush();
            }
        }
Ejemplo n.º 19
0
        public new void DrawInRect(GLKView view, CGRect rect)
        {
#if false
            if (designMode)
            {
                return;
            }
#endif

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

            // manage the drawing surface
            if (_renderTarget == null || _surface == null || _renderTarget.Width != DrawableWidth || _renderTarget.Height != DrawableHeight)
            {
                // create or update the dimensions
                _renderTarget?.Dispose();
                Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out int framebuffer);
                Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out int stencil);
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                _renderTarget = new GRBackendRenderTarget((int)DrawableWidth, (int)DrawableHeight, _context.GetMaxSurfaceSampleCount(colorType), stencil, glInfo);

                // create the surface
                _surface?.Dispose();
                _surface = SKSurface.Create(_context, _renderTarget, surfaceOrigin, colorType);
            }

            using (new SKAutoCanvasRestore(_surface.Canvas, true))
            {
                // start drawing
                //var e = new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType);
                PaintSurface(_surface);
            }

            // flush the SkiaSharp contents to GL
            _surface.Canvas.Flush();
            _context.Flush();
        }
Ejemplo n.º 20
0
        protected sealed override void OnDrawFrame()
        {
            if (glSurface != IntPtr.Zero)
            {
                Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

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

                    // flush the SkiaSharp contents to GL
                    surface.Canvas.Flush();
                    context.Flush();
                }
            }
        }
Ejemplo n.º 21
0
        public static GRBackendRenderTarget CreateRenderTarget(int bufferWidth, int bufferHeight)
        {
            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);

#if __IOS__ || __TVOS__
            Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_WIDTH, out bufferWidth);
            Gles.glGetRenderbufferParameteriv(Gles.GL_RENDERBUFFER, Gles.GL_RENDERBUFFER_HEIGHT, out bufferHeight);
#endif

#if __TIZEN__
            var isBgra = SKImageInfo.PlatformColorType == SKColorType.Bgra8888;
            var config = isBgra ? GRPixelConfig.Bgra8888 : GRPixelConfig.Rgba8888;
#else
            var config = GRPixelConfig.Rgba8888;
#endif

            var glInfo = new GRGlFramebufferInfo((uint)framebuffer, config.ToGlSizedFormat());
            return(new GRBackendRenderTarget(bufferWidth, bufferHeight, samples, stencil, glInfo));
        }
Ejemplo n.º 22
0
        protected sealed override void OnDrawFrame()
        {
            if (glSurface != IntPtr.Zero)
            {
                Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

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

                    // flush the SkiaSharp contents to GL
                    canvas.Flush();
                    context.Flush();
                }
            }
        }
Ejemplo n.º 23
0
        public static GRBackendRenderTargetDesc CreateRenderTarget()
        {
            int framebuffer, stencil, samples;

            Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out framebuffer);
            Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out stencil);
            Gles.glGetIntegerv(Gles.GL_SAMPLES, out samples);

            int bufferWidth  = 0;
            int bufferHeight = 0;

            return(new GRBackendRenderTargetDesc
            {
                Width = bufferWidth,
                Height = bufferHeight,
                Config = GRPixelConfig.Rgba8888,
                Origin = GRSurfaceOrigin.TopLeft,
                SampleCount = samples,
                StencilBits = stencil,
                RenderTargetHandle = (IntPtr)framebuffer,
            });
        }
Ejemplo n.º 24
0
        protected override void OnRenderFrame(Rect rect)
        {
            // clear everything
            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT | Gles.GL_STENCIL_BUFFER_BIT);

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

            // manage the drawing surface
            if (renderTarget == null || surface == null || renderTarget.Width != (int)rect.Width || renderTarget.Height != (int)rect.Height)
            {
                // create or update the dimensions
                renderTarget?.Dispose();
                Gles.glGetIntegerv(Gles.GL_FRAMEBUFFER_BINDING, out var framebuffer);
                Gles.glGetIntegerv(Gles.GL_STENCIL_BITS, out var stencil);
                var glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());
                renderTarget = new GRBackendRenderTarget((int)rect.Width, (int)rect.Height, 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
                OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType));
            }

            // update the control
            surface.Canvas.Flush();
            context.Flush();
        }
Ejemplo n.º 25
0
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

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

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

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

                // 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);
#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();
        }
Ejemplo n.º 26
0
 public void SetViewportSize(int width, int height)
 {
     Gles.glViewport(0, 0, width, height);
 }
Ejemplo n.º 27
0
        private void UpdateBuffers()
        {
            var context = GlesContext.CurrentContext;

            // make a copy as they may change
            var rbWidth       = renderbufferWidth;
            var rbHeight      = renderbufferHeight;
            var backing       = BackingOption;
            var multisampling = Multisampling;
            var depth         = DepthFormat;
            var stencil       = StencilFormat;

            // make sure we really do need to update
            var needsUpdate = Interlocked.CompareExchange(ref propertiesChanged, No, Yes) == Yes;

            if (!needsUpdate)
            {
                return;
            }

            // destroy the old buffers
            if (renderbuffer != 0)
            {
                Gles.glDeleteRenderbuffers(1, ref renderbuffer);
                renderbuffer = 0;
            }
            if (depthbuffer != 0)
            {
                Gles.glDeleteRenderbuffers(1, ref depthbuffer);
                depthbuffer = 0;
            }
            if (framebuffer != 0)
            {
                Gles.glDeleteFramebuffers(1, ref framebuffer);
                framebuffer = 0;
            }

            // re-create buffers
            Gles.glGenRenderbuffers(1, ref renderbuffer);
            Gles.glBindRenderbuffer(Gles.GL_RENDERBUFFER, renderbuffer);
            Gles.glGenFramebuffers(1, ref framebuffer);
            Gles.glBindFramebuffer(Gles.GL_FRAMEBUFFER, framebuffer);

            // create the surface
            context.SetSurface(this, rbWidth, rbHeight, backing, multisampling, GlesRenderTarget.Renderbuffer);
            Gles.glFramebufferRenderbuffer(Gles.GL_FRAMEBUFFER, Gles.GL_COLOR_ATTACHMENT0, Gles.GL_RENDERBUFFER, renderbuffer);

            // set up the depth and stencil buffers
            if (depth != GlesDepthFormat.None)
            {
                uint fmt = 0;
                if (depth == GlesDepthFormat.Format16)
                {
                    if (stencil == GlesStencilFormat.None)
                    {
                        fmt = Gles.GL_DEPTH_COMPONENT16;
                    }
                    else
                    {
                        fmt = Gles.GL_DEPTH24_STENCIL8_OES;
                    }
                }
                else if (depth == GlesDepthFormat.Format24)
                {
                    fmt = Gles.GL_DEPTH24_STENCIL8_OES;
                }

                Gles.glGenRenderbuffers(1, ref depthbuffer);
                Gles.glBindRenderbuffer(Gles.GL_RENDERBUFFER, depthbuffer);

                if (multisampling == GlesMultisampling.FourTimes)
                {
                    Gles.glRenderbufferStorageMultisampleANGLE(Gles.GL_RENDERBUFFER, 4, fmt, rbWidth, rbHeight);
                }
                else
                {
                    Gles.glRenderbufferStorage(Gles.GL_RENDERBUFFER, fmt, rbWidth, rbHeight);
                }
                Gles.glFramebufferRenderbuffer(Gles.GL_FRAMEBUFFER, Gles.GL_DEPTH_ATTACHMENT, Gles.GL_RENDERBUFFER, depthbuffer);
            }

            // format for stencils will always be D24_S8 here.
            if (stencil != GlesStencilFormat.None)
            {
                Gles.glFramebufferRenderbuffer(Gles.GL_FRAMEBUFFER, Gles.GL_STENCIL_ATTACHMENT, Gles.GL_RENDERBUFFER, depthbuffer);
            }

            Gles.glBindRenderbuffer(Gles.GL_RENDERBUFFER, renderbuffer);
        }