1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-01-02 10:43:31 +00:00

Profiles - Tweaked render pipeline, improving render performance by ~40%

This commit is contained in:
SpoinkyNL 2020-11-15 17:38:06 +01:00
parent 7735edea1e
commit 991c5fd955
11 changed files with 231 additions and 207 deletions

View File

@ -29,7 +29,6 @@ namespace Artemis.Core
Profile = Parent.Profile;
Name = name;
Enabled = true;
Renderer = new Renderer();
_layerEffects = new List<BaseLayerEffect>();
_expandedPropertyGroups = new List<string>();
@ -47,7 +46,6 @@ namespace Artemis.Core
Name = folderEntity.Name;
Enabled = folderEntity.Enabled;
Order = folderEntity.Order;
Renderer = new Renderer();
_layerEffects = new List<BaseLayerEffect>();
_expandedPropertyGroups = new List<string>();
@ -69,8 +67,6 @@ namespace Artemis.Core
internal override RenderElementEntity RenderElementEntity => FolderEntity;
internal Renderer Renderer { get; }
/// <inheritdoc />
public override List<ILayerProperty> GetAllLayerProperties()
{
@ -133,7 +129,7 @@ namespace Artemis.Core
/// <returns>The newly created copy</returns>
public Folder CreateCopy()
{
JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
JsonSerializerSettings settings = new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All};
FolderEntity entityCopy = JsonConvert.DeserializeObject<FolderEntity>(JsonConvert.SerializeObject(FolderEntity, settings), settings)!;
entityCopy.Id = Guid.NewGuid();
entityCopy.Name += " - Copy";
@ -233,7 +229,6 @@ namespace Artemis.Core
lock (Timeline)
{
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => e.Enabled))
{
baseLayerEffect.BaseProperties?.Update(Timeline);
@ -244,18 +239,18 @@ namespace Artemis.Core
{
canvas.Save();
Renderer.Open(Path, Parent as Folder);
if (Renderer.Canvas == null || Renderer.Path == null || Renderer.Paint == null)
throw new ArtemisCoreException("Failed to open folder render context");
SKRect rendererBounds = Renderer.Path.Bounds;
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => e.Enabled))
baseLayerEffect.PreProcess(Renderer.Canvas, Renderer.Path, Renderer.Paint);
baseLayerEffect.PreProcess(Renderer.Canvas, rendererBounds, Renderer.Paint);
// If required, apply the opacity override of the module to the root folder
if (IsRootFolder && Profile.Module.OpacityOverride < 1)
{
double multiplier = Easings.SineEaseInOut(Profile.Module.OpacityOverride);
Renderer.Paint.Color = Renderer.Paint.Color.WithAlpha((byte)(Renderer.Paint.Color.Alpha * multiplier));
Renderer.Paint.Color = Renderer.Paint.Color.WithAlpha((byte) (Renderer.Paint.Color.Alpha * multiplier));
}
// No point rendering if the alpha was set to zero by one of the effects
@ -267,7 +262,7 @@ namespace Artemis.Core
Children[index].Render(Renderer.Canvas);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => e.Enabled))
baseLayerEffect.PostProcess(Renderer.Canvas, Renderer.Path, Renderer.Paint);
baseLayerEffect.PostProcess(Renderer.Canvas, rendererBounds, Renderer.Paint);
canvas.DrawBitmap(Renderer.Bitmap, Renderer.TargetLocation, Renderer.Paint);
}

View File

@ -39,7 +39,6 @@ namespace Artemis.Core
Enabled = true;
General = new LayerGeneralProperties();
Transform = new LayerTransformProperties();
Renderer = new Renderer();
_layerEffects = new List<BaseLayerEffect>();
_leds = new List<ArtemisLed>();
@ -58,7 +57,6 @@ namespace Artemis.Core
Parent = parent;
General = new LayerGeneralProperties();
Transform = new LayerTransformProperties();
Renderer = new Renderer();
_layerEffects = new List<BaseLayerEffect>();
_leds = new List<ArtemisLed>();
@ -114,8 +112,6 @@ namespace Artemis.Core
internal override RenderElementEntity RenderElementEntity => LayerEntity;
internal Renderer Renderer { get; }
/// <summary>
/// Creates a deep copy of the layer
/// </summary>
@ -349,11 +345,11 @@ namespace Artemis.Core
Renderer.Paint.BlendMode = General.BlendMode.CurrentValue;
Renderer.Paint.Color = new SKColor(0, 0, 0, (byte) (Transform.Opacity.CurrentValue * 2.55f));
// Clip anything outside the LED selection bounds
canvas.ClipPath(Renderer.ClipPath);
using SKPath renderPath = new SKPath();
if (General.ShapeType.CurrentValue == LayerShapeType.Rectangle)
renderPath.AddRect(Renderer.Path.Bounds);
else
renderPath.AddOval(Renderer.Path.Bounds);
if (General.TransformMode.CurrentValue == LayerTransformMode.Normal)
{
@ -373,7 +369,7 @@ namespace Artemis.Core
// If a brush is a bad boy and tries to color outside the lines, ensure that its clipped off
Renderer.Canvas.ClipPath(renderPath);
DelegateRendering(renderPath);
DelegateRendering(renderPath.Bounds);
}
else if (General.TransformMode.CurrentValue == LayerTransformMode.Clip)
{
@ -382,7 +378,7 @@ namespace Artemis.Core
// If a brush is a bad boy and tries to color outside the lines, ensure that its clipped off
Renderer.Canvas.ClipPath(renderPath);
DelegateRendering(Renderer.Path);
DelegateRendering(Renderer.Path.Bounds);
}
canvas.DrawBitmap(Renderer.Bitmap, Renderer.TargetLocation, Renderer.Paint);
@ -394,15 +390,15 @@ namespace Artemis.Core
}
}
private void DelegateRendering(SKPath path)
private void DelegateRendering(SKRect bounds)
{
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => e.Enabled))
baseLayerEffect.PreProcess(Renderer.Canvas, path, Renderer.Paint);
baseLayerEffect.PreProcess(Renderer.Canvas, bounds, Renderer.Paint);
LayerBrush.InternalRender(Renderer.Canvas, path, Renderer.Paint);
LayerBrush.InternalRender(Renderer.Canvas, bounds, Renderer.Paint);
foreach (BaseLayerEffect baseLayerEffect in LayerEffects.Where(e => e.Enabled))
baseLayerEffect.PostProcess(Renderer.Canvas, path, Renderer.Paint);
baseLayerEffect.PostProcess(Renderer.Canvas, bounds, Renderer.Paint);
}
internal void CalculateRenderProperties()

View File

@ -16,6 +16,7 @@ namespace Artemis.Core
protected RenderProfileElement()
{
Timeline = new Timeline();
Renderer = new Renderer();
LayerEffectStore.LayerEffectAdded += LayerEffectStoreOnLayerEffectAdded;
LayerEffectStore.LayerEffectRemoved += LayerEffectStoreOnLayerEffectRemoved;
@ -102,9 +103,23 @@ namespace Artemis.Core
#region Properties
private ProfileElement _parent;
private SKPath _path;
internal abstract RenderElementEntity RenderElementEntity { get; }
/// <summary>
/// Gets the parent of this element
/// </summary>
public new ProfileElement Parent
{
get => _parent;
internal set
{
SetAndNotify(ref _parent, value);
Renderer.Invalidate();
}
}
/// <summary>
/// Gets the path containing all the LEDs this entity is applied to, any rendering outside the entity Path is
/// clipped.
@ -118,6 +133,7 @@ namespace Artemis.Core
// I can't really be sure about the performance impact of calling Bounds often but
// SkiaSharp calls SkiaApi.sk_path_get_bounds (Handle, &rect); which sounds expensive
Bounds = value?.Bounds ?? SKRect.Empty;
Renderer.Invalidate();
}
}
@ -130,6 +146,8 @@ namespace Artemis.Core
private set => SetAndNotify(ref _bounds, value);
}
internal Renderer Renderer { get; }
#region Property group expansion
protected List<string> _expandedPropertyGroups;

View File

@ -5,12 +5,12 @@ namespace Artemis.Core
{
internal class Renderer : IDisposable
{
private bool _valid;
private bool _disposed;
public SKBitmap? Bitmap { get; private set; }
public SKCanvas? Canvas { get; private set; }
public SKPaint? Paint { get; private set; }
public SKPath? Path { get; private set; }
public SKPath? ClipPath { get; private set; }
public SKPoint TargetLocation { get; private set; }
public bool IsOpen { get; private set; }
@ -26,27 +26,30 @@ namespace Artemis.Core
if (IsOpen)
throw new ArtemisCoreException("Cannot open render context because it is already open");
int width = (int) path.Bounds.Width;
int height = (int) path.Bounds.Height;
if (!_valid)
{
SKRect pathBounds = path.Bounds;
int width = (int) pathBounds.Width;
int height = (int) pathBounds.Height;
if (Bitmap == null)
Bitmap = new SKBitmap(width, height);
else if (Bitmap.Info.Width != width || Bitmap.Info.Height != height)
Bitmap = new SKBitmap(width, height);
Path = new SKPath(path);
Canvas = new SKCanvas(Bitmap);
Path.Transform(SKMatrix.MakeTranslation(pathBounds.Left * -1, pathBounds.Top * -1));
TargetLocation = new SKPoint(pathBounds.Location.X, pathBounds.Location.Y);
if (parent != null)
TargetLocation -= parent.Bounds.Location;
Canvas.ClipPath(Path);
_valid = true;
}
Paint = new SKPaint();
Path.Transform(SKMatrix.MakeTranslation(Path.Bounds.Left * -1, Path.Bounds.Top * -1));
Canvas.Clear();
TargetLocation = new SKPoint(path.Bounds.Location.X, path.Bounds.Location.Y);
if (parent != null)
TargetLocation -= parent.Path.Bounds.Location;
ClipPath = new SKPath(Path);
ClipPath.Transform(SKMatrix.MakeTranslation(TargetLocation.X, TargetLocation.Y));
Canvas.Save();
IsOpen = true;
}
@ -56,25 +59,34 @@ namespace Artemis.Core
if (_disposed)
throw new ObjectDisposedException("Renderer");
Canvas?.Dispose();
Canvas.Restore();
Paint?.Dispose();
Path?.Dispose();
ClipPath?.Dispose();
Canvas = null;
Paint = null;
Path = null;
ClipPath = null;
IsOpen = false;
}
public void Invalidate()
{
if (_disposed)
throw new ObjectDisposedException("Renderer");
_valid = false;
}
public void Dispose()
{
if (IsOpen)
Close();
Canvas?.Dispose();
Paint?.Dispose();
Path?.Dispose();
Bitmap?.Dispose();
Canvas = null;
Paint = null;
Path = null;
Bitmap = null;
_disposed = true;

View File

@ -95,7 +95,7 @@ namespace Artemis.Core.LayerBrushes
// but LayerBrush<T> and RgbNetLayerBrush<T> outside the core
internal abstract void Initialize();
internal abstract void InternalRender(SKCanvas canvas, SKPath path, SKPaint paint);
internal abstract void InternalRender(SKCanvas canvas, SKRect path, SKPaint paint);
internal virtual void Dispose(bool disposing)
{

View File

@ -22,13 +22,13 @@ namespace Artemis.Core.LayerBrushes
/// <para>Called during rendering or layer preview, in the order configured on the layer</para>
/// </summary>
/// <param name="canvas">The layer canvas</param>
/// <param name="path">The path to be filled, represents the shape</param>
/// <param name="bounds">The area to be filled, covers the shape</param>
/// <param name="paint">The paint to be used to fill the shape</param>
public abstract void Render(SKCanvas canvas, SKPath path, SKPaint paint);
public abstract void Render(SKCanvas canvas, SKRect bounds, SKPaint paint);
internal override void InternalRender(SKCanvas canvas, SKPath path, SKPaint paint)
internal override void InternalRender(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
Render(canvas, path, paint);
Render(canvas, bounds, paint);
}
internal override void Initialize()

View File

@ -28,7 +28,7 @@ namespace Artemis.Core.LayerBrushes
/// <returns>The color the LED will receive</returns>
public abstract SKColor GetColor(ArtemisLed led, SKPoint renderPoint);
internal override void InternalRender(SKCanvas canvas, SKPath path, SKPaint paint)
internal override void InternalRender(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
// We don't want rotation on this canvas because that'll displace the LEDs, translations are applied to the points of each LED instead
if (Layer.General.TransformMode.CurrentValue == LayerTransformMode.Normal && SupportsTransformation)

View File

@ -68,7 +68,7 @@ namespace Artemis.Core.LayerBrushes
}
// Not used in this effect type
internal override void InternalRender(SKCanvas canvas, SKPath path, SKPaint paint)
internal override void InternalRender(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
throw new NotImplementedException("RGB.NET layer effectes do not implement InternalRender");
}

View File

@ -132,7 +132,7 @@ namespace Artemis.Core.LayerEffects
/// <param name="canvas">The canvas used to render the frame</param>
/// <param name="renderBounds">The bounds this layer/folder will render in</param>
/// <param name="paint">The paint this layer/folder will use to render</param>
public abstract void PreProcess(SKCanvas canvas, SKPath renderBounds, SKPaint paint);
public abstract void PreProcess(SKCanvas canvas, SKRect renderBounds, SKPaint paint);
/// <summary>
/// Called after the layer of folder has been rendered
@ -140,7 +140,7 @@ namespace Artemis.Core.LayerEffects
/// <param name="canvas">The canvas used to render the frame</param>
/// <param name="renderBounds">The bounds this layer/folder rendered in</param>
/// <param name="paint">The paint this layer/folder used to render</param>
public abstract void PostProcess(SKCanvas canvas, SKPath renderBounds, SKPaint paint);
public abstract void PostProcess(SKCanvas canvas, SKRect renderBounds, SKPaint paint);
// Not only is this needed to initialize properties on the layer effects, it also prevents implementing anything
// but LayerEffect<T> outside the core

View File

@ -40,12 +40,12 @@ namespace Artemis.Core.LayerEffects.Placeholder
}
/// <inheritdoc />
public override void PreProcess(SKCanvas canvas, SKPath renderBounds, SKPaint paint)
public override void PreProcess(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
}
/// <inheritdoc />
public override void PostProcess(SKCanvas canvas, SKPath renderBounds, SKPaint paint)
public override void PostProcess(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
}

View File

@ -123,19 +123,28 @@ namespace Artemis.Core.Services
#endregion
public List<Plugin> GetAllPlugins()
{
lock (_plugins)
{
return new List<Plugin>(_plugins);
}
}
public List<T> GetFeaturesOfType<T>() where T : PluginFeature
{
lock (_plugins)
{
return _plugins.Where(p => p.IsEnabled).SelectMany(p => p.Features.Where(i => i.IsEnabled && i is T)).Cast<T>().ToList();
}
}
public Plugin? GetPluginByAssembly(Assembly assembly)
{
lock (_plugins)
{
return _plugins.FirstOrDefault(p => p.Assembly == assembly);
}
}
// TODO: move to a more appropriate service
public DeviceProvider GetDeviceProviderByDevice(IRGBDevice rgbDevice)
@ -171,8 +180,6 @@ namespace Artemis.Core.Services
if (LoadingPlugins)
throw new ArtemisCoreException("Cannot load plugins while a previous load hasn't been completed yet.");
lock (_plugins)
{
LoadingPlugins = true;
// Unload all currently loaded plugins first
@ -196,23 +203,20 @@ namespace Artemis.Core.Services
LoadingPlugins = false;
}
}
public void UnloadPlugins()
{
lock (_plugins)
{
// Unload all plugins
while (_plugins.Count > 0)
UnloadPlugin(_plugins[0]);
lock (_plugins)
{
_plugins.Clear();
}
}
public Plugin LoadPlugin(DirectoryInfo directory)
{
lock (_plugins)
{
_logger.Debug("Loading plugin from {directory}", directory.FullName);
@ -227,9 +231,12 @@ namespace Artemis.Core.Services
if (pluginInfo.Guid == Constants.CorePluginInfo.Guid)
throw new ArtemisPluginException($"Plugin cannot use reserved GUID {pluginInfo.Guid}");
lock (_plugins)
{
// Ensure the plugin is not already loaded
if (_plugins.Any(p => p.Guid == pluginInfo.Guid))
throw new ArtemisCoreException("Cannot load a plugin that is already loaded");
}
Plugin plugin = new Plugin(pluginInfo, directory);
OnPluginLoading(new PluginEventArgs(plugin));
@ -264,13 +271,14 @@ namespace Artemis.Core.Services
if (bootstrappers.Any())
plugin.Bootstrapper = (IPluginBootstrapper?) Activator.CreateInstance(bootstrappers.First());
lock (_plugins)
{
_plugins.Add(plugin);
}
OnPluginLoaded(new PluginEventArgs(plugin));
return plugin;
}
}
public void EnablePlugin(Plugin plugin, bool saveState, bool ignorePluginLock)
{
@ -341,8 +349,6 @@ namespace Artemis.Core.Services
}
public void UnloadPlugin(Plugin plugin)
{
lock (_plugins)
{
try
{
@ -358,6 +364,8 @@ namespace Artemis.Core.Services
}
plugin.Dispose();
lock (_plugins)
{
_plugins.Remove(plugin);
}
}
@ -401,8 +409,7 @@ namespace Artemis.Core.Services
{
_logger.Debug("Enabling plugin feature {feature} - {plugin}", pluginFeature, pluginFeature.Plugin);
lock (_plugins)
{
OnPluginFeatureEnabling(new PluginFeatureEventArgs(pluginFeature));
try
{
@ -441,11 +448,8 @@ namespace Artemis.Core.Services
}
}
}
}
public void DisablePluginFeature(PluginFeature pluginFeature, bool saveState)
{
lock (_plugins)
{
try
{
@ -467,7 +471,6 @@ namespace Artemis.Core.Services
}
}
}
}
#endregion