Skip to content

Commit

Permalink
Get rid of more unnecessary uses of ManagedTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Dec 12, 2023
1 parent 4c3f82d commit 2aca8fe
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 43 deletions.
29 changes: 15 additions & 14 deletions Common/Render/ManagedTexture.cpp
Expand Up @@ -152,6 +152,19 @@ Draw::Texture *CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t
return texture;
}

Draw::Texture *CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType type, bool generateMips) {
INFO_LOG(SYSTEM, "CreateTextureFromFile(%s)", filename);
size_t fileSize;
uint8_t *buffer = g_VFS.ReadFile(filename, &fileSize);
if (!buffer) {
ERROR_LOG(IO, "Failed to read file '%s'", filename);
return nullptr;
}
Draw::Texture *texture = CreateTextureFromFileData(draw, buffer, fileSize, type, generateMips, filename);
delete[] buffer;
return texture;
}

bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, ImageFileType type, bool generateMips, const char *name) {
generateMips_ = generateMips;

Expand All @@ -171,6 +184,7 @@ bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, Imag
}

bool ManagedTexture::LoadFromFile(const std::string &filename, ImageFileType type, bool generateMips) {
INFO_LOG(SYSTEM, "ManagedTexture::LoadFromFile (%s)", filename.c_str());
generateMips_ = generateMips;
size_t fileSize;
uint8_t *buffer = g_VFS.ReadFile(filename.c_str(), &fileSize);
Expand All @@ -191,6 +205,7 @@ bool ManagedTexture::LoadFromFile(const std::string &filename, ImageFileType typ
}

std::unique_ptr<ManagedTexture> CreateManagedTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType type, bool generateMips) {
INFO_LOG(SYSTEM, "ManagedTexture::CreateFromFile (%s)", filename);
if (!draw)
return std::unique_ptr<ManagedTexture>();
// TODO: Load the texture on a background thread.
Expand Down Expand Up @@ -234,17 +249,3 @@ Draw::Texture *ManagedTexture::GetTexture() {
}
return texture_;
}

// TODO: Remove the code duplication between this and LoadFromFileData
std::unique_ptr<ManagedTexture> CreateManagedTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType type, bool generateMips, const char *name) {
if (!draw)
return std::unique_ptr<ManagedTexture>();
ManagedTexture *mtex = new ManagedTexture(draw);
if (mtex->LoadFromFileData(data, size, type, generateMips, name)) {
return std::unique_ptr<ManagedTexture>(mtex);
} else {
// Best to return a null pointer if we fail!
delete mtex;
return std::unique_ptr<ManagedTexture>();
}
}
2 changes: 1 addition & 1 deletion Common/Render/ManagedTexture.h
Expand Up @@ -41,6 +41,6 @@ class ManagedTexture {
};

Draw::Texture *CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, size_t dataSize, ImageFileType type, bool generateMips, const char *name);
Draw::Texture *CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType type, bool generateMips);

std::unique_ptr<ManagedTexture> CreateManagedTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType fileType, bool generateMips);
std::unique_ptr<ManagedTexture> CreateManagedTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType fileType, bool generateMips, const char *name);
14 changes: 8 additions & 6 deletions Common/UI/Context.cpp
Expand Up @@ -22,6 +22,8 @@ UIContext::~UIContext() {
sampler_->Release();
delete fontStyle_;
delete textDrawer_;
uitexture_->Release();
fontTexture_->Release();
}

void UIContext::Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pipeline *uipipenotex, DrawBuffer *uidrawbuffer) {
Expand All @@ -41,17 +43,17 @@ void UIContext::setUIAtlas(const std::string &name) {

void UIContext::BeginFrame() {
if (!uitexture_ || UIAtlas_ != lastUIAtlas_) {
uitexture_ = CreateManagedTextureFromFile(draw_, UIAtlas_.c_str(), ImageFileType::ZIM, false);
uitexture_ = CreateTextureFromFile(draw_, UIAtlas_.c_str(), ImageFileType::ZIM, false);
lastUIAtlas_ = UIAtlas_;
if (!fontTexture_) {
#if PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID)
// Don't bother with loading font_atlas.zim
#else
fontTexture_ = CreateManagedTextureFromFile(draw_, "font_atlas.zim", ImageFileType::ZIM, false);
fontTexture_ = CreateTextureFromFile(draw_, "font_atlas.zim", ImageFileType::ZIM, false);
#endif
if (!fontTexture_) {
// Load the smaller ascii font only, like on Android. For debug ui etc.
fontTexture_ = CreateManagedTextureFromFile(draw_, "asciifont_atlas.zim", ImageFileType::ZIM, false);
fontTexture_ = CreateTextureFromFile(draw_, "asciifont_atlas.zim", ImageFileType::ZIM, false);
if (!fontTexture_) {
WARN_LOG(SYSTEM, "Failed to load font_atlas.zim or asciifont_atlas.zim");
}
Expand Down Expand Up @@ -88,15 +90,15 @@ void UIContext::BeginPipeline(Draw::Pipeline *pipeline, Draw::SamplerState *samp

void UIContext::RebindTexture() const {
if (uitexture_)
draw_->BindTexture(0, uitexture_->GetTexture());
draw_->BindTexture(0, uitexture_);
}

void UIContext::BindFontTexture() const {
// Fall back to the UI texture, in case they have an old atlas.
if (fontTexture_)
draw_->BindTexture(0, fontTexture_->GetTexture());
draw_->BindTexture(0, fontTexture_);
else if (uitexture_)
draw_->BindTexture(0, uitexture_->GetTexture());
draw_->BindTexture(0, uitexture_);
}

void UIContext::Flush() {
Expand Down
5 changes: 2 additions & 3 deletions Common/UI/Context.h
Expand Up @@ -23,7 +23,6 @@ namespace Draw {
}

class Texture;
class ManagedTexture;
class DrawBuffer;
class TextDrawer;

Expand Down Expand Up @@ -128,8 +127,8 @@ class UIContext {
Draw::SamplerState *sampler_ = nullptr;
Draw::Pipeline *ui_pipeline_ = nullptr;
Draw::Pipeline *ui_pipeline_notex_ = nullptr;
std::unique_ptr<ManagedTexture> uitexture_;
std::unique_ptr<ManagedTexture> fontTexture_;
Draw::Texture *uitexture_ = nullptr;
Draw::Texture *fontTexture_ = nullptr;

DrawBuffer *uidrawbuffer_ = nullptr;

Expand Down
13 changes: 12 additions & 1 deletion UI/GameInfoCache.cpp
Expand Up @@ -27,9 +27,9 @@
#include "Common/File/VFS/VFS.h"
#include "Common/File/FileUtil.h"
#include "Common/File/Path.h"
#include "Common/Render/ManagedTexture.h"
#include "Common/StringUtils.h"
#include "Common/TimeUtil.h"
#include "Common/Render/ManagedTexture.h"
#include "Core/FileSystems/ISOFileSystem.h"
#include "Core/FileSystems/DirectoryFileSystem.h"
#include "Core/FileSystems/VirtualDiscFileSystem.h"
Expand All @@ -43,6 +43,17 @@

GameInfoCache *g_gameInfoCache;

void GameInfoTex::Clear() {
if (!data.empty()) {
data.clear();
dataLoaded = false;
}
if (texture) {
texture->Release();
texture = nullptr;
}
}

GameInfo::GameInfo() : fileType(IdentifiedFileType::UNKNOWN) {
pending = true;
}
Expand Down
12 changes: 1 addition & 11 deletions UI/GameInfoCache.h
Expand Up @@ -26,7 +26,6 @@
#include "Common/Thread/Event.h"
#include "Core/ELF/ParamSFO.h"
#include "Common/File/Path.h"
#include "Common/Render/ManagedTexture.h"

namespace Draw {
class DrawContext;
Expand Down Expand Up @@ -71,16 +70,7 @@ struct GameInfoTex {
std::atomic<bool> dataLoaded{};

// Can ONLY be called from the main thread!
void Clear() {
if (!data.empty()) {
data.clear();
dataLoaded = false;
}
if (texture) {
texture->Release();
texture = nullptr;
}
}
void Clear();
};

class GameInfo {
Expand Down
19 changes: 12 additions & 7 deletions UI/MiscScreens.cpp
Expand Up @@ -39,6 +39,8 @@
#include "Common/Data/Random/Rng.h"
#include "Common/TimeUtil.h"
#include "Common/File/FileUtil.h"
#include "Common/Render/ManagedTexture.h"

#include "Core/Config.h"
#include "Core/System.h"
#include "Core/MIPS/JitCommon/JitCommon.h"
Expand Down Expand Up @@ -74,7 +76,7 @@ static const uint32_t colors[4] = {
0xC0FFFFFF,
};

static std::unique_ptr<ManagedTexture> bgTexture;
static Draw::Texture *bgTexture;

class Animation {
public:
Expand All @@ -96,7 +98,7 @@ class MovingBackground : public Animation {
return;

dc.Flush();
dc.GetDrawContext()->BindTexture(0, bgTexture->GetTexture());
dc.GetDrawContext()->BindTexture(0, bgTexture);
Bounds bounds = dc.GetBounds();

x = std::min(std::max(x/bounds.w, 0.0f), 1.0f) * XFAC;
Expand Down Expand Up @@ -287,22 +289,25 @@ class RecentGamesAnimation : public Animation {

static BackgroundAnimation g_CurBackgroundAnimation = BackgroundAnimation::OFF;
static std::unique_ptr<Animation> g_Animation;
static bool bgTextureInited = false;
static bool bgTextureInited = false; // Separate variable because init could fail.

void UIBackgroundInit(UIContext &dc) {
const Path bgPng = GetSysDirectory(DIRECTORY_SYSTEM) / "background.png";
const Path bgJpg = GetSysDirectory(DIRECTORY_SYSTEM) / "background.jpg";
if (File::Exists(bgPng) || File::Exists(bgJpg)) {
const Path &bgFile = File::Exists(bgPng) ? bgPng : bgJpg;
bgTexture = CreateManagedTextureFromFile(dc.GetDrawContext(), bgFile.c_str(), DETECT, true);
bgTexture = CreateTextureFromFile(dc.GetDrawContext(), bgFile.c_str(), DETECT, true);
}
}

void UIBackgroundShutdown() {
bgTexture.reset(nullptr);
if (bgTexture) {
bgTexture->Release();
bgTexture = nullptr;
}
bgTextureInited = false;
g_Animation.reset(nullptr);
g_CurBackgroundAnimation = BackgroundAnimation::OFF;
bgTextureInited = false;
}

void DrawBackground(UIContext &dc, float alpha, float x, float y, float z) {
Expand Down Expand Up @@ -336,7 +341,7 @@ void DrawBackground(UIContext &dc, float alpha, float x, float y, float z) {
if (bgTexture != nullptr) {
dc.Flush();
dc.Begin();
dc.GetDrawContext()->BindTexture(0, bgTexture->GetTexture());
dc.GetDrawContext()->BindTexture(0, bgTexture);
dc.Draw()->DrawTexRect(dc.GetBounds(), 0, 0, 1, 1, bgColor);

dc.Flush();
Expand Down

0 comments on commit 2aca8fe

Please sign in to comment.