Enable crash mitigation when running OpenGL in the Android emulator (#2858)

This commit is contained in:
mwilsnd
2024-09-20 15:47:29 -04:00
committed by GitHub
parent e2df2d9061
commit 58dd529790
8 changed files with 64 additions and 10 deletions

View File

@@ -113,6 +113,10 @@ public:
void reduceMemoryUse();
void clearData();
#if MLN_RENDER_BACKEND_OPENGL
void enableAndroidEmulatorGoldfishMitigation(bool enable);
#endif
private:
class Impl;
std::unique_ptr<Impl> impl;

View File

@@ -13,6 +13,27 @@
#include "android_renderer_backend.hpp"
#include "map_renderer_runnable.hpp"
#if MLN_RENDER_BACKEND_OPENGL
#include <sys/system_properties.h>
namespace {
std::string androidSysProp(const char* key) {
assert(strlen(key) < PROP_NAME_MAX);
if (__system_property_find(key) == nullptr) {
return "";
}
char prop[PROP_VALUE_MAX + 1];
__system_property_get(key, prop);
return prop;
}
bool inEmulator() {
return androidSysProp("ro.kernel.qemu") == "1" || androidSysProp("ro.boot.qemu") == "1" ||
androidSysProp("ro.hardware.egl") == "emulation";
}
} // namespace
#endif
namespace mbgl {
namespace android {
@@ -232,6 +253,14 @@ void MapRenderer::onSurfaceCreated(JNIEnv& env, const jni::Object<AndroidSurface
renderer = std::make_unique<Renderer>(backend->getImpl(), pixelRatio, localIdeographFontFamily);
rendererRef = std::make_unique<ActorRef<Renderer>>(*renderer, mailboxData.getMailbox());
#if MLN_RENDER_BACKEND_OPENGL
// If we're running the emulator with the OpenGL backend, we're going to crash eventually,
// unless we enable this mitigation.
if (inEmulator()) {
renderer->enableAndroidEmulatorGoldfishMitigation(true);
}
#endif
backend->setSwapBehavior(swapBehaviorFlush ? gfx::Renderable::SwapBehaviour::Flush
: gfx::Renderable::SwapBehaviour::NoFlush);

View File

@@ -33,6 +33,10 @@ public:
const std::shared_ptr<UpdateParameters>&,
const RenderTree&,
UniqueChangeRequestVec&) override;
/// Remove all the drawables for tiles
/// @return The number of drawables actually removed.
std::size_t removeAllDrawables() override;
#endif
private:
@@ -63,10 +67,6 @@ private:
/// Remove all drawables for the tile from the layer group
/// @return The number of drawables actually removed.
std::size_t removeTile(RenderPass, const OverscaledTileID&) override;
/// Remove all the drawables for tiles
/// @return The number of drawables actually removed.
std::size_t removeAllDrawables() override;
#endif
// Paint properties

View File

@@ -98,6 +98,9 @@ public:
const std::shared_ptr<UpdateParameters>&,
const RenderTree&,
UniqueChangeRequestVec&) override;
/// Remove all the drawables for tiles
std::size_t removeAllDrawables() override;
#endif // MLN_DRAWABLE_RENDERER
protected:
@@ -119,9 +122,6 @@ protected:
/// Remove all drawables for the tile from the layer group
std::size_t removeTile(RenderPass, const OverscaledTileID&) override;
/// Remove all the drawables for tiles
std::size_t removeAllDrawables() override;
#endif // MLN_DRAWABLE_RENDERER
private:

View File

@@ -191,6 +191,9 @@ public:
/// Returns the current renderability mode of the layer
bool isLayerRenderable() const noexcept { return isRenderable; }
/// Remove all the drawables for tiles
virtual std::size_t removeAllDrawables();
#endif
using Dependency = style::expression::Dependency;
@@ -247,9 +250,6 @@ protected:
/// @return The number of drawables actually removed.
virtual std::size_t removeTile(RenderPass, const OverscaledTileID&);
/// Remove all the drawables for tiles
virtual std::size_t removeAllDrawables();
/// Update `renderTileIDs` from `renderTiles`
void updateRenderTileIDs();

View File

@@ -993,6 +993,13 @@ void RenderOrchestrator::updateLayers(gfx::ShaderRegistry& shaders,
for (const auto& item : items) {
auto& renderLayer = item.layer.get();
#if MLN_RENDER_BACKEND_OPENGL
// Android Emulator: Goldfish is *very* broken. This will prevent a crash
// inside the GL translation layer at the cost of emulator performance.
if (androidGoldfishMitigationEnabled) {
renderLayer.removeAllDrawables();
}
#endif
renderLayer.update(shaders, context, state, updateParameters, renderTree, changes);
}
addChanges(changes);

View File

@@ -61,6 +61,10 @@ public:
const std::optional<std::string>& localFontFamily_);
~RenderOrchestrator() override;
#if MLN_RENDER_BACKEND_OPENGL
void enableAndroidEmulatorGoldfishMitigation(bool enable) { androidGoldfishMitigationEnabled = enable; }
#endif
void markContextLost() { contextLost = true; };
// TODO: Introduce RenderOrchestratorObserver.
void setObserver(RendererObserver*);
@@ -212,6 +216,10 @@ private:
bool placedSymbolDataCollected = false;
bool tileCacheEnabled = true;
#if MLN_RENDER_BACKEND_OPENGL
bool androidGoldfishMitigationEnabled{false};
#endif
// Vectors with reserved capacity of layerImpls->size() to avoid
// reallocation on each frame.
std::vector<Immutable<style::LayerProperties>> filteredLayersForSource;

View File

@@ -152,4 +152,10 @@ void Renderer::clearData() {
impl->orchestrator.clearData();
}
#if MLN_RENDER_BACKEND_OPENGL
void Renderer::enableAndroidEmulatorGoldfishMitigation(bool enable) {
impl->orchestrator.enableAndroidEmulatorGoldfishMitigation(enable);
}
#endif
} // namespace mbgl