From 8d0285dedd0822a0b20efd4b942cda8cfea626b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 27 Mar 2018 23:10:33 +0200 Subject: [PATCH] Android: Properly restore screenshots on the Pause screen on task switching away and back. Basically plumbs through DeviceLost/DeviceRestored to view elements and into ManagedTexture. --- UI/NativeApp.cpp | 3 +++ UI/PauseScreen.cpp | 14 +++++++++++-- UI/PauseScreen.h | 7 +++++-- UI/TextureUtil.cpp | 40 +++++++++++++++++++++++++++++++----- UI/TextureUtil.h | 9 ++++++-- ext/native/file/zip_read.cpp | 2 +- ext/native/ui/screen.cpp | 10 +++++++++ ext/native/ui/screen.h | 5 +++++ ext/native/ui/ui_screen.cpp | 10 +++++++++ ext/native/ui/ui_screen.h | 18 ++++++++-------- ext/native/ui/view.h | 4 ++++ ext/native/ui/viewgroup.cpp | 16 ++++++++++++++- ext/native/ui/viewgroup.h | 3 +++ 13 files changed, 120 insertions(+), 21 deletions(-) diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 6f5dcf48b6..8308e6fb00 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -681,6 +681,7 @@ bool NativeInitGraphics(GraphicsContext *graphicsContext) { screenManager->setUIContext(uiContext); screenManager->setDrawContext(g_draw); screenManager->setPostRenderCallback(&RenderOverlays, nullptr); + screenManager->deviceRestored(); #ifdef _WIN32 winAudioBackend = CreateAudioBackend((AudioBackendType)g_Config.iAudioBackend); @@ -702,6 +703,8 @@ bool NativeInitGraphics(GraphicsContext *graphicsContext) { } void NativeShutdownGraphics() { + screenManager->deviceLost(); + if (gpu) gpu->DeviceLost(); diff --git a/UI/PauseScreen.cpp b/UI/PauseScreen.cpp index 433b316997..160cb2c75e 100644 --- a/UI/PauseScreen.cpp +++ b/UI/PauseScreen.cpp @@ -49,7 +49,7 @@ AsyncImageFileView::AsyncImageFileView(const std::string &filename, UI::ImageSiz AsyncImageFileView::~AsyncImageFileView() {} void AsyncImageFileView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { - if (texture_) { + if (texture_ && texture_->GetTexture()) { float texw = (float)texture_->Width(); float texh = (float)texture_->Height(); switch (sizeMode_) { @@ -77,6 +77,16 @@ void AsyncImageFileView::SetFilename(std::string filename) { } } +void AsyncImageFileView::DeviceLost() { + if (texture_.get()) + texture_->DeviceLost(); +} + +void AsyncImageFileView::DeviceRestored(Draw::DrawContext *draw) { + if (texture_.get()) + texture_->DeviceRestored(draw); +} + void AsyncImageFileView::Draw(UIContext &dc) { using namespace Draw; if (!texture_ && !textureFailed_ && !filename_.empty()) { @@ -90,7 +100,7 @@ void AsyncImageFileView::Draw(UIContext &dc) { } // TODO: involve sizemode - if (texture_) { + if (texture_ && texture_->GetTexture()) { dc.Flush(); dc.GetDrawContext()->BindTexture(0, texture_->GetTexture()); dc.Draw()->Rect(bounds_.x, bounds_.y, bounds_.w, bounds_.h, color_); diff --git a/UI/PauseScreen.h b/UI/PauseScreen.h index 292f5fea0f..c6ffd88b0b 100644 --- a/UI/PauseScreen.h +++ b/UI/PauseScreen.h @@ -60,8 +60,8 @@ private: class PrioritizedWorkQueue; -// TextureView takes a texture that is assumed to be alive during the lifetime -// of the view. TODO: Actually make async using the task. +// AsyncImageFileView loads a texture from a file, and reloads it as necessary. +// TODO: Actually make async, doh. class AsyncImageFileView : public UI::Clickable { public: AsyncImageFileView(const std::string &filename, UI::ImageSizeMode sizeMode, PrioritizedWorkQueue *wq, UI::LayoutParams *layoutParams = 0); @@ -70,6 +70,9 @@ public: void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; void Draw(UIContext &dc) override; + void DeviceLost() override; + void DeviceRestored(Draw::DrawContext *draw) override; + void SetFilename(std::string filename); void SetColor(uint32_t color) { color_ = color; } void SetOverlayText(std::string text) { text_ = text; } diff --git a/UI/TextureUtil.cpp b/UI/TextureUtil.cpp index d934f956cf..b86693e2b6 100644 --- a/UI/TextureUtil.cpp +++ b/UI/TextureUtil.cpp @@ -7,6 +7,7 @@ #include "file/vfs.h" #include "ext/jpge/jpgd.h" #include "UI/TextureUtil.h" +#include "Common/Log.h" static Draw::DataFormat ZimToT3DFormat(int zim) { switch (zim) { @@ -37,7 +38,7 @@ static bool LoadTextureLevels(const uint8_t *data, size_t size, ImageFileType ty type = DetectImageFileType(data, size); } if (type == TYPE_UNKNOWN) { - ELOG("File has unknown format"); + ELOG("File (size: %d) has unknown format", (int)size); return false; } @@ -79,7 +80,7 @@ static bool LoadTextureLevels(const uint8_t *data, size_t size, ImageFileType ty break; default: - ELOG("Unknown image format"); + ELOG("Unsupported image format %d", (int)type); return false; } @@ -87,6 +88,7 @@ static bool LoadTextureLevels(const uint8_t *data, size_t size, ImageFileType ty } bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, ImageFileType type, bool generateMips) { + generateMips_ = generateMips; using namespace Draw; int width[16]{}, height[16]{}; @@ -135,18 +137,20 @@ bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, Imag } bool ManagedTexture::LoadFromFile(const std::string &filename, ImageFileType type, bool generateMips) { - filename_ = ""; + generateMips_ = generateMips; size_t fileSize; uint8_t *buffer = VFSReadFile(filename.c_str(), &fileSize); if (!buffer) { - ELOG("Failed to read file %s", filename.c_str()); + filename_ = ""; + ELOG("Failed to read file '%s'", filename.c_str()); return false; } bool retval = LoadFromFileData(buffer, fileSize, type, generateMips); if (retval) { filename_ = filename; } else { - ELOG("Failed to load texture %s", filename.c_str()); + filename_ = ""; + ELOG("Failed to load texture '%s'", filename.c_str()); } delete[] buffer; return retval; @@ -164,6 +168,32 @@ std::unique_ptr CreateTextureFromFile(Draw::DrawContext *draw, c return std::unique_ptr(mtex); } +void ManagedTexture::DeviceLost() { + ILOG("ManagedTexture::DeviceLost(%s)", filename_.c_str()); + if (texture_) + texture_->Release(); + texture_ = nullptr; +} + +void ManagedTexture::DeviceRestored(Draw::DrawContext *draw) { + ILOG("ManagedTexture::DeviceRestored(%s)", filename_.c_str()); + _assert_(!texture_); + draw_ = draw; + // Vulkan: Can't load textures before the first frame has started. + // Should probably try to lift that restriction again someday.. + loadPending_ = true; +} + +Draw::Texture *ManagedTexture::GetTexture() { + if (loadPending_) { + if (!LoadFromFile(filename_, ImageFileType::DETECT, generateMips_)) { + ELOG("ManagedTexture failed: '%s'", filename_.c_str()); + } + loadPending_ = false; + } + return texture_; +} + // TODO: Remove the code duplication between this and LoadFromFileData std::unique_ptr CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType type, bool generateMips) { if (!draw) diff --git a/UI/TextureUtil.h b/UI/TextureUtil.h index c71b1e39ba..40b2c6bd18 100644 --- a/UI/TextureUtil.h +++ b/UI/TextureUtil.h @@ -23,15 +23,20 @@ public: bool LoadFromFile(const std::string &filename, ImageFileType type = ImageFileType::DETECT, bool generateMips = false); bool LoadFromFileData(const uint8_t *data, size_t dataSize, ImageFileType type = ImageFileType::DETECT, bool generateMips = false); - Draw::Texture *GetTexture() { return texture_; } // For immediate use, don't store. + Draw::Texture *GetTexture(); // For immediate use, don't store. int Width() const { return texture_->Width(); } int Height() const { return texture_->Height(); } + void DeviceLost(); + void DeviceRestored(Draw::DrawContext *draw); + private: Draw::Texture *texture_ = nullptr; Draw::DrawContext *draw_; std::string filename_; // Textures that are loaded from files can reload themselves automatically. + bool generateMips_ = false; + bool loadPending_ = false; }; std::unique_ptr CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType fileType, bool generateMips = false); -std::unique_ptr CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType fileType, bool generateMips = false); +std::unique_ptr CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType fileType, bool generateMips = false); \ No newline at end of file diff --git a/ext/native/file/zip_read.cpp b/ext/native/file/zip_read.cpp index 18331630e8..fa3a7c38f6 100644 --- a/ext/native/file/zip_read.cpp +++ b/ext/native/file/zip_read.cpp @@ -366,7 +366,7 @@ uint8_t *VFSReadFile(const char *filename, size_t *size) { } } if (!fileSystemFound) { - ELOG("Missing filesystem for %s", filename); + ELOG("Missing filesystem for '%s'", filename); } // Otherwise, the file was just missing. No need to log. return 0; } diff --git a/ext/native/ui/screen.cpp b/ext/native/ui/screen.cpp index f9cc2cf7eb..9f88c02bb1 100644 --- a/ext/native/ui/screen.cpp +++ b/ext/native/ui/screen.cpp @@ -97,6 +97,16 @@ bool ScreenManager::axis(const AxisInput &axis) { } } +void ScreenManager::deviceLost() { + for (auto &iter : stack_) + iter.screen->deviceLost(); +} + +void ScreenManager::deviceRestored() { + for (auto &iter : stack_) + iter.screen->deviceRestored(); +} + void ScreenManager::resized() { std::lock_guard guard(inputLock_); // Have to notify the whole stack, otherwise there will be problems when going back diff --git a/ext/native/ui/screen.h b/ext/native/ui/screen.h index 7d09c23d80..abca7f9769 100644 --- a/ext/native/ui/screen.h +++ b/ext/native/ui/screen.h @@ -58,6 +58,8 @@ public: virtual bool key(const KeyInput &key) { return false; } virtual bool axis(const AxisInput &touch) { return false; } virtual void sendMessage(const char *msg, const char *value) {} + virtual void deviceLost() {} + virtual void deviceRestored() {} virtual void RecreateViews() {} @@ -115,6 +117,9 @@ public: void resized(); void shutdown(); + void deviceLost(); + void deviceRestored(); + // Push a dialog box in front. Currently 1-level only. void push(Screen *screen, int layerFlags = 0); diff --git a/ext/native/ui/ui_screen.cpp b/ext/native/ui/ui_screen.cpp index d4f5f0f41d..e6830635f3 100644 --- a/ext/native/ui/ui_screen.cpp +++ b/ext/native/ui/ui_screen.cpp @@ -61,6 +61,16 @@ void UIScreen::update() { } } +void UIScreen::deviceLost() { + if (root_) + root_->DeviceLost(); +} + +void UIScreen::deviceRestored() { + if (root_) + root_->DeviceRestored(screenManager()->getDrawContext()); +} + void UIScreen::preRender() { using namespace Draw; Draw::DrawContext *draw = screenManager()->getDrawContext(); diff --git a/ext/native/ui/ui_screen.h b/ext/native/ui/ui_screen.h index c18de75381..f2fe7b157f 100644 --- a/ext/native/ui/ui_screen.h +++ b/ext/native/ui/ui_screen.h @@ -15,16 +15,18 @@ public: UIScreen(); ~UIScreen(); - virtual void update() override; - virtual void preRender() override; - virtual void render() override; - virtual void postRender() override; + void update() override; + void preRender() override; + void render() override; + void postRender() override; + void deviceLost() override; + void deviceRestored() override; - virtual bool touch(const TouchInput &touch) override; - virtual bool key(const KeyInput &touch) override; - virtual bool axis(const AxisInput &touch) override; + bool touch(const TouchInput &touch) override; + bool key(const KeyInput &touch) override; + bool axis(const AxisInput &touch) override; - virtual TouchInput transformTouch(const TouchInput &touch) override; + TouchInput transformTouch(const TouchInput &touch) override; virtual void TriggerFinish(DialogResult result); diff --git a/ext/native/ui/view.h b/ext/native/ui/view.h index 06da7b98c6..05e3100ab0 100644 --- a/ext/native/ui/view.h +++ b/ext/native/ui/view.h @@ -33,6 +33,7 @@ class Texture; class UIContext; namespace Draw { + class DrawContext; class Texture; } @@ -365,6 +366,9 @@ public: virtual void Axis(const AxisInput &input) {} virtual void Update(); + virtual void DeviceLost() {} + virtual void DeviceRestored(Draw::DrawContext *draw) {} + // If this view covers these coordinates, it should add itself and its children to the list. virtual void Query(float x, float y, std::vector &list); virtual std::string Describe() const; diff --git a/ext/native/ui/viewgroup.cpp b/ext/native/ui/viewgroup.cpp index ab748a9772..244f852fd6 100644 --- a/ext/native/ui/viewgroup.cpp +++ b/ext/native/ui/viewgroup.cpp @@ -117,6 +117,20 @@ void ViewGroup::Axis(const AxisInput &input) { } } +void ViewGroup::DeviceLost() { + std::lock_guard guard(modifyLock_); + for (auto iter = views_.begin(); iter != views_.end(); ++iter) { + (*iter)->DeviceLost(); + } +} + +void ViewGroup::DeviceRestored(Draw::DrawContext *draw) { + std::lock_guard guard(modifyLock_); + for (auto iter = views_.begin(); iter != views_.end(); ++iter) { + (*iter)->DeviceRestored(draw); + } +} + void ViewGroup::Draw(UIContext &dc) { if (hasDropShadow_) { // Darken things behind. @@ -124,7 +138,7 @@ void ViewGroup::Draw(UIContext &dc) { float dropsize = 30.0f; dc.Draw()->DrawImage4Grid(dc.theme->dropShadow4Grid, bounds_.x - dropsize, bounds_.y, - bounds_.x2() + dropsize, bounds_.y2()+dropsize*1.5, 0xDF000000, 3.0f); + bounds_.x2() + dropsize, bounds_.y2()+dropsize*1.5f, 0xDF000000, 3.0f); } if (clip_) { diff --git a/ext/native/ui/viewgroup.h b/ext/native/ui/viewgroup.h index 6ede1fb9d2..fb693c33a7 100644 --- a/ext/native/ui/viewgroup.h +++ b/ext/native/ui/viewgroup.h @@ -38,6 +38,9 @@ public: virtual void Update() override; virtual void Query(float x, float y, std::vector &list) override; + virtual void DeviceLost() override; + virtual void DeviceRestored(Draw::DrawContext *draw) override; + virtual void Draw(UIContext &dc) override; // These should be unused.