Skip to content

Commit

Permalink
Move vkb::filesystem to components (#894)
Browse files Browse the repository at this point in the history
* Move to std filesystem

* quality checks

* android compile fixes

* use std::path

* fix windows

* qa

* review fixes

* qa
  • Loading branch information
tomadamatkinson authored Mar 11, 2024
1 parent 6e2b524 commit 187e806
Show file tree
Hide file tree
Showing 81 changed files with 1,488 additions and 1,063 deletions.
4 changes: 2 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023, Arm Limited and Contributors
# Copyright (c) 2019-2024, Arm Limited and Contributors
#
# SPDX-License-Identifier: Apache-2.0
#
Expand Down Expand Up @@ -41,7 +41,7 @@ else()
add_executable(${PROJECT_NAME} WIN32 ${SRC})
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE vkb__core apps plugins)
target_link_libraries(${PROJECT_NAME} PRIVATE vkb__core vkb__filesystem apps plugins)

# Create android project
if(ANDROID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(toolbar);

if (loadNativeLibrary(getResources().getString(R.string.native_lib_name))) {
// Initialize cpp android platform
File external_files_dir = getExternalFilesDir("");
File temp_files_dir = getCacheDir();
if (external_files_dir != null && temp_files_dir != null) {
initFilePath(external_files_dir.toString(), temp_files_dir.toString());
}

// Get sample info from cpp cmake generated file
samples = new SampleStore(Arrays.asList(getSamples()));
}
Expand Down Expand Up @@ -340,9 +333,4 @@ public void launchSample(String sampleID) {
* @param args The arguments that are to be passed to the app
*/
private native void sendArgumentsToPlatform(String[] args);

/**
* @brief Initiate the file system for the Native Application
*/
private native void initFilePath(String external_dir, String temp_path);
}
7 changes: 5 additions & 2 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2019-2023, Arm Limited and Contributors
/* Copyright (c) 2019-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -15,11 +15,12 @@
* limitations under the License.
*/

#include "common/logging.h"
#include "core/util/logging.hpp"
#include "platform/platform.h"
#include "plugins/plugins.h"

#include <core/platform/entrypoint.hpp>
#include <filesystem/filesystem.hpp>

#if defined(PLATFORM__ANDROID)
# include "platform/android/android_platform.h"
Expand All @@ -35,6 +36,8 @@

CUSTOM_MAIN(context)
{
vkb::filesystem::init_with_context(context);

#if defined(PLATFORM__ANDROID)
vkb::AndroidPlatform platform{context};
#elif defined(PLATFORM__WINDOWS)
Expand Down
10 changes: 5 additions & 5 deletions app/plugins/screenshot/screenshot.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2020-2021, Arm Limited and Contributors
/* Copyright (c) 2020-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -17,7 +17,7 @@

#pragma once

#include "platform/filesystem.h"
#include "filesystem/legacy.h"
#include "platform/plugins/plugin_base.h"

namespace plugins
Expand All @@ -28,11 +28,11 @@ using ScreenshotTags = vkb::PluginBase<Screenshot, vkb::tags::Passive>;

/**
* @brief Screenshot
*
*
* Capture a screen shot of the last rendered image at a given frame. The output can also be named
*
*
* Usage: vulkan_sample sample afbc --screenshot 1 --screenshot-output afbc-screenshot
*
*
*/
class Screenshot : public ScreenshotTags
{
Expand Down
4 changes: 2 additions & 2 deletions bldsys/cmake/template/sample/sample.cpp.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2019-2023, Arm Limited and Contributors
/* Copyright (c) 2019-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -20,7 +20,7 @@
#include "common/vk_common.h"
#include "gltf_loader.h"
#include "gui.h"
#include "platform/filesystem.h"
#include "filesystem/legacy.h"
#include "platform/platform.h"
#include "rendering/subpasses/forward_subpass.h"
#include "stats/stats.h"
Expand Down
5 changes: 4 additions & 1 deletion components/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023, Thomas Atkinson
# Copyright (c) 2023-2024, Thomas Atkinson
#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -25,3 +25,6 @@ elseif(APPLE OR UNIX)
else()
message(FATAL_ERROR "Unsupported platform")
endif()


add_subdirectory(filesystem)
52 changes: 34 additions & 18 deletions components/android/src/context.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2023, Thomas Atkinson
/* Copyright (c) 2023-2024, Thomas Atkinson
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -21,18 +21,7 @@

extern "C"
{
JNIEXPORT void JNICALL
Java_com_khronos_vulkan_1samples_SampleLauncherActivity_initFilePath(JNIEnv *env, jobject thiz, jstring external_dir, jstring temp_dir)
{
const char *external_dir_cstr = env->GetStringUTFChars(external_dir, 0);
vkb::AndroidPlatformContext::android_external_storage_directory = std::string(external_dir_cstr) + "/";
env->ReleaseStringUTFChars(external_dir, external_dir_cstr);

const char *temp_dir_cstr = env->GetStringUTFChars(temp_dir, 0);
vkb::AndroidPlatformContext::android_temp_directory = std::string(temp_dir_cstr) + "/";
env->ReleaseStringUTFChars(temp_dir, temp_dir_cstr);
}

// TODO: Arguments can be parsed from the bundle
JNIEXPORT void JNICALL
Java_com_khronos_vulkan_1samples_SampleLauncherActivity_sendArgumentsToPlatform(JNIEnv *env, jobject thiz, jobjectArray arg_strings)
{
Expand All @@ -53,17 +42,44 @@ extern "C"
}
}

namespace details
{
std::string get_external_storage_directory(android_app *app)
{
return app->activity->externalDataPath;
}

std::string get_external_cache_directory(android_app *app)
{
JNIEnv *env;
app->activity->vm->AttachCurrentThread(&env, NULL);

jclass cls = env->FindClass("android/app/NativeActivity");
jmethodID getCacheDir = env->GetMethodID(cls, "getCacheDir", "()Ljava/io/File;");
jobject cache_dir = env->CallObjectMethod(app->activity->javaGameActivity, getCacheDir);

jclass fcls = env->FindClass("java/io/File");
jmethodID getPath = env->GetMethodID(fcls, "getPath", "()Ljava/lang/String;");
jstring path_string = (jstring) env->CallObjectMethod(cache_dir, getPath);

const char *path_chars = env->GetStringUTFChars(path_string, NULL);
std::string temp_folder(path_chars);

env->ReleaseStringUTFChars(path_string, path_chars);
app->activity->vm->DetachCurrentThread();
return temp_folder;
}
} // namespace details

namespace vkb
{
std::string AndroidPlatformContext::android_external_storage_directory = {};
std::string AndroidPlatformContext::android_temp_directory = {};
std::vector<std::string> AndroidPlatformContext::android_arguments = {};
std::vector<std::string> AndroidPlatformContext::android_arguments = {};

AndroidPlatformContext::AndroidPlatformContext(android_app *app) :
PlatformContext{}, app{app}
{
_external_storage_directory = android_external_storage_directory;
_temp_directory = android_temp_directory;
_external_storage_directory = details::get_external_storage_directory(app);
_temp_directory = details::get_external_cache_directory(app);
_arguments = android_arguments;
}
} // namespace vkb
10 changes: 7 additions & 3 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023, Thomas Atkinson
# Copyright (c) 2023-2024, Thomas Atkinson
#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -17,14 +17,18 @@
vkb__register_component(
NAME core
HEADERS
include/core/platform/context.hpp
include/core/platform/entrypoint.hpp

include/core/util/strings.hpp
include/core/util/error.hpp
include/core/util/hash.hpp
include/core/util/logging.hpp
SRC
src/strings.cpp
src/logging.cpp
LINK_LIBS
spdlog
fmt
spdlog::spdlog
)

vkb__register_tests(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2018-2023, Arm Limited and Contributors
/* Copyright (c) 2018-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -17,20 +17,21 @@

#pragma once

#include <fmt/format.h>
#include <spdlog/fmt/fmt.h>
#include <spdlog/spdlog.h>

#define LOGGER_FORMAT "[%^%l%$] %v"
#define PROJECT_NAME "VulkanSamples"

// Mainly for IDEs
#ifndef ROOT_PATH_SIZE
# define ROOT_PATH_SIZE 0
#endif

#define __FILENAME__ (static_cast<const char *>(__FILE__) + ROOT_PATH_SIZE)

#define LOGI(...) spdlog::info(__VA_ARGS__);
#define LOGW(...) spdlog::warn(__VA_ARGS__);
#define LOGE(...) spdlog::error("[{}:{}] {}", __FILENAME__, __LINE__, fmt::format(__VA_ARGS__));
#define LOGE(...) spdlog::error("{}", fmt::format(__VA_ARGS__));
#define LOGD(...) spdlog::debug(__VA_ARGS__);

namespace vkb
{
namespace logging
{
void init();
}
} // namespace vkb
52 changes: 52 additions & 0 deletions components/core/src/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright (c) 2024, Thomas Atkinson
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 "core/util/logging.hpp"

#include "spdlog/cfg/env.h"

#ifdef PLATFORM__ANDROID
# include "spdlog/sinks/android_sink.h"
#else
# include "spdlog/sinks/stdout_color_sinks.h"
#endif

namespace vkb
{
namespace logging
{
void init()
{
// Taken from "spdlog/cfg/env.h" and renamed SPDLOG_LEVEL to VKB_LOG_LEVEL
auto env_val = spdlog::details::os::getenv("VKB_LOG_LEVEL");
if (!env_val.empty())
{
spdlog::cfg::helpers::load_levels(env_val);
}

#ifdef PLATFORM__ANDROID
auto logger = spdlog::android_logger_mt("vkb", "VulkanSamples");
#else
auto logger = spdlog::stdout_color_mt("vkb");
#endif

logger->set_pattern(LOGGER_FORMAT);
logger->set_level(spdlog::level::trace);
spdlog::set_default_logger(logger);
}
} // namespace logging
} // namespace vkb
46 changes: 46 additions & 0 deletions components/filesystem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (c) 2023-2024, Thomas Atkinson
#
# SPDX-License-Identifier: Apache-2.0
#
# 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.

vkb__register_component(
NAME filesystem
HEADERS
include/filesystem/filesystem.hpp
include/filesystem/legacy.h
# private
src/std_filesystem.hpp
SRC
src/legacy.cpp
src/filesystem.cpp
src/std_filesystem.cpp
LINK_LIBS
vkb__core
stb
)

# GCC 9.0 and later has std::filesystem in the stdc++ library
# Earlier versions require linking against stdc++fs
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
target_link_libraries(vkb__filesystem PRIVATE stdc++fs)
endif()

vkb__register_tests(
COMPONENT filesystem
NAME filesystem
SRC
tests/filesystem.test.cpp
LINK_LIBS
vkb__filesystem
)
19 changes: 19 additions & 0 deletions components/filesystem/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
////
- Copyright (c) 2023-2024, Thomas Atkinson
-
- SPDX-License-Identifier: Apache-2.0
-
- 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.
-
////
= File System
Loading

0 comments on commit 187e806

Please sign in to comment.