Skip to content

Commit

Permalink
Don't use exceptions for expected control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanGriffiths committed Oct 24, 2023
1 parent 1822b55 commit b3e221d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/platforms/virtual/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ configure_file(
)
set(symbol_map ${CMAKE_CURRENT_BINARY_DIR}/symbols.map)

add_compile_definitions(MIR_LOG_COMPONENT="mir::virtual")

add_library(mirplatformgraphicsvirtualobjects OBJECT
graphics.cpp
platform.cpp
Expand Down
32 changes: 14 additions & 18 deletions src/platforms/virtual/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "display.h"
#include "mir/graphics/platform.h"
#include "mir/graphics/egl_error.h"
#include "mir/log.h"
#include "options_parsing_helpers.h"
#include <drm_fourcc.h>

Expand All @@ -41,18 +42,11 @@ class mgv::Platform::VirtualDisplayInterfaceProvider : public mg::DisplayInterfa
class VirtualEGLDisplayProvider : public GenericEGLDisplayProvider
{
public:

explicit VirtualEGLDisplayProvider(EGLDisplay const egl_display) : egl_display{egl_display} {}

auto get_egl_display() -> EGLDisplay override
{
auto const egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (egl_display == EGL_NO_DISPLAY)
{
BOOST_THROW_EXCEPTION((std::runtime_error{"Failed to create EGL display"}));
}
EGLint major, minor;
if (eglInitialize(egl_display, &major, &minor) == EGL_FALSE)
{
BOOST_THROW_EXCEPTION(egl_error("Failed to initialize EGL display"));
}
return egl_display;
}

Expand All @@ -79,6 +73,7 @@ class mgv::Platform::VirtualDisplayInterfaceProvider : public mg::DisplayInterfa
}

private:
EGLDisplay const egl_display;
};

class VirtualCPUAddressableDisplayProvider: public CPUAddressableDisplayProvider
Expand Down Expand Up @@ -130,17 +125,18 @@ class mgv::Platform::VirtualDisplayInterfaceProvider : public mg::DisplayInterfa

if (dynamic_cast<mg::GenericEGLDisplayProvider::Tag const*>(&type_tag))
{
auto egl_display_provider = std::make_shared<VirtualEGLDisplayProvider>();
try
auto const egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (egl_display == EGL_NO_DISPLAY)
{
log_info("Failed to create EGL display");
}
else if (eglInitialize(egl_display, nullptr, nullptr) == EGL_FALSE)
{
// If we cannot get the egl display, we can ignore it in the hopes
// that we can use the CPUAddressableDisplayProvider later on instead.
egl_display_provider->get_egl_display();
return egl_display_provider;
log_debug("Failed to initialise EGL: %s", mg::egl_category().message(eglGetError()).c_str());
}
catch (std::runtime_error const&)
else
{
return nullptr;
return std::make_shared<VirtualEGLDisplayProvider>(egl_display);
}
}

Expand Down
10 changes: 2 additions & 8 deletions tests/unit-tests/platforms/virtual/test_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ TEST_F(VirtualGraphicsPlatformTest, can_acquire_interface_for_generic_egl_displa
ON_CALL(mock_egl, eglGetDisplay(_))
.WillByDefault(Return(fake_display));
ON_CALL(mock_egl, eglInitialize(_,_,_))
.WillByDefault(DoAll(
SetArgPointee<1>(1),
SetArgPointee<2>(4),
Return(EGL_TRUE)));
.WillByDefault(Return(EGL_TRUE));
auto platform = create_platform();
auto interface = mg::DisplayPlatform::interface_for(platform);
EXPECT_TRUE(interface->acquire_interface<mg::GenericEGLDisplayProvider>() != nullptr);
Expand All @@ -94,10 +91,7 @@ TEST_F(VirtualGraphicsPlatformTest, cannot_acquire_interface_for_generic_egl_dis
ON_CALL(mock_egl, eglGetDisplay(_))
.WillByDefault(Return(EGL_NO_DISPLAY));
ON_CALL(mock_egl, eglInitialize(_,_,_))
.WillByDefault(DoAll(
SetArgPointee<1>(1),
SetArgPointee<2>(4),
Return(EGL_TRUE)));
.WillByDefault(Return(EGL_TRUE));
auto platform = create_platform();
auto interface = mg::DisplayPlatform::interface_for(platform);
EXPECT_THAT(interface->acquire_interface<mg::GenericEGLDisplayProvider>(), nullptr);
Expand Down

0 comments on commit b3e221d

Please sign in to comment.