Example #1
0
        SKImage ToImage(Texture2D texture)
        {
            // Taken form VL.Video.MediaFoundation
            const int GL_TEXTURE_BINDING_2D = 0x8069;

            var eglContext = renderContext.EglContext;
            var eglImage   = eglContext.CreateImageFromD3D11Texture(texture.NativePointer);

            uint textureId = 0;

            NativeGles.glGenTextures(1, ref textureId);

            // We need to restore the currently bound texture (https://github.com/devvvvs/vvvv/issues/5925)
            NativeGles.glGetIntegerv(GL_TEXTURE_BINDING_2D, out var currentTextureId);
            NativeGles.glBindTexture(NativeGles.GL_TEXTURE_2D, textureId);
            NativeGles.glEGLImageTargetTexture2DOES(NativeGles.GL_TEXTURE_2D, eglImage);
            NativeGles.glBindTexture(NativeGles.GL_TEXTURE_2D, (uint)currentTextureId);

            var description = texture.Description;
            var colorType   = GetColorType(description.Format);
            var glInfo      = new GRGlTextureInfo(
                id: textureId,
                target: NativeGles.GL_TEXTURE_2D,
                format: colorType.ToGlSizedFormat());

            var backendTexture = new GRBackendTexture(
                width: description.Width,
                height: description.Height,
                mipmapped: false,
                glInfo: glInfo);

            var image = SKImage.FromTexture(
                renderContext.SkiaContext,
                backendTexture,
                GRSurfaceOrigin.BottomLeft,
                colorType,
                SKAlphaType.Premul,
                colorspace: SKColorSpace.CreateSrgb(),
                releaseProc: _ =>
            {
                renderContext.MakeCurrent();

                backendTexture.Dispose();
                NativeGles.glDeleteTextures(1, ref textureId);
                eglImage.Dispose();
                texture.Dispose();
            });

            return(image);
        }
Example #2
0
        public void CanConvertFromPointerToDescToTextureWithNewInfo()
        {
            // the custom struct to contain the info
            var oldInfo = new GRGlTextureInfo
            {
                Id     = 123,
                Target = 456,
                Format = 789
            };

            // pin it for the native code
            var textureHandle = GCHandle.Alloc(oldInfo, GCHandleType.Pinned);

            // use the very old desc
            var textureDesc = new GRBackendTextureDesc
            {
                Width         = 100,
                Height        = 100,
                Config        = GRPixelConfig.Rgba8888,
                Flags         = GRBackendTextureDescFlags.RenderTarget,
                Origin        = GRSurfaceOrigin.BottomLeft,
                SampleCount   = 246,
                TextureHandle = textureHandle.AddrOfPinnedObject(),
            };

            // create the new texture
            var texture = new GRBackendTexture(textureDesc);

            // free up all resourcess
            textureHandle.Free();

            // make sure we kept the information
            Assert.Equal(100, texture.Width);
            Assert.Equal(100, texture.Height);
            var newInfo = texture.GetGlTextureInfo();

            Assert.Equal(oldInfo.Id, newInfo.Id);
            Assert.Equal(oldInfo.Target, newInfo.Target);
            Assert.Equal(GRPixelConfig.Rgba8888.ToGlSizedFormat(), newInfo.Format);
        }
        private void PrepareSkia()
        {
            // CONTEXT

            // create the SkiaSharp context
            var glInterface = GRGlInterface.CreateNativeGlInterface();

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

            // TEXTURE

            // the texture size
            var textureSize = new SKSizeI(256, 256);

            // create the OpenGL texture
            textureId = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, textureId);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, textureSize.Width, textureSize.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            // create the SkiaSharp texture description
            var textureInfo = new GRGlTextureInfo
            {
                Id     = (uint)textureId,
                Target = (uint)TextureTarget.Texture2D
            };
            var textureHandle = GCHandle.Alloc(textureInfo, GCHandleType.Pinned);

            textureDesc = new GRBackendTextureDesc
            {
                Width         = textureSize.Width,
                Height        = textureSize.Height,
                Config        = GRPixelConfig.Rgba8888,
                Flags         = GRBackendTextureDescFlags.RenderTarget,
                Origin        = GRSurfaceOrigin.TopLeft,
                SampleCount   = 0,
                TextureHandle = textureHandle.AddrOfPinnedObject(),
            };

            // create the SkiaSharp texture surface
            textureSurface = SKSurface.CreateAsRenderTarget(context, textureDesc);

            // free the pinned GC handle when we are done
            textureHandle.Free();

            // initialize the texture content
            UpdateTexture(false);

            // RENDER TARGET

            // create the SkiaSharp render target description
            renderDesc = new GRBackendRenderTargetDesc
            {
                RenderTargetHandle = (IntPtr)0,
                Width       = Width,
                Height      = Height,
                Config      = GRPixelConfig.Rgba8888,
                Origin      = GRSurfaceOrigin.TopLeft,
                SampleCount = 0,
                StencilBits = 0,
            };

            // create the SkiaSharp render target surface
            renderSurface = SKSurface.Create(context, renderDesc);
        }