save and restore layers image when context is lost in Android
This commit is contained in:
@@ -362,15 +362,112 @@ bool ui::Canvas::create(int width, int height)
|
||||
m_plane.create<1>(1, 1);
|
||||
m_plane_brush.create<1>(1, 1);
|
||||
m_mesh.create();
|
||||
for (auto& l : m_layers)
|
||||
{
|
||||
l.create(width, height, "");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ui::Canvas::snapshot_save()
|
||||
void ui::Canvas::snapshot_save(std::string data_path)
|
||||
{
|
||||
|
||||
LOG("SAVE SNAPSHOT");
|
||||
m_layers_snapshot.clear();
|
||||
m_layers_snapshot.resize(m_layers.size());
|
||||
for (int i = 0; i < m_layers.size(); i++)
|
||||
m_layers_snapshot[i] = m_layers[i].snapshot(data_path);
|
||||
}
|
||||
|
||||
void ui::Canvas::snapshot_restore()
|
||||
{
|
||||
LOG("RESTORE SNAPSHOT");
|
||||
for (int i = 0; i < m_layers.size(); i++)
|
||||
m_layers[i].restore(m_layers_snapshot[i]);
|
||||
m_layers_snapshot.clear();
|
||||
LOG("RESTORE SNAPSHOT complete");
|
||||
}
|
||||
|
||||
};
|
||||
void ui::Canvas::clear_context()
|
||||
{
|
||||
LOG("Canvas CLEAR CONTEXT");
|
||||
for (auto& layer : m_layers)
|
||||
layer.destroy();
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
m_tmp[i].destroy();
|
||||
m_tex[i].destroy();
|
||||
m_tex2[i].destroy();
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void ui::Layer::destroy()
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
m_rtt[i].destroy();
|
||||
}
|
||||
|
||||
void ui::Layer::restore(const Snapshot& snap)
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (!snap.image[i])
|
||||
continue;
|
||||
m_rtt[i].create(512, 512); // TODO: this should not be recreated here! Sorry I messed up with this, just quick fix DON'T SHIP!!
|
||||
m_rtt[i].bindTexture();
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_rtt[i].getWidth(), m_rtt[i].getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, snap.image[i].get());
|
||||
m_rtt[i].unbindTexture();
|
||||
LOG("restore face %d - %d bytes (%dx%d)", i, m_rtt[i].bytes(), m_rtt[i].getWidth(), m_rtt[i].getHeight());
|
||||
}
|
||||
}
|
||||
|
||||
ui::Layer::Snapshot ui::Layer::snapshot(std::string data_path)
|
||||
{
|
||||
Snapshot snap;
|
||||
static int counter = 0;
|
||||
LOG("errno = %d", errno);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
snap.image[i] = std::make_unique<uint8_t[]>(m_rtt[i].bytes());
|
||||
m_rtt[i].readTextureData(snap.image[i].get());
|
||||
LOG("snapshot face %d - %d bytes (%dx%d)", i, m_rtt[i].bytes(), m_rtt[i].getWidth(), m_rtt[i].getHeight());
|
||||
static char name[128];
|
||||
sprintf(name, "%s/Layer%d-%d.png", data_path.c_str(), counter, i);
|
||||
//int ret = stbi_write_png(name, m_rtt[i].getWidth(), m_rtt[i].getHeight(), 4, snap.image[i].get(), m_rtt[i].stride());
|
||||
}
|
||||
counter++;
|
||||
return std::move(snap);
|
||||
}
|
||||
|
||||
void ui::Layer::clear(const glm::vec4& c)
|
||||
{
|
||||
// push clear color state
|
||||
GLfloat cc[4];
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
|
||||
glClearColor(c.r, c.g, c.b, c.a);
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
m_rtt[i].bindFramebuffer();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
m_rtt[i].unbindFramebuffer();
|
||||
}
|
||||
|
||||
// restore clear color state
|
||||
glClearColor(cc[0], cc[1], cc[2], cc[3]);
|
||||
}
|
||||
|
||||
bool ui::Layer::create(int width, int height, std::string name)
|
||||
{
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
m_rtt[i].create(width, height);
|
||||
m_rtt[i].bindFramebuffer();
|
||||
m_rtt[i].clear();
|
||||
m_rtt[i].unbindFramebuffer();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user