Add function & environment option to choose graphics backend 25/315325/1
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Wed, 16 Oct 2024 21:21:44 +0000 (22:21 +0100)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Fri, 29 Nov 2024 22:18:06 +0000 (22:18 +0000)
Change-Id: If40ae6f65fcc52a4e08f06dfecfd60413ca40421

dali/dali.h
dali/internal/adaptor/common/adaptor-builder-impl.cpp
dali/internal/adaptor/common/application-impl.cpp
dali/internal/system/common/environment-options.cpp
dali/internal/system/common/environment-options.h
dali/internal/system/common/environment-variables.h
dali/public-api/adaptor-framework/graphics-backend.cpp [new file with mode: 0644]
dali/public-api/adaptor-framework/graphics-backend.h [new file with mode: 0644]
dali/public-api/file.list

index db4687dface681c44cff165f0a6324a575adce9b..722cc1f0dbc9ba7b8f80218b132a3665adf7c91d 100644 (file)
@@ -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 <dali/public-api/adaptor-framework/application.h>
 #include <dali/public-api/adaptor-framework/device-status.h>
+#include <dali/public-api/adaptor-framework/graphics-backend.h>
 #include <dali/public-api/adaptor-framework/input-method.h>
 #include <dali/public-api/adaptor-framework/key-grab.h>
 #include <dali/public-api/adaptor-framework/key.h>
index 1520de45fc2c9a3abbc9c9b9673851185f53bbc7..7432f104e13b7dafc32831d8bcddf6c6f54e06ca 100644 (file)
@@ -20,6 +20,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/internal/window-system/common/display-utils.h>
+#include <dali/public-api/adaptor-framework/graphics-backend.h>
 
 #if defined(VULKAN_ENABLED)
 #include <dali/internal/graphics/vulkan/vulkan-graphics-factory.h>
@@ -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<GraphicsFactory>(environmentOptions);
 }
index 41c8f646e2b6901bad6c9c80949e316a1e0d3139..fba4ee115ffb53bcff96a9bba8ed2862256f56fc 100644 (file)
@@ -42,6 +42,7 @@
 #include <dali/internal/window-system/common/window-impl.h>
 #include <dali/internal/window-system/common/window-render-surface.h>
 #include <dali/internal/window-system/common/window-system.h>
+#include <dali/public-api/adaptor-framework/graphics-backend.h>
 
 // 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<EnvironmentOptions>(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);
index 21d052097d291d59b07616666c12a3456a028b3c..6782a3050e73c91546b5db789827f6824919df77 100644 (file)
@@ -21,6 +21,7 @@
 // EXTERNAL INCLUDES
 #include <dali/integration-api/render-controller.h>
 #include <dali/public-api/math/math-utils.h>
+#include <algorithm>
 #include <cstdlib>
 #include <functional>
 
@@ -108,6 +109,55 @@ void SetFromEnvironmentVariable(const char* variable, std::function<void(Type)>
   }
 }
 
+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<Graphics::Backend>(*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<int>(DALI_REFRESH_RATE, GreaterThan(mRenderRefreshRate, 1));
 
   SetFromEnvironmentVariable(DALI_ENV_MULTI_SAMPLING_LEVEL, mMultiSamplingLevel);
index 61f9607df2dc2031647052fa5a63fba7695b309f..cd8f5bf5d65abc21dbffe1be18e4f917cda8bd95 100644 (file)
@@ -26,6 +26,7 @@
 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
 #include <dali/integration-api/adaptor-framework/trace-factory-interface.h>
 #include <dali/internal/adaptor/common/threading-mode.h>
+#include <dali/public-api/adaptor-framework/graphics-backend.h>
 
 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
index 30c0102d79d2b57f1a51095c1da12d733f814c3f..b657601c7ffb9ea4adb5c923605db06cc42b07bb 100644 (file)
@@ -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 (file)
index 0000000..3d8e9f0
--- /dev/null
@@ -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 <dali/public-api/adaptor-framework/graphics-backend.h>
+
+// EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
+
+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 (file)
index 0000000..6fa346f
--- /dev/null
@@ -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 <dali/public-api/dali-adaptor-common.h>
+
+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
index 7128e2dc46d9b4fe0b700dd97d2bace9c2f252d2..cba5dcb55a3e553365f5d2b71e4a35d26fd1d733 100644 (file)
@@ -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