Skip to content

Commit

Permalink
feat: Androind support [2 of series]
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryolitia authored and harry75369 committed Sep 25, 2024
1 parent 32f3a36 commit 58cdea0
Show file tree
Hide file tree
Showing 12 changed files with 540 additions and 8 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,19 @@ You need to download Android NDK r27 from here: https://github.com/android/ndk/r
Build & install `vgg_container` libraries for [vgg_android](https://github.com/verygoodgraphics/vgg_android).

```bash
mkdir build.android
cd build.android
# For x86_64
cmake .. -DCMAKE_TOOLCHAIN_FILE=<android_ndk_path>/build/cmake/android.toolchain.cmake -DVGG_VAR_TARGET="Android-x86_64" -DANDROID_NDK=<android_ndk_path> -DANDROID_PLATFORM=android-24
mkdir build.android.x86_64
cd build.android.x86_64
cmake .. -DCMAKE_TOOLCHAIN_FILE=<android_ndk_path>/build/cmake/android.toolchain.cmake -DVGG_VAR_TARGET="Android-x86_64" -DANDROID_NDK=<android_ndk_path> -DANDROID_PLATFORM=android-24 -DANDROID_ABI=x86_64
cmake --build . --parallel
cmake --install . --prefix <path/to/vgg_android>/VggRuntime/external/x86_64
# For arm64
cmake .. -DCMAKE_TOOLCHAIN_FILE=<android_ndk_path>/build/cmake/android.toolchain.cmake -DVGG_VAR_TARGET="Android-arm64-v8a" -DANDROID_NDK=<android_ndk_path> -DANDROID_PLATFORM=android-24
cd ..
mkdir build.android.arm64
cd build.android.arm64
cmake .. -DCMAKE_TOOLCHAIN_FILE=<android_ndk_path>/build/cmake/android.toolchain.cmake -DVGG_VAR_TARGET="Android-arm64-v8a" -DANDROID_NDK=<android_ndk_path> -DANDROID_PLATFORM=android-24 -DANDROID_ABI=arm64-v8a
cmake --build . --parallel
cmake --install . --prefix <path/to/vgg_android/VggRuntime/external>
cmake --install . --prefix <path/to/vgg_android>/VggRuntime/external/arm64-v8a
```

#### Qt building example
Expand Down
1 change: 1 addition & 0 deletions cmake/SkiaUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ skia_use_expat=true
skia_use_freetype=true
skia_use_system_expat=false
skia_use_system_icu=false
skia_use_egl=true
skia_use_vulkan=${VULKAN_AVAILABLE}")

cmake_minimum_required(VERSION 3.19) # for string(JSON ...)
Expand Down
60 changes: 58 additions & 2 deletions include/Utility/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
#define MOD4 KMOD_CTRL
#endif

#ifdef __ANDROID__
#include <android/log.h>
#include <android/set_abort_message.h>
#endif

/**************************************************************************************************/
/* Common */
/**************************************************************************************************/
Expand Down Expand Up @@ -159,7 +164,7 @@ inline std::string scalarfmt(double v, size_t maxFractionalDigitNums = 2)
#define _BG_WHITE_ "47"

/// Common formatters
#if !defined(EMSCRIPTEN) && !defined(VGG_TARGET_PLATFORM_iOS)
#if !defined(EMSCRIPTEN) && !defined(VGG_TARGET_PLATFORM_iOS) && !defined(VGG_TARGET_PLATFORM_Android)
#define FAIL_COLOR(X) "\x1b[" _FMT_BRIGHT_ ";" _FG_RED_ "m" X "\x1b[" _FMT_RESET_ "m"
#define WARN_COLOR(X) "\x1b[" _FMT_BRIGHT_ ";" _FG_YELLOW_ "m" X "\x1b[" _FMT_RESET_ "m"
#define INFO_COLOR(X) "\x1b[" _FMT_BRIGHT_ ";" _FG_BLUE_ "m" X "\x1b[" _FMT_RESET_ "m"
Expand Down Expand Up @@ -198,26 +203,77 @@ static inline FILE* _log_file_()
} // namespace LOG

/// Private logging macros

#if defined(__ANDROID__)

// Android don't use stdout, stderr, use android log instead

#define _MSG_(type, log, msg, ...) \
do \
{ \
constexpr auto _fnTypeToAndroidLevel = [](const char* _str_type) constexpr \
{ \
char _c = _str_type[0]; \
switch (_c) \
{ \
case 'F': \
return ANDROID_LOG_ERROR; \
case 'W': \
return ANDROID_LOG_WARN; \
case 'I': \
return ANDROID_LOG_INFO; \
case 'D': \
return ANDROID_LOG_DEBUG; \
default: \
return ANDROID_LOG_VERBOSE; \
} \
}; \
__android_log_print(_fnTypeToAndroidLevel(type), "VGG", "" msg, ##__VA_ARGS__); \
} while (0)

#else

#define _MSG_(type, log, msg, ...) \
do \
{ \
fprintf((log), "[" type "] " msg "\n", ##__VA_ARGS__); \
fflush((log)); \
} while (0)

#endif

/// Public logging macros
#define FAIL(msg, ...) \
do \
{ \
_MSG_("FAIL", LOG::_log_file_(), FAIL_COLOR(msg), ##__VA_ARGS__); \
} while (0)

#if defined(__ANDROID__)
// make abort message on one line, a fatal log will implicitly call set_abort_message
// format: ("%s:%d " msg, __FILENAME__, __LINE__, ##__VA_ARGS__)
#define FAILED(msg, ...) \
do \
{ \
__android_log_print( \
ANDROID_LOG_FATAL, \
"VGG", \
"%s:%d " msg, \
__FILENAME__, \
__LINE__, \
##__VA_ARGS__); \
std::abort(); \
} while (0)
#else
#define FAILED(msg, ...) \
do \
{ \
_MSG_("FAIL", LOG::_log_file_(), FAIL_COLOR("%s:%d "), __FILENAME__, __LINE__); \
FAIL(msg, ##__VA_ARGS__); \
std::abort(); \
} while (0)
#endif

#define WARN(msg, ...) \
do \
{ \
Expand Down Expand Up @@ -259,7 +315,7 @@ static inline FILE* _log_file_()
{ \
if (!(x)) \
{ \
if (strcmp((msg), "")) \
if (strcmp((msg), "") != 0) \
{ \
FAILED(STR(x) " : " msg, ##__VA_ARGS__); \
} \
Expand Down
48 changes: 48 additions & 0 deletions include/VGG/Container/AndroidContainer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023-2024 VeryGoodGraphics LTD <[email protected]>
*
* Licensed under the VGG License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.verygoodgraphics.com/licenses/LICENSE-1.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 "Event.hpp"
#include "IContainer.hpp"
#include "ISdk.hpp"
#include "VggTypes.hpp"

#include <memory>

struct ANativeWindow;
typedef struct ANativeWindow ANativeWindow;

namespace VGG
{

class AndroidContainerImpl;
class AndroidContainer : public IContainer
{
std::unique_ptr<AndroidContainerImpl> m_impl;

public:

AndroidContainer();
~AndroidContainer();

void setView(ANativeWindow* window);

private:
virtual IContainer* container() override;
};

} // namespace VGG
9 changes: 9 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ if(VGG_VAR_TARGET MATCHES "^iOS")
)
elseif(VGG_VAR_TARGET MATCHES "^Android")
target_link_directories(vgg_libnode PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/${LIB_NODE_FOLDER}/bin)
target_link_directories(vgg_libnode PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/${LIB_NODE_FOLDER}/bin/${ANDROID_ABI})
elseif(APPLE)
target_link_directories(vgg_libnode PUBLIC ${NODE_FOLDER}/out/Release)
if (VGG_VAR_TARGET_ARCH STREQUAL "ARM")
Expand Down Expand Up @@ -322,3 +323,11 @@ if(VGG_CONTAINER_FOR_QT)
)
endif()
endif()

if(VGG_VAR_TARGET MATCHES "^Android")
# install libnode.so
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${LIB_NODE_FOLDER}/bin/${ANDROID_ABI}/libnode.so
COMPONENT container
DESTINATION lib
)
endif ()
7 changes: 7 additions & 0 deletions src/Adapter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ else()
target_include_directories(vgg_adapter PUBLIC
${CMAKE_SOURCE_DIR}/include/Application/Event
)
elseif(VGG_VAR_TARGET MATCHES "^Android")
target_sources(vgg_adapter PRIVATE
Container/AndroidGraphicsContext.cpp
)
target_include_directories(vgg_adapter PUBLIC
${CMAKE_SOURCE_DIR}/include/Application/Event
)
elseif(VGG_CONTAINER_FOR_QT)
target_sources(vgg_adapter PRIVATE
Container/QtGraphicsContext.cpp
Expand Down
Loading

0 comments on commit 58cdea0

Please sign in to comment.