Skip to content
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

Add the linux demo. #77

Merged
merged 1 commit into from
Dec 16, 2023
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 .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ jobs:

- name: Build Linux
run: |
node build_tgfx -DTGFX_USE_SWIFTSHADER=ON
node build_tgfx -s ./linux Hello2D -o ./out/release/linux -a x64

- name: Save Third-Party Cache
if: ${{ (github.event_name == 'push') && (steps.third-party-cache.outputs.cache-hit != 'true') }}
Expand Down
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ pack the generated `.wasm` file into the final web program. This is because comm
usually ignore the `.wasm` file. Moreover, remember to upload the `.wasm` file to a server, enabling
users to access it from the network.

### Linux

When running Linux, the system usually lacks GPU hardware support. Therefore, we utilize the
[**SwiftShader**](https://github.com/google/swiftshader) library to emulate the GPU rendering
environment. Since SwiftShader relies on certain X11 header files, it is necessary to install the
following packages before building the demo project:

```
yum install libX11-devel --nogpg
```

Next, execute the following commands in the linux/ directory:

```
cmake -B ./build -DCMAKE_BUILD_TYPE=Release
cmake --build ./build -- -j 12
```

You will get the demo executable file in the build directory. You also have the option of opening
the `linux/` directory in CLion and building the demo project directly in the IDE.

### Windows

To start, open the `win/` directory in CLion. Next, open the `File->Setting` panel and navigate to
Expand Down
24 changes: 13 additions & 11 deletions codeformat.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
cd $(dirname $0)

if [[ $(uname) == 'Darwin' ]]; then
MAC_REQUIRED_TOOLS="python3"
for TOOL in ${MAC_REQUIRED_TOOLS[@]}; do
Expand Down Expand Up @@ -26,24 +28,24 @@ echo "----begin to scan code format----"
find include -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find src -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find drawers -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find test -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find qt -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find ios -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find mac -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find linux -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find win -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find web -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find android -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find test/src -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find qt/src -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find ios/Hello2D -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find mac/Hello2D -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find linux/src -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find win/src -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find web/demo/src -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i
find android/app/src -name "*.cpp" -print -o -name "*.c" -print -o -name "*.h" -print -o -name "*.mm" -print -o -name "*.m" -print | xargs clang-format -i

git diff
result=`git diff`
if [[ $result =~ "diff" ]]
then
echo "----Failed to pass coding specification----"
echo "----Failed to pass the code format check----"
exit 1
else
echo "----Pass coding specification----"
echo "----Complete the code format check-----"
fi

echo "----Complete the scan code format-----"


17 changes: 17 additions & 0 deletions linux/.idea/fileTemplates/includes/TGFX File Header.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions linux/.idea/fileTemplates/internal/C Header File.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions linux/.idea/fileTemplates/internal/C Source File.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions linux/.idea/fileTemplates/internal/C++ Class Header.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions linux/.idea/fileTemplates/internal/C++ Class.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions linux/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.13)
project(Hello2D)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif ()

if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR $CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
set(ARCH x64)
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
set(ARCH arm64)
else ()
set(ARCH x86)
endif ()

file(GLOB SWIFTSHADER_LIBS ../vendor/swiftshader/mac/${ARCH}/*.${CMAKE_SHARED_LIBRARY_SUFFIX})
list(APPEND HELLO_2D_LIBS ${SWIFTSHADER_LIBS})
list(APPEND HELLO_2D_INCLUDES ../vendor/swiftshader/include)

if (APPLE)
find_library(APPLICATION_SERVICES_FRAMEWORK ApplicationServices REQUIRED)
list(APPEND HELLO_2D_LIBS ${APPLICATION_SERVICES_FRAMEWORK})
find_library(QUARTZ_CORE QuartzCore REQUIRED)
list(APPEND HELLO_2D_LIBS ${QUARTZ_CORE})
find_library(COCOA Cocoa REQUIRED)
list(APPEND HELLO_2D_LIBS ${COCOA})
find_library(FOUNDATION Foundation REQUIRED)
list(APPEND HELLO_2D_LIBS ${FOUNDATION})
find_library(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c)
list(APPEND HELLO_2D_LIBS ${ICONV_LIBRARIES})
find_library(CORE_MEDIA CoreMedia)
list(APPEND HELLO_2D_LIBS ${CORE_MEDIA})
else ()
find_package(Threads)
list(APPEND HELLO_2D_LIBS ${CMAKE_THREAD_LIBS_INIT})
list(APPEND HELLO_2D_LIBS dl)
list(APPEND HELLO_2D_COMPILE_OPTIONS -fPIC -pthread)
endif ()

set(TGFX_BUILD_DRAWERS ON)
set(TGFX_USE_SWIFTSHADER ON)
set(TGFX_USE_FREETYPE ON)

set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../ ./tgfx)

file(GLOB_RECURSE HELLO_2D_SOURCE_FILES src/*.*)
add_executable(Hello2D ${HELLO_2D_SOURCE_FILES})
target_include_directories(Hello2D PRIVATE ${HELLO_2D_INCLUDES} src)
target_link_libraries(Hello2D tgfx-drawers tgfx ${HELLO_2D_LIBS})

87 changes: 87 additions & 0 deletions linux/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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 <filesystem>
#include <fstream>
#include "drawers/Drawer.h"
#include "tgfx/gpu/Surface.h"
#include "tgfx/opengl/GLDevice.h"
#include "tgfx/platform/Print.h"

static std::string GetRootPath() {
std::filesystem::path filePath = __FILE__;
auto dir = filePath.parent_path().string();
return std::filesystem::path(dir + "/../..").lexically_normal();
}

static void SaveFile(std::shared_ptr<tgfx::Data> data, const std::string& output) {
std::filesystem::path path = output;
std::filesystem::create_directories(path.parent_path());
std::ofstream out(path);
out.write(reinterpret_cast<const char*>(data->data()),
static_cast<std::streamsize>(data->size()));
out.close();
}

int main() {
auto rootPath = GetRootPath();
drawers::AppHost appHost(720, 720, 2.0f);
auto image = tgfx::Image::MakeFromFile(rootPath + "resources/assets/bridge.jpg");
appHost.addImage("bridge", std::move(image));
auto typeface = tgfx::Typeface::MakeFromPath(rootPath + "resources/font/NotoSansSC-Regular.otf");
appHost.addTypeface("default", std::move(typeface));
typeface = tgfx::Typeface::MakeFromPath(rootPath + "resources/font/NotoColorEmoji.ttf");
appHost.addTypeface("emoji", std::move(typeface));

auto device = tgfx::GLDevice::Make();
if (device == nullptr) {
tgfx::PrintError("Failed to create the Device!");
return -1;
}
auto context = device->lockContext();
if (context == nullptr) {
tgfx::PrintError("Failed to lock the Context!");
return -1;
}
auto surface = tgfx::Surface::Make(context, appHost.width(), appHost.height());
auto canvas = surface->getCanvas();
auto drawerNames = drawers::Drawer::Names();
for (auto& name : drawerNames) {
auto drawer = drawers::Drawer::GetByName(name);
canvas->clear();
drawer->draw(canvas, &appHost);
tgfx::Bitmap bitmap = {};
bitmap.allocPixels(surface->width(), surface->height());
auto pixels = bitmap.lockPixels();
auto success = surface->readPixels(bitmap.info(), pixels);
bitmap.unlockPixels();
if (!success) {
tgfx::PrintError("Failed to readPixels!");
return -1;
}
auto data = bitmap.encode();
if (data == nullptr) {
tgfx::PrintError("Failed to encode bitmap!");
return -1;
}
SaveFile(data, "out/" + name + ".png");
}
device->unlock();
tgfx::PrintLog("All images have been saved to the 'out/' directory");
return 0;
}