[Bf-blender-cvs] [4eeb7523e9c] temp-ghost_openxr: Use some C++ features where useful

Julian Eisel noreply at git.blender.org
Tue Jun 18 01:04:43 CEST 2019


Commit: 4eeb7523e9c1ef8b28596d0d830d847bace10bd9
Author: Julian Eisel
Date:   Tue Jun 18 01:02:33 2019 +0200
Branches: temp-ghost_openxr
https://developer.blender.org/rB4eeb7523e9c1ef8b28596d0d830d847bace10bd9

Use some C++ features where useful

Also correct include guards.

===================================================================

M	intern/ghost/intern/GHOST_XR.cpp
M	intern/ghost/intern/GHOST_XREvent.cpp
M	intern/ghost/intern/GHOST_XRSession.cpp
M	intern/ghost/intern/GHOST_XR_intern.h
M	intern/ghost/intern/GHOST_XR_openxr_includes.h

===================================================================

diff --git a/intern/ghost/intern/GHOST_XR.cpp b/intern/ghost/intern/GHOST_XR.cpp
index cc26ba60e14..cba1e8b8413 100644
--- a/intern/ghost/intern/GHOST_XR.cpp
+++ b/intern/ghost/intern/GHOST_XR.cpp
@@ -15,22 +15,16 @@
  */
 
 /** \file
- * \ingroup wm
+ * \ingroup GHOST
  *
  * Abstraction for XR (VR, AR, MR, ..) access via OpenXR.
  */
 
 #include <cassert>
-#include <cstring>
-#include <cstdio>
-#include <stdlib.h>
-#include <string.h>
+#include <string>
 
 #include "GHOST_C-api.h"
 
-#include "GHOST_XR_openxr_includes.h"
-
-//#include "wm_xr.h"
 #include "GHOST_XR_intern.h"
 
 /* Toggle printing of available OpenXR extensions and API-layers. Should probably be changed to use
@@ -40,14 +34,15 @@
 /**
  * \param layer_name May be NULL for extensions not belonging to a specific layer.
  */
-static bool openxr_gather_extensions_ex(OpenXRData *oxr, const char *layer_name)
+static bool openxr_gather_extensions_ex(std::vector<XrExtensionProperties> &extensions,
+                                        const char *layer_name)
 {
+  const unsigned long old_extension_count = extensions.size();
   uint32_t extension_count = 0;
 
-  oxr->extensions = NULL;
-
   /* Get count for array creation/init first. */
-  if (XR_FAILED(xrEnumerateInstanceExtensionProperties(layer_name, 0, &extension_count, NULL))) {
+  if (XR_FAILED(
+          xrEnumerateInstanceExtensionProperties(layer_name, 0, &extension_count, nullptr))) {
     return false;
   }
 
@@ -56,11 +51,11 @@ static bool openxr_gather_extensions_ex(OpenXRData *oxr, const char *layer_name)
     return true;
   }
 
-  oxr->extensions = (XrExtensionProperties *)calloc(oxr->extension_count,
-                                                    sizeof(*oxr->extensions));
-  oxr->extension_count = extension_count;
-  for (uint i = 0; i < extension_count; i++) {
-    oxr->extensions[i].type = XR_TYPE_EXTENSION_PROPERTIES;
+  for (uint32_t i = 0; i < extension_count; i++) {
+    XrExtensionProperties ext{};
+
+    ext.type = XR_TYPE_EXTENSION_PROPERTIES;
+    extensions.push_back(ext);
   }
 
 #ifdef USE_EXT_LAYER_PRINTS
@@ -70,10 +65,10 @@ static bool openxr_gather_extensions_ex(OpenXRData *oxr, const char *layer_name)
 #endif
   /* Actually get the extensions. */
   xrEnumerateInstanceExtensionProperties(
-      layer_name, extension_count, &extension_count, oxr->extensions);
+      layer_name, extension_count, &extension_count, extensions.data());
 #ifdef USE_EXT_LAYER_PRINTS
-  for (uint i = 0; i < extension_count; i++) {
-    printf("Extension: %s\n", oxr->extensions[i].extensionName);
+  for (uint32_t i = 0; i < extension_count; i++) {
+    printf("Extension: %s\n", extensions[i + old_extension_count].extensionName);
   }
 #endif
 
@@ -81,7 +76,7 @@ static bool openxr_gather_extensions_ex(OpenXRData *oxr, const char *layer_name)
 }
 static bool openxr_gather_extensions(OpenXRData *oxr)
 {
-  return openxr_gather_extensions_ex(oxr, NULL);
+  return openxr_gather_extensions_ex(oxr->extensions, nullptr);
 }
 
 static bool openxr_gather_api_layers(OpenXRData *oxr)
@@ -89,7 +84,7 @@ static bool openxr_gather_api_layers(OpenXRData *oxr)
   uint32_t layer_count = 0;
 
   /* Get count for array creation/init first. */
-  if (XR_FAILED(xrEnumerateApiLayerProperties(0, &layer_count, NULL))) {
+  if (XR_FAILED(xrEnumerateApiLayerProperties(0, &layer_count, nullptr))) {
     return false;
   }
 
@@ -98,29 +93,28 @@ static bool openxr_gather_api_layers(OpenXRData *oxr)
     return true;
   }
 
-  oxr->layers = (XrApiLayerProperties *)calloc(layer_count, sizeof(*oxr->layers));
-  oxr->layer_count = layer_count;
-  for (uint i = 0; i < layer_count; i++) {
-    oxr->layers[i].type = XR_TYPE_API_LAYER_PROPERTIES;
+  oxr->layers = std::vector<XrApiLayerProperties>(layer_count);
+  for (XrApiLayerProperties &layer : oxr->layers) {
+    layer.type = XR_TYPE_API_LAYER_PROPERTIES;
   }
 
   /* Actually get the layers. */
-  xrEnumerateApiLayerProperties(layer_count, &layer_count, oxr->layers);
-  for (uint i = 0; i < layer_count; i++) {
+  xrEnumerateApiLayerProperties(layer_count, &layer_count, oxr->layers.data());
+  for (XrApiLayerProperties &layer : oxr->layers) {
 #ifdef USE_EXT_LAYER_PRINTS
-    printf("Layer: %s\n", oxr->layers[i].layerName);
+    printf("Layer: %s\n", layer.layerName);
 #endif
     /* Each layer may have own extensions */
-    openxr_gather_extensions_ex(oxr, oxr->layers[i].layerName);
+    openxr_gather_extensions_ex(oxr->extensions, layer.layerName);
   }
 
   return true;
 }
 
-static bool openxr_extension_is_available(const OpenXRData *oxr, const char *extension_name)
+static bool openxr_extension_is_available(const OpenXRData *oxr, const std::string &extension_name)
 {
-  for (uint i = 0; i < oxr->extension_count; i++) {
-    if (strncmp(oxr->extensions[i].extensionName, extension_name, strlen(extension_name)) == 0) {
+  for (const XrExtensionProperties &ext : oxr->extensions) {
+    if (ext.extensionName == extension_name) {
       return true;
     }
   }
@@ -139,9 +133,10 @@ static const char *openxr_ext_name_from_wm_gpu_binding(eWM_xrGraphicsBinding bin
 #endif
     case WM_XR_GRAPHICS_UNKNOWN:
       assert(false);
+      return nullptr;
   }
 
-  return NULL;
+  return nullptr;
 }
 
 /**
@@ -154,7 +149,7 @@ static eWM_xrGraphicsBinding openxr_graphics_extension_to_enable_get(
   assert(create_info->gpu_binding_candidates != NULL);
   assert(create_info->gpu_binding_candidates_count > 0);
 
-  for (uint i = 0; i < create_info->gpu_binding_candidates_count; i++) {
+  for (uint32_t i = 0; i < create_info->gpu_binding_candidates_count; i++) {
     assert(create_info->gpu_binding_candidates[i] != WM_XR_GRAPHICS_UNKNOWN);
     const char *ext_name = openxr_ext_name_from_wm_gpu_binding(
         create_info->gpu_binding_candidates[i]);
@@ -171,54 +166,40 @@ static eWM_xrGraphicsBinding openxr_graphics_extension_to_enable_get(
  */
 static void openxr_extensions_to_enable_get(const wmXRContext *context,
                                             const OpenXRData *oxr,
-                                            const char ***r_ext_names,
-                                            uint *r_ext_count)
+                                            std::vector<const char *> &r_ext_names)
 {
   assert(context->gpu_binding != WM_XR_GRAPHICS_UNKNOWN);
 
   const char *gpu_binding = openxr_ext_name_from_wm_gpu_binding(context->gpu_binding);
-  assert(gpu_binding);
+  const static std::vector<std::string> try_ext; /* None yet */
 
-  {
-/* Zero sized array is GCC extension, so disable until it grows. */
-#if 0
-    const static char *try_ext[] = {};
-    const uint try_ext_count = ARRAY_SIZE(try_ext);
-#else
-    const static char **try_ext = NULL;
-    const uint try_ext_count = 0;
-#endif
-
-    assert(r_ext_names != NULL);
-    assert(r_ext_count != NULL);
+  assert(gpu_binding);
 
-    /* try_ext_count + 1 for graphics binding extension. */
-    *r_ext_names = (const char **)malloc(sizeof(char *) * (try_ext_count + 1));
+  r_ext_names.reserve(try_ext.size() + 1); /* + 1 for graphics binding extension. */
 
-    /* Add graphics binding extension. */
-    *r_ext_names[0] = gpu_binding;
-    *r_ext_count = 1;
+  /* Add graphics binding extension. */
+  r_ext_names.push_back(gpu_binding);
 
-    for (uint i = 1, j = 0; j < try_ext_count; j++) {
-      if (openxr_extension_is_available(oxr, try_ext[j])) {
-        *r_ext_names[i++] = try_ext[j];
-        (*r_ext_count)++;
-      }
+  for (const std::string &ext : try_ext) {
+    if (openxr_extension_is_available(oxr, ext)) {
+      r_ext_names.push_back(ext.c_str());
     }
   }
 }
 
 static bool openxr_instance_create(wmXRContext *context)
 {
-  XrInstanceCreateInfo create_info = {.type = XR_TYPE_INSTANCE_CREATE_INFO};
+  XrInstanceCreateInfo create_info{};
   OpenXRData *oxr = &context->oxr;
 
-  strncpy(create_info.applicationInfo.applicationName, "Blender", XR_MAX_APPLICATION_NAME_SIZE);
+  create_info.type = XR_TYPE_INSTANCE_CREATE_INFO;
+  std::string("Blender").copy(create_info.applicationInfo.applicationName,
+                              XR_MAX_APPLICATION_NAME_SIZE);
   create_info.applicationInfo.apiVersion = XR_CURRENT_API_VERSION;
 
-  openxr_extensions_to_enable_get(
-      context, oxr, &context->enabled_extensions, &create_info.enabledExtensionCount);
-  create_info.enabledExtensionNames = context->enabled_extensions;
+  openxr_extensions_to_enable_get(context, oxr, context->enabled_extensions);
+  create_info.enabledExtensionCount = context->enabled_extensions.size();
+  create_info.enabledExtensionNames = context->enabled_extensions.data();
 
   xrCreateInstance(&create_info, &oxr->instance);
 
@@ -229,7 +210,8 @@ static void openxr_instance_log_print(OpenXRData *oxr)
 {
   assert(oxr->instance != XR_NULL_HANDLE);
 
-  XrInstanceProperties instanceProperties = {.type = XR_TYPE_INSTANCE_PROPERTIES};
+  XrInstanceProperties instanceProperties{};
+  instanceProperties.type = XR_TYPE_INSTANCE_PROPERTIES;
   xrGetInstanceProperties(oxr->instance, &instanceProperties);
 
   printf("Connected to OpenXR runtime: %s\n", instanceProperties.runtimeName);
@@ -242,15 +224,15 @@ static void openxr_instance_log_print(OpenXRData *oxr)
  */
 wmXRContext *wm_xr_context_create(const wmXRContextCreateInfo *create_info)
 {
-  wmXRContext *xr_context = (wmXRContext *)calloc(1, sizeof(*xr_context));
+  wmXRContext *xr_context = new wmXRContext();
   OpenXRData *oxr = &xr_context->oxr;
 
 #ifdef USE_EXT_LAYER_PRINTS
   puts("Available OpenXR layers/extensions:");
 #endif
   if (!openxr_gather_api_layers(oxr) || !openxr_gather_extensions(oxr)) {
-    free(xr_context);
-    return NULL;
+    delete xr_context;
+    return nullptr;
   }
 #ifdef USE_EXT_LAYER_PRINTS
   puts("Done printing OpenXR layers/extensions.");
@@ -272,7 +254,7 @@ void wm_xr_context_destroy(wmXRContext *xr_context)
   OpenXRData *oxr = &xr_context->oxr;
 
   /* Unbinding may involve destruction, so call here too */
-  wm_xr_graphics_context_unbind(xr_context);
+  wm_xr_graphics_context_unbind(*xr_context);
 
   if (oxr->session != XR_NULL_HANDLE) {
     xrDestroySession(oxr->session);
@@ -281,11 +263,6 @@ void wm_xr_context_destroy(wmXRContext *xr_context)
     xrDestroyInstance(oxr->instance

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list