Skip to content

Headless and GLFW Windows #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bldsys/cmake/component_helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function(vkb__register_component)
if(TARGET_SRC) # Create static library
message("ADDING STATIC: vkb__${TARGET_NAME}")

add_library("vkb__${TARGET_NAME}" STATIC ${TARGET_SRC} ${TARGET_HEADERS})
add_library("vkb__${TARGET_NAME}" STATIC ${TARGET_SRC})

if(TARGET_LINK_LIBS)
target_link_libraries("vkb__${TARGET_NAME}" PUBLIC ${TARGET_LINK_LIBS})
Expand Down
2 changes: 2 additions & 0 deletions bldsys/cmake/test_helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function(vkb__register_tests)
message(FATAL_ERROR "One or more source files must be added to vkb__register_tests")
endif()

message("TEST: ${TARGET_NAME}")

add_executable(${TARGET_NAME} ${TARGET_SRC})
target_link_libraries(${TARGET_NAME} PUBLIC Catch2::Catch2WithMain)

Expand Down
1 change: 1 addition & 0 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
add_subdirectory(common)
add_subdirectory(platform)
add_subdirectory(events)
add_subdirectory(windows)
add_subdirectory(vfs)
8 changes: 4 additions & 4 deletions components/events/include/components/events/event_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ struct KeyEvent

struct CursorPositionEvent
{
uint32_t pos_x;
uint32_t pos_y;
float x;
float y;
};

enum class TouchAction
Expand All @@ -169,8 +169,8 @@ struct TouchEvent
{
TouchAction action;
uint32_t pointer_id;
uint32_t pos_x;
uint32_t pos_y;
float x;
float y;
};
} // namespace events
} // namespace components
4 changes: 2 additions & 2 deletions components/events/include/components/events/input_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace events
{
struct CursorPosition
{
uint32_t x;
uint32_t y;
float x;
float y;
};

struct Touch
Expand Down
18 changes: 9 additions & 9 deletions components/events/src/input_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ void InputManager::process(ChannelReceiverPtr<CursorPositionEvent> &events)

auto &next_known_position = *optional_next_known_position;

if (next_known_position.pos_x != m_last_cursor_position.x || next_known_position.pos_y != m_last_cursor_position.y)
if (next_known_position.x != m_last_cursor_position.x || next_known_position.y != m_last_cursor_position.y)
{
m_cursor_position_delta.x = next_known_position.pos_x - m_last_cursor_position.x;
m_cursor_position_delta.y = next_known_position.pos_y - m_last_cursor_position.y;
m_cursor_position_delta.x = next_known_position.x - m_last_cursor_position.x;
m_cursor_position_delta.y = next_known_position.y - m_last_cursor_position.y;
}

m_last_cursor_position = CursorPosition{next_known_position.pos_x, next_known_position.pos_y};
m_last_cursor_position = CursorPosition{next_known_position.x, next_known_position.y};
}

void InputManager::process(ChannelReceiverPtr<TouchEvent> &events)
Expand All @@ -66,18 +66,18 @@ void InputManager::process(ChannelReceiverPtr<TouchEvent> &events)
assert(res.second);
it = res.first;

it->second.position = CursorPosition{event.pos_x, event.pos_y};
it->second.position = CursorPosition{event.x, event.y};
}

auto &touch = it->second;

if (event.pos_x != touch.position.x || event.pos_y != touch.position.y)
if (event.x != touch.position.x || event.y != touch.position.y)
{
touch.position.x = event.pos_x - touch.position.x;
touch.position.y = event.pos_y - touch.position.y;
touch.position.x = event.x - touch.position.x;
touch.position.y = event.y - touch.position.y;
}

touch.position = CursorPosition{event.pos_x, event.pos_y};
touch.position = CursorPosition{event.x, event.y};
}
}

Expand Down
25 changes: 13 additions & 12 deletions components/vfs/include/components/vfs/std_filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ class StdFSFileSystem : public FileSystem
{
public:
StdFSFileSystem(const std::filesystem::path &base_path = "");
virtual ~StdFSFileSystem() = default;

virtual bool folder_exists(const std::string &folder_path) const override;
virtual bool file_exists(const std::string &file_path) const override;
virtual std::vector<uint8_t> read_chunk(const std::string &file_path, size_t offset, size_t count) const override;
virtual size_t file_size(const std::string &file_path) const override;
virtual void write_file(const std::string &file_path, const void *data, size_t size) override;
virtual std::vector<std::string> enumerate_files(const std::string &folder_path) const override;
virtual std::vector<std::string> enumerate_folders(const std::string &folderPath) const override;
virtual void make_directory(const std::string &path) override;
virtual bool remove(const std::string &path) override;
~StdFSFileSystem() override = default;

bool folder_exists(const std::string &folder_path) const override;
bool file_exists(const std::string &file_path) const override;
std::vector<uint8_t> read_chunk(const std::string &file_path, size_t offset, size_t count) const override;
size_t file_size(const std::string &file_path) const override;
void write_file(const std::string &file_path, const void *data, size_t size) override;
void make_directory(const std::string &path) override;
bool remove(const std::string &path) override;

std::vector<std::string> enumerate_files(const std::string &folder_path) const override;
std::vector<std::string> enumerate_folders(const std::string &folderPath) const override;

protected:
std::filesystem::path m_base_path;
Expand All @@ -57,7 +58,7 @@ class StdFSTempFileSystem final : public StdFSFileSystem
{
public:
StdFSTempFileSystem();
virtual ~StdFSTempFileSystem() = default;
~StdFSTempFileSystem() override = default;
};
} // namespace vfs
} // namespace components
1 change: 0 additions & 1 deletion components/vfs/src/android_aasset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ void AndroidAAssetManager::make_directory(const std::string &path)
bool AndroidAAssetManager::remove(const std::string &path)
{
throw std::runtime_error("vfs/android_aasset_manager.cpp line" + std::to_string(__LINE__) + "remove not implemented");
return false;
}

} // namespace vfs
Expand Down
6 changes: 5 additions & 1 deletion components/vfs/src/root_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ std::vector<uint8_t> FileSystem::read_file(const std::string &file_path) const
return read_chunk(file_path, 0, file_size(file_path));
}


void FileSystem::make_directory_recursive(const std::string &path)
{
auto parts = helpers::get_directory_parts(path);
Expand Down Expand Up @@ -165,7 +166,10 @@ size_t RootFileSystem::file_size(const std::string &file_path) const

void RootFileSystem::write_file(const std::string &file_path, const void *data, size_t size)
{
assert(data);
if (!data)
{
return;
}

std::string adjusted_path;
auto fs = find_file_system(file_path, &adjusted_path);
Expand Down
7 changes: 0 additions & 7 deletions components/vfs/tests/basic.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

using namespace components;

#define CHECK_ERROR(err) \
if (err != nullptr) \
{ \
INFO(err->what()); \
REQUIRE(err == nullptr); \
}

#define CATCH_ERROR() \
catch (std::exception & err) \
{ \
Expand Down
69 changes: 69 additions & 0 deletions components/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (c) 2022, Arm Limited and Contributors
#
# 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 window_headers
LINK_LIBS
vkb__events
volk
INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/include
)

# headless window
vkb__register_component(
NAME headless_window
SRC
src/headless.cpp
LINK_LIBS
vkb__window_headers
)

vkb__register_tests(
NAME headless_window_tests
SRC
tests/headless.test.cpp
LIBS
vkb__headless_window
)

if(DIRECT_TO_DISPLAY)
# Direct to display can only use specific windows. windows past this point are not compatible
return()
endif()

# glfw window
vkb__register_component(
NAME glfw_window
SRC
src/glfw.cpp
LINK_LIBS
vkb__window_headers
glfw
)

option(VKB_TEST_GLFW "Enable GLFW Tests" OFF)

if(VKB_TEST_GLFW)
vkb__register_tests(
NAME glfw_window_test
SRC
tests/glfw.test.cpp
LIBS
vkb__glfw_window
)
endif()
67 changes: 67 additions & 0 deletions components/windows/include/components/windows/glfw.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright (c) 2022, Arm Limited and Contributors
*
* 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.
*/

#pragma once

#include "components/events/event_types.hpp"
#include <components/windows/window.hpp>

struct GLFWwindow;

namespace components
{
namespace windows
{
class GLFWCallbackHelper;

class GLFWWindow : public Window
{
friend class GLFWCallbackHelper;

public:
GLFWWindow(const std::string &title = "New Window", const Extent &initial_extent = {600, 600});
~GLFWWindow();
void set_extent(const Extent &extent) override;
Extent extent() const override;
void set_position(const Position &position) override;
Position position() const override;
float dpi_factor() const override;
void set_title(const std::string &title) override;
std::string_view title() const override;
void update() override;
void attach(events::EventBus &bus) override;

VkResult create_surface(VkInstance instance, VkSurfaceKHR* surface) override;

protected:
std::string m_title;
Extent m_extent;
Position m_position;

GLFWwindow *m_handle{nullptr};
std::unique_ptr<GLFWCallbackHelper> m_callback_helper{nullptr};
events::ChannelSenderPtr<PositionChangedEvent> m_position_sender{nullptr};
events::ChannelSenderPtr<ContentRectChangedEvent> m_content_rect_sender{nullptr};
events::ChannelSenderPtr<FocusChangedEvent> m_focus_sender{nullptr};
events::ChannelSenderPtr<ShouldCloseEvent> m_should_close_sender{nullptr};

events::ChannelSenderPtr<events::KeyEvent> m_key_sender{nullptr};
events::ChannelSenderPtr<events::CursorPositionEvent> m_cursor_position_sender{nullptr};
events::ChannelSenderPtr<events::TouchEvent> m_touch_sender{nullptr};
};
} // namespace windows
} // namespace components
58 changes: 58 additions & 0 deletions components/windows/include/components/windows/headless.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright (c) 2022, Arm Limited and Contributors
*
* 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.
*/

#pragma once

#include <components/windows/window.hpp>

namespace components
{
namespace windows
{
class HeadlessWindow : public Window
{
public:
HeadlessWindow(const std::string &title = "New Window", const Extent &initial_extent = {600, 600});
~HeadlessWindow() = default;
void set_extent(const Extent &extent) override;
Extent extent() const override;
void set_position(const Position &position) override;
Position position() const override;
float dpi_factor() const override;
void set_title(const std::string &title) override;
std::string_view title() const override;
void update() override;
void attach(events::EventBus &bus) override;

VkResult create_surface(VkInstance instance, VkSurfaceKHR* surface) override;

private:
std::string m_title;

bool m_extent_changed{false};
Extent m_extent{};

bool m_position_changed{false};
Position m_position{0, 0};

float m_dpi_factor{1.0f};

events::ChannelSenderPtr<PositionChangedEvent> m_position_sender{nullptr};
events::ChannelSenderPtr<ContentRectChangedEvent> m_content_rect_sender{nullptr};
};
} // namespace windows
} // namespace components
Loading