summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/render/bgfx/texturemanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/osd/modules/render/bgfx/texturemanager.cpp')
-rw-r--r--src/osd/modules/render/bgfx/texturemanager.cpp170
1 files changed, 101 insertions, 69 deletions
diff --git a/src/osd/modules/render/bgfx/texturemanager.cpp b/src/osd/modules/render/bgfx/texturemanager.cpp
index a5fbca97f35..b9acd5748e8 100644
--- a/src/osd/modules/render/bgfx/texturemanager.cpp
+++ b/src/osd/modules/render/bgfx/texturemanager.cpp
@@ -9,23 +9,26 @@
//
//============================================================
-#include <bgfx/bgfx.h>
-
#include "texturemanager.h"
-#include "texture.h"
+
#include "bgfxutil.h"
-#include "rendutil.h"
+#include "texture.h"
+
#include "modules/render/copyutil.h"
+#include "osdcore.h"
+
+#include "emucore.h"
+#include "fileio.h"
+#include "rendutil.h"
+
+
+texture_manager::texture_manager()
+{
+ // out-of-line so header works with forward declarations
+}
texture_manager::~texture_manager()
{
- for (std::pair<std::string, bgfx_texture_handle_provider*> texture : m_textures)
- {
- if (texture.second != nullptr && !(texture.second)->is_target())
- {
- delete texture.second;
- }
- }
m_textures.clear();
for (std::pair<uint64_t, sequenced_handle> mame_texture : m_mame_textures)
@@ -35,68 +38,95 @@ texture_manager::~texture_manager()
m_mame_textures.clear();
}
-void texture_manager::add_provider(std::string name, bgfx_texture_handle_provider* provider)
+void texture_manager::add_provider(const std::string &name, std::unique_ptr<bgfx_texture_handle_provider> &&provider)
{
- std::map<std::string, bgfx_texture_handle_provider*>::iterator iter = m_textures.find(name);
+ const auto iter = m_textures.find(name);
if (iter != m_textures.end())
- {
- iter->second = provider;
- }
+ iter->second = std::make_pair(provider.get(), std::move(provider));
else
- {
- m_textures[name] = provider;
- }
+ m_textures.emplace(name, std::make_pair(provider.get(), std::move(provider)));
+}
+
+void texture_manager::add_provider(const std::string &name, bgfx_texture_handle_provider &provider)
+{
+ const auto iter = m_textures.find(name);
+ if (iter != m_textures.end())
+ iter->second = std::make_pair(&provider, nullptr);
+ else
+ m_textures.emplace(name, std::make_pair(&provider, nullptr));
}
-bgfx_texture* texture_manager::create_texture(std::string name, bgfx::TextureFormat::Enum format, uint32_t width, uint32_t height, void* data, uint32_t flags)
+bgfx_texture* texture_manager::create_texture(
+ const std::string &name,
+ bgfx::TextureFormat::Enum format,
+ uint32_t width,
+ uint32_t width_margin,
+ uint32_t height,
+ void* data,
+ uint32_t flags)
{
- bgfx_texture* texture = new bgfx_texture(name, format, width, height, flags, data);
- m_textures[name] = texture;
- return texture;
+ auto texture = std::make_unique<bgfx_texture>(name, format, width, width_margin, height, flags, data);
+ bgfx_texture &result = *texture;
+ m_textures[name] = std::make_pair(texture.get(), std::move(texture));
+ return &result;
}
-bgfx_texture* texture_manager::create_png_texture(std::string path, std::string file_name, std::string texture_name, uint32_t flags, uint32_t screen)
+bgfx_texture* texture_manager::create_png_texture(
+ const std::string &path,
+ const std::string &file_name,
+ std::string texture_name,
+ uint32_t flags,
+ uint32_t screen)
{
bitmap_argb32 bitmap;
- emu_file file(path.c_str(), OPEN_FLAG_READ);
- render_load_png(bitmap, file, nullptr, file_name.c_str());
+ emu_file file(path, OPEN_FLAG_READ);
+ if (!file.open(file_name))
+ {
+ render_load_png(bitmap, file);
+ file.close();
+ }
if (bitmap.width() == 0 || bitmap.height() == 0)
{
- printf("Unable to load PNG '%s' from path '%s'\n", path.c_str(), file_name.c_str());
+ osd_printf_error("Unable to load PNG '%s' from path '%s'\n", file_name, path);
return nullptr;
}
- uint8_t *data = new uint8_t[bitmap.width() * bitmap.height() * 4];
- uint32_t *data32 = reinterpret_cast<uint32_t *>(data);
-
const uint32_t width = bitmap.width();
const uint32_t height = bitmap.height();
+ auto data32 = std::make_unique<uint32_t []>(width * height);
+
const uint32_t rowpixels = bitmap.rowpixels();
- uint32_t* base = reinterpret_cast<uint32_t *>(bitmap.raw_pixptr(0));
+ auto* base = reinterpret_cast<uint32_t *>(bitmap.raw_pixptr(0));
for (int y = 0; y < height; y++)
{
- copy_util::copyline_argb32(data32 + y * width, base + y * rowpixels, width, nullptr);
+ copy_util::copyline_argb32_to_bgra(&data32[y * width], base + y * rowpixels, width, nullptr);
}
if (screen >= 0)
{
texture_name += std::to_string(screen);
}
- bgfx_texture* texture = create_texture(texture_name, bgfx::TextureFormat::RGBA8, width, height, data, flags);
-
- delete [] data;
-
- return texture;
+ return create_texture(texture_name, bgfx::TextureFormat::BGRA8, width, 0, height, data32.get(), flags);
}
-bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t format, int width, int height
- , int rowpixels, const rgb_t *palette, void *base, uint32_t seqid, uint32_t flags, uint64_t key, uint64_t old_key)
+bgfx::TextureHandle texture_manager::create_or_update_mame_texture(
+ uint32_t format,
+ int width,
+ int width_margin,
+ int height,
+ int rowpixels,
+ const rgb_t *palette,
+ void *base,
+ uint32_t seqid,
+ uint32_t flags,
+ uint64_t key,
+ uint64_t old_key)
{
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
if (old_key != ~0ULL)
{
- std::map<uint64_t, sequenced_handle>::iterator iter = m_mame_textures.find(old_key);
+ const auto iter = m_mame_textures.find(old_key);
if (iter != m_mame_textures.end())
{
handle = iter->second.handle;
@@ -116,8 +146,12 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form
}
else
{
- const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(format, width, height, rowpixels, palette, base);
- bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)width, (uint16_t)height, mem);
+ bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
+ uint16_t pitch = width;
+ int width_div_factor = 1;
+ int width_mul_factor = 1;
+ const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, width_margin, height, palette, base, pitch, width_div_factor, width_mul_factor);
+ bgfx::updateTexture2D(handle, 0, 0, 0, 0, uint16_t((rowpixels * width_mul_factor) / width_div_factor), uint16_t(height), mem, pitch);
return handle;
}
}
@@ -127,7 +161,7 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form
}
else
{
- std::map<uint64_t, sequenced_handle>::iterator iter = m_mame_textures.find(key);
+ auto iter = m_mame_textures.find(key);
if (iter != m_mame_textures.end())
{
handle = iter->second.handle;
@@ -142,8 +176,12 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form
}
else
{
- const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(format, width, height, rowpixels, palette, base);
- bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)width, (uint16_t)height, mem);
+ bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
+ uint16_t pitch = width;
+ int width_div_factor = 1;
+ int width_mul_factor = 1;
+ const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, width_margin, height, palette, base, pitch, width_div_factor, width_mul_factor);
+ bgfx::updateTexture2D(handle, 0, 0, 0, 0, uint16_t((rowpixels * width_mul_factor) / width_div_factor), uint16_t(height), mem, pitch);
return handle;
}
}
@@ -151,46 +189,40 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form
}
}
- const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(format, width, height, rowpixels, palette, base);
- handle = bgfx::createTexture2D(width, height, false, 1, bgfx::TextureFormat::RGBA8, flags, nullptr);
- bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)width, (uint16_t)height, mem);
+ bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
+ uint16_t pitch = width;
+ int width_div_factor = 1;
+ int width_mul_factor = 1;
+ const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, width_margin, height, palette, base, pitch, width_div_factor, width_mul_factor);
+ const uint16_t adjusted_width = uint16_t((rowpixels * width_mul_factor) / width_div_factor);
+ handle = bgfx::createTexture2D(adjusted_width, height, false, 1, dst_format, flags, nullptr);
+ bgfx::updateTexture2D(handle, 0, 0, 0, 0, adjusted_width, uint16_t(height), mem, pitch);
m_mame_textures[key] = { handle, seqid, width, height };
return handle;
}
-bgfx::TextureHandle texture_manager::handle(std::string name)
+bgfx::TextureHandle texture_manager::handle(const std::string &name)
{
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
- std::map<std::string, bgfx_texture_handle_provider*>::iterator iter = m_textures.find(name);
+ const auto iter = m_textures.find(name);
if (iter != m_textures.end())
- {
- handle = (iter->second)->texture();
- }
+ handle = iter->second.first->texture();
assert(handle.idx != bgfx::kInvalidHandle);
return handle;
}
-bgfx_texture_handle_provider* texture_manager::provider(std::string name)
+bgfx_texture_handle_provider* texture_manager::provider(const std::string &name)
{
- std::map<std::string, bgfx_texture_handle_provider*>::iterator iter = m_textures.find(name);
+ const auto iter = m_textures.find(name);
if (iter != m_textures.end())
- {
- return iter->second;
- }
-
- return nullptr;
+ return iter->second.first;
+ else
+ return nullptr;
}
-void texture_manager::remove_provider(std::string name, bool delete_provider)
+void texture_manager::remove_provider(const std::string &name)
{
- if (provider(name) != nullptr)
- {
- if (delete_provider)
- {
- delete m_textures[name];
- }
- m_textures[name] = nullptr;
- }
+ m_textures.erase(name);
}