/// <summary>
        /// Processes a single file which has all of the layers of the surface within it.
        /// </summary>
        private Surface3D ProcessSingleFile(ContentManager manager, IContentProcessorMetadata metadata, PlatformNativeSurface input)
        {
            var mdat        = metadata.As <SDL2Surface3DProcessorMetadata>();
            var srgbEncoded = mdat.SrgbEncoded ?? manager.Ultraviolet.Properties.SrgbDefaultForSurface3D;

            // Layers must be square. Validate our dimensions.
            var layerHeight = input.Height;
            var layerWidth  = layerHeight;
            var layerCount  = input.Width / layerWidth;

            if (input.Width % layerWidth != 0)
            {
                throw new InvalidDataException(SDL2Strings.SurfaceMustHaveSquareLayers);
            }

            // Create surfaces for each of our layers.
            using (var mainSurface = Surface2D.Create(input))
            {
                mainSurface.SrgbEncoded = srgbEncoded;

                var resultOpts = srgbEncoded ? SurfaceOptions.SrgbColor : SurfaceOptions.LinearColor;
                var result     = new SDL2Surface3D(manager.Ultraviolet, layerWidth, layerHeight, layerCount, mainSurface.BytesPerPixel, resultOpts);
                for (int i = 0; i < layerCount; i++)
                {
                    var layerSurface = mainSurface.CreateSurface(new Rectangle(i * layerWidth, 0, layerWidth, layerHeight));
                    result.SetLayer(i, layerSurface, true);
                }
                return(result);
            }
        }
Beispiel #2
0
        private static void CreateThePixel(UltravioletContext uv)
        {
            var surface = Surface2D.Create(1, 1, SurfaceOptions.Default);

            surface.SetData(new Color[] { Color.White });
            pixel = Texture2D.CreateTexture(surface.Pixels, 1, 1, surface.BytesPerPixel, TextureOptions.Default);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes the font texture atlas for the current ImGui context.
        /// </summary>
        private void InitFontTextureAtlas()
        {
            unsafe
            {
                var io = ImGui.GetIO();

                io.Fonts.GetTexDataAsRGBA32(out var textureData,
                                            out var textureWidth, out var textureHeight, out var textureBytesPerPixel);

                var srgb = Ultraviolet.GetGraphics().Capabilities.SrgbEncodingEnabled;

                var surfaceOptions = srgb ? SurfaceOptions.SrgbColor : SurfaceOptions.LinearColor;
                var surface        = Surface2D.Create(textureWidth, textureHeight, surfaceOptions);
                surface.SetRawData((IntPtr)textureData, 0, 0, textureWidth * textureHeight);
                surface.Flip(SurfaceFlipDirection.Vertical);

                var textureOptions = TextureOptions.ImmutableStorage | (srgb ? TextureOptions.SrgbColor : TextureOptions.LinearColor);
                var texture        = Texture2D.CreateTexture(textureWidth, textureHeight, textureOptions);
                texture.SetData(surface);

                fontAtlasID = Textures.Register(texture);

                io.Fonts.SetTexID(new IntPtr(fontAtlasID));
                io.Fonts.ClearTexData();
            }
        }
Beispiel #4
0
 protected override void OnInitialized()
 {
     UsePlatformSpecificFileSource();
     _frameBuffer     = Texture2D.CreateTexture(_width * _scale, _height * _scale);
     _canvas          = Surface2D.Create(_width * _scale, _height * _scale);
     this.spriteBatch = SpriteBatch.Create();
     base.OnInitialized();
 }
        /// <summary>
        /// Processes a collection of files, each of which represents a separate layer of the surface.
        /// </summary>
        private Surface3D ProcessMultipleFiles(ContentManager manager, IContentProcessorMetadata metadata, PlatformNativeSurface input, String filename)
        {
            var mdat        = metadata.As <SDL2Surface3DProcessorMetadata>();
            var srgbEncoded = mdat.SrgbEncoded ?? manager.Ultraviolet.Properties.SrgbDefaultForSurface3D;

            var layer0     = input.CreateCopy();
            var layers     = new Dictionary <Int32, String>();
            var layerIndex = 1;

            var extension = Path.GetExtension(metadata.AssetFileName);
            var directory = (metadata.AssetPath == null) ? String.Empty : Path.GetDirectoryName(metadata.AssetPath);

            if (!String.IsNullOrEmpty(directory))
            {
                var assetsInDirectory = manager.GetAssetFilePathsInDirectory(directory);
                while (true)
                {
                    var layerAsset = assetsInDirectory.Where(x => String.Equals(Path.GetFileName(x),
                                                                                $"{filename}_{layerIndex}{extension}", StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
                    if (layerAsset == null)
                    {
                        break;
                    }

                    layerAsset = ResolveDependencyAssetPath(metadata, Path.GetFileName(layerAsset));

                    layers[layerIndex] = layerAsset;
                    layerIndex++;
                }
            }

            var surfaceOpts = srgbEncoded ? SurfaceOptions.SrgbColor : SurfaceOptions.LinearColor;
            var surface     = new SDL2Surface3D(manager.Ultraviolet, layer0.Width, layer0.Height, 1 + layers.Count, layer0.BytesPerPixel, surfaceOpts);

            var surfaceLayer0 = Surface2D.Create(layer0);

            surfaceLayer0.SrgbEncoded = srgbEncoded;
            surface.SetLayer(0, surfaceLayer0, true);

            for (int i = 0; i < layers.Count; i++)
            {
                var layerAsset = layers[1 + i];
                metadata.AddAssetDependency(layerAsset);

                var layerSurface = manager.Load <Surface2D>(layerAsset, metadata.AssetDensity, false, metadata.IsLoadedFromSolution);
                layerSurface.SrgbEncoded = srgbEncoded;
                surface.SetLayer(1 + i, layerSurface, true);
            }

            return(surface);
        }
        /// <inheritdoc/>
        public override Surface2D LoadIcon()
        {
            var bundle = NSBundle.MainBundle;

            if (bundle == null)
            {
                return(null);
            }

            var icon = default(NSImage);

            try
            {
                try
                {
                    if (String.Equals(Path.GetExtension(bundle.BundlePath), ".app", StringComparison.OrdinalIgnoreCase))
                    {
                        icon = NSWorkspace.SharedWorkspace.IconForFile(bundle.BundlePath);
                        if (icon == null)
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    else
                    {
                        using (var stream = typeof(UltravioletContext).Assembly.GetManifestResourceStream("TwistedLogik.Ultraviolet.uv.ico"))
                        {
                            icon = NSImage.FromStream(stream);
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    return(null);
                }

                using (var source = new OSXSurfaceSource(icon))
                {
                    return(Surface2D.Create(source));
                }
            }
            finally
            {
                if (icon != null)
                {
                    icon.Dispose();
                }
            }
        }
        /// <inheritdoc/>
        public override Texture2D Process(ContentManager manager, IContentProcessorMetadata metadata, PlatformNativeSurface input)
        {
            var caps        = manager.Ultraviolet.GetGraphics().Capabilities;
            var mdat        = metadata.As <OpenGLTexture2DProcessorMetadata>();
            var srgbEncoded = mdat.SrgbEncoded ?? manager.Ultraviolet.Properties.SrgbDefaultForTexture2D;
            var surfOptions = srgbEncoded ? SurfaceOptions.SrgbColor : SurfaceOptions.LinearColor;

            using (var surface = Surface2D.Create(input, surfOptions))
            {
                var flipdir = manager.Ultraviolet.GetGraphics().Capabilities.FlippedTextures ? SurfaceFlipDirection.Vertical : SurfaceFlipDirection.None;
                surface.FlipAndProcessAlpha(flipdir, mdat.PremultiplyAlpha, mdat.Opaque ? null : (Color?)Color.Magenta);

                return(surface.CreateTexture(unprocessed: true));
            }
        }
        /// <inheritdoc/>
        public override void SaveAsPng(Stream stream)
        {
            Contract.EnsureNotDisposed(this, Disposed);
            Contract.Require(stream, nameof(stream));
            Contract.Ensure(IsComplete, SDL2Strings.SurfaceIsNotComplete);

            EnsureAllLayersMatchSrgbEncoding();

            var outputSurfaceOptions = SrgbEncoded ? SurfaceOptions.SrgbColor : SurfaceOptions.LinearColor;

            using (var outputSurface = Surface2D.Create(Width * Depth, Height, outputSurfaceOptions))
            {
                var position = 0;
                for (int i = 0; i < Depth; i++)
                {
                    GetLayer(i).Blit(outputSurface, new Rectangle(position, 0, Width, Height));
                    position += Width;
                }

                var surfaceSaver = SurfaceSaver.Create();
                surfaceSaver.SaveAsPng(outputSurface, stream);
            }
        }
        /// <inheritdoc/>
        public override void ExportPreprocessed(ContentManager manager, IContentProcessorMetadata metadata, BinaryWriter writer, PlatformNativeSurface input, Boolean delete)
        {
            var mdat        = metadata.As <OpenGLTexture2DProcessorMetadata>();
            var caps        = manager.Ultraviolet.GetGraphics().Capabilities;
            var srgbEncoded = mdat.SrgbEncoded ?? manager.Ultraviolet.Properties.SrgbDefaultForTexture2D;
            var surfOptions = srgbEncoded ? SurfaceOptions.SrgbColor : SurfaceOptions.LinearColor;

            using (var surface = Surface2D.Create(input, surfOptions))
            {
                var flipdir = caps.FlippedTextures ? SurfaceFlipDirection.Vertical : SurfaceFlipDirection.None;
                surface.FlipAndProcessAlpha(flipdir, mdat.PremultiplyAlpha, mdat.Opaque ? null : (Color?)Color.Magenta);

                using (var memstream = new MemoryStream())
                {
                    surface.SaveAsPng(memstream);
                    writer.Write(Int32.MaxValue);
                    writer.Write(1u);
                    writer.Write(surface.SrgbEncoded);
                    writer.Write((int)memstream.Length);
                    writer.Write(memstream.ToArray());
                }
            }
        }
Beispiel #10
0
        public void Surface2D_RendersCorrectly_WhenFlippedDuringBlit()
        {
            var sbatch        = default(SpriteBatch);
            var surfaceOutput = default(Surface2D);
            var surfaceInput  = default(Surface2D);
            var texture       = default(Texture2D);

            var result = GivenAnUltravioletApplication()
                         .WithContent(content =>
            {
                sbatch = SpriteBatch.Create();

                surfaceOutput = Surface2D.Create(128, 128);
                surfaceOutput.Clear(Color.Lime);

                surfaceInput = content.Load <Surface2D>("Surfaces/Test");
                surfaceInput.ProcessAlpha(true, null);
                surfaceInput.Blit(surfaceOutput, new Point2(0, 0), SurfaceFlipDirection.None);
                surfaceInput.Blit(surfaceOutput, new Point2(64, 0), SurfaceFlipDirection.Horizontal);
                surfaceInput.Blit(surfaceOutput, new Point2(0, 64), SurfaceFlipDirection.Vertical);

                texture = surfaceOutput.CreateTexture();
            })
                         .Render(uv =>
            {
                uv.GetGraphics().Clear(Color.CornflowerBlue);

                sbatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

                sbatch.Draw(texture, new RectangleF(0, 0, 128, 128), Color.White);

                sbatch.End();
            });

            TheResultingImage(result)
            .ShouldMatch(@"Resources/Expected/Graphics/Surface2D_RendersCorrectly_WhenFlippedDuringBlit.png");
        }
        /// <inheritdoc/>
        public override Surface2D LoadIcon()
        {
            var assembly         = Assembly.GetEntryAssembly();
            var assemblyLocation = (assembly == null) ? typeof(UltravioletContext).Assembly.Location : assembly.Location;

            /* HACK: Trying to load an icon from a network path throws an exception, which is a problem
             * given the way the test servers are currently configured. So just skip loading it. */
            var uri = new Uri(assemblyLocation);

            if (uri.IsUnc)
            {
                return(null);
            }

            var icon = System.Drawing.Icon.ExtractAssociatedIcon(assemblyLocation);

            if (icon == null)
            {
                throw new InvalidOperationException();
            }

            try
            {
                using (var iconbmp = icon.ToBitmap())
                {
                    using (var source = new DesktopSurfaceSource(iconbmp))
                    {
                        return(Surface2D.Create(source));
                    }
                }
            }
            finally
            {
                icon.Dispose();
            }
        }
        /// <inheritdoc/>
        public override Surface2D LoadIcon()
        {
            var asmEntry  = Assembly.GetEntryAssembly();
            var asmLoader = typeof(NETCore3IconLoader).Assembly;

            var asmResourceNames  = asmEntry.GetManifestResourceNames();
            var asmResourcePrefix = GetLongestCommonResourcePrefix(asmResourceNames);
            var asmResourceIcon   = String.IsNullOrEmpty(asmResourcePrefix) && asmResourceNames.Length == 1 && asmResourceNames[0].EndsWith(".icon.ico") ?
                                    asmResourceNames[0] : $"{asmResourcePrefix}.icon.ico";

            var iconStream =
                asmEntry.GetManifestResourceStream(asmResourceIcon) ??
                asmLoader.GetManifestResourceStream($"Ultraviolet.Shims.NETCore3.icon.ico");

            if (iconStream != null)
            {
                using (var source = SurfaceSource.Create(iconStream))
                {
                    return(Surface2D.Create(source, SurfaceOptions.SrgbColor));
                }
            }

            return(null);
        }