private static void cachedTexture_Disposed(object sender, EventArgs e)
        {
            lock (GUIGraphicsContext.RenderLock)
            {
                CachedTexture cached = sender as CachedTexture;
                if (cached != null)
                {
                    string cacheKey = cached.Name.ToLowerInvariant();

                    cached.Disposed -= new EventHandler(cachedTexture_Disposed);

                    bool removed;
                    _persistentTextures.TryRemove(cacheKey, out removed);

                    CachedTexture removedItem;
                    _cacheTextures.TryRemove(cacheKey, out removedItem);
                }
            }
        }
Exemplo n.º 2
0
        private static void cachedTexture_Disposed(object sender, EventArgs e)
        {
            lock (GUIGraphicsContext.RenderLock)
            {
                CachedTexture cached = sender as CachedTexture;
                if (cached != null)
                {
                    string cacheKey = cached.Name.ToLowerInvariant();

                    // a texture in the cache has been disposed of! remove from the cache
                    //if (_cacheTextures.ContainsKey(cacheKey))
                    //{
                    //Log.Debug("TextureManager: Already disposed texture - cleaning up the cache...");
                    cached.Disposed -= new EventHandler(cachedTexture_Disposed);
                    //if (_persistentTextures.ContainsKey(cacheKey))
                    //{
                    _persistentTextures.Remove(cacheKey); // remove is fine even if it doesnt exist
                    //}
                    _cacheTextures.Remove(cacheKey);
                    //}
                }
            }
        }
Exemplo n.º 3
0
        public static int LoadFromMemoryEx(Image memoryImage, string name, long lColorKey, out Texture texture)
        {
            Log.Debug("TextureManagerEx: load from memory: {0}", name);
            string cacheName = name;
            string cacheKey  = cacheName.ToLowerInvariant();

            texture = null;
            CachedTexture cached;

            if (_cacheTextures.TryGetValue(cacheKey, out cached))
            {
                return(cached.Frames);
            }

            if (memoryImage == null)
            {
                return(0);
            }
            try
            {
                CachedTexture newCache = new CachedTexture();

                newCache.Name   = cacheName;
                newCache.Frames = 1;

                //load gif into texture
                using (MemoryStream stream = new MemoryStream())
                {
                    memoryImage.Save(stream, ImageFormat.Png);
                    ImageInformation info2 = new ImageInformation();
                    stream.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    texture = TextureLoader.FromStream(
                        GUIGraphicsContext.DX9Device,
                        stream,
                        0, 0,          //width/height
                        1,             //mipslevels
                        Usage.Dynamic, //Usage.Dynamic,
                        Format.A8R8G8B8,
                        Pool.Default,
                        Filter.None,
                        Filter.None,
                        (int)lColorKey,
                        ref info2);
                    newCache.Width   = info2.Width;
                    newCache.Height  = info2.Height;
                    newCache.texture = new CachedTexture.Frame(cacheName, texture, 0);
                }
                //memoryImage.SafeDispose();
                //memoryImage = null;
                newCache.Disposed += new EventHandler(cachedTexture_Disposed);

                _cacheTextures[cacheKey] = newCache;

                Log.Debug("TextureManager: added: memoryImage  " + " total count: " + _cacheTextures.Count + ", mem left (MB): " +
                          ((uint)GUIGraphicsContext.DX9Device.AvailableTextureMemory / 1048576));
                return(newCache.Frames);
            }
            catch (Exception ex)
            {
                Log.Error("TextureManager: exception loading texture memoryImage");
                Log.Error(ex);
            }
            return(0);
        }
Exemplo n.º 4
0
        public static int Load(string fileNameOrg, long lColorKey, int iMaxWidth, int iMaxHeight, bool persistent)
        {
            string fileName = GetFileName(fileNameOrg);
            string cacheKey = fileName.ToLowerInvariant();

            if (String.IsNullOrEmpty(fileName))
            {
                return(0);
            }

            CachedTexture cached;

            if (_cacheTextures.TryGetValue(cacheKey, out cached))
            {
                return(cached.Frames);
            }

            string extension = Path.GetExtension(fileName).ToLowerInvariant();

            if (extension == ".gif")
            {
                Image theImage = null;
                try
                {
                    try
                    {
                        theImage = ImageFast.FromFile(fileName);
                    }
                    catch (FileNotFoundException)
                    {
                        Log.Warn("TextureManager: texture: {0} does not exist", fileName);
                        return(0);
                    }
                    catch (Exception)
                    {
                        Log.Warn("TextureManager: Fast loading texture {0} failed using safer fallback", fileName);
                        theImage = Image.FromFile(fileName);
                    }
                    if (theImage != null)
                    {
                        CachedTexture newCache = new CachedTexture();

                        newCache.Name = fileName;
                        FrameDimension oDimension = new FrameDimension(theImage.FrameDimensionsList[0]);
                        newCache.Frames = theImage.GetFrameCount(oDimension);
                        int[] frameDelay = new int[newCache.Frames];
                        for (int num2 = 0; (num2 < newCache.Frames); ++num2)
                        {
                            frameDelay[num2] = 0;
                        }

                        // Getting Frame duration of an animated Gif image
                        try
                        {
                            int          num1  = 20736;
                            PropertyItem item1 = theImage.GetPropertyItem(num1);
                            if (item1 != null)
                            {
                                byte[] buffer1 = item1.Value;
                                for (int num2 = 0; (num2 < newCache.Frames); ++num2)
                                {
                                    frameDelay[num2] = (((buffer1[(num2 * 4)] + (256 * buffer1[((num2 * 4) + 1)])) +
                                                         (65536 * buffer1[((num2 * 4) + 2)])) + (16777216 * buffer1[((num2 * 4) + 3)]));
                                }
                            }
                        }
                        catch (Exception) { }

                        for (int i = 0; i < newCache.Frames; ++i)
                        {
                            theImage.SelectActiveFrame(oDimension, i);

                            //load gif into texture
                            using (MemoryStream stream = new MemoryStream())
                            {
                                theImage.Save(stream, ImageFormat.Png);
                                ImageInformation info2 = new ImageInformation();
                                stream.Flush();
                                stream.Seek(0, SeekOrigin.Begin);
                                Texture texture = TextureLoader.FromStream(
                                    GUIGraphicsContext.DX9Device,
                                    stream,
                                    0, 0, //width/height
                                    1,    //mipslevels
                                    0,    //Usage.Dynamic,
                                    Format.A8R8G8B8,
                                    GUIGraphicsContext.GetTexturePoolType(),
                                    Filter.None,
                                    Filter.None,
                                    (int)lColorKey,
                                    ref info2);
                                newCache.Width  = info2.Width;
                                newCache.Height = info2.Height;
                                newCache[i]     = new CachedTexture.Frame(fileName, texture, (frameDelay[i] / 5) * 50);
                            }
                        }

                        theImage.SafeDispose();
                        theImage           = null;
                        newCache.Disposed += new EventHandler(cachedTexture_Disposed);
                        if (persistent && !_persistentTextures.ContainsKey(cacheKey))
                        {
                            _persistentTextures[cacheKey] = true;
                        }

                        _cacheTextures[cacheKey] = newCache;

                        //Log.Info("  TextureManager:added:" + fileName + " total:" + _cache.Count + " mem left:" + GUIGraphicsContext.DX9Device.AvailableTextureMemory.ToString());
                        return(newCache.Frames);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("TextureManager: exception loading texture {0} - {1}", fileName, ex.Message);
                }
                return(0);
            }

            try
            {
                int width, height;

                if (MediaPortal.Util.Utils.FileExistsInCache(fileName))
                {
                    Texture dxtexture = LoadGraphic(fileName, lColorKey, iMaxWidth, iMaxHeight, out width, out height);
                    if (dxtexture != null)
                    {
                        CachedTexture newCache = new CachedTexture();
                        newCache.Name    = fileName;
                        newCache.Frames  = 1;
                        newCache.Width   = width;
                        newCache.Height  = height;
                        newCache.texture = new CachedTexture.Frame(fileName, dxtexture, 0);
                        //Log.Info("  texturemanager:added:" + fileName + " total:" + _cache.Count + " mem left:" + GUIGraphicsContext.DX9Device.AvailableTextureMemory.ToString());
                        newCache.Disposed += new EventHandler(cachedTexture_Disposed);
                        if (persistent && !_persistentTextures.ContainsKey(cacheKey))
                        {
                            _persistentTextures[cacheKey] = true;
                        }

                        _cacheTextures[cacheKey] = newCache;
                        return(1);
                    }
                }
            }
            catch (Exception)
            {
                return(0);
            }
            return(0);
        }
    public static int LoadFromMemoryEx(Image memoryImage, string name, long lColorKey, out Texture texture)
    {
      Log.Debug("TextureManagerEx: load from memory: {0}", name);
      string cacheName = name;
      string cacheKey = cacheName.ToLower();

      texture = null;
      CachedTexture cached;
      if (_cacheTextures.TryGetValue(cacheKey, out cached))
      {
        return cached.Frames;
      }

      if (memoryImage == null)
      {
        return 0;
      }
      try
      {
        CachedTexture newCache = new CachedTexture();

        newCache.Name = cacheName;
        newCache.Frames = 1;

        //load gif into texture
        using (MemoryStream stream = new MemoryStream())
        {
          memoryImage.Save(stream, ImageFormat.Png);
          ImageInformation info2 = new ImageInformation();
          stream.Flush();
          stream.Seek(0, SeekOrigin.Begin);
          texture = TextureLoader.FromStream(
            GUIGraphicsContext.DX9Device,
            stream,
            0, 0, //width/height
            1, //mipslevels
            Usage.Dynamic, //Usage.Dynamic,
            Format.A8R8G8B8,
            Pool.Default,
            Filter.None,
            Filter.None,
            (int)lColorKey,
            ref info2);
          newCache.Width = info2.Width;
          newCache.Height = info2.Height;
          newCache.texture = new CachedTexture.Frame(cacheName, texture, 0);
        }
        //memoryImage.SafeDispose();
        //memoryImage = null;
        newCache.Disposed += new EventHandler(cachedTexture_Disposed);

        _cacheTextures[cacheKey] = newCache;

        Log.Debug("TextureManager: added: memoryImage  " + " total count: " + _cacheTextures.Count + ", mem left (MB): " +
                  ((uint)GUIGraphicsContext.DX9Device.AvailableTextureMemory / 1048576));
        return newCache.Frames;
      }
      catch (Exception ex)
      {
        Log.Error("TextureManager: exception loading texture memoryImage");
        Log.Error(ex);
      }
      return 0;
    }
    public static int Load(string fileNameOrg, long lColorKey, int iMaxWidth, int iMaxHeight, bool persistent)
    {
      string fileName = GetFileName(fileNameOrg);
      string cacheKey = fileName.ToLower();
      if (String.IsNullOrEmpty(fileName))
      {
        return 0;
      }

      CachedTexture cached;
      if (_cacheTextures.TryGetValue(cacheKey, out cached))
      {
        return cached.Frames;
      }

      string extension = Path.GetExtension(fileName).ToLower();
      if (extension == ".gif")
      {
        Image theImage = null;
        try
        {
          try
          {
            theImage = ImageFast.FromFile(fileName);
          }
          catch (FileNotFoundException)
          {
            Log.Warn("TextureManager: texture: {0} does not exist", fileName);
            return 0;
          }
          catch (Exception)
          {
            Log.Warn("TextureManager: Fast loading texture {0} failed using safer fallback", fileName);
            theImage = Image.FromFile(fileName);
          }
          if (theImage != null)
          {
            CachedTexture newCache = new CachedTexture();

            newCache.Name = fileName;
            FrameDimension oDimension = new FrameDimension(theImage.FrameDimensionsList[0]);
            newCache.Frames = theImage.GetFrameCount(oDimension);
            int[] frameDelay = new int[newCache.Frames];
            for (int num2 = 0; (num2 < newCache.Frames); ++num2)
            {
              frameDelay[num2] = 0;
            }

            // Getting Frame duration of an animated Gif image            
            try
            {
              int num1 = 20736;
              PropertyItem item1 = theImage.GetPropertyItem(num1);
              if (item1 != null)
              {
                byte[] buffer1 = item1.Value;
                for (int num2 = 0; (num2 < newCache.Frames); ++num2)
                {
                  frameDelay[num2] = (((buffer1[(num2 * 4)] + (256 * buffer1[((num2 * 4) + 1)])) +
                                       (65536 * buffer1[((num2 * 4) + 2)])) + (16777216 * buffer1[((num2 * 4) + 3)]));
                }
              }
            }
            catch (Exception) {}

            for (int i = 0; i < newCache.Frames; ++i)
            {
              theImage.SelectActiveFrame(oDimension, i);

              //load gif into texture
              using (MemoryStream stream = new MemoryStream())
              {
                theImage.Save(stream, ImageFormat.Png);
                ImageInformation info2 = new ImageInformation();
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                Texture texture = TextureLoader.FromStream(
                  GUIGraphicsContext.DX9Device,
                  stream,
                  0, 0, //width/height
                  1, //mipslevels
                  0, //Usage.Dynamic,
                  Format.A8R8G8B8,
                  GUIGraphicsContext.GetTexturePoolType(),
                  Filter.None,
                  Filter.None,
                  (int)lColorKey,
                  ref info2);
                newCache.Width = info2.Width;
                newCache.Height = info2.Height;
                newCache[i] = new CachedTexture.Frame(fileName, texture, (frameDelay[i] / 5) * 50);
              }
            }

            theImage.SafeDispose();
            theImage = null;
            newCache.Disposed += new EventHandler(cachedTexture_Disposed);
            if (persistent && !_persistentTextures.ContainsKey(cacheKey))
            {
              _persistentTextures[cacheKey] = true;
            }

            _cacheTextures[cacheKey] = newCache;

            //Log.Info("  TextureManager:added:" + fileName + " total:" + _cache.Count + " mem left:" + GUIGraphicsContext.DX9Device.AvailableTextureMemory.ToString());
            return newCache.Frames;
          }
        }
        catch (Exception ex)
        {
          Log.Error("TextureManager: exception loading texture {0} - {1}", fileName, ex.Message);
        }
        return 0;
      }

      try
      {
        int width, height;
        Texture dxtexture = LoadGraphic(fileName, lColorKey, iMaxWidth, iMaxHeight, out width, out height);
        if (dxtexture != null)
        {
          CachedTexture newCache = new CachedTexture();
          newCache.Name = fileName;
          newCache.Frames = 1;
          newCache.Width = width;
          newCache.Height = height;
          newCache.texture = new CachedTexture.Frame(fileName, dxtexture, 0);
          //Log.Info("  texturemanager:added:" + fileName + " total:" + _cache.Count + " mem left:" + GUIGraphicsContext.DX9Device.AvailableTextureMemory.ToString());
          newCache.Disposed += new EventHandler(cachedTexture_Disposed);
          if (persistent && !_persistentTextures.ContainsKey(cacheKey))
          {
            _persistentTextures[cacheKey] = true;
          }

          _cacheTextures[cacheKey] = newCache;
          return 1;
        }
      }
      catch (Exception)
      {
        return 0;
      }
      return 0;
    }
        public static int LoadFromMemory(Image memoryImage, string name, long lColorKey, int iMaxWidth, int iMaxHeight)
        {
            bool bDebugLog = !name.StartsWith("[NoLog:");

            if (bDebugLog)
            {
                Log.Debug("TextureManager: load from memory: {0}", name);
            }
            string cacheName = name;
            string cacheKey  = name.ToLowerInvariant();

            CachedTexture cached;

            if (_cacheTextures.TryGetValue(cacheKey, out cached))
            {
                return(cached.Frames);
            }

            if (memoryImage == null)
            {
                return(0);
            }
            if (memoryImage.FrameDimensionsList == null)
            {
                return(0);
            }
            if (memoryImage.FrameDimensionsList.Length == 0)
            {
                return(0);
            }

            try
            {
                CachedTexture newCache = new CachedTexture();

                newCache.Name = cacheName;
                FrameDimension oDimension = new FrameDimension(memoryImage.FrameDimensionsList[0]);
                newCache.Frames = memoryImage.GetFrameCount(oDimension);
                if (newCache.Frames != 1)
                {
                    return(0);
                }
                //load gif into texture
                using (MemoryStream stream = new MemoryStream())
                {
                    memoryImage.Save(stream, ImageFormat.Png);
                    ImageInformation info2 = new ImageInformation();
                    stream.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    Texture texture = TextureLoader.FromStream(
                        GUIGraphicsContext.DX9Device,
                        stream,
                        0, 0, //width/height
                        1,    //mipslevels
                        0,    //Usage.Dynamic,
                        Format.A8R8G8B8,
                        GUIGraphicsContext.GetTexturePoolType(),
                        Filter.None,
                        Filter.None,
                        (int)lColorKey,
                        ref info2);
                    newCache.Width   = info2.Width;
                    newCache.Height  = info2.Height;
                    newCache.Texture = new TextureFrame(cacheName, texture, 0);
                }
                memoryImage.SafeDispose();
                memoryImage        = null;
                newCache.Disposed += new EventHandler(cachedTexture_Disposed);

                _cacheTextures[cacheKey] = newCache;

                if (bDebugLog)
                {
                    Log.Debug("TextureManager: added: memoryImage  " + " total count: " + _cacheTextures.Count + ", mem left (MB): " +
                              ((uint)GUIGraphicsContext.DX9Device.AvailableTextureMemory / 1048576));
                }
                return(newCache.Frames);
            }
            catch (Exception ex)
            {
                Log.Error("TextureManager: exception loading texture memoryImage");
                Log.Error(ex);
            }
            return(0);
        }