// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
using System.Collections.Generic;
namespace SixLabors.ImageSharp.Drawing.Processing.Backends {
///
/// Base type for retained drawing backend scenes.
///
public abstract class DrawingBackendScene : IDisposable
{
private readonly IReadOnlyList? ownedResources;
private bool isDisposed;
///
/// Initializes a new instance of the class.
///
/// The target bounds used to create the scene.
/// Resources that must stay alive for the retained scene.
protected DrawingBackendScene(
Rectangle bounds,
IReadOnlyList? ownedResources)
{
this.Bounds = bounds;
this.ownedResources = ownedResources;
}
///
/// Gets the target bounds used to create the scene.
///
public Rectangle Bounds { get; }
///
public void Dispose()
{
if (this.isDisposed)
{
return;
}
this.DisposeCore();
this.DisposeOwnedResources();
this.isDisposed = true;
GC.SuppressFinalize(this);
}
///
/// Disposes backend-specific resources retained by this scene.
///
protected virtual void DisposeCore()
{
}
///
/// Disposes resources retained for image-brush commands in this scene.
///
private void DisposeOwnedResources()
{
if (this.ownedResources is null)
{
return;
}
for (int i = 0; i < this.ownedResources.Count; i++)
{
this.ownedResources[i].Dispose();
}
}
}
}