/// <summary> /// Initializes a new instance of the <see cref="CustomCompositor"/> class. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="window">The window with which this compositor is associated.</param> public CustomCompositor(UltravioletContext uv, IUltravioletWindow window) : base(uv, window) { rtScene = RenderTarget2D.Create(BufferWidth, BufferHeight); rtSceneColor = RenderBuffer2D.Create(RenderBufferFormat.Color, BufferWidth, BufferHeight); rtScene.Attach(rtSceneColor); rtSceneDepthStencil = RenderBuffer2D.Create(RenderBufferFormat.Depth24Stencil8, BufferWidth, BufferHeight); rtScene.Attach(rtSceneDepthStencil); rtInterface = RenderTarget2D.Create(BufferWidth, BufferHeight); rtInterfaceColor = RenderBuffer2D.Create(RenderBufferFormat.Color, BufferWidth, BufferHeight); rtInterface.Attach(rtInterfaceColor); rtInterfaceDepthStencil = RenderBuffer2D.Create(RenderBufferFormat.Depth24Stencil8, BufferWidth, BufferHeight); rtInterface.Attach(rtInterfaceDepthStencil); rtComposition = RenderTarget2D.Create(BufferWidth, BufferHeight, RenderTargetUsage.PreserveContents); rtCompositionColor = RenderBuffer2D.Create(RenderBufferFormat.Color, BufferWidth, BufferHeight); rtComposition.Attach(rtCompositionColor); rtCompositionDepthStencil = RenderBuffer2D.Create(RenderBufferFormat.Depth24Stencil8, BufferWidth, BufferHeight); rtComposition.Attach(rtCompositionDepthStencil); spriteBatch = SpriteBatch.Create(); }
/// <inheritdoc/> public override void SaveAsJpeg(RenderTarget2D renderTarget, Stream stream) { Contract.Require(renderTarget, "renderTarget"); Contract.Require(stream, "stream"); Save(renderTarget, stream, ImageFormat.Jpeg); }
/// <inheritdoc/> public override void SaveAsPng(RenderTarget2D renderTarget, Stream stream) { Contract.Require(renderTarget, nameof(renderTarget)); Contract.Require(stream, nameof(stream)); Save(renderTarget, stream, Bitmap.CompressFormat.Png); }
/// <summary> /// Saves the specified render target as an image with the specified format. /// </summary> /// <param name="renderTarget">The render target to save.</param> /// <param name="stream">The stream to which to save the render target data.</param> /// <param name="format">The format with which to save the image.</param> private void Save(RenderTarget2D renderTarget, Stream stream, ImageFormat format) { var data = new Color[renderTarget.Width * renderTarget.Height]; renderTarget.GetData(data); Save(data, renderTarget.Width, renderTarget.Height, stream, format); }
/// <inheritdoc/> public override void SaveAsPng(RenderTarget2D renderTarget, Stream stream) { Contract.Require(renderTarget, nameof(renderTarget)); Contract.Require(stream, nameof(stream)); var data = new Color[renderTarget.Width * renderTarget.Height]; renderTarget.GetData(data); Save(data, renderTarget.Width, renderTarget.Height, stream, img => img.AsPNG()); }
/// <inheritdoc/> public override void SaveAsPng(RenderTarget2D renderTarget, Stream stream) { Contract.Require(renderTarget, "renderTarget"); Contract.Require(stream, "stream"); var data = new Color[renderTarget.Width * renderTarget.Height]; renderTarget.GetData(data); Save(data, renderTarget.Width, renderTarget.Height, stream, asPng: true); }
partial void SaveImage(SurfaceSaver surfaceSaver, RenderTarget2D target) { var saver = SurfaceSaver.Create(); var filename = $"output-{DateTime.Now:yyyyMMdd-HHmmss}.png"; var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), filename); using (var stream = File.OpenWrite(path)) saver.SaveAsPng(rtarget, stream); confirmMsgText = $"Image saved to {path}"; confirmMsgOpacity = 1; }
partial void SaveImage(SurfaceSaver surfaceSaver, RenderTarget2D target) { var saver = SurfaceSaver.Create(); var filename = $"output-{DateTime.Now:yyyyMMdd-HHmmss}.png"; var path = filename; using (var stream = File.OpenWrite(path)) saver.SaveAsPng(rtarget, stream); confirmMsgText = $"Image saved to {filename}"; confirmMsgOpacity = 1; }
partial void SaveImage(SurfaceSaver surfaceSaver, RenderTarget2D target) { var filename = $"output-{DateTime.Now:yyyyMMdd-HHmmss}.png"; var path = filename; var dir = AndroidEnvironment.GetExternalStoragePublicDirectory( AndroidEnvironment.DirectoryPictures).AbsolutePath; path = Path.Combine(dir, filename); using (var stream = File.OpenWrite(path)) surfaceSaver.SaveAsPng(rtarget, stream); MediaScannerConnection.ScanFile(ApplicationContext, new String[] { path }, new String[] { MimeTypeMap.Singleton.GetMimeTypeFromExtension("png") }, null); confirmMsgText = $"Image saved to photo gallery"; confirmMsgOpacity = 1; }
partial void SaveImage(SurfaceSaver surfaceSaver, RenderTarget2D target) { using (var stream = new MemoryStream()) { surfaceSaver.SaveAsPng(rtarget, stream); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); using (var imgData = Foundation.NSData.FromStream(stream)) using (var img = UIKit.UIImage.LoadFromData(imgData)) { img.SaveToPhotosAlbum(null); } confirmMsgText = $"Image saved to photo gallery"; confirmMsgOpacity = 1; } }
/// <summary> /// Initializes a new instance of the <see cref="OutOfBandRenderTarget"/> class. /// </summary> /// <param name="uv">The Ultraviolet context.</param> internal OutOfBandRenderTarget(UltravioletContext uv) : base(uv) { renderTarget = RenderTarget2D.Create(1, 1); colorBuffer = RenderBuffer2D.Create(RenderBufferFormat.Color, 1, 1, RenderBufferOptions.None); renderTarget.Attach(colorBuffer); if (uv.GetGraphics().Capabilities.SupportsDepthStencilTextures) { depthBuffer = RenderBuffer2D.Create(RenderBufferFormat.Depth24Stencil8, 1, 1, RenderBufferOptions.WillNotBeSampled); renderTarget.Attach(depthBuffer); } else { depthBuffer = RenderBuffer2D.Create(RenderBufferFormat.Depth16, 1, 1, RenderBufferOptions.WillNotBeSampled); renderTarget.Attach(depthBuffer); stencilBuffer = RenderBuffer2D.Create(RenderBufferFormat.Stencil8, 1, 1, RenderBufferOptions.WillNotBeSampled); renderTarget.Attach(stencilBuffer); } }
/// <inheritdoc/> public void SetRenderTarget(RenderTarget2D renderTarget) { Contract.EnsureNotDisposed(this, Disposed); var currentWindow = Ultraviolet.GetPlatform().Windows.GetCurrent(); if (currentWindow != null && renderTarget == null) renderTarget = currentWindow.Compositor.GetRenderTarget(); SetRenderTargetInternal(renderTarget); }
/// <summary> /// Converts the specified render target to a bitmap image. /// </summary> /// <param name="rt">The render target to convert.</param> /// <returns>The converted bitmap image.</returns> private Bitmap ConvertRenderTargetToBitmap(RenderTarget2D rt) { var data = new Color[rt.Width * rt.Height]; rt.GetData(data); var bmp = new Bitmap(rtarget.Width, rtarget.Height); var pixel = 0; for (int y = 0; y < rtarget.Height; y++) { for (int x = 0; x < rtarget.Width; x++) { bmp.SetPixel(x, y, System.Drawing.Color.FromArgb((int)data[pixel++].ToArgb())); } } return bmp; }
/// <inheritdoc/> public void SetRenderTarget(RenderTarget2D rt) { Contract.EnsureNotDisposed(this, Disposed); }
/// <inheritdoc/> public void SetRenderTarget(RenderTarget2D renderTarget) { Contract.EnsureNotDisposed(this, Disposed); Ultraviolet.ValidateResource(renderTarget); var oglRenderTarget = (OpenGLRenderTarget2D)renderTarget; if (oglRenderTarget != this.renderTarget) { var targetName = 0u; var targetSize = Size2.Zero; var currentWindow = Ultraviolet.GetPlatform().Windows.GetCurrent(); if (oglRenderTarget != null) { oglRenderTarget.ValidateStatus(); targetName = oglRenderTarget.OpenGLName; targetSize = renderTarget.Size; } else { if (currentWindow != null) targetSize = currentWindow.ClientSize; } OpenGLState.BindFramebuffer(targetName); if (this.renderTarget != null) this.renderTarget.UnbindWrite(); this.renderTarget = oglRenderTarget; if (this.renderTarget != null) this.renderTarget.BindWrite(); this.viewport = default(Viewport); SetViewport(new Viewport(0, 0, targetSize.Width, targetSize.Height)); if (this.renderTarget != null) { Clear(Color.FromArgb(0xFF442288)); } } }
/// <summary> /// Saves the specified render target as a JPEG image. /// </summary> /// <param name="renderTarget">The render target to save.</param> /// <param name="stream">The stream to which to save the render target data.</param> public abstract void SaveAsJpeg(RenderTarget2D renderTarget, Stream stream);
/// <summary> /// Sets the current render target. /// </summary> private void SetRenderTargetInternal(RenderTarget2D renderTarget) { Ultraviolet.ValidateResource(renderTarget); var usage = renderTarget?.RenderTargetUsage ?? backBufferRenderTargetUsage; if (usage == RenderTargetUsage.PlatformContents) { usage = Capabilities.SupportsPreservingRenderTargetContentInHardware ? RenderTargetUsage.PreserveContents : RenderTargetUsage.DiscardContents; } var oglRenderTarget = (OpenGLRenderTarget2D)renderTarget; if (oglRenderTarget != this.renderTarget) { var targetName = gl.DefaultFramebuffer; var targetSize = Size2.Zero; if (oglRenderTarget != null) { oglRenderTarget.ValidateStatus(); targetName = oglRenderTarget.OpenGLName; targetSize = renderTarget.Size; } else { var currentWindow = Ultraviolet.GetPlatform().Windows.GetCurrent(); if (currentWindow != null) targetSize = currentWindow.DrawableSize; } OpenGLState.BindFramebuffer(targetName); if (this.renderTarget != null) this.renderTarget.UnbindWrite(); this.renderTarget = oglRenderTarget; if (this.renderTarget != null) this.renderTarget.BindWrite(); this.viewport = default(Viewport); SetViewport(new Viewport(0, 0, targetSize.Width, targetSize.Height)); if (usage == RenderTargetUsage.DiscardContents) { Clear(Color.FromArgb(0xFF442288)); } } }
partial void SaveImage(SurfaceSaver surfaceSaver, RenderTarget2D target);
/// <summary> /// Converts the specified render target to a bitmap image. /// </summary> /// <param name="rt">The render target to convert.</param> /// <returns>The converted bitmap image.</returns> private Bitmap ConvertRenderTargetToBitmap(RenderTarget2D rt) { // HACK: Our buffer has been rounded up to the nearest // power of two, so at this point we clip it back down // to the size of the window. var window = Ultraviolet.GetPlatform().Windows.GetPrimary(); var windowWidth = window.DrawableSize.Width; var windowHeight = window.DrawableSize.Height; var data = new Color[rt.Width * rt.Height]; rt.GetData(data); var bmp = new Bitmap(windowWidth, windowHeight); var pixel = 0; for (int y = 0; y < rt.Height; y++) { for (int x = 0; x < rt.Width; x++) { if (x < windowWidth && y < windowHeight) { bmp.SetPixel(x, y, System.Drawing.Color.FromArgb((int)data[pixel].ToArgb())); } pixel++; } } return bmp; }
/// <inheritdoc/> protected override void OnLoadingContent() { var window = Ultraviolet.GetPlatform().Windows.GetPrimary(); if (!headless) { // HACK: AMD drivers produce weird rasterization artifacts when rendering // to a NPOT render buffer??? So we have to fix it with this stupid hack??? var width = MathUtil.FindNextPowerOfTwo(window.DrawableSize.Width); var height = MathUtil.FindNextPowerOfTwo(window.DrawableSize.Height); rtargetColorBuffer = RenderBuffer2D.Create(RenderBufferFormat.Color, width, height); rtargetDepthStencilBuffer = RenderBuffer2D.Create(RenderBufferFormat.Depth24Stencil8, width, height); rtarget = RenderTarget2D.Create(width, height); rtarget.Attach(rtargetColorBuffer); rtarget.Attach(rtargetDepthStencilBuffer); } if (loader != null) { content = ContentManager.Create("Content"); loader(content); } base.OnLoadingContent(); }