From 6e62df85013dfdb2cbafa34c74015f83fa186d22 Mon Sep 17 00:00:00 2001 From: Adeel Kazmi Date: Wed, 16 Oct 2024 22:21:44 +0100 Subject: [PATCH] Add function & environment option to choose graphics backend Change-Id: If40ae6f65fcc52a4e08f06dfecfd60413ca40421 --- dali/dali.h | 3 +- .../adaptor/common/adaptor-builder-impl.cpp | 18 +++++ .../adaptor/common/application-impl.cpp | 4 ++ .../system/common/environment-options.cpp | 58 ++++++++++++++++ .../system/common/environment-options.h | 9 ++- .../system/common/environment-variables.h | 6 ++ .../adaptor-framework/graphics-backend.cpp | 56 ++++++++++++++++ .../adaptor-framework/graphics-backend.h | 67 +++++++++++++++++++ dali/public-api/file.list | 2 + 9 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 dali/public-api/adaptor-framework/graphics-backend.cpp create mode 100644 dali/public-api/adaptor-framework/graphics-backend.h diff --git a/dali/dali.h b/dali/dali.h index db4687dfa..722cc1f0d 100644 --- a/dali/dali.h +++ b/dali/dali.h @@ -2,7 +2,7 @@ #define DALI_H /* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * Copyright (c) 2024 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ // Application / UI Framework adaption #include #include +#include #include #include #include diff --git a/dali/internal/adaptor/common/adaptor-builder-impl.cpp b/dali/internal/adaptor/common/adaptor-builder-impl.cpp index 1520de45f..7432f104e 100644 --- a/dali/internal/adaptor/common/adaptor-builder-impl.cpp +++ b/dali/internal/adaptor/common/adaptor-builder-impl.cpp @@ -20,6 +20,7 @@ // INTERNAL INCLUDES #include +#include #if defined(VULKAN_ENABLED) #include @@ -51,6 +52,23 @@ void AdaptorBuilder::Finalize() AdaptorBuilder::AdaptorBuilder(EnvironmentOptions& environmentOptions) : mEnvironmentOptions(environmentOptions) { + switch(Graphics::GetCurrentGraphicsBackend()) + { + case Graphics::Backend::GLES: + { + DALI_LOG_RELEASE_INFO("DALi Graphics Backend: GLES\n"); + // TODO: Load GLES library + break; + } + + case Graphics::Backend::VULKAN: + { + DALI_LOG_RELEASE_INFO("DALi Graphics Backend: VULKAN\n"); + // TODO: Attempt to load Vulkan library + break; + } + } + // Construct Graphics Factory mGraphicsFactory = Utils::MakeUnique(environmentOptions); } diff --git a/dali/internal/adaptor/common/application-impl.cpp b/dali/internal/adaptor/common/application-impl.cpp index 41c8f646e..fba4ee115 100644 --- a/dali/internal/adaptor/common/application-impl.cpp +++ b/dali/internal/adaptor/common/application-impl.cpp @@ -42,6 +42,7 @@ #include #include #include +#include // To disable a macro with the same name from one of OpenGL headers #undef Status @@ -363,6 +364,9 @@ void Application::OnInit() { mEnvironmentOptions = std::unique_ptr(new EnvironmentOptions()); + // Call will be ignored if this function has already been called by the application. + Graphics::SetGraphicsBackend(mEnvironmentOptions->GetGraphicsBackend()); + mFramework->AddAbortCallback(MakeCallback(this, &Application::QuitFromMainLoop)); auto& adaptorBuilder = AdaptorBuilder::Get(*mEnvironmentOptions); diff --git a/dali/internal/system/common/environment-options.cpp b/dali/internal/system/common/environment-options.cpp index 21d052097..6782a3050 100644 --- a/dali/internal/system/common/environment-options.cpp +++ b/dali/internal/system/common/environment-options.cpp @@ -21,6 +21,7 @@ // EXTERNAL INCLUDES #include #include +#include #include #include @@ -108,6 +109,55 @@ void SetFromEnvironmentVariable(const char* variable, std::function } } +void SetGraphicsBackendFromEnvironmentVariable(Graphics::Backend& api) +{ + const char* charValue = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_GRAPHICS_BACKEND); + if(charValue) + { + // Expecting upper/lower case variations of GLES and VULKAN/VK, and 0 and 1 too so just check the first character + switch(*charValue) + { + case '0': + case '1': + { + api = static_cast(*charValue - '0'); + break; + } + + case 'g': + case 'G': + { + std::string stringValue(charValue); + if(stringValue.size() == 4) + { + std::transform(stringValue.begin(), stringValue.end(), stringValue.begin(), ::toupper); + if(stringValue == "GLES") + { + api = Graphics::Backend::GLES; + } + } + break; + } + + case 'v': + case 'V': + { + std::string stringValue(charValue); + const auto stringValueSize = stringValue.size(); + if(stringValueSize == 2 || stringValueSize == 6) + { + std::transform(stringValue.begin(), stringValue.end(), stringValue.begin(), ::toupper); + if(stringValue == "VULKAN" || stringValue == "VK") + { + api = Graphics::Backend::VULKAN; + } + } + break; + } + } + } +} + /// Provides a functor which ensures a non-negative number is set for the given member variable struct MinimumZero { @@ -245,6 +295,7 @@ EnvironmentOptions::EnvironmentOptions() mGlesCallTime(0), mMultiSamplingLevel(DEFAULT_MULTI_SAMPLING_LEVEL), mThreadingMode(ThreadingMode::COMBINED_UPDATE_RENDER), + mGraphicsBackend(Graphics::Backend::DEFAULT), mGlesCallAccumulate(false), mDepthBufferRequired(DEFAULT_DEPTH_BUFFER_REQUIRED_SETTING), mStencilBufferRequired(DEFAULT_STENCIL_BUFFER_REQUIRED_SETTING), @@ -481,6 +532,11 @@ ThreadingMode::Type EnvironmentOptions::GetThreadingMode() const return mThreadingMode; } +Graphics::Backend EnvironmentOptions::GetGraphicsBackend() const +{ + return mGraphicsBackend; +} + unsigned int EnvironmentOptions::GetRenderRefreshRate() const { return mRenderRefreshRate; @@ -601,6 +657,8 @@ void EnvironmentOptions::ParseEnvironmentOptions() } }); + SetGraphicsBackendFromEnvironmentVariable(mGraphicsBackend); + SetFromEnvironmentVariable(DALI_REFRESH_RATE, GreaterThan(mRenderRefreshRate, 1)); SetFromEnvironmentVariable(DALI_ENV_MULTI_SAMPLING_LEVEL, mMultiSamplingLevel); diff --git a/dali/internal/system/common/environment-options.h b/dali/internal/system/common/environment-options.h index 61f9607df..cd8f5bf5d 100644 --- a/dali/internal/system/common/environment-options.h +++ b/dali/internal/system/common/environment-options.h @@ -26,6 +26,7 @@ #include #include #include +#include namespace Dali { @@ -294,6 +295,11 @@ public: */ ThreadingMode::Type GetThreadingMode() const; + /** + * @return The graphics backend to use. + */ + Graphics::Backend GetGraphicsBackend() const; + /** * @return The render refresh rate. */ @@ -403,7 +409,8 @@ private: // Data int mGlesCallTime; ///< time in seconds between status updates int mMultiSamplingLevel; ///< The number of samples required in multisample buffers - ThreadingMode::Type mThreadingMode; ///< threading mode + ThreadingMode::Type mThreadingMode; ///< Threading mode + Graphics::Backend mGraphicsBackend; ///< The graphics backend bool mGlesCallAccumulate; ///< Whether or not to accumulate gles call statistics bool mDepthBufferRequired; ///< Whether the depth buffer is required diff --git a/dali/internal/system/common/environment-variables.h b/dali/internal/system/common/environment-variables.h index 30c0102d7..b657601c7 100644 --- a/dali/internal/system/common/environment-variables.h +++ b/dali/internal/system/common/environment-variables.h @@ -113,6 +113,12 @@ namespace Adaptor #define DALI_THREADING_MODE "DALI_THREADING_MODE" +/** + * Specify the Graphics Backend to use. + * Can be "GLES" or "VULKAN", or the appropriate integer value from Graphics::Backend. + */ +#define DALI_GRAPHICS_BACKEND "DALI_GRAPHICS_BACKEND" + #define DALI_REFRESH_RATE "DALI_REFRESH_RATE" #define DALI_WATCH_REFRESH_RATE "DALI_WATCH_REFRESH_RATE" diff --git a/dali/public-api/adaptor-framework/graphics-backend.cpp b/dali/public-api/adaptor-framework/graphics-backend.cpp new file mode 100644 index 000000000..3d8e9f0aa --- /dev/null +++ b/dali/public-api/adaptor-framework/graphics-backend.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// HEADER +#include + +// EXTERNAL INCLUDES +#include + +namespace Dali::Graphics +{ +namespace +{ +Backend gCurrentGraphicsBackend = Backend::DEFAULT; +} + +Backend GetCurrentGraphicsBackend() +{ +// TODO: Remove below once we actually have it dynamic, but for now just use define +// return gCurrentGraphics; +#if VULKAN_ENABLED + return Backend::VULKAN; +#else + return Backend::GLES; +#endif +} + +void SetGraphicsBackend(Backend backend) +{ + static bool setOnce = false; + if(!setOnce) + { + setOnce = true; + gCurrentGraphicsBackend = backend; + } + else if(backend != gCurrentGraphicsBackend) + { + DALI_LOG_ERROR("Graphics backend already set to: %s\n", gCurrentGraphicsBackend == Backend::GLES ? "GLES" : "VULKAN"); + } +} + +} // namespace Dali::Graphics diff --git a/dali/public-api/adaptor-framework/graphics-backend.h b/dali/public-api/adaptor-framework/graphics-backend.h new file mode 100644 index 000000000..6fa346f64 --- /dev/null +++ b/dali/public-api/adaptor-framework/graphics-backend.h @@ -0,0 +1,67 @@ +#ifndef DALI_GRAPHICS_BACKEND_H +#define DALI_GRAPHICS_BACKEND_H + +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// INTERNAL INCLUDES +#include + +namespace Dali +{ +/** + * @addtogroup dali_adaptor_framework + * @{ + */ + +namespace Graphics +{ +enum class Backend +{ + GLES, ///< The graphics backend uses GLES. @SINCE_2_3.53 + VULKAN, ///< The graphics backend uses VULKAN. @SINCE_2_3.53 + + DEFAULT = GLES, ///< The default graphics backend. @SINCE_2_3.53 +}; + +/** + * @brief Returns the graphics backend currently in use. + * @SINCE_2_3.53 + * @return The graphics backend currently in use. + */ +DALI_ADAPTOR_API Backend GetCurrentGraphicsBackend(); + +/** + * @brief Sets the graphics backend. + * @details Generally, calling this is not required and can be set using the environment variable (DALI_GRAPHICS_BACKEND) instead. + * If called before the Application class is created, then the environment variable is ignored. + * @SINCE_2_3.53 + * @param[in] backend The graphics backend to use + * @note This can only be called once and only before the graphics backend has been created (i.e. before the Application Class is started). + * If called again or after the graphics backend has started, then the call will not change anything. + * @note Currently has no effect as the Graphics backend is chosen at compile time + */ +DALI_ADAPTOR_API void SetGraphicsBackend(Backend backend); + +} // namespace Graphics + +/** + * @} + */ +} // namespace Dali + +#endif // DALI_GRAPHICS_BACKEND_H diff --git a/dali/public-api/file.list b/dali/public-api/file.list index 7128e2dc4..cba5dcb55 100644 --- a/dali/public-api/file.list +++ b/dali/public-api/file.list @@ -3,6 +3,7 @@ SET( adaptor_public_api_src_files ${adaptor_public_api_dir}/adaptor-framework/application.cpp ${adaptor_public_api_dir}/adaptor-framework/encoded-image-buffer.cpp + ${adaptor_public_api_dir}/adaptor-framework/graphics-backend.cpp ${adaptor_public_api_dir}/adaptor-framework/key.cpp ${adaptor_public_api_dir}/adaptor-framework/window.cpp ${adaptor_public_api_dir}/adaptor-framework/timer.cpp @@ -30,6 +31,7 @@ SET( public_api_adaptor_framework_header_files ${adaptor_public_api_dir}/adaptor-framework/application.h ${adaptor_public_api_dir}/adaptor-framework/device-status.h ${adaptor_public_api_dir}/adaptor-framework/encoded-image-buffer.h + ${adaptor_public_api_dir}/adaptor-framework/graphics-backend.h ${adaptor_public_api_dir}/adaptor-framework/input-method.h ${adaptor_public_api_dir}/adaptor-framework/key.h ${adaptor_public_api_dir}/adaptor-framework/key-grab.h -- 2.34.1