From 809600ea47fb49323494fc8247861c54b1b95341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Sun, 15 Jun 2014 20:33:09 +0000 Subject: [PATCH] Fixed Thread-safe image loading --- src/image/image.cpp | 48 +++++++++++++++++++++++++++++++++++++------ src/lol/image/image.h | 11 ++++++++-- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/image/image.cpp b/src/image/image.cpp index ddd0e951..9dac7c27 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -63,6 +63,8 @@ static class ImageBank public: void Init(); Image *Create(char const *path); + bool Store(Image *img); + Image *Load(char const *path); void Unref(Image *img); @@ -80,8 +82,6 @@ void ImageBank::Init() Image *ImageBank::Create(char const *path) { - Init(); - /* Is our image already in the bank? If so, no need to create it. */ Image *img; @@ -91,15 +91,35 @@ Image *ImageBank::Create(char const *path) } else { - m_images[path] = new Image(); - m_images[path]->m_data = ImageCodec::Load(path); - img = m_images[path]; + img = Load(path); + m_images[path] = img; } ++img->m_data->m_refcount; return img; } +Image *ImageBank::Load(char const *path) +{ + Init(); + + Image *img = new Image(path); + img->m_data = ImageCodec::Load(img->GetPath().C()); + return img; +} + +bool ImageBank::Store(Image *img) +{ + /* Is our image already in the bank? If so, no need to create it. */ + if (m_images.HasKey(img->GetPath().C())) + return false; + + m_images[img->GetPath().C()] = img; + + ++img->m_data->m_refcount; + return true; +} + void ImageBank::Unref(Image *img) { ASSERT(img->m_data->m_refcount > 0); @@ -125,13 +145,24 @@ Image *Image::Create(char const *path) return g_image_bank.Create(path); } +Image *Image::Load(char const *path) +{ + return g_image_bank.Load(path); +} + +bool Image::Store(Image *img) +{ + return g_image_bank.Store(img); +} + /* * Public Image class */ -Image::Image() +Image::Image(char const* path) : m_data(nullptr) { + m_path = path; } void Image::Destroy() @@ -159,6 +190,11 @@ uint8_t *Image::GetData() const return m_data->GetData(); } +String Image::GetPath() const +{ + return m_path; +} + bool Image::RetrieveTiles(Array& tiles) const { return m_data->RetrieveTiles(tiles); diff --git a/src/lol/image/image.h b/src/lol/image/image.h index a065f017..8da761e0 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -26,7 +26,12 @@ class Image friend class ImageBank; public: - static Image *Create(char const *path); + //Create/Load/Store image into bank. THREAD: NOT SAFE + static Image* Create(char const *path); + //Create/Load image into bank. THREAD: SAFE + static Image* Load(char const *path); + //Store image into bank. THREAD: NOT SAFE + static bool Store(Image *img); bool Save(char const *path); void Destroy(); @@ -34,13 +39,15 @@ public: ivec2 GetSize() const; PixelFormat GetFormat() const; uint8_t *GetData() const; + String GetPath() const; bool RetrieveTiles(Array& tiles) const; private: - Image(); + Image(char const* path); ~Image(); class ImageData *m_data; + String m_path; }; } /* namespace lol */