2018-12-30 17:42:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 flyinghead
|
|
|
|
|
|
|
|
This file is part of reicast.
|
|
|
|
|
|
|
|
reicast is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
reicast is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with reicast. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "CustomTexture.h"
|
2020-03-28 16:58:01 +00:00
|
|
|
#include "cfg/cfg.h"
|
2018-12-30 17:42:55 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <dirent.h>
|
2019-09-04 16:40:18 +00:00
|
|
|
#include <png.h>
|
2020-03-28 16:58:01 +00:00
|
|
|
#include <sstream>
|
2020-04-07 12:32:09 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
#define STBI_ONLY_JPEG
|
|
|
|
#include <stb_image.h>
|
2018-12-30 17:42:55 +00:00
|
|
|
|
2020-03-13 17:01:33 +00:00
|
|
|
CustomTexture custom_texture;
|
2019-10-04 10:22:18 +00:00
|
|
|
|
2018-12-30 17:42:55 +00:00
|
|
|
void CustomTexture::LoaderThread()
|
|
|
|
{
|
2020-04-07 12:32:09 +00:00
|
|
|
LoadMap();
|
2018-12-30 17:42:55 +00:00
|
|
|
while (initialized)
|
|
|
|
{
|
2019-10-04 10:22:18 +00:00
|
|
|
BaseTextureCacheData *texture;
|
2018-12-30 17:42:55 +00:00
|
|
|
|
2018-12-30 21:23:23 +00:00
|
|
|
do {
|
|
|
|
texture = NULL;
|
|
|
|
|
2020-03-30 12:24:33 +00:00
|
|
|
work_queue_mutex.lock();
|
2018-12-30 21:23:23 +00:00
|
|
|
if (!work_queue.empty())
|
2018-12-30 17:42:55 +00:00
|
|
|
{
|
2018-12-30 21:23:23 +00:00
|
|
|
texture = work_queue.back();
|
|
|
|
work_queue.pop_back();
|
2018-12-30 17:42:55 +00:00
|
|
|
}
|
2020-03-30 12:24:33 +00:00
|
|
|
work_queue_mutex.unlock();
|
2018-12-30 21:23:23 +00:00
|
|
|
|
|
|
|
if (texture != NULL)
|
|
|
|
{
|
|
|
|
texture->ComputeHash();
|
2019-04-06 17:38:00 +00:00
|
|
|
if (texture->custom_image_data != NULL)
|
2019-03-26 20:14:39 +00:00
|
|
|
{
|
2019-04-06 17:38:00 +00:00
|
|
|
delete [] texture->custom_image_data;
|
|
|
|
texture->custom_image_data = NULL;
|
2019-03-26 20:14:39 +00:00
|
|
|
}
|
2019-04-06 17:38:00 +00:00
|
|
|
if (!texture->dirty)
|
2018-12-30 21:23:23 +00:00
|
|
|
{
|
2019-04-06 17:38:00 +00:00
|
|
|
int width, height;
|
|
|
|
u8 *image_data = LoadCustomTexture(texture->texture_hash, width, height);
|
|
|
|
if (image_data == NULL)
|
|
|
|
{
|
|
|
|
image_data = LoadCustomTexture(texture->old_texture_hash, width, height);
|
|
|
|
}
|
|
|
|
if (image_data != NULL)
|
|
|
|
{
|
|
|
|
texture->custom_width = width;
|
|
|
|
texture->custom_height = height;
|
|
|
|
texture->custom_image_data = image_data;
|
|
|
|
}
|
2018-12-30 21:23:23 +00:00
|
|
|
}
|
2019-04-06 17:38:00 +00:00
|
|
|
texture->custom_load_in_progress--;
|
2018-12-30 21:23:23 +00:00
|
|
|
}
|
2019-01-02 08:33:19 +00:00
|
|
|
|
2018-12-30 21:23:23 +00:00
|
|
|
} while (texture != NULL);
|
2018-12-30 17:42:55 +00:00
|
|
|
|
|
|
|
wakeup_thread.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 08:33:19 +00:00
|
|
|
std::string CustomTexture::GetGameId()
|
|
|
|
{
|
2019-10-25 19:50:59 +00:00
|
|
|
std::string game_id(cfgGetGameId());
|
2019-09-07 12:37:39 +00:00
|
|
|
const size_t str_end = game_id.find_last_not_of(' ');
|
2019-01-02 08:33:19 +00:00
|
|
|
if (str_end == std::string::npos)
|
|
|
|
return "";
|
|
|
|
game_id = game_id.substr(0, str_end + 1);
|
|
|
|
std::replace(game_id.begin(), game_id.end(), ' ', '_');
|
|
|
|
|
|
|
|
return game_id;
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:42:55 +00:00
|
|
|
bool CustomTexture::Init()
|
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
initialized = true;
|
2019-01-02 08:33:19 +00:00
|
|
|
std::string game_id = GetGameId();
|
|
|
|
if (game_id.length() > 0)
|
2018-12-30 17:42:55 +00:00
|
|
|
{
|
2019-07-24 16:24:58 +00:00
|
|
|
textures_path = get_readonly_data_path(DATA_PATH) + "textures/" + game_id + "/";
|
2018-12-30 17:42:55 +00:00
|
|
|
|
|
|
|
DIR *dir = opendir(textures_path.c_str());
|
|
|
|
if (dir != NULL)
|
|
|
|
{
|
2019-07-01 15:41:15 +00:00
|
|
|
INFO_LOG(RENDERER, "Found custom textures directory: %s", textures_path.c_str());
|
2018-12-30 17:42:55 +00:00
|
|
|
custom_textures_available = true;
|
|
|
|
closedir(dir);
|
|
|
|
loader_thread.Start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return custom_textures_available;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CustomTexture::Terminate()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
initialized = false;
|
2020-03-30 12:24:33 +00:00
|
|
|
work_queue_mutex.lock();
|
2018-12-30 17:42:55 +00:00
|
|
|
work_queue.clear();
|
2020-03-30 12:24:33 +00:00
|
|
|
work_queue_mutex.unlock();
|
2018-12-30 17:42:55 +00:00
|
|
|
wakeup_thread.Set();
|
|
|
|
loader_thread.WaitToEnd();
|
2020-04-07 12:32:09 +00:00
|
|
|
texture_map.clear();
|
2018-12-30 17:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* CustomTexture::LoadCustomTexture(u32 hash, int& width, int& height)
|
|
|
|
{
|
2020-04-07 12:32:09 +00:00
|
|
|
auto it = texture_map.find(hash);
|
|
|
|
if (it == texture_map.end())
|
|
|
|
return nullptr;
|
|
|
|
char c = it->second[it->second.length() - 2];
|
|
|
|
if (c == 'n' || c == 'N')
|
|
|
|
return loadPNGData(it->second.c_str(), width, height);
|
|
|
|
stbi_set_flip_vertically_on_load(1);
|
|
|
|
int n;
|
|
|
|
return stbi_load(it->second.c_str(), &width, &height, &n, 4);
|
2018-12-30 17:42:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:22:18 +00:00
|
|
|
void CustomTexture::LoadCustomTextureAsync(BaseTextureCacheData *texture_data)
|
2018-12-30 17:42:55 +00:00
|
|
|
{
|
|
|
|
if (!Init())
|
|
|
|
return;
|
2019-04-06 17:38:00 +00:00
|
|
|
|
|
|
|
texture_data->custom_load_in_progress++;
|
2020-03-30 12:24:33 +00:00
|
|
|
work_queue_mutex.lock();
|
2018-12-30 17:42:55 +00:00
|
|
|
work_queue.insert(work_queue.begin(), texture_data);
|
2020-03-30 12:24:33 +00:00
|
|
|
work_queue_mutex.unlock();
|
2018-12-30 17:42:55 +00:00
|
|
|
wakeup_thread.Set();
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:22:18 +00:00
|
|
|
void CustomTexture::DumpTexture(u32 hash, int w, int h, TextureType textype, void *temp_tex_buffer)
|
2019-01-02 08:33:19 +00:00
|
|
|
{
|
2019-07-24 16:24:58 +00:00
|
|
|
std::string base_dump_dir = get_writable_data_path(DATA_PATH "texdump/");
|
2019-01-02 08:33:19 +00:00
|
|
|
if (!file_exists(base_dump_dir))
|
2019-02-13 19:29:49 +00:00
|
|
|
make_directory(base_dump_dir);
|
2019-01-02 08:33:19 +00:00
|
|
|
std::string game_id = GetGameId();
|
|
|
|
if (game_id.length() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
base_dump_dir += game_id + "/";
|
|
|
|
if (!file_exists(base_dump_dir))
|
2019-02-13 19:29:49 +00:00
|
|
|
make_directory(base_dump_dir);
|
2019-01-02 08:33:19 +00:00
|
|
|
|
|
|
|
std::stringstream path;
|
|
|
|
path << base_dump_dir << std::hex << hash << ".png";
|
|
|
|
FILE *fp = fopen(path.str().c_str(), "wb");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2019-07-01 15:41:15 +00:00
|
|
|
WARN_LOG(RENDERER, "Failed to open %s for writing", path.str().c_str());
|
2019-01-02 08:33:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 *src = (u16 *)temp_tex_buffer;
|
|
|
|
|
|
|
|
png_bytepp rows = (png_bytepp)malloc(h * sizeof(png_bytep));
|
|
|
|
for (int y = 0; y < h; y++)
|
|
|
|
{
|
2019-03-26 20:14:39 +00:00
|
|
|
rows[h - y - 1] = (png_bytep)malloc(w * 4); // 32-bit per pixel
|
|
|
|
u8 *dst = (u8 *)rows[h - y - 1];
|
2019-01-02 08:33:19 +00:00
|
|
|
switch (textype)
|
|
|
|
{
|
2019-10-04 10:22:18 +00:00
|
|
|
case TextureType::_4444:
|
2019-01-02 08:33:19 +00:00
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
*dst++ = ((*src >> 12) & 0xF) << 4;
|
|
|
|
*dst++ = ((*src >> 8) & 0xF) << 4;
|
|
|
|
*dst++ = ((*src >> 4) & 0xF) << 4;
|
|
|
|
*dst++ = (*src & 0xF) << 4;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
break;
|
2019-10-04 10:22:18 +00:00
|
|
|
case TextureType::_565:
|
2019-01-02 08:33:19 +00:00
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
*dst++ = ((*src >> 11) & 0x1F) << 3;
|
|
|
|
*dst++ = ((*src >> 5) & 0x3F) << 2;
|
|
|
|
*dst++ = (*src & 0x1F) << 3;
|
|
|
|
*dst++ = 255;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
break;
|
2019-10-04 10:22:18 +00:00
|
|
|
case TextureType::_5551:
|
2019-01-02 08:33:19 +00:00
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
*dst++ = ((*src >> 11) & 0x1F) << 3;
|
|
|
|
*dst++ = ((*src >> 6) & 0x1F) << 3;
|
|
|
|
*dst++ = ((*src >> 1) & 0x1F) << 3;
|
|
|
|
*dst++ = (*src & 1) ? 255 : 0;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
break;
|
2019-10-04 10:22:18 +00:00
|
|
|
case TextureType::_8888:
|
2019-01-02 08:33:19 +00:00
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
*(u32 *)dst = *(u32 *)src;
|
|
|
|
dst += 4;
|
|
|
|
src += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2019-10-04 10:22:18 +00:00
|
|
|
WARN_LOG(RENDERER, "dumpTexture: unsupported picture format %x", (u32)textype);
|
2019-01-02 08:33:19 +00:00
|
|
|
fclose(fp);
|
|
|
|
free(rows[0]);
|
|
|
|
free(rows);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
|
|
|
png_init_io(png_ptr, fp);
|
|
|
|
|
|
|
|
|
|
|
|
// write header
|
|
|
|
png_set_IHDR(png_ptr, info_ptr, w, h,
|
|
|
|
8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
|
|
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
|
|
|
|
// write bytes
|
|
|
|
png_write_image(png_ptr, rows);
|
|
|
|
|
|
|
|
// end write
|
|
|
|
png_write_end(png_ptr, NULL);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
for (int y = 0; y < h; y++)
|
|
|
|
free(rows[y]);
|
|
|
|
free(rows);
|
|
|
|
}
|
2020-04-07 12:32:09 +00:00
|
|
|
|
|
|
|
void CustomTexture::LoadMap()
|
|
|
|
{
|
|
|
|
texture_map.clear();
|
|
|
|
DIR *dir = opendir(textures_path.c_str());
|
|
|
|
if (dir == nullptr)
|
|
|
|
return;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
struct dirent *entry = readdir(dir);
|
|
|
|
if (entry == nullptr)
|
|
|
|
break;
|
|
|
|
std::string name(entry->d_name);
|
|
|
|
if (name == "." || name == "..")
|
|
|
|
continue;
|
2020-04-07 14:32:38 +00:00
|
|
|
std::string child_path = textures_path + name;
|
2020-04-07 12:32:09 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (entry->d_type == DT_DIR)
|
|
|
|
continue;
|
|
|
|
if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(child_path.c_str(), &st) != 0)
|
|
|
|
continue;
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string extension = get_file_extension(name);
|
|
|
|
if (extension != "jpg" && extension != "jpeg" && extension != "png")
|
|
|
|
continue;
|
|
|
|
std::string::size_type dotpos = name.find_last_of('.');
|
|
|
|
std::string basename = name.substr(0, dotpos);
|
|
|
|
char *endptr;
|
2020-04-07 14:32:38 +00:00
|
|
|
u32 hash = (u32)strtoll(basename.c_str(), &endptr, 16);
|
2020-04-07 12:32:09 +00:00
|
|
|
if (endptr - basename.c_str() < (ptrdiff_t)basename.length())
|
|
|
|
{
|
|
|
|
INFO_LOG(RENDERER, "Invalid hash %s", basename.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
texture_map[hash] = child_path;
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
custom_textures_available = !texture_map.empty();
|
|
|
|
}
|