[9102] | 1 | ///
|
---|
| 2 | /// This file is part of ILNumerics Community Edition.
|
---|
| 3 | ///
|
---|
| 4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
| 5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
| 6 | ///
|
---|
| 7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
| 8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
| 9 | /// the Free Software Foundation.
|
---|
| 10 | ///
|
---|
| 11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
| 12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | /// GNU General Public License for more details.
|
---|
| 15 | ///
|
---|
| 16 | /// You should have received a copy of the GNU General Public License
|
---|
| 17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
| 18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | ///
|
---|
| 20 | /// In addition this software uses the following components and/or licenses:
|
---|
| 21 | ///
|
---|
| 22 | /// =================================================================================
|
---|
| 23 | /// The Open Toolkit Library License
|
---|
| 24 | ///
|
---|
| 25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
| 26 | ///
|
---|
| 27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
| 29 | /// in the Software without restriction, including without limitation the rights to
|
---|
| 30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
| 31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
| 32 | /// so, subject to the following conditions:
|
---|
| 33 | ///
|
---|
| 34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
| 35 | /// copies or substantial portions of the Software.
|
---|
| 36 | ///
|
---|
| 37 | /// =================================================================================
|
---|
| 38 | ///
|
---|
| 39 |
|
---|
| 40 | using System;
|
---|
| 41 | using System.Collections.Generic;
|
---|
| 42 | using System.Text;
|
---|
| 43 | using System.Drawing;
|
---|
| 44 | using ILNumerics.Drawing.Platform.OpenGL;
|
---|
| 45 |
|
---|
| 46 | namespace ILNumerics.Drawing.Labeling {
|
---|
| 47 | /// <summary>
|
---|
| 48 | /// manages multiple texture sheets for storage and rendering
|
---|
| 49 | /// </summary>
|
---|
| 50 | public class ILTextureManager {
|
---|
| 51 |
|
---|
| 52 | #region events
|
---|
| 53 | /// <summary>
|
---|
| 54 | /// Fires, when the managed texture sheets are cleared. Registrars must rebuild needed items afterwards
|
---|
| 55 | /// </summary>
|
---|
| 56 | public event EventHandler TextureCacheCleared;
|
---|
| 57 | #endregion
|
---|
| 58 |
|
---|
| 59 | #region attributes / properties
|
---|
| 60 | protected int m_lastTextureSheet = -1;
|
---|
| 61 | private List<ILTextureStorage> m_textureSheets;
|
---|
| 62 | /// <summary>
|
---|
| 63 | /// All texture sheets must conform to this graphics device
|
---|
| 64 | /// </summary>
|
---|
| 65 | /// <remarks>the graphics device type must be the same as
|
---|
| 66 | /// the one used by the panel!</remarks>
|
---|
| 67 | public GraphicDeviceType DeviceType {
|
---|
| 68 | get { return m_deviceType; }
|
---|
| 69 | set { m_deviceType = value; }
|
---|
| 70 | }
|
---|
| 71 | private GraphicDeviceType m_deviceType;
|
---|
| 72 | /// <summary>
|
---|
| 73 | /// Default height for new texture sheets
|
---|
| 74 | /// </summary>
|
---|
| 75 | public int DefaultHeight {
|
---|
| 76 | get { return m_defaultHeight; }
|
---|
| 77 | set { m_defaultHeight = value; }
|
---|
| 78 | }
|
---|
| 79 | private int m_defaultHeight;
|
---|
| 80 | /// <summary>
|
---|
| 81 | /// Default width for new texture sheets
|
---|
| 82 | /// </summary>
|
---|
| 83 | public int DefaultWidth {
|
---|
| 84 | get { return m_defaultWidth; }
|
---|
| 85 | set { m_defaultWidth = value; }
|
---|
| 86 | }
|
---|
| 87 | private int m_defaultWidth;
|
---|
| 88 | private static object m_lock = new object();
|
---|
| 89 | #endregion
|
---|
| 90 |
|
---|
| 91 | #region constructor
|
---|
| 92 | internal ILTextureManager(GraphicDeviceType deviceType) {
|
---|
| 93 | m_textureSheets = new List<ILTextureStorage>();
|
---|
| 94 | m_deviceType = deviceType;
|
---|
| 95 | if (m_deviceType != GraphicDeviceType.OpenGL) {
|
---|
| 96 | throw new NotImplementedException("Only OpenGL contexts can be handled so far!");
|
---|
| 97 | }
|
---|
| 98 | m_defaultHeight = 500;
|
---|
| 99 | m_defaultWidth = 500;
|
---|
| 100 | }
|
---|
| 101 | #endregion
|
---|
| 102 |
|
---|
| 103 | #region public interface
|
---|
| 104 | /// <summary>
|
---|
| 105 | /// Clear all texture sheets and free their memory (cache) from GL.
|
---|
| 106 | /// </summary>
|
---|
| 107 | /// <remarks> This will also fire the TextureCacheCleared event.</remarks>
|
---|
| 108 | public void Clear() {
|
---|
| 109 | lock (m_lock) {
|
---|
| 110 | foreach(ILTextureStorage storage in m_textureSheets) {
|
---|
| 111 | storage.Dispose(true);
|
---|
| 112 | }
|
---|
| 113 | m_textureSheets.Clear();
|
---|
| 114 | if (TextureCacheCleared != null) {
|
---|
| 115 | TextureCacheCleared(this, new EventArgs());
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /// <summary>
|
---|
| 121 | /// Reset any caching may beeing active for this texture manager. Call this once to begin rendering.
|
---|
| 122 | /// </summary>
|
---|
| 123 | public void Reset() {
|
---|
| 124 | m_lastTextureSheet = -1;
|
---|
| 125 | }
|
---|
| 126 | /// <summary>
|
---|
| 127 | /// retrieve rendering information for texture item
|
---|
| 128 | /// </summary>
|
---|
| 129 | /// <param name="key">key used to identify the texture item</param>
|
---|
| 130 | /// <returns>texture item data</returns>
|
---|
| 131 | /// <remarks>If the key has been found in one of the texture sheets, the
|
---|
| 132 | /// corresponding texture sheet will be set as current</remarks>
|
---|
| 133 | public ILTextureData GetTextureItem(string key) {
|
---|
| 134 | return GetTextureItem(key,true);
|
---|
| 135 | }
|
---|
| 136 | /// <summary>
|
---|
| 137 | /// retrieve rendering information for texture item
|
---|
| 138 | /// </summary>
|
---|
| 139 | /// <param name="key">key used to identify the texture item</param>
|
---|
| 140 | /// <param name="makeCurrent">if true: sets the corresponding texture sheet
|
---|
| 141 | /// as 'current' for subsequent rendering</param>
|
---|
| 142 | /// <returns>texture item data</returns>
|
---|
| 143 | public ILTextureData GetTextureItem(string key, bool makeCurrent) {
|
---|
| 144 | ILTextureData ret;
|
---|
| 145 | lock (m_lock) {
|
---|
| 146 | foreach (ILTextureStorage storage in m_textureSheets) {
|
---|
| 147 | ret = storage.Get(key);
|
---|
| 148 | if (ret != null) {
|
---|
| 149 | if (makeCurrent && m_lastTextureSheet != storage.TextureID) {
|
---|
| 150 | #if PRINTTEXTURESHEETSWITCH
|
---|
| 151 | Console.Out.WriteLine(String.Format("TSheet switching from {0} to {1}",m_lastTextureSheet,storage.TextureID));
|
---|
| 152 | #endif
|
---|
| 153 | storage.MakeCurrent();
|
---|
| 154 | m_lastTextureSheet = storage.TextureID;
|
---|
| 155 | }
|
---|
| 156 | return ret;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | throw new ArgumentException("The key was not found in any texture sheet");
|
---|
| 161 | }
|
---|
| 162 | /// <summary>
|
---|
| 163 | /// retrieve rendering information for texture item
|
---|
| 164 | /// </summary>
|
---|
| 165 | /// <param name="key">key used to identify the texture item</param>
|
---|
| 166 | /// <param name="makeCurrent">if true: sets the corresponding texture sheet
|
---|
| 167 | /// as 'current' for subsequent rendering</param>
|
---|
| 168 | /// <param name="textureID">returns the texture sheet key of the item,
|
---|
| 169 | /// used to identify the texture in the graphic system</param>
|
---|
| 170 | /// <returns>texture item data</returns>
|
---|
| 171 | public ILTextureData GetTextureItem(string key, bool makeCurrent, out int textureID) {
|
---|
| 172 | ILTextureData ret;
|
---|
| 173 | lock (m_lock) {
|
---|
| 174 | foreach (ILTextureStorage storage in m_textureSheets) {
|
---|
| 175 | ret = storage.Get(key);
|
---|
| 176 | if (ret != null) {
|
---|
| 177 | if (makeCurrent) {
|
---|
| 178 | storage.MakeCurrent();
|
---|
| 179 | }
|
---|
| 180 | textureID = storage.TextureID;
|
---|
| 181 | return ret;
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | throw new ArgumentException("The key was not found in any texture sheet");
|
---|
| 186 | }
|
---|
| 187 | /// <summary>
|
---|
| 188 | /// Store item into texture cache, use full bitmap
|
---|
| 189 | /// </summary>
|
---|
| 190 | /// <param name="key">key used to identify the texture in cache</param>
|
---|
| 191 | /// <param name="item">bitmap holding the texture content</param>
|
---|
| 192 | /// <returns>true if the texture was successfully stored, false otherwise</returns>
|
---|
| 193 | public bool StoreTextureItem(string key, Bitmap item) {
|
---|
| 194 | return StoreTextureItem(key,item, new Rectangle(new Point(), item.Size));
|
---|
| 195 | }
|
---|
| 196 | /// <summary>
|
---|
| 197 | /// Store item into texture cache, use full bitmap
|
---|
| 198 | /// </summary>
|
---|
| 199 | /// <param name="key">key used to identify the texture in cache</param>
|
---|
| 200 | /// <param name="item">bitmap holding the texture content</param>
|
---|
| 201 | /// <param name="rect">rectangle defining the area in item used for content</param>
|
---|
| 202 | /// <returns>true if the texture was successfully stored, false otherwise</returns>
|
---|
| 203 | public bool StoreTextureItem(string key, Bitmap item, Rectangle rect) {
|
---|
| 204 | lock (m_lock) {
|
---|
| 205 | foreach (ILTextureStorage storage in m_textureSheets) {
|
---|
| 206 | if (storage.Store(key,item,rect)) {
|
---|
| 207 | return true;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | ILTextureStorage stor = CreateStorage();
|
---|
| 211 | m_textureSheets.Add(stor);
|
---|
| 212 | return stor.Store(key,item,rect);
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | public bool StoreTextureItem(string key, Bitmap item, RectangleF usedArea) {
|
---|
| 216 | lock (m_lock) {
|
---|
| 217 | foreach (ILTextureStorage storage in m_textureSheets) {
|
---|
| 218 | if (storage.Store(key,item,usedArea)) {
|
---|
| 219 | return true;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 | ILTextureStorage stor = CreateStorage();
|
---|
| 223 | m_textureSheets.Add(stor);
|
---|
| 224 | return stor.Store(key,item,usedArea);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | /// <summary>
|
---|
| 228 | /// test, if a texture item for the given key exists
|
---|
| 229 | /// in any texture storage sheets
|
---|
| 230 | /// </summary>
|
---|
| 231 | /// <param name="key">unique key to be tested for</param>
|
---|
| 232 | /// <returns>true, if a corresponding texture item exists, otherwise false</returns>
|
---|
| 233 | public bool Exists (string key) {
|
---|
| 234 | lock (m_lock) {
|
---|
| 235 | foreach (ILTextureStorage storage in m_textureSheets) {
|
---|
| 236 | if (storage.Exists(key)) {
|
---|
| 237 | return true;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | return false;
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 | public bool TryGetTextureItem (string key, out ILTextureData item) {
|
---|
| 244 | lock (m_lock) {
|
---|
| 245 | foreach (ILTextureStorage storage in m_textureSheets) {
|
---|
| 246 | if (storage.TryGetTextureItem(key, out item)) {
|
---|
| 247 | return true;
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | item = null;
|
---|
| 251 | return false;
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | internal void Dispose() {
|
---|
| 256 | Clear();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | #endregion
|
---|
| 260 |
|
---|
| 261 | #region helper functions
|
---|
| 262 |
|
---|
| 263 | private ILTextureStorage CreateStorage () {
|
---|
| 264 | switch (m_deviceType) {
|
---|
| 265 | case GraphicDeviceType.OpenGL:
|
---|
| 266 | return new ILOGLTextureStorage(m_defaultWidth,m_defaultHeight);
|
---|
| 267 | default:
|
---|
| 268 | throw new NotImplementedException();
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | #endregion
|
---|
| 272 |
|
---|
| 273 | }
|
---|
| 274 | }
|
---|