Skip to content

Commit

Permalink
Gl context loss (#1234)
Browse files Browse the repository at this point in the history
* Remove generation tracking code

Rebuild valid framebuffer selection on context loss

* Request GLSurfaceView to preserve EGL context on pause
  • Loading branch information
karimnaaji authored Jan 26, 2017
1 parent 78770dc commit c35ba6a
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 141 deletions.
2 changes: 0 additions & 2 deletions android/tangram/src/main/cpp/platform_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ std::vector<FontSourceHandle> systemFontFallbacksHandle() {
std::string fallbackPath = fontFallbackPath(importance, weightHint);

while (!fallbackPath.empty()) {
LOG("Loading font %s", fallbackPath.c_str());

handles.emplace_back(fallbackPath);

fallbackPath = fontFallbackPath(importance++, weightHint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ protected MapController(GLSurfaceView view) {
mapView = view;
view.setRenderer(this);
view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
view.setPreserveEGLContextOnPause(true);

// Set a default HTTPHandler
httpHandler = new HttpHandler();
Expand Down Expand Up @@ -251,7 +252,6 @@ public void loadSceneFile(String path) {
* @param sceneUpdates List of {@code SceneUpdate}
*/
public void loadSceneFile(String path, List<SceneUpdate> sceneUpdates) {

String[] updateStrings = bundleSceneUpdates(sceneUpdates);
scenePath = path;
checkPointer(mapPointer);
Expand Down
2 changes: 0 additions & 2 deletions core/src/gl/dynamicQuadMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ void DynamicQuadMesh<T>::upload(RenderState& rs) {

if (m_nVertices == 0 || m_isUploaded) { return; }

MeshBase::checkValidity(rs);

// Generate vertex buffer, if needed
if (m_glVertexBuffer == 0) {
GL::genBuffers(1, &m_glVertexBuffer);
Expand Down
10 changes: 2 additions & 8 deletions core/src/gl/framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Tangram {

FrameBuffer::FrameBuffer(int _width, int _height, bool _colorRenderBuffer) :
m_glFrameBufferHandle(0),
m_generation(-1),
m_valid(false),
m_colorRenderBuffer(_colorRenderBuffer),
m_width(_width), m_height(_height) {
Expand Down Expand Up @@ -145,22 +144,17 @@ void FrameBuffer::init(RenderState& _rs) {
m_valid = true;
}

m_generation = _rs.generation();

m_disposer = Disposer(_rs);
}

FrameBuffer::~FrameBuffer() {

int generation = m_generation;
GLuint glHandle = m_glFrameBufferHandle;

m_disposer([=](RenderState& rs) {
if (rs.isValidGeneration(generation)) {
rs.framebufferUnset(glHandle);
rs.framebufferUnset(glHandle);

GL::deleteFramebuffers(1, &glHandle);
}
GL::deleteFramebuffers(1, &glHandle);
});
}

Expand Down
2 changes: 0 additions & 2 deletions core/src/gl/framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class FrameBuffer {

GLuint m_glColorRenderBufferHandle;

int m_generation;

bool m_valid;

bool m_colorRenderBuffer;
Expand Down
39 changes: 8 additions & 31 deletions core/src/gl/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ MeshBase::MeshBase() {
m_dirty = false;
m_isUploaded = false;
m_isCompiled = false;

m_generation = -1;
}

MeshBase::MeshBase(std::shared_ptr<VertexLayout> _vertexLayout, GLenum _drawMode, GLenum _hint)
Expand All @@ -38,24 +36,21 @@ MeshBase::MeshBase(std::shared_ptr<VertexLayout> _vertexLayout, GLenum _drawMode
MeshBase::~MeshBase() {

auto vaos = m_vaos;
auto generation = m_generation;
auto glVertexBuffer = m_glVertexBuffer;
auto glIndexBuffer = m_glIndexBuffer;

m_disposer([=](RenderState& rs) mutable {
// Deleting a index/array buffer being used ends up setting up the current vertex/index buffer to 0
// after the driver finishes using it, force the render state to be 0 for vertex/index buffer
if (rs.isValidGeneration(generation)) {
if (glVertexBuffer) {
rs.vertexBufferUnset(glVertexBuffer);
GL::deleteBuffers(1, &glVertexBuffer);
}
if (glIndexBuffer) {
rs.indexBufferUnset(glIndexBuffer);
GL::deleteBuffers(1, &glIndexBuffer);
}
vaos.dispose();
if (glVertexBuffer) {
rs.vertexBufferUnset(glVertexBuffer);
GL::deleteBuffers(1, &glVertexBuffer);
}
if (glIndexBuffer) {
rs.indexBufferUnset(glIndexBuffer);
GL::deleteBuffers(1, &glIndexBuffer);
}
vaos.dispose();
});


Expand Down Expand Up @@ -156,7 +151,6 @@ void MeshBase::upload(RenderState& rs) {
m_glIndexData = nullptr;
}

m_generation = rs.generation();
m_disposer = Disposer(rs);

m_isUploaded = true;
Expand All @@ -165,8 +159,6 @@ void MeshBase::upload(RenderState& rs) {
bool MeshBase::draw(RenderState& rs, ShaderProgram& _shader, bool _useVao) {
bool useVao = _useVao && Hardware::supportsVAOs;

checkValidity(rs);

if (!m_isCompiled) { return false; }
if (m_nVertices == 0) { return false; }

Expand Down Expand Up @@ -232,21 +224,6 @@ bool MeshBase::draw(RenderState& rs, ShaderProgram& _shader, bool _useVao) {
return true;
}

bool MeshBase::checkValidity(RenderState& rs) {
if (!rs.isValidGeneration(m_generation)) {
m_isUploaded = false;
m_glVertexBuffer = 0;
m_glIndexBuffer = 0;
m_vaos = {};

m_generation = rs.generation();

return false;
}

return true;
}

size_t MeshBase::bufferSize() const {
return m_nVertices * m_vertexLayout->getStride() + m_nIndices * sizeof(GLushort);
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/gl/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ struct MeshBase {

protected:

int m_generation; // Generation in which this mesh's GL handles were created

// Used in draw for legth and offsets: sumIndices, sumVertices
// needs to be set by compile()
std::vector<std::pair<uint32_t, uint32_t>> m_vertexOffsets;
Expand Down Expand Up @@ -109,8 +107,6 @@ struct MeshBase {

Disposer m_disposer;

bool checkValidity(RenderState& rs);

size_t compileIndices(const std::vector<std::pair<uint32_t, uint32_t>>& _offsets,
const std::vector<uint16_t>& _indices, size_t _offset);

Expand Down
13 changes: 0 additions & 13 deletions core/src/gl/renderState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,6 @@ void RenderState::cacheDefaultFramebuffer() {
GL::getIntegerv(GL_FRAMEBUFFER_BINDING, &m_defaultFramebuffer);
}

void RenderState::increaseGeneration() {
generateQuadIndexBuffer();
m_validGeneration++;
}

bool RenderState::isValidGeneration(int _generation) {
return _generation == m_validGeneration;
}

int RenderState::generation() {
return m_validGeneration;
}

int RenderState::nextAvailableTextureUnit() {
if (m_nextTextureUnit >= Hardware::maxCombinedTextureUnits) {
LOGE("Too many combined texture units are being used");
Expand Down
7 changes: 0 additions & 7 deletions core/src/gl/renderState.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ class RenderState {
// Reset the render states.
void invalidate();

int generation();

void increaseGeneration();

bool isValidGeneration(int _generation);

// Get the texture slot from a texture unit from 0 to TANGRAM_MAX_TEXTURE_UNIT-1.
static GLuint getTextureUnit(GLuint _unit);

Expand Down Expand Up @@ -119,7 +113,6 @@ class RenderState {

private:

int m_validGeneration = 0;
uint32_t m_nextTextureUnit = 0;

GLuint m_quadIndexBuffer = 0;
Expand Down
32 changes: 5 additions & 27 deletions core/src/gl/shaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ ShaderProgram::ShaderProgram() {

ShaderProgram::~ShaderProgram() {

auto generation = m_generation;
auto glProgram = m_glProgram;

m_disposer([=](RenderState& rs) {
if (rs.isValidGeneration(generation)) {
if (glProgram != 0) {
GL::deleteProgram(glProgram);
}
if (glProgram != 0) {
GL::deleteProgram(glProgram);
}
// Deleting the shader program that is currently in-use sets the current shader program to 0
// so we un-set the current program in the render state.
Expand Down Expand Up @@ -89,39 +86,29 @@ GLint ShaderProgram::getAttribLocation(const std::string& _attribName) {

GLint ShaderProgram::getUniformLocation(const UniformLocation& _uniform) {

if (m_generation == _uniform.generation) {
return _uniform.location;
}

_uniform.generation = m_generation;
_uniform.location = GL::getUniformLocation(m_glProgram, _uniform.name.c_str());

return _uniform.location;
}

bool ShaderProgram::use(RenderState& rs) {
bool valid = true;

checkValidity(rs);

if (m_needsBuild) {
build(rs);
}

valid &= (m_glProgram != 0);

if (valid) {
if (isValid()) {
rs.shaderProgram(m_glProgram);
return true;
}

return valid;
return false;
}

bool ShaderProgram::build(RenderState& rs) {

if (!m_needsBuild) { return false; }
m_needsBuild = false;
m_generation = rs.generation();

// Delete handle for old program; values of 0 are silently ignored
GL::deleteProgram(m_glProgram);
Expand Down Expand Up @@ -316,15 +303,6 @@ std::string ShaderProgram::applySourceBlocks(const std::string& source, bool fra
return sourceOut.str();
}

void ShaderProgram::checkValidity(RenderState& rs) {

if (!rs.isValidGeneration(m_generation)) {
m_glProgram = 0;
m_needsBuild = true;
m_uniformCache.clear();
}
}

std::string ShaderProgram::getExtensionDeclaration(const std::string& _extension) {
std::ostringstream oss;
oss << "#if defined(GL_ES) == 0 || defined(GL_" << _extension << ")\n";
Expand Down
5 changes: 1 addition & 4 deletions core/src/gl/shaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ShaderProgram {
GLint getUniformLocation(const UniformLocation& _uniformName);

// Return true if this object represents a valid OpenGL shader program.
bool isValid(RenderState& rs) const { return m_glProgram != 0; };
bool isValid() const { return m_glProgram != 0; };

// Bind the program in OpenGL if it is not already bound; If the shader sources
// have been modified since the last time build() was called, also calls build().
Expand Down Expand Up @@ -123,7 +123,6 @@ class ShaderProgram {
return false;
}

int m_generation = -1;
GLuint m_glProgram = 0;

fastmap<std::string, GLint> m_attribMap;
Expand All @@ -141,8 +140,6 @@ class ShaderProgram {

Disposer m_disposer;

void checkValidity(RenderState& rs);

std::string applySourceBlocks(const std::string& source, bool fragShader) const;

};
Expand Down
Loading

0 comments on commit c35ba6a

Please sign in to comment.