///
/// This file is part of ILNumerics Community Edition.
///
/// ILNumerics Community Edition - high performance computing for applications.
/// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
///
/// ILNumerics Community Edition is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License version 3 as published by
/// the Free Software Foundation.
///
/// ILNumerics Community Edition is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with ILNumerics Community Edition. See the file License.txt in the root
/// of your distribution package. If not, see .
///
/// In addition this software uses the following components and/or licenses:
///
/// =================================================================================
/// The Open Toolkit Library License
///
/// Copyright (c) 2006 - 2009 the Open Toolkit library.
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights to
/// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
/// the Software, and to permit persons to whom the Software is furnished to do
/// so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// =================================================================================
///
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ILNumerics.Drawing.Platform.OpenGL;
namespace ILNumerics.Drawing.Labeling {
///
/// manages multiple texture sheets for storage and rendering
///
public class ILTextureManager {
#region events
///
/// Fires, when the managed texture sheets are cleared. Registrars must rebuild needed items afterwards
///
public event EventHandler TextureCacheCleared;
#endregion
#region attributes / properties
protected int m_lastTextureSheet = -1;
private List m_textureSheets;
///
/// All texture sheets must conform to this graphics device
///
/// the graphics device type must be the same as
/// the one used by the panel!
public GraphicDeviceType DeviceType {
get { return m_deviceType; }
set { m_deviceType = value; }
}
private GraphicDeviceType m_deviceType;
///
/// Default height for new texture sheets
///
public int DefaultHeight {
get { return m_defaultHeight; }
set { m_defaultHeight = value; }
}
private int m_defaultHeight;
///
/// Default width for new texture sheets
///
public int DefaultWidth {
get { return m_defaultWidth; }
set { m_defaultWidth = value; }
}
private int m_defaultWidth;
private static object m_lock = new object();
#endregion
#region constructor
internal ILTextureManager(GraphicDeviceType deviceType) {
m_textureSheets = new List();
m_deviceType = deviceType;
if (m_deviceType != GraphicDeviceType.OpenGL) {
throw new NotImplementedException("Only OpenGL contexts can be handled so far!");
}
m_defaultHeight = 500;
m_defaultWidth = 500;
}
#endregion
#region public interface
///
/// Clear all texture sheets and free their memory (cache) from GL.
///
/// This will also fire the TextureCacheCleared event.
public void Clear() {
lock (m_lock) {
foreach(ILTextureStorage storage in m_textureSheets) {
storage.Dispose(true);
}
m_textureSheets.Clear();
if (TextureCacheCleared != null) {
TextureCacheCleared(this, new EventArgs());
}
}
}
///
/// Reset any caching may beeing active for this texture manager. Call this once to begin rendering.
///
public void Reset() {
m_lastTextureSheet = -1;
}
///
/// retrieve rendering information for texture item
///
/// key used to identify the texture item
/// texture item data
/// If the key has been found in one of the texture sheets, the
/// corresponding texture sheet will be set as current
public ILTextureData GetTextureItem(string key) {
return GetTextureItem(key,true);
}
///
/// retrieve rendering information for texture item
///
/// key used to identify the texture item
/// if true: sets the corresponding texture sheet
/// as 'current' for subsequent rendering
/// texture item data
public ILTextureData GetTextureItem(string key, bool makeCurrent) {
ILTextureData ret;
lock (m_lock) {
foreach (ILTextureStorage storage in m_textureSheets) {
ret = storage.Get(key);
if (ret != null) {
if (makeCurrent && m_lastTextureSheet != storage.TextureID) {
#if PRINTTEXTURESHEETSWITCH
Console.Out.WriteLine(String.Format("TSheet switching from {0} to {1}",m_lastTextureSheet,storage.TextureID));
#endif
storage.MakeCurrent();
m_lastTextureSheet = storage.TextureID;
}
return ret;
}
}
}
throw new ArgumentException("The key was not found in any texture sheet");
}
///
/// retrieve rendering information for texture item
///
/// key used to identify the texture item
/// if true: sets the corresponding texture sheet
/// as 'current' for subsequent rendering
/// returns the texture sheet key of the item,
/// used to identify the texture in the graphic system
/// texture item data
public ILTextureData GetTextureItem(string key, bool makeCurrent, out int textureID) {
ILTextureData ret;
lock (m_lock) {
foreach (ILTextureStorage storage in m_textureSheets) {
ret = storage.Get(key);
if (ret != null) {
if (makeCurrent) {
storage.MakeCurrent();
}
textureID = storage.TextureID;
return ret;
}
}
}
throw new ArgumentException("The key was not found in any texture sheet");
}
///
/// Store item into texture cache, use full bitmap
///
/// key used to identify the texture in cache
/// bitmap holding the texture content
/// true if the texture was successfully stored, false otherwise
public bool StoreTextureItem(string key, Bitmap item) {
return StoreTextureItem(key,item, new Rectangle(new Point(), item.Size));
}
///
/// Store item into texture cache, use full bitmap
///
/// key used to identify the texture in cache
/// bitmap holding the texture content
/// rectangle defining the area in item used for content
/// true if the texture was successfully stored, false otherwise
public bool StoreTextureItem(string key, Bitmap item, Rectangle rect) {
lock (m_lock) {
foreach (ILTextureStorage storage in m_textureSheets) {
if (storage.Store(key,item,rect)) {
return true;
}
}
ILTextureStorage stor = CreateStorage();
m_textureSheets.Add(stor);
return stor.Store(key,item,rect);
}
}
public bool StoreTextureItem(string key, Bitmap item, RectangleF usedArea) {
lock (m_lock) {
foreach (ILTextureStorage storage in m_textureSheets) {
if (storage.Store(key,item,usedArea)) {
return true;
}
}
ILTextureStorage stor = CreateStorage();
m_textureSheets.Add(stor);
return stor.Store(key,item,usedArea);
}
}
///
/// test, if a texture item for the given key exists
/// in any texture storage sheets
///
/// unique key to be tested for
/// true, if a corresponding texture item exists, otherwise false
public bool Exists (string key) {
lock (m_lock) {
foreach (ILTextureStorage storage in m_textureSheets) {
if (storage.Exists(key)) {
return true;
}
}
return false;
}
}
public bool TryGetTextureItem (string key, out ILTextureData item) {
lock (m_lock) {
foreach (ILTextureStorage storage in m_textureSheets) {
if (storage.TryGetTextureItem(key, out item)) {
return true;
}
}
item = null;
return false;
}
}
internal void Dispose() {
Clear();
}
#endregion
#region helper functions
private ILTextureStorage CreateStorage () {
switch (m_deviceType) {
case GraphicDeviceType.OpenGL:
return new ILOGLTextureStorage(m_defaultWidth,m_defaultHeight);
default:
throw new NotImplementedException();
}
}
#endregion
}
}