Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Add both a lost and restore phase #8968

Merged
merged 4 commits into from
Sep 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Core/Dialog/PSPSaveDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ void PSPSaveDialog::ExecuteNotVisibleIOAction() {
{
bool result = param.GetSize(param.GetPspParam());
// TODO: According to JPCSP, should test/verify this part but seems edge casey.
if (MemoryStick_State() != PSP_MEMORYSTICK_STATE_DRIVER_READY) {
if (MemoryStick_State() != PSP_MEMORYSTICK_STATE_INSERTED) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just saw the warning when compiling so snuck it in.

-[Unknown]

param.GetPspParam()->common.result = SCE_UTILITY_SAVEDATA_ERROR_RW_NO_MEMSTICK;
} else if (result) {
param.GetPspParam()->common.result = 0;
Expand Down
4 changes: 4 additions & 0 deletions GPU/Directx9/GPU_DX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ void GPU_DX9::DeviceLost() {
framebufferManager_.DeviceLost();
}

void GPU_DX9::DeviceRestore() {
// Nothing needed.
}

void GPU_DX9::InitClear() {
ScheduleEvent(GPU_EVENT_INIT_CLEAR);
}
Expand Down
1 change: 1 addition & 0 deletions GPU/Directx9/GPU_DX9.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class GPU_DX9 : public GPUCommon {
bool PerformStencilUpload(u32 dest, int size) override;
void ClearCacheNextFrame() override;
void DeviceLost() override; // Only happens on Android. Drop all textures and shaders.
void DeviceRestore() override;

void DumpNextFrame() override;
void DoState(PointerWrap &p) override;
Expand Down
8 changes: 6 additions & 2 deletions GPU/GLES/DrawEngineGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,18 @@ void DrawEngineGLES::DestroyDeviceObjects() {
}
}

void DrawEngineGLES::GLRestore() {
ILOG("TransformDrawEngine::GLRestore()");
void DrawEngineGLES::GLLost() {
ILOG("TransformDrawEngine::GLLost()");
// The objects have already been deleted.
bufferNameCache_.clear();
bufferNameInfo_.clear();
freeSizedBuffers_.clear();
bufferNameCacheSize_ = 0;
ClearTrackedVertexArrays();
}

void DrawEngineGLES::GLRestore() {
ILOG("TransformDrawEngine::GLRestore()");
InitDeviceObjects();
}

Expand Down
1 change: 1 addition & 0 deletions GPU/GLES/DrawEngineGLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class DrawEngineGLES : public DrawEngineCommon, public GfxResourceHolder {
void RestoreVAO();
void InitDeviceObjects();
void DestroyDeviceObjects();
void GLLost() override;
void GLRestore() override;
void Resized();

Expand Down
4 changes: 4 additions & 0 deletions GPU/GLES/GPU_GLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ void GPU_GLES::DeviceLost() {
fragmentTestCache_.Clear(false);
depalShaderCache_.Clear();
framebufferManager_.DeviceLost();
}

void GPU_GLES::DeviceRestore() {
ILOG("GPU_GLES: DeviceRestore");

UpdateVsyncInterval(true);
}
Expand Down
1 change: 1 addition & 0 deletions GPU/GLES/GPU_GLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class GPU_GLES : public GPUCommon {
bool PerformStencilUpload(u32 dest, int size) override;
void ClearCacheNextFrame() override;
void DeviceLost() override; // Only happens on Android. Drop all textures and shaders.
void DeviceRestore() override;

void DumpNextFrame() override;
void DoState(PointerWrap &p) override;
Expand Down
1 change: 1 addition & 0 deletions GPU/GPUInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ class GPUInterface {
virtual void EnableInterrupts(bool enable) = 0;

virtual void DeviceLost() = 0;
virtual void DeviceRestore() = 0;
virtual void ReapplyGfxState() = 0;
virtual void SyncThread(bool force = false) = 0;
virtual void SyncBeginFrame() = 0;
Expand Down
1 change: 1 addition & 0 deletions GPU/Null/NullGpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class NullGPU : public GPUCommon {
void ClearCacheNextFrame() override {}

void DeviceLost() override {}
void DeviceRestore() override {}
void DumpNextFrame() override {}

void Resized() override {}
Expand Down
4 changes: 4 additions & 0 deletions GPU/Software/SoftGpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ void SoftGPU::DeviceLost() {
// Handled by thin3d.
}

void SoftGPU::DeviceRestore() {
// Handled by thin3d.
}

SoftGPU::~SoftGPU() {
vformat->Release();
vformat = nullptr;
Expand Down
1 change: 1 addition & 0 deletions GPU/Software/SoftGpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class SoftGPU : public GPUCommon {
void ClearCacheNextFrame() override {}

void DeviceLost() override;
void DeviceRestore() override;
void DumpNextFrame() override {}

void Resized() override {}
Expand Down
4 changes: 4 additions & 0 deletions GPU/Vulkan/GPU_Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,10 @@ void GPU_Vulkan::DeviceLost() {
// TODO
}

void GPU_Vulkan::DeviceRestore() {
// TODO
}

void GPU_Vulkan::GetStats(char *buffer, size_t bufsize) {
const DrawEngineVulkanStats &drawStats = drawEngine_.GetStats();
float vertexAverageCycles = gpuStats.numVertsSubmitted > 0 ? (float)gpuStats.vertexGPUCycles / (float)gpuStats.numVertsSubmitted : 0.0f;
Expand Down
1 change: 1 addition & 0 deletions GPU/Vulkan/GPU_Vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class GPU_Vulkan : public GPUCommon {
bool PerformStencilUpload(u32 dest, int size) override;
void ClearCacheNextFrame() override;
void DeviceLost() override; // Only happens on Android. Drop all textures and shaders.
void DeviceRestore() override;

void DumpNextFrame() override;
void DoState(PointerWrap &p) override;
Expand Down
6 changes: 6 additions & 0 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,12 @@ void EmuScreen::deviceLost() {
ILOG("EmuScreen::deviceLost()");
if (gpu)
gpu->DeviceLost();
}

void EmuScreen::deviceRestore() {
ILOG("EmuScreen::deviceRestore()");
if (gpu)
gpu->DeviceRestore();

RecreateViews();
}
Expand Down
1 change: 1 addition & 0 deletions UI/EmuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class EmuScreen : public UIScreen {
void update(InputState &input) override;
void render() override;
void deviceLost() override;
void deviceRestore() override;
void dialogFinished(const Screen *dialog, DialogResult result) override;
void sendMessage(const char *msg, const char *value) override;

Expand Down
2 changes: 1 addition & 1 deletion UI/GameInfoCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ class GameInfoWorkItem : public PrioritizedWorkQueueItem {
// ILOG("Completed writing info for %s", info_->GetTitle().c_str());
}

virtual float priority() {
float priority() override {
return info_->lastAccessedTime;
}

Expand Down
7 changes: 6 additions & 1 deletion UI/NativeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,11 +808,16 @@ void NativeUpdate(InputState &input) {
screenManager->update(input);
}

void NativeDeviceRestore() {
void NativeDeviceLost() {
if (g_gameInfoCache)
g_gameInfoCache->Clear();
screenManager->deviceLost();
gl_lost();
}

void NativeDeviceRestore() {
NativeDeviceLost();
screenManager->deviceRestore();
if (GetGPUBackend() == GPUBackend::OPENGL) {
gl_restore();
}
Expand Down
1 change: 1 addition & 0 deletions android/jni/app-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeRenderer_displayRender(JNIEnv *env,
extern "C" void Java_org_ppsspp_ppsspp_NativeRenderer_displayShutdown(JNIEnv *env, jobject obj) {
ILOG("NativeApp.displayShutdown()");
if (renderer_inited) {
NativeDeviceLost();
NativeShutdownGraphics();
renderer_inited = false;
NativeMessageReceived("recreateviews", "");
Expand Down
10 changes: 5 additions & 5 deletions android/src/org/ppsspp/ppsspp/NativeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,11 @@ protected void onStop() {
Log.i(TAG, "onStop - do nothing special");
}

@Override
@Override
protected void onDestroy() {
super.onDestroy();
if (javaGL) {
Log.i(TAG, "onDestroy");
if (javaGL) {
Log.i(TAG, "onDestroy");
mGLSurfaceView.onDestroy();
nativeRenderer.onDestroyed();
NativeApp.audioShutdown();
Expand All @@ -597,8 +597,8 @@ protected void onDestroy() {
audioFocusChangeListener = null;
audioManager = null;
unregisterCallbacks();
}
if (shuttingDown) {
}
if (shuttingDown || isFinishing()) {
NativeApp.shutdown();
}
}
Expand Down
18 changes: 17 additions & 1 deletion ext/native/gfx/gl_lost_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void gl_restore() {
return;
}

// TODO: We should really do this when we get the context back, not during gl_lost...
ILOG("gl_restore() restoring %i items:", (int)holders->size());
for (size_t i = 0; i < holders->size(); i++) {
ILOG("gl_restore(%i / %i, %p, %08x)", (int)(i + 1), (int)holders->size(), (*holders)[i], *((uint32_t *)((*holders)[i])));
Expand All @@ -57,6 +56,23 @@ void gl_restore() {
inRestore = false;
}

void gl_lost() {
inLost = true;
if (!holders) {
WLOG("GL resource holder not initialized, cannot process restore request");
inLost = false;
return;
}

ILOG("gl_lost() clearing %i items:", (int)holders->size());
for (size_t i = 0; i < holders->size(); i++) {
ILOG("gl_lost(%i / %i, %p, %08x)", (int)(i + 1), (int)holders->size(), (*holders)[i], *((uint32_t *)((*holders)[i])));
(*holders)[i]->GLLost();
}
ILOG("gl_lost() completed on %i items:", (int)holders->size());
inLost = false;
}

void gl_lost_manager_init() {
if (holders) {
FLOG("Double GL lost manager init");
Expand Down
4 changes: 4 additions & 0 deletions ext/native/gfx/gl_lost_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class GfxResourceHolder {
public:
virtual ~GfxResourceHolder() {}
virtual void GLRestore() = 0;
virtual void GLLost() = 0;
};

void gl_lost_manager_init();
Expand All @@ -15,5 +16,8 @@ void gl_lost_manager_shutdown();
void register_gl_resource_holder(GfxResourceHolder *holder);
void unregister_gl_resource_holder(GfxResourceHolder *holder);

// Notifies all objects it's time to forget / delete things.
void gl_lost();

// Notifies all objects that it's time to be restored.
void gl_restore();
11 changes: 7 additions & 4 deletions ext/native/gfx_es2/glsl_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,20 @@ bool glsl_recompile(GLSLProgram *program, std::string *error_message) {
return true;
}

void GLSLProgram::GLRestore() {
void GLSLProgram::GLLost() {
// Quoth http://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer.html;
// "Note that when the EGL context is lost, all OpenGL resources associated with that context will be automatically deleted.
// You do not need to call the corresponding "glDelete" methods such as glDeleteTextures to manually delete these lost resources."
// Hence, we comment out:
// glDeleteShader(this->vsh_);
// glDeleteShader(this->fsh_);
// glDeleteProgram(this->program_);
this->program_ = 0;
this->vsh_ = 0;
this->fsh_ = 0;
program_ = 0;
vsh_ = 0;
fsh_ = 0;
}

void GLSLProgram::GLRestore() {
ILOG("Restoring GLSL program %s/%s",
strlen(this->vshader_filename) > 0 ? this->vshader_filename : "(mem)",
strlen(this->fshader_filename) > 0 ? this->fshader_filename : "(mem)");
Expand Down
1 change: 1 addition & 0 deletions ext/native/gfx_es2/glsl_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct GLSLProgram : public GfxResourceHolder {
GLuint fsh_;
GLuint program_;

void GLLost() override;
void GLRestore() override;
};

Expand Down
29 changes: 26 additions & 3 deletions ext/native/thin3d/thin3d_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ class Thin3DGLBuffer : public Thin3DBuffer, GfxResourceHolder {
glBindBuffer(target_, buffer_);
}

void GLLost() override {
buffer_ = 0;
}

void GLRestore() override {
ILOG("Recreating vertex buffer after glLost");
ILOG("Recreating vertex buffer after gl_restore");
knownSize_ = 0; // Will cause a new glBufferData call. Should genBuffers again though?
glGenBuffers(1, &buffer_);
}
Expand All @@ -235,6 +239,10 @@ class Thin3DGLShader : public Thin3DShader {
}
const std::string &GetSource() const { return source_; }

void Unset() {
shader_ = 0;
}

~Thin3DGLShader() {
glDeleteShader(shader_);
}
Expand All @@ -257,7 +265,7 @@ bool Thin3DGLShader::Compile(const char *source) {
source = temp.c_str();
}

glShaderSource(shader_, 1, &source, 0);
glShaderSource(shader_, 1, &source, nullptr);
glCompileShader(shader_);
GLint success = 0;
glGetShaderiv(shader_, GL_COMPILE_STATUS, &success);
Expand All @@ -283,6 +291,7 @@ class Thin3DGLVertexFormat : public Thin3DVertexFormat, GfxResourceHolder {
void Unapply();
void Compile();
void GLRestore() override;
void GLLost() override;
bool RequiresBuffer() override {
return id_ != 0;
}
Expand Down Expand Up @@ -323,6 +332,12 @@ class Thin3DGLShaderSet : public Thin3DShaderSet, GfxResourceHolder {
void SetVector(const char *name, float *value, int n) override;
void SetMatrix4x4(const char *name, const float value[16]) override;

void GLLost() override {
program_ = 0;
vshader->Unset();
fshader->Unset();
}

void GLRestore() override {
vshader->Compile(vshader->GetSource().c_str());
fshader->Compile(fshader->GetSource().c_str());
Expand Down Expand Up @@ -550,15 +565,19 @@ class Thin3DGLTexture : public Thin3DTexture, GfxResourceHolder {
glBindTexture(target_, tex_);
}

void GLRestore() override {
void GLLost() override {
// We can assume that the texture is gone.
tex_ = 0;
generatedMips_ = false;
}

void GLRestore() override {
if (!filename_.empty()) {
if (LoadFromFile(filename_.c_str())) {
ILOG("Reloaded lost texture %s", filename_.c_str());
} else {
ELOG("Failed to reload lost texture %s", filename_.c_str());
tex_ = 0;
}
} else {
WLOG("Texture %p cannot be restored - has no filename", this);
Expand Down Expand Up @@ -663,6 +682,10 @@ void Thin3DGLVertexFormat::Compile() {
lastBase_ = -1;
}

void Thin3DGLVertexFormat::GLLost() {
id_ = 0;
}

void Thin3DGLVertexFormat::GLRestore() {
Compile();
}
Expand Down
8 changes: 8 additions & 0 deletions ext/native/ui/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ void ScreenManager::deviceLost() {
// TODO: Change this when it becomes necessary.
}

void ScreenManager::deviceRestore() {
for (size_t i = 0; i < stack_.size(); i++) {
stack_[i].screen->deviceRestore();
}
// Dialogs too? Nah, they should only use the standard UI texture anyway.
// TODO: Change this when it becomes necessary.
}

Screen *ScreenManager::topScreen() const {
if (!stack_.empty())
return stack_.back().screen;
Expand Down
Loading