2019-08-13 14:44:37 +08:00
|
|
|
// Copyright (c) 2016-2019 Kiwano - Nomango
|
2019-03-31 01:37:06 +08:00
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
#include "ImageCache.h"
|
2019-08-16 00:50:54 +08:00
|
|
|
#include "Renderer.h"
|
2019-08-14 21:52:49 +08:00
|
|
|
#include "../base/Logger.h"
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
ImageCache::ImageCache()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
ImageCache::~ImageCache()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
Image ImageCache::AddImage(Resource const& res)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
size_t hash_code = res.GetHashCode();
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
auto iter = image_cache_.find(hash_code);
|
|
|
|
|
if (iter != image_cache_.end())
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return iter->second;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
Image image;
|
|
|
|
|
if (image.Load(res))
|
2019-08-13 14:44:37 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
image_cache_.insert(std::make_pair(hash_code, image));
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
2019-08-16 00:50:54 +08:00
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageCache::RemoveImage(Resource const& res)
|
|
|
|
|
{
|
|
|
|
|
size_t hash_code = res.GetHashCode();
|
|
|
|
|
|
|
|
|
|
auto iter = image_cache_.find(hash_code);
|
|
|
|
|
if (iter != image_cache_.end())
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
image_cache_.erase(iter);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GifImagePtr ImageCache::AddGifImage(Resource const& res)
|
|
|
|
|
{
|
|
|
|
|
size_t hash_code = res.GetHashCode();
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
auto iter = gif_image_cache_.find(hash_code);
|
|
|
|
|
if (iter != gif_image_cache_.end())
|
2019-08-13 14:44:37 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
return iter->second;
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
2019-08-16 00:50:54 +08:00
|
|
|
|
|
|
|
|
GifImagePtr ptr = new GifImage;
|
|
|
|
|
if (ptr->Load(res))
|
2019-08-13 14:44:37 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
gif_image_cache_.insert(std::make_pair(hash_code, ptr));
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
2019-08-16 00:50:54 +08:00
|
|
|
return ptr;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void ImageCache::RemoveGifImage(Resource const& res)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
size_t hash_code = res.GetHashCode();
|
2019-08-16 00:50:54 +08:00
|
|
|
|
|
|
|
|
auto iter = gif_image_cache_.find(hash_code);
|
|
|
|
|
if (iter != gif_image_cache_.end())
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
gif_image_cache_.erase(iter);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 14:44:37 +08:00
|
|
|
void ImageCache::Clear()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
image_cache_.clear();
|
2019-08-16 00:50:54 +08:00
|
|
|
gif_image_cache_.clear();
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|