Skip to content

Commit

Permalink
Enable capture with OpenXR layer
Browse files Browse the repository at this point in the history
  • Loading branch information
RenfengLiu committed Oct 18, 2023
1 parent 087cb91 commit 10c8af7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 34 deletions.
11 changes: 9 additions & 2 deletions layer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/vk_dispatch.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/vk_layer_impl.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/vk_layer_android.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/gles_layer.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/layer_common.cc"
)

include_directories(${CMAKE_SOURCE_DIR}
Expand Down Expand Up @@ -83,7 +84,9 @@ set_target_properties(${target_name}

set(target_name XrApiLayer_dive)
set(XR_LAYER_SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/openxr_layer.cc"
${CMAKE_SOURCE_DIR}/third_party/OpenXR-SDK/src/xr_generated_dispatch_table.c)
"${CMAKE_SOURCE_DIR}/third_party/OpenXR-SDK/src/xr_generated_dispatch_table.c"
"${CMAKE_CURRENT_SOURCE_DIR}/layer_common.cc"
)

add_library(${target_name} SHARED ${XR_LAYER_SRC_FILES})
set_target_properties(${target_name}
Expand All @@ -95,7 +98,11 @@ target_include_directories(${target_name} PUBLIC
${CMAKE_SOURCE_DIR}/third_party/OpenXR-SDK/src
${CMAKE_SOURCE_DIR}/third_party/OpenXR-SDK/src/common
)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set_target_properties(${target_name} PROPERTIES LINK_FLAGS_RELEASE -s)
endif()

if(ANDROID)
target_link_libraries(${target_name} log)
target_link_libraries(${target_name} ${TARGET_LINK_LIBS})

endif()
51 changes: 51 additions & 0 deletions layer/layer_common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2023 Google Inc.
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.
*/

#include "layer_common.h"

#include <cstdio>
#include <cstring>
#include <iostream>

namespace DiveLayer
{

bool IsLibwrapLoaded()
{
bool loaded = false;
#if defined(__ANDROID__)
FILE *maps = fopen("/proc/self/maps", "r");
if (!maps)
{
return loaded;
}

char *line = NULL;
size_t size = 0;
while (getline(&line, &size, maps) > 0)
{
if (strstr(line, "libwrap.so"))
{
loaded = true;
break;
}
}
free(line);
fclose(maps);
#endif
return loaded;
}
} // namespace DiveLayer
3 changes: 3 additions & 0 deletions layer/layer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ inline uintptr_t DataKey(const void *object)
{
return (uintptr_t)(*(void **)object);
}

bool IsLibwrapLoaded();

} // namespace DiveLayer
2 changes: 1 addition & 1 deletion layer/manifest/XrApiLayer_dive.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"functions": {
"xrNegotiateLoaderApiLayerInterface": "xrNegotiateLoaderApiLayerInterface"
},
"disable_environment": "DISABLE_XR_APILAYER_DIVE"
"disable_environment": "DISABLE_XR_APILAYER_DIVE_1"
}
}
29 changes: 27 additions & 2 deletions layer/openxr_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ limitations under the License.
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>

#include "capture_service/log.h"
#include "capture_service/server.h"
#include "capture_service/trace_mgr.h"
#include "layer_common.h"
#include "loader_interfaces.h"
#include "xr_generated_dispatch_table.h"
Expand All @@ -52,6 +55,26 @@ struct XrInstanceData
{
XrInstance instance;
XrGeneratedDispatchTable dispatch_table;
bool is_libwrap_loaded;
std::thread server_thread;

XrInstanceData()
{
is_libwrap_loaded = IsLibwrapLoaded();
LOGI("libwrap loaded: %d", is_libwrap_loaded);
if (is_libwrap_loaded)
{
server_thread = std::thread(Dive::server_main);
}
}
~XrInstanceData()
{
if (is_libwrap_loaded && server_thread.joinable())
{
LOGI("Wait for server thread to join");
server_thread.join();
}
}
};

struct XrSessionData
Expand Down Expand Up @@ -104,19 +127,21 @@ XrSessionData *GetXrSessionLayerData(uintptr_t key)
XRAPI_ATTR XrResult XRAPI_CALL ApiDiveLayerXrEndFrame(XrSession session,
const XrFrameEndInfo *frameEndInfo)
{
XrResult result = XrResult::XR_ERROR_HANDLE_INVALID;

LOGD("ApiDiveLayerXrEndFrame key %lu, sess %p \n", DataKey(session), session);
auto sess_data = GetXrSessionLayerData(DataKey(session));
if (sess_data)
{
return sess_data->dispatch_table.EndFrame(session, frameEndInfo);
result = sess_data->dispatch_table.EndFrame(session, frameEndInfo);
}
else
{
LOGE("sess_data is null in ApiDiveLayerXrEndFrame\n");
}
Dive::GetTraceMgr().OnNewFrame();

return XrResult::XR_ERROR_HANDLE_INVALID;
return result;
}

XRAPI_ATTR XrResult XRAPI_CALL
Expand Down
37 changes: 8 additions & 29 deletions layer/vk_layer_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,23 @@ limitations under the License.
namespace DiveLayer
{

bool is_libwrap_loaded()
{
bool loaded = false;
#if defined(__ANDROID__)
FILE *maps = fopen("/proc/self/maps", "r");
if (!maps)
{
return loaded;
}

char *line = NULL;
size_t size = 0;
while (getline(&line, &size, maps) > 0)
{
if (strstr(line, "libwrap.so"))
{
loaded = true;
break;
}
}
free(line);
fclose(maps);
#endif
return loaded;
}

// Declare our per-instance and per-device contexts.
// These are created and initialized in vkCreateInstance and vkCreateDevice.
struct InstanceData
{
VkInstance instance;
InstanceDispatchTable dispatch_table;
bool is_libwrap_loaded;
std::thread server_thread;

InstanceData() :
server_thread(std::thread(Dive::server_main))
InstanceData()
{
LOGI("libwrap loaded: %d", is_libwrap_loaded());
is_libwrap_loaded = IsLibwrapLoaded();
LOGI("libwrap loaded: %d", is_libwrap_loaded);
if (is_libwrap_loaded)
{
server_thread = std::thread(Dive::server_main);
}
}

~InstanceData()
Expand Down

0 comments on commit 10c8af7

Please sign in to comment.