Reduce working set by releasing buffers for sheets.

Sheets carry a managed buffer of data that allows updates to be made without having to constantly fetch and set data to the texture memory of the video card. This is useful for things like SheetBuilder which make small progressive changes to sheets.

However these buffers are often large and are kept alive because sheets are referenced by the sprites that use them. If this buffer is explicitly null'ed when it is no longer needed then the GC can reclaim it. Sometimes a buffer need not even be created because the object using the sheet only works on the texture directly anyway.

In practise, this reduced memory consumed by such buffers from ~165 MiB to ~112 MiB (at the start of a new RA skirmish mission).
This commit is contained in:
RoosterDragon
2014-06-22 16:01:55 +01:00
parent 0d61a1d19a
commit 3a30748f05
11 changed files with 97 additions and 57 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
@@ -27,14 +27,13 @@ namespace OpenRA.Widgets
bool stopped;
bool paused;
Action OnComplete;
Action onComplete;
public bool Paused { get { return paused; } }
readonly World world;
[ObjectCreator.UseCtor]
public VqaPlayerWidget( World world )
public VqaPlayerWidget(World world)
{
this.world = world;
}
@@ -48,33 +47,35 @@ namespace OpenRA.Widgets
stopped = true;
paused = true;
Sound.StopVideo();
OnComplete = () => {};
onComplete = () => { };
cachedVideo = filename;
video = new VqaReader(GlobalFileSystem.Open(filename));
invLength = video.Framerate*1f/video.Frames;
invLength = video.Framerate * 1f / video.Frames;
var size = Math.Max(video.Width, video.Height);
var textureSize = Exts.NextPowerOf2(size);
videoSprite = new Sprite(new Sheet(new Size(textureSize,textureSize)), new Rectangle( 0, 0, video.Width, video.Height ), TextureChannel.Alpha);
videoSprite.sheet.Texture.SetData(video.FrameData);
var videoSheet = new Sheet(new Size(textureSize, textureSize), false);
videoSheet.Texture.SetData(video.FrameData);
videoSprite = new Sprite(videoSheet, new Rectangle(0, 0, video.Width, video.Height), TextureChannel.Alpha);
var scale = Math.Min(RenderBounds.Width / video.Width, RenderBounds.Height / video.Height);
videoOrigin = new float2(RenderBounds.X + (RenderBounds.Width - scale*video.Width)/2, RenderBounds.Y + (RenderBounds.Height - scale*video.Height)/2);
videoOrigin = new float2(RenderBounds.X + (RenderBounds.Width - scale * video.Width) / 2, RenderBounds.Y + (RenderBounds.Height - scale * video.Height) / 2);
videoSize = new float2(video.Width * scale, video.Height * scale);
if (!DrawOverlay)
return;
overlay = new uint[2*textureSize, 2*textureSize];
overlay = new uint[2 * textureSize, 2 * textureSize];
var black = (uint)255 << 24;
for (var y = 0; y < video.Height; y++)
for (var x = 0; x < video.Width; x++)
overlay[2*y,x] = black;
overlay[2 * y, x] = black;
overlaySprite = new Sprite(new Sheet(new Size(2*textureSize,2*textureSize)), new Rectangle( 0, 0, video.Width, 2*video.Height ), TextureChannel.Alpha);
overlaySprite.sheet.Texture.SetData(overlay);
var overlaySheet = new Sheet(new Size(2 * textureSize, 2 * textureSize), false);
overlaySheet.Texture.SetData(overlay);
overlaySprite = new Sprite(overlaySheet, new Rectangle(0, 0, video.Width, 2 * video.Height), TextureChannel.Alpha);
}
public override void Draw()
@@ -84,7 +85,7 @@ namespace OpenRA.Widgets
if (!(stopped || paused))
{
var nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition*invLength);
var nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition * invLength);
if (nextFrame > video.Frames)
{
Stop();
@@ -115,12 +116,13 @@ namespace OpenRA.Widgets
return true;
}
}
return false;
}
public void Play()
{
PlayThen(() => {});
PlayThen(() => { });
}
public void PlayThen(Action after)
@@ -128,7 +130,7 @@ namespace OpenRA.Widgets
if (video == null)
return;
OnComplete = after;
onComplete = after;
if (stopped)
Sound.PlayVideo(video.AudioData);
else
@@ -156,7 +158,7 @@ namespace OpenRA.Widgets
Sound.StopVideo();
video.Reset();
videoSprite.sheet.Texture.SetData(video.FrameData);
world.AddFrameEndTask(_ => OnComplete());
world.AddFrameEndTask(_ => onComplete());
}
}
}