diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 7734cc1e..69b58791 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -11,23 +11,33 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Install libraries run: | sudo apt update + sudo apt install clang-format sudo apt install libgles2-mesa-dev libegl1-mesa-dev libxkbcommon-dev libwayland-dev libdrm-dev libgbm-dev libinput-dev libudev-dev libsystemd-dev wayland-protocols libx11-dev + - name: Verify formatting + run: | + find src/flutter/shell/platform/linux_embedded/ -iname *.h -o -iname *.cc | xargs clang-format --dry-run --Werror + find src/client_wrapper -iname *.h -o -iname *.cc | xargs clang-format --dry-run --Werror + - name: Create Build Environment run: cmake -E make_directory ${{github.workspace}}/build - name: Install Flutter Engine library + #run: | + # echo `curl https://raw.githubusercontent.com/flutter/flutter/master/bin/internal/engine.version` > embedder.version + # export FLUTTER_ENGINE=`cat embedder.version` + # curl -O https://storage.googleapis.com/flutter_infra/flutter/${FLUTTER_ENGINE}/linux-x64/linux-x64-embedder + # ls + # unzip linux-x64-embedder + # mv libflutter_engine.so ${{github.workspace}}/build run: | - echo `curl https://raw.githubusercontent.com/flutter/flutter/master/bin/internal/engine.version` > embedder.version - export FLUTTER_ENGINE=`cat embedder.version` - curl -O https://storage.googleapis.com/flutter_infra/flutter/${FLUTTER_ENGINE}/linux-x64/linux-x64-embedder - ls - unzip linux-x64-embedder + curl -L https://github.com/sony/flutter-embedded-linux/releases/latest/download/elinux-x64-release.zip > elinux-x64-release.zip + unzip elinux-x64-release.zip mv libflutter_engine.so ${{github.workspace}}/build - name: Configure CMake for wayland client @@ -70,3 +80,13 @@ jobs: working-directory: ${{github.workspace}}/build shell: bash run: cmake --build . --config $BUILD_TYPE + + - name: Configure CMake to build libflutter_elinux.so + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DBUILD_ELINUX_SO=ON -DBACKEND_TYPE=WAYLAND .. + + - name: Build libflutter_elinux.so + working-directory: ${{github.workspace}}/build + shell: bash + run: cmake --build . --config $BUILD_TYPE diff --git a/.gitignore b/.gitignore index 4b17e1c8..d199025c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode/ /build/ +/src/third_party/wayland/protocols/* \ No newline at end of file diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 00000000..6a816263 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,20 @@ +# Below is a list of people and organizations that have contributed +# to sony/flutter-embedded-linux. Names should be added to the list +# like so: +# +# Name/Organization + +Sony Group Corporation +Hidenori Matsubayashi (hidenori.matsubayashi@gmail.com) +Andrea Daoud (andreadaoud6@gmail.com) +Valentin Hăloiu (valentin.haloiu@gmail.com) +FlafyDev +Makoto Sato (makoto.sato@atmark-techno.com) +Yunhao Tian (t123yh@outlook.com) +Luke Howard +Stanislav Shmarov +Sebastian Urban +Ómar Högni Guðmarsson +Athaariq Ardhiansyah +Anton Sakhon +Bari Rao diff --git a/CMakeLists.txt b/CMakeLists.txt index 99b8cfd5..a467410a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,23 +1,49 @@ cmake_minimum_required(VERSION 3.10) -project("flutter_linuxes" LANGUAGES CXX C) # We only support clang, so the build will be faild if you try to build with gcc compiler instead of it. if(NOT DEFINED CMAKE_TOOLCHAIN_FILE) - set(CMAKE_C_COMPILER clang) - set(CMAKE_CXX_COMPILER clang++) + set(CMAKE_C_COMPILER clang) + set(CMAKE_CXX_COMPILER clang++) endif() set(CMAKE_CXX_STANDARD 17) +project("flutter_elinux" LANGUAGES CXX C) + # Build options. -option(BACKEND_TYPE "Select WAYLAND, DRM-GBM, DRM-EGLSTREAM, or X11 as the display backend type" WAYLAND) -option(DESKTOP_SHELL "Work as weston desktop-shell" OFF) -option(USE_VIRTUAL_KEYBOARD "Use virtual keyboard" OFF) +set(BACKEND_TYPE "WAYLAND" CACHE STRING "Select WAYLAND, DRM-GBM, DRM-EGLSTREAM, or X11 as the display backend type") +set_property(CACHE BACKEND_TYPE PROPERTY STRINGS "WAYLAND" "DRM-GBM" "DRM-EGLSTREAM" "X11") +# Disabled USE_DIRTY_REGION_MANAGEMENT due flicker issue. +# See https://github.com/sony/flutter-embedded-linux/issues/334 +option(USE_DIRTY_REGION_MANAGEMENT "Use Flutter dirty region management" OFF) option(USE_GLES3 "Use OpenGL ES3 (default is OpenGL ES2)" OFF) +option(ENABLE_EGL_ALPHA_COMPONENT_OF_COLOR_BUFFER "Enable alpha component of the EGL color buffer" ON) + # todo: need to investigate https://github.com/sony/flutter-embedded-linux/pull/376 when enabling this option. +option(ENABLE_VSYNC "Enable embedder vsync" OFF) +option(BUILD_ELINUX_SO "Build .so file of elinux embedder" OFF) +option(ENABLE_ELINUX_EMBEDDER_LOG "Enable logger of eLinux embedder" ON) +option(FLUTTER_RELEASE "Build Flutter Engine with release mode" OFF) -# Load the user project. -set(USER_PROJECT_PATH "examples/flutter-wayland-client" CACHE STRING "") -message("User project: ${USER_PROJECT_PATH}") -include(${USER_PROJECT_PATH}/cmake/user_config.cmake) +if(NOT BUILD_ELINUX_SO) + # Load the user project. + set(USER_PROJECT_PATH "" CACHE STRING "examples/flutter-wayland-client") + message("User project: ${USER_PROJECT_PATH}") + if(NOT ${BACKEND_TYPE} STREQUAL "WAYLAND") + message(WARNING "BACKEND_TYPE variable is ignored because BUILD_ELINUX_SO is OFF") + endif() + include(${USER_PROJECT_PATH}/cmake/user_config.cmake) +else() + # Set the filename of elinux .so file + if(${BACKEND_TYPE} STREQUAL "DRM-GBM") + set(TARGET_SUFFIX "gbm") + elseif(${BACKEND_TYPE} STREQUAL "DRM-EGLSTREAM") + set(TARGET_SUFFIX "eglstream") + elseif(${BACKEND_TYPE} STREQUAL "X11") + set(TARGET_SUFFIX "x11") + else() + set(TARGET_SUFFIX "wayland") + endif() + set(TARGET "flutter_elinux_${TARGET_SUFFIX}") +endif() # System level dependent library. include(cmake/package.cmake) @@ -28,3 +54,6 @@ include(cmake/rapidjson.cmake) # Build for target. include(cmake/build.cmake) + +# Install the bundle. +include(cmake/install.cmake) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..4f2807de --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,2 @@ +# Contributing to flutter-embedded-linux +Welcome to this project. We welcome all your contribution and feedback. If you've never submitted code before, you must add your (or your organization's) name and contact info to the [AUTHORS](./AUTHORS) file. diff --git a/LICENSE b/LICENSE index 1d33397b..9feb6cc7 100644 --- a/LICENSE +++ b/LICENSE @@ -9,7 +9,7 @@ are permitted provided that the following conditions are met: copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the name of Google Inc. nor the names of its + * Neither the name of the Flutter Authors nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. diff --git a/README.md b/README.md index 0ef6b97c..c5c6bd64 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,18 @@ -# Embedded Linux embedding for Flutter +# Embedded Linux (eLinux) embedding for Flutter +![image](https://github.com/sony/flutter-elinux/blob/main/doc/images/overview.png) + [![build-test](https://github.com/sony/flutter-embedded-linux/actions/workflows/build-test.yml/badge.svg)](https://github.com/sony/flutter-embedded-linux/actions/workflows/build-test.yml) -This project was created to develop **non-official** embedded Linux embeddings of [Flutter](https://flutter.dev/). This embedder is focusing on embedded Linux system use cases. It is also implemented based on Flutter desktop for Windows and has some unique features to use it in embedded systems. +This project was created to develop **non-official** embedded Linux embeddings of [Flutter](https://flutter.dev/). This embedder is focusing on embedded Linux (eLinux) system use cases. It is also implemented based on Flutter desktop for Windows and has some unique features to use it in embedded systems. + +If you develop flutter apps for eLinux, use [flutter-elinux](https://github.com/sony/flutter-elinux), which is a non-official extension to the [Flutter SDK](https://github.com/flutter/flutter) to build and debug Flutter apps for embedded Linux devices. + +### Repositories + +- [flutter-elinux](https://github.com/sony/flutter-elinux): Flutter tools for eLinux +- [flutter-elinux-plugins](https://github.com/sony/flutter-elinux-plugins): Flutter plugins for eLinux +- [flutter-embedded-linux](https://github.com/sony/flutter-embedded-linux): eLinux embedding for Flutter +- [meta-flutter](https://github.com/sony/meta-flutter): Yocto recipes of eLinux embedding for Flutter ## Objective & Goal Our objective is to use Flutter in embedded systems. We're developing this embedder to use Flutter in embedded products. Ultimately we would like to propose and contribute this software to the mainline of [Flutter Engine](https://github.com/flutter/engine), which means we would like to add an embedded systems version into the Flutter repo for all embedded developers. Please note that this is just our ideal, not the official opinion of the Flutter community. @@ -10,8 +21,8 @@ We would be grateful if you could give us feedback on bugs and new feature reque ## Features - Flutter embedder optimized for Embedded Systems - - Minimal dependent libraries - Lightweight than Flutter desktop for Linux (Not using X11 and GTK) + - Minimal dependent libraries - The main target of this embedder is Arm64 devices. We haven't confirmed in Arm 32bit (ARMv7, armhf) devices - Display backend support - [Wayland](https://wayland.freedesktop.org/) @@ -25,42 +36,8 @@ We would be grateful if you could give us feedback on bugs and new feature reque - API compatibility with Flutter desktop for Windows and GLFW - APIs such as MethodChannel and EventChannel are completely the same with them -## Supported platforms -This embedder supports x64 and Arm64 (aarch64, ARMv8) architectures on Linux which supports either Wayland backend or DRM backend. - -### Tested devices - -| Board / SoC | Vendor | OS / BSP | Backend | Status | -| :-------------: | :-------------: | :-------------: | :-------------: | :-------------: | -| Desktop (x86_64) | Intel | Ubuntu 18.04 | Wayland | :heavy_check_mark: | -| Desktop (x86_64) | Intel | Ubuntu 18.04 | DRM | :heavy_check_mark: | -| Desktop (x86_64) | Intel | Ubuntu 18.04 | X11 | :heavy_check_mark: | -| QEMU (x86_64) | QEMU | [AGL (Automotive Grade Linux)](https://wiki.automotivelinux.org/) koi | Wayland | :heavy_check_mark: | -| QEMU (x86_64) | QEMU | [AGL (Automotive Grade Linux)](https://wiki.automotivelinux.org/) koi | DRM | :heavy_check_mark: | -| [Jetson Nano](https://developer.nvidia.com/embedded/jetson-nano-developer-kit) | NVIDIA | JetPack 4.3 | Wayland | :heavy_check_mark: | -| [Jetson Nano](https://developer.nvidia.com/embedded/jetson-nano-developer-kit) | NVIDIA | JetPack 4.3 | DRM | :heavy_check_mark: ([#1](https://github.com/sony/flutter-embedded-linux/issues/1))| -| [Raspberry Pi 4 Model B](https://www.raspberrypi.org/products/raspberry-pi-4-model-b/) | Raspberry Pi Foundation | Ubuntu 20.10 | Wayland | :heavy_check_mark: | -| [Raspberry Pi 4 Model B](https://www.raspberrypi.org/products/raspberry-pi-4-model-b/) | Raspberry Pi Foundation | Ubuntu 20.10 | DRM | :heavy_check_mark: ([#9](https://github.com/sony/flutter-embedded-linux/issues/9)) | -| [i.MX 8MQuad EVK](https://www.nxp.com/design/development-boards/i-mx-evaluation-and-development-boards/evaluation-kit-for-the-i-mx-8m-applications-processor:MCIMX8M-EVK) | NXP | Sumo (kernel 4.14.98) | Wayland | :heavy_check_mark: | -| [i.MX 8M Mini EVKB](https://www.nxp.com/design/development-boards/i-mx-evaluation-and-development-boards/evaluation-kit-for-the-i-mx-8m-mini-applications-processor:8MMINILPD4-EVK) | NXP | Zeus (kernel 5.4.70) | Wayland | :heavy_check_mark: | -| [RB5 Development Kit](https://developer.qualcomm.com/qualcomm-robotics-rb5-kit) | Qualcomm | Ubuntu 18.04.05 | DRM | :heavy_check_mark: | -| Zynq | Xilinx | - | - | Not tested | - -Note - - i.MX 8M platforms don't support applications using EGL on GBM, which means the DRM-GBM backend won't work on i.MX 8M devices. - -### Tested Wayland compositors -||||||||||| -| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | -[Weston](https://gitlab.freedesktop.org/wayland/weston/-/blob/master/README.md) | :heavy_check_mark: | [Sway](https://swaywm.org/) | :heavy_check_mark: | [Wayfire](https://wayfire.org/) | :heavy_check_mark: | [Gnome](https://www.gnome.org/) | :heavy_check_mark: | [Phosh](https://source.puri.sm/Librem5/phosh) | :heavy_check_mark: | -| [Cage](https://www.hjdskes.nl/projects/cage/) | :heavy_check_mark: | [Lomiri](https://lomiri.com/) | :heavy_check_mark: | [Plasma Wayland](https://community.kde.org/Plasma/Wayland) | :heavy_check_mark: | [Plasma Mobile](https://www.plasma-mobile.org/) | :heavy_check_mark: | [GlacierUX](https://wiki.merproject.org/wiki/Nemo/Glacier) | :heavy_check_mark: | - -## Contributing -**Now, we cannot accept any Pull Request (PR).** Because We are building a system (e.g. CLA) to accept PRs, so please wait for a while the system is getting ready! However, we are always welcome to report bugs and request new features by creating issues. - -With the assumption, our final goal of this software openly is to be merged this embedder into [Flutter Engine](https://github.com/flutter/engine) after getting feedbacks. And [Google CLA](https://cla.developers.google.com/about/google-corporate) will be required when we do that in the future. Therefore, we cannot easily accept an external PR. However, you can free to create issues for reporting bugs and requesting new features. - -See also: [Contributing to the Flutter engine](https://github.com/flutter/engine/blob/master/CONTRIBUTING.md) - ## Documentation -Documentation for this embedder is under the [doc](./doc/README.md) directory. +Documentation for this software can be found at [Wiki](https://github.com/sony/flutter-embedded-linux/wiki). + +## Supported platforms +This embedder supports x64 and Arm64 (aarch64, ARMv8) architectures on Linux which supports either Wayland backend or DRM backend. See [Support status](https://github.com/sony/flutter-elinux/wiki/Support-status) for details. diff --git a/cmake/build.cmake b/cmake/build.cmake index c75a299a..ad69502b 100644 --- a/cmake/build.cmake +++ b/cmake/build.cmake @@ -18,64 +18,78 @@ endif() set(DISPLAY_BACKEND_SRC "") if(${BACKEND_TYPE} STREQUAL "DRM-GBM") add_definitions(-DDISPLAY_BACKEND_TYPE_DRM_GBM) + add_definitions(-DFLUTTER_TARGET_BACKEND_GBM) set(DISPLAY_BACKEND_SRC - src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc - src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.cc) + "src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc" + "src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.cc") elseif(${BACKEND_TYPE} STREQUAL "DRM-EGLSTREAM") add_definitions(-DDISPLAY_BACKEND_TYPE_DRM_EGLSTREAM) + add_definitions(-DFLUTTER_TARGET_BACKEND_EGLSTREAM) set(DISPLAY_BACKEND_SRC - src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.cc - src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.cc - src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc - src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.cc) + "src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.cc" + "src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.cc" + "src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc" + "src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.cc") elseif(${BACKEND_TYPE} STREQUAL "X11") add_definitions(-DDISPLAY_BACKEND_TYPE_X11) + add_definitions(-DFLUTTER_TARGET_BACKEND_X11) set(DISPLAY_BACKEND_SRC - src/flutter/shell/platform/linux_embedded/window/linuxes_window_x11.cc - src/flutter/shell/platform/linux_embedded/window/native_window_x11.cc) + "src/flutter/shell/platform/linux_embedded/window/elinux_window_x11.cc" + "src/flutter/shell/platform/linux_embedded/window/native_window_x11.cc") else() include(cmake/generate_wayland_protocols.cmake) - set(_wayland_protocols_xml_dir $ENV{PKG_CONFIG_SYSROOT_DIR}/usr/share/wayland-protocols) - set(_wayland_protocols_src_dir ${CMAKE_CURRENT_SOURCE_DIR}/src/wayland/protocol) + pkg_get_variable(WAYLAND_PROTOCOLS_DATADIR wayland-protocols pkgdatadir) + set(_wayland_protocols_xml_dir "${WAYLAND_PROTOCOLS_DATADIR}") + set(_wayland_protocols_src_dir "${CMAKE_CURRENT_SOURCE_DIR}/src/third_party/wayland/protocols") + file(MAKE_DIRECTORY "${_wayland_protocols_src_dir}") generate_wayland_client_protocol( - PROTOCOL_FILE ${_wayland_protocols_xml_dir}/stable/xdg-shell/xdg-shell.xml - CODE_FILE ${_wayland_protocols_src_dir}/xdg-shell-protocol.c - HEADER_FILE ${_wayland_protocols_src_dir}/xdg-shell-client-protocol.h) + PROTOCOL_FILE "${_wayland_protocols_xml_dir}/stable/xdg-shell/xdg-shell.xml" + CODE_FILE "${_wayland_protocols_src_dir}/xdg-shell-protocol.c" + HEADER_FILE "${_wayland_protocols_src_dir}/xdg-shell-client-protocol.h") generate_wayland_client_protocol( - PROTOCOL_FILE ${_wayland_protocols_xml_dir}/unstable/text-input/text-input-unstable-v1.xml - CODE_FILE ${_wayland_protocols_src_dir}/text-input-unstable-v1-protocol.c - HEADER_FILE ${_wayland_protocols_src_dir}/text-input-unstable-v1-client-protocol.h) + PROTOCOL_FILE "${_wayland_protocols_xml_dir}/unstable/text-input/text-input-unstable-v1.xml" + CODE_FILE "${_wayland_protocols_src_dir}/text-input-unstable-v1-protocol.c" + HEADER_FILE "${_wayland_protocols_src_dir}/text-input-unstable-v1-client-protocol.h") generate_wayland_client_protocol( - PROTOCOL_FILE ${_wayland_protocols_xml_dir}/unstable/text-input/text-input-unstable-v3.xml - CODE_FILE ${_wayland_protocols_src_dir}/text-input-unstable-v3-protocol.c - HEADER_FILE ${_wayland_protocols_src_dir}/text-input-unstable-v3-client-protocol.h) + PROTOCOL_FILE "${_wayland_protocols_xml_dir}/unstable/text-input/text-input-unstable-v3.xml" + CODE_FILE "${_wayland_protocols_src_dir}/text-input-unstable-v3-protocol.c" + HEADER_FILE "${_wayland_protocols_src_dir}/text-input-unstable-v3-client-protocol.h") - add_definitions(-DDISPLAY_BACKEND_TYPE_WAYLAND) - set(DISPLAY_BACKEND_SRC - ${_wayland_protocols_src_dir}/xdg-shell-protocol.c - ${_wayland_protocols_src_dir}/text-input-unstable-v1-protocol.c - ${_wayland_protocols_src_dir}/text-input-unstable-v3-protocol.c - src/flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.cc - src/flutter/shell/platform/linux_embedded/window/native_window_wayland.cc) -endif() + generate_wayland_client_protocol( + PROTOCOL_FILE "${_wayland_protocols_xml_dir}/stable/presentation-time/presentation-time.xml" + CODE_FILE "${_wayland_protocols_src_dir}/presentation-time-protocol.c" + HEADER_FILE "${_wayland_protocols_src_dir}/presentation-time-protocol.h") -# desktop-shell for weston. -if((${BACKEND_TYPE} STREQUAL "WAYLAND") AND DESKTOP_SHELL) - add_definitions(-DDESKTOP_SHELL) -endif() + generate_wayland_client_protocol( + PROTOCOL_FILE "${_wayland_protocols_xml_dir}/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml" + CODE_FILE "${_wayland_protocols_src_dir}/xdg-decoration-unstable-v1-protocol.c" + HEADER_FILE "${_wayland_protocols_src_dir}/xdg-decoration-unstable-v1-protocol.h") -# weston private protocols. -set(WAYLAND_PROTOCOL_SRC "") -if((${BACKEND_TYPE} STREQUAL "WAYLAND") AND DESKTOP_SHELL) - set(WAYLAND_PROTOCOL_SRC ${WAYLAND_PROTOCOL_SRC} src/wayland/protocol/weston-desktop-shell-protocol.c) + add_definitions(-DFLUTTER_TARGET_BACKEND_WAYLAND) + add_definitions(-DDISPLAY_BACKEND_TYPE_WAYLAND) + set(DISPLAY_BACKEND_SRC + "${_wayland_protocols_src_dir}/xdg-shell-protocol.c" + "${_wayland_protocols_src_dir}/text-input-unstable-v1-protocol.c" + "${_wayland_protocols_src_dir}/text-input-unstable-v3-protocol.c" + "${_wayland_protocols_src_dir}/presentation-time-protocol.c" + "${_wayland_protocols_src_dir}/xdg-decoration-unstable-v1-protocol.c" + "src/flutter/shell/platform/linux_embedded/window/elinux_window_wayland.cc" + "src/flutter/shell/platform/linux_embedded/window/native_window_wayland.cc" + "src/flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.cc" + "src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.cc" + "src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.cc" + "src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.cc" + "src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.cc" + "src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.cc" + "src/flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.cc") endif() -# Use virtual keybard(on-screen keyboard). -if(USE_VIRTUAL_KEYBOARD) - add_definitions(-DUSE_VIRTUAL_KEYBOARD) +# Use flutter dirty region management +if(USE_DIRTY_REGION_MANAGEMENT) + add_definitions(-DUSE_OPENGL_DIRTY_REGION_MANAGEMENT) endif() # OpenGL ES version. @@ -84,62 +98,129 @@ if(USE_GLES3) endif() # Flutter embedder runtime mode. -if(NOT CMAKE_BUILD_TYPE MATCHES Debug) +if(FLUTTER_RELEASE) add_definitions( -DFLUTTER_RELEASE # release mode ) endif() -# cmake script for developers. -include(${USER_PROJECT_PATH}/cmake/user_build.cmake) - -add_executable(${TARGET} - ${USER_APP_SRCS} - src/client_wrapper/flutter_engine.cc - src/client_wrapper/flutter_view_controller.cc - src/flutter/shell/platform/linux_embedded/flutter_linuxes.cc - src/flutter/shell/platform/linux_embedded/flutter_linuxes_engine.cc - src/flutter/shell/platform/linux_embedded/flutter_linuxes_view.cc - src/flutter/shell/platform/linux_embedded/flutter_project_bundle.cc - src/flutter/shell/platform/linux_embedded/task_runner.cc - src/flutter/shell/platform/linux_embedded/system_utils.cc - src/flutter/shell/platform/linux_embedded/logger.cc - src/flutter/shell/platform/linux_embedded/external_texture_gl.cc - src/flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.cc - src/flutter/shell/platform/linux_embedded/plugin/key_event_plugin.cc - src/flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.cc - src/flutter/shell/platform/linux_embedded/plugin/text_input_plugin.cc - src/flutter/shell/platform/linux_embedded/plugin/platform_plugin.cc - src/flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.cc - src/flutter/shell/platform/linux_embedded/surface/context_egl.cc - src/flutter/shell/platform/linux_embedded/surface/egl_utils.cc - src/flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.cc - src/flutter/shell/platform/linux_embedded/surface/linuxes_surface.cc - src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.cc - ${DISPLAY_BACKEND_SRC} - ${WAYLAND_PROTOCOL_SRC} +# Enable logger of eLinux embedder. +if(ENABLE_ELINUX_EMBEDDER_LOG) + add_definitions( + -DENABLE_ELINUX_EMBEDDER_LOG + ) +endif() + +# Enable embedder vsync. +if(ENABLE_VSYNC) + add_definitions( + -DENABLE_VSYNC + ) +endif() + +# Enable alpha component of the egl color buffer. +if(ENABLE_EGL_ALPHA_COMPONENT_OF_COLOR_BUFFER) + add_definitions( + -DENABLE_EGL_ALPHA_COMPONENT_OF_COLOR_BUFFER + ) +endif() + +set(CPP_WRAPPER_SOURCES_CORE + "src/flutter/shell/platform/common/client_wrapper/engine_method_result.cc" + "src/flutter/shell/platform/common/client_wrapper/standard_codec.cc" + "src/flutter/shell/platform/common/client_wrapper/string_message_codec.cc" +) +set(CPP_WRAPPER_SOURCES_PLUGIN + "src/flutter/shell/platform/common/client_wrapper/plugin_registrar.cc" +) +set(CPP_WRAPPER_SOURCES_APP + "src/client_wrapper/flutter_engine.cc" + "src/client_wrapper/flutter_view_controller.cc" +) + +set(ELINUX_COMMON_SRC + "src/flutter/shell/platform/linux_embedded/flutter_elinux.cc" + "src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.cc" + "src/flutter/shell/platform/linux_embedded/flutter_elinux_view.cc" + "src/flutter/shell/platform/linux_embedded/flutter_project_bundle.cc" + "src/flutter/shell/platform/linux_embedded/task_runner.cc" + "src/flutter/shell/platform/linux_embedded/system_utils.cc" + "src/flutter/shell/platform/linux_embedded/logger.cc" + "src/flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.cc" + "src/flutter/shell/platform/linux_embedded/external_texture_egl_image.cc" + "src/flutter/shell/platform/linux_embedded/vsync_waiter.cc" + "src/flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.cc" + "src/flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.cc" + "src/flutter/shell/platform/linux_embedded/plugins/key_event_plugin.cc" + "src/flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.cc" + "src/flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.cc" + "src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.cc" + "src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.cc" + "src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.cc" + "src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.cc" + "src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.cc" + "src/flutter/shell/platform/linux_embedded/surface/context_egl.cc" + "src/flutter/shell/platform/linux_embedded/surface/egl_utils.cc" + "src/flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.cc" + "src/flutter/shell/platform/linux_embedded/surface/surface_base.cc" + "src/flutter/shell/platform/linux_embedded/surface/surface_gl.cc" + "src/flutter/shell/platform/linux_embedded/surface/surface_decoration.cc" + "${DISPLAY_BACKEND_SRC}" ## The following file were copied from: ## https://github.com/flutter/engine/blob/master/shell/platform/glfw/ - src/flutter/shell/platform/linux_embedded/system_utils.cc + "src/flutter/shell/platform/linux_embedded/system_utils.cc" ## Following files were imported from: ## https://github.com/flutter/engine/tree/master/shell/platform/common - src/flutter/shell/platform/common/client_wrapper/engine_method_result.cc - src/flutter/shell/platform/common/client_wrapper/standard_codec.cc - src/flutter/shell/platform/common/client_wrapper/plugin_registrar.cc - src/flutter/shell/platform/common/text_input_model.cc - src/flutter/shell/platform/common/json_message_codec.cc - src/flutter/shell/platform/common/json_method_codec.cc - src/flutter/shell/platform/common/engine_switches.cc - src/flutter/shell/platform/common/incoming_message_dispatcher.cc + "src/flutter/shell/platform/common/text_input_model.cc" + "src/flutter/shell/platform/common/json_message_codec.cc" + "src/flutter/shell/platform/common/json_method_codec.cc" + "src/flutter/shell/platform/common/engine_switches.cc" + "src/flutter/shell/platform/common/incoming_message_dispatcher.cc" ) -set(THIRD_PARTY_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/third_party) +if(NOT BUILD_ELINUX_SO) + # cmake script for developers. + include(${USER_PROJECT_PATH}/cmake/user_build.cmake) + + add_executable(${TARGET} + ${ELINUX_COMMON_SRC} + ${CPP_WRAPPER_SOURCES_CORE} + ${CPP_WRAPPER_SOURCES_PLUGIN} + ${CPP_WRAPPER_SOURCES_APP} + ${USER_APP_SRCS} + ) +else() + add_library(${TARGET} + SHARED + ${ELINUX_COMMON_SRC} + ${CPP_WRAPPER_SOURCES_CORE} + ${CPP_WRAPPER_SOURCES_PLUGIN} + ) + + target_include_directories(${TARGET} + PUBLIC + "src/flutter/shell/platform/linux_embedded/public" + ) + + target_include_directories(${TARGET} + PRIVATE + "src/client_wrapper/include" + "src/flutter/shell/platform/common/client_wrapper" + "src/flutter/shell/platform/common/client_wrapper/include" + "src/flutter/shell/platform/common/client_wrapper/include/flutter" + "src/flutter/shell/platform/common/public" + ) +endif() + +set(THIRD_PARTY_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src/third_party") +set(RAPIDJSON_INCLUDE_DIRS "${THIRD_PARTY_DIRS}/rapidjson/include/") target_include_directories(${TARGET} PRIVATE - src + "src" ## third-party libraries. + ${THIRD_PARTY_DIRS} + ${RAPIDJSON_INCLUDE_DIRS} ${XKBCOMMON_INCLUDE_DIRS} - ${THIRD_PARTY_INCLUDE_DIR} ${WAYLAND_CLIENT_INCLUDE_DIRS} ${WAYLAND_CURSOR_INCLUDE_DIRS} ${WAYLAND_EGL_INCLUDE_DIRS} @@ -156,8 +237,6 @@ target_include_directories(${TARGET} ${USER_APP_INCLUDE_DIRS} ) -set(CMAKE_SKIP_RPATH true) -set(FLUTTER_EMBEDDER_LIB ${CMAKE_CURRENT_SOURCE_DIR}/build/libflutter_engine.so) target_link_libraries(${TARGET} PRIVATE ${XKBCOMMON_LIBRARIES} @@ -170,6 +249,7 @@ target_link_libraries(${TARGET} ${LIBINPUT_LIBRARIES} ${LIBUDEV_LIBRARIES} ${LIBSYSTEMD_LIBRARIES} + ${LIBUV_LIBRARIES} ${X11_LIBRARIES} ${LIBWESTON_LIBRARIES} ${FLUTTER_EMBEDDER_LIB} @@ -177,14 +257,31 @@ target_link_libraries(${TARGET} ${USER_APP_LIBRARIES} ) -if(${BACKEND_TYPE} MATCHES "DRM-(GBM|EGLSTREAM)") -target_link_libraries(${TARGET} +if(${BACKEND_TYPE} MATCHES "^DRM-(GBM|EGLSTREAM)$") + target_link_libraries(${TARGET} PRIVATE Threads::Threads -) + ) + + # Indicate whether libsystemd must replace libuv + if("${LIBSYSTEMD_FOUND}" STREQUAL "1") + add_definitions(-DUSE_LIBSYSTEMD) + endif() endif() +set(FLUTTER_EMBEDDER_LIB "${CMAKE_CURRENT_SOURCE_DIR}/build/libflutter_engine.so") +set(CMAKE_SKIP_RPATH true) +target_link_libraries(${TARGET} + PRIVATE + ${FLUTTER_EMBEDDER_LIB} +) + target_compile_options(${TARGET} PUBLIC ${EGL_CFLAGS} ) + +if(NOT BUILD_ELINUX_SO) + # Generated plugin build rules + include(${USER_PROJECT_PATH}/flutter/generated_plugins.cmake) +endif() diff --git a/cmake/cross-toolchain-aarch64-template.cmake b/cmake/cross-toolchain-aarch64-template.cmake index 0be57f16..d766d449 100644 --- a/cmake/cross-toolchain-aarch64-template.cmake +++ b/cmake/cross-toolchain-aarch64-template.cmake @@ -3,11 +3,12 @@ cmake_minimum_required(VERSION 3.10) set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR aarch64) -# Specify the sysroot. You need to modify appropriately for your environment +# Specify the sysroot. +# You need to modify appropriately for your environment. set(target_sysroot ) set(CMAKE_SYSROOT ${target_sysroot}) -# Specify the cross compiler +# Specify the cross compiler. set(triple aarch64-linux-gnu) set(CMAKE_C_COMPILER_TARGET ${triple}) set(CMAKE_C_COMPILER clang) @@ -19,4 +20,3 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) - diff --git a/cmake/cross-toolchain-aarch64-yocto-template.cmake b/cmake/cross-toolchain-aarch64-yocto-template.cmake index d6d69ed0..3bdc0dad 100644 --- a/cmake/cross-toolchain-aarch64-yocto-template.cmake +++ b/cmake/cross-toolchain-aarch64-yocto-template.cmake @@ -3,9 +3,12 @@ cmake_minimum_required(VERSION 3.10) set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR aarch64) -# Specify the sysroot. You need to modify appropriately for your environment +# Specify the sysroot. +# You need to modify appropriately for your environment. set(target_sysroot "") -# Specify the cross compiler. You need to modify appropriately for your environment + +# Specify the cross compiler. +# You need to modify appropriately for your environment. set(toolchain "") set(CMAKE_SYSROOT ${target_sysroot}) @@ -21,4 +24,3 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) - diff --git a/cmake/install.cmake b/cmake/install.cmake new file mode 100644 index 00000000..6711f4dc --- /dev/null +++ b/cmake/install.cmake @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.10) + +set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) +endif() + +# Start with a clean build bundle directory every time. +install(CODE " + file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") + " COMPONENT Runtime) + +set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") + +install(FILES "${FLUTTER_EMBEDDER_LIB}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime +) + +if(PLUGIN_BUNDLED_LIBRARIES) + install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime + ) +endif() diff --git a/cmake/package.cmake b/cmake/package.cmake index 055e7739..7a2ebcaa 100644 --- a/cmake/package.cmake +++ b/cmake/package.cmake @@ -9,17 +9,26 @@ pkg_check_modules(EGL REQUIRED egl) pkg_check_modules(XKBCOMMON REQUIRED xkbcommon) # depends on backend type. -if(${BACKEND_TYPE} MATCHES "DRM-(GBM|EGLSTREAM)") +if(${BACKEND_TYPE} MATCHES "^DRM-(GBM|EGLSTREAM)$") # DRM backend pkg_check_modules(DRM REQUIRED libdrm) pkg_check_modules(LIBINPUT REQUIRED libinput) pkg_check_modules(LIBUDEV REQUIRED libudev) - pkg_check_modules(LIBSYSTEMD REQUIRED libsystemd) + pkg_check_modules(LIBSYSTEMD libsystemd) + pkg_check_modules(LIBUV libuv) if(${BACKEND_TYPE} STREQUAL "DRM-GBM") pkg_check_modules(GBM REQUIRED gbm) endif() set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) + # Check if either systemd or libuv exist + if((NOT "${LIBSYSTEMD_FOUND}" STREQUAL "1") AND (NOT "${LIBUV_FOUND}" STREQUAL "1")) + message(FATAL_ERROR + "${BACKEND_TYPE} backend requires either libsystemd or libuv, but + they're not exist.") + elseif(("${LIBSYSTEMD_FOUND}" STREQUAL "1") AND ("${LIBUV_FOUND}" STREQUAL "1")) + message("!! NOTICE: libsystemd found, libuv won't be used.") + endif() elseif(${BACKEND_TYPE} STREQUAL "X11") pkg_check_modules(X11 REQUIRED x11) else() @@ -28,13 +37,6 @@ else() pkg_check_modules(WAYLAND_CLIENT REQUIRED wayland-client>=1.16.0) pkg_check_modules(WAYLAND_CURSOR REQUIRED wayland-cursor>=1.16.0) pkg_check_modules(WAYLAND_EGL REQUIRED wayland-egl>=1.16.0) - - if(DESKTOP_SHELL) - pkg_check_modules(WESTON REQUIRED weston>=8.0.0) - string(REPLACE "." ";" WESTON_VERSION_LIST ${WESTON_VERSION}) - list(GET WESTON_VERSION_LIST 0 WESTON_VERSION_MAJOR) - pkg_check_modules(LIBWESTON REQUIRED libweston-${WESTON_VERSION_MAJOR}) - endif() endif() # requires for supporting external texture plugin. diff --git a/doc/README.md b/doc/README.md deleted file mode 100644 index 138d2054..00000000 --- a/doc/README.md +++ /dev/null @@ -1,261 +0,0 @@ -# Documentation - -## 1. Install libraries -You need to install the following dependent libraries to build and run. Here introduce how to install the libraries on Debian-based systems like Ubuntu. - -### Mandatory -- clang (for building) -- cmake (for building) -- build-essential (for building) -- pkg-config (for building) -- EGL -- xkbcommon -- OpenGL ES (>=2.0) - -```Shell -$ sudo apt install clang cmake build-essential pkg-config libegl1-mesa-dev libxkbcommon-dev libgles2-mesa-dev -``` - -### Only when using Wayland backend -- libwayland -- wayland-protocols (for generating xdg-shell source files) - -```Shell -$ sudo apt install libwayland-dev wayland-protocols -``` - -### Only when using weston desktop-shell -- weston (>=8.0.0) -- libweston-*-dev - -```Shell -$ sudo apt install weston libweston-8-dev -``` - -### Only when using DRM backend -- libdrm -- libgbm -- libinput -- libudev -- libsystemd - -```Shell -$ sudo apt install libdrm-dev libgbm-dev libinput-dev libudev-dev libsystemd-dev -``` - -### Only when using x11 backend -- x11 - -```Shell -$ sudo apt install libx11-dev -``` - -### Install Flutter Engine library -This embedder requres `libflutter_engine.so` (Flutter embedder library). You need to install it in `` to build. See: [Building Flutter Engine embedder](./building-engine-embedder.md) - -Or you can download a specific pre-built Flutter Engine from Google's infra by the following steps, but it's limited to **debug mode** and **x64** targets. - -Step 1) Check the version (SHA) of the channel you want to use. -- [master channel](https://raw.githubusercontent.com/flutter/flutter/master/bin/internal/engine.version) -- [dev channel](https://raw.githubusercontent.com/flutter/flutter/dev/bin/internal/engine.version) -- [beta channel](https://raw.githubusercontent.com/flutter/flutter/beta/bin/internal/engine.version) -- [stable channel](https://raw.githubusercontent.com/flutter/flutter/stable/bin/internal/engine.version) - -You can also get the version from `${path_to_flutter_sdk_install}/flutter/bin/internal/engine.version` of the Flutter SDK which you are currently using. - -Step 2) Download Flutter Engine embedder library. Note that replace `FLUTTER_ENGINE` with the SHA of the Flutter engine you want to use. -```Shell -$ curl -O https://storage.googleapis.com/flutter_infra/flutter/FLUTTER_ENGINE/linux-x64/linux-x64-embedder -``` - -Step 3) Install the library. Note that the downloaded library is only **debug mode** and for **x64** targets. -```Shell -$ unzip ./linux-x64-embedder -$ cp ./libflutter_engine.so -``` - -## 2. Examples -There are sample projects in [`examples`](../examples) directory. You can also comunicate with Dart code by using the plugin APIs with the same specifications as with Flutter desktops for Windows. - -## 3. Building - -### 3.1. Self-build - -### Build for Wayland backend (Stand-alone Wayland app) - -```Shell -$ mkdir build -$ cd build -$ cmake -DUSER_PROJECT_PATH=examples/flutter-wayland-client .. -$ cmake --build . -``` - -### Build for Wayland backend (Weston desktop-shell) -This binary will run as a desktop-shell by setting `weston.ini` when Weston starts. See [Settings of weston.ini file](#7-settings-of-westonini-file-only-when-using-weston-desktop-shell). - -```Shell -$ mkdir build -$ cd build -$ cmake -DUSER_PROJECT_PATH=examples/flutter-weston-desktop-shell .. -$ cmake --build . -``` - -### Build for DRM backend - -#### Use GBM - -```Shell -$ mkdir build -$ cd build -$ cmake -DUSER_PROJECT_PATH=examples/flutter-drm-gbm-backend .. -$ cmake --build . -``` - -#### Use EGLStream - -```Shell -$ mkdir build -$ cd build -$ cmake -DUSER_PROJECT_PATH=examples/flutter-drm-eglstream-backend .. -$ cmake --build . -``` - -### Build for x11 backend (Stand-alone X11 app) -Basically, the x11 backend is just only for debugging and developing Flutter apps on desktops. And it's still being implemented now. - -```Shell -$ mkdir build -$ cd build -$ cmake -DUSER_PROJECT_PATH=examples/flutter-x11-client .. -$ cmake --build . -``` - -### 3.2. Cross-build -You need to create a toolchain file to cross compile using the Yocto SDK for aarch64 on x64 hosts. [toolchain-template.cmake](../cmake/cross-toolchain-aarch64-template.cmake) is the templete file for aarch64 toolchain. Also, you need to modify appropriately for your environment if you want to use the template file. -```Shell -$ cmake -DUSER_PROJECT_PATH= -DCMAKE_TOOLCHAIN_FILE= .. -``` - -### 3.3. Yocto-build -If you want to build using Yocto, see: [meta-flutter](../meta-flutter/) - -### How to debug the embedder -You need to build the embedder with `CMAKE_BUILD_TYPE=Debug` option if you want to debug the embedder. Using this option, you can get gather logs and debug them with debuggers such as gdb / lldb. - -```Shell -$ cmake -DUSER_PROJECT_PATH= -DCMAKE_BUILD_TYPE=Debug .. -``` - -### User configuration parameters (CMAKE options) -Please edit `cmake/user_config.cmake` file. - -| Option | Description | -| ------------- | ------------- | -| BACKEND_TYPE | Select WAYLAND, DRM-GBM, DRM-EGLSTREAM, or X11 as the display backend type (The default setting is WAYLAND) | -| DESKTOP_SHELL | Work as Weston desktop-shell | -| USE_VIRTUAL_KEYBOARD | Use Virtual Keyboard (Only available when you choose Wayland backend) | -| USE_GLES3 | Use OpenGLES3 instead of OpenGLES2 | - -## 4. Building Flutter app - -### Install Flutter SDK -See also: [Desktop support for Flutter](https://flutter.dev/desktop) - -```Shell -$ git clone https://github.com/flutter/flutter -$ sudo mv flutter /opt/ -$ export PATH=$PATH:/opt/flutter/bin -$ flutter config --enable-linux-desktop -$ flutter doctor -``` - -Please note that you must use the same version (channel) that you built Flutter embedder for. I recommend that you use the latest version of the master channel for both the SDK and Flutter Engine. -See also: [Building Flutter Engine embedder](./building-engine-embedder.md) - -### Build Flutter app -Here introduce how to build the flutter sample app. - -#### For x64 targets on x64 hosts / for Arm64 targets on Arm64 hosts - -Build for release mode: -```Shell -$ flutter create sample -$ cd sample/$ cd sample/ -$ flutter build linux -$ cd .. -``` - -Build for debug mode: -```Shell -$ flutter build linux --debug -``` - -#### For Arm64 targets on x64 hosts -Comming soon. We are contributing to support this now. See: https://github.com/flutter/flutter/issues/74929 - -#### FYI: only in debug mode -If you want to work Flutter apps in debug mode, you can also do the following steps on both x64 and arm64 hosts. In debug mode, the Flutter bundle artifacts are not architecturally different between x64 and arm64. - -```Shell -$ flutter build bundle --asset-dir=./bundle/data/flutter_assets -$ cp /bin/cache/artifacts/engine/linux-*/icudtl.dat ./bundle/data -``` - -## 5. Running Flutter app - -### Run with Wayland backend -Wayland compositor such as Weston must be running before running the program. - -```Shell -$ ./flutter-client --bundle=./sample/build/linux/x64/release/bundle -``` - -### Supplement -You can switch quickly between debug / profile / release modes for the Flutter app without replacing `libflutter_engine.so` by using `LD_LIBRARY_PATH` when you run the Flutter app. - -```Shell -$ LD_LIBRARY_PATH= ./flutter-client --bundle= - -# e.g. Run in debug mode -$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/debug/ ./flutter-client --bundle=./sample/build/linux/x64/debug/bundle - -# e.g. Run in profile mode -$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/profile/ ./flutter-client --bundle=./sample/build/linux/x64/profile/bundle - -# e.g. Run in release mode -$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/release/ ./flutter-client --bundle=./sample/build/linux/x64/release/bundle -``` - -### Run with DRM backend -You need to switch from GUI which is running X11 or Wayland to the Character User Interface (CUI). In addition, `FLUTTER_DRM_DEVICE` must be set properly. The default value is `/dev/dri/card0`. - -```Shell -$ Ctrl + Alt + F3 # Switching to CUI -$ sudo FLUTTER_DRM_DEVICE="/dev/dri/card1" --bundle=./sample/build/linux/x64/release/bundle -``` - -If you want to switch back from CUI to GUI, run `Ctrl + Alt + F2` keys in a terminal. - -#### Note -You need to run this program by a user who has the permission to access the input devices(/dev/input/xxx), if you use the DRM backend. Generally, it is a root user or a user who belongs to an input group. - -## 6. Debugging Flutter apps -You can do debugging Flutter apps. Please see: [How to debug Flutter apps](./debugging.md) - -## 7. Settings of weston.ini file (Only when using Weston desktop-shell) -Sets the following parameters when this embedder works as a desktop-shell on Weston. Sample file can be found [examples/config/weston.ini](../examples/config/weston.ini). See also `man weston.ini`. - -### shell section -Specifies the path to the binary file to start as the shell when Weston starts. - -| Field | Description | -| ------------- | ------------- | -| client | /flutter-desktop-shell | - -### extended section -An extended section for this embedder. The entries that can appear in this section are: - -| Field | Description | -| ------------- | ------------- | -| show-cursor | Set whether to show mouse cursor (boolean) | -| flutter-project-path | Set an absolute path or relative path from the binary file to Flutter project path (string) | diff --git a/doc/building-engine-embedder.md b/doc/building-engine-embedder.md deleted file mode 100644 index cd0450bc..00000000 --- a/doc/building-engine-embedder.md +++ /dev/null @@ -1,140 +0,0 @@ -# How to build Flutter Engine embedder - -See also: -- [Custom Flutter Engine Embedders](https://github.com/flutter/flutter/wiki/Custom-Flutter-Engine-Embedders) -- [Custom Flutter Engine Embedding in AOT Mode](https://github.com/flutter/flutter/wiki/Custom-Flutter-Engine-Embedding-in-AOT-Mode) - -## 1. Install build tools - -``` -$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git -$ export PATH=$PATH:$(pwd)/depot_tools -``` - -### Python 2 - -Python 2 is required to build. If you default installation is Python 3 you could workaround this by using virtualenv: - -```Shell -$ virtualenv .env -p python2 -$ source .env/bin/activate -``` - -See also: https://github.com/dart-lang/sdk/wiki/Building#python-2 - -## 2. Create .gclient file - -### When using the latest version of Flutter Engine - -```yaml -solutions = [ - { - "managed": False, - "name": "src/flutter", - "url": "https://github.com/flutter/engine.git", - "custom_deps": {}, - "deps_file": "DEPS", - "safesync_url": "", - }, -] -``` - -### When using a specific version of Flutter Engine - -You can check the current engine version (commit id / SHA): -- [master channel](https://raw.githubusercontent.com/flutter/flutter/master/bin/internal/engine.version) -- [dev channel](https://raw.githubusercontent.com/flutter/flutter/dev/bin/internal/engine.version) -- [beta channel](https://raw.githubusercontent.com/flutter/flutter/beta/bin/internal/engine.version) -- [stable channel](https://raw.githubusercontent.com/flutter/flutter/stable/bin/internal/engine.version) - -You can also get the engine version from `${path_to_flutter_sdk_install}/flutter/bin/internal/engine.version` of the Flutter SDK which you are currently using. - -```yaml -solutions = [ - { - "managed": False, - "name": "src/flutter", - "url": "https://github.com/flutter/engine.git@FLUTTER_ENGINE", - "custom_deps": {}, - "deps_file": "DEPS", - "safesync_url": "", - }, -] -``` -Note: Replace `FLUTTER_ENGINE` with the commid it of the Flutter engine you want to use. - -## 3. Get source files - -```Shell -$ gclient sync -``` - -## 4. Build embedder - -```Shell -$ cd src -``` - -### arm64 targets with debug mode - -```Shell -$ ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode debug --unoptimized --embedder-for-target --disable-desktop-embeddings -$ ninja -C out/linux_debug_unopt_arm64 -``` - -### arm64 targets with profile mode - -```Shell -$ ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode profile --no-lto --embedder-for-target --disable-desktop-embeddings -$ ninja -C out/linux_profile_arm64 -``` - -### arm64 targets with release mode - -```Shell -$ ./flutter/tools/gn --target-os linux --linux-cpu arm64 --runtime-mode release --embedder-for-target --disable-desktop-embeddings -$ ninja -C out/linux_release_arm64 -``` - -### x64 targets with debug mode - -```Shell -$ ./flutter/tools/gn --runtime-mode debug --unoptimized --embedder-for-target --disable-desktop-embeddings -$ ninja -C out/host_debug_unopt -``` - -### x64 targets with profile mode - -```Shell -$ ./flutter/tools/gn --runtime-mode profile --no-lto --embedder-for-target --disable-desktop-embeddings -$ ninja -C out/host_profile -``` - -### x64 targets with release mode - -```Shell -$ ./flutter/tools/gn --runtime-mode release --embedder-for-target --disable-desktop-embeddings -$ ninja -C out/host_release -``` - -## 5. Install embedder library - -```Shell -$ cp ./out/${path to your selected target and mode}/libflutter_engine.so -``` - -### Supplement -You need to install `libflutter_engine.so` in `` to build. But you can switch quickly between debug / profile / release modes for the Flutter app without replacing `libflutter_engine.so` by using `LD_LIBRARY_PATH` when you run the Flutter app. - -```Shell -$ LD_LIBRARY_PATH= ./flutter-client - -# e.g. Run in debug mode -$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/debug/ ./flutter-client ./sample/build/linux/x64/debug/bundle - -# e.g. Run in profile mode -$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/profile/ ./flutter-client ./sample/build/linux/x64/profile/bundle - -# e.g. Run in release mode -$ LD_LIBRARY_PATH=/usr/lib/flutter_engine/release/ ./flutter-client ./sample/build/linux/x64/release/bundle -``` diff --git a/doc/debugging.md b/doc/debugging.md deleted file mode 100644 index e7a5c065..00000000 --- a/doc/debugging.md +++ /dev/null @@ -1,48 +0,0 @@ -# How to debug Flutter apps -It is possible to do most things, including source-level debugging, profiling and hot reload when you debug Flutter apps on this embedder. Note that you need to use `libflutter_engine.so` built with **debug mode**. - -## Getting the Observatory port - -You will find the following message in the console when you run Flutter apps. You need to attach a debugger to this port when you want to debug it. **Note that the URI changes every time after launching**. - -```Shell -flutter: Observatory listening on http://127.0.0.1:40409/k8IUol2dnPI=/ -``` -## Debugging - -### Command line - -```Shell -$ cd / -$ flutter attach --device-id=flutter-tester --debug-uri http://127.0.0.1:40409/k8IUol2dnPI=/ -``` - -### How to use command line options in the Dart VM -You can use the environment variables such as `FLUTTER_ENGINE_SWITCHES`, `FLUTTER_ENGINE_SWITCH_*` to use Dart VM command line options. If you want to fix always same observatory URI, you can use the following command. Note that this option is only available in debug mode. See also: [How to debug the embedder](./README.md#how-to-debug-the-embedder) - -```Shell -$ FLUTTER_ENGINE_SWITCHES=2 \ - FLUTTER_ENGINE_SWITCH_1="observatory-port=12345" \ - FLUTTER_ENGINE_SWITCH_2="disable-service-auth-codes" \ - ./flutter-x11-clinet --bundle=sample/build/linux/x64/debug/bundle -flutter: Observatory listening on http://127.0.0.1:12345/ -``` - -### VS Code - -Create a [launch.json file](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations) like the following. - -```Json -{ - "version": "0.2.0" - "configurations": [ - { - "type": "dart", - "name": "Flutter Embedded Linux Attach", - "request": "attach", - "deviceId": "flutter-tester", - "observatoryUri": "http://127.0.0.1:40409/k8IUol2dnPI=/" - } - ] -} -``` diff --git a/examples/README.md b/examples/README.md index 26f864c1..ef2e517e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -3,11 +3,12 @@ ## Description These are an example of how to use embedded Linux embedding for Flutter. -## Examples +## Each backend examples - [flutter-wayland-client](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-wayland-client): Wayland client app -- [flutter-weston-desktop-shell](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-weston-desktop-shell): Works on Weston desktop-shell -- [flutter-weston-desktop-shell-virtual-keyboard](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-weston-desktop-shell-virtual-keyboard): Works on Weston desktop-shell with the virtual keyboard - [flutter-drm-gbm-backend](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-drm-gbm-backend): Fullscreen app on DRM backend with GBM - [flutter-drm-eglstream-backend](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-drm-eglstream-backend): Fullscreen app on DRM backend with EGLStream - [flutter-x11-client](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-x11-client): X11 client app -- [flutter-external-texture-plugin](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-external-texture-plugin): Wayland client app using external texture plugin + +## Examples using Flutter Plugins +- [flutter-video-player-plugin](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-video-player-plugin): Flutter video player plugin +- [flutter-external-texture-plugin](https://github.com/sony/flutter-embedded-linux/tree/master/examples/flutter-external-texture-plugin): Flutter external texture plugin diff --git a/examples/config/weston.ini b/examples/config/weston.ini deleted file mode 100644 index 746d9ffd..00000000 --- a/examples/config/weston.ini +++ /dev/null @@ -1,23 +0,0 @@ -[core] -# Disable idle timeout -idle-time=0 - -# Flutter Engine only support this format -gbm-format=argb8888 - -[shell] -# Disable the panel at the top -panel-position=none - -# Anmation type when an app starts -animation=zoom - -# The binary path of weston-desktop-shell -client=/usr/sbin/flutter-desktop-shell - -[flutter_linux_wayland] -# Show mouse cursor -show-cursor=true - -# Flutter dart project path -flutter-project-path=../flutter-projects/sample/bundle diff --git a/examples/flutter-drm-eglstream-backend/cmake/user_build.cmake b/examples/flutter-drm-eglstream-backend/cmake/user_build.cmake index bfb83d21..ecf37e32 100644 --- a/examples/flutter-drm-eglstream-backend/cmake/user_build.cmake +++ b/examples/flutter-drm-eglstream-backend/cmake/user_build.cmake @@ -5,6 +5,8 @@ set(TARGET flutter-drm-eglstream-backend) # source files for user apps. set(USER_APP_SRCS + examples/flutter-drm-eglstream-backend/flutter/generated_plugin_registrant.cc + examples/flutter-drm-eglstream-backend/flutter_window.cc examples/flutter-drm-eglstream-backend/main.cc ) @@ -13,13 +15,13 @@ set(USER_APP_INCLUDE_DIRS ## Public APIs for developers (Don't edit!). src/client_wrapper/include src/flutter/shell/platform/common/client_wrapper + src/flutter/shell/platform/common/client_wrapper/include src/flutter/shell/platform/common/client_wrapper/include/flutter src/flutter/shell/platform/common/public src/flutter/shell/platform/linux_embedded/public - src/public/include ## header file include path for user apps. examples/flutter-drm-eglstream-backend ) # link libraries for user apps. -set(USER_APP_LIBRARIES "") +set(USER_APP_LIBRARIES) diff --git a/examples/flutter-drm-eglstream-backend/cmake/user_config.cmake b/examples/flutter-drm-eglstream-backend/cmake/user_config.cmake index f923ce62..4d2cb636 100644 --- a/examples/flutter-drm-eglstream-backend/cmake/user_config.cmake +++ b/examples/flutter-drm-eglstream-backend/cmake/user_config.cmake @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.10) # Flutter embedder configurations. -# See: https://github.com/sony/flutter-embedded-linux/doc/README.md#user-configuration-parameters-cmake-options +# See: https://github.com/sony/flutter-embedded-linux/wiki/Building-Embedded-Linux-embedding-for-Flutter#user-configuration-parameters-cmake-options set(BACKEND_TYPE DRM-EGLSTREAM) -set(DESKTOP_SHELL OFF) -set(USE_VIRTUAL_KEYBOARD OFF) set(USE_GLES3 OFF) diff --git a/examples/flutter-drm-eglstream-backend/command_options.h b/examples/flutter-drm-eglstream-backend/command_options.h index e051a043..b0de9316 100644 --- a/examples/flutter-drm-eglstream-backend/command_options.h +++ b/examples/flutter-drm-eglstream-backend/command_options.h @@ -1,7 +1,10 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2022 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef COMMAND_OPTIONS_ +#define COMMAND_OPTIONS_ + #include #include #include @@ -10,8 +13,6 @@ #include #include -// todo: Supports other types besides int, string. - namespace commandline { namespace { @@ -36,30 +37,49 @@ class CommandOptions { CommandOptions() = default; ~CommandOptions() = default; - void AddWithoutValue(const std::string& name, const std::string& short_name, - const std::string& description, bool required) { + void AddWithoutValue(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required) { Add(name, short_name, description, "", ReaderString(), required, false); } - void AddInt(const std::string& name, const std::string& short_name, - const std::string& description, const int& default_value, + void AddInt(const std::string& name, + const std::string& short_name, + const std::string& description, + const int& default_value, bool required) { Add(name, short_name, description, default_value, ReaderInt(), required, true); } - void AddString(const std::string& name, const std::string& short_name, + void AddDouble(const std::string& name, + const std::string& short_name, + const std::string& description, + const double& default_value, + bool required) { + Add(name, short_name, description, default_value, + ReaderDouble(), required, true); + } + + void AddString(const std::string& name, + const std::string& short_name, const std::string& description, - const std::string& default_value, bool required) { + const std::string& default_value, + bool required) { Add(name, short_name, description, default_value, ReaderString(), required, true); } template - void Add(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader = F(), bool required = true, bool required_value = true) { + void Add(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader = F(), + bool required = true, + bool required_value = true) { if (options_.find(name) != options_.end()) { std::cerr << "Already registered option: " << name << std::endl; return; @@ -142,7 +162,7 @@ class CommandOptions { // short options: e.g. -f /foo/file.txt -h 640 -abc else if (arg.length() > 1 && arg.substr(0, 1).compare(kOptionStyleShort) == 0) { - for (int j = 1; j < arg.length(); j++) { + for (size_t j = 1; j < arg.length(); j++) { const std::string option_name{argv[i][j]}; if (lut_short_options_.find(option_name) == @@ -167,7 +187,7 @@ class CommandOptions { } } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired() && !registration_order_options_[i]->HasValue()) { errors_.push_back( @@ -187,7 +207,7 @@ class CommandOptions { std::ostringstream ostream; ostream << "Usage: " << command_name_ << " "; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired()) { ostream << registration_order_options_[i]->GetHelpShortMessage() << " "; } @@ -196,12 +216,12 @@ class CommandOptions { ostream << "Global options:" << std::endl; size_t max_name_len = 0; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { max_name_len = std::max( max_name_len, registration_order_options_[i]->GetName().length()); } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (!registration_order_options_[i]->GetShortName().empty()) { ostream << kOptionStyleShort << registration_order_options_[i]->GetShortName() << ", "; @@ -210,7 +230,7 @@ class CommandOptions { } size_t index_adjust = 0; - constexpr int kSpacerNum = 5; + constexpr int kSpacerNum = 10; auto need_value = registration_order_options_[i]->IsRequiredValue(); ostream << kOptionStyleNormal << registration_order_options_[i]->GetName(); @@ -237,10 +257,17 @@ class CommandOptions { std::string operator()(const std::string& value) { return value; } }; + struct ReaderDouble { + double operator()(const std::string& value) { return std::stod(value); } + }; + class Option { public: - Option(const std::string& name, const std::string& short_name, - const std::string& description, bool required, bool required_value) + Option(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required, + bool required_value) : name_(name), short_name_(short_name), description_(description), @@ -274,20 +301,23 @@ class CommandOptions { virtual bool HasValue() const = 0; protected: - bool is_required_; - bool is_required_value_; std::string name_; std::string short_name_; std::string description_; + bool is_required_; + bool is_required_value_; bool value_set_; }; template class OptionValue : public Option { public: - OptionValue(const std::string& name, const std::string& short_name, - const std::string& description, const T& default_value, - bool required, bool required_value) + OptionValue(const std::string& name, + const std::string& short_name, + const std::string& description, + const T& default_value, + bool required, + bool required_value) : Option(name, short_name, description, required, required_value), default_value_(default_value), value_(default_value){}; @@ -313,10 +343,18 @@ class CommandOptions { template class OptionValueReader : public OptionValue { public: - OptionValueReader(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader, bool required, bool required_value) - : OptionValue(name, short_name, description, default_value, required, + OptionValueReader(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader, + bool required, + bool required_value) + : OptionValue(name, + short_name, + description, + default_value, + required, required_value), reader_(reader) {} ~OptionValueReader() = default; @@ -360,3 +398,5 @@ class CommandOptions { }; } // namespace commandline + +#endif // COMMAND_OPTIONS_ diff --git a/examples/flutter-drm-eglstream-backend/flutter/generated_plugin_registrant.cc b/examples/flutter-drm-eglstream-backend/flutter/generated_plugin_registrant.cc new file mode 100644 index 00000000..7e6e6c2d --- /dev/null +++ b/examples/flutter-drm-eglstream-backend/flutter/generated_plugin_registrant.cc @@ -0,0 +1,10 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + +void RegisterPlugins(flutter::PluginRegistry* registry) { +} diff --git a/examples/flutter-drm-eglstream-backend/flutter/generated_plugin_registrant.h b/examples/flutter-drm-eglstream-backend/flutter/generated_plugin_registrant.h new file mode 100644 index 00000000..a31c23cd --- /dev/null +++ b/examples/flutter-drm-eglstream-backend/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/examples/flutter-drm-eglstream-backend/flutter/generated_plugins.cmake b/examples/flutter-drm-eglstream-backend/flutter/generated_plugins.cmake new file mode 100644 index 00000000..fcbf0d6e --- /dev/null +++ b/examples/flutter-drm-eglstream-backend/flutter/generated_plugins.cmake @@ -0,0 +1,22 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory( + ${USER_PROJECT_PATH}/flutter/plugins/${plugin}/elinux plugins/${plugin}) + + target_link_libraries(${TARGET} + PRIVATE + ${plugin}_plugin + ) + + list(APPEND PLUGIN_BUNDLED_LIBRARIES + ${PROJECT_BINARY_DIR}/plugins/${plugin}/lib${plugin}_plugin.so + ) +endforeach(plugin) diff --git a/examples/flutter-drm-eglstream-backend/flutter_embedder_options.h b/examples/flutter-drm-eglstream-backend/flutter_embedder_options.h new file mode 100644 index 00000000..41d0bd10 --- /dev/null +++ b/examples/flutter-drm-eglstream-backend/flutter_embedder_options.h @@ -0,0 +1,203 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_EMBEDDER_OPTIONS_ +#define FLUTTER_EMBEDDER_OPTIONS_ + +#include + +#include + +#include "command_options.h" + +class FlutterEmbedderOptions { + public: + FlutterEmbedderOptions() { + options_.AddString("bundle", "b", "Path to Flutter project bundle", + "./bundle", true); + options_.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", + false); + options_.AddInt("rotation", "r", + "Window rotation(degree) [0(default)|90|180|270]", 0, + false); + options_.AddDouble("text-scaling-factor", "x", "Text scaling factor", 1.0, + false); + options_.AddWithoutValue("enable-high-contrast", "i", + "Request that UI be rendered with darker colors.", + false); + options_.AddDouble("force-scale-factor", "s", + "Force a scale factor instead using default value", 1.0, + false); + options_.AddWithoutValue( + "async-vblank", "v", + "Don't sync to compositor redraw/vblank (eglSwapInterval 0)", false); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + // no more options. +#elif defined(FLUTTER_TARGET_BACKEND_X11) + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddString("app-id", "a", "XDG App ID", "dev.flutter.elinux", + false); + options_.AddWithoutValue("onscreen-keyboard", "k", + "Enable on-screen keyboard", false); + options_.AddWithoutValue("window-decoration", "d", + "Enable window decorations", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#endif + } + ~FlutterEmbedderOptions() = default; + + bool Parse(int argc, char** argv) { + if (!options_.Parse(argc, argv)) { + std::cerr << options_.GetError() << std::endl; + std::cout << options_.ShowHelp(); + return false; + } + + bundle_path_ = options_.GetValue("bundle"); + use_mouse_cursor_ = !options_.Exist("no-cursor"); + if (options_.Exist("rotation")) { + switch (options_.GetValue("rotation")) { + case 90: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_90; + break; + case 180: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_180; + break; + case 270: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_270; + break; + default: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + break; + } + } + + text_scale_factor_ = options_.GetValue("text-scaling-factor"); + enable_high_contrast_ = options_.Exist("enable-high-contrast"); + + if (options_.Exist("force-scale-factor")) { + is_force_scale_factor_ = true; + scale_factor_ = options_.GetValue("force-scale-factor"); + } else { + is_force_scale_factor_ = false; + scale_factor_ = 1.0; + } + + enable_vsync_ = !options_.Exist("async-vblank"); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_view_mode_ = flutter::FlutterViewController::ViewMode::kFullscreen; +#elif defined(FLUTTER_TARGET_BACKEND_X11) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_title_ = options_.GetValue("title"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + window_title_ = options_.GetValue("title"); + window_app_id_ = options_.GetValue("app-id"); + use_onscreen_keyboard_ = options_.Exist("onscreen-keyboard"); + use_window_decoration_ = options_.Exist("window-decoration"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#endif + + return true; + } + + std::string BundlePath() const { + return bundle_path_; + } + std::string WindowTitle() const { + return window_title_; + } + std::string WindowAppID() const { + return window_app_id_; + } + bool IsUseMouseCursor() const { + return use_mouse_cursor_; + } + bool IsUseOnscreenKeyboard() const { + return use_onscreen_keyboard_; + } + bool IsUseWindowDecoraation() const { + return use_window_decoration_; + } + flutter::FlutterViewController::ViewMode WindowViewMode() const { + return window_view_mode_; + } + int WindowWidth() const { + return window_width_; + } + int WindowHeight() const { + return window_height_; + } + flutter::FlutterViewController::ViewRotation WindowRotation() const { + return window_view_rotation_; + } + double TextScaleFactor() const { + return text_scale_factor_; + } + bool EnableHighContrast() const { + return enable_high_contrast_; + } + bool IsForceScaleFactor() const { + return is_force_scale_factor_; + } + double ScaleFactor() const { + return scale_factor_; + } + bool EnableVsync() const { + return enable_vsync_; + } + + private: + commandline::CommandOptions options_; + + std::string bundle_path_; + std::string window_title_; + std::string window_app_id_; + bool use_mouse_cursor_ = true; + bool use_onscreen_keyboard_ = false; + bool use_window_decoration_ = false; + flutter::FlutterViewController::ViewMode window_view_mode_ = + flutter::FlutterViewController::ViewMode::kNormal; + int window_width_ = 1280; + int window_height_ = 720; + flutter::FlutterViewController::ViewRotation window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + bool is_force_scale_factor_; + double scale_factor_; + double text_scale_factor_; + bool enable_high_contrast_; + bool enable_vsync_; +}; + +#endif // FLUTTER_EMBEDDER_OPTIONS_ diff --git a/examples/flutter-drm-eglstream-backend/flutter_window.cc b/examples/flutter-drm-eglstream-backend/flutter_window.cc new file mode 100644 index 00000000..0c5b6397 --- /dev/null +++ b/examples/flutter-drm-eglstream-backend/flutter_window.cc @@ -0,0 +1,79 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter_window.h" + +#include +#include +#include +#include + +#include "flutter/generated_plugin_registrant.h" + +FlutterWindow::FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project) + : view_properties_(view_properties), project_(project) {} + +bool FlutterWindow::OnCreate() { + flutter_view_controller_ = std::make_unique( + view_properties_, project_); + + // Ensure that basic setup of the controller was successful. + if (!flutter_view_controller_->engine() || + !flutter_view_controller_->view()) { + return false; + } + + // Register Flutter plugins. + RegisterPlugins(flutter_view_controller_->engine()); + + return true; +} + +void FlutterWindow::OnDestroy() { + if (flutter_view_controller_) { + flutter_view_controller_ = nullptr; + } +} + +void FlutterWindow::Run() { + // Main loop. + auto next_flutter_event_time = + std::chrono::steady_clock::time_point::clock::now(); + while (flutter_view_controller_->view()->DispatchEvent()) { + // Wait until the next event. + { + auto wait_duration = + std::max(std::chrono::nanoseconds(0), + next_flutter_event_time - + std::chrono::steady_clock::time_point::clock::now()); + std::this_thread::sleep_for( + std::chrono::duration_cast(wait_duration)); + } + + // Processes any pending events in the Flutter engine, and returns the + // number of nanoseconds until the next scheduled event (or max, if none). + auto wait_duration = flutter_view_controller_->engine()->ProcessMessages(); + { + auto next_event_time = std::chrono::steady_clock::time_point::max(); + if (wait_duration != std::chrono::nanoseconds::max()) { + next_event_time = + std::min(next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + wait_duration); + } else { + // Wait for the next frame if no events. + auto frame_rate = flutter_view_controller_->view()->GetFrameRate(); + next_event_time = std::min( + next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + std::chrono::milliseconds( + static_cast(std::trunc(1000000.0 / frame_rate)))); + } + next_flutter_event_time = + std::max(next_flutter_event_time, next_event_time); + } + } +} diff --git a/examples/flutter-drm-eglstream-backend/flutter_window.h b/examples/flutter-drm-eglstream-backend/flutter_window.h new file mode 100644 index 00000000..20b9cb88 --- /dev/null +++ b/examples/flutter-drm-eglstream-backend/flutter_window.h @@ -0,0 +1,34 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_WINDOW_ +#define FLUTTER_WINDOW_ + +#include +#include + +#include + +class FlutterWindow { + public: + explicit FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project); + ~FlutterWindow() = default; + + // Prevent copying. + FlutterWindow(FlutterWindow const&) = delete; + FlutterWindow& operator=(FlutterWindow const&) = delete; + + bool OnCreate(); + void OnDestroy(); + void Run(); + + private: + flutter::FlutterViewController::ViewProperties view_properties_; + flutter::DartProject project_; + std::unique_ptr flutter_view_controller_; +}; + +#endif // FLUTTER_WINDOW_ diff --git a/examples/flutter-drm-eglstream-backend/main.cc b/examples/flutter-drm-eglstream-backend/main.cc index ec4f968d..579daee6 100644 --- a/examples/flutter-drm-eglstream-backend/main.cc +++ b/examples/flutter-drm-eglstream-backend/main.cc @@ -5,77 +5,49 @@ #include #include -#include #include #include #include -#include -#include "command_options.h" +#include "flutter_embedder_options.h" +#include "flutter_window.h" int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); + FlutterEmbedderOptions options; if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); return 0; } - // The project to run. - const auto bundle_path = options.GetValue("bundle"); + // Creates the Flutter project. + const auto bundle_path = options.BundlePath(); const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); flutter::DartProject project(fl_path); auto command_line_arguments = std::vector(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - auto flutter_controller = std::make_unique( - flutter::FlutterViewController::ViewMode::kFullscreen, 0, 0, show_cursor, - project); + flutter::FlutterViewController::ViewProperties view_properties = {}; + view_properties.width = options.WindowWidth(); + view_properties.height = options.WindowHeight(); + view_properties.view_mode = options.WindowViewMode(); + view_properties.view_rotation = options.WindowRotation(); + view_properties.title = options.WindowTitle(); + view_properties.app_id = options.WindowAppID(); + view_properties.use_mouse_cursor = options.IsUseMouseCursor(); + view_properties.use_onscreen_keyboard = options.IsUseOnscreenKeyboard(); + view_properties.use_window_decoration = options.IsUseWindowDecoraation(); + view_properties.text_scale_factor = options.TextScaleFactor(); + view_properties.enable_high_contrast = options.EnableHighContrast(); + view_properties.force_scale_factor = options.IsForceScaleFactor(); + view_properties.scale_factor = options.ScaleFactor(); + view_properties.enable_vsync = options.EnableVsync(); - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { + // The Flutter instance hosted by this window. + FlutterWindow window(view_properties, project); + if (!window.OnCreate()) { return 0; } - - // Main loop. - auto next_flutter_event_time = - std::chrono::steady_clock::time_point::clock::now(); - while (flutter_controller->view()->DispatchEvent()) { - // Wait until the next event. - { - auto wait_duration = - std::max(std::chrono::nanoseconds(0), - next_flutter_event_time - - std::chrono::steady_clock::time_point::clock::now()); - std::this_thread::sleep_for( - std::chrono::duration_cast(wait_duration)); - } - - // Processes any pending events in the Flutter engine, and returns the - // number of nanoseconds until the next scheduled event (or max, if none). - auto wait_duration = flutter_controller->engine()->ProcessMessages(); - { - auto next_event_time = std::chrono::steady_clock::time_point::max(); - if (wait_duration != std::chrono::nanoseconds::max()) { - auto next_wakeup = - std::max(std::chrono::steady_clock::time_point::max(), - std::chrono::steady_clock::time_point::clock::now() + - wait_duration); - next_event_time = std::min(next_event_time, next_wakeup); - } else { - // Wait 1/60 [sec] = 13 [msec] if no events. - next_event_time = std::chrono::steady_clock::time_point::clock::now() + - std::chrono::milliseconds(13); - } - next_flutter_event_time = - std::max(next_flutter_event_time, next_event_time); - } - } + window.Run(); + window.OnDestroy(); return 0; } diff --git a/examples/flutter-drm-gbm-backend/cmake/user_build.cmake b/examples/flutter-drm-gbm-backend/cmake/user_build.cmake index 512ced26..59c0f368 100644 --- a/examples/flutter-drm-gbm-backend/cmake/user_build.cmake +++ b/examples/flutter-drm-gbm-backend/cmake/user_build.cmake @@ -5,6 +5,8 @@ set(TARGET flutter-drm-gbm-backend) # source files for user apps. set(USER_APP_SRCS + examples/flutter-drm-gbm-backend/flutter/generated_plugin_registrant.cc + examples/flutter-drm-gbm-backend/flutter_window.cc examples/flutter-drm-gbm-backend/main.cc ) @@ -13,13 +15,13 @@ set(USER_APP_INCLUDE_DIRS ## Public APIs for developers (Don't edit!). src/client_wrapper/include src/flutter/shell/platform/common/client_wrapper + src/flutter/shell/platform/common/client_wrapper/include src/flutter/shell/platform/common/client_wrapper/include/flutter src/flutter/shell/platform/common/public src/flutter/shell/platform/linux_embedded/public - src/public/include ## header file include path for user apps. examples/flutter-drm-gbm-backend ) # link libraries for user apps. -set(USER_APP_LIBRARIES "") +set(USER_APP_LIBRARIES) diff --git a/examples/flutter-drm-gbm-backend/cmake/user_config.cmake b/examples/flutter-drm-gbm-backend/cmake/user_config.cmake index abb08e0d..a3a5931e 100644 --- a/examples/flutter-drm-gbm-backend/cmake/user_config.cmake +++ b/examples/flutter-drm-gbm-backend/cmake/user_config.cmake @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.10) # Flutter embedder configurations. -# See: https://github.com/sony/flutter-embedded-linux/doc/README.md#user-configuration-parameters-cmake-options +# See: https://github.com/sony/flutter-embedded-linux/wiki/Building-Embedded-Linux-embedding-for-Flutter#user-configuration-parameters-cmake-options set(BACKEND_TYPE DRM-GBM) -set(DESKTOP_SHELL OFF) -set(USE_VIRTUAL_KEYBOARD OFF) set(USE_GLES3 OFF) diff --git a/examples/flutter-drm-gbm-backend/command_options.h b/examples/flutter-drm-gbm-backend/command_options.h index e051a043..b0de9316 100644 --- a/examples/flutter-drm-gbm-backend/command_options.h +++ b/examples/flutter-drm-gbm-backend/command_options.h @@ -1,7 +1,10 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2022 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef COMMAND_OPTIONS_ +#define COMMAND_OPTIONS_ + #include #include #include @@ -10,8 +13,6 @@ #include #include -// todo: Supports other types besides int, string. - namespace commandline { namespace { @@ -36,30 +37,49 @@ class CommandOptions { CommandOptions() = default; ~CommandOptions() = default; - void AddWithoutValue(const std::string& name, const std::string& short_name, - const std::string& description, bool required) { + void AddWithoutValue(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required) { Add(name, short_name, description, "", ReaderString(), required, false); } - void AddInt(const std::string& name, const std::string& short_name, - const std::string& description, const int& default_value, + void AddInt(const std::string& name, + const std::string& short_name, + const std::string& description, + const int& default_value, bool required) { Add(name, short_name, description, default_value, ReaderInt(), required, true); } - void AddString(const std::string& name, const std::string& short_name, + void AddDouble(const std::string& name, + const std::string& short_name, + const std::string& description, + const double& default_value, + bool required) { + Add(name, short_name, description, default_value, + ReaderDouble(), required, true); + } + + void AddString(const std::string& name, + const std::string& short_name, const std::string& description, - const std::string& default_value, bool required) { + const std::string& default_value, + bool required) { Add(name, short_name, description, default_value, ReaderString(), required, true); } template - void Add(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader = F(), bool required = true, bool required_value = true) { + void Add(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader = F(), + bool required = true, + bool required_value = true) { if (options_.find(name) != options_.end()) { std::cerr << "Already registered option: " << name << std::endl; return; @@ -142,7 +162,7 @@ class CommandOptions { // short options: e.g. -f /foo/file.txt -h 640 -abc else if (arg.length() > 1 && arg.substr(0, 1).compare(kOptionStyleShort) == 0) { - for (int j = 1; j < arg.length(); j++) { + for (size_t j = 1; j < arg.length(); j++) { const std::string option_name{argv[i][j]}; if (lut_short_options_.find(option_name) == @@ -167,7 +187,7 @@ class CommandOptions { } } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired() && !registration_order_options_[i]->HasValue()) { errors_.push_back( @@ -187,7 +207,7 @@ class CommandOptions { std::ostringstream ostream; ostream << "Usage: " << command_name_ << " "; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired()) { ostream << registration_order_options_[i]->GetHelpShortMessage() << " "; } @@ -196,12 +216,12 @@ class CommandOptions { ostream << "Global options:" << std::endl; size_t max_name_len = 0; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { max_name_len = std::max( max_name_len, registration_order_options_[i]->GetName().length()); } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (!registration_order_options_[i]->GetShortName().empty()) { ostream << kOptionStyleShort << registration_order_options_[i]->GetShortName() << ", "; @@ -210,7 +230,7 @@ class CommandOptions { } size_t index_adjust = 0; - constexpr int kSpacerNum = 5; + constexpr int kSpacerNum = 10; auto need_value = registration_order_options_[i]->IsRequiredValue(); ostream << kOptionStyleNormal << registration_order_options_[i]->GetName(); @@ -237,10 +257,17 @@ class CommandOptions { std::string operator()(const std::string& value) { return value; } }; + struct ReaderDouble { + double operator()(const std::string& value) { return std::stod(value); } + }; + class Option { public: - Option(const std::string& name, const std::string& short_name, - const std::string& description, bool required, bool required_value) + Option(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required, + bool required_value) : name_(name), short_name_(short_name), description_(description), @@ -274,20 +301,23 @@ class CommandOptions { virtual bool HasValue() const = 0; protected: - bool is_required_; - bool is_required_value_; std::string name_; std::string short_name_; std::string description_; + bool is_required_; + bool is_required_value_; bool value_set_; }; template class OptionValue : public Option { public: - OptionValue(const std::string& name, const std::string& short_name, - const std::string& description, const T& default_value, - bool required, bool required_value) + OptionValue(const std::string& name, + const std::string& short_name, + const std::string& description, + const T& default_value, + bool required, + bool required_value) : Option(name, short_name, description, required, required_value), default_value_(default_value), value_(default_value){}; @@ -313,10 +343,18 @@ class CommandOptions { template class OptionValueReader : public OptionValue { public: - OptionValueReader(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader, bool required, bool required_value) - : OptionValue(name, short_name, description, default_value, required, + OptionValueReader(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader, + bool required, + bool required_value) + : OptionValue(name, + short_name, + description, + default_value, + required, required_value), reader_(reader) {} ~OptionValueReader() = default; @@ -360,3 +398,5 @@ class CommandOptions { }; } // namespace commandline + +#endif // COMMAND_OPTIONS_ diff --git a/examples/flutter-drm-gbm-backend/flutter/generated_plugin_registrant.cc b/examples/flutter-drm-gbm-backend/flutter/generated_plugin_registrant.cc new file mode 100644 index 00000000..7e6e6c2d --- /dev/null +++ b/examples/flutter-drm-gbm-backend/flutter/generated_plugin_registrant.cc @@ -0,0 +1,10 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + +void RegisterPlugins(flutter::PluginRegistry* registry) { +} diff --git a/examples/flutter-drm-gbm-backend/flutter/generated_plugin_registrant.h b/examples/flutter-drm-gbm-backend/flutter/generated_plugin_registrant.h new file mode 100644 index 00000000..a31c23cd --- /dev/null +++ b/examples/flutter-drm-gbm-backend/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/examples/flutter-drm-gbm-backend/flutter/generated_plugins.cmake b/examples/flutter-drm-gbm-backend/flutter/generated_plugins.cmake new file mode 100644 index 00000000..fcbf0d6e --- /dev/null +++ b/examples/flutter-drm-gbm-backend/flutter/generated_plugins.cmake @@ -0,0 +1,22 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory( + ${USER_PROJECT_PATH}/flutter/plugins/${plugin}/elinux plugins/${plugin}) + + target_link_libraries(${TARGET} + PRIVATE + ${plugin}_plugin + ) + + list(APPEND PLUGIN_BUNDLED_LIBRARIES + ${PROJECT_BINARY_DIR}/plugins/${plugin}/lib${plugin}_plugin.so + ) +endforeach(plugin) diff --git a/examples/flutter-drm-gbm-backend/flutter_embedder_options.h b/examples/flutter-drm-gbm-backend/flutter_embedder_options.h new file mode 100644 index 00000000..41d0bd10 --- /dev/null +++ b/examples/flutter-drm-gbm-backend/flutter_embedder_options.h @@ -0,0 +1,203 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_EMBEDDER_OPTIONS_ +#define FLUTTER_EMBEDDER_OPTIONS_ + +#include + +#include + +#include "command_options.h" + +class FlutterEmbedderOptions { + public: + FlutterEmbedderOptions() { + options_.AddString("bundle", "b", "Path to Flutter project bundle", + "./bundle", true); + options_.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", + false); + options_.AddInt("rotation", "r", + "Window rotation(degree) [0(default)|90|180|270]", 0, + false); + options_.AddDouble("text-scaling-factor", "x", "Text scaling factor", 1.0, + false); + options_.AddWithoutValue("enable-high-contrast", "i", + "Request that UI be rendered with darker colors.", + false); + options_.AddDouble("force-scale-factor", "s", + "Force a scale factor instead using default value", 1.0, + false); + options_.AddWithoutValue( + "async-vblank", "v", + "Don't sync to compositor redraw/vblank (eglSwapInterval 0)", false); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + // no more options. +#elif defined(FLUTTER_TARGET_BACKEND_X11) + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddString("app-id", "a", "XDG App ID", "dev.flutter.elinux", + false); + options_.AddWithoutValue("onscreen-keyboard", "k", + "Enable on-screen keyboard", false); + options_.AddWithoutValue("window-decoration", "d", + "Enable window decorations", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#endif + } + ~FlutterEmbedderOptions() = default; + + bool Parse(int argc, char** argv) { + if (!options_.Parse(argc, argv)) { + std::cerr << options_.GetError() << std::endl; + std::cout << options_.ShowHelp(); + return false; + } + + bundle_path_ = options_.GetValue("bundle"); + use_mouse_cursor_ = !options_.Exist("no-cursor"); + if (options_.Exist("rotation")) { + switch (options_.GetValue("rotation")) { + case 90: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_90; + break; + case 180: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_180; + break; + case 270: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_270; + break; + default: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + break; + } + } + + text_scale_factor_ = options_.GetValue("text-scaling-factor"); + enable_high_contrast_ = options_.Exist("enable-high-contrast"); + + if (options_.Exist("force-scale-factor")) { + is_force_scale_factor_ = true; + scale_factor_ = options_.GetValue("force-scale-factor"); + } else { + is_force_scale_factor_ = false; + scale_factor_ = 1.0; + } + + enable_vsync_ = !options_.Exist("async-vblank"); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_view_mode_ = flutter::FlutterViewController::ViewMode::kFullscreen; +#elif defined(FLUTTER_TARGET_BACKEND_X11) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_title_ = options_.GetValue("title"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + window_title_ = options_.GetValue("title"); + window_app_id_ = options_.GetValue("app-id"); + use_onscreen_keyboard_ = options_.Exist("onscreen-keyboard"); + use_window_decoration_ = options_.Exist("window-decoration"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#endif + + return true; + } + + std::string BundlePath() const { + return bundle_path_; + } + std::string WindowTitle() const { + return window_title_; + } + std::string WindowAppID() const { + return window_app_id_; + } + bool IsUseMouseCursor() const { + return use_mouse_cursor_; + } + bool IsUseOnscreenKeyboard() const { + return use_onscreen_keyboard_; + } + bool IsUseWindowDecoraation() const { + return use_window_decoration_; + } + flutter::FlutterViewController::ViewMode WindowViewMode() const { + return window_view_mode_; + } + int WindowWidth() const { + return window_width_; + } + int WindowHeight() const { + return window_height_; + } + flutter::FlutterViewController::ViewRotation WindowRotation() const { + return window_view_rotation_; + } + double TextScaleFactor() const { + return text_scale_factor_; + } + bool EnableHighContrast() const { + return enable_high_contrast_; + } + bool IsForceScaleFactor() const { + return is_force_scale_factor_; + } + double ScaleFactor() const { + return scale_factor_; + } + bool EnableVsync() const { + return enable_vsync_; + } + + private: + commandline::CommandOptions options_; + + std::string bundle_path_; + std::string window_title_; + std::string window_app_id_; + bool use_mouse_cursor_ = true; + bool use_onscreen_keyboard_ = false; + bool use_window_decoration_ = false; + flutter::FlutterViewController::ViewMode window_view_mode_ = + flutter::FlutterViewController::ViewMode::kNormal; + int window_width_ = 1280; + int window_height_ = 720; + flutter::FlutterViewController::ViewRotation window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + bool is_force_scale_factor_; + double scale_factor_; + double text_scale_factor_; + bool enable_high_contrast_; + bool enable_vsync_; +}; + +#endif // FLUTTER_EMBEDDER_OPTIONS_ diff --git a/examples/flutter-drm-gbm-backend/flutter_window.cc b/examples/flutter-drm-gbm-backend/flutter_window.cc new file mode 100644 index 00000000..0c5b6397 --- /dev/null +++ b/examples/flutter-drm-gbm-backend/flutter_window.cc @@ -0,0 +1,79 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter_window.h" + +#include +#include +#include +#include + +#include "flutter/generated_plugin_registrant.h" + +FlutterWindow::FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project) + : view_properties_(view_properties), project_(project) {} + +bool FlutterWindow::OnCreate() { + flutter_view_controller_ = std::make_unique( + view_properties_, project_); + + // Ensure that basic setup of the controller was successful. + if (!flutter_view_controller_->engine() || + !flutter_view_controller_->view()) { + return false; + } + + // Register Flutter plugins. + RegisterPlugins(flutter_view_controller_->engine()); + + return true; +} + +void FlutterWindow::OnDestroy() { + if (flutter_view_controller_) { + flutter_view_controller_ = nullptr; + } +} + +void FlutterWindow::Run() { + // Main loop. + auto next_flutter_event_time = + std::chrono::steady_clock::time_point::clock::now(); + while (flutter_view_controller_->view()->DispatchEvent()) { + // Wait until the next event. + { + auto wait_duration = + std::max(std::chrono::nanoseconds(0), + next_flutter_event_time - + std::chrono::steady_clock::time_point::clock::now()); + std::this_thread::sleep_for( + std::chrono::duration_cast(wait_duration)); + } + + // Processes any pending events in the Flutter engine, and returns the + // number of nanoseconds until the next scheduled event (or max, if none). + auto wait_duration = flutter_view_controller_->engine()->ProcessMessages(); + { + auto next_event_time = std::chrono::steady_clock::time_point::max(); + if (wait_duration != std::chrono::nanoseconds::max()) { + next_event_time = + std::min(next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + wait_duration); + } else { + // Wait for the next frame if no events. + auto frame_rate = flutter_view_controller_->view()->GetFrameRate(); + next_event_time = std::min( + next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + std::chrono::milliseconds( + static_cast(std::trunc(1000000.0 / frame_rate)))); + } + next_flutter_event_time = + std::max(next_flutter_event_time, next_event_time); + } + } +} diff --git a/examples/flutter-drm-gbm-backend/flutter_window.h b/examples/flutter-drm-gbm-backend/flutter_window.h new file mode 100644 index 00000000..20b9cb88 --- /dev/null +++ b/examples/flutter-drm-gbm-backend/flutter_window.h @@ -0,0 +1,34 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_WINDOW_ +#define FLUTTER_WINDOW_ + +#include +#include + +#include + +class FlutterWindow { + public: + explicit FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project); + ~FlutterWindow() = default; + + // Prevent copying. + FlutterWindow(FlutterWindow const&) = delete; + FlutterWindow& operator=(FlutterWindow const&) = delete; + + bool OnCreate(); + void OnDestroy(); + void Run(); + + private: + flutter::FlutterViewController::ViewProperties view_properties_; + flutter::DartProject project_; + std::unique_ptr flutter_view_controller_; +}; + +#endif // FLUTTER_WINDOW_ diff --git a/examples/flutter-drm-gbm-backend/main.cc b/examples/flutter-drm-gbm-backend/main.cc index ec4f968d..579daee6 100644 --- a/examples/flutter-drm-gbm-backend/main.cc +++ b/examples/flutter-drm-gbm-backend/main.cc @@ -5,77 +5,49 @@ #include #include -#include #include #include #include -#include -#include "command_options.h" +#include "flutter_embedder_options.h" +#include "flutter_window.h" int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); + FlutterEmbedderOptions options; if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); return 0; } - // The project to run. - const auto bundle_path = options.GetValue("bundle"); + // Creates the Flutter project. + const auto bundle_path = options.BundlePath(); const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); flutter::DartProject project(fl_path); auto command_line_arguments = std::vector(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - auto flutter_controller = std::make_unique( - flutter::FlutterViewController::ViewMode::kFullscreen, 0, 0, show_cursor, - project); + flutter::FlutterViewController::ViewProperties view_properties = {}; + view_properties.width = options.WindowWidth(); + view_properties.height = options.WindowHeight(); + view_properties.view_mode = options.WindowViewMode(); + view_properties.view_rotation = options.WindowRotation(); + view_properties.title = options.WindowTitle(); + view_properties.app_id = options.WindowAppID(); + view_properties.use_mouse_cursor = options.IsUseMouseCursor(); + view_properties.use_onscreen_keyboard = options.IsUseOnscreenKeyboard(); + view_properties.use_window_decoration = options.IsUseWindowDecoraation(); + view_properties.text_scale_factor = options.TextScaleFactor(); + view_properties.enable_high_contrast = options.EnableHighContrast(); + view_properties.force_scale_factor = options.IsForceScaleFactor(); + view_properties.scale_factor = options.ScaleFactor(); + view_properties.enable_vsync = options.EnableVsync(); - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { + // The Flutter instance hosted by this window. + FlutterWindow window(view_properties, project); + if (!window.OnCreate()) { return 0; } - - // Main loop. - auto next_flutter_event_time = - std::chrono::steady_clock::time_point::clock::now(); - while (flutter_controller->view()->DispatchEvent()) { - // Wait until the next event. - { - auto wait_duration = - std::max(std::chrono::nanoseconds(0), - next_flutter_event_time - - std::chrono::steady_clock::time_point::clock::now()); - std::this_thread::sleep_for( - std::chrono::duration_cast(wait_duration)); - } - - // Processes any pending events in the Flutter engine, and returns the - // number of nanoseconds until the next scheduled event (or max, if none). - auto wait_duration = flutter_controller->engine()->ProcessMessages(); - { - auto next_event_time = std::chrono::steady_clock::time_point::max(); - if (wait_duration != std::chrono::nanoseconds::max()) { - auto next_wakeup = - std::max(std::chrono::steady_clock::time_point::max(), - std::chrono::steady_clock::time_point::clock::now() + - wait_duration); - next_event_time = std::min(next_event_time, next_wakeup); - } else { - // Wait 1/60 [sec] = 13 [msec] if no events. - next_event_time = std::chrono::steady_clock::time_point::clock::now() + - std::chrono::milliseconds(13); - } - next_flutter_event_time = - std::max(next_flutter_event_time, next_event_time); - } - } + window.Run(); + window.OnDestroy(); return 0; } diff --git a/examples/flutter-external-texture-plugin/README.md b/examples/flutter-external-texture-plugin/README.md index 7ed5f1fc..21247b17 100644 --- a/examples/flutter-external-texture-plugin/README.md +++ b/examples/flutter-external-texture-plugin/README.md @@ -31,18 +31,16 @@ class _MyAppState extends State { int _textureId = 0; @override - void initState() async { + void initState() { super.initState(); - - await initialize(); - setState(() {}); + initialize().then((value) => setState(() {})); } Future initialize() async { late Completer creatingCompleter; try { creatingCompleter = Completer(); - final channel = const MethodChannel('external_texture_test'); + var channel = const MethodChannel('external_texture_test'); final reply = await channel.invokeMapMethod('initialize'); if (reply != null) { diff --git a/examples/flutter-external-texture-plugin/cmake/user_build.cmake b/examples/flutter-external-texture-plugin/cmake/user_build.cmake index 3e495810..fc29a3bb 100644 --- a/examples/flutter-external-texture-plugin/cmake/user_build.cmake +++ b/examples/flutter-external-texture-plugin/cmake/user_build.cmake @@ -5,8 +5,9 @@ set(TARGET flutter-client) # source files for user apps. set(USER_APP_SRCS + examples/flutter-external-texture-plugin/flutter/generated_plugin_registrant.cc + examples/flutter-external-texture-plugin/flutter_window.cc examples/flutter-external-texture-plugin/main.cc - examples/flutter-external-texture-plugin/external_texture_test_plugin.cc ) # header files for user apps. @@ -14,13 +15,13 @@ set(USER_APP_INCLUDE_DIRS ## Public APIs for developers (Don't edit!). src/client_wrapper/include src/flutter/shell/platform/common/client_wrapper + src/flutter/shell/platform/common/client_wrapper/include src/flutter/shell/platform/common/client_wrapper/include/flutter src/flutter/shell/platform/common/public src/flutter/shell/platform/linux_embedded/public - src/public/include ## header file include path for user apps. examples/flutter-external-texture-plugin ) # link libraries for user apps. -set(USER_APP_LIBRARIES "") +set(USER_APP_LIBRARIES) diff --git a/examples/flutter-external-texture-plugin/cmake/user_config.cmake b/examples/flutter-external-texture-plugin/cmake/user_config.cmake index 919c51d2..5f72786b 100644 --- a/examples/flutter-external-texture-plugin/cmake/user_config.cmake +++ b/examples/flutter-external-texture-plugin/cmake/user_config.cmake @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.10) # Flutter embedder configurations. -# See: https://github.com/sony/flutter-embedded-linux/doc/README.md#user-configuration-parameters-cmake-options +# See: https://github.com/sony/flutter-embedded-linux/wiki/Building-Embedded-Linux-embedding-for-Flutter#user-configuration-parameters-cmake-options set(BACKEND_TYPE WAYLAND) -set(DESKTOP_SHELL OFF) -set(USE_VIRTUAL_KEYBOARD OFF) set(USE_GLES3 OFF) diff --git a/examples/flutter-external-texture-plugin/command_options.h b/examples/flutter-external-texture-plugin/command_options.h index e051a043..b0de9316 100644 --- a/examples/flutter-external-texture-plugin/command_options.h +++ b/examples/flutter-external-texture-plugin/command_options.h @@ -1,7 +1,10 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2022 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef COMMAND_OPTIONS_ +#define COMMAND_OPTIONS_ + #include #include #include @@ -10,8 +13,6 @@ #include #include -// todo: Supports other types besides int, string. - namespace commandline { namespace { @@ -36,30 +37,49 @@ class CommandOptions { CommandOptions() = default; ~CommandOptions() = default; - void AddWithoutValue(const std::string& name, const std::string& short_name, - const std::string& description, bool required) { + void AddWithoutValue(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required) { Add(name, short_name, description, "", ReaderString(), required, false); } - void AddInt(const std::string& name, const std::string& short_name, - const std::string& description, const int& default_value, + void AddInt(const std::string& name, + const std::string& short_name, + const std::string& description, + const int& default_value, bool required) { Add(name, short_name, description, default_value, ReaderInt(), required, true); } - void AddString(const std::string& name, const std::string& short_name, + void AddDouble(const std::string& name, + const std::string& short_name, + const std::string& description, + const double& default_value, + bool required) { + Add(name, short_name, description, default_value, + ReaderDouble(), required, true); + } + + void AddString(const std::string& name, + const std::string& short_name, const std::string& description, - const std::string& default_value, bool required) { + const std::string& default_value, + bool required) { Add(name, short_name, description, default_value, ReaderString(), required, true); } template - void Add(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader = F(), bool required = true, bool required_value = true) { + void Add(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader = F(), + bool required = true, + bool required_value = true) { if (options_.find(name) != options_.end()) { std::cerr << "Already registered option: " << name << std::endl; return; @@ -142,7 +162,7 @@ class CommandOptions { // short options: e.g. -f /foo/file.txt -h 640 -abc else if (arg.length() > 1 && arg.substr(0, 1).compare(kOptionStyleShort) == 0) { - for (int j = 1; j < arg.length(); j++) { + for (size_t j = 1; j < arg.length(); j++) { const std::string option_name{argv[i][j]}; if (lut_short_options_.find(option_name) == @@ -167,7 +187,7 @@ class CommandOptions { } } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired() && !registration_order_options_[i]->HasValue()) { errors_.push_back( @@ -187,7 +207,7 @@ class CommandOptions { std::ostringstream ostream; ostream << "Usage: " << command_name_ << " "; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired()) { ostream << registration_order_options_[i]->GetHelpShortMessage() << " "; } @@ -196,12 +216,12 @@ class CommandOptions { ostream << "Global options:" << std::endl; size_t max_name_len = 0; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { max_name_len = std::max( max_name_len, registration_order_options_[i]->GetName().length()); } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (!registration_order_options_[i]->GetShortName().empty()) { ostream << kOptionStyleShort << registration_order_options_[i]->GetShortName() << ", "; @@ -210,7 +230,7 @@ class CommandOptions { } size_t index_adjust = 0; - constexpr int kSpacerNum = 5; + constexpr int kSpacerNum = 10; auto need_value = registration_order_options_[i]->IsRequiredValue(); ostream << kOptionStyleNormal << registration_order_options_[i]->GetName(); @@ -237,10 +257,17 @@ class CommandOptions { std::string operator()(const std::string& value) { return value; } }; + struct ReaderDouble { + double operator()(const std::string& value) { return std::stod(value); } + }; + class Option { public: - Option(const std::string& name, const std::string& short_name, - const std::string& description, bool required, bool required_value) + Option(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required, + bool required_value) : name_(name), short_name_(short_name), description_(description), @@ -274,20 +301,23 @@ class CommandOptions { virtual bool HasValue() const = 0; protected: - bool is_required_; - bool is_required_value_; std::string name_; std::string short_name_; std::string description_; + bool is_required_; + bool is_required_value_; bool value_set_; }; template class OptionValue : public Option { public: - OptionValue(const std::string& name, const std::string& short_name, - const std::string& description, const T& default_value, - bool required, bool required_value) + OptionValue(const std::string& name, + const std::string& short_name, + const std::string& description, + const T& default_value, + bool required, + bool required_value) : Option(name, short_name, description, required, required_value), default_value_(default_value), value_(default_value){}; @@ -313,10 +343,18 @@ class CommandOptions { template class OptionValueReader : public OptionValue { public: - OptionValueReader(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader, bool required, bool required_value) - : OptionValue(name, short_name, description, default_value, required, + OptionValueReader(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader, + bool required, + bool required_value) + : OptionValue(name, + short_name, + description, + default_value, + required, required_value), reader_(reader) {} ~OptionValueReader() = default; @@ -360,3 +398,5 @@ class CommandOptions { }; } // namespace commandline + +#endif // COMMAND_OPTIONS_ diff --git a/examples/flutter-external-texture-plugin/flutter/generated_plugin_registrant.cc b/examples/flutter-external-texture-plugin/flutter/generated_plugin_registrant.cc new file mode 100644 index 00000000..7b30517d --- /dev/null +++ b/examples/flutter-external-texture-plugin/flutter/generated_plugin_registrant.cc @@ -0,0 +1,14 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + +#include + +void RegisterPlugins(flutter::PluginRegistry* registry) { + ExternalTextureTestPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("ExternalTextureTestPlugin")); +} diff --git a/examples/flutter-external-texture-plugin/flutter/generated_plugin_registrant.h b/examples/flutter-external-texture-plugin/flutter/generated_plugin_registrant.h new file mode 100644 index 00000000..a31c23cd --- /dev/null +++ b/examples/flutter-external-texture-plugin/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/examples/flutter-external-texture-plugin/flutter/generated_plugins.cmake b/examples/flutter-external-texture-plugin/flutter/generated_plugins.cmake new file mode 100644 index 00000000..a17b7381 --- /dev/null +++ b/examples/flutter-external-texture-plugin/flutter/generated_plugins.cmake @@ -0,0 +1,23 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST + external_texture_test +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory( + ${USER_PROJECT_PATH}/flutter/plugins/${plugin}/elinux plugins/${plugin}) + + target_link_libraries(${TARGET} + PRIVATE + ${plugin}_plugin + ) + + list(APPEND PLUGIN_BUNDLED_LIBRARIES + ${PROJECT_BINARY_DIR}/plugins/${plugin}/lib${plugin}_plugin.so + ) +endforeach(plugin) diff --git a/examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/CMakeLists.txt b/examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/CMakeLists.txt new file mode 100644 index 00000000..e84bfcc5 --- /dev/null +++ b/examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.10) + +set(PROJECT_NAME "external_texture_test") +set(PLUGIN_NAME "${PROJECT_NAME}_plugin") +project(${PROJECT_NAME} LANGUAGES CXX) + +add_library(${PLUGIN_NAME} + SHARED + "${PLUGIN_NAME}.cc" +) + +target_compile_definitions(${PLUGIN_NAME} + PRIVATE + FLUTTER_PLUGIN_IMPL +) + +target_include_directories(${PLUGIN_NAME} + INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +target_include_directories(${PLUGIN_NAME} + PRIVATE + ${CMAKE_SOURCE_DIR}/src/flutter/shell/platform/common/client_wrapper/include + ${CMAKE_SOURCE_DIR}/src/flutter/shell/platform/common/public +) diff --git a/examples/flutter-external-texture-plugin/external_texture_test_plugin.cc b/examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/external_texture_test_plugin.cc similarity index 96% rename from examples/flutter-external-texture-plugin/external_texture_test_plugin.cc rename to examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/external_texture_test_plugin.cc index 01ccddb2..4d8367b8 100644 --- a/examples/flutter-external-texture-plugin/external_texture_test_plugin.cc +++ b/examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/external_texture_test_plugin.cc @@ -1,9 +1,8 @@ -#include "external_texture_test_plugin.h" +#include "include/external_texture_test/external_texture_test_plugin.h" +#include #include -#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h" - namespace { static constexpr char kChannelName[] = "external_texture_test"; diff --git a/examples/flutter-external-texture-plugin/external_texture_test_plugin.h b/examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/include/external_texture_test/external_texture_test_plugin.h similarity index 73% rename from examples/flutter-external-texture-plugin/external_texture_test_plugin.h rename to examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/include/external_texture_test/external_texture_test_plugin.h index 94a82651..f55b8591 100644 --- a/examples/flutter-external-texture-plugin/external_texture_test_plugin.h +++ b/examples/flutter-external-texture-plugin/flutter/plugins/external_texture_test/elinux/include/external_texture_test/external_texture_test_plugin.h @@ -1,8 +1,10 @@ -#ifndef EXAMPLES_FLUTTER_EXTERNAL_TEXTURE_PLUGIN_EXTERNAL_TEXTURE_TEST_PLUGIN_H_ -#define EXAMPLES_FLUTTER_EXTERNAL_TEXTURE_PLUGIN_EXTERNAL_TEXTURE_TEST_PLUGIN_H_ +#ifndef PLUGINS_EXTERNAL_TEXTURE_TEST_ELINUX_EXTERNAL_TEXTURE_TEST_PLUGIN_H_ +#define PLUGINS_EXTERNAL_TEXTURE_TEST_ELINUX_EXTERNAL_TEXTURE_TEST_PLUGIN_H_ -#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" -#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h" +#include +#include + +#include namespace { class ColorBarTexture { @@ -44,4 +46,4 @@ class ExternalTextureTestPlugin : public flutter::Plugin { void ExternalTextureTestPluginRegisterWithRegistrar( FlutterDesktopPluginRegistrarRef registrar); -#endif // EXAMPLES_FLUTTER_EXTERNAL_TEXTURE_PLUGIN_EXTERNAL_TEXTURE_TEST_PLUGIN_H_ +#endif // PLUGINS_EXTERNAL_TEXTURE_TEST_ELINUX_EXTERNAL_TEXTURE_TEST_PLUGIN_H_ diff --git a/examples/flutter-external-texture-plugin/flutter_embedder_options.h b/examples/flutter-external-texture-plugin/flutter_embedder_options.h new file mode 100644 index 00000000..41d0bd10 --- /dev/null +++ b/examples/flutter-external-texture-plugin/flutter_embedder_options.h @@ -0,0 +1,203 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_EMBEDDER_OPTIONS_ +#define FLUTTER_EMBEDDER_OPTIONS_ + +#include + +#include + +#include "command_options.h" + +class FlutterEmbedderOptions { + public: + FlutterEmbedderOptions() { + options_.AddString("bundle", "b", "Path to Flutter project bundle", + "./bundle", true); + options_.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", + false); + options_.AddInt("rotation", "r", + "Window rotation(degree) [0(default)|90|180|270]", 0, + false); + options_.AddDouble("text-scaling-factor", "x", "Text scaling factor", 1.0, + false); + options_.AddWithoutValue("enable-high-contrast", "i", + "Request that UI be rendered with darker colors.", + false); + options_.AddDouble("force-scale-factor", "s", + "Force a scale factor instead using default value", 1.0, + false); + options_.AddWithoutValue( + "async-vblank", "v", + "Don't sync to compositor redraw/vblank (eglSwapInterval 0)", false); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + // no more options. +#elif defined(FLUTTER_TARGET_BACKEND_X11) + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddString("app-id", "a", "XDG App ID", "dev.flutter.elinux", + false); + options_.AddWithoutValue("onscreen-keyboard", "k", + "Enable on-screen keyboard", false); + options_.AddWithoutValue("window-decoration", "d", + "Enable window decorations", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#endif + } + ~FlutterEmbedderOptions() = default; + + bool Parse(int argc, char** argv) { + if (!options_.Parse(argc, argv)) { + std::cerr << options_.GetError() << std::endl; + std::cout << options_.ShowHelp(); + return false; + } + + bundle_path_ = options_.GetValue("bundle"); + use_mouse_cursor_ = !options_.Exist("no-cursor"); + if (options_.Exist("rotation")) { + switch (options_.GetValue("rotation")) { + case 90: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_90; + break; + case 180: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_180; + break; + case 270: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_270; + break; + default: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + break; + } + } + + text_scale_factor_ = options_.GetValue("text-scaling-factor"); + enable_high_contrast_ = options_.Exist("enable-high-contrast"); + + if (options_.Exist("force-scale-factor")) { + is_force_scale_factor_ = true; + scale_factor_ = options_.GetValue("force-scale-factor"); + } else { + is_force_scale_factor_ = false; + scale_factor_ = 1.0; + } + + enable_vsync_ = !options_.Exist("async-vblank"); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_view_mode_ = flutter::FlutterViewController::ViewMode::kFullscreen; +#elif defined(FLUTTER_TARGET_BACKEND_X11) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_title_ = options_.GetValue("title"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + window_title_ = options_.GetValue("title"); + window_app_id_ = options_.GetValue("app-id"); + use_onscreen_keyboard_ = options_.Exist("onscreen-keyboard"); + use_window_decoration_ = options_.Exist("window-decoration"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#endif + + return true; + } + + std::string BundlePath() const { + return bundle_path_; + } + std::string WindowTitle() const { + return window_title_; + } + std::string WindowAppID() const { + return window_app_id_; + } + bool IsUseMouseCursor() const { + return use_mouse_cursor_; + } + bool IsUseOnscreenKeyboard() const { + return use_onscreen_keyboard_; + } + bool IsUseWindowDecoraation() const { + return use_window_decoration_; + } + flutter::FlutterViewController::ViewMode WindowViewMode() const { + return window_view_mode_; + } + int WindowWidth() const { + return window_width_; + } + int WindowHeight() const { + return window_height_; + } + flutter::FlutterViewController::ViewRotation WindowRotation() const { + return window_view_rotation_; + } + double TextScaleFactor() const { + return text_scale_factor_; + } + bool EnableHighContrast() const { + return enable_high_contrast_; + } + bool IsForceScaleFactor() const { + return is_force_scale_factor_; + } + double ScaleFactor() const { + return scale_factor_; + } + bool EnableVsync() const { + return enable_vsync_; + } + + private: + commandline::CommandOptions options_; + + std::string bundle_path_; + std::string window_title_; + std::string window_app_id_; + bool use_mouse_cursor_ = true; + bool use_onscreen_keyboard_ = false; + bool use_window_decoration_ = false; + flutter::FlutterViewController::ViewMode window_view_mode_ = + flutter::FlutterViewController::ViewMode::kNormal; + int window_width_ = 1280; + int window_height_ = 720; + flutter::FlutterViewController::ViewRotation window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + bool is_force_scale_factor_; + double scale_factor_; + double text_scale_factor_; + bool enable_high_contrast_; + bool enable_vsync_; +}; + +#endif // FLUTTER_EMBEDDER_OPTIONS_ diff --git a/examples/flutter-external-texture-plugin/flutter_window.cc b/examples/flutter-external-texture-plugin/flutter_window.cc new file mode 100644 index 00000000..0c5b6397 --- /dev/null +++ b/examples/flutter-external-texture-plugin/flutter_window.cc @@ -0,0 +1,79 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter_window.h" + +#include +#include +#include +#include + +#include "flutter/generated_plugin_registrant.h" + +FlutterWindow::FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project) + : view_properties_(view_properties), project_(project) {} + +bool FlutterWindow::OnCreate() { + flutter_view_controller_ = std::make_unique( + view_properties_, project_); + + // Ensure that basic setup of the controller was successful. + if (!flutter_view_controller_->engine() || + !flutter_view_controller_->view()) { + return false; + } + + // Register Flutter plugins. + RegisterPlugins(flutter_view_controller_->engine()); + + return true; +} + +void FlutterWindow::OnDestroy() { + if (flutter_view_controller_) { + flutter_view_controller_ = nullptr; + } +} + +void FlutterWindow::Run() { + // Main loop. + auto next_flutter_event_time = + std::chrono::steady_clock::time_point::clock::now(); + while (flutter_view_controller_->view()->DispatchEvent()) { + // Wait until the next event. + { + auto wait_duration = + std::max(std::chrono::nanoseconds(0), + next_flutter_event_time - + std::chrono::steady_clock::time_point::clock::now()); + std::this_thread::sleep_for( + std::chrono::duration_cast(wait_duration)); + } + + // Processes any pending events in the Flutter engine, and returns the + // number of nanoseconds until the next scheduled event (or max, if none). + auto wait_duration = flutter_view_controller_->engine()->ProcessMessages(); + { + auto next_event_time = std::chrono::steady_clock::time_point::max(); + if (wait_duration != std::chrono::nanoseconds::max()) { + next_event_time = + std::min(next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + wait_duration); + } else { + // Wait for the next frame if no events. + auto frame_rate = flutter_view_controller_->view()->GetFrameRate(); + next_event_time = std::min( + next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + std::chrono::milliseconds( + static_cast(std::trunc(1000000.0 / frame_rate)))); + } + next_flutter_event_time = + std::max(next_flutter_event_time, next_event_time); + } + } +} diff --git a/examples/flutter-external-texture-plugin/flutter_window.h b/examples/flutter-external-texture-plugin/flutter_window.h new file mode 100644 index 00000000..20b9cb88 --- /dev/null +++ b/examples/flutter-external-texture-plugin/flutter_window.h @@ -0,0 +1,34 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_WINDOW_ +#define FLUTTER_WINDOW_ + +#include +#include + +#include + +class FlutterWindow { + public: + explicit FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project); + ~FlutterWindow() = default; + + // Prevent copying. + FlutterWindow(FlutterWindow const&) = delete; + FlutterWindow& operator=(FlutterWindow const&) = delete; + + bool OnCreate(); + void OnDestroy(); + void Run(); + + private: + flutter::FlutterViewController::ViewProperties view_properties_; + flutter::DartProject project_; + std::unique_ptr flutter_view_controller_; +}; + +#endif // FLUTTER_WINDOW_ diff --git a/examples/flutter-external-texture-plugin/main.cc b/examples/flutter-external-texture-plugin/main.cc index 284bd7de..579daee6 100644 --- a/examples/flutter-external-texture-plugin/main.cc +++ b/examples/flutter-external-texture-plugin/main.cc @@ -9,55 +9,45 @@ #include #include -#include "command_options.h" -#include "external_texture_test_plugin.h" +#include "flutter_embedder_options.h" +#include "flutter_window.h" int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("fullscreen", "f", "Always full-screen display", - false); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); - options.AddInt("width", "w", "Flutter app window width", 1280, false); - options.AddInt("height", "h", "Flutter app window height", 720, false); + FlutterEmbedderOptions options; if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); return 0; } - // The project to run. - const auto bundle_path = options.GetValue("bundle"); + // Creates the Flutter project. + const auto bundle_path = options.BundlePath(); const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); flutter::DartProject project(fl_path); auto command_line_arguments = std::vector(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); + flutter::FlutterViewController::ViewProperties view_properties = {}; + view_properties.width = options.WindowWidth(); + view_properties.height = options.WindowHeight(); + view_properties.view_mode = options.WindowViewMode(); + view_properties.view_rotation = options.WindowRotation(); + view_properties.title = options.WindowTitle(); + view_properties.app_id = options.WindowAppID(); + view_properties.use_mouse_cursor = options.IsUseMouseCursor(); + view_properties.use_onscreen_keyboard = options.IsUseOnscreenKeyboard(); + view_properties.use_window_decoration = options.IsUseWindowDecoraation(); + view_properties.text_scale_factor = options.TextScaleFactor(); + view_properties.enable_high_contrast = options.EnableHighContrast(); + view_properties.force_scale_factor = options.IsForceScaleFactor(); + view_properties.scale_factor = options.ScaleFactor(); + view_properties.enable_vsync = options.EnableVsync(); + // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - const auto view_mode = - options.Exist("fullscreen") - ? flutter::FlutterViewController::ViewMode::kFullscreen - : flutter::FlutterViewController::ViewMode::kNormal; - const auto width = options.GetValue("width"); - const auto height = options.GetValue("height"); - auto flutter_controller = std::make_unique( - view_mode, width, height, show_cursor, project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { + FlutterWindow window(view_properties, project); + if (!window.OnCreate()) { return 0; } - - ExternalTextureTestPluginRegisterWithRegistrar( - flutter_controller->engine()->GetRegistrarForPlugin( - "ExternalTextureTestPlugin")); - - // Main loop. - while (flutter_controller->view()->DispatchEvent()) { - flutter_controller->engine()->ProcessMessages(); - } + window.Run(); + window.OnDestroy(); return 0; } diff --git a/examples/flutter-video-player-plugin/README.md b/examples/flutter-video-player-plugin/README.md new file mode 100644 index 00000000..4e643a99 --- /dev/null +++ b/examples/flutter-video-player-plugin/README.md @@ -0,0 +1,42 @@ +# Overview +Flutter video player example using video player plugin for Embedded Linux. The interface of the plugin is compatible with [the Flutter official video player plugin](https://github.com/flutter/plugins/tree/master/packages/video_player/video_player). + +Note that this is just example for eLinux embedder, so please use [sony/flutter-elinux-plugins/packages/video_player](https://github.com/sony/flutter-elinux-plugins/tree/main/packages/video_player) + +![image](https://user-images.githubusercontent.com/62131389/124210378-43f06400-db26-11eb-8723-40dad0eb67b0.png) + +## Quick start + +### Install libraries +This plugin uses GStreamer internally. + +```Shell +$ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-0 \ + gstreamer1.0-plugins-base gstreamer1.0-plugins-good \ + gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav +``` + +### Building the embedder + +```Shell +$ mkdir build +$ cd build +$ cmake -DUSER_PROJECT_PATH=examples/flutter-video-player-plugin .. +$ cmake --build . +``` + +### Building Flutter app with debug mode + +```Shell +$ git clone https://github.com/flutter/plugins.git +$ cd plugins/packages/video_player/video_player +$ flutter build bundle --asset-dir=./bundle/data/flutter_assets +$ cp /bin/cache/artifacts/engine/linux-*/icudtl.dat ./bundle/data +``` + +### Running Flutter app + +```Shell +$ cd +$ LD_LIBRARY_PATH=./plugins/video_player/ ./flutter-client -b ./bundle/ +``` diff --git a/examples/flutter-weston-desktop-shell-virtual-keyboard/cmake/user_build.cmake b/examples/flutter-video-player-plugin/cmake/user_build.cmake similarity index 61% rename from examples/flutter-weston-desktop-shell-virtual-keyboard/cmake/user_build.cmake rename to examples/flutter-video-player-plugin/cmake/user_build.cmake index abba32bf..6014e1db 100644 --- a/examples/flutter-weston-desktop-shell-virtual-keyboard/cmake/user_build.cmake +++ b/examples/flutter-video-player-plugin/cmake/user_build.cmake @@ -1,11 +1,13 @@ cmake_minimum_required(VERSION 3.10) # user binary name. -set(TARGET flutter-desktop-shell) +set(TARGET flutter-client) # source files for user apps. set(USER_APP_SRCS - examples/flutter-weston-desktop-shell-virtual-keyboard/main.cc + examples/flutter-video-player-plugin/flutter/generated_plugin_registrant.cc + examples/flutter-video-player-plugin/flutter_window.cc + examples/flutter-video-player-plugin/main.cc ) # header files for user apps. @@ -13,14 +15,13 @@ set(USER_APP_INCLUDE_DIRS ## Public APIs for developers (Don't edit!). src/client_wrapper/include src/flutter/shell/platform/common/client_wrapper + src/flutter/shell/platform/common/client_wrapper/include src/flutter/shell/platform/common/client_wrapper/include/flutter src/flutter/shell/platform/common/public src/flutter/shell/platform/linux_embedded/public - src/public/include ## header file include path for user apps. - examples/flutter-weston-desktop-shell-virtual-keyboard + examples/flutter-video-player-plugin ) # link libraries for user apps. -set(USER_APP_LIBRARIES -) +set(USER_APP_LIBRARIES) diff --git a/examples/flutter-video-player-plugin/cmake/user_config.cmake b/examples/flutter-video-player-plugin/cmake/user_config.cmake new file mode 100644 index 00000000..5f72786b --- /dev/null +++ b/examples/flutter-video-player-plugin/cmake/user_config.cmake @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.10) + +# Flutter embedder configurations. +# See: https://github.com/sony/flutter-embedded-linux/wiki/Building-Embedded-Linux-embedding-for-Flutter#user-configuration-parameters-cmake-options +set(BACKEND_TYPE WAYLAND) +set(USE_GLES3 OFF) diff --git a/src/templates/app/common/command_options.h b/examples/flutter-video-player-plugin/command_options.h similarity index 78% rename from src/templates/app/common/command_options.h rename to examples/flutter-video-player-plugin/command_options.h index e051a043..b0de9316 100644 --- a/src/templates/app/common/command_options.h +++ b/examples/flutter-video-player-plugin/command_options.h @@ -1,7 +1,10 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2022 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef COMMAND_OPTIONS_ +#define COMMAND_OPTIONS_ + #include #include #include @@ -10,8 +13,6 @@ #include #include -// todo: Supports other types besides int, string. - namespace commandline { namespace { @@ -36,30 +37,49 @@ class CommandOptions { CommandOptions() = default; ~CommandOptions() = default; - void AddWithoutValue(const std::string& name, const std::string& short_name, - const std::string& description, bool required) { + void AddWithoutValue(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required) { Add(name, short_name, description, "", ReaderString(), required, false); } - void AddInt(const std::string& name, const std::string& short_name, - const std::string& description, const int& default_value, + void AddInt(const std::string& name, + const std::string& short_name, + const std::string& description, + const int& default_value, bool required) { Add(name, short_name, description, default_value, ReaderInt(), required, true); } - void AddString(const std::string& name, const std::string& short_name, + void AddDouble(const std::string& name, + const std::string& short_name, + const std::string& description, + const double& default_value, + bool required) { + Add(name, short_name, description, default_value, + ReaderDouble(), required, true); + } + + void AddString(const std::string& name, + const std::string& short_name, const std::string& description, - const std::string& default_value, bool required) { + const std::string& default_value, + bool required) { Add(name, short_name, description, default_value, ReaderString(), required, true); } template - void Add(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader = F(), bool required = true, bool required_value = true) { + void Add(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader = F(), + bool required = true, + bool required_value = true) { if (options_.find(name) != options_.end()) { std::cerr << "Already registered option: " << name << std::endl; return; @@ -142,7 +162,7 @@ class CommandOptions { // short options: e.g. -f /foo/file.txt -h 640 -abc else if (arg.length() > 1 && arg.substr(0, 1).compare(kOptionStyleShort) == 0) { - for (int j = 1; j < arg.length(); j++) { + for (size_t j = 1; j < arg.length(); j++) { const std::string option_name{argv[i][j]}; if (lut_short_options_.find(option_name) == @@ -167,7 +187,7 @@ class CommandOptions { } } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired() && !registration_order_options_[i]->HasValue()) { errors_.push_back( @@ -187,7 +207,7 @@ class CommandOptions { std::ostringstream ostream; ostream << "Usage: " << command_name_ << " "; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired()) { ostream << registration_order_options_[i]->GetHelpShortMessage() << " "; } @@ -196,12 +216,12 @@ class CommandOptions { ostream << "Global options:" << std::endl; size_t max_name_len = 0; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { max_name_len = std::max( max_name_len, registration_order_options_[i]->GetName().length()); } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (!registration_order_options_[i]->GetShortName().empty()) { ostream << kOptionStyleShort << registration_order_options_[i]->GetShortName() << ", "; @@ -210,7 +230,7 @@ class CommandOptions { } size_t index_adjust = 0; - constexpr int kSpacerNum = 5; + constexpr int kSpacerNum = 10; auto need_value = registration_order_options_[i]->IsRequiredValue(); ostream << kOptionStyleNormal << registration_order_options_[i]->GetName(); @@ -237,10 +257,17 @@ class CommandOptions { std::string operator()(const std::string& value) { return value; } }; + struct ReaderDouble { + double operator()(const std::string& value) { return std::stod(value); } + }; + class Option { public: - Option(const std::string& name, const std::string& short_name, - const std::string& description, bool required, bool required_value) + Option(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required, + bool required_value) : name_(name), short_name_(short_name), description_(description), @@ -274,20 +301,23 @@ class CommandOptions { virtual bool HasValue() const = 0; protected: - bool is_required_; - bool is_required_value_; std::string name_; std::string short_name_; std::string description_; + bool is_required_; + bool is_required_value_; bool value_set_; }; template class OptionValue : public Option { public: - OptionValue(const std::string& name, const std::string& short_name, - const std::string& description, const T& default_value, - bool required, bool required_value) + OptionValue(const std::string& name, + const std::string& short_name, + const std::string& description, + const T& default_value, + bool required, + bool required_value) : Option(name, short_name, description, required, required_value), default_value_(default_value), value_(default_value){}; @@ -313,10 +343,18 @@ class CommandOptions { template class OptionValueReader : public OptionValue { public: - OptionValueReader(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader, bool required, bool required_value) - : OptionValue(name, short_name, description, default_value, required, + OptionValueReader(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader, + bool required, + bool required_value) + : OptionValue(name, + short_name, + description, + default_value, + required, required_value), reader_(reader) {} ~OptionValueReader() = default; @@ -360,3 +398,5 @@ class CommandOptions { }; } // namespace commandline + +#endif // COMMAND_OPTIONS_ diff --git a/examples/flutter-video-player-plugin/flutter/generated_plugin_registrant.cc b/examples/flutter-video-player-plugin/flutter/generated_plugin_registrant.cc new file mode 100644 index 00000000..3d6f1746 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/generated_plugin_registrant.cc @@ -0,0 +1,14 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + +#include + +void RegisterPlugins(flutter::PluginRegistry* registry) { + VideoPlayerPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("VideoPlayerPlugin")); +} diff --git a/examples/flutter-video-player-plugin/flutter/generated_plugin_registrant.h b/examples/flutter-video-player-plugin/flutter/generated_plugin_registrant.h new file mode 100644 index 00000000..a31c23cd --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/examples/flutter-video-player-plugin/flutter/generated_plugins.cmake b/examples/flutter-video-player-plugin/flutter/generated_plugins.cmake new file mode 100644 index 00000000..0c60dc83 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/generated_plugins.cmake @@ -0,0 +1,23 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST + video_player +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory( + ${USER_PROJECT_PATH}/flutter/plugins/${plugin}/elinux plugins/${plugin}) + + target_link_libraries(${TARGET} + PRIVATE + ${plugin}_plugin + ) + + list(APPEND PLUGIN_BUNDLED_LIBRARIES + ${PROJECT_BINARY_DIR}/plugins/${plugin}/lib${plugin}_plugin.so + ) +endforeach(plugin) diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/CMakeLists.txt b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/CMakeLists.txt new file mode 100644 index 00000000..75ffbd0f --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/CMakeLists.txt @@ -0,0 +1,49 @@ +cmake_minimum_required(VERSION 3.10) + +set(PROJECT_NAME "video_player") +set(PLUGIN_NAME "${PROJECT_NAME}_plugin") +project(${PROJECT_NAME} LANGUAGES CXX) + +find_package(PkgConfig) +pkg_check_modules(GLIB REQUIRED glib-2.0) +pkg_check_modules(GSTREAMER REQUIRED + gstreamer-1.0 + gstreamer-app-1.0 + gstreamer-video-1.0 + gstreamer-audio-1.0 +) + +add_library(${PLUGIN_NAME} + SHARED + "${PLUGIN_NAME}.cc" + "gst_video_player.cc" +) + +target_compile_definitions(${PLUGIN_NAME} + PRIVATE + FLUTTER_PLUGIN_IMPL +) + +target_include_directories(${PLUGIN_NAME} + INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +target_include_directories(${PLUGIN_NAME} + PRIVATE + ${CMAKE_SOURCE_DIR}/src/flutter/shell/platform/common/client_wrapper/include + ${CMAKE_SOURCE_DIR}/src/flutter/shell/platform/common/public +) + +target_include_directories(${PLUGIN_NAME} + PRIVATE + ${GLIB_INCLUDE_DIRS} + ${GSTREAMER_INCLUDE_DIRS} + ${CMAKE_CURRENT_SOURCE_DIR}/src +) + +target_link_libraries(${PLUGIN_NAME} + PRIVATE + ${GLIB_LIBRARIES} + ${GSTREAMER_LIBRARIES} +) diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/gst_video_player.cc b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/gst_video_player.cc new file mode 100644 index 00000000..40a3fc2f --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/gst_video_player.cc @@ -0,0 +1,395 @@ +// Copyright 2021 Sony Group Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "gst_video_player.h" + +#include +#include +#include + +#include + +GstVideoPlayer::GstVideoPlayer( + const std::string& uri, std::unique_ptr handler) + : stream_handler_(std::move(handler)) { + gst_.pipeline = nullptr; + gst_.playbin = nullptr; + gst_.video_convert = nullptr; + gst_.video_sink = nullptr; + gst_.output = nullptr; + gst_.bus = nullptr; + gst_.buffer = nullptr; + + uri_ = ParseUri(uri); + if (!CreatePipeline()) { + std::cerr << "Failed to create a pipeline" << std::endl; + DestroyPipeline(); + return; + } + + // Prerolls before getting information from the pipeline. + Preroll(); + + // Sets internal video size and buffier. + GetVideoSize(width_, height_); + pixels_.reset(new uint32_t[width_ * height_]); + + stream_handler_->OnNotifyInitialized(); +} + +GstVideoPlayer::~GstVideoPlayer() { + Stop(); + DestroyPipeline(); +} + +// static +void GstVideoPlayer::GstLibraryLoad() { gst_init(NULL, NULL); } + +// static +void GstVideoPlayer::GstLibraryUnload() { gst_deinit(); } + +bool GstVideoPlayer::Play() { + if (gst_element_set_state(gst_.pipeline, GST_STATE_PLAYING) == + GST_STATE_CHANGE_FAILURE) { + std::cerr << "Failed to change the state to PLAYING" << std::endl; + return false; + } + return true; +} + +bool GstVideoPlayer::Pause() { + if (gst_element_set_state(gst_.pipeline, GST_STATE_PAUSED) == + GST_STATE_CHANGE_FAILURE) { + std::cerr << "Failed to change the state to PAUSED" << std::endl; + return false; + } + return true; +} + +bool GstVideoPlayer::Stop() { + if (gst_element_set_state(gst_.pipeline, GST_STATE_READY) == + GST_STATE_CHANGE_FAILURE) { + std::cerr << "Failed to change the state to READY" << std::endl; + return false; + } + return true; +} + +bool GstVideoPlayer::SetVolume(double volume) { + if (!gst_.playbin) { + return false; + } + + volume_ = volume; + g_object_set(gst_.playbin, "volume", volume, NULL); + return true; +} + +bool GstVideoPlayer::SetPlaybackRate(double rate) { + if (!gst_.playbin) { + return false; + } + + if (rate <= 0) { + std::cerr << "Rate " << rate << " is not supported" << std::endl; + return false; + } + + if (!gst_element_seek(gst_.pipeline, rate, GST_FORMAT_TIME, + GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, + GetCurrentPosition() * GST_MSECOND, GST_SEEK_TYPE_SET, + GST_CLOCK_TIME_NONE)) { + std::cerr << "Failed to set playback rate to " << rate + << " (gst_element_seek failed)" << std::endl; + return false; + } + + playback_rate_ = rate; + mute_ = (rate < 0.5 || rate > 2); + g_object_set(gst_.playbin, "mute", mute_, NULL); + + return true; +} + +bool GstVideoPlayer::SetSeek(int64_t position) { + auto nanosecond = position * 1000 * 1000; + if (!gst_element_seek( + gst_.pipeline, playback_rate_, GST_FORMAT_TIME, + (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT), + GST_SEEK_TYPE_SET, nanosecond, GST_SEEK_TYPE_SET, + GST_CLOCK_TIME_NONE)) { + std::cerr << "Failed to seek " << nanosecond << std::endl; + return false; + } + return true; +} + +int64_t GstVideoPlayer::GetDuration() { + GstFormat fmt = GST_FORMAT_TIME; + int64_t duration_msec; + if (!gst_element_query_duration(gst_.pipeline, fmt, &duration_msec)) { + std::cerr << "Failed to get duration" << std::endl; + return -1; + } + duration_msec /= GST_MSECOND; + return duration_msec; +} + +int64_t GstVideoPlayer::GetCurrentPosition() { + gint64 position = 0; + if (!gst_element_query_position(gst_.pipeline, GST_FORMAT_TIME, &position)) { + std::cerr << "Failed to get current position" << std::endl; + } + return position / GST_MSECOND; +} + +const uint8_t* GstVideoPlayer::GetFrameBuffer() { + std::shared_lock lock(mutex_buffer_); + if (!gst_.buffer) { + return nullptr; + } + + const uint32_t pixel_bytes = width_ * height_ * 4; + gst_buffer_extract(gst_.buffer, 0, pixels_.get(), pixel_bytes); + return reinterpret_cast(pixels_.get()); +} + +// Creats a video pipeline using playbin. +// $ playbin uri= video-sink="videoconvert ! video/x-raw,format=RGBA ! +// fakesink" +bool GstVideoPlayer::CreatePipeline() { + gst_.pipeline = gst_pipeline_new("pipeline"); + if (!gst_.pipeline) { + std::cerr << "Failed to create a pipeline" << std::endl; + return false; + } + gst_.playbin = gst_element_factory_make("playbin", "playbin"); + if (!gst_.playbin) { + std::cerr << "Failed to create a source" << std::endl; + return false; + } + gst_.video_convert = gst_element_factory_make("videoconvert", "videoconvert"); + if (!gst_.video_convert) { + std::cerr << "Failed to create a videoconvert" << std::endl; + return false; + } + gst_.video_sink = gst_element_factory_make("fakesink", "videosink"); + if (!gst_.video_sink) { + std::cerr << "Failed to create a videosink" << std::endl; + return false; + } + gst_.output = gst_bin_new("output"); + if (!gst_.output) { + std::cerr << "Failed to create an output" << std::endl; + return false; + } + gst_.bus = gst_pipeline_get_bus(GST_PIPELINE(gst_.pipeline)); + if (!gst_.bus) { + std::cerr << "Failed to create a bus" << std::endl; + return false; + } + gst_bus_set_sync_handler(gst_.bus, (GstBusSyncHandler)HandleGstMessage, this, + NULL); + + // Sets properties to fakesink to get the callback of a decoded frame. + g_object_set(G_OBJECT(gst_.video_sink), "sync", TRUE, "qos", FALSE, NULL); + g_object_set(G_OBJECT(gst_.video_sink), "signal-handoffs", TRUE, NULL); + g_signal_connect(G_OBJECT(gst_.video_sink), "handoff", + G_CALLBACK(HandoffHandler), this); + gst_bin_add_many(GST_BIN(gst_.output), gst_.video_convert, gst_.video_sink, + NULL); + + // Adds caps to the converter to convert the color format to RGBA. + auto* caps = gst_caps_from_string("video/x-raw,format=RGBA"); + auto link_ok = + gst_element_link_filtered(gst_.video_convert, gst_.video_sink, caps); + gst_caps_unref(caps); + if (!link_ok) { + std::cerr << "Failed to link elements" << std::endl; + return false; + } + + auto* sinkpad = gst_element_get_static_pad(gst_.video_convert, "sink"); + auto* ghost_sinkpad = gst_ghost_pad_new("sink", sinkpad); + gst_pad_set_active(ghost_sinkpad, TRUE); + gst_element_add_pad(gst_.output, ghost_sinkpad); + + // Sets properties to playbin. + g_object_set(gst_.playbin, "uri", uri_.c_str(), NULL); + g_object_set(gst_.playbin, "video-sink", gst_.output, NULL); + gst_bin_add_many(GST_BIN(gst_.pipeline), gst_.playbin, NULL); + + return true; +} + +void GstVideoPlayer::Preroll() { + if (!gst_.playbin) { + return; + } + + auto result = gst_element_set_state(gst_.pipeline, GST_STATE_PAUSED); + if (result == GST_STATE_CHANGE_FAILURE) { + std::cerr << "Failed to change the state to PAUSED" << std::endl; + return; + } + + // Waits until the state becomes GST_STATE_PAUSED. + if (result == GST_STATE_CHANGE_ASYNC) { + GstState state; + result = + gst_element_get_state(gst_.pipeline, &state, NULL, GST_CLOCK_TIME_NONE); + if (result == GST_STATE_CHANGE_FAILURE) { + std::cerr << "Failed to get the current state" << std::endl; + } + } +} + +void GstVideoPlayer::DestroyPipeline() { + if (gst_.video_sink) { + g_object_set(G_OBJECT(gst_.video_sink), "signal-handoffs", FALSE, NULL); + } + + if (gst_.pipeline) { + gst_element_set_state(gst_.pipeline, GST_STATE_NULL); + } + + if (gst_.buffer) { + gst_buffer_unref(gst_.buffer); + gst_.buffer = nullptr; + } + + if (gst_.bus) { + gst_object_unref(gst_.bus); + gst_.bus = nullptr; + } + + if (gst_.pipeline) { + gst_object_unref(gst_.pipeline); + gst_.pipeline = nullptr; + } + + if (gst_.playbin) { + gst_.playbin = nullptr; + } + + if (gst_.output) { + gst_.output = nullptr; + } + + if (gst_.video_sink) { + gst_.video_sink = nullptr; + } + + if (gst_.video_convert) { + gst_.video_convert = nullptr; + } +} + +std::string GstVideoPlayer::ParseUri(const std::string& uri) { + if (gst_uri_is_valid(uri.c_str())) { + return uri; + } + + const auto* filename_uri = gst_filename_to_uri(uri.c_str(), NULL); + if (!filename_uri) { + std::cerr << "Faild to open " << uri.c_str() << std::endl; + return uri; + } + std::string result_uri(filename_uri); + delete filename_uri; + + return result_uri; +} + +void GstVideoPlayer::GetVideoSize(int32_t& width, int32_t& height) { + if (!gst_.pipeline || !gst_.video_sink) { + std::cerr + << "Failed to get video size. The pileline hasn't initialized yet."; + return; + } + + auto* sink_pad = gst_element_get_static_pad(gst_.video_sink, "sink"); + if (!sink_pad) { + std::cerr << "Failed to get a pad"; + return; + } + + auto* caps = gst_pad_get_current_caps(sink_pad); + auto* structure = gst_caps_get_structure(caps, 0); + if (!structure) { + std::cerr << "Failed to get a structure"; + return; + } + + gst_structure_get_int(structure, "width", &width); + gst_structure_get_int(structure, "height", &height); +} + +// static +void GstVideoPlayer::HandoffHandler(GstElement* fakesink, GstBuffer* buf, + GstPad* new_pad, gpointer user_data) { + auto* self = reinterpret_cast(user_data); + auto* caps = gst_pad_get_current_caps(new_pad); + auto* structure = gst_caps_get_structure(caps, 0); + + int width; + int height; + gst_structure_get_int(structure, "width", &width); + gst_structure_get_int(structure, "height", &height); + if (width != self->width_ || height != self->height_) { + self->width_ = width; + self->height_ = height; + self->pixels_.reset(new uint32_t[width * height]); + std::cout << "Pixel buffer size: width = " << width + << ", height = " << height << std::endl; + } + + std::lock_guard lock(self->mutex_buffer_); + if (self->gst_.buffer) { + gst_buffer_unref(self->gst_.buffer); + self->gst_.buffer = nullptr; + } + self->gst_.buffer = gst_buffer_ref(buf); + self->stream_handler_->OnNotifyFrameDecoded(); +} + +// static +gboolean GstVideoPlayer::HandleGstMessage(GstBus* bus, GstMessage* message, + gpointer user_data) { + switch (GST_MESSAGE_TYPE(message)) { + case GST_MESSAGE_EOS: { + auto* self = reinterpret_cast(user_data); + self->stream_handler_->OnNotifyCompleted(); + if (self->auto_repeat_) { + self->SetSeek(0); + } + break; + } + case GST_MESSAGE_WARNING: { + gchar* debug; + GError* error; + gst_message_parse_warning(message, &error, &debug); + g_printerr("WARNING from element %s: %s\n", GST_OBJECT_NAME(message->src), + error->message); + g_printerr("Warning details: %s\n", debug); + g_free(debug); + g_error_free(error); + break; + } + case GST_MESSAGE_ERROR: { + gchar* debug; + GError* error; + gst_message_parse_error(message, &error, &debug); + g_printerr("ERROR from element %s: %s\n", GST_OBJECT_NAME(message->src), + error->message); + g_printerr("Error details: %s\n", debug); + g_free(debug); + g_error_free(error); + break; + } + default: + break; + } + return TRUE; +} diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/gst_video_player.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/gst_video_player.h new file mode 100644 index 00000000..eb589077 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/gst_video_player.h @@ -0,0 +1,72 @@ +// Copyright 2021 Sony Group Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_GST_VIDEO_PLAYER_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_GST_VIDEO_PLAYER_H_ + +#include + +#include +#include +#include + +#include "video_player_stream_handler.h" + +class GstVideoPlayer { + public: + GstVideoPlayer(const std::string& uri, + std::unique_ptr handler); + ~GstVideoPlayer(); + + static void GstLibraryLoad(); + static void GstLibraryUnload(); + + bool Play(); + bool Pause(); + bool Stop(); + bool SetVolume(double volume); + bool SetPlaybackRate(double rate); + void SetAutoRepeat(bool auto_repeat) { auto_repeat_ = auto_repeat; }; + bool SetSeek(int64_t position); + int64_t GetDuration(); + int64_t GetCurrentPosition(); + const uint8_t* GetFrameBuffer(); + int32_t GetWidth() const { return width_; }; + int32_t GetHeight() const { return height_; }; + + private: + struct GstVideoElements { + GstElement* pipeline; + GstElement* playbin; + GstElement* video_convert; + GstElement* video_sink; + GstElement* output; + GstBus* bus; + GstBuffer* buffer; + }; + + static void HandoffHandler(GstElement* fakesink, GstBuffer* buf, + GstPad* new_pad, gpointer user_data); + static gboolean HandleGstMessage(GstBus* bus, GstMessage* message, + gpointer user_data); + std::string ParseUri(const std::string& uri); + bool CreatePipeline(); + void DestroyPipeline(); + void Preroll(); + void GetVideoSize(int32_t& width, int32_t& height); + + GstVideoElements gst_; + std::string uri_; + std::unique_ptr pixels_; + int32_t width_; + int32_t height_; + double volume_ = 1.0; + double playback_rate_ = 1.0; + bool mute_ = false; + bool auto_repeat_ = false; + std::shared_mutex mutex_buffer_; + std::unique_ptr stream_handler_; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_GST_VIDEO_PLAYER_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/include/video_player/video_player_plugin.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/include/video_player/video_player_plugin.h new file mode 100644 index 00000000..ee0ae0a3 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/include/video_player/video_player_plugin.h @@ -0,0 +1,23 @@ +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_INCLUDE_VIDEO_PLAYER_VIDEO_PLAYER_PLUGIN_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_INCLUDE_VIDEO_PLAYER_VIDEO_PLAYER_PLUGIN_H_ + +#include + +#ifdef FLUTTER_PLUGIN_IMPL +#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default"))) +#else +#define FLUTTER_PLUGIN_EXPORT +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + +FLUTTER_PLUGIN_EXPORT void VideoPlayerPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_INCLUDE_VIDEO_PLAYER_VIDEO_PLAYER_PLUGIN_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/create_message.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/create_message.h new file mode 100644 index 00000000..fc0fd0fe --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/create_message.h @@ -0,0 +1,90 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_CREATE_MESSAGE_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_CREATE_MESSAGE_H_ + +#include +#include + +class CreateMessage { + public: + CreateMessage() = default; + ~CreateMessage() = default; + + // Prevent copying. + CreateMessage(CreateMessage const&) = default; + CreateMessage& operator=(CreateMessage const&) = default; + + void SetAsset(const std::string& asset) { asset_ = asset; } + + std::string GetAsset() const { return asset_; } + + void SetUri(const std::string& uri) { uri_ = uri; } + + std::string GetUri() const { return uri_; } + + void SetPackageName(const std::string& packageName) { + package_name_ = packageName; + } + + std::string GetPackageName() const { return package_name_; } + + void SetFormatHint(const std::string& formatHint) { + format_hint_ = formatHint; + } + + std::string GetFormatHint() const { return format_hint_; } + + flutter::EncodableValue ToMap() { + // todo: Add httpHeaders. + flutter::EncodableMap map = { + {flutter::EncodableValue("asset"), flutter::EncodableValue(asset_)}, + {flutter::EncodableValue("uri"), flutter::EncodableValue(uri_)}, + {flutter::EncodableValue("packageName"), + flutter::EncodableValue(package_name_)}, + {flutter::EncodableValue("formatHint"), + flutter::EncodableValue(format_hint_)}}; + return flutter::EncodableValue(map); + } + + static CreateMessage FromMap(const flutter::EncodableValue& value) { + CreateMessage message; + if (std::holds_alternative(value)) { + auto map = std::get(value); + + flutter::EncodableValue& asset = map[flutter::EncodableValue("asset")]; + if (std::holds_alternative(asset)) { + message.SetAsset(std::get(asset)); + } + + flutter::EncodableValue& uri = map[flutter::EncodableValue("uri")]; + if (std::holds_alternative(uri)) { + message.SetUri(std::get(uri)); + } + + flutter::EncodableValue& packageName = + map[flutter::EncodableValue("packageName")]; + if (std::holds_alternative(packageName)) { + message.SetPackageName(std::get(uri)); + } + + flutter::EncodableValue& formatHint = + map[flutter::EncodableValue("formatHint")]; + if (std::holds_alternative(formatHint)) { + message.SetFormatHint(std::get(formatHint)); + } + } + + return message; + } + + private: + std::string asset_; + std::string uri_; + std::string package_name_; + std::string format_hint_; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_CREATE_MESSAGE_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/looping_message.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/looping_message.h new file mode 100644 index 00000000..2219f37b --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/looping_message.h @@ -0,0 +1,63 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_LOOPING_MESSAGE_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_LOOPING_MESSAGE_H_ + +#include +#include + +class LoopingMessage { + public: + LoopingMessage() = default; + ~LoopingMessage() = default; + + // Prevent copying. + LoopingMessage(LoopingMessage const&) = default; + LoopingMessage& operator=(LoopingMessage const&) = default; + + void SetTextureId(int64_t texture_id) { texture_id_ = texture_id; } + + int64_t GetTextureId() const { return texture_id_; } + + void SetIsLooping(bool is_looping) { is_looping_ = is_looping; } + + bool GetIsLooping() const { return is_looping_; } + + flutter::EncodableValue ToMap() { + flutter::EncodableMap map = {{flutter::EncodableValue("textureId"), + flutter::EncodableValue(texture_id_)}, + {flutter::EncodableValue("isLooping"), + flutter::EncodableValue(is_looping_)}}; + return flutter::EncodableValue(map); + } + + static LoopingMessage FromMap(const flutter::EncodableValue& value) { + LoopingMessage message; + if (std::holds_alternative(value)) { + auto map = std::get(value); + + flutter::EncodableValue& texture_id = + map[flutter::EncodableValue("textureId")]; + if (std::holds_alternative(texture_id) || + std::holds_alternative(texture_id)) { + message.SetTextureId(texture_id.LongValue()); + } + + flutter::EncodableValue& is_looping = + map[flutter::EncodableValue("isLooping")]; + if (std::holds_alternative(is_looping)) { + message.SetIsLooping(std::get(is_looping)); + } + } + + return message; + } + + private: + int64_t texture_id_ = 0; + bool is_looping_ = false; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_LOOPING_MESSAGE_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/messages.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/messages.h new file mode 100644 index 00000000..4f642e0f --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/messages.h @@ -0,0 +1,16 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_MESSAGES_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_MESSAGES_H_ + +#include "create_message.h" +#include "looping_message.h" +#include "mix_with_others_message.h" +#include "playback_speed_message.h" +#include "position_message.h" +#include "texture_message.h" +#include "volume_message.h" + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_MESSAGES_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/mix_with_others_message.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/mix_with_others_message.h new file mode 100644 index 00000000..78798a5e --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/mix_with_others_message.h @@ -0,0 +1,52 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_MIX_WITH_OTHERS_MESSAGE_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_MIX_WITH_OTHERS_MESSAGE_H_ + +#include +#include + +class MixWithOthersMessage { + public: + MixWithOthersMessage() = default; + ~MixWithOthersMessage() = default; + + // Prevent copying. + MixWithOthersMessage(MixWithOthersMessage const&) = default; + MixWithOthersMessage& operator=(MixWithOthersMessage const&) = default; + + void SetMixWithOthers(bool mixWithOthers) { + mix_with_others_ = mixWithOthers; + } + + bool GetMixWithOthers() const { return mix_with_others_; } + + flutter::EncodableValue ToMap() { + flutter::EncodableMap map = {{flutter::EncodableValue("mixWithOthers"), + flutter::EncodableValue(mix_with_others_)}}; + + return flutter::EncodableValue(map); + } + + static MixWithOthersMessage FromMap(const flutter::EncodableValue& value) { + MixWithOthersMessage message; + if (std::holds_alternative(value)) { + auto map = std::get(value); + + flutter::EncodableValue& mixWithOthers = + map[flutter::EncodableValue("mixWithOthers")]; + if (std::holds_alternative(mixWithOthers)) { + message.SetMixWithOthers(std::get(mixWithOthers)); + } + } + + return message; + } + + private: + bool mix_with_others_ = false; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_MIX_WITH_OTHERS_MESSAGE_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/playback_speed_message.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/playback_speed_message.h new file mode 100644 index 00000000..147da225 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/playback_speed_message.h @@ -0,0 +1,62 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_PLAYBACK_SPEED_MESSAGE_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_PLAYBACK_SPEED_MESSAGE_H_ + +#include +#include + +class PlaybackSpeedMessage { + public: + PlaybackSpeedMessage() = default; + ~PlaybackSpeedMessage() = default; + + // Prevent copying. + PlaybackSpeedMessage(PlaybackSpeedMessage const&) = default; + PlaybackSpeedMessage& operator=(PlaybackSpeedMessage const&) = default; + + void SetTextureId(int64_t texture_id) { texture_id_ = texture_id; } + + int64_t GetTextureId() const { return texture_id_; } + + void SetSpeed(double speed) { speed_ = speed; } + + double GetSpeed() const { return speed_; } + + flutter::EncodableValue ToMap() { + flutter::EncodableMap map = { + {flutter::EncodableValue("textureId"), + flutter::EncodableValue(texture_id_)}, + {flutter::EncodableValue("speed"), flutter::EncodableValue(speed_)}}; + return flutter::EncodableValue(map); + } + + static PlaybackSpeedMessage FromMap(const flutter::EncodableValue& value) { + PlaybackSpeedMessage message; + if (std::holds_alternative(value)) { + auto map = std::get(value); + + flutter::EncodableValue& texture_id = + map[flutter::EncodableValue("textureId")]; + if (std::holds_alternative(texture_id) || + std::holds_alternative(texture_id)) { + message.SetTextureId(texture_id.LongValue()); + } + + flutter::EncodableValue& speed = map[flutter::EncodableValue("speed")]; + if (std::holds_alternative(speed)) { + message.SetSpeed(std::get(speed)); + } + } + + return message; + } + + private: + int64_t texture_id_ = 0; + double speed_ = 1.0; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_PLAYBACK_SPEED_MESSAGE_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/position_message.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/position_message.h new file mode 100644 index 00000000..b1ce2f5b --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/position_message.h @@ -0,0 +1,65 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_POSITION_MESSAGE_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_POSITION_MESSAGE_H_ + +#include +#include + +class PositionMessage { + public: + PositionMessage() = default; + ~PositionMessage() = default; + + // Prevent copying. + PositionMessage(PositionMessage const&) = default; + PositionMessage& operator=(PositionMessage const&) = default; + + void SetTextureId(int64_t texture_id) { texture_id_ = texture_id; } + + int64_t GetTextureId() const { return texture_id_; } + + void SetPosition(int64_t position) { position_ = position; } + + int64_t GetPosition() const { return position_; } + + flutter::EncodableValue ToMap() { + flutter::EncodableMap toMapResult = {{flutter::EncodableValue("textureId"), + flutter::EncodableValue(texture_id_)}, + {flutter::EncodableValue("position"), + flutter::EncodableValue(position_)}}; + + return flutter::EncodableValue(toMapResult); + } + + static PositionMessage FromMap(const flutter::EncodableValue& value) { + PositionMessage message; + if (std::holds_alternative(value)) { + auto map = std::get(value); + + flutter::EncodableValue& texture_id = + map[flutter::EncodableValue("textureId")]; + if (std::holds_alternative(texture_id) || + std::holds_alternative(texture_id)) { + message.SetTextureId(texture_id.LongValue()); + } + + flutter::EncodableValue& position = + map[flutter::EncodableValue("position")]; + if (std::holds_alternative(position) || + std::holds_alternative(position)) { + message.SetPosition(position.LongValue()); + } + } + + return message; + } + + private: + int64_t texture_id_ = 0; + int64_t position_ = 0; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_POSITION_MESSAGE_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/texture_message.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/texture_message.h new file mode 100644 index 00000000..8b730184 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/texture_message.h @@ -0,0 +1,49 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_TEXTURE_MESSAGE_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_TEXTURE_MESSAGE_H_ + +#include +#include + +class TextureMessage { + public: + TextureMessage() = default; + ~TextureMessage() = default; + + // Prevent copying. + TextureMessage(TextureMessage const&) = default; + TextureMessage& operator=(TextureMessage const&) = default; + + void SetTextureId(int64_t texture_id) { texture_id_ = texture_id; } + + int64_t GetTextureId() const { return texture_id_; } + + flutter::EncodableValue ToMap() { + flutter::EncodableMap map = {{flutter::EncodableValue("textureId"), + flutter::EncodableValue(texture_id_)}}; + return flutter::EncodableValue(map); + } + + static TextureMessage FromMap(const flutter::EncodableValue& value) { + TextureMessage message; + if (std::holds_alternative(value)) { + auto map = std::get(value); + + flutter::EncodableValue& texture_id = + map[flutter::EncodableValue("textureId")]; + if (std::holds_alternative(texture_id) || + std::holds_alternative(texture_id)) { + message.SetTextureId(texture_id.LongValue()); + } + } + return message; + } + + private: + int64_t texture_id_ = 0; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_TEXTURE_MESSAGE_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/volume_message.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/volume_message.h new file mode 100644 index 00000000..dd8d1574 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/messages/volume_message.h @@ -0,0 +1,62 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_VOLUME_MESSAGE_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_VOLUME_MESSAGE_H_ + +#include +#include + +class VolumeMessage { + public: + VolumeMessage() = default; + ~VolumeMessage() = default; + + // Prevent copying. + VolumeMessage(VolumeMessage const&) = default; + VolumeMessage& operator=(VolumeMessage const&) = default; + + void SetTextureId(int64_t texture_id) { texture_id_ = texture_id; } + + int64_t GetTextureId() const { return texture_id_; } + + void SetVolume(double volume) { volume_ = volume; } + + double GetVolume() const { return volume_; } + + flutter::EncodableValue ToMap() { + flutter::EncodableMap map = { + {flutter::EncodableValue("textureId"), + flutter::EncodableValue(texture_id_)}, + {flutter::EncodableValue("volume"), flutter::EncodableValue(volume_)}}; + return flutter::EncodableValue(map); + } + + static VolumeMessage FromMap(const flutter::EncodableValue& value) { + VolumeMessage message; + if (std::holds_alternative(value)) { + auto map = std::get(value); + + flutter::EncodableValue& texture_id = + map[flutter::EncodableValue("textureId")]; + if (std::holds_alternative(texture_id) || + std::holds_alternative(texture_id)) { + message.SetTextureId(texture_id.LongValue()); + } + + flutter::EncodableValue& volume = map[flutter::EncodableValue("volume")]; + if (std::holds_alternative(volume)) { + message.SetVolume(std::get(volume)); + } + } + + return message; + } + + private: + int64_t texture_id_ = 0; + double volume_ = 0; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_MESSAGES_VOLUME_MESSAGE_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_plugin.cc b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_plugin.cc new file mode 100644 index 00000000..ff99793e --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_plugin.cc @@ -0,0 +1,590 @@ +// Copyright 2021 Sony Group Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "include/video_player/video_player_plugin.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "gst_video_player.h" +#include "messages/messages.h" +#include "video_player_stream_handler_impl.h" + +namespace { +constexpr char kVideoPlayerApiChannelInitializeName[] = + "dev.flutter.pigeon.VideoPlayerApi.initialize"; +constexpr char kVideoPlayerApiChannelSetMixWithOthersName[] = + "dev.flutter.pigeon.VideoPlayerApi.setMixWithOthers"; +constexpr char kVideoPlayerApiChannelCreateName[] = + "dev.flutter.pigeon.VideoPlayerApi.create"; +constexpr char kVideoPlayerApiChannelDisposeName[] = + "dev.flutter.pigeon.VideoPlayerApi.dispose"; +constexpr char kVideoPlayerApiChannelSetLoopingName[] = + "dev.flutter.pigeon.VideoPlayerApi.setLooping"; +constexpr char kVideoPlayerApiChannelSetVolumeName[] = + "dev.flutter.pigeon.VideoPlayerApi.setVolume"; +constexpr char kVideoPlayerApiChannelPauseName[] = + "dev.flutter.pigeon.VideoPlayerApi.pause"; +constexpr char kVideoPlayerApiChannelPlayName[] = + "dev.flutter.pigeon.VideoPlayerApi.play"; +constexpr char kVideoPlayerApiChannelPositionName[] = + "dev.flutter.pigeon.VideoPlayerApi.position"; +constexpr char kVideoPlayerApiChannelSetPlaybackSpeedName[] = + "dev.flutter.pigeon.VideoPlayerApi.setPlaybackSpeed"; +constexpr char kVideoPlayerApiChannelSeekToName[] = + "dev.flutter.pigeon.VideoPlayerApi.seekTo"; + +constexpr char kVideoPlayerVideoEventsChannelName[] = + "flutter.io/videoPlayer/videoEvents"; + +constexpr char kEncodableMapkeyResult[] = "result"; +constexpr char kEncodableMapkeyError[] = "error"; + +class VideoPlayerPlugin : public flutter::Plugin { + public: + static void RegisterWithRegistrar(flutter::PluginRegistrar* registrar); + + VideoPlayerPlugin(flutter::PluginRegistrar* plugin_registrar, + flutter::TextureRegistrar* texture_registrar) + : plugin_registrar_(plugin_registrar), + texture_registrar_(texture_registrar) { + // Needs to call 'gst_init' that initializing the GStreamer library before + // using it. + GstVideoPlayer::GstLibraryLoad(); + } + virtual ~VideoPlayerPlugin() { + for (auto itr = players_.begin(); itr != players_.end(); itr++) { + auto texture_id = itr->first; + auto* player = itr->second.get(); + player->event_sink = nullptr; + if (player->event_channel) { + player->event_channel->SetStreamHandler(nullptr); + } + player->player = nullptr; + player->buffer = nullptr; + player->texture = nullptr; + texture_registrar_->UnregisterTexture(texture_id); + } + players_.clear(); + + GstVideoPlayer::GstLibraryUnload(); + } + + private: + struct FlutterVideoPlayer { + int64_t texture_id; + std::unique_ptr player; + std::unique_ptr texture; + std::unique_ptr buffer; + std::unique_ptr> + event_channel; + std::unique_ptr> event_sink; + }; + + void HandleInitializeMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandleCreateMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandleDisposeMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandlePauseMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandlePlayMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandleSetLoopingMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandleSetVolumeMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandleSetMixWithOthersMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandleSetPlaybackSpeedMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandleSeekToMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + void HandlePositionMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply); + + void SendInitializedEventMessage(int64_t texture_id); + void SendPlayCompletedEventMessage(int64_t texture_id); + + flutter::EncodableValue WrapError(const std::string& message, + const std::string& code = std::string(), + const std::string& details = std::string()); + + flutter::PluginRegistrar* plugin_registrar_; + flutter::TextureRegistrar* texture_registrar_; + std::unordered_map> players_; +}; + +// static +void VideoPlayerPlugin::RegisterWithRegistrar( + flutter::PluginRegistrar* registrar) { + auto plugin = std::make_unique( + registrar, registrar->texture_registrar()); + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelInitializeName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleInitializeMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelCreateName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleCreateMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelDisposeName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleDisposeMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelPauseName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandlePauseMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelPlayName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandlePlayMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelSetLoopingName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleSetLoopingMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelSetVolumeName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleSetVolumeMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelSetMixWithOthersName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleSetMixWithOthersMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelSetPlaybackSpeedName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleSetPlaybackSpeedMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelSeekToName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandleSeekToMethodCall(message, reply); + }); + } + + { + auto channel = + std::make_unique>( + registrar->messenger(), kVideoPlayerApiChannelPositionName, + &flutter::StandardMessageCodec::GetInstance()); + channel->SetMessageHandler( + [plugin_pointer = plugin.get()](const auto& message, auto reply) { + plugin_pointer->HandlePositionMethodCall(message, reply); + }); + } + + registrar->AddPlugin(std::move(plugin)); +} + +void VideoPlayerPlugin::HandleInitializeMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + flutter::EncodableMap result; + + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandleCreateMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto meta = CreateMessage::FromMap(message); + std::string uri; + if (!meta.GetAsset().empty()) { + // todo: gets propery path of the Flutter project. + std::string flutter_project_path = "./bundle/data/"; + uri = flutter_project_path + "flutter_assets/" + meta.GetAsset(); + } else { + uri = meta.GetUri(); + } + + auto instance = std::make_unique(); + instance->buffer = std::make_unique(); + instance->texture = + std::make_unique(flutter::PixelBufferTexture( + [instance = instance.get()]( + size_t width, size_t height) -> const FlutterDesktopPixelBuffer* { + instance->buffer->width = instance->player->GetWidth(); + instance->buffer->height = instance->player->GetHeight(); + instance->buffer->buffer = instance->player->GetFrameBuffer(); + return instance->buffer.get(); + })); + const auto texture_id = + texture_registrar_->RegisterTexture(instance->texture.get()); + instance->texture_id = texture_id; + { + auto event_channel = + std::make_unique>( + plugin_registrar_->messenger(), + kVideoPlayerVideoEventsChannelName + std::to_string(texture_id), + &flutter::StandardMethodCodec::GetInstance()); + auto event_channel_handler = std::make_unique< + flutter::StreamHandlerFunctions>( + [instance = instance.get(), host = this]( + const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) + -> std::unique_ptr< + flutter::StreamHandlerError> { + instance->event_sink = std::move(events); + host->SendInitializedEventMessage(instance->texture_id); + return nullptr; + }, + [instance = instance.get()](const flutter::EncodableValue* arguments) + -> std::unique_ptr< + flutter::StreamHandlerError> { + instance->event_sink = nullptr; + return nullptr; + }); + event_channel->SetStreamHandler(std::move(event_channel_handler)); + instance->event_channel = std::move(event_channel); + } + { + auto player_handler = std::make_unique( + // OnNotifyInitialized + [texture_id, host = this]() { + host->SendInitializedEventMessage(texture_id); + }, + // OnNotifyFrameDecoded + [texture_id, host = this]() { + host->texture_registrar_->MarkTextureFrameAvailable(texture_id); + }, + // OnNotifyCompleted + [texture_id, host = this]() { + host->SendPlayCompletedEventMessage(texture_id); + }); + instance->player = + std::make_unique(uri, std::move(player_handler)); + players_[texture_id] = std::move(instance); + } + + flutter::EncodableMap value; + TextureMessage result; + result.SetTextureId(texture_id); + value.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + result.ToMap()); + reply(flutter::EncodableValue(value)); +} + +void VideoPlayerPlugin::HandleDisposeMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = TextureMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + auto* player = players_[texture_id].get(); + player->event_sink = nullptr; + player->event_channel->SetStreamHandler(nullptr); + player->player = nullptr; + player->buffer = nullptr; + player->texture = nullptr; + players_.erase(texture_id); + texture_registrar_->UnregisterTexture(texture_id); + + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandlePauseMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = TextureMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + players_[texture_id]->player->Pause(); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandlePlayMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = TextureMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + players_[texture_id]->player->Play(); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandleSetLoopingMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = LoopingMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + players_[texture_id]->player->SetAutoRepeat(parameter.GetIsLooping()); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandleSetVolumeMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = VolumeMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + players_[texture_id]->player->SetVolume(parameter.GetVolume()); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandleSetMixWithOthersMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + // todo: implements here. + + flutter::EncodableMap result; + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandlePositionMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = TextureMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + PositionMessage send_message; + send_message.SetTextureId(texture_id); + send_message.SetPosition( + players_[texture_id]->player->GetCurrentPosition()); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + send_message.ToMap()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandleSetPlaybackSpeedMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = PlaybackSpeedMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + players_[texture_id]->player->SetPlaybackRate(parameter.GetSpeed()); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::HandleSeekToMethodCall( + const flutter::EncodableValue& message, + flutter::MessageReply reply) { + auto parameter = PositionMessage::FromMap(message); + const auto texture_id = parameter.GetTextureId(); + flutter::EncodableMap result; + + if (players_.find(texture_id) != players_.end()) { + players_[texture_id]->player->SetSeek(parameter.GetPosition()); + result.emplace(flutter::EncodableValue(kEncodableMapkeyResult), + flutter::EncodableValue()); + } else { + auto error_message = "Couldn't find the player with texture id: " + + std::to_string(texture_id); + result.emplace(flutter::EncodableValue(kEncodableMapkeyError), + flutter::EncodableValue(WrapError(error_message))); + } + reply(flutter::EncodableValue(result)); +} + +void VideoPlayerPlugin::SendInitializedEventMessage(int64_t texture_id) { + if (players_.find(texture_id) == players_.end() || + !players_[texture_id]->event_sink) { + return; + } + + auto duration = players_[texture_id]->player->GetDuration(); + auto width = players_[texture_id]->player->GetWidth(); + auto height = players_[texture_id]->player->GetHeight(); + flutter::EncodableMap encodables = { + {flutter::EncodableValue("event"), + flutter::EncodableValue("initialized")}, + {flutter::EncodableValue("duration"), flutter::EncodableValue(duration)}, + {flutter::EncodableValue("width"), flutter::EncodableValue(width)}, + {flutter::EncodableValue("height"), flutter::EncodableValue(height)}}; + flutter::EncodableValue event(encodables); + players_[texture_id]->event_sink->Success(event); +} + +void VideoPlayerPlugin::SendPlayCompletedEventMessage(int64_t texture_id) { + if (players_.find(texture_id) == players_.end() || + !players_[texture_id]->event_sink) { + return; + } + + flutter::EncodableMap encodables = { + {flutter::EncodableValue("event"), flutter::EncodableValue("completed")}}; + flutter::EncodableValue event(encodables); + players_[texture_id]->event_sink->Success(event); +} + +flutter::EncodableValue VideoPlayerPlugin::WrapError( + const std::string& message, const std::string& code, + const std::string& details) { + flutter::EncodableMap map = { + {flutter::EncodableValue("message"), flutter::EncodableValue(message)}, + {flutter::EncodableValue("code"), flutter::EncodableValue(code)}, + {flutter::EncodableValue("details"), flutter::EncodableValue(details)}}; + return flutter::EncodableValue(map); +} + +} // namespace + +void VideoPlayerPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar) { + VideoPlayerPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarManager::GetInstance() + ->GetRegistrar(registrar)); +} diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_stream_handler.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_stream_handler.h new file mode 100644 index 00000000..cf63c4aa --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_stream_handler.h @@ -0,0 +1,32 @@ +// Copyright 2021 Sony Group Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_VIDEO_PLAYER_STREAM_HANDLER_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_VIDEO_PLAYER_STREAM_HANDLER_H_ + +class VideoPlayerStreamHandler { + public: + VideoPlayerStreamHandler() = default; + virtual ~VideoPlayerStreamHandler() = default; + + // Prevent copying. + VideoPlayerStreamHandler(VideoPlayerStreamHandler const&) = delete; + VideoPlayerStreamHandler& operator=(VideoPlayerStreamHandler const&) = delete; + + // Notifies the completion of initializing the video player. + void OnNotifyInitialized() { OnNotifyInitializedInternal(); } + + // Notifies the completion of decoding a video frame. + void OnNotifyFrameDecoded() { OnNotifyFrameDecodedInternal(); } + + // Notifies the completion of playing a video. + void OnNotifyCompleted() { OnNotifyCompletedInternal(); } + + protected: + virtual void OnNotifyInitializedInternal() = 0; + virtual void OnNotifyFrameDecodedInternal() = 0; + virtual void OnNotifyCompletedInternal() = 0; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_VIDEO_PLAYER_STREAM_HANDLER_H_ diff --git a/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_stream_handler_impl.h b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_stream_handler_impl.h new file mode 100644 index 00000000..7b955b0f --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter/plugins/video_player/elinux/video_player_stream_handler_impl.h @@ -0,0 +1,58 @@ +// Copyright 2021 Sony Group Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_VIDEO_PLAYER_STREAM_HANDLER_IMPL_H_ +#define PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_VIDEO_PLAYER_STREAM_HANDLER_IMPL_H_ + +#include + +#include "video_player_stream_handler.h" + +class VideoPlayerStreamHandlerImpl : public VideoPlayerStreamHandler { + public: + using OnNotifyInitialized = std::function; + using OnNotifyFrameDecoded = std::function; + using OnNotifyCompleted = std::function; + + VideoPlayerStreamHandlerImpl(OnNotifyInitialized on_notify_initialized, + OnNotifyFrameDecoded on_notify_frame_decoded, + OnNotifyCompleted on_notify_completed) + : on_notify_initialized_(on_notify_initialized), + on_notify_frame_decoded_(on_notify_frame_decoded), + on_notify_completed_(on_notify_completed) {} + virtual ~VideoPlayerStreamHandlerImpl() = default; + + // Prevent copying. + VideoPlayerStreamHandlerImpl(VideoPlayerStreamHandlerImpl const&) = delete; + VideoPlayerStreamHandlerImpl& operator=(VideoPlayerStreamHandlerImpl const&) = + delete; + + protected: + // |VideoPlayerStreamHandler| + void OnNotifyInitializedInternal() { + if (on_notify_initialized_) { + on_notify_initialized_(); + } + } + + // |VideoPlayerStreamHandler| + void OnNotifyFrameDecodedInternal() { + if (on_notify_frame_decoded_) { + on_notify_frame_decoded_(); + } + } + + // |VideoPlayerStreamHandler| + void OnNotifyCompletedInternal() { + if (on_notify_completed_) { + on_notify_completed_(); + } + } + + OnNotifyInitialized on_notify_initialized_; + OnNotifyFrameDecoded on_notify_frame_decoded_; + OnNotifyCompleted on_notify_completed_; +}; + +#endif // PACKAGES_VIDEO_PLAYER_VIDEO_PLAYER_ELINUX_VIDEO_PLAYER_STREAM_HANDLER_IMPL_H_ diff --git a/examples/flutter-video-player-plugin/flutter_embedder_options.h b/examples/flutter-video-player-plugin/flutter_embedder_options.h new file mode 100644 index 00000000..41d0bd10 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter_embedder_options.h @@ -0,0 +1,203 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_EMBEDDER_OPTIONS_ +#define FLUTTER_EMBEDDER_OPTIONS_ + +#include + +#include + +#include "command_options.h" + +class FlutterEmbedderOptions { + public: + FlutterEmbedderOptions() { + options_.AddString("bundle", "b", "Path to Flutter project bundle", + "./bundle", true); + options_.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", + false); + options_.AddInt("rotation", "r", + "Window rotation(degree) [0(default)|90|180|270]", 0, + false); + options_.AddDouble("text-scaling-factor", "x", "Text scaling factor", 1.0, + false); + options_.AddWithoutValue("enable-high-contrast", "i", + "Request that UI be rendered with darker colors.", + false); + options_.AddDouble("force-scale-factor", "s", + "Force a scale factor instead using default value", 1.0, + false); + options_.AddWithoutValue( + "async-vblank", "v", + "Don't sync to compositor redraw/vblank (eglSwapInterval 0)", false); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + // no more options. +#elif defined(FLUTTER_TARGET_BACKEND_X11) + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddString("app-id", "a", "XDG App ID", "dev.flutter.elinux", + false); + options_.AddWithoutValue("onscreen-keyboard", "k", + "Enable on-screen keyboard", false); + options_.AddWithoutValue("window-decoration", "d", + "Enable window decorations", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#endif + } + ~FlutterEmbedderOptions() = default; + + bool Parse(int argc, char** argv) { + if (!options_.Parse(argc, argv)) { + std::cerr << options_.GetError() << std::endl; + std::cout << options_.ShowHelp(); + return false; + } + + bundle_path_ = options_.GetValue("bundle"); + use_mouse_cursor_ = !options_.Exist("no-cursor"); + if (options_.Exist("rotation")) { + switch (options_.GetValue("rotation")) { + case 90: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_90; + break; + case 180: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_180; + break; + case 270: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_270; + break; + default: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + break; + } + } + + text_scale_factor_ = options_.GetValue("text-scaling-factor"); + enable_high_contrast_ = options_.Exist("enable-high-contrast"); + + if (options_.Exist("force-scale-factor")) { + is_force_scale_factor_ = true; + scale_factor_ = options_.GetValue("force-scale-factor"); + } else { + is_force_scale_factor_ = false; + scale_factor_ = 1.0; + } + + enable_vsync_ = !options_.Exist("async-vblank"); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_view_mode_ = flutter::FlutterViewController::ViewMode::kFullscreen; +#elif defined(FLUTTER_TARGET_BACKEND_X11) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_title_ = options_.GetValue("title"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + window_title_ = options_.GetValue("title"); + window_app_id_ = options_.GetValue("app-id"); + use_onscreen_keyboard_ = options_.Exist("onscreen-keyboard"); + use_window_decoration_ = options_.Exist("window-decoration"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#endif + + return true; + } + + std::string BundlePath() const { + return bundle_path_; + } + std::string WindowTitle() const { + return window_title_; + } + std::string WindowAppID() const { + return window_app_id_; + } + bool IsUseMouseCursor() const { + return use_mouse_cursor_; + } + bool IsUseOnscreenKeyboard() const { + return use_onscreen_keyboard_; + } + bool IsUseWindowDecoraation() const { + return use_window_decoration_; + } + flutter::FlutterViewController::ViewMode WindowViewMode() const { + return window_view_mode_; + } + int WindowWidth() const { + return window_width_; + } + int WindowHeight() const { + return window_height_; + } + flutter::FlutterViewController::ViewRotation WindowRotation() const { + return window_view_rotation_; + } + double TextScaleFactor() const { + return text_scale_factor_; + } + bool EnableHighContrast() const { + return enable_high_contrast_; + } + bool IsForceScaleFactor() const { + return is_force_scale_factor_; + } + double ScaleFactor() const { + return scale_factor_; + } + bool EnableVsync() const { + return enable_vsync_; + } + + private: + commandline::CommandOptions options_; + + std::string bundle_path_; + std::string window_title_; + std::string window_app_id_; + bool use_mouse_cursor_ = true; + bool use_onscreen_keyboard_ = false; + bool use_window_decoration_ = false; + flutter::FlutterViewController::ViewMode window_view_mode_ = + flutter::FlutterViewController::ViewMode::kNormal; + int window_width_ = 1280; + int window_height_ = 720; + flutter::FlutterViewController::ViewRotation window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + bool is_force_scale_factor_; + double scale_factor_; + double text_scale_factor_; + bool enable_high_contrast_; + bool enable_vsync_; +}; + +#endif // FLUTTER_EMBEDDER_OPTIONS_ diff --git a/examples/flutter-video-player-plugin/flutter_window.cc b/examples/flutter-video-player-plugin/flutter_window.cc new file mode 100644 index 00000000..0c5b6397 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter_window.cc @@ -0,0 +1,79 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter_window.h" + +#include +#include +#include +#include + +#include "flutter/generated_plugin_registrant.h" + +FlutterWindow::FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project) + : view_properties_(view_properties), project_(project) {} + +bool FlutterWindow::OnCreate() { + flutter_view_controller_ = std::make_unique( + view_properties_, project_); + + // Ensure that basic setup of the controller was successful. + if (!flutter_view_controller_->engine() || + !flutter_view_controller_->view()) { + return false; + } + + // Register Flutter plugins. + RegisterPlugins(flutter_view_controller_->engine()); + + return true; +} + +void FlutterWindow::OnDestroy() { + if (flutter_view_controller_) { + flutter_view_controller_ = nullptr; + } +} + +void FlutterWindow::Run() { + // Main loop. + auto next_flutter_event_time = + std::chrono::steady_clock::time_point::clock::now(); + while (flutter_view_controller_->view()->DispatchEvent()) { + // Wait until the next event. + { + auto wait_duration = + std::max(std::chrono::nanoseconds(0), + next_flutter_event_time - + std::chrono::steady_clock::time_point::clock::now()); + std::this_thread::sleep_for( + std::chrono::duration_cast(wait_duration)); + } + + // Processes any pending events in the Flutter engine, and returns the + // number of nanoseconds until the next scheduled event (or max, if none). + auto wait_duration = flutter_view_controller_->engine()->ProcessMessages(); + { + auto next_event_time = std::chrono::steady_clock::time_point::max(); + if (wait_duration != std::chrono::nanoseconds::max()) { + next_event_time = + std::min(next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + wait_duration); + } else { + // Wait for the next frame if no events. + auto frame_rate = flutter_view_controller_->view()->GetFrameRate(); + next_event_time = std::min( + next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + std::chrono::milliseconds( + static_cast(std::trunc(1000000.0 / frame_rate)))); + } + next_flutter_event_time = + std::max(next_flutter_event_time, next_event_time); + } + } +} diff --git a/examples/flutter-video-player-plugin/flutter_window.h b/examples/flutter-video-player-plugin/flutter_window.h new file mode 100644 index 00000000..20b9cb88 --- /dev/null +++ b/examples/flutter-video-player-plugin/flutter_window.h @@ -0,0 +1,34 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_WINDOW_ +#define FLUTTER_WINDOW_ + +#include +#include + +#include + +class FlutterWindow { + public: + explicit FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project); + ~FlutterWindow() = default; + + // Prevent copying. + FlutterWindow(FlutterWindow const&) = delete; + FlutterWindow& operator=(FlutterWindow const&) = delete; + + bool OnCreate(); + void OnDestroy(); + void Run(); + + private: + flutter::FlutterViewController::ViewProperties view_properties_; + flutter::DartProject project_; + std::unique_ptr flutter_view_controller_; +}; + +#endif // FLUTTER_WINDOW_ diff --git a/examples/flutter-video-player-plugin/main.cc b/examples/flutter-video-player-plugin/main.cc new file mode 100644 index 00000000..579daee6 --- /dev/null +++ b/examples/flutter-video-player-plugin/main.cc @@ -0,0 +1,53 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include +#include + +#include +#include +#include + +#include "flutter_embedder_options.h" +#include "flutter_window.h" + +int main(int argc, char** argv) { + FlutterEmbedderOptions options; + if (!options.Parse(argc, argv)) { + return 0; + } + + // Creates the Flutter project. + const auto bundle_path = options.BundlePath(); + const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); + flutter::DartProject project(fl_path); + auto command_line_arguments = std::vector(); + project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); + + flutter::FlutterViewController::ViewProperties view_properties = {}; + view_properties.width = options.WindowWidth(); + view_properties.height = options.WindowHeight(); + view_properties.view_mode = options.WindowViewMode(); + view_properties.view_rotation = options.WindowRotation(); + view_properties.title = options.WindowTitle(); + view_properties.app_id = options.WindowAppID(); + view_properties.use_mouse_cursor = options.IsUseMouseCursor(); + view_properties.use_onscreen_keyboard = options.IsUseOnscreenKeyboard(); + view_properties.use_window_decoration = options.IsUseWindowDecoraation(); + view_properties.text_scale_factor = options.TextScaleFactor(); + view_properties.enable_high_contrast = options.EnableHighContrast(); + view_properties.force_scale_factor = options.IsForceScaleFactor(); + view_properties.scale_factor = options.ScaleFactor(); + view_properties.enable_vsync = options.EnableVsync(); + + // The Flutter instance hosted by this window. + FlutterWindow window(view_properties, project); + if (!window.OnCreate()) { + return 0; + } + window.Run(); + window.OnDestroy(); + + return 0; +} diff --git a/examples/flutter-wayland-client/cmake/user_build.cmake b/examples/flutter-wayland-client/cmake/user_build.cmake index 2c73e8cb..8bcd69ae 100644 --- a/examples/flutter-wayland-client/cmake/user_build.cmake +++ b/examples/flutter-wayland-client/cmake/user_build.cmake @@ -5,6 +5,8 @@ set(TARGET flutter-client) # source files for user apps. set(USER_APP_SRCS + examples/flutter-wayland-client/flutter/generated_plugin_registrant.cc + examples/flutter-wayland-client/flutter_window.cc examples/flutter-wayland-client/main.cc ) @@ -13,13 +15,13 @@ set(USER_APP_INCLUDE_DIRS ## Public APIs for developers (Don't edit!). src/client_wrapper/include src/flutter/shell/platform/common/client_wrapper + src/flutter/shell/platform/common/client_wrapper/include src/flutter/shell/platform/common/client_wrapper/include/flutter src/flutter/shell/platform/common/public src/flutter/shell/platform/linux_embedded/public - src/public/include ## header file include path for user apps. examples/flutter-wayland-client ) # link libraries for user apps. -set(USER_APP_LIBRARIES "") +set(USER_APP_LIBRARIES) diff --git a/examples/flutter-wayland-client/cmake/user_config.cmake b/examples/flutter-wayland-client/cmake/user_config.cmake index 919c51d2..5f72786b 100644 --- a/examples/flutter-wayland-client/cmake/user_config.cmake +++ b/examples/flutter-wayland-client/cmake/user_config.cmake @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.10) # Flutter embedder configurations. -# See: https://github.com/sony/flutter-embedded-linux/doc/README.md#user-configuration-parameters-cmake-options +# See: https://github.com/sony/flutter-embedded-linux/wiki/Building-Embedded-Linux-embedding-for-Flutter#user-configuration-parameters-cmake-options set(BACKEND_TYPE WAYLAND) -set(DESKTOP_SHELL OFF) -set(USE_VIRTUAL_KEYBOARD OFF) set(USE_GLES3 OFF) diff --git a/examples/flutter-wayland-client/command_options.h b/examples/flutter-wayland-client/command_options.h index e051a043..b0de9316 100644 --- a/examples/flutter-wayland-client/command_options.h +++ b/examples/flutter-wayland-client/command_options.h @@ -1,7 +1,10 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2022 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef COMMAND_OPTIONS_ +#define COMMAND_OPTIONS_ + #include #include #include @@ -10,8 +13,6 @@ #include #include -// todo: Supports other types besides int, string. - namespace commandline { namespace { @@ -36,30 +37,49 @@ class CommandOptions { CommandOptions() = default; ~CommandOptions() = default; - void AddWithoutValue(const std::string& name, const std::string& short_name, - const std::string& description, bool required) { + void AddWithoutValue(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required) { Add(name, short_name, description, "", ReaderString(), required, false); } - void AddInt(const std::string& name, const std::string& short_name, - const std::string& description, const int& default_value, + void AddInt(const std::string& name, + const std::string& short_name, + const std::string& description, + const int& default_value, bool required) { Add(name, short_name, description, default_value, ReaderInt(), required, true); } - void AddString(const std::string& name, const std::string& short_name, + void AddDouble(const std::string& name, + const std::string& short_name, + const std::string& description, + const double& default_value, + bool required) { + Add(name, short_name, description, default_value, + ReaderDouble(), required, true); + } + + void AddString(const std::string& name, + const std::string& short_name, const std::string& description, - const std::string& default_value, bool required) { + const std::string& default_value, + bool required) { Add(name, short_name, description, default_value, ReaderString(), required, true); } template - void Add(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader = F(), bool required = true, bool required_value = true) { + void Add(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader = F(), + bool required = true, + bool required_value = true) { if (options_.find(name) != options_.end()) { std::cerr << "Already registered option: " << name << std::endl; return; @@ -142,7 +162,7 @@ class CommandOptions { // short options: e.g. -f /foo/file.txt -h 640 -abc else if (arg.length() > 1 && arg.substr(0, 1).compare(kOptionStyleShort) == 0) { - for (int j = 1; j < arg.length(); j++) { + for (size_t j = 1; j < arg.length(); j++) { const std::string option_name{argv[i][j]}; if (lut_short_options_.find(option_name) == @@ -167,7 +187,7 @@ class CommandOptions { } } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired() && !registration_order_options_[i]->HasValue()) { errors_.push_back( @@ -187,7 +207,7 @@ class CommandOptions { std::ostringstream ostream; ostream << "Usage: " << command_name_ << " "; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired()) { ostream << registration_order_options_[i]->GetHelpShortMessage() << " "; } @@ -196,12 +216,12 @@ class CommandOptions { ostream << "Global options:" << std::endl; size_t max_name_len = 0; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { max_name_len = std::max( max_name_len, registration_order_options_[i]->GetName().length()); } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (!registration_order_options_[i]->GetShortName().empty()) { ostream << kOptionStyleShort << registration_order_options_[i]->GetShortName() << ", "; @@ -210,7 +230,7 @@ class CommandOptions { } size_t index_adjust = 0; - constexpr int kSpacerNum = 5; + constexpr int kSpacerNum = 10; auto need_value = registration_order_options_[i]->IsRequiredValue(); ostream << kOptionStyleNormal << registration_order_options_[i]->GetName(); @@ -237,10 +257,17 @@ class CommandOptions { std::string operator()(const std::string& value) { return value; } }; + struct ReaderDouble { + double operator()(const std::string& value) { return std::stod(value); } + }; + class Option { public: - Option(const std::string& name, const std::string& short_name, - const std::string& description, bool required, bool required_value) + Option(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required, + bool required_value) : name_(name), short_name_(short_name), description_(description), @@ -274,20 +301,23 @@ class CommandOptions { virtual bool HasValue() const = 0; protected: - bool is_required_; - bool is_required_value_; std::string name_; std::string short_name_; std::string description_; + bool is_required_; + bool is_required_value_; bool value_set_; }; template class OptionValue : public Option { public: - OptionValue(const std::string& name, const std::string& short_name, - const std::string& description, const T& default_value, - bool required, bool required_value) + OptionValue(const std::string& name, + const std::string& short_name, + const std::string& description, + const T& default_value, + bool required, + bool required_value) : Option(name, short_name, description, required, required_value), default_value_(default_value), value_(default_value){}; @@ -313,10 +343,18 @@ class CommandOptions { template class OptionValueReader : public OptionValue { public: - OptionValueReader(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader, bool required, bool required_value) - : OptionValue(name, short_name, description, default_value, required, + OptionValueReader(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader, + bool required, + bool required_value) + : OptionValue(name, + short_name, + description, + default_value, + required, required_value), reader_(reader) {} ~OptionValueReader() = default; @@ -360,3 +398,5 @@ class CommandOptions { }; } // namespace commandline + +#endif // COMMAND_OPTIONS_ diff --git a/examples/flutter-wayland-client/flutter/generated_plugin_registrant.cc b/examples/flutter-wayland-client/flutter/generated_plugin_registrant.cc new file mode 100644 index 00000000..7e6e6c2d --- /dev/null +++ b/examples/flutter-wayland-client/flutter/generated_plugin_registrant.cc @@ -0,0 +1,10 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + +void RegisterPlugins(flutter::PluginRegistry* registry) { +} diff --git a/examples/flutter-wayland-client/flutter/generated_plugin_registrant.h b/examples/flutter-wayland-client/flutter/generated_plugin_registrant.h new file mode 100644 index 00000000..a31c23cd --- /dev/null +++ b/examples/flutter-wayland-client/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/examples/flutter-wayland-client/flutter/generated_plugins.cmake b/examples/flutter-wayland-client/flutter/generated_plugins.cmake new file mode 100644 index 00000000..fcbf0d6e --- /dev/null +++ b/examples/flutter-wayland-client/flutter/generated_plugins.cmake @@ -0,0 +1,22 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory( + ${USER_PROJECT_PATH}/flutter/plugins/${plugin}/elinux plugins/${plugin}) + + target_link_libraries(${TARGET} + PRIVATE + ${plugin}_plugin + ) + + list(APPEND PLUGIN_BUNDLED_LIBRARIES + ${PROJECT_BINARY_DIR}/plugins/${plugin}/lib${plugin}_plugin.so + ) +endforeach(plugin) diff --git a/examples/flutter-wayland-client/flutter_embedder_options.h b/examples/flutter-wayland-client/flutter_embedder_options.h new file mode 100644 index 00000000..41d0bd10 --- /dev/null +++ b/examples/flutter-wayland-client/flutter_embedder_options.h @@ -0,0 +1,203 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_EMBEDDER_OPTIONS_ +#define FLUTTER_EMBEDDER_OPTIONS_ + +#include + +#include + +#include "command_options.h" + +class FlutterEmbedderOptions { + public: + FlutterEmbedderOptions() { + options_.AddString("bundle", "b", "Path to Flutter project bundle", + "./bundle", true); + options_.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", + false); + options_.AddInt("rotation", "r", + "Window rotation(degree) [0(default)|90|180|270]", 0, + false); + options_.AddDouble("text-scaling-factor", "x", "Text scaling factor", 1.0, + false); + options_.AddWithoutValue("enable-high-contrast", "i", + "Request that UI be rendered with darker colors.", + false); + options_.AddDouble("force-scale-factor", "s", + "Force a scale factor instead using default value", 1.0, + false); + options_.AddWithoutValue( + "async-vblank", "v", + "Don't sync to compositor redraw/vblank (eglSwapInterval 0)", false); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + // no more options. +#elif defined(FLUTTER_TARGET_BACKEND_X11) + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddString("app-id", "a", "XDG App ID", "dev.flutter.elinux", + false); + options_.AddWithoutValue("onscreen-keyboard", "k", + "Enable on-screen keyboard", false); + options_.AddWithoutValue("window-decoration", "d", + "Enable window decorations", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#endif + } + ~FlutterEmbedderOptions() = default; + + bool Parse(int argc, char** argv) { + if (!options_.Parse(argc, argv)) { + std::cerr << options_.GetError() << std::endl; + std::cout << options_.ShowHelp(); + return false; + } + + bundle_path_ = options_.GetValue("bundle"); + use_mouse_cursor_ = !options_.Exist("no-cursor"); + if (options_.Exist("rotation")) { + switch (options_.GetValue("rotation")) { + case 90: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_90; + break; + case 180: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_180; + break; + case 270: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_270; + break; + default: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + break; + } + } + + text_scale_factor_ = options_.GetValue("text-scaling-factor"); + enable_high_contrast_ = options_.Exist("enable-high-contrast"); + + if (options_.Exist("force-scale-factor")) { + is_force_scale_factor_ = true; + scale_factor_ = options_.GetValue("force-scale-factor"); + } else { + is_force_scale_factor_ = false; + scale_factor_ = 1.0; + } + + enable_vsync_ = !options_.Exist("async-vblank"); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_view_mode_ = flutter::FlutterViewController::ViewMode::kFullscreen; +#elif defined(FLUTTER_TARGET_BACKEND_X11) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_title_ = options_.GetValue("title"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + window_title_ = options_.GetValue("title"); + window_app_id_ = options_.GetValue("app-id"); + use_onscreen_keyboard_ = options_.Exist("onscreen-keyboard"); + use_window_decoration_ = options_.Exist("window-decoration"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#endif + + return true; + } + + std::string BundlePath() const { + return bundle_path_; + } + std::string WindowTitle() const { + return window_title_; + } + std::string WindowAppID() const { + return window_app_id_; + } + bool IsUseMouseCursor() const { + return use_mouse_cursor_; + } + bool IsUseOnscreenKeyboard() const { + return use_onscreen_keyboard_; + } + bool IsUseWindowDecoraation() const { + return use_window_decoration_; + } + flutter::FlutterViewController::ViewMode WindowViewMode() const { + return window_view_mode_; + } + int WindowWidth() const { + return window_width_; + } + int WindowHeight() const { + return window_height_; + } + flutter::FlutterViewController::ViewRotation WindowRotation() const { + return window_view_rotation_; + } + double TextScaleFactor() const { + return text_scale_factor_; + } + bool EnableHighContrast() const { + return enable_high_contrast_; + } + bool IsForceScaleFactor() const { + return is_force_scale_factor_; + } + double ScaleFactor() const { + return scale_factor_; + } + bool EnableVsync() const { + return enable_vsync_; + } + + private: + commandline::CommandOptions options_; + + std::string bundle_path_; + std::string window_title_; + std::string window_app_id_; + bool use_mouse_cursor_ = true; + bool use_onscreen_keyboard_ = false; + bool use_window_decoration_ = false; + flutter::FlutterViewController::ViewMode window_view_mode_ = + flutter::FlutterViewController::ViewMode::kNormal; + int window_width_ = 1280; + int window_height_ = 720; + flutter::FlutterViewController::ViewRotation window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + bool is_force_scale_factor_; + double scale_factor_; + double text_scale_factor_; + bool enable_high_contrast_; + bool enable_vsync_; +}; + +#endif // FLUTTER_EMBEDDER_OPTIONS_ diff --git a/examples/flutter-wayland-client/flutter_window.cc b/examples/flutter-wayland-client/flutter_window.cc new file mode 100644 index 00000000..0c5b6397 --- /dev/null +++ b/examples/flutter-wayland-client/flutter_window.cc @@ -0,0 +1,79 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter_window.h" + +#include +#include +#include +#include + +#include "flutter/generated_plugin_registrant.h" + +FlutterWindow::FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project) + : view_properties_(view_properties), project_(project) {} + +bool FlutterWindow::OnCreate() { + flutter_view_controller_ = std::make_unique( + view_properties_, project_); + + // Ensure that basic setup of the controller was successful. + if (!flutter_view_controller_->engine() || + !flutter_view_controller_->view()) { + return false; + } + + // Register Flutter plugins. + RegisterPlugins(flutter_view_controller_->engine()); + + return true; +} + +void FlutterWindow::OnDestroy() { + if (flutter_view_controller_) { + flutter_view_controller_ = nullptr; + } +} + +void FlutterWindow::Run() { + // Main loop. + auto next_flutter_event_time = + std::chrono::steady_clock::time_point::clock::now(); + while (flutter_view_controller_->view()->DispatchEvent()) { + // Wait until the next event. + { + auto wait_duration = + std::max(std::chrono::nanoseconds(0), + next_flutter_event_time - + std::chrono::steady_clock::time_point::clock::now()); + std::this_thread::sleep_for( + std::chrono::duration_cast(wait_duration)); + } + + // Processes any pending events in the Flutter engine, and returns the + // number of nanoseconds until the next scheduled event (or max, if none). + auto wait_duration = flutter_view_controller_->engine()->ProcessMessages(); + { + auto next_event_time = std::chrono::steady_clock::time_point::max(); + if (wait_duration != std::chrono::nanoseconds::max()) { + next_event_time = + std::min(next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + wait_duration); + } else { + // Wait for the next frame if no events. + auto frame_rate = flutter_view_controller_->view()->GetFrameRate(); + next_event_time = std::min( + next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + std::chrono::milliseconds( + static_cast(std::trunc(1000000.0 / frame_rate)))); + } + next_flutter_event_time = + std::max(next_flutter_event_time, next_event_time); + } + } +} diff --git a/examples/flutter-wayland-client/flutter_window.h b/examples/flutter-wayland-client/flutter_window.h new file mode 100644 index 00000000..20b9cb88 --- /dev/null +++ b/examples/flutter-wayland-client/flutter_window.h @@ -0,0 +1,34 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_WINDOW_ +#define FLUTTER_WINDOW_ + +#include +#include + +#include + +class FlutterWindow { + public: + explicit FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project); + ~FlutterWindow() = default; + + // Prevent copying. + FlutterWindow(FlutterWindow const&) = delete; + FlutterWindow& operator=(FlutterWindow const&) = delete; + + bool OnCreate(); + void OnDestroy(); + void Run(); + + private: + flutter::FlutterViewController::ViewProperties view_properties_; + flutter::DartProject project_; + std::unique_ptr flutter_view_controller_; +}; + +#endif // FLUTTER_WINDOW_ diff --git a/examples/flutter-wayland-client/main.cc b/examples/flutter-wayland-client/main.cc index 459bc04b..579daee6 100644 --- a/examples/flutter-wayland-client/main.cc +++ b/examples/flutter-wayland-client/main.cc @@ -9,50 +9,45 @@ #include #include -#include "command_options.h" +#include "flutter_embedder_options.h" +#include "flutter_window.h" int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("fullscreen", "f", "Always full-screen display", - false); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); - options.AddInt("width", "w", "Flutter app window width", 1280, false); - options.AddInt("height", "h", "Flutter app window height", 720, false); + FlutterEmbedderOptions options; if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); return 0; } - // The project to run. - const auto bundle_path = options.GetValue("bundle"); + // Creates the Flutter project. + const auto bundle_path = options.BundlePath(); const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); flutter::DartProject project(fl_path); auto command_line_arguments = std::vector(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); + flutter::FlutterViewController::ViewProperties view_properties = {}; + view_properties.width = options.WindowWidth(); + view_properties.height = options.WindowHeight(); + view_properties.view_mode = options.WindowViewMode(); + view_properties.view_rotation = options.WindowRotation(); + view_properties.title = options.WindowTitle(); + view_properties.app_id = options.WindowAppID(); + view_properties.use_mouse_cursor = options.IsUseMouseCursor(); + view_properties.use_onscreen_keyboard = options.IsUseOnscreenKeyboard(); + view_properties.use_window_decoration = options.IsUseWindowDecoraation(); + view_properties.text_scale_factor = options.TextScaleFactor(); + view_properties.enable_high_contrast = options.EnableHighContrast(); + view_properties.force_scale_factor = options.IsForceScaleFactor(); + view_properties.scale_factor = options.ScaleFactor(); + view_properties.enable_vsync = options.EnableVsync(); + // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - const auto view_mode = - options.Exist("fullscreen") - ? flutter::FlutterViewController::ViewMode::kFullscreen - : flutter::FlutterViewController::ViewMode::kNormal; - const auto width = options.GetValue("width"); - const auto height = options.GetValue("height"); - auto flutter_controller = std::make_unique( - view_mode, width, height, show_cursor, project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { + FlutterWindow window(view_properties, project); + if (!window.OnCreate()) { return 0; } - - // Main loop. - while (flutter_controller->view()->DispatchEvent()) { - flutter_controller->engine()->ProcessMessages(); - } + window.Run(); + window.OnDestroy(); return 0; } diff --git a/examples/flutter-weston-desktop-shell-virtual-keyboard/README.md b/examples/flutter-weston-desktop-shell-virtual-keyboard/README.md deleted file mode 100644 index 12a15ede..00000000 --- a/examples/flutter-weston-desktop-shell-virtual-keyboard/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Overview - -This is the example of Wayland backend + weston desktop-shell + virtual keyboard. - -## Building (Wayland backend weston desktop-shell) - -```Shell -$ mkdir build -$ cd build -$ cmake -DUSER_PROJECT_PATH=examples/flutter-weston-desktop-shell-virtual-keyboard .. -$ cmake --build . -``` diff --git a/examples/flutter-weston-desktop-shell-virtual-keyboard/cmake/user_config.cmake b/examples/flutter-weston-desktop-shell-virtual-keyboard/cmake/user_config.cmake deleted file mode 100644 index 089b2504..00000000 --- a/examples/flutter-weston-desktop-shell-virtual-keyboard/cmake/user_config.cmake +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.10) - -# Flutter embedder configurations. -# See: https://github.com/sony/flutter-embedded-linux/doc/README.md#user-configuration-parameters-cmake-options -set(BACKEND_TYPE WAYLAND) -set(DESKTOP_SHELL ON) -set(USE_VIRTUAL_KEYBOARD ON) -set(USE_GLES3 OFF) diff --git a/examples/flutter-weston-desktop-shell-virtual-keyboard/main.cc b/examples/flutter-weston-desktop-shell-virtual-keyboard/main.cc deleted file mode 100644 index f4c0b56d..00000000 --- a/examples/flutter-weston-desktop-shell-virtual-keyboard/main.cc +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include -#include - -#include -#include -#include - -int main(int argc, char** argv) { - // Works as a weston desktop shell. - bool show_cursor; - auto config_file = weston_config_get_name_from_env(); - auto config = weston_config_parse(config_file); - auto s = weston_config_get_section(config, "flutter_linux_wayland", nullptr, - nullptr); - char* path = nullptr; - if (s) { - weston_config_section_get_bool(s, "show-cursor", &show_cursor, true); - weston_config_section_get_string( - s, "flutter-project-path", &path, - "../../sample-app/sample/build/linux/x64/release/bundle"); - } - - std::wstring fl_path; - if (path != nullptr) { - // convert char* to wstring. - std::string str(path); - std::wstring ws_temp(str.begin(), str.end()); - if (!ws_temp.empty()) { - fl_path = ws_temp; - } - - free(path); - } - - // libweston-6 does not export weston_config_destroy(). - // So run free() instead. - if (config) { - free(config); - } - // weston_config_destroy(config); - - // The project to run. - flutter::DartProject project(fl_path); - auto command_line_arguments = std::vector(); - project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - - // The Flutter instance hosted by this window. - int width = 640; - int height = 480; - auto flutter_controller = std::make_unique( - flutter::FlutterViewController::ViewMode::kFullscreen, width, height, - show_cursor, project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { - return 0; - } - - // Main loop. - while (flutter_controller->view()->DispatchEvent()) { - flutter_controller->engine()->ProcessMessages(); - } - - return 0; -} diff --git a/examples/flutter-weston-desktop-shell/README.md b/examples/flutter-weston-desktop-shell/README.md deleted file mode 100644 index b87f0918..00000000 --- a/examples/flutter-weston-desktop-shell/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Overview - -This is the example of Wayland backend + weston desktop-shell. - -## Building - -```Shell -$ mkdir build -$ cd build -$ cmake -DUSER_PROJECT_PATH=examples/flutter-weston-desktop-shell .. -$ cmake --build . -``` diff --git a/examples/flutter-weston-desktop-shell/cmake/user_build.cmake b/examples/flutter-weston-desktop-shell/cmake/user_build.cmake deleted file mode 100644 index 5d24e871..00000000 --- a/examples/flutter-weston-desktop-shell/cmake/user_build.cmake +++ /dev/null @@ -1,26 +0,0 @@ -cmake_minimum_required(VERSION 3.10) - -# user binary name. -set(TARGET flutter-desktop-shell) - -# source files for user apps. -set(USER_APP_SRCS - examples/flutter-weston-desktop-shell/main.cc -) - -# header files for user apps. -set(USER_APP_INCLUDE_DIRS - ## Public APIs for developers (Don't edit!). - src/client_wrapper/include - src/flutter/shell/platform/common/client_wrapper - src/flutter/shell/platform/common/client_wrapper/include/flutter - src/flutter/shell/platform/common/public - src/flutter/shell/platform/linux_embedded/public - src/public/include - ## header file include path for user apps. - examples/flutter-weston-desktop-shell -) - -# link libraries for user apps. -set(USER_APP_LIBRARIES -) diff --git a/examples/flutter-weston-desktop-shell/cmake/user_config.cmake b/examples/flutter-weston-desktop-shell/cmake/user_config.cmake deleted file mode 100644 index eecee1af..00000000 --- a/examples/flutter-weston-desktop-shell/cmake/user_config.cmake +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.10) - -# Flutter embedder configurations. -# See: https://github.com/sony/flutter-embedded-linux/doc/README.md#user-configuration-parameters-cmake-options -set(BACKEND_TYPE WAYLAND) -set(DESKTOP_SHELL ON) -set(USE_VIRTUAL_KEYBOARD OFF) -set(USE_GLES3 OFF) diff --git a/examples/flutter-weston-desktop-shell/main.cc b/examples/flutter-weston-desktop-shell/main.cc deleted file mode 100644 index f4c0b56d..00000000 --- a/examples/flutter-weston-desktop-shell/main.cc +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include -#include - -#include -#include -#include - -int main(int argc, char** argv) { - // Works as a weston desktop shell. - bool show_cursor; - auto config_file = weston_config_get_name_from_env(); - auto config = weston_config_parse(config_file); - auto s = weston_config_get_section(config, "flutter_linux_wayland", nullptr, - nullptr); - char* path = nullptr; - if (s) { - weston_config_section_get_bool(s, "show-cursor", &show_cursor, true); - weston_config_section_get_string( - s, "flutter-project-path", &path, - "../../sample-app/sample/build/linux/x64/release/bundle"); - } - - std::wstring fl_path; - if (path != nullptr) { - // convert char* to wstring. - std::string str(path); - std::wstring ws_temp(str.begin(), str.end()); - if (!ws_temp.empty()) { - fl_path = ws_temp; - } - - free(path); - } - - // libweston-6 does not export weston_config_destroy(). - // So run free() instead. - if (config) { - free(config); - } - // weston_config_destroy(config); - - // The project to run. - flutter::DartProject project(fl_path); - auto command_line_arguments = std::vector(); - project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - - // The Flutter instance hosted by this window. - int width = 640; - int height = 480; - auto flutter_controller = std::make_unique( - flutter::FlutterViewController::ViewMode::kFullscreen, width, height, - show_cursor, project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { - return 0; - } - - // Main loop. - while (flutter_controller->view()->DispatchEvent()) { - flutter_controller->engine()->ProcessMessages(); - } - - return 0; -} diff --git a/examples/flutter-x11-client/cmake/user_build.cmake b/examples/flutter-x11-client/cmake/user_build.cmake index 8865c9e5..8b7f3d70 100644 --- a/examples/flutter-x11-client/cmake/user_build.cmake +++ b/examples/flutter-x11-client/cmake/user_build.cmake @@ -5,6 +5,8 @@ set(TARGET flutter-x11-client) # source files for user apps. set(USER_APP_SRCS + examples/flutter-x11-client/flutter/generated_plugin_registrant.cc + examples/flutter-x11-client/flutter_window.cc examples/flutter-x11-client/main.cc ) @@ -13,13 +15,13 @@ set(USER_APP_INCLUDE_DIRS ## Public APIs for developers (Don't edit!). src/client_wrapper/include src/flutter/shell/platform/common/client_wrapper + src/flutter/shell/platform/common/client_wrapper/include src/flutter/shell/platform/common/client_wrapper/include/flutter src/flutter/shell/platform/common/public src/flutter/shell/platform/linux_embedded/public - src/public/include ## header file include path for user apps. examples/flutter-x11-client ) # link libraries for user apps. -set(USER_APP_LIBRARIES "") +set(USER_APP_LIBRARIES) diff --git a/examples/flutter-x11-client/cmake/user_config.cmake b/examples/flutter-x11-client/cmake/user_config.cmake index 30624406..aa0196fc 100644 --- a/examples/flutter-x11-client/cmake/user_config.cmake +++ b/examples/flutter-x11-client/cmake/user_config.cmake @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.10) # Flutter embedder configurations. -# See: https://github.com/sony/flutter-embedded-linux/doc/README.md#user-configuration-parameters-cmake-options +# See: https://github.com/sony/flutter-embedded-linux/wiki/Building-Embedded-Linux-embedding-for-Flutter#user-configuration-parameters-cmake-options set(BACKEND_TYPE X11) -set(DESKTOP_SHELL OFF) -set(USE_VIRTUAL_KEYBOARD OFF) set(USE_GLES3 OFF) diff --git a/examples/flutter-x11-client/command_options.h b/examples/flutter-x11-client/command_options.h index e051a043..b0de9316 100644 --- a/examples/flutter-x11-client/command_options.h +++ b/examples/flutter-x11-client/command_options.h @@ -1,7 +1,10 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2022 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#ifndef COMMAND_OPTIONS_ +#define COMMAND_OPTIONS_ + #include #include #include @@ -10,8 +13,6 @@ #include #include -// todo: Supports other types besides int, string. - namespace commandline { namespace { @@ -36,30 +37,49 @@ class CommandOptions { CommandOptions() = default; ~CommandOptions() = default; - void AddWithoutValue(const std::string& name, const std::string& short_name, - const std::string& description, bool required) { + void AddWithoutValue(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required) { Add(name, short_name, description, "", ReaderString(), required, false); } - void AddInt(const std::string& name, const std::string& short_name, - const std::string& description, const int& default_value, + void AddInt(const std::string& name, + const std::string& short_name, + const std::string& description, + const int& default_value, bool required) { Add(name, short_name, description, default_value, ReaderInt(), required, true); } - void AddString(const std::string& name, const std::string& short_name, + void AddDouble(const std::string& name, + const std::string& short_name, + const std::string& description, + const double& default_value, + bool required) { + Add(name, short_name, description, default_value, + ReaderDouble(), required, true); + } + + void AddString(const std::string& name, + const std::string& short_name, const std::string& description, - const std::string& default_value, bool required) { + const std::string& default_value, + bool required) { Add(name, short_name, description, default_value, ReaderString(), required, true); } template - void Add(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader = F(), bool required = true, bool required_value = true) { + void Add(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader = F(), + bool required = true, + bool required_value = true) { if (options_.find(name) != options_.end()) { std::cerr << "Already registered option: " << name << std::endl; return; @@ -142,7 +162,7 @@ class CommandOptions { // short options: e.g. -f /foo/file.txt -h 640 -abc else if (arg.length() > 1 && arg.substr(0, 1).compare(kOptionStyleShort) == 0) { - for (int j = 1; j < arg.length(); j++) { + for (size_t j = 1; j < arg.length(); j++) { const std::string option_name{argv[i][j]}; if (lut_short_options_.find(option_name) == @@ -167,7 +187,7 @@ class CommandOptions { } } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired() && !registration_order_options_[i]->HasValue()) { errors_.push_back( @@ -187,7 +207,7 @@ class CommandOptions { std::ostringstream ostream; ostream << "Usage: " << command_name_ << " "; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (registration_order_options_[i]->IsRequired()) { ostream << registration_order_options_[i]->GetHelpShortMessage() << " "; } @@ -196,12 +216,12 @@ class CommandOptions { ostream << "Global options:" << std::endl; size_t max_name_len = 0; - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { max_name_len = std::max( max_name_len, registration_order_options_[i]->GetName().length()); } - for (auto i = 0; i < registration_order_options_.size(); i++) { + for (size_t i = 0; i < registration_order_options_.size(); i++) { if (!registration_order_options_[i]->GetShortName().empty()) { ostream << kOptionStyleShort << registration_order_options_[i]->GetShortName() << ", "; @@ -210,7 +230,7 @@ class CommandOptions { } size_t index_adjust = 0; - constexpr int kSpacerNum = 5; + constexpr int kSpacerNum = 10; auto need_value = registration_order_options_[i]->IsRequiredValue(); ostream << kOptionStyleNormal << registration_order_options_[i]->GetName(); @@ -237,10 +257,17 @@ class CommandOptions { std::string operator()(const std::string& value) { return value; } }; + struct ReaderDouble { + double operator()(const std::string& value) { return std::stod(value); } + }; + class Option { public: - Option(const std::string& name, const std::string& short_name, - const std::string& description, bool required, bool required_value) + Option(const std::string& name, + const std::string& short_name, + const std::string& description, + bool required, + bool required_value) : name_(name), short_name_(short_name), description_(description), @@ -274,20 +301,23 @@ class CommandOptions { virtual bool HasValue() const = 0; protected: - bool is_required_; - bool is_required_value_; std::string name_; std::string short_name_; std::string description_; + bool is_required_; + bool is_required_value_; bool value_set_; }; template class OptionValue : public Option { public: - OptionValue(const std::string& name, const std::string& short_name, - const std::string& description, const T& default_value, - bool required, bool required_value) + OptionValue(const std::string& name, + const std::string& short_name, + const std::string& description, + const T& default_value, + bool required, + bool required_value) : Option(name, short_name, description, required, required_value), default_value_(default_value), value_(default_value){}; @@ -313,10 +343,18 @@ class CommandOptions { template class OptionValueReader : public OptionValue { public: - OptionValueReader(const std::string& name, const std::string& short_name, - const std::string& description, const T default_value, - F reader, bool required, bool required_value) - : OptionValue(name, short_name, description, default_value, required, + OptionValueReader(const std::string& name, + const std::string& short_name, + const std::string& description, + const T default_value, + F reader, + bool required, + bool required_value) + : OptionValue(name, + short_name, + description, + default_value, + required, required_value), reader_(reader) {} ~OptionValueReader() = default; @@ -360,3 +398,5 @@ class CommandOptions { }; } // namespace commandline + +#endif // COMMAND_OPTIONS_ diff --git a/examples/flutter-x11-client/flutter/generated_plugin_registrant.cc b/examples/flutter-x11-client/flutter/generated_plugin_registrant.cc new file mode 100644 index 00000000..7e6e6c2d --- /dev/null +++ b/examples/flutter-x11-client/flutter/generated_plugin_registrant.cc @@ -0,0 +1,10 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + +void RegisterPlugins(flutter::PluginRegistry* registry) { +} diff --git a/examples/flutter-x11-client/flutter/generated_plugin_registrant.h b/examples/flutter-x11-client/flutter/generated_plugin_registrant.h new file mode 100644 index 00000000..a31c23cd --- /dev/null +++ b/examples/flutter-x11-client/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/examples/flutter-x11-client/flutter/generated_plugins.cmake b/examples/flutter-x11-client/flutter/generated_plugins.cmake new file mode 100644 index 00000000..fcbf0d6e --- /dev/null +++ b/examples/flutter-x11-client/flutter/generated_plugins.cmake @@ -0,0 +1,22 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory( + ${USER_PROJECT_PATH}/flutter/plugins/${plugin}/elinux plugins/${plugin}) + + target_link_libraries(${TARGET} + PRIVATE + ${plugin}_plugin + ) + + list(APPEND PLUGIN_BUNDLED_LIBRARIES + ${PROJECT_BINARY_DIR}/plugins/${plugin}/lib${plugin}_plugin.so + ) +endforeach(plugin) diff --git a/examples/flutter-x11-client/flutter_embedder_options.h b/examples/flutter-x11-client/flutter_embedder_options.h new file mode 100644 index 00000000..41d0bd10 --- /dev/null +++ b/examples/flutter-x11-client/flutter_embedder_options.h @@ -0,0 +1,203 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_EMBEDDER_OPTIONS_ +#define FLUTTER_EMBEDDER_OPTIONS_ + +#include + +#include + +#include "command_options.h" + +class FlutterEmbedderOptions { + public: + FlutterEmbedderOptions() { + options_.AddString("bundle", "b", "Path to Flutter project bundle", + "./bundle", true); + options_.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", + false); + options_.AddInt("rotation", "r", + "Window rotation(degree) [0(default)|90|180|270]", 0, + false); + options_.AddDouble("text-scaling-factor", "x", "Text scaling factor", 1.0, + false); + options_.AddWithoutValue("enable-high-contrast", "i", + "Request that UI be rendered with darker colors.", + false); + options_.AddDouble("force-scale-factor", "s", + "Force a scale factor instead using default value", 1.0, + false); + options_.AddWithoutValue( + "async-vblank", "v", + "Don't sync to compositor redraw/vblank (eglSwapInterval 0)", false); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + // no more options. +#elif defined(FLUTTER_TARGET_BACKEND_X11) + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + options_.AddString("title", "t", "Window title", "Flutter", false); + options_.AddString("app-id", "a", "XDG App ID", "dev.flutter.elinux", + false); + options_.AddWithoutValue("onscreen-keyboard", "k", + "Enable on-screen keyboard", false); + options_.AddWithoutValue("window-decoration", "d", + "Enable window decorations", false); + options_.AddWithoutValue("fullscreen", "f", "Always full-screen display", + false); + options_.AddInt("width", "w", "Window width", 1280, false); + options_.AddInt("height", "h", "Window height", 720, false); +#endif + } + ~FlutterEmbedderOptions() = default; + + bool Parse(int argc, char** argv) { + if (!options_.Parse(argc, argv)) { + std::cerr << options_.GetError() << std::endl; + std::cout << options_.ShowHelp(); + return false; + } + + bundle_path_ = options_.GetValue("bundle"); + use_mouse_cursor_ = !options_.Exist("no-cursor"); + if (options_.Exist("rotation")) { + switch (options_.GetValue("rotation")) { + case 90: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_90; + break; + case 180: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_180; + break; + case 270: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_270; + break; + default: + window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + break; + } + } + + text_scale_factor_ = options_.GetValue("text-scaling-factor"); + enable_high_contrast_ = options_.Exist("enable-high-contrast"); + + if (options_.Exist("force-scale-factor")) { + is_force_scale_factor_ = true; + scale_factor_ = options_.GetValue("force-scale-factor"); + } else { + is_force_scale_factor_ = false; + scale_factor_ = 1.0; + } + + enable_vsync_ = !options_.Exist("async-vblank"); + +#if defined(FLUTTER_TARGET_BACKEND_GBM) || \ + defined(FLUTTER_TARGET_BACKEND_EGLSTREAM) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_view_mode_ = flutter::FlutterViewController::ViewMode::kFullscreen; +#elif defined(FLUTTER_TARGET_BACKEND_X11) + use_onscreen_keyboard_ = false; + use_window_decoration_ = false; + window_title_ = options_.GetValue("title"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#else // FLUTTER_TARGET_BACKEND_WAYLAND + window_title_ = options_.GetValue("title"); + window_app_id_ = options_.GetValue("app-id"); + use_onscreen_keyboard_ = options_.Exist("onscreen-keyboard"); + use_window_decoration_ = options_.Exist("window-decoration"); + window_view_mode_ = + options_.Exist("fullscreen") + ? flutter::FlutterViewController::ViewMode::kFullscreen + : flutter::FlutterViewController::ViewMode::kNormal; + window_width_ = options_.GetValue("width"); + window_height_ = options_.GetValue("height"); +#endif + + return true; + } + + std::string BundlePath() const { + return bundle_path_; + } + std::string WindowTitle() const { + return window_title_; + } + std::string WindowAppID() const { + return window_app_id_; + } + bool IsUseMouseCursor() const { + return use_mouse_cursor_; + } + bool IsUseOnscreenKeyboard() const { + return use_onscreen_keyboard_; + } + bool IsUseWindowDecoraation() const { + return use_window_decoration_; + } + flutter::FlutterViewController::ViewMode WindowViewMode() const { + return window_view_mode_; + } + int WindowWidth() const { + return window_width_; + } + int WindowHeight() const { + return window_height_; + } + flutter::FlutterViewController::ViewRotation WindowRotation() const { + return window_view_rotation_; + } + double TextScaleFactor() const { + return text_scale_factor_; + } + bool EnableHighContrast() const { + return enable_high_contrast_; + } + bool IsForceScaleFactor() const { + return is_force_scale_factor_; + } + double ScaleFactor() const { + return scale_factor_; + } + bool EnableVsync() const { + return enable_vsync_; + } + + private: + commandline::CommandOptions options_; + + std::string bundle_path_; + std::string window_title_; + std::string window_app_id_; + bool use_mouse_cursor_ = true; + bool use_onscreen_keyboard_ = false; + bool use_window_decoration_ = false; + flutter::FlutterViewController::ViewMode window_view_mode_ = + flutter::FlutterViewController::ViewMode::kNormal; + int window_width_ = 1280; + int window_height_ = 720; + flutter::FlutterViewController::ViewRotation window_view_rotation_ = + flutter::FlutterViewController::ViewRotation::kRotation_0; + bool is_force_scale_factor_; + double scale_factor_; + double text_scale_factor_; + bool enable_high_contrast_; + bool enable_vsync_; +}; + +#endif // FLUTTER_EMBEDDER_OPTIONS_ diff --git a/examples/flutter-x11-client/flutter_window.cc b/examples/flutter-x11-client/flutter_window.cc new file mode 100644 index 00000000..0c5b6397 --- /dev/null +++ b/examples/flutter-x11-client/flutter_window.cc @@ -0,0 +1,79 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter_window.h" + +#include +#include +#include +#include + +#include "flutter/generated_plugin_registrant.h" + +FlutterWindow::FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project) + : view_properties_(view_properties), project_(project) {} + +bool FlutterWindow::OnCreate() { + flutter_view_controller_ = std::make_unique( + view_properties_, project_); + + // Ensure that basic setup of the controller was successful. + if (!flutter_view_controller_->engine() || + !flutter_view_controller_->view()) { + return false; + } + + // Register Flutter plugins. + RegisterPlugins(flutter_view_controller_->engine()); + + return true; +} + +void FlutterWindow::OnDestroy() { + if (flutter_view_controller_) { + flutter_view_controller_ = nullptr; + } +} + +void FlutterWindow::Run() { + // Main loop. + auto next_flutter_event_time = + std::chrono::steady_clock::time_point::clock::now(); + while (flutter_view_controller_->view()->DispatchEvent()) { + // Wait until the next event. + { + auto wait_duration = + std::max(std::chrono::nanoseconds(0), + next_flutter_event_time - + std::chrono::steady_clock::time_point::clock::now()); + std::this_thread::sleep_for( + std::chrono::duration_cast(wait_duration)); + } + + // Processes any pending events in the Flutter engine, and returns the + // number of nanoseconds until the next scheduled event (or max, if none). + auto wait_duration = flutter_view_controller_->engine()->ProcessMessages(); + { + auto next_event_time = std::chrono::steady_clock::time_point::max(); + if (wait_duration != std::chrono::nanoseconds::max()) { + next_event_time = + std::min(next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + wait_duration); + } else { + // Wait for the next frame if no events. + auto frame_rate = flutter_view_controller_->view()->GetFrameRate(); + next_event_time = std::min( + next_event_time, + std::chrono::steady_clock::time_point::clock::now() + + std::chrono::milliseconds( + static_cast(std::trunc(1000000.0 / frame_rate)))); + } + next_flutter_event_time = + std::max(next_flutter_event_time, next_event_time); + } + } +} diff --git a/examples/flutter-x11-client/flutter_window.h b/examples/flutter-x11-client/flutter_window.h new file mode 100644 index 00000000..20b9cb88 --- /dev/null +++ b/examples/flutter-x11-client/flutter_window.h @@ -0,0 +1,34 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_WINDOW_ +#define FLUTTER_WINDOW_ + +#include +#include + +#include + +class FlutterWindow { + public: + explicit FlutterWindow( + const flutter::FlutterViewController::ViewProperties view_properties, + const flutter::DartProject project); + ~FlutterWindow() = default; + + // Prevent copying. + FlutterWindow(FlutterWindow const&) = delete; + FlutterWindow& operator=(FlutterWindow const&) = delete; + + bool OnCreate(); + void OnDestroy(); + void Run(); + + private: + flutter::FlutterViewController::ViewProperties view_properties_; + flutter::DartProject project_; + std::unique_ptr flutter_view_controller_; +}; + +#endif // FLUTTER_WINDOW_ diff --git a/examples/flutter-x11-client/main.cc b/examples/flutter-x11-client/main.cc index 7d099ba4..579daee6 100644 --- a/examples/flutter-x11-client/main.cc +++ b/examples/flutter-x11-client/main.cc @@ -5,86 +5,49 @@ #include #include -#include #include #include #include -#include -#include "command_options.h" +#include "flutter_embedder_options.h" +#include "flutter_window.h" int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("fullscreen", "f", "Always full-screen display", - false); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); - options.AddInt("width", "w", "Flutter app window width", 1280, false); - options.AddInt("height", "h", "Flutter app window height", 720, false); + FlutterEmbedderOptions options; if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); return 0; } - // The project to run. - const auto bundle_path = options.GetValue("bundle"); + // Creates the Flutter project. + const auto bundle_path = options.BundlePath(); const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); flutter::DartProject project(fl_path); auto command_line_arguments = std::vector(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - const auto view_mode = - options.Exist("fullscreen") - ? flutter::FlutterViewController::ViewMode::kFullscreen - : flutter::FlutterViewController::ViewMode::kNormal; - const auto width = options.GetValue("width"); - const auto height = options.GetValue("height"); - auto flutter_controller = std::make_unique( - view_mode, width, height, show_cursor, project); + flutter::FlutterViewController::ViewProperties view_properties = {}; + view_properties.width = options.WindowWidth(); + view_properties.height = options.WindowHeight(); + view_properties.view_mode = options.WindowViewMode(); + view_properties.view_rotation = options.WindowRotation(); + view_properties.title = options.WindowTitle(); + view_properties.app_id = options.WindowAppID(); + view_properties.use_mouse_cursor = options.IsUseMouseCursor(); + view_properties.use_onscreen_keyboard = options.IsUseOnscreenKeyboard(); + view_properties.use_window_decoration = options.IsUseWindowDecoraation(); + view_properties.text_scale_factor = options.TextScaleFactor(); + view_properties.enable_high_contrast = options.EnableHighContrast(); + view_properties.force_scale_factor = options.IsForceScaleFactor(); + view_properties.scale_factor = options.ScaleFactor(); + view_properties.enable_vsync = options.EnableVsync(); - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { + // The Flutter instance hosted by this window. + FlutterWindow window(view_properties, project); + if (!window.OnCreate()) { return 0; } - - // Main loop. - auto next_flutter_event_time = - std::chrono::steady_clock::time_point::clock::now(); - while (flutter_controller->view()->DispatchEvent()) { - // Wait until the next event. - { - auto wait_duration = - std::max(std::chrono::nanoseconds(0), - next_flutter_event_time - - std::chrono::steady_clock::time_point::clock::now()); - std::this_thread::sleep_for( - std::chrono::duration_cast(wait_duration)); - } - - // Processes any pending events in the Flutter engine, and returns the - // number of nanoseconds until the next scheduled event (or max, if none). - auto wait_duration = flutter_controller->engine()->ProcessMessages(); - { - auto next_event_time = std::chrono::steady_clock::time_point::max(); - if (wait_duration != std::chrono::nanoseconds::max()) { - auto next_wakeup = - std::max(std::chrono::steady_clock::time_point::max(), - std::chrono::steady_clock::time_point::clock::now() + - wait_duration); - next_event_time = std::min(next_event_time, next_wakeup); - } else { - // Wait 1/60 [sec] = 13 [msec] if no events. - next_event_time = std::chrono::steady_clock::time_point::clock::now() + - std::chrono::milliseconds(13); - } - next_flutter_event_time = - std::max(next_flutter_event_time, next_event_time); - } - } + window.Run(); + window.OnDestroy(); return 0; } diff --git a/meta-flutter/README.md b/meta-flutter/README.md deleted file mode 100644 index bc07bfda..00000000 --- a/meta-flutter/README.md +++ /dev/null @@ -1,113 +0,0 @@ -# meta-flutter for Yocto Project -Recipe file examples for Yocto Project. In this README, we explain how to build for Arm64 using `core-image-weston` which is one of Yocto Images, and `dunfell` which is one of LTS Yocto versions. See also: https://docs.yoctoproject.org/ - - -## Setup environment -There are two ways to build using Yocto. One is a build using bitbake and the other is a build using Yocto SDK. - -### Building Yocto -Downloading `Poky` and `meta-clang`, `meta-flutter` source code: -```Shell -$ git clone git://git.yoctoproject.org/poky.git -b dunfell -$ git clone https://github.com/kraj/meta-clang -b dunfell -$ git clone https://github.com/sony/flutter-embedded-linux.git -``` - -Setup the build environment using `oe-init-build-env` script in Poky. -```Shell -$ source poky/oe-init-build-env build -``` - -Set your target machine in your `conf/local.conf`: -``` -MACHINE ?= "qemuarm64" -``` - -Add meta-clang layer into `conf/bblayers.conf`. -```Shell -$ bitbake-layers add-layer ../meta-clang -``` - -Add meta-flutter layer into `conf/bblayers.conf`. -```Shell -$ bitbake-layers add-layer ../flutter-embedded-linux/meta-flutter -``` - -### Building Yocto SDK (Only when using cross-building with Yocto SDK) -Add the following in your `conf/local.conf`: -``` -CLANGSDK = "1" -``` -See also: [Adding clang in generated SDK toolchain](https://github.com/kraj/meta-clang/blob/master/README.md#adding-clang-in-generated-sdk-toolchain) - -Build Yocto SDK for cross-building. -```Shell -$ bitbake core-image-weston -c populate_sdk -``` -See also: [SDK building an sdk installer](https://www.yoctoproject.org/docs/2.1/sdk-manual/sdk-manual.html#sdk-building-an-sdk-installer) - -Install Yocto SDK. -```Shell -$ ./tmp/deploy/sdk/poky-glibc-x86_64-core-image-weston-aarch64-qemuarm64-toolchain-3.1.7.sh -``` - -## Cross-building with bitbake -### Flutter Engine (libflutter_engine.so) -Build target is fixed to Linux and Arm64, and Flutter Engine version is also fixed in the recipe file. - -#### Flutter Engine version -``` -ENGINE_VERSION ?= "f5b97d0b23a3905e9b5b69aa873afcb52f550aaf" -``` - -When creating a Flutter project, you will need to use the following version of the Flutter SDK. -`Flutter 2.3.0-1.0.pre.269` - -If you want to change the version of the Flutter engine, change to the appropriate version of the Flutter SDK and add the following to `conf/local.conf`: -``` -ENGINE_VERSION_pn-flutter-engine = "" -``` - -Flutter Engine is built with release mode by default. If you want to change the build mode, you can change it to add the following in your `conf/local.conf`: -#### Flutter Engine mode -``` -# e.g. debug mode -PACKAGECONFIG_pn-flutter-engine = "debug-mode" -``` - -### Wayland backend -```Shell -$ bitbake flutter-wayland-client -``` - -### DRM-GBM backend -`libsystemd` is required to build this backend. Put the following in your `conf/local.conf`: -``` -DESTRO_FEATURES_append = " systemd" -``` -See also: [Using systemd for the Main Image and Using SysVinit for the Rescue Image](https://www.yoctoproject.org/docs/current/mega-manual/mega-manual.html#using-systemd-for-the-main-image-and-using-sysvinit-for-the-rescue-image) - -```Shell -$ bitbake flutter-drm-gbm-backend -``` - -### DRM-EGLStream backend -You need to install libsystemd in the same way with the DRM-GBM backend. - -```Shell -$ bitbake flutter-drm-eglstream-backend -``` - -## Cross-building with Yocto SDK -Setup the cross-building toolchain environment using a script that you built and installed. -```Shell -$ source /opt/poky/3.1.7/environment-setup-aarch64-poky-linux -``` - -Set the following environment vars to cross-build using clang. -```Shell -$ export CC=${CLANGCC} -$ export CXX=${CLANGCXX} -``` - -After doing that, you can build the embedder as normal like self-building on hosts. It means you don't need to be aware of cross-building. See: [3.1. Self-build](../doc/README.md) diff --git a/meta-flutter/conf/layer.conf b/meta-flutter/conf/layer.conf deleted file mode 100644 index d64bfcc3..00000000 --- a/meta-flutter/conf/layer.conf +++ /dev/null @@ -1,12 +0,0 @@ -# We have a conf and classes directory, add to BBPATH -BBPATH .= ":${LAYERDIR}" - -# We have recipes-* directories, add to BBFILES -BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \ - ${LAYERDIR}/recipes-*/*/*.bbappend" - -BBFILE_COLLECTIONS += "meta-flutter" -BBFILE_PATTERN_meta-flutter = "^${LAYERDIR}/" -BBFILE_PRIORITY_meta-flutter = "6" - -LAYERDEPENDS_meta-flutter = "core" diff --git a/meta-flutter/recipes-graphics/flutter-embedded-linux/dependency.inc b/meta-flutter/recipes-graphics/flutter-embedded-linux/dependency.inc deleted file mode 100644 index 0a76c8e3..00000000 --- a/meta-flutter/recipes-graphics/flutter-embedded-linux/dependency.inc +++ /dev/null @@ -1,7 +0,0 @@ -TOOLCHAIN = "clang" -DEPENDS += "libxkbcommon \ - virtual/libgles2 \ - virtual/egl \ - flutter-engine" - -inherit pkgconfig cmake features_check diff --git a/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-drm-eglstream-backend.bb b/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-drm-eglstream-backend.bb deleted file mode 100644 index 00d7c150..00000000 --- a/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-drm-eglstream-backend.bb +++ /dev/null @@ -1,20 +0,0 @@ -SUMMARY = "Flutter embedding for embedded Linux with DRM-EGLStream backend" -DESCRIPTION = "Build the flutter-drm-eglstream-backend project" - -require repository.inc -require dependency.inc - -DEPENDS += "libdrm \ - virtual/mesa \ - libinput \ - udev \ - systemd" - -EXTRA_OECMAKE = "-DUSER_PROJECT_PATH=examples/flutter-drm-eglstream-backend" - -do_install() { - install -d ${D}${bindir} - install -m 0755 ${WORKDIR}/build/flutter-drm-eglstream-backend ${D}${bindir} -} - -require tasks.inc diff --git a/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-drm-gbm-backend.bb b/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-drm-gbm-backend.bb deleted file mode 100644 index b43b522c..00000000 --- a/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-drm-gbm-backend.bb +++ /dev/null @@ -1,20 +0,0 @@ -SUMMARY = "Flutter embedding for embedded Linux with DRM-GBM backend" -DESCRIPTION = "Build the flutter-drm-gbm-backend project" - -require repository.inc -require dependency.inc - -DEPENDS += "libdrm \ - virtual/mesa \ - libinput \ - udev \ - systemd" - -EXTRA_OECMAKE = "-DUSER_PROJECT_PATH=examples/flutter-drm-gbm-backend" - -do_install() { - install -d ${D}${bindir} - install -m 0755 ${WORKDIR}/build/flutter-drm-gbm-backend ${D}${bindir} -} - -require tasks.inc diff --git a/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-wayland-client.bb b/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-wayland-client.bb deleted file mode 100644 index 5120aeb2..00000000 --- a/meta-flutter/recipes-graphics/flutter-embedded-linux/flutter-wayland-client.bb +++ /dev/null @@ -1,18 +0,0 @@ -SUMMARY = "Flutter embedding for embedded Linux with Wayland backend" -DESCRIPTION = "Build the flutter-wayland-client project" - -require repository.inc -require dependency.inc - -DEPENDS += "wayland \ - wayland-protocols \ - wayland-native" - -EXTRA_OECMAKE = "-DUSER_PROJECT_PATH=examples/flutter-wayland-client" - -do_install() { - install -d ${D}${bindir} - install -m 0755 ${WORKDIR}/build/flutter-client ${D}${bindir} -} - -require tasks.inc diff --git a/meta-flutter/recipes-graphics/flutter-embedded-linux/repository.inc b/meta-flutter/recipes-graphics/flutter-embedded-linux/repository.inc deleted file mode 100644 index 2387d0cd..00000000 --- a/meta-flutter/recipes-graphics/flutter-embedded-linux/repository.inc +++ /dev/null @@ -1,11 +0,0 @@ -AUTHOR = "Sony Group Corporation" -HOMEPAGE = "https://github.com/sony/flutter-embedded-linux" -BUGTRACKER = "https://github.com/sony/flutter-embedded-linux/issues" - -LICENSE = "BSD-3-Clause" -LIC_FILES_CHKSUM = "file://LICENSE;md5=79ca841e7b9e09b0401186f2aa334adf" - -SRC_URI = "git://github.com/sony/flutter-embedded-linux.git;protocol=https" -SRCREV = "${AUTOREV}" -S = "${WORKDIR}/git" - diff --git a/meta-flutter/recipes-graphics/flutter-embedded-linux/tasks.inc b/meta-flutter/recipes-graphics/flutter-embedded-linux/tasks.inc deleted file mode 100644 index bcd8096c..00000000 --- a/meta-flutter/recipes-graphics/flutter-embedded-linux/tasks.inc +++ /dev/null @@ -1,7 +0,0 @@ -do_configure_prepend() { - install -d ${S}/build - install -m 0644 ${STAGING_LIBDIR}/libflutter_engine.so ${S}/build/ -} -do_configure_prepend[depends] += "flutter-engine:do_populate_sysroot" - -FILES_${PN} = "${bindir}" diff --git a/meta-flutter/recipes-graphics/flutter-engine/flutter-engine.bb b/meta-flutter/recipes-graphics/flutter-engine/flutter-engine.bb deleted file mode 100755 index 960f7c2e..00000000 --- a/meta-flutter/recipes-graphics/flutter-engine/flutter-engine.bb +++ /dev/null @@ -1,69 +0,0 @@ -AUTHOR = "Sony Group Corporation" -LICENSE = "BSD-3-Clause" -LIC_FILES_CHKSUM = "file://LICENSE;md5=c2c05f9bdd5fc0b458037c2d1fb8d95e" - -SRC_URI = "git://chromium.googlesource.com/chromium/tools/depot_tools.git;protocol=https" -SRCREV = "da768751d43b1f287bf99bea703ea13e2eedcf4d" - -S = "${WORKDIR}/git" - -inherit pkgconfig -# TODO: Add dependent packages. -DEPENDS = "freetype" - -GN_TOOLS_PYTHON2_PATH ??= "bootstrap-3.8.0.chromium.8_bin/python/bin" - -require gn-args-utils.inc - -ENGINE_VERSION ?= "f5b97d0b23a3905e9b5b69aa873afcb52f550aaf" -PACKAGECONFIG ?= "release-mode" -PACKAGECONFIG[debug-mode] = "--runtime-mode debug --unoptimized" -PACKAGECONFIG[profile-mode] = "--runtime-mode profile --no-lto" -PACKAGECONFIG[release-mode] = "--runtime-mode release" - -GN_TARGET_OS = "linux" -GN_TARGET_ARCH = "arm64" - -GN_ARGS = "--target-sysroot ${STAGING_DIR_TARGET}${PACKAGECONFIG_CONFARGS}" -GN_ARGS_append = " --target-os ${GN_TARGET_OS}" -GN_ARGS_append = " --linux-cpu ${GN_TARGET_ARCH}" -GN_ARGS_append = " --embedder-for-target" -GN_ARGS_append = " --disable-desktop-embeddings" -ARTIFACT_DIR = "${@get_engine_artifact_dir(d)}" - -do_configure() { - # To disable auto update. - export DEPOT_TOOLS_UPDATE=0 - export PATH=${S}:${S}/${GN_TOOLS_PYTHON2_PATH}:$PATH - - cd ${WORKDIR} - echo 'solutions = [ - { - "managed" : False, - "name" : "src/flutter", - "url" : "https://github.com/flutter/engine.git@$%7BENGINE_VERSION%7D", - "custom_deps": {}, - "deps_file": "DEPS", - "safesync_url": "", - }, - ]' > .gclient - gclient sync - - cd ${WORKDIR}/src - ./flutter/tools/gn ${GN_ARGS} -} - -do_compile() { - export PATH=${S}:${S}/${GN_TOOLS_PYTHON2_PATH}:$PATH - - cd ${WORKDIR}/src - ninja -C ${ARTIFACT_DIR} -} - -do_install() { - install -d ${D}${libdir} - install -m 0755 ${WORKDIR}/src/${ARTIFACT_DIR}/libflutter_engine.so ${D}${libdir} -} - -FILES_${PN} = "${libdir}" -FILES_${PN}-dev = "${includedir}" diff --git a/meta-flutter/recipes-graphics/flutter-engine/gn-args-utils.inc b/meta-flutter/recipes-graphics/flutter-engine/gn-args-utils.inc deleted file mode 100755 index e0c17587..00000000 --- a/meta-flutter/recipes-graphics/flutter-engine/gn-args-utils.inc +++ /dev/null @@ -1,12 +0,0 @@ -def get_engine_artifact_dir(d): - gn_args = d.getVar("GN_ARGS") - - artifact_dir = 'out/' - if 'debug' in gn_args: - artifact_dir += 'linux_debug_unopt_arm64' - elif 'profile' in gn_args: - artifact_dir += 'linux_profile_arm64' - else: - artifact_dir += 'linux_release_arm64' - - return artifact_dir \ No newline at end of file diff --git a/src/client_wrapper/flutter_engine.cc b/src/client_wrapper/flutter_engine.cc index a1fc0c46..4cf26678 100644 --- a/src/client_wrapper/flutter_engine.cc +++ b/src/client_wrapper/flutter_engine.cc @@ -30,7 +30,7 @@ FlutterEngine::FlutterEngine(const DartProject& project) { c_engine_properties.dart_entrypoint_argv = entrypoint_argv.size() > 0 ? entrypoint_argv.data() : nullptr; - engine_ = FlutterDesktopEngineCreate(c_engine_properties); + engine_ = FlutterDesktopEngineCreate(&c_engine_properties); auto core_messenger = FlutterDesktopEngineGetMessenger(engine_); messenger_ = std::make_unique(core_messenger); diff --git a/src/client_wrapper/flutter_view_controller.cc b/src/client_wrapper/flutter_view_controller.cc index b8e10f13..47f81529 100644 --- a/src/client_wrapper/flutter_view_controller.cc +++ b/src/client_wrapper/flutter_view_controller.cc @@ -9,19 +9,44 @@ namespace flutter { -FlutterViewController::FlutterViewController(ViewMode view_mode, int width, - int height, bool show_cursor, - const DartProject& project) { +FlutterViewController::FlutterViewController( + const ViewProperties& view_properties, + const DartProject& project) { engine_ = std::make_unique(project); FlutterDesktopViewProperties c_view_properties = {}; - c_view_properties.width = width; - c_view_properties.height = height; - c_view_properties.windw_display_mode = (view_mode == ViewMode::kFullscreen) - ? FlutterWindowMode::kFullscreen - : FlutterWindowMode::kNormal; - c_view_properties.show_cursor = show_cursor; - controller_ = FlutterDesktopViewControllerCreate(c_view_properties, + c_view_properties.width = view_properties.width; + c_view_properties.height = view_properties.height; + c_view_properties.view_rotation = + (view_properties.view_rotation == ViewRotation::kRotation_90) + ? FlutterDesktopViewRotation::kRotation_90 + : (view_properties.view_rotation == ViewRotation::kRotation_180) + ? FlutterDesktopViewRotation::kRotation_180 + : (view_properties.view_rotation == ViewRotation::kRotation_270) + ? FlutterDesktopViewRotation::kRotation_270 + : FlutterDesktopViewRotation::kRotation_0; + c_view_properties.view_mode = + (view_properties.view_mode == ViewMode::kFullscreen) + ? FlutterDesktopViewMode::kFullscreen + : FlutterDesktopViewMode::kNormalscreen; + c_view_properties.title = view_properties.title.has_value() + ? (*view_properties.title).c_str() + : nullptr; + c_view_properties.app_id = view_properties.app_id.has_value() + ? (*view_properties.app_id).c_str() + : nullptr; + c_view_properties.use_mouse_cursor = view_properties.use_mouse_cursor; + c_view_properties.use_onscreen_keyboard = + view_properties.use_onscreen_keyboard; + c_view_properties.use_window_decoration = + view_properties.use_window_decoration; + c_view_properties.text_scale_factor = view_properties.text_scale_factor; + c_view_properties.enable_high_contrast = view_properties.enable_high_contrast; + c_view_properties.force_scale_factor = view_properties.force_scale_factor; + c_view_properties.scale_factor = view_properties.scale_factor; + c_view_properties.enable_vsync = view_properties.enable_vsync; + + controller_ = FlutterDesktopViewControllerCreate(&c_view_properties, engine_->RelinquishEngine()); if (!controller_) { std::cerr << "Failed to create view controller." << std::endl; diff --git a/src/client_wrapper/include/flutter/flutter_engine.h b/src/client_wrapper/include/flutter/flutter_engine.h index df51d88b..4cb7f1d7 100644 --- a/src/client_wrapper/include/flutter/flutter_engine.h +++ b/src/client_wrapper/include/flutter/flutter_engine.h @@ -5,7 +5,7 @@ #ifndef FLUTTER_SHELL_PLATFORM_LINUX_ENBEDDED_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_ #define FLUTTER_SHELL_PLATFORM_LINUX_ENBEDDED_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_ -#include +#include #include #include diff --git a/src/client_wrapper/include/flutter/flutter_view.h b/src/client_wrapper/include/flutter/flutter_view.h index b82c9b5d..31a0b436 100644 --- a/src/client_wrapper/include/flutter/flutter_view.h +++ b/src/client_wrapper/include/flutter/flutter_view.h @@ -5,7 +5,7 @@ #ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_VIEW_H_ #define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_VIEW_H_ -#include +#include namespace flutter { @@ -24,6 +24,9 @@ class FlutterView { // you have to call this every time in the main loop. bool DispatchEvent() { return FlutterDesktopViewDispatchEvent(view_); } + // Returns the display frame rate. + int32_t GetFrameRate() { return FlutterDesktopViewGetFrameRate(view_); } + private: // Handle for interacting with the C API's view. FlutterDesktopViewRef view_ = nullptr; diff --git a/src/client_wrapper/include/flutter/flutter_view_controller.h b/src/client_wrapper/include/flutter/flutter_view_controller.h index d6c43612..2abf0e6a 100644 --- a/src/client_wrapper/include/flutter/flutter_view_controller.h +++ b/src/client_wrapper/include/flutter/flutter_view_controller.h @@ -5,10 +5,11 @@ #ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_VIEW_CONTROLLER_H_ #define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_VIEW_CONTROLLER_H_ -#include +#include #include #include +#include #include "dart_project.h" #include "flutter_engine.h" @@ -25,18 +26,78 @@ namespace flutter { // methods in the C API directly, as this class will do that internally. class FlutterViewController { public: - // The View screen size mode. enum ViewMode { + // Shows the Flutter view by user specific size. kNormal = 0, + // Shows always the Flutter view by fullscreen. kFullscreen = 1, }; + enum ViewRotation { + // Rotation constant: 0 degree rotation (natural orientation) + kRotation_0 = 0, + // Rotation constant: 90 degree rotation. + kRotation_90 = 1, + // Rotation constant: 180 degree rotation. + kRotation_180 = 2, + // Rotation constant: 270 degree rotation. + kRotation_270 = 3, + }; + + // Properties for configuring a Flutter view instance. + typedef struct { + // View width. + int width; + + // View height. + int height; + + // View rotation. + ViewRotation view_rotation; + + // View display mode. If you set kFullscreen, the parameters of both `width` + // and `height` will be ignored. + ViewMode view_mode; + + // View title. + std::optional title; + + // View XDG application ID. As a best practice, it is suggested to select an + // app ID that matches the basename of the application's .desktop file. + std::optional app_id; + + // Uses mouse cursor. + bool use_mouse_cursor; + + // Uses the on-screen keyboard. + bool use_onscreen_keyboard; + + // Uses the window decoration such as toolbar and max/min buttons. + // This option is only active for Wayland backend. + bool use_window_decoration; + + // Text scaling factor. + double text_scale_factor; + + // Enable high contrast. Request that UI be rendered with darker colors. + bool enable_high_contrast; + + // Force scale factor specified by command line argument + bool force_scale_factor; + double scale_factor; + + // Enable Vsync. + // True: Sync to compositor redraw/v-blank (eglSwapInterval 1) + // False: Do not sync to compositor redraw/v-blank (eglSwapInterval 0) + bool enable_vsync; + } ViewProperties; + // Creates a FlutterView that can be parented into a Windows View hierarchy // either using HWNDs or in the future into a CoreWindow, or using compositor. // // |dart_project| will be used to configure the engine backing this view. - explicit FlutterViewController(ViewMode view_mode, int width, int height, - bool show_cursor, const DartProject& project); + explicit FlutterViewController(const ViewProperties& view_properties, + const DartProject& project); virtual ~FlutterViewController(); diff --git a/src/flutter/common/constants.h b/src/flutter/common/constants.h new file mode 100644 index 00000000..668aa4f6 --- /dev/null +++ b/src/flutter/common/constants.h @@ -0,0 +1,38 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_COMMON_CONSTANTS_H_ +#define FLUTTER_COMMON_CONSTANTS_H_ + +namespace flutter { +constexpr double kMegaByteSizeInBytes = (1 << 20); + +// The ID for the implicit view if the implicit view is enabled. +// +// The implicit view is a compatibility mechanism to help the transition from +// the older single-view APIs to the newer multi-view APIs. The two sets of APIs +// use different models for view management. The implicit view mechanism allows +// single-view APIs to operate a special view as if other views don't exist. +// +// In the regular multi-view model, all views should be created by +// `Shell::AddView` before being used, and removed by `Shell::RemoveView` to +// signify that they are gone. If a view is added or removed, the framework +// (`PlatformDispatcher`) will be notified. New view IDs are always unique, +// never reused. Operating a non-existing view is an error. +// +// The implicit view is another special view in addition to the "regular views" +// as above. The shell starts up having the implicit view, which has a fixed +// view ID of `kFlutterImplicitViewId` and is available throughout the lifetime +// of the shell. `Shell::AddView` or `RemoveView` must not be called for this +// view. Even when the window that shows the view is closed, the framework is +// unaware and might continue rendering into or operating this view. +// +// The single-view APIs, which are APIs that do not specify view IDs, operate +// the implicit view. The multi-view APIs can operate all views, including the +// implicit view if the target ID is `kFlutterImplicitViewId`, unless specified +// otherwise. +constexpr int64_t kFlutterImplicitViewId = 0; +} // namespace flutter + +#endif // FLUTTER_COMMON_CONSTANTS_H_ diff --git a/src/flutter/fml/closure.h b/src/flutter/fml/closure.h new file mode 100644 index 00000000..264a95f7 --- /dev/null +++ b/src/flutter/fml/closure.h @@ -0,0 +1,77 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_FML_CLOSURE_H_ +#define FLUTTER_FML_CLOSURE_H_ + +#include + +#include "flutter/fml/macros.h" + +namespace fml { + +using closure = std::function; + +//------------------------------------------------------------------------------ +/// @brief Wraps a closure that is invoked in the destructor unless +/// released by the caller. +/// +/// This is especially useful in dealing with APIs that return a +/// resource by accepting ownership of a sub-resource and a closure +/// that releases that resource. When such APIs are chained, each +/// link in the chain must check that the next member in the chain +/// has accepted the resource. If not, it must invoke the closure +/// eagerly. Not doing this results in a resource leak in the +/// erroneous case. Using this wrapper, the closure can be released +/// once the next call in the chain has successfully accepted +/// ownership of the resource. If not, the closure gets invoked +/// automatically at the end of the scope. This covers the cases +/// where there are early returns as well. +/// +class ScopedCleanupClosure final { + public: + ScopedCleanupClosure() = default; + + ScopedCleanupClosure(ScopedCleanupClosure&& other) { + closure_ = other.Release(); + } + + ScopedCleanupClosure& operator=(ScopedCleanupClosure&& other) { + closure_ = other.Release(); + return *this; + } + + explicit ScopedCleanupClosure(const fml::closure& closure) + : closure_(closure) {} + + ~ScopedCleanupClosure() { Reset(); } + + fml::closure SetClosure(const fml::closure& closure) { + auto old_closure = closure_; + closure_ = closure; + return old_closure; + } + + fml::closure Release() { + fml::closure closure = closure_; + closure_ = nullptr; + return closure; + } + + void Reset() { + if (closure_) { + closure_(); + closure_ = nullptr; + } + } + + private: + fml::closure closure_; + + FML_DISALLOW_COPY_AND_ASSIGN(ScopedCleanupClosure); +}; + +} // namespace fml + +#endif // FLUTTER_FML_CLOSURE_H_ diff --git a/src/flutter/fml/macros.h b/src/flutter/fml/macros.h new file mode 100644 index 00000000..7de4ddf1 --- /dev/null +++ b/src/flutter/fml/macros.h @@ -0,0 +1,44 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_FML_MACROS_H_ +#define FLUTTER_FML_MACROS_H_ + +#ifndef FML_USED_ON_EMBEDDER + +#define FML_EMBEDDER_ONLY [[deprecated]] + +#else // FML_USED_ON_EMBEDDER + +#define FML_EMBEDDER_ONLY + +#endif // FML_USED_ON_EMBEDDER + +#define FML_DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete + +#define FML_DISALLOW_ASSIGN(TypeName) \ + TypeName& operator=(const TypeName&) = delete + +#define FML_DISALLOW_MOVE(TypeName) \ + TypeName(TypeName&&) = delete; \ + TypeName& operator=(TypeName&&) = delete + +#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&) = delete; \ + TypeName& operator=(const TypeName&) = delete + +#define FML_DISALLOW_COPY_ASSIGN_AND_MOVE(TypeName) \ + TypeName(const TypeName&) = delete; \ + TypeName(TypeName&&) = delete; \ + TypeName& operator=(const TypeName&) = delete; \ + TypeName& operator=(TypeName&&) = delete + +#define FML_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ + TypeName() = delete; \ + FML_DISALLOW_COPY_ASSIGN_AND_MOVE(TypeName) + +#define FML_FRIEND_TEST(test_case_name, test_name) \ + friend class test_case_name##_##test_name##_Test + +#endif // FLUTTER_FML_MACROS_H_ diff --git a/src/flutter/shell/platform/common/client_wrapper/core_implementations.cc b/src/flutter/shell/platform/common/client_wrapper/core_implementations.cc index 1412db7c..ec0d6dd9 100644 --- a/src/flutter/shell/platform/common/client_wrapper/core_implementations.cc +++ b/src/flutter/shell/platform/common/client_wrapper/core_implementations.cc @@ -66,6 +66,11 @@ BinaryMessengerImpl::BinaryMessengerImpl( BinaryMessengerImpl::~BinaryMessengerImpl() = default; +void CaptureCleaner(void* lambda) { + auto& cleanup = *reinterpret_cast*>(lambda); + cleanup(); +} + void BinaryMessengerImpl::Send(const std::string& channel, const uint8_t* message, size_t message_size, @@ -87,12 +92,13 @@ void BinaryMessengerImpl::Send(const std::string& channel, captures->reply(data, data_size); delete captures; }; - bool result = FlutterDesktopMessengerSendWithReply( + + FlutterDesktopMessengerSendWithReply( messenger_, channel.c_str(), message, message_size, message_reply, - captures); - if (!result) { - delete captures; - } + captures, [](void* captures_data) { + auto captures = reinterpret_cast(captures_data); + delete captures; + }); } void BinaryMessengerImpl::SetMessageHandler(const std::string& channel, @@ -157,25 +163,46 @@ TextureRegistrarImpl::TextureRegistrarImpl( TextureRegistrarImpl::~TextureRegistrarImpl() = default; int64_t TextureRegistrarImpl::RegisterTexture(TextureVariant* texture) { + FlutterDesktopTextureInfo info = {}; if (auto pixel_buffer_texture = std::get_if(texture)) { - FlutterDesktopTextureInfo info = {}; info.type = kFlutterDesktopPixelBufferTexture; info.pixel_buffer_config.user_data = pixel_buffer_texture; info.pixel_buffer_config.callback = [](size_t width, size_t height, void* user_data) -> const FlutterDesktopPixelBuffer* { auto texture = static_cast(user_data); - auto buffer = texture->CopyPixelBuffer(width, height); - return buffer; + return texture->CopyPixelBuffer(width, height); }; - - int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture( - texture_registrar_ref_, &info); - return texture_id; + } else if (auto gpu_surface_texture = + std::get_if(texture)) { + info.type = kFlutterDesktopGpuSurfaceTexture; + info.gpu_surface_config.struct_size = + sizeof(FlutterDesktopGpuSurfaceTextureConfig); + info.gpu_surface_config.type = gpu_surface_texture->surface_type(); + info.gpu_surface_config.user_data = gpu_surface_texture; + info.gpu_surface_config.callback = + [](size_t width, size_t height, + void* user_data) -> const FlutterDesktopGpuSurfaceDescriptor* { + auto texture = static_cast(user_data); + return texture->ObtainDescriptor(width, height); + }; + } else if (auto egl_image_texture = std::get_if(texture)) { + info.type = kFlutterDesktopEGLImageTexture; + info.egl_image_config.user_data = egl_image_texture; + info.egl_image_config.callback = + [](size_t width, size_t height, void* egl_display, void* egl_context, + void* user_data) -> const FlutterDesktopEGLImage* { + auto texture = static_cast(user_data); + return texture->GetEGLImage(width, height, egl_display, egl_context); + }; + } else { + std::cerr << "Attempting to register unknown texture variant." << std::endl; + return -1; } - std::cerr << "Attempting to register unknown texture variant." << std::endl; - return -1; + int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture( + texture_registrar_ref_, &info); + return texture_id; } // namespace flutter bool TextureRegistrarImpl::MarkTextureFrameAvailable(int64_t texture_id) { @@ -183,9 +210,32 @@ bool TextureRegistrarImpl::MarkTextureFrameAvailable(int64_t texture_id) { texture_registrar_ref_, texture_id); } +void TextureRegistrarImpl::UnregisterTexture(int64_t texture_id, + std::function callback) { + if (callback == nullptr) { + FlutterDesktopTextureRegistrarUnregisterExternalTexture( + texture_registrar_ref_, texture_id, nullptr, nullptr); + return; + } + + struct Captures { + std::function callback; + }; + auto captures = new Captures(); + captures->callback = std::move(callback); + FlutterDesktopTextureRegistrarUnregisterExternalTexture( + texture_registrar_ref_, texture_id, + [](void* opaque) { + auto captures = reinterpret_cast(opaque); + captures->callback(); + delete captures; + }, + captures); +} + bool TextureRegistrarImpl::UnregisterTexture(int64_t texture_id) { - return FlutterDesktopTextureRegistrarUnregisterExternalTexture( - texture_registrar_ref_, texture_id); + UnregisterTexture(texture_id, nullptr); + return true; } } // namespace flutter diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h index a4c7ac40..bf243fde 100644 --- a/src/flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h @@ -63,12 +63,16 @@ class CustomEncodableValue { ~CustomEncodableValue() = default; // Allow implicit conversion to std::any to allow direct use of any_cast. + // NOLINTNEXTLINE(google-explicit-constructor) operator std::any&() { return value_; } + // NOLINTNEXTLINE(google-explicit-constructor) operator const std::any&() const { return value_; } #if defined(FLUTTER_ENABLE_RTTI) && FLUTTER_ENABLE_RTTI // Passthrough to std::any's type(). - const std::type_info& type() const noexcept { return value_.type(); } + const std::type_info& type() const noexcept { + return value_.type(); + } #endif // This operator exists only to provide a stable ordering for use as a @@ -110,7 +114,8 @@ using EncodableValueVariant = std::variant, EncodableList, EncodableMap, - CustomEncodableValue>; + CustomEncodableValue, + std::vector>; } // namespace internal // An object that can contain any value or collection type supported by @@ -155,6 +160,7 @@ using EncodableValueVariant = std::variant -> Uint8List // std::vector -> Int32List // std::vector -> Int64List +// std::vector -> Float32List // std::vector -> Float64List // EncodableList -> List // EncodableMap -> Map @@ -180,6 +186,7 @@ class EncodableValue : public internal::EncodableValueVariant { // to use it with EncodableValue, so the risk of unintended conversions is // minimal, and it avoids the need for the verbose: // EncodableValue(CustomEncodableValue(...)). + // NOLINTNEXTLINE(google-explicit-constructor) EncodableValue(const CustomEncodableValue& v) : super(v) {} // Override the conversion constructors from std::variant to make them @@ -210,6 +217,13 @@ class EncodableValue : public internal::EncodableValueVariant { } return std::get(*this); } + + // Explicitly provide operator<, delegating to std::variant's operator<. + // There are issues with with the way the standard library-provided + // < and <=> comparisons interact with classes derived from variant. + friend bool operator<(const EncodableValue& lhs, const EncodableValue& rhs) { + return static_cast(lhs) < static_cast(rhs); + } }; } // namespace flutter diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/engine_method_result.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/engine_method_result.h index c29ff09b..3cc8b6a2 100644 --- a/src/flutter/shell/platform/common/client_wrapper/include/flutter/engine_method_result.h +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/engine_method_result.h @@ -21,7 +21,7 @@ namespace internal { // vary based on the template type. class ReplyManager { public: - ReplyManager(BinaryReply reply_handler_); + explicit ReplyManager(BinaryReply reply_handler_); ~ReplyManager(); // Prevent copying. diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h index 1c1a056d..c96e6408 100644 --- a/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h @@ -47,10 +47,13 @@ class EventChannel { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will // be handled silently by providing an empty stream. + // + // Note that the EventChannel does not own the handler and will not + // unregister it on destruction. The caller is responsible for unregistering + // the handler if it should no longer be called. void SetStreamHandler(std::unique_ptr> handler) { if (!handler) { messenger_->SetMessageHandler(name_, nullptr); - is_listening_ = false; return; } @@ -61,69 +64,75 @@ class EventChannel { const MethodCodec* codec = codec_; const std::string channel_name = name_; const BinaryMessenger* messenger = messenger_; - BinaryMessageHandler binary_handler = [shared_handler, codec, channel_name, - messenger, - this](const uint8_t* message, - const size_t message_size, - BinaryReply reply) { - constexpr char kOnListenMethod[] = "listen"; - constexpr char kOnCancelMethod[] = "cancel"; - - std::unique_ptr> method_call = - codec->DecodeMethodCall(message, message_size); - if (!method_call) { - std::cerr << "Unable to construct method call from message on channel: " - << channel_name << std::endl; - reply(nullptr, 0); - return; - } - - const std::string& method = method_call->method_name(); - if (method.compare(kOnListenMethod) == 0) { - if (is_listening_) { - std::unique_ptr> error = - shared_handler->OnCancel(nullptr); - if (error) { - std::cerr << "Failed to cancel existing stream: " - << (error->error_code) << ", " << (error->error_message) - << ", " << (error->error_details); + BinaryMessageHandler binary_handler = + [shared_handler, codec, channel_name, messenger, + // Mutable state to track the handler's listening status. + is_listening = bool(false)](const uint8_t* message, + const size_t message_size, + BinaryReply reply) mutable { + constexpr char kOnListenMethod[] = "listen"; + constexpr char kOnCancelMethod[] = "cancel"; + + std::unique_ptr> method_call = + codec->DecodeMethodCall(message, message_size); + if (!method_call) { + std::cerr + << "Unable to construct method call from message on channel: " + << channel_name << std::endl; + reply(nullptr, 0); + return; } - } - is_listening_ = true; - - std::unique_ptr> result; - auto sink = std::make_unique( - messenger, channel_name, codec); - std::unique_ptr> error = - shared_handler->OnListen(method_call->arguments(), std::move(sink)); - if (error) { - result = codec->EncodeErrorEnvelope( - error->error_code, error->error_message, error->error_details); - } else { - result = codec->EncodeSuccessEnvelope(); - } - reply(result->data(), result->size()); - } else if (method.compare(kOnCancelMethod) == 0) { - std::unique_ptr> result; - if (is_listening_) { - std::unique_ptr> error = - shared_handler->OnCancel(method_call->arguments()); - if (error) { - result = codec->EncodeErrorEnvelope( - error->error_code, error->error_message, error->error_details); + + const std::string& method = method_call->method_name(); + if (method.compare(kOnListenMethod) == 0) { + if (is_listening) { + std::unique_ptr> error = + shared_handler->OnCancel(nullptr); + if (error) { + std::cerr << "Failed to cancel existing stream: " + << (error->error_code) << ", " + << (error->error_message) << ", " + << (error->error_details.get()); + } + } + is_listening = true; + + std::unique_ptr> result; + auto sink = std::make_unique( + messenger, channel_name, codec); + std::unique_ptr> error = + shared_handler->OnListen(method_call->arguments(), + std::move(sink)); + if (error) { + result = codec->EncodeErrorEnvelope(error->error_code, + error->error_message, + error->error_details.get()); + } else { + result = codec->EncodeSuccessEnvelope(); + } + reply(result->data(), result->size()); + } else if (method.compare(kOnCancelMethod) == 0) { + std::unique_ptr> result; + if (is_listening) { + std::unique_ptr> error = + shared_handler->OnCancel(method_call->arguments()); + if (error) { + result = codec->EncodeErrorEnvelope(error->error_code, + error->error_message, + error->error_details.get()); + } else { + result = codec->EncodeSuccessEnvelope(); + } + is_listening = false; + } else { + result = codec->EncodeErrorEnvelope( + "error", "No active stream to cancel", nullptr); + } + reply(result->data(), result->size()); } else { - result = codec->EncodeSuccessEnvelope(); + reply(nullptr, 0); } - is_listening_ = false; - } else { - result = codec->EncodeErrorEnvelope( - "error", "No active stream to cancel", nullptr); - } - reply(result->data(), result->size()); - } else { - reply(nullptr, 0); - } - }; + }; messenger_->SetMessageHandler(name_, std::move(binary_handler)); } @@ -165,7 +174,6 @@ class EventChannel { BinaryMessenger* messenger_; const std::string name_; const MethodCodec* codec_; - bool is_listening_; }; } // namespace flutter diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h index 9eced6cf..9c1d8941 100644 --- a/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h @@ -5,6 +5,9 @@ #ifndef FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_ #define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_ +#include +#include + #include "event_sink.h" namespace flutter { @@ -13,16 +16,16 @@ class EncodableValue; template struct StreamHandlerError { - const std::string& error_code; - const std::string& error_message; - const T* error_details; + const std::string error_code; + const std::string error_message; + const std::unique_ptr error_details; - StreamHandlerError(const std::string& error_code, - const std::string& error_message, - const T* error_details) + StreamHandlerError(const std::string error_code, + const std::string error_message, + std::unique_ptr&& error_details) : error_code(error_code), error_message(error_message), - error_details(error_details) {} + error_details(std::move(error_details)) {} }; // Handler for stream setup and teardown requests. diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h index e9ed6161..6e39a644 100644 --- a/src/flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h @@ -91,9 +91,10 @@ class MethodChannel { // Registers a handler that should be called any time a method call is // received on this channel. A null handler will remove any previous handler. // - // Note that the MethodChannel does not own the handler, and will not - // unregister it on destruction, so the caller is responsible for - // unregistering explicitly if it should no longer be called. + // The handler will be owned by the underlying BinaryMessageHandler. + // Destroying the MethodChannel will not unregister the handler, so + // the caller is responsible for unregistering explicitly if the handler + // stops being valid before the engine is destroyed. void SetMethodCallHandler(MethodCallHandler handler) const { if (!handler) { messenger_->SetMessageHandler(name_, nullptr); diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h index 858119e3..15cf9968 100644 --- a/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h @@ -49,8 +49,8 @@ class PluginRegistrar { // Takes ownership of |plugin|. // // Plugins are not required to call this method if they have other lifetime - // management, but this is a convient place for plugins to be owned to ensure - // that they stay valid for any registered callbacks. + // management, but this is a convenient place for plugins to be owned to + // ensure that they stay valid for any registered callbacks. void AddPlugin(std::unique_ptr plugin); protected: diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/string_message_codec.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/string_message_codec.h new file mode 100644 index 00000000..11f31d41 --- /dev/null +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/string_message_codec.h @@ -0,0 +1,45 @@ +// Copyright 2022 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef __CODE_FLUTTER_EMBEDDED_LINUX_SRC_FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_STRING_MESSAGE_CODEC_H_ +#define __CODE_FLUTTER_EMBEDDED_LINUX_SRC_FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_STRING_MESSAGE_CODEC_H_ + +#include "message_codec.h" + +namespace flutter { + +// A message encoding/decoding mechanism for communications to/from the Flutter +// engine via UTF-8 encoded string messages. +// +// This codec is guaranteed to be compatible with the corresponding +// [StringCodec](https://api.flutter.dev/flutter/services/StringCodec-class.html) +// on the Dart side. These parts of the Flutter SDK are evolved synchronously. +class StringMessageCodec : public MessageCodec { + public: + // Returns the shared instance of the codec. + static const StringMessageCodec& GetInstance(); + + ~StringMessageCodec() = default; + + // Prevent copying. + StringMessageCodec(StringMessageCodec const&) = delete; + StringMessageCodec& operator=(StringMessageCodec const&) = delete; + + protected: + // Instances should be obtained via GetInstance. + StringMessageCodec() = default; + + // |flutter::MessageCodec| + std::unique_ptr DecodeMessageInternal( + const uint8_t* binary_message, + const size_t message_size) const override; + + // |flutter::MessageCodec| + std::unique_ptr> EncodeMessageInternal( + const std::string& message) const override; +}; + +} // namespace flutter + +#endif // __CODE_FLUTTER_EMBEDDED_LINUX_SRC_FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_STRING_MESSAGE_CODEC_H_ diff --git a/src/flutter/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h b/src/flutter/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h index 5b59829f..43f1147c 100644 --- a/src/flutter/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h +++ b/src/flutter/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h @@ -27,7 +27,7 @@ class PixelBufferTexture { // As the callback is usually invoked from the render thread, the callee must // take care of proper synchronization. It also needs to be ensured that the // returned buffer isn't released prior to unregistering this texture. - PixelBufferTexture(CopyBufferCallback copy_buffer_callback) + explicit PixelBufferTexture(CopyBufferCallback copy_buffer_callback) : copy_buffer_callback_(copy_buffer_callback) {} // Returns the callback-provided FlutterDesktopPixelBuffer that contains the @@ -42,10 +42,73 @@ class PixelBufferTexture { const CopyBufferCallback copy_buffer_callback_; }; +// A EGLImage texture. +class EGLImageTexture { + public: + // A callback used for retrieving pixel buffers. + typedef std::function + GetEGLImageCallback; + + // Creates a EGLImage texture that uses the provided |get_egl_image_cb| to + // retrieve the EGLImage. + // As the callback is usually invoked from the render thread, the callee must + // take care of proper synchronization. It also needs to be ensured that the + // returned buffer isn't released prior to unregistering this texture. + explicit EGLImageTexture(GetEGLImageCallback get_egl_image_callback) + : get_egl_image_callback_(get_egl_image_callback) {} + + // Returns the callback-provided FlutterDesktopEGLImage. + // The intended surface size is specified by |width| and + // |height|. + const FlutterDesktopEGLImage* GetEGLImage(size_t width, + size_t height, + void* egl_display, + void* egl_context) const { + return get_egl_image_callback_(width, height, egl_display, egl_context); + } + + private: + const GetEGLImageCallback get_egl_image_callback_; +}; + +// A GPU surface-based texture. +class GpuSurfaceTexture { + public: + // A callback used for retrieving surface descriptors. + typedef std::function< + const FlutterDesktopGpuSurfaceDescriptor*(size_t width, size_t height)> + ObtainDescriptorCallback; + + GpuSurfaceTexture(FlutterDesktopGpuSurfaceType surface_type, + ObtainDescriptorCallback obtain_descriptor_callback) + : surface_type_(surface_type), + obtain_descriptor_callback_(obtain_descriptor_callback) {} + + // Returns the callback-provided FlutterDesktopGpuSurfaceDescriptor that + // contains the surface handle. The intended surface size is specified by + // |width| and |height|. + const FlutterDesktopGpuSurfaceDescriptor* ObtainDescriptor( + size_t width, + size_t height) const { + return obtain_descriptor_callback_(width, height); + } + + // Gets the surface type. + FlutterDesktopGpuSurfaceType surface_type() const { return surface_type_; } + + private: + const FlutterDesktopGpuSurfaceType surface_type_; + const ObtainDescriptorCallback obtain_descriptor_callback_; +}; + // The available texture variants. // Only PixelBufferTexture is currently implemented. // Other variants are expected to be added in the future. -typedef std::variant TextureVariant; +typedef std::variant + TextureVariant; // An object keeping track of external textures. // @@ -65,10 +128,15 @@ class TextureRegistrar { // the callback that was provided upon creating the texture. virtual bool MarkTextureFrameAvailable(int64_t texture_id) = 0; - // Unregisters an existing Texture object. - // Textures must not be unregistered while they're in use. + // Asynchronously unregisters an existing texture object. + // Upon completion, the optional |callback| gets invoked. + virtual void UnregisterTexture(int64_t texture_id, + std::function callback) = 0; + + // Unregisters an existing texture object. + // DEPRECATED: Use UnregisterTexture(texture_id, optional_callback) instead. virtual bool UnregisterTexture(int64_t texture_id) = 0; -}; +} SWIFT_UNSAFE_REFERENCE; } // namespace flutter diff --git a/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc b/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc index 3e86879b..5e93d407 100644 --- a/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc +++ b/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc @@ -41,6 +41,7 @@ enum class EncodedType { kFloat64List, kList, kMap, + kFloat32List, }; // Returns the encoded type that should be written when serializing |value|. @@ -70,6 +71,8 @@ EncodedType EncodedTypeForValue(const EncodableValue& value) { return EncodedType::kList; case 11: return EncodedType::kMap; + case 13: + return EncodedType::kFloat32List; } assert(false); return EncodedType::kNull; @@ -95,7 +98,7 @@ EncodableValue StandardCodecSerializer::ReadValue( void StandardCodecSerializer::WriteValue(const EncodableValue& value, ByteStreamWriter* stream) const { stream->WriteByte(static_cast(EncodedTypeForValue(value))); - // TODO: Consider replacing this this with a std::visitor. + // TODO(cbracken): Consider replacing this with std::visit. switch (value.index()) { case 0: case 1: @@ -155,6 +158,10 @@ void StandardCodecSerializer::WriteValue(const EncodableValue& value, << "Unhandled custom type in StandardCodecSerializer::WriteValue. " << "Custom types require codec extensions." << std::endl; break; + case 13: { + WriteVector(std::get>(value), stream); + break; + } } } @@ -210,6 +217,9 @@ EncodableValue StandardCodecSerializer::ReadValueOfType( } return EncodableValue(map_value); } + case EncodedType::kFloat32List: { + return ReadVector(stream); + } } std::cerr << "Unknown type in StandardCodecSerializer::ReadValueOfType: " << static_cast(type) << std::endl; @@ -221,11 +231,11 @@ size_t StandardCodecSerializer::ReadSize(ByteStreamReader* stream) const { if (byte < 254) { return byte; } else if (byte == 254) { - uint16_t value; + uint16_t value = 0; stream->ReadBytes(reinterpret_cast(&value), 2); return value; } else { - uint32_t value; + uint32_t value = 0; stream->ReadBytes(reinterpret_cast(&value), 4); return value; } @@ -285,8 +295,8 @@ const StandardMessageCodec& StandardMessageCodec::GetInstance( if (!serializer) { serializer = &StandardCodecSerializer::GetInstance(); } - auto* sInstances = new std::map>; + static auto* sInstances = new std::map>; auto it = sInstances->find(serializer); if (it == sInstances->end()) { // Uses new due to private constructor (to prevent API clients from @@ -308,6 +318,9 @@ StandardMessageCodec::~StandardMessageCodec() = default; std::unique_ptr StandardMessageCodec::DecodeMessageInternal( const uint8_t* binary_message, size_t message_size) const { + if (!binary_message) { + return std::make_unique(); + } ByteBufferStreamReader stream(binary_message, message_size); return std::make_unique(serializer_->ReadValue(&stream)); } @@ -329,8 +342,8 @@ const StandardMethodCodec& StandardMethodCodec::GetInstance( if (!serializer) { serializer = &StandardCodecSerializer::GetInstance(); } - auto* sInstances = new std::map>; + static auto* sInstances = new std::map>; auto it = sInstances->find(serializer); if (it == sInstances->end()) { // Uses new due to private constructor (to prevent API clients from diff --git a/src/flutter/shell/platform/common/client_wrapper/string_message_codec.cc b/src/flutter/shell/platform/common/client_wrapper/string_message_codec.cc new file mode 100644 index 00000000..d0c12bac --- /dev/null +++ b/src/flutter/shell/platform/common/client_wrapper/string_message_codec.cc @@ -0,0 +1,34 @@ +// Copyright 2022 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "include/flutter/string_message_codec.h" + +#include + +namespace flutter { + +// static +const StringMessageCodec& StringMessageCodec::GetInstance() { + static StringMessageCodec sInstance; + return sInstance; +} + +std::unique_ptr> StringMessageCodec::EncodeMessageInternal( + const std::string& message) const { + return std::make_unique>(message.begin(), message.end()); +} + +std::unique_ptr StringMessageCodec::DecodeMessageInternal( + const uint8_t* binary_message, + const size_t message_size) const { + if (!binary_message) { + return nullptr; + } + + auto raw_message = reinterpret_cast(binary_message); + + return std::make_unique(raw_message, message_size); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/common/client_wrapper/texture_registrar_impl.h b/src/flutter/shell/platform/common/client_wrapper/texture_registrar_impl.h index df51f6dd..bd01839d 100644 --- a/src/flutter/shell/platform/common/client_wrapper/texture_registrar_impl.h +++ b/src/flutter/shell/platform/common/client_wrapper/texture_registrar_impl.h @@ -27,6 +27,10 @@ class TextureRegistrarImpl : public TextureRegistrar { // |flutter::TextureRegistrar| bool MarkTextureFrameAvailable(int64_t texture_id) override; + // |flutter::TextureRegistrar| + void UnregisterTexture(int64_t texture_id, + std::function callback) override; + // |flutter::TextureRegistrar| bool UnregisterTexture(int64_t texture_id) override; diff --git a/src/flutter/shell/platform/common/engine_switches.cc b/src/flutter/shell/platform/common/engine_switches.cc index 5c750f13..37b98fae 100644 --- a/src/flutter/shell/platform/common/engine_switches.cc +++ b/src/flutter/shell/platform/common/engine_switches.cc @@ -16,7 +16,6 @@ std::vector GetSwitchesFromEnvironment() { // Read engine switches from the environment in debug/profile. If release mode // support is needed in the future, it should likely use a whitelist. #ifndef FLUTTER_RELEASE -#ifndef WINUWP const char* switch_count_key = "FLUTTER_ENGINE_SWITCHES"; const int kMaxSwitchCount = 50; const char* switch_count_string = std::getenv(switch_count_key); @@ -37,7 +36,6 @@ std::vector GetSwitchesFromEnvironment() { << ", but " << switch_key.str() << " is missing." << std::endl; } } -#endif // !WINUWP #endif // !FLUTTER_RELEASE return switches; } diff --git a/src/flutter/shell/platform/common/json_message_codec.cc b/src/flutter/shell/platform/common/json_message_codec.cc index b91e57e4..ae362f73 100644 --- a/src/flutter/shell/platform/common/json_message_codec.cc +++ b/src/flutter/shell/platform/common/json_message_codec.cc @@ -37,9 +37,7 @@ std::unique_ptr JsonMessageCodec::DecodeMessageInternal( auto json_message = std::make_unique(); rapidjson::ParseResult result = json_message->Parse(raw_message, message_size); - bool parsing_successful = - result == rapidjson::ParseErrorCode::kParseErrorNone; - if (!parsing_successful) { + if (result.IsError()) { std::cerr << "Unable to parse JSON message:" << std::endl << rapidjson::GetParseError_En(result.Code()) << std::endl; return nullptr; diff --git a/src/flutter/shell/platform/common/json_method_codec.cc b/src/flutter/shell/platform/common/json_method_codec.cc index cc4ffec6..9ccb79c8 100644 --- a/src/flutter/shell/platform/common/json_method_codec.cc +++ b/src/flutter/shell/platform/common/json_method_codec.cc @@ -104,7 +104,8 @@ JsonMethodCodec::EncodeErrorEnvelopeInternal( rapidjson::Document envelope(rapidjson::kArrayType); auto& allocator = envelope.GetAllocator(); envelope.PushBack(rapidjson::Value(error_code.c_str(), allocator), allocator); - envelope.PushBack(rapidjson::Value(error_message.c_str(), allocator), allocator); + envelope.PushBack(rapidjson::Value(error_message.c_str(), allocator), + allocator); rapidjson::Value details_value; if (error_details) { details_value.CopyFrom(*error_details, allocator); diff --git a/src/flutter/shell/platform/common/public/flutter_export.h b/src/flutter/shell/platform/common/public/flutter_export.h index 38cac85b..5fe8c593 100644 --- a/src/flutter/shell/platform/common/public/flutter_export.h +++ b/src/flutter/shell/platform/common/public/flutter_export.h @@ -25,4 +25,13 @@ #endif // FLUTTER_DESKTOP_LIBRARY +#if __has_include() +#include +#else +#define SWIFT_UNSAFE_REFERENCE +#define SWIFT_SHARED_REFERENCE(_retain, _release) +#define SWIFT_RETURNS_RETAINED +#define SWIFT_RETURNS_UNRETAINED +#endif + #endif // FLUTTER_SHELL_PLATFORM_COMMON_PUBLIC_FLUTTER_EXPORT_H_ diff --git a/src/flutter/shell/platform/common/public/flutter_messenger.h b/src/flutter/shell/platform/common/public/flutter_messenger.h index 854ff985..d06385ca 100644 --- a/src/flutter/shell/platform/common/public/flutter_messenger.h +++ b/src/flutter/shell/platform/common/public/flutter_messenger.h @@ -63,7 +63,8 @@ FLUTTER_EXPORT bool FlutterDesktopMessengerSendWithReply( const uint8_t* message, const size_t message_size, const FlutterDesktopBinaryReply reply, - void* user_data); + void* user_data, + void (*cleanup)(void* captures_data)); // Sends a reply to a FlutterDesktopMessage for the given response handle. // @@ -87,6 +88,53 @@ FLUTTER_EXPORT void FlutterDesktopMessengerSetCallback( FlutterDesktopMessageCallback callback, void* user_data); +// Increments the reference count for the |messenger|. +// +// Operation is thread-safe. +// +// See also: |FlutterDesktopMessengerRelease| +FLUTTER_EXPORT FlutterDesktopMessengerRef FlutterDesktopMessengerAddRef( + FlutterDesktopMessengerRef messenger) SWIFT_RETURNS_RETAINED; + +// Decrements the reference count for the |messenger|. +// +// Operation is thread-safe. +// +// See also: |FlutterDesktopMessengerAddRef| +FLUTTER_EXPORT void FlutterDesktopMessengerRelease( + FlutterDesktopMessengerRef messenger); + +// Returns `true` if the |FlutterDesktopMessengerRef| still references a running +// engine. +// +// This check should be made inside of a |FlutterDesktopMessengerLock| and +// before any other calls are made to the FlutterDesktopMessengerRef when using +// it from a thread other than the platform thread. +FLUTTER_EXPORT bool FlutterDesktopMessengerIsAvailable( + FlutterDesktopMessengerRef messenger); + +// Locks the `FlutterDesktopMessengerRef` ensuring that +// |FlutterDesktopMessengerIsAvailable| does not change while locked. +// +// All calls to the FlutterDesktopMessengerRef from threads other than the +// platform thread should happen inside of a lock. +// +// Operation is thread-safe. +// +// Returns the |messenger| value. +// +// See also: |FlutterDesktopMessengerUnlock| +FLUTTER_EXPORT FlutterDesktopMessengerRef FlutterDesktopMessengerLock( + FlutterDesktopMessengerRef messenger) SWIFT_RETURNS_UNRETAINED; + +// Unlocks the `FlutterDesktopMessengerRef`. +// +// Operation is thread-safe. +// +// See also: |FlutterDesktopMessengerLock| +FLUTTER_EXPORT void FlutterDesktopMessengerUnlock( + FlutterDesktopMessengerRef messenger); + #if defined(__cplusplus) } // extern "C" #endif diff --git a/src/flutter/shell/platform/common/public/flutter_plugin_registrar.h b/src/flutter/shell/platform/common/public/flutter_plugin_registrar.h index b0847069..23a13b6d 100644 --- a/src/flutter/shell/platform/common/public/flutter_plugin_registrar.h +++ b/src/flutter/shell/platform/common/public/flutter_plugin_registrar.h @@ -26,7 +26,7 @@ typedef void (*FlutterDesktopOnPluginRegistrarDestroyed)( // Returns the engine messenger associated with this registrar. FLUTTER_EXPORT FlutterDesktopMessengerRef FlutterDesktopPluginRegistrarGetMessenger( - FlutterDesktopPluginRegistrarRef registrar); + FlutterDesktopPluginRegistrarRef registrar) SWIFT_RETURNS_UNRETAINED; // Returns the texture registrar associated with this registrar. FLUTTER_EXPORT FlutterDesktopTextureRegistrarRef diff --git a/src/flutter/shell/platform/common/public/flutter_texture_registrar.h b/src/flutter/shell/platform/common/public/flutter_texture_registrar.h index b115234e..a56c3107 100644 --- a/src/flutter/shell/platform/common/public/flutter_texture_registrar.h +++ b/src/flutter/shell/platform/common/public/flutter_texture_registrar.h @@ -23,9 +23,37 @@ typedef struct FlutterDesktopTextureRegistrar* // Additional types may be added in the future. typedef enum { // A Pixel buffer-based texture. - kFlutterDesktopPixelBufferTexture + kFlutterDesktopPixelBufferTexture, + // A platform-specific GPU surface-backed texture. + kFlutterDesktopGpuSurfaceTexture, + // An EGLImage-based texture + kFlutterDesktopEGLImageTexture } FlutterDesktopTextureType; +// Supported GPU surface types. +typedef enum { + // Uninitialized. + kFlutterDesktopGpuSurfaceTypeNone, + // A DXGI shared texture handle (Windows only). + // See + // https://docs.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiresource-getsharedhandle + kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle, + // A |ID3D11Texture2D| (Windows only). + kFlutterDesktopGpuSurfaceTypeD3d11Texture2D, +} FlutterDesktopGpuSurfaceType; + +// Supported pixel formats. +typedef enum { + // Uninitialized. + kFlutterDesktopPixelFormatNone, + // Represents a 32-bit RGBA color format with 8 bits each for red, green, blue + // and alpha. + kFlutterDesktopPixelFormatRGBA8888, + // Represents a 32-bit BGRA color format with 8 bits each for blue, green, red + // and alpha. + kFlutterDesktopPixelFormatBGRA8888 +} FlutterDesktopPixelFormat; + // An image buffer object. typedef struct { // The pixel data buffer. @@ -34,22 +62,94 @@ typedef struct { size_t width; // Height of the pixel buffer. size_t height; + // An optional callback that gets invoked when the |buffer| can be released. + void (*release_callback)(void* release_context); + // Opaque data passed to |release_callback|. + void* release_context; } FlutterDesktopPixelBuffer; +// An EGLImage object. +typedef struct { + // The EGLImage object.(opaque) + const void* egl_image; + // Width of the EGLImage. + size_t width; + // Height of the EGLImage. + size_t height; + // An optional callback that gets invoked when the |egl_image| can be + // released. + void (*release_callback)(void* release_context); + // Opaque data passed to |release_callback|. + void* release_context; +} FlutterDesktopEGLImage; + +// A GPU surface descriptor. +typedef struct { + // The size of this struct. Must be + // sizeof(FlutterDesktopGpuSurfaceDescriptor). + size_t struct_size; + // The surface handle. The expected type depends on the + // |FlutterDesktopGpuSurfaceType|. + // + // Provide a |ID3D11Texture2D*| when using + // |kFlutterDesktopGpuSurfaceTypeD3d11Texture2D| or a |HANDLE| when using + // |kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle|. + // + // The referenced resource needs to stay valid until it has been opened by + // Flutter. Consider incrementing the resource's reference count in the + // |FlutterDesktopGpuSurfaceTextureCallback| and registering a + // |release_callback| for decrementing the reference count once it has been + // opened. + void* handle; + // The physical width. + size_t width; + // The physical height. + size_t height; + // The visible width. + // It might be less or equal to the physical |width|. + size_t visible_width; + // The visible height. + // It might be less or equal to the physical |height|. + size_t visible_height; + // The pixel format which might be optional depending on the surface type. + FlutterDesktopPixelFormat format; + // An optional callback that gets invoked when the |handle| has been opened. + void (*release_callback)(void* release_context); + // Opaque data passed to |release_callback|. + void* release_context; +} FlutterDesktopGpuSurfaceDescriptor; + // The pixel buffer copy callback definition provided to // the Flutter engine to copy the texture. // It is invoked with the intended surface size specified by |width| and -// |height| and the |user_data| held by FlutterDesktopPixelBufferTextureConfig. +// |height| and the |user_data| held by +// |FlutterDesktopPixelBufferTextureConfig|. // // As this is usually called from the render thread, the callee must take // care of proper synchronization. It also needs to be ensured that the -// returned FlutterDesktopPixelBuffer isn't released prior to unregistering +// returned |FlutterDesktopPixelBuffer| isn't released prior to unregistering // the corresponding texture. typedef const FlutterDesktopPixelBuffer* ( *FlutterDesktopPixelBufferTextureCallback)(size_t width, size_t height, void* user_data); +// The GPU surface callback definition provided to the Flutter engine to obtain +// the surface. It is invoked with the intended surface size specified by +// |width| and |height| and the |user_data| held by +// |FlutterDesktopGpuSurfaceTextureConfig|. +typedef const FlutterDesktopGpuSurfaceDescriptor* ( + *FlutterDesktopGpuSurfaceTextureCallback)(size_t width, + size_t height, + void* user_data); + +typedef const FlutterDesktopEGLImage* (*FlutterDesktopEGLImageTextureCallback)( + size_t width, + size_t height, + void* egl_display, + void* egl_context, + void* user_data); + // An object used to configure pixel buffer textures. typedef struct { // The callback used by the engine to copy the pixel buffer object. @@ -58,10 +158,34 @@ typedef struct { void* user_data; } FlutterDesktopPixelBufferTextureConfig; +// An object used to configure GPU-surface textures. +typedef struct { + // The size of this struct. Must be + // sizeof(FlutterDesktopGpuSurfaceTextureConfig). + size_t struct_size; + // The concrete surface type (e.g. + // |kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle|) + FlutterDesktopGpuSurfaceType type; + // The callback used by the engine to obtain the surface descriptor. + FlutterDesktopGpuSurfaceTextureCallback callback; + // Opaque data that will get passed to the provided |callback|. + void* user_data; +} FlutterDesktopGpuSurfaceTextureConfig; + +// An object used to configure EGLImage textures. +typedef struct { + // The callback used by the engine to get the EGLImage object. + FlutterDesktopEGLImageTextureCallback callback; + // Opaque data that will get passed to the provided |callback|. + void* user_data; +} FlutterDesktopEGLImageTextureConfig; + typedef struct { FlutterDesktopTextureType type; union { FlutterDesktopPixelBufferTextureConfig pixel_buffer_config; + FlutterDesktopGpuSurfaceTextureConfig gpu_surface_config; + FlutterDesktopEGLImageTextureConfig egl_image_config; }; } FlutterDesktopTextureInfo; @@ -71,13 +195,15 @@ FLUTTER_EXPORT int64_t FlutterDesktopTextureRegistrarRegisterExternalTexture( FlutterDesktopTextureRegistrarRef texture_registrar, const FlutterDesktopTextureInfo* info); -// Unregisters an existing texture from the Flutter engine for a |texture_id|. -// Returns true on success or false if the specified texture doesn't exist. +// Asynchronously unregisters the texture identified by |texture_id| from the +// Flutter engine. +// An optional |callback| gets invoked upon completion. // This function can be called from any thread. -// However, textures must not be unregistered while they're in use. -FLUTTER_EXPORT bool FlutterDesktopTextureRegistrarUnregisterExternalTexture( +FLUTTER_EXPORT void FlutterDesktopTextureRegistrarUnregisterExternalTexture( FlutterDesktopTextureRegistrarRef texture_registrar, - int64_t texture_id); + int64_t texture_id, + void (*callback)(void* user_data), + void* user_data); // Marks that a new texture frame is available for a given |texture_id|. // Returns true on success or false if the specified texture doesn't exist. diff --git a/src/flutter/shell/platform/common/text_range.h b/src/flutter/shell/platform/common/text_range.h index a7b3760b..6a0deff6 100644 --- a/src/flutter/shell/platform/common/text_range.h +++ b/src/flutter/shell/platform/common/text_range.h @@ -7,7 +7,7 @@ #include -//#include "flutter/fml/logging.h" +// #include "flutter/fml/logging.h" namespace flutter { @@ -66,7 +66,7 @@ class TextRange { // // Asserts that the range is of length 0. size_t position() const { - //FML_DCHECK(base_ == extent_); + // FML_DCHECK(base_ == extent_); return extent_; } diff --git a/src/flutter/shell/platform/embedder/embedder.h b/src/flutter/shell/platform/embedder/embedder.h index 8a1ce19d..9e9b875a 100644 --- a/src/flutter/shell/platform/embedder/embedder.h +++ b/src/flutter/shell/platform/embedder/embedder.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_EMBEDDER_H_ -#define FLUTTER_EMBEDDER_H_ +#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_H_ +#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_H_ #include #include @@ -25,9 +25,16 @@ // - Function signatures (names, argument counts, argument order, and argument // type) cannot change. // - The core behavior of existing functions cannot change. +// - Instead of nesting structures by value within another structure/union, +// prefer nesting by pointer. This ensures that adding members to the nested +// struct does not break the ABI of the parent struct/union. +// - Instead of array of structures, prefer array of pointers to structures. +// This ensures that array indexing does not break if members are added +// to the structure. // // These changes are allowed: -// - Adding new struct members at the end of a structure. +// - Adding new struct members at the end of a structure as long as the struct +// is not nested within another struct by value. // - Adding new enum members with a new value. // - Renaming a struct member as long as its type, size, and intent remain the // same. @@ -76,6 +83,7 @@ typedef enum { /// iOS version >= 10.0 (device), 13.0 (simulator) /// macOS version >= 10.14 kMetal, + kVulkan, } FlutterRendererType; /// Additional accessibility features that may be enabled by the platform. @@ -91,8 +99,12 @@ typedef enum { /// Request that text be rendered at a bold font weight. kFlutterAccessibilityFeatureBoldText = 1 << 3, /// Request that certain animations be simplified and parallax effects - // removed. + /// removed. kFlutterAccessibilityFeatureReduceMotion = 1 << 4, + /// Request that UI be rendered with darker colors. + kFlutterAccessibilityFeatureHighContrast = 1 << 5, + /// Request to show on/off labels inside switches. + kFlutterAccessibilityFeatureOnOffSwitchLabels = 1 << 6, } FlutterAccessibilityFeature; /// The set of possible actions that can be conveyed to a semantics node. @@ -148,6 +160,10 @@ typedef enum { kFlutterSemanticsActionMoveCursorForwardByWord = 1 << 19, /// Move the cursor backward by one word. kFlutterSemanticsActionMoveCursorBackwardByWord = 1 << 20, + /// Replace the current text in the text field. + kFlutterSemanticsActionSetText = 1 << 21, + /// Request that the respective focusable widget gain input focus. + kFlutterSemanticsActionFocus = 1 << 22, } FlutterSemanticsAction; /// The set of properties that may be associated with a semantics node. @@ -202,6 +218,12 @@ typedef enum { /// `PageView` widget does not have implicit scrolling, so that users don't /// navigate to the next page when reaching the end of the current one. kFlutterSemanticsFlagHasImplicitScrolling = 1 << 18, + /// Whether the value of the semantics node is coming from a multi-line text + /// field. + /// + /// This is used for text fields to distinguish single-line text fields from + /// multi-line ones. + kFlutterSemanticsFlagIsMultiline = 1 << 19, /// Whether the semantic node is read only. /// /// Only applicable when kFlutterSemanticsFlagIsTextField flag is on. @@ -214,6 +236,13 @@ typedef enum { kFlutterSemanticsFlagIsSlider = 1 << 23, /// Whether the semantics node represents a keyboard key. kFlutterSemanticsFlagIsKeyboardKey = 1 << 24, + /// Whether the semantics node represents a tristate checkbox in mixed state. + kFlutterSemanticsFlagIsCheckStateMixed = 1 << 25, + /// The semantics node has the quality of either being "expanded" or + /// "collapsed". + kFlutterSemanticsFlagHasExpandedState = 1 << 26, + /// Whether a semantic node that hasExpandedState is currently expanded. + kFlutterSemanticsFlagIsExpanded = 1 << 27, } FlutterSemanticsFlag; typedef enum { @@ -225,8 +254,26 @@ typedef enum { kFlutterTextDirectionLTR = 2, } FlutterTextDirection; +/// Valid values for priority of Thread. +typedef enum { + /// Suitable for threads that shouldn't disrupt high priority work. + kBackground = 0, + /// Default priority level. + kNormal = 1, + /// Suitable for threads which generate data for the display. + kDisplay = 2, + /// Suitable for thread which raster data. + kRaster = 3, +} FlutterThreadPriority; + typedef struct _FlutterEngine* FLUTTER_API_SYMBOL(FlutterEngine); +/// Unique identifier for views. +/// +/// View IDs are generated by the embedder and are +/// opaque to the engine; the engine does not interpret view IDs in any way. +typedef int64_t FlutterViewId; + typedef struct { /// horizontal scale factor double scaleX; @@ -257,8 +304,68 @@ typedef enum { /// Specifies an OpenGL frame-buffer target type. Framebuffers are specified /// using the FlutterOpenGLFramebuffer struct. kFlutterOpenGLTargetTypeFramebuffer, + /// Specifies an OpenGL on-screen surface target type. Surfaces are specified + /// using the FlutterOpenGLSurface struct. + kFlutterOpenGLTargetTypeSurface, } FlutterOpenGLTargetType; +/// A pixel format to be used for software rendering. +/// +/// A single pixel always stored as a POT number of bytes. (so in practice +/// either 1, 2, 4, 8, 16 bytes per pixel) +/// +/// There are two kinds of pixel formats: +/// - formats where all components are 8 bits, called array formats +/// The component order as specified in the pixel format name is the +/// order of the components' bytes in memory, with the leftmost component +/// occupying the lowest memory address. +/// +/// - all other formats are called packed formats, and the component order +/// as specified in the format name refers to the order in the native type. +/// for example, for kFlutterSoftwarePixelFormatRGB565, the R component +/// uses the 5 least significant bits of the uint16_t pixel value. +/// +/// Each pixel format in this list is documented with an example on how to get +/// the color components from the pixel. +/// - for packed formats, p is the pixel value as a word. For example, you can +/// get the pixel value for a RGB565 formatted buffer like this: +/// uint16_t p = ((const uint16_t*) allocation)[row_bytes * y / bpp + x]; +/// (with bpp being the bytes per pixel, so 2 for RGB565) +/// +/// - for array formats, p is a pointer to the pixel value. For example, you +/// can get the p for a RGBA8888 formatted buffer like this: +/// const uint8_t *p = ((const uint8_t*) allocation) + row_bytes*y + x*4; +typedef enum { + /// pixel with 8 bit grayscale value. + /// The grayscale value is the luma value calculated from r, g, b + /// according to BT.709. (gray = r*0.2126 + g*0.7152 + b*0.0722) + kFlutterSoftwarePixelFormatGray8, + + /// pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word. + /// r = p & 0x3F; g = (p>>5) & 0x3F; b = p>>11; + kFlutterSoftwarePixelFormatRGB565, + + /// pixel with 4 bits for alpha, red, green, blue; in 16-bit word. + /// r = p & 0xF; g = (p>>4) & 0xF; b = (p>>8) & 0xF; a = p>>12; + kFlutterSoftwarePixelFormatRGBA4444, + + /// pixel with 8 bits for red, green, blue, alpha. + /// r = p[0]; g = p[1]; b = p[2]; a = p[3]; + kFlutterSoftwarePixelFormatRGBA8888, + + /// pixel with 8 bits for red, green and blue and 8 unused bits. + /// r = p[0]; g = p[1]; b = p[2]; + kFlutterSoftwarePixelFormatRGBX8888, + + /// pixel with 8 bits for blue, green, red and alpha. + /// r = p[2]; g = p[1]; b = p[0]; a = p[3]; + kFlutterSoftwarePixelFormatBGRA8888, + + /// either kFlutterSoftwarePixelFormatBGRA8888 or + /// kFlutterSoftwarePixelFormatRGBA8888 depending on CPU endianess and OS + kFlutterSoftwarePixelFormatNative32, +} FlutterSoftwarePixelFormat; + typedef struct { /// Target texture of the active texture unit (example GL_TEXTURE_2D or /// GL_TEXTURE_RECTANGLE). @@ -283,9 +390,14 @@ typedef struct { } FlutterOpenGLTexture; typedef struct { - /// The target of the color attachment of the frame-buffer. For example, - /// GL_TEXTURE_2D or GL_RENDERBUFFER. In case of ambiguity when dealing with - /// Window bound frame-buffers, 0 may be used. + /// The format of the color attachment of the frame-buffer. For example, + /// GL_RGBA8. + /// + /// In case of ambiguity when dealing with Window bound frame-buffers, 0 may + /// be used. + /// + /// @bug This field is incorrectly named as "target" when it actually + /// refers to a format. uint32_t target; /// The name of the framebuffer. @@ -299,6 +411,62 @@ typedef struct { VoidCallback destruction_callback; } FlutterOpenGLFramebuffer; +typedef bool (*FlutterOpenGLSurfaceCallback)(void* /* user data */, + bool* /* opengl state changed */); + +typedef struct { + /// The size of this struct. Must be sizeof(FlutterOpenGLSurface). + size_t struct_size; + + /// User data to be passed to the make_current, clear_current and + /// destruction callbacks. + void* user_data; + + /// Callback invoked (on an engine-managed thread) that asks the embedder to + /// make the surface current. + /// + /// Should return true if the operation succeeded, false if the surface could + /// not be made current and rendering should be cancelled. + /// + /// The second parameter 'opengl state changed' should be set to true if + /// any OpenGL API state is different than before this callback was called. + /// In that case, Flutter will invalidate the internal OpenGL API state cache, + /// which is a somewhat expensive operation. + /// + /// @attention required. (non-null) + FlutterOpenGLSurfaceCallback make_current_callback; + + /// Callback invoked (on an engine-managed thread) when the current surface + /// can be cleared. + /// + /// Should return true if the operation succeeded, false if an error ocurred. + /// That error will be logged but otherwise not handled by the engine. + /// + /// The second parameter 'opengl state changed' is the same as with the + /// @ref make_current_callback. + /// + /// The embedder might clear the surface here after it was previously made + /// current. That's not required however, it's also possible to clear it in + /// the destruction callback. There's no way to signal OpenGL state + /// changes in the destruction callback though. + /// + /// @attention required. (non-null) + FlutterOpenGLSurfaceCallback clear_current_callback; + + /// Callback invoked (on an engine-managed thread) that asks the embedder to + /// collect the surface. + /// + /// @attention required. (non-null) + VoidCallback destruction_callback; + + /// The surface format. + /// + /// Allowed values: + /// - GL_RGBA8 + /// - GL_BGRA8_EXT + uint32_t format; +} FlutterOpenGLSurface; + typedef bool (*BoolCallback)(void* /* user data */); typedef FlutterTransformation (*TransformationCallback)(void* /* user data */); typedef uint32_t (*UIntCallback)(void* /* user data */); @@ -313,6 +481,7 @@ typedef bool (*TextureFrameCallback)(void* /* user data */, size_t /* height */, FlutterOpenGLTexture* /* texture out */); typedef void (*VsyncCallback)(void* /* user data */, intptr_t /* baton */); +typedef void (*OnPreEngineRestartCallback)(void* /* user data */); /// A structure to represent the width and height. typedef struct { @@ -351,10 +520,22 @@ typedef struct { FlutterSize lower_left_corner_radius; } FlutterRoundedRect; +/// A structure to represent a damage region. +typedef struct { + /// The size of this struct. Must be sizeof(FlutterDamage). + size_t struct_size; + /// The number of rectangles within the damage region. + size_t num_rects; + /// The actual damage region(s) in question. + FlutterRect* damage; +} FlutterDamage; + /// This information is passed to the embedder when requesting a frame buffer /// object. /// -/// See: \ref FlutterOpenGLRendererConfig.fbo_with_frame_info_callback. +/// See: \ref FlutterOpenGLRendererConfig.fbo_with_frame_info_callback, +/// \ref FlutterMetalRendererConfig.get_next_drawable_callback, +/// and \ref FlutterVulkanRendererConfig.get_next_image_callback. typedef struct { /// The size of this struct. Must be sizeof(FlutterFrameInfo). size_t struct_size; @@ -367,14 +548,25 @@ typedef uint32_t (*UIntFrameInfoCallback)( void* /* user data */, const FlutterFrameInfo* /* frame info */); +/// Callback for when a frame buffer object is requested with necessary +/// information for partial repaint. +typedef void (*FlutterFrameBufferWithDamageCallback)( + void* /* user data */, + const intptr_t /* fbo id */, + FlutterDamage* /* existing damage */); + /// This information is passed to the embedder when a surface is presented. /// /// See: \ref FlutterOpenGLRendererConfig.present_with_info. typedef struct { - /// The size of this struct. Must be sizeof(FlutterFrameInfo). + /// The size of this struct. Must be sizeof(FlutterPresentInfo). size_t struct_size; /// Id of the fbo backing the surface that was presented. uint32_t fbo_id; + /// Damage representing the area that the compositor needs to render. + FlutterDamage frame_damage; + /// Damage used to set the buffer's damage region. + FlutterDamage buffer_damage; } FlutterPresentInfo; /// Callback for when a surface is presented. @@ -389,7 +581,10 @@ typedef struct { BoolCallback clear_current; /// Specifying one (and only one) of `present` or `present_with_info` is /// required. Specifying both is an error and engine initialization will be - /// terminated. The return value indicates success of the present call. + /// terminated. The return value indicates success of the present call. If + /// the intent is to use dirty region management, present_with_info must be + /// defined as present will not succeed in communicating information about + /// damage. BoolCallback present; /// Specifying one (and only one) of the `fbo_callback` or /// `fbo_with_frame_info_callback` is required. Specifying both is an error @@ -438,8 +633,27 @@ typedef struct { /// required. Specifying both is an error and engine initialization will be /// terminated. When using this variant, the embedder is passed a /// `FlutterPresentInfo` struct that the embedder can use to release any - /// resources. The return value indicates success of the present call. + /// resources. The return value indicates success of the present call. This + /// callback is essential for dirty region management. If not defined, all the + /// pixels on the screen will be rendered at every frame (regardless of + /// whether damage is actually being computed or not). This is because the + /// information that is passed along to the callback contains the frame and + /// buffer damage that are essential for dirty region management. BoolPresentInfoCallback present_with_info; + /// Specifying this callback is a requirement for dirty region management. + /// Dirty region management will only render the areas of the screen that have + /// changed in between frames, greatly reducing rendering times and energy + /// consumption. To take advantage of these benefits, it is necessary to + /// define populate_existing_damage as a callback that takes user + /// data, an FBO ID, and an existing damage FlutterDamage. The callback should + /// use the given FBO ID to identify the FBO's exisiting damage (i.e. areas + /// that have changed since the FBO was last used) and use it to populate the + /// given existing damage variable. This callback is dependent on either + /// fbo_callback or fbo_with_frame_info_callback being defined as they are + /// responsible for providing populate_existing_damage with the FBO's + /// ID. Not specifying populate_existing_damage will result in full + /// repaint (i.e. rendering all the pixels on the screen at every frame). + FlutterFrameBufferWithDamageCallback populate_existing_damage; } FlutterOpenGLRendererConfig; /// Alias for id. @@ -457,6 +671,12 @@ typedef enum { kRGBA, } FlutterMetalExternalTexturePixelFormat; +/// YUV color space for the YUV external texture. +typedef enum { + kBT601FullRange, + kBT601LimitedRange, +} FlutterMetalExternalTextureYUVColorSpace; + typedef struct { /// The size of this struct. Must be sizeof(FlutterMetalExternalTexture). size_t struct_size; @@ -477,6 +697,8 @@ typedef struct { /// `FlutterEngineUnregisterExternalTexture`, the embedder has to release /// these textures. FlutterMetalTextureHandle* textures; + /// The YUV color space of the YUV external texture. + FlutterMetalExternalTextureYUVColorSpace yuv_color_space; } FlutterMetalExternalTexture; /// Callback to provide an external texture for a given texture_id. @@ -493,11 +715,13 @@ typedef struct { size_t struct_size; /// Embedder provided unique identifier to the texture buffer. Given that the /// `texture` handle is passed to the engine to render to, the texture buffer - /// is itseld owned by the embedder. This `texture_id` is then also given to + /// is itself owned by the embedder. This `texture_id` is then also given to /// the embedder in the present callback. int64_t texture_id; /// Handle to the MTLTexture that is owned by the embedder. Engine will render /// the frame into this texture. + /// + /// A NULL texture is considered invalid. FlutterMetalTextureHandle texture; /// A baton that is not interpreted by the engine in any way. It will be given /// back to the embedder in the destruction callback below. Embedder resources @@ -529,9 +753,13 @@ typedef struct { FlutterMetalCommandQueueHandle present_command_queue; /// The callback that gets invoked when the engine requests the embedder for a /// texture to render to. + /// + /// Not used if a FlutterCompositor is supplied in FlutterProjectArgs. FlutterMetalTextureCallback get_next_drawable_callback; /// The callback presented to the embedder to present a fully populated metal /// texture to the user. + /// + /// Not used if a FlutterCompositor is supplied in FlutterProjectArgs. FlutterMetalPresentCallback present_drawable_callback; /// When the embedder specifies that a texture has a frame available, the /// engine will call this method (on an internal engine managed thread) so @@ -540,6 +768,115 @@ typedef struct { FlutterMetalTextureFrameCallback external_texture_frame_callback; } FlutterMetalRendererConfig; +/// Alias for VkInstance. +typedef void* FlutterVulkanInstanceHandle; + +/// Alias for VkPhysicalDevice. +typedef void* FlutterVulkanPhysicalDeviceHandle; + +/// Alias for VkDevice. +typedef void* FlutterVulkanDeviceHandle; + +/// Alias for VkQueue. +typedef void* FlutterVulkanQueueHandle; + +/// Alias for VkImage. +typedef uint64_t FlutterVulkanImageHandle; + +typedef struct { + /// The size of this struct. Must be sizeof(FlutterVulkanImage). + size_t struct_size; + /// Handle to the VkImage that is owned by the embedder. The engine will + /// bind this image for writing the frame. + FlutterVulkanImageHandle image; + /// The VkFormat of the image (for example: VK_FORMAT_R8G8B8A8_UNORM). + uint32_t format; +} FlutterVulkanImage; + +/// Callback to fetch a Vulkan function pointer for a given instance. Normally, +/// this should return the results of vkGetInstanceProcAddr. +typedef void* (*FlutterVulkanInstanceProcAddressCallback)( + void* /* user data */, + FlutterVulkanInstanceHandle /* instance */, + const char* /* name */); + +/// Callback for when a VkImage is requested. +typedef FlutterVulkanImage (*FlutterVulkanImageCallback)( + void* /* user data */, + const FlutterFrameInfo* /* frame info */); + +/// Callback for when a VkImage has been written to and is ready for use by the +/// embedder. +typedef bool (*FlutterVulkanPresentCallback)( + void* /* user data */, + const FlutterVulkanImage* /* image */); + +typedef struct { + /// The size of this struct. Must be sizeof(FlutterVulkanRendererConfig). + size_t struct_size; + + /// The Vulkan API version. This should match the value set in + /// VkApplicationInfo::apiVersion when the VkInstance was created. + uint32_t version; + /// VkInstance handle. Must not be destroyed before `FlutterEngineShutdown` is + /// called. + FlutterVulkanInstanceHandle instance; + /// VkPhysicalDevice handle. + FlutterVulkanPhysicalDeviceHandle physical_device; + /// VkDevice handle. Must not be destroyed before `FlutterEngineShutdown` is + /// called. + FlutterVulkanDeviceHandle device; + /// The queue family index of the VkQueue supplied in the next field. + uint32_t queue_family_index; + /// VkQueue handle. + /// The queue should not be used without protection from a mutex to make sure + /// it is not used simultaneously with other threads. That mutex should match + /// the one injected via the |get_instance_proc_address_callback|. + /// There is a proposal to remove the need for the mutex at + /// https://github.com/flutter/flutter/issues/134573. + FlutterVulkanQueueHandle queue; + /// The number of instance extensions available for enumerating in the next + /// field. + size_t enabled_instance_extension_count; + /// Array of enabled instance extension names. This should match the names + /// passed to `VkInstanceCreateInfo.ppEnabledExtensionNames` when the instance + /// was created, but any subset of enabled instance extensions may be + /// specified. + /// This field is optional; `nullptr` may be specified. + /// This memory is only accessed during the call to FlutterEngineInitialize. + const char** enabled_instance_extensions; + /// The number of device extensions available for enumerating in the next + /// field. + size_t enabled_device_extension_count; + /// Array of enabled logical device extension names. This should match the + /// names passed to `VkDeviceCreateInfo.ppEnabledExtensionNames` when the + /// logical device was created, but any subset of enabled logical device + /// extensions may be specified. + /// This field is optional; `nullptr` may be specified. + /// This memory is only accessed during the call to FlutterEngineInitialize. + /// For example: VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME + const char** enabled_device_extensions; + /// The callback invoked when resolving Vulkan function pointers. + /// At a bare minimum this should be used to swap out any calls that operate + /// on vkQueue's for threadsafe variants that obtain locks for their duration. + /// The functions to swap out are "vkQueueSubmit" and "vkQueueWaitIdle". An + /// example of how to do that can be found in the test + /// "EmbedderTest.CanSwapOutVulkanCalls" unit-test in + /// //shell/platform/embedder/tests/embedder_vk_unittests.cc. + FlutterVulkanInstanceProcAddressCallback get_instance_proc_address_callback; + /// The callback invoked when the engine requests a VkImage from the embedder + /// for rendering the next frame. + /// Not used if a FlutterCompositor is supplied in FlutterProjectArgs. + FlutterVulkanImageCallback get_next_image_callback; + /// The callback invoked when a VkImage has been written to and is ready for + /// use by the embedder. Prior to calling this callback, the engine performs + /// a host sync, and so the VkImage can be used in a pipeline by the embedder + /// without any additional synchronization. + /// Not used if a FlutterCompositor is supplied in FlutterProjectArgs. + FlutterVulkanPresentCallback present_image_callback; + +} FlutterVulkanRendererConfig; + typedef struct { /// The size of this struct. Must be sizeof(FlutterSoftwareRendererConfig). size_t struct_size; @@ -556,9 +893,15 @@ typedef struct { FlutterOpenGLRendererConfig open_gl; FlutterSoftwareRendererConfig software; FlutterMetalRendererConfig metal; + FlutterVulkanRendererConfig vulkan; }; } FlutterRendererConfig; +/// Display refers to a graphics hardware system consisting of a framebuffer, +/// typically a monitor or a screen. This ID is unique per display and is +/// stable until the Flutter application restarts. +typedef uint64_t FlutterEngineDisplayId; + typedef struct { /// The size of this struct. Must be sizeof(FlutterWindowMetricsEvent). size_t struct_size; @@ -572,8 +915,116 @@ typedef struct { size_t left; /// Vertical physical location of the top of the window on the screen. size_t top; + /// Top inset of window. + double physical_view_inset_top; + /// Right inset of window. + double physical_view_inset_right; + /// Bottom inset of window. + double physical_view_inset_bottom; + /// Left inset of window. + double physical_view_inset_left; + /// The identifier of the display the view is rendering on. + FlutterEngineDisplayId display_id; + /// The view that this event is describing. + int64_t view_id; } FlutterWindowMetricsEvent; +typedef struct { + /// The size of this struct. + /// Must be sizeof(FlutterAddViewResult). + size_t struct_size; + + /// True if the add view operation succeeded. + bool added; + + /// The |FlutterAddViewInfo.user_data|. + void* user_data; +} FlutterAddViewResult; + +/// The callback invoked by the engine when the engine has attempted to add a +/// view. +/// +/// The |FlutterAddViewResult| is only guaranteed to be valid during this +/// callback. +typedef void (*FlutterAddViewCallback)(const FlutterAddViewResult* result); + +typedef struct { + /// The size of this struct. + /// Must be sizeof(FlutterAddViewInfo). + size_t struct_size; + + /// The identifier for the view to add. This must be unique. + FlutterViewId view_id; + + /// The view's properties. + /// + /// The metric's |view_id| must match this struct's |view_id|. + const FlutterWindowMetricsEvent* view_metrics; + + /// A baton that is not interpreted by the engine in any way. It will be given + /// back to the embedder in |add_view_callback|. Embedder resources may be + /// associated with this baton. + void* user_data; + + /// Called once the engine has attempted to add the view. This callback is + /// required. + /// + /// The embedder/app must not use the view until the callback is invoked with + /// an `added` value of `true`. + /// + /// This callback is invoked on an internal engine managed thread. Embedders + /// must re-thread if necessary. + FlutterAddViewCallback add_view_callback; +} FlutterAddViewInfo; + +typedef struct { + /// The size of this struct. + /// Must be sizeof(FlutterRemoveViewResult). + size_t struct_size; + + /// True if the remove view operation succeeded. + bool removed; + + /// The |FlutterRemoveViewInfo.user_data|. + void* user_data; +} FlutterRemoveViewResult; + +/// The callback invoked by the engine when the engine has attempted to remove +/// a view. +/// +/// The |FlutterRemoveViewResult| is only guaranteed to be valid during this +/// callback. +typedef void (*FlutterRemoveViewCallback)( + const FlutterRemoveViewResult* /* result */); + +typedef struct { + /// The size of this struct. + /// Must be sizeof(FlutterRemoveViewInfo). + size_t struct_size; + + /// The identifier for the view to remove. + /// + /// The implicit view cannot be removed if it is enabled. + FlutterViewId view_id; + + /// A baton that is not interpreted by the engine in any way. + /// It will be given back to the embedder in |remove_view_callback|. + /// Embedder resources may be associated with this baton. + void* user_data; + + /// Called once the engine has attempted to remove the view. + /// This callback is required. + /// + /// The embedder must not destroy the underlying surface until the callback is + /// invoked with a `removed` value of `true`. + /// + /// This callback is invoked on an internal engine managed thread. + /// Embedders must re-thread if necessary. + /// + /// The |result| argument will be deallocated when the callback returns. + FlutterRemoveViewCallback remove_view_callback; +} FlutterRemoveViewInfo; + /// The phase of the pointer event. typedef enum { kCancel, @@ -584,7 +1035,7 @@ typedef enum { /// any other buttons are still pressed when one button is released, that /// should be sent as a kMove rather than a kUp. kUp, - /// The pointer, which must have been been up, is now down. + /// The pointer, which must have been up, is now down. /// /// For touch, this means that the pointer has come into contact with the /// screen. For a mouse, it means a button is now pressed. Note that if any @@ -608,6 +1059,12 @@ typedef enum { kRemove, /// The pointer moved while up. kHover, + /// A pan/zoom started on this pointer. + kPanZoomStart, + /// The pan/zoom updated. + kPanZoomUpdate, + /// The pan/zoom ended. + kPanZoomEnd, } FlutterPointerPhase; /// The device type that created a pointer event. @@ -615,6 +1072,7 @@ typedef enum { kFlutterPointerDeviceKindMouse = 1, kFlutterPointerDeviceKindTouch, kFlutterPointerDeviceKindStylus, + kFlutterPointerDeviceKindTrackpad, } FlutterPointerDeviceKind; /// Flags for the `buttons` field of `FlutterPointerEvent` when `device_kind` @@ -633,6 +1091,8 @@ typedef enum { typedef enum { kFlutterPointerSignalKindNone, kFlutterPointerSignalKindScroll, + kFlutterPointerSignalKindScrollInertiaCancel, + kFlutterPointerSignalKindScale, } FlutterPointerSignalKind; typedef struct { @@ -663,6 +1123,16 @@ typedef struct { FlutterPointerDeviceKind device_kind; /// The buttons currently pressed, if any. int64_t buttons; + /// The x offset of the pan/zoom in physical pixels. + double pan_x; + /// The y offset of the pan/zoom in physical pixels. + double pan_y; + /// The scale of the pan/zoom, where 1.0 is the initial scale. + double scale; + /// The rotation of the pan/zoom in radians, where 0.0 is the initial angle. + double rotation; + /// The identifier of the view that received the pointer event. + FlutterViewId view_id; } FlutterPointerEvent; typedef enum { @@ -671,6 +1141,14 @@ typedef enum { kFlutterKeyEventTypeRepeat, } FlutterKeyEventType; +typedef enum { + kFlutterKeyEventDeviceTypeKeyboard = 1, + kFlutterKeyEventDeviceTypeDirectionalPad, + kFlutterKeyEventDeviceTypeGamepad, + kFlutterKeyEventDeviceTypeJoystick, + kFlutterKeyEventDeviceTypeHdmi, +} FlutterKeyEventDeviceType; + /// A structure to represent a key event. /// /// Sending `FlutterKeyEvent` via `FlutterEngineSendKeyEvent` results in a @@ -685,6 +1163,14 @@ typedef enum { /// released. /// * All events throughout a key press sequence shall have the same `physical` /// and `logical`. Having different `character`s is allowed. +/// +/// A `FlutterKeyEvent` with `physical` 0 and `logical` 0 is an empty event. +/// This is the only case either `physical` or `logical` can be 0. An empty +/// event must be sent if a key message should be converted to no +/// `FlutterKeyEvent`s, for example, when a key down message is received for a +/// key that has already been pressed according to the record. This is to ensure +/// some `FlutterKeyEvent` arrives at the framework before raw key message. +/// See https://github.com/flutter/flutter/issues/87230. typedef struct { /// The size of this struct. Must be sizeof(FlutterKeyEvent). size_t struct_size; @@ -698,11 +1184,17 @@ typedef struct { /// /// For the full definition and list of pre-defined physical keys, see /// `PhysicalKeyboardKey` from the framework. + /// + /// The only case that `physical` might be 0 is when this is an empty event. + /// See `FlutterKeyEvent` for introduction. uint64_t physical; /// The key ID for the logical key of this event. /// /// For the full definition and a list of pre-defined logical keys, see /// `LogicalKeyboardKey` from the framework. + /// + /// The only case that `logical` might be 0 is when this is an empty event. + /// See `FlutterKeyEvent` for introduction. uint64_t logical; /// Null-terminated character input from the event. Can be null. Ignored for /// up events. @@ -720,6 +1212,8 @@ typedef struct { /// An event being synthesized means that the `timestamp` might greatly /// deviate from the actual time when the event occurs physically. bool synthesized; + /// The source device for the key event. + FlutterKeyEventDeviceType device_type; } FlutterKeyEvent; typedef void (*FlutterKeyEventCallback)(bool /* handled */, @@ -758,16 +1252,73 @@ typedef void (*FlutterDataCallback)(const uint8_t* /* data */, typedef int64_t FlutterPlatformViewIdentifier; /// `FlutterSemanticsNode` ID used as a sentinel to signal the end of a batch of -/// semantics node updates. +/// semantics node updates. This is unused if using +/// `FlutterUpdateSemanticsCallback2`. FLUTTER_EXPORT extern const int32_t kFlutterSemanticsNodeIdBatchEnd; +// The enumeration of possible string attributes that affect how assistive +// technologies announce a string. +// +// See dart:ui's implementers of the StringAttribute abstract class. +typedef enum { + // Indicates the string should be announced character by character. + kSpellOut, + // Indicates the string should be announced using the specified locale. + kLocale, +} FlutterStringAttributeType; + +// Indicates the assistive technology should announce out the string character +// by character. +// +// See dart:ui's SpellOutStringAttribute. +typedef struct { + /// The size of this struct. Must be sizeof(FlutterSpellOutStringAttribute). + size_t struct_size; +} FlutterSpellOutStringAttribute; + +// Indicates the assistive technology should announce the string using the +// specified locale. +// +// See dart:ui's LocaleStringAttribute. +typedef struct { + /// The size of this struct. Must be sizeof(FlutterLocaleStringAttribute). + size_t struct_size; + // The locale of this attribute. + const char* locale; +} FlutterLocaleStringAttribute; + +// Indicates how the assistive technology should treat the string. +// +// See dart:ui's StringAttribute. +typedef struct { + /// The size of this struct. Must be sizeof(FlutterStringAttribute). + size_t struct_size; + // The position this attribute starts. + size_t start; + // The next position after the attribute ends. + size_t end; + /// The type of the attribute described by the subsequent union. + FlutterStringAttributeType type; + union { + // Indicates the string should be announced character by character. + const FlutterSpellOutStringAttribute* spell_out; + // Indicates the string should be announced using the specified locale. + const FlutterLocaleStringAttribute* locale; + }; +} FlutterStringAttribute; + /// A node that represents some semantic data. /// /// The semantics tree is maintained during the semantics phase of the pipeline /// (i.e., during PipelineOwner.flushSemantics), which happens after /// compositing. Updates are then pushed to embedders via the registered -/// `FlutterUpdateSemanticsNodeCallback`. +/// `FlutterUpdateSemanticsCallback`. +/// +/// @deprecated Use `FlutterSemanticsNode2` instead. In order to preserve +/// ABI compatibility for existing users, no new fields will be +/// added to this struct. New fields will continue to be added +/// to `FlutterSemanticsNode2`. typedef struct { /// The size of this struct. Must be sizeof(FlutterSemanticsNode). size_t struct_size; @@ -809,8 +1360,8 @@ typedef struct { /// A value that `value` will have after a kFlutterSemanticsActionDecrease` /// action has been performed. const char* decreased_value; - /// The reading direction for `label`, `value`, `hint`, `increasedValue`, and - /// `decreasedValue`. + /// The reading direction for `label`, `value`, `hint`, `increasedValue`, + /// `decreasedValue`, and `tooltip`. FlutterTextDirection text_direction; /// The bounding box for this node in its coordinate system. FlutterRect rect; @@ -831,10 +1382,113 @@ typedef struct { /// Identifier of the platform view associated with this semantics node, or /// -1 if none. FlutterPlatformViewIdentifier platform_view_id; + /// A textual tooltip attached to the node. + const char* tooltip; } FlutterSemanticsNode; +/// A node in the Flutter semantics tree. +/// +/// The semantics tree is maintained during the semantics phase of the pipeline +/// (i.e., during PipelineOwner.flushSemantics), which happens after +/// compositing. Updates are then pushed to embedders via the registered +/// `FlutterUpdateSemanticsCallback2`. +/// +/// @see https://api.flutter.dev/flutter/semantics/SemanticsNode-class.html +typedef struct { + /// The size of this struct. Must be sizeof(FlutterSemanticsNode). + size_t struct_size; + /// The unique identifier for this node. + int32_t id; + /// The set of semantics flags associated with this node. + FlutterSemanticsFlag flags; + /// The set of semantics actions applicable to this node. + FlutterSemanticsAction actions; + /// The position at which the text selection originates. + int32_t text_selection_base; + /// The position at which the text selection terminates. + int32_t text_selection_extent; + /// The total number of scrollable children that contribute to semantics. + int32_t scroll_child_count; + /// The index of the first visible semantic child of a scroll node. + int32_t scroll_index; + /// The current scrolling position in logical pixels if the node is + /// scrollable. + double scroll_position; + /// The maximum in-range value for `scrollPosition` if the node is scrollable. + double scroll_extent_max; + /// The minimum in-range value for `scrollPosition` if the node is scrollable. + double scroll_extent_min; + /// The elevation along the z-axis at which the rect of this semantics node is + /// located above its parent. + double elevation; + /// Describes how much space the semantics node takes up along the z-axis. + double thickness; + /// A textual description of the node. + const char* label; + /// A brief description of the result of performing an action on the node. + const char* hint; + /// A textual description of the current value of the node. + const char* value; + /// A value that `value` will have after a kFlutterSemanticsActionIncrease` + /// action has been performed. + const char* increased_value; + /// A value that `value` will have after a kFlutterSemanticsActionDecrease` + /// action has been performed. + const char* decreased_value; + /// The reading direction for `label`, `value`, `hint`, `increasedValue`, + /// `decreasedValue`, and `tooltip`. + FlutterTextDirection text_direction; + /// The bounding box for this node in its coordinate system. + FlutterRect rect; + /// The transform from this node's coordinate system to its parent's + /// coordinate system. + FlutterTransformation transform; + /// The number of children this node has. + size_t child_count; + /// Array of child node IDs in traversal order. Has length `child_count`. + const int32_t* children_in_traversal_order; + /// Array of child node IDs in hit test order. Has length `child_count`. + const int32_t* children_in_hit_test_order; + /// The number of custom accessibility action associated with this node. + size_t custom_accessibility_actions_count; + /// Array of `FlutterSemanticsCustomAction` IDs associated with this node. + /// Has length `custom_accessibility_actions_count`. + const int32_t* custom_accessibility_actions; + /// Identifier of the platform view associated with this semantics node, or + /// -1 if none. + FlutterPlatformViewIdentifier platform_view_id; + /// A textual tooltip attached to the node. + const char* tooltip; + // The number of string attributes associated with the `label`. + size_t label_attribute_count; + // Array of string attributes associated with the `label`. + // Has length `label_attribute_count`. + const FlutterStringAttribute** label_attributes; + // The number of string attributes associated with the `hint`. + size_t hint_attribute_count; + // Array of string attributes associated with the `hint`. + // Has length `hint_attribute_count`. + const FlutterStringAttribute** hint_attributes; + // The number of string attributes associated with the `value`. + size_t value_attribute_count; + // Array of string attributes associated with the `value`. + // Has length `value_attribute_count`. + const FlutterStringAttribute** value_attributes; + // The number of string attributes associated with the `increased_value`. + size_t increased_value_attribute_count; + // Array of string attributes associated with the `increased_value`. + // Has length `increased_value_attribute_count`. + const FlutterStringAttribute** increased_value_attributes; + // The number of string attributes associated with the `decreased_value`. + size_t decreased_value_attribute_count; + // Array of string attributes associated with the `decreased_value`. + // Has length `decreased_value_attribute_count`. + const FlutterStringAttribute** decreased_value_attributes; +} FlutterSemanticsNode2; + /// `FlutterSemanticsCustomAction` ID used as a sentinel to signal the end of a -/// batch of semantics custom action updates. +/// batch of semantics custom action updates. This is unused if using +/// `FlutterUpdateSemanticsCallback2`. FLUTTER_EXPORT extern const int32_t kFlutterSemanticsCustomActionIdBatchEnd; @@ -847,6 +1501,11 @@ extern const int32_t kFlutterSemanticsCustomActionIdBatchEnd; /// Action overrides are custom actions that the application developer requests /// to be used in place of the standard actions in the `FlutterSemanticsAction` /// enum. +/// +/// @deprecated Use `FlutterSemanticsCustomAction2` instead. In order to +/// preserve ABI compatility for existing users, no new fields +/// will be added to this struct. New fields will continue to +/// be added to `FlutterSemanticsCustomAction2`. typedef struct { /// The size of the struct. Must be sizeof(FlutterSemanticsCustomAction). size_t struct_size; @@ -861,6 +1520,65 @@ typedef struct { const char* hint; } FlutterSemanticsCustomAction; +/// A custom semantics action, or action override. +/// +/// Custom actions can be registered by applications in order to provide +/// semantic actions other than the standard actions available through the +/// `FlutterSemanticsAction` enum. +/// +/// Action overrides are custom actions that the application developer requests +/// to be used in place of the standard actions in the `FlutterSemanticsAction` +/// enum. +/// +/// @see +/// https://api.flutter.dev/flutter/semantics/CustomSemanticsAction-class.html +typedef struct { + /// The size of the struct. Must be sizeof(FlutterSemanticsCustomAction). + size_t struct_size; + /// The unique custom action or action override ID. + int32_t id; + /// For overridden standard actions, corresponds to the + /// `FlutterSemanticsAction` to override. + FlutterSemanticsAction override_action; + /// The user-readable name of this custom semantics action. + const char* label; + /// The hint description of this custom semantics action. + const char* hint; +} FlutterSemanticsCustomAction2; + +/// A batch of updates to semantics nodes and custom actions. +/// +/// @deprecated Use `FlutterSemanticsUpdate2` instead. Adding members +/// to `FlutterSemanticsNode` or `FlutterSemanticsCustomAction` +/// breaks the ABI of this struct. +typedef struct { + /// The size of the struct. Must be sizeof(FlutterSemanticsUpdate). + size_t struct_size; + /// The number of semantics node updates. + size_t nodes_count; + // Array of semantics nodes. Has length `nodes_count`. + FlutterSemanticsNode* nodes; + /// The number of semantics custom action updates. + size_t custom_actions_count; + /// Array of semantics custom actions. Has length `custom_actions_count`. + FlutterSemanticsCustomAction* custom_actions; +} FlutterSemanticsUpdate; + +/// A batch of updates to semantics nodes and custom actions. +typedef struct { + /// The size of the struct. Must be sizeof(FlutterSemanticsUpdate2). + size_t struct_size; + /// The number of semantics node updates. + size_t node_count; + // Array of semantics node pointers. Has length `node_count`. + FlutterSemanticsNode2** nodes; + /// The number of semantics custom action updates. + size_t custom_action_count; + /// Array of semantics custom action pointers. Has length + /// `custom_action_count`. + FlutterSemanticsCustomAction2** custom_actions; +} FlutterSemanticsUpdate2; + typedef void (*FlutterUpdateSemanticsNodeCallback)( const FlutterSemanticsNode* /* semantics node */, void* /* user data */); @@ -869,6 +1587,28 @@ typedef void (*FlutterUpdateSemanticsCustomActionCallback)( const FlutterSemanticsCustomAction* /* semantics custom action */, void* /* user data */); +typedef void (*FlutterUpdateSemanticsCallback)( + const FlutterSemanticsUpdate* /* semantics update */, + void* /* user data*/); + +typedef void (*FlutterUpdateSemanticsCallback2)( + const FlutterSemanticsUpdate2* /* semantics update */, + void* /* user data*/); + +/// An update to whether a message channel has a listener set or not. +typedef struct { + /// The size of the struct. Must be sizeof(FlutterChannelUpdate). + size_t struct_size; + /// The name of the channel. + const char* channel; + /// True if a listener has been set, false if one has been cleared. + bool listening; +} FlutterChannelUpdate; + +typedef void (*FlutterChannelUpdateCallback)( + const FlutterChannelUpdate* /* channel update */, + void* /* user data */); + typedef struct _FlutterTaskRunner* FlutterTaskRunner; typedef struct { @@ -923,6 +1663,9 @@ typedef struct { /// and platform task runners. This makes the Flutter engine use the same /// thread for both task runners. const FlutterTaskRunnerDescription* render_task_runner; + /// Specify a callback that is used to set the thread priority for embedder + /// task runners. + void (*thread_priority_setter)(FlutterThreadPriority); } FlutterCustomTaskRunners; typedef struct { @@ -935,6 +1678,9 @@ typedef struct { /// A framebuffer for Flutter to render into. The embedder must ensure that /// the framebuffer is complete. FlutterOpenGLFramebuffer framebuffer; + /// A surface for Flutter to render into. Basically a wrapper around + /// a closure that'll be called when the surface should be made current. + FlutterOpenGLSurface surface; }; } FlutterOpenGLBackingStore; @@ -955,6 +1701,28 @@ typedef struct { VoidCallback destruction_callback; } FlutterSoftwareBackingStore; +typedef struct { + /// The size of this struct. Must be sizeof(FlutterSoftwareBackingStore2). + size_t struct_size; + /// A pointer to the raw bytes of the allocation described by this software + /// backing store. + const void* allocation; + /// The number of bytes in a single row of the allocation. + size_t row_bytes; + /// The number of rows in the allocation. + size_t height; + /// A baton that is not interpreted by the engine in any way. It will be given + /// back to the embedder in the destruction callback below. Embedder resources + /// may be associated with this baton. + void* user_data; + /// The callback invoked by the engine when it no longer needs this backing + /// store. + VoidCallback destruction_callback; + /// The pixel format that the engine should use to render into the allocation. + /// In most cases, kR + FlutterSoftwarePixelFormat pixel_format; +} FlutterSoftwareBackingStore2; + typedef struct { /// The size of this struct. Must be sizeof(FlutterMetalBackingStore). size_t struct_size; @@ -966,6 +1734,25 @@ typedef struct { }; } FlutterMetalBackingStore; +typedef struct { + /// The size of this struct. Must be sizeof(FlutterVulkanBackingStore). + size_t struct_size; + /// The image that the layer will be rendered to. This image must already be + /// available for the engine to bind for writing when it's given to the engine + /// via the backing store creation callback. The engine will perform a host + /// sync for all layers prior to calling the compositor present callback, and + /// so the written layer images can be freely bound by the embedder without + /// any additional synchronization. + const FlutterVulkanImage* image; + /// A baton that is not interpreted by the engine in any way. It will be given + /// back to the embedder in the destruction callback below. Embedder resources + /// may be associated with this baton. + void* user_data; + /// The callback invoked by the engine when it no longer needs this backing + /// store. + VoidCallback destruction_callback; +} FlutterVulkanBackingStore; + typedef enum { /// Indicates that the Flutter application requested that an opacity be /// applied to the platform view. @@ -1025,6 +1812,11 @@ typedef enum { kFlutterBackingStoreTypeSoftware, /// Specifies a Metal backing store. This is backed by a Metal texture. kFlutterBackingStoreTypeMetal, + /// Specifies a Vulkan backing store. This is backed by a Vulkan VkImage. + kFlutterBackingStoreTypeVulkan, + /// Specifies a allocation that the engine should render into using + /// software rendering. + kFlutterBackingStoreTypeSoftware2, } FlutterBackingStoreType; typedef struct { @@ -1044,8 +1836,12 @@ typedef struct { FlutterOpenGLBackingStore open_gl; /// The description of the software backing store. FlutterSoftwareBackingStore software; + /// The description of the software backing store. + FlutterSoftwareBackingStore2 software2; // The description of the Metal backing store. FlutterMetalBackingStore metal; + // The description of the Vulkan backing store. + FlutterVulkanBackingStore vulkan; }; } FlutterBackingStore; @@ -1054,6 +1850,9 @@ typedef struct { size_t struct_size; /// The size of the render target the engine expects to render into. FlutterSize size; + /// The identifier for the view that the engine will use this backing store to + /// render into. + FlutterViewId view_id; } FlutterBackingStoreConfig; typedef enum { @@ -1064,6 +1863,28 @@ typedef enum { kFlutterLayerContentTypePlatformView, } FlutterLayerContentType; +/// A region represented by a collection of non-overlapping rectangles. +typedef struct { + /// The size of this struct. Must be sizeof(FlutterRegion). + size_t struct_size; + /// Number of rectangles in the region. + size_t rects_count; + /// The rectangles that make up the region. + FlutterRect* rects; +} FlutterRegion; + +/// Contains additional information about the backing store provided +/// during presentation to the embedder. +typedef struct { + /// The size of this struct. Must be sizeof(FlutterBackingStorePresentInfo). + size_t struct_size; + + /// The area of the backing store that contains Flutter contents. Pixels + /// outside of this area are transparent and the embedder may choose not + /// to render them. Coordinates are in physical pixels. + FlutterRegion* paint_region; +} FlutterBackingStorePresentInfo; + typedef struct { /// This size of this struct. Must be sizeof(FlutterLayer). size_t struct_size; @@ -1083,8 +1904,34 @@ typedef struct { FlutterPoint offset; /// The size of the layer (in physical pixels). FlutterSize size; + + /// Extra information for the backing store that the embedder may + /// use during presentation. + FlutterBackingStorePresentInfo* backing_store_present_info; + + // Time in nanoseconds at which this frame is scheduled to be presented. 0 if + // not known. See FlutterEngineGetCurrentTime(). + uint64_t presentation_time; } FlutterLayer; +typedef struct { + /// The size of this struct. + /// Must be sizeof(FlutterPresentViewInfo). + size_t struct_size; + + /// The identifier of the target view. + FlutterViewId view_id; + + /// The layers that should be composited onto the view. + const FlutterLayer** layers; + + /// The count of layers. + size_t layers_count; + + /// The |FlutterCompositor.user_data|. + void* user_data; +} FlutterPresentViewInfo; + typedef bool (*FlutterBackingStoreCreateCallback)( const FlutterBackingStoreConfig* config, FlutterBackingStore* backing_store_out, @@ -1098,13 +1945,20 @@ typedef bool (*FlutterLayersPresentCallback)(const FlutterLayer** layers, size_t layers_count, void* user_data); +/// The callback invoked when the embedder should present to a view. +/// +/// The |FlutterPresentViewInfo| will be deallocated once the callback returns. +typedef bool (*FlutterPresentViewCallback)( + const FlutterPresentViewInfo* /* present info */); + typedef struct { /// This size of this struct. Must be sizeof(FlutterCompositor). size_t struct_size; /// A baton that in not interpreted by the engine in any way. If it passed /// back to the embedder in `FlutterCompositor.create_backing_store_callback`, - /// `FlutterCompositor.collect_backing_store_callback` and - /// `FlutterCompositor.present_layers_callback` + /// `FlutterCompositor.collect_backing_store_callback`, + /// `FlutterCompositor.present_layers_callback`, and + /// `FlutterCompositor.present_view_callback`. void* user_data; /// A callback invoked by the engine to obtain a backing store for a specific /// `FlutterLayer`. @@ -1113,15 +1967,38 @@ typedef struct { /// `FlutterBackingStore::struct_size` when specifying a new backing store to /// the engine. This only matters if the embedder expects to be used with /// engines older than the version whose headers it used during compilation. + /// + /// The callback should return true if the operation was successful. FlutterBackingStoreCreateCallback create_backing_store_callback; /// A callback invoked by the engine to release the backing store. The /// embedder may collect any resources associated with the backing store. + /// + /// The callback should return true if the operation was successful. FlutterBackingStoreCollectCallback collect_backing_store_callback; /// Callback invoked by the engine to composite the contents of each layer - /// onto the screen. + /// onto the implicit view. + /// + /// DEPRECATED: Use `present_view_callback` to support multiple views. + /// If this callback is provided, `FlutterEngineAddView` and + /// `FlutterEngineRemoveView` should not be used. + /// + /// Only one of `present_layers_callback` and `present_view_callback` may be + /// provided. Providing both is an error and engine initialization will + /// terminate. + /// + /// The callback should return true if the operation was successful. FlutterLayersPresentCallback present_layers_callback; /// Avoid caching backing stores provided by this compositor. bool avoid_backing_store_cache; + /// Callback invoked by the engine to composite the contents of each layer + /// onto the specified view. + /// + /// Only one of `present_layers_callback` and `present_view_callback` may be + /// provided. Providing both is an error and engine initialization will + /// terminate. + /// + /// The callback should return true if the operation was successful. + FlutterPresentViewCallback present_view_callback; } FlutterCompositor; typedef struct { @@ -1160,13 +2037,8 @@ typedef const FlutterLocale* (*FlutterComputePlatformResolvedLocaleCallback)( const FlutterLocale** /* supported_locales*/, size_t /* Number of locales*/); -/// Display refers to a graphics hardware system consisting of a framebuffer, -/// typically a monitor or a screen. This ID is unique per display and is -/// stable until the Flutter application restarts. -typedef uint64_t FlutterEngineDisplayId; - typedef struct { - /// This size of this struct. Must be sizeof(FlutterDisplay). + /// The size of this struct. Must be sizeof(FlutterEngineDisplay). size_t struct_size; FlutterEngineDisplayId display_id; @@ -1180,6 +2052,16 @@ typedef struct { /// This represents the refresh period in frames per second. This value may be /// zero if the device is not running or unavailable or unknown. double refresh_rate; + + /// The width of the display, in physical pixels. + size_t width; + + /// The height of the display, in physical pixels. + size_t height; + + /// The pixel ratio of the display, which is used to convert physical pixels + /// to logical pixels. + double device_pixel_ratio; } FlutterEngineDisplay; /// The update type parameter that is passed to @@ -1372,9 +2254,10 @@ typedef struct { /// `switches.h` engine source file. const char* const* command_line_argv; /// The callback invoked by the engine in order to give the embedder the - /// chance to respond to platform messages from the Dart application. The - /// callback will be invoked on the thread on which the `FlutterEngineRun` - /// call is made. + /// chance to respond to platform messages from the Dart application. + /// The callback will be invoked on the thread on which the `FlutterEngineRun` + /// call is made. The second parameter, `user_data`, is supplied when + /// `FlutterEngineRun` or `FlutterEngineInitialize` is called. FlutterPlatformMessageCallback platform_message_callback; /// The VM snapshot data buffer used in AOT operation. This buffer must be /// mapped in as read-only. For more information refer to the documentation on @@ -1411,24 +2294,44 @@ typedef struct { /// The callback invoked by the engine in root isolate scope. Called /// immediately after the root isolate has been created and marked runnable. VoidCallback root_isolate_create_callback; - /// The callback invoked by the engine in order to give the embedder the - /// chance to respond to semantics node updates from the Dart application. + /// The legacy callback invoked by the engine in order to give the embedder + /// the chance to respond to semantics node updates from the Dart application. /// Semantics node updates are sent in batches terminated by a 'batch end' /// callback that is passed a sentinel `FlutterSemanticsNode` whose `id` field /// has the value `kFlutterSemanticsNodeIdBatchEnd`. /// /// The callback will be invoked on the thread on which the `FlutterEngineRun` /// call is made. + /// + /// @deprecated Use `update_semantics_callback2` instead. Only one of + /// `update_semantics_node_callback`, + /// `update_semantics_callback`, and + /// `update_semantics_callback2` may be provided; the others + /// should be set to null. + /// + /// This callback is incompatible with multiple views. If this + /// callback is provided, `FlutterEngineAddView` and + /// `FlutterEngineRemoveView` should not be used. FlutterUpdateSemanticsNodeCallback update_semantics_node_callback; - /// The callback invoked by the engine in order to give the embedder the - /// chance to respond to updates to semantics custom actions from the Dart - /// application. Custom action updates are sent in batches terminated by a + /// The legacy callback invoked by the engine in order to give the embedder + /// the chance to respond to updates to semantics custom actions from the Dart + /// application. Custom action updates are sent in batches terminated by a /// 'batch end' callback that is passed a sentinel /// `FlutterSemanticsCustomAction` whose `id` field has the value /// `kFlutterSemanticsCustomActionIdBatchEnd`. /// /// The callback will be invoked on the thread on which the `FlutterEngineRun` /// call is made. + /// + /// @deprecated Use `update_semantics_callback2` instead. Only one of + /// `update_semantics_node_callback`, + /// `update_semantics_callback`, and + /// `update_semantics_callback2` may be provided; the others + /// should be set to null. + /// + /// This callback is incompatible with multiple views. If this + /// callback is provided, `FlutterEngineAddView` and + /// `FlutterEngineRemoveView` should not be used. FlutterUpdateSemanticsCustomActionCallback update_semantics_custom_action_callback; /// Path to a directory used to store data that is cached across runs of a @@ -1555,10 +2458,57 @@ typedef struct { // or component name to embedder's logger. This string will be passed to to // callbacks on `log_message_callback`. Defaults to "flutter" if unspecified. const char* log_tag; + + // A callback that is invoked right before the engine is restarted. + // + // This optional callback is typically used to reset states to as if the + // engine has just been started, and usually indicates the user has requested + // a hot restart (Shift-R in the Flutter CLI.) It is not called the first time + // the engine starts. + // + // The first argument is the `user_data` from `FlutterEngineInitialize`. + OnPreEngineRestartCallback on_pre_engine_restart_callback; + + /// The callback invoked by the engine in order to give the embedder the + /// chance to respond to updates to semantics nodes and custom actions from + /// the Dart application. + /// + /// The callback will be invoked on the thread on which the `FlutterEngineRun` + /// call is made. + /// + /// @deprecated Use `update_semantics_callback2` instead. Only one of + /// `update_semantics_node_callback`, + /// `update_semantics_callback`, and + /// `update_semantics_callback2` may be provided; the others + /// must be set to null. + /// + /// This callback is incompatible with multiple views. If this + /// callback is provided, `FlutterEngineAddView` and + /// `FlutterEngineRemoveView` should not be used. + FlutterUpdateSemanticsCallback update_semantics_callback; + + /// The callback invoked by the engine in order to give the embedder the + /// chance to respond to updates to semantics nodes and custom actions from + /// the Dart application. + /// + /// The callback will be invoked on the thread on which the `FlutterEngineRun` + /// call is made. + /// + /// Only one of `update_semantics_node_callback`, `update_semantics_callback`, + /// and `update_semantics_callback2` may be provided; the others must be set + /// to null. + FlutterUpdateSemanticsCallback2 update_semantics_callback2; + + /// The callback invoked by the engine in response to a channel listener + /// being registered on the framework side. The callback is invoked from + /// a task posted to the platform thread. + FlutterChannelUpdateCallback channel_update_callback; } FlutterProjectArgs; #ifndef FLUTTER_ENGINE_NO_PROTOTYPES +// NOLINTBEGIN(google-objc-function-naming) + //------------------------------------------------------------------------------ /// @brief Creates the necessary data structures to launch a Flutter Dart /// application in AOT mode. The data may only be collected after @@ -1700,6 +2650,63 @@ FLUTTER_EXPORT FlutterEngineResult FlutterEngineRunInitialized( FLUTTER_API_SYMBOL(FlutterEngine) engine); +//------------------------------------------------------------------------------ +/// @brief Adds a view. +/// +/// This is an asynchronous operation. The view should not be used +/// until the |info.add_view_callback| is invoked with an |added| +/// value of true. The embedder should prepare resources in advance +/// but be ready to clean up on failure. +/// +/// A frame is scheduled if the operation succeeds. +/// +/// The callback is invoked on a thread managed by the engine. The +/// embedder should re-thread if needed. +/// +/// Attempting to add the implicit view will fail and will return +/// kInvalidArguments. Attempting to add a view with an already +/// existing view ID will fail, and |info.add_view_callback| will be +/// invoked with an |added| value of false. +/// +/// @param[in] engine A running engine instance. +/// @param[in] info The add view arguments. This can be deallocated +/// once |FlutterEngineAddView| returns, before +/// |add_view_callback| is invoked. +/// +/// @return The result of *starting* the asynchronous operation. If +/// `kSuccess`, the |add_view_callback| will be invoked. +FLUTTER_EXPORT +FlutterEngineResult FlutterEngineAddView(FLUTTER_API_SYMBOL(FlutterEngine) + engine, + const FlutterAddViewInfo* info); + +//------------------------------------------------------------------------------ +/// @brief Removes a view. +/// +/// This is an asynchronous operation. The view's resources must not +/// be cleaned up until |info.remove_view_callback| is invoked with +/// a |removed| value of true. +/// +/// The callback is invoked on a thread managed by the engine. The +/// embedder should re-thread if needed. +/// +/// Attempting to remove the implicit view will fail and will return +/// kInvalidArguments. Attempting to remove a view with a +/// non-existent view ID will fail, and |info.remove_view_callback| +/// will be invoked with a |removed| value of false. +/// +/// @param[in] engine A running engine instance. +/// @param[in] info The remove view arguments. This can be deallocated +/// once |FlutterEngineRemoveView| returns, before +/// |remove_view_callback| is invoked. +/// +/// @return The result of *starting* the asynchronous operation. If +/// `kSuccess`, the |remove_view_callback| will be invoked. +FLUTTER_EXPORT +FlutterEngineResult FlutterEngineRemoveView(FLUTTER_API_SYMBOL(FlutterEngine) + engine, + const FlutterRemoveViewInfo* info); + FLUTTER_EXPORT FlutterEngineResult FlutterEngineSendWindowMetricsEvent( FLUTTER_API_SYMBOL(FlutterEngine) engine, @@ -1896,8 +2903,8 @@ FlutterEngineResult FlutterEngineMarkExternalTextureFrameAvailable( /// @param[in] engine A running engine instance. /// @param[in] enabled When enabled, changes to the semantic contents of the /// window are sent via the -/// `FlutterUpdateSemanticsNodeCallback` registered to -/// `update_semantics_node_callback` in +/// `FlutterUpdateSemanticsCallback2` registered to +/// `update_semantics_callback2` in /// `FlutterProjectArgs`. /// /// @return The result of the call. @@ -1924,7 +2931,7 @@ FlutterEngineResult FlutterEngineUpdateAccessibilityFeatures( /// @brief Dispatch a semantics action to the specified semantics node. /// /// @param[in] engine A running engine instance. -/// @param[in] identifier The semantics action identifier. +/// @param[in] node_id The semantics node identifier. /// @param[in] action The semantics action. /// @param[in] data Data associated with the action. /// @param[in] data_length The data length. @@ -1934,7 +2941,7 @@ FlutterEngineResult FlutterEngineUpdateAccessibilityFeatures( FLUTTER_EXPORT FlutterEngineResult FlutterEngineDispatchSemanticsAction( FLUTTER_API_SYMBOL(FlutterEngine) engine, - uint64_t id, + uint64_t node_id, FlutterSemanticsAction action, const uint8_t* data, size_t data_length); @@ -2217,6 +3224,37 @@ FlutterEngineResult FlutterEngineNotifyDisplayUpdate( const FlutterEngineDisplay* displays, size_t display_count); +//------------------------------------------------------------------------------ +/// @brief Schedule a new frame to redraw the content. +/// +/// @param[in] engine A running engine instance. +/// +/// @return the result of the call made to the engine. +/// +FLUTTER_EXPORT +FlutterEngineResult FlutterEngineScheduleFrame(FLUTTER_API_SYMBOL(FlutterEngine) + engine); + +//------------------------------------------------------------------------------ +/// @brief Schedule a callback to be called after the next frame is drawn. +/// This must be called from the platform thread. The callback is +/// executed only once from the raster thread; embedders must +/// re-thread if necessary. Performing blocking calls +/// in this callback may introduce application jank. +/// +/// @param[in] engine A running engine instance. +/// @param[in] callback The callback to execute. +/// @param[in] user_data A baton passed by the engine to the callback. This +/// baton is not interpreted by the engine in any way. +/// +/// @return The result of the call. +/// +FLUTTER_EXPORT +FlutterEngineResult FlutterEngineSetNextFrameCallback( + FLUTTER_API_SYMBOL(FlutterEngine) engine, + VoidCallback callback, + void* user_data); + #endif // !FLUTTER_ENGINE_NO_PROTOTYPES // Typedefs for the function pointers in FlutterEngineProcTable. @@ -2333,6 +3371,18 @@ typedef FlutterEngineResult (*FlutterEngineNotifyDisplayUpdateFnPtr)( FlutterEngineDisplaysUpdateType update_type, const FlutterEngineDisplay* displays, size_t display_count); +typedef FlutterEngineResult (*FlutterEngineScheduleFrameFnPtr)( + FLUTTER_API_SYMBOL(FlutterEngine) engine); +typedef FlutterEngineResult (*FlutterEngineSetNextFrameCallbackFnPtr)( + FLUTTER_API_SYMBOL(FlutterEngine) engine, + VoidCallback callback, + void* user_data); +typedef FlutterEngineResult (*FlutterEngineAddViewFnPtr)( + FLUTTER_API_SYMBOL(FlutterEngine) engine, + const FlutterAddViewInfo* info); +typedef FlutterEngineResult (*FlutterEngineRemoveViewFnPtr)( + FLUTTER_API_SYMBOL(FlutterEngine) engine, + const FlutterRemoveViewInfo* info); /// Function-pointer-based versions of the APIs above. typedef struct { @@ -2377,6 +3427,10 @@ typedef struct { FlutterEnginePostCallbackOnAllNativeThreadsFnPtr PostCallbackOnAllNativeThreads; FlutterEngineNotifyDisplayUpdateFnPtr NotifyDisplayUpdate; + FlutterEngineScheduleFrameFnPtr ScheduleFrame; + FlutterEngineSetNextFrameCallbackFnPtr SetNextFrameCallback; + FlutterEngineAddViewFnPtr AddView; + FlutterEngineRemoveViewFnPtr RemoveView; } FlutterEngineProcTable; //------------------------------------------------------------------------------ @@ -2391,8 +3445,10 @@ FLUTTER_EXPORT FlutterEngineResult FlutterEngineGetProcAddresses( FlutterEngineProcTable* table); +// NOLINTEND(google-objc-function-naming) + #if defined(__cplusplus) } // extern "C" #endif -#endif // FLUTTER_EMBEDDER_H_ +#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_H_ diff --git a/src/flutter/shell/platform/embedder/embedder_struct_macros.h b/src/flutter/shell/platform/embedder/embedder_struct_macros.h new file mode 100644 index 00000000..483fd2ab --- /dev/null +++ b/src/flutter/shell/platform/embedder/embedder_struct_macros.h @@ -0,0 +1,32 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_STRUCT_MACROS_H_ +#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_STRUCT_MACROS_H_ + +#include + +// Checks if the given struct contains a member, whether set or not. +#define STRUCT_HAS_MEMBER(pointer, member) \ + ((offsetof(std::remove_pointer::type, member) + \ + sizeof(pointer->member) <= \ + pointer->struct_size)) + +#define SAFE_ACCESS(pointer, member, default_value) \ + ([=]() { \ + if (STRUCT_HAS_MEMBER(pointer, member)) { \ + return pointer->member; \ + } \ + return static_castmember)>((default_value)); \ + })() + +/// Checks if the member exists and is non-null. +#define SAFE_EXISTS(pointer, member) \ + (SAFE_ACCESS(pointer, member, nullptr) != nullptr) + +/// Checks if exactly one of member1 or member2 exists and is non-null. +#define SAFE_EXISTS_ONE_OF(pointer, member1, member2) \ + (SAFE_EXISTS(pointer, member1) != SAFE_EXISTS(pointer, member2)) + +#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_STRUCT_MACROS_H_ diff --git a/src/flutter/shell/platform/linux_embedded/external_texture.h b/src/flutter/shell/platform/linux_embedded/external_texture.h new file mode 100644 index 00000000..0a4ce93d --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/external_texture.h @@ -0,0 +1,64 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_EXTERNAL_TEXTURE_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_EXTERNAL_TEXTURE_H_ + +#include "flutter/shell/platform/embedder/embedder.h" + +#ifdef USE_GLES3 +#include +#else +#include +#include +#endif + +namespace flutter { + +typedef void (*glGenTexturesProc)(GLsizei n, GLuint* textures); +typedef void (*glDeleteTexturesProc)(GLsizei n, const GLuint* textures); +typedef void (*glBindTextureProc)(GLenum target, GLuint texture); +typedef void (*glTexParameteriProc)(GLenum target, GLenum pname, GLint param); +typedef void (*glTexImage2DProc)(GLenum target, + GLint level, + GLint internalformat, + GLsizei width, + GLsizei height, + GLint border, + GLenum format, + GLenum type, + const void* data); +typedef void (*glEGLImageTargetTexture2DOESProc)(GLenum target, + GLeglImageOES image); + +// A struct containing pointers to resolved gl* functions. +struct GlProcs { + glGenTexturesProc glGenTextures; + glDeleteTexturesProc glDeleteTextures; + glBindTextureProc glBindTexture; + glTexParameteriProc glTexParameteri; + glTexImage2DProc glTexImage2D; + glEGLImageTargetTexture2DOESProc glEGLImageTargetTexture2DOES; + bool valid; +}; + +// Abstract external texture. +class ExternalTexture { + public: + virtual ~ExternalTexture() = default; + + // Returns the unique id of this texture. + int64_t texture_id() const { return reinterpret_cast(this); }; + + // Attempts to populate the specified |opengl_texture| with texture details + // such as the name, width, height and the pixel format. + // Returns true on success. + virtual bool PopulateTexture(size_t width, + size_t height, + FlutterOpenGLTexture* opengl_texture) = 0; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EXTERNAL_TEXTURE_H_ diff --git a/src/flutter/shell/platform/linux_embedded/external_texture_egl_image.cc b/src/flutter/shell/platform/linux_embedded/external_texture_egl_image.cc new file mode 100644 index 00000000..843b2f98 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/external_texture_egl_image.cc @@ -0,0 +1,89 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/external_texture_egl_image.h" +#include +#include + +namespace flutter { + +struct ExternalTextureEGLImageState { + GLuint gl_texture = 0; +}; + +ExternalTextureEGLImage::ExternalTextureEGLImage( + FlutterDesktopEGLImageTextureCallback texture_callback, + void* user_data, + const GlProcs& gl_procs) + : state_(std::make_unique()), + texture_callback_(texture_callback), + user_data_(user_data), + gl_(gl_procs) {} + +ExternalTextureEGLImage::~ExternalTextureEGLImage() { + if (state_->gl_texture != 0) { + gl_.glDeleteTextures(1, &state_->gl_texture); + } +} + +bool ExternalTextureEGLImage::PopulateTexture( + size_t width, + size_t height, + FlutterOpenGLTexture* opengl_texture) { + if (!GetEGLImage(width, height, eglGetCurrentDisplay(), + eglGetCurrentContext())) { + return false; + } + + // Populate the texture object used by the engine. + opengl_texture->target = GL_TEXTURE_2D; + opengl_texture->name = state_->gl_texture; +#ifdef USE_GLES3 + opengl_texture->format = GL_RGBA8; +#else + opengl_texture->format = GL_RGBA8_OES; +#endif + opengl_texture->destruction_callback = nullptr; + opengl_texture->user_data = nullptr; + opengl_texture->width = width; + opengl_texture->height = height; + + return true; +} + +bool ExternalTextureEGLImage::GetEGLImage(size_t& width, + size_t& height, + void* egl_display, + void* egl_context) { + using namespace std; + + const FlutterDesktopEGLImage* egl_image = + texture_callback_(width, height, egl_display, egl_context, user_data_); + if (!egl_image || !egl_image->egl_image) { + return false; + } + width = egl_image->width; + height = egl_image->height; + + if (state_->gl_texture == 0) { + gl_.glGenTextures(1, &state_->gl_texture); + + gl_.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + } else { + gl_.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); + } + gl_.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, + (EGLImageKHR)egl_image->egl_image); + + if (egl_image->release_callback) { + egl_image->release_callback(egl_image->release_context); + } + return true; +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/external_texture_egl_image.h b/src/flutter/shell/platform/linux_embedded/external_texture_egl_image.h new file mode 100644 index 00000000..7dfaaeb4 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/external_texture_egl_image.h @@ -0,0 +1,54 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_EGL_IMAGE_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_EGL_IMAGE_H_ + +#include + +#include + +#include "flutter/shell/platform/common/public/flutter_texture_registrar.h" + +#include "flutter/shell/platform/linux_embedded/external_texture.h" + +namespace flutter { + +typedef struct ExternalTextureEGLImageState ExternalTextureEGLImageState; + +// An abstraction of an EGL Image based texture. +class ExternalTextureEGLImage : public ExternalTexture { + public: + ExternalTextureEGLImage( + FlutterDesktopEGLImageTextureCallback texture_callback, + void* user_data, + const GlProcs& gl_procs); + + virtual ~ExternalTextureEGLImage(); + + // |ExternalTexture| + bool PopulateTexture(size_t width, + size_t height, + FlutterOpenGLTexture* opengl_texture) override; + + private: + // Attempts to get the EGLImage returned by |texture_callback_| to + // OpenGL. + // The |width| and |height| will be set to the actual bounds of the EGLImage + // Returns true on success or false if the EGLImage returned + // by |texture_callback_| was invalid. + bool GetEGLImage(size_t& width, + size_t& height, + void* egl_display, + void* egl_context); + + std::unique_ptr state_; + FlutterDesktopEGLImageTextureCallback texture_callback_ = nullptr; + void* const user_data_ = nullptr; + const GlProcs& gl_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_EGL_IMAGE_H_ diff --git a/src/flutter/shell/platform/linux_embedded/external_texture_gl.cc b/src/flutter/shell/platform/linux_embedded/external_texture_gl.cc deleted file mode 100644 index 0b0935ea..00000000 --- a/src/flutter/shell/platform/linux_embedded/external_texture_gl.cc +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/linux_embedded/external_texture_gl.h" - -#include - -#ifdef USE_GLES3 -#include -#else -#include -#include -#endif - -namespace { - -typedef void (*glGenTexturesProc)(GLsizei n, GLuint* textures); -typedef void (*glDeleteTexturesProc)(GLsizei n, const GLuint* textures); -typedef void (*glBindTextureProc)(GLenum target, GLuint texture); -typedef void (*glTexParameteriProc)(GLenum target, GLenum pname, GLint param); -typedef void (*glTexImage2DProc)(GLenum target, - GLint level, - GLint internalformat, - GLsizei width, - GLsizei height, - GLint border, - GLenum format, - GLenum type, - const void* data); - -// A struct containing pointers to resolved gl* functions. -struct GlProcs { - glGenTexturesProc glGenTextures; - glDeleteTexturesProc glDeleteTextures; - glBindTextureProc glBindTexture; - glTexParameteriProc glTexParameteri; - glTexImage2DProc glTexImage2D; - bool valid; -}; - -static const GlProcs& GlProcs() { - static struct GlProcs procs = {}; - static bool initialized = false; - if (!initialized) { - procs.glGenTextures = - reinterpret_cast(eglGetProcAddress("glGenTextures")); - procs.glDeleteTextures = reinterpret_cast( - eglGetProcAddress("glDeleteTextures")); - procs.glBindTexture = - reinterpret_cast(eglGetProcAddress("glBindTexture")); - procs.glTexParameteri = reinterpret_cast( - eglGetProcAddress("glTexParameteri")); - procs.glTexImage2D = - reinterpret_cast(eglGetProcAddress("glTexImage2D")); - - procs.valid = procs.glGenTextures && procs.glDeleteTextures && - procs.glBindTexture && procs.glTexParameteri && - procs.glTexImage2D; - initialized = true; - } - return procs; -} - -} // namespace - -namespace flutter { - -struct ExternalTextureGLState { - GLuint gl_texture = 0; -}; - -ExternalTextureGL::ExternalTextureGL( - FlutterDesktopPixelBufferTextureCallback texture_callback, - void* user_data) - : state_(std::make_unique()), - texture_callback_(texture_callback), - user_data_(user_data) {} - -ExternalTextureGL::~ExternalTextureGL() { - const auto& gl = GlProcs(); - if (gl.valid && state_->gl_texture != 0) { - gl.glDeleteTextures(1, &state_->gl_texture); - } -} - -bool ExternalTextureGL::PopulateTexture(size_t width, - size_t height, - FlutterOpenGLTexture* opengl_texture) { - if (!CopyPixelBuffer(width, height)) { - return false; - } - - // Populate the texture object used by the engine. - opengl_texture->target = GL_TEXTURE_2D; - opengl_texture->name = state_->gl_texture; -#ifdef USE_GLES3 - opengl_texture->format = GL_RGBA8; -#else - opengl_texture->format = GL_RGBA8_OES; -#endif - opengl_texture->destruction_callback = nullptr; - opengl_texture->user_data = nullptr; - opengl_texture->width = width; - opengl_texture->height = height; - - return true; -} - -bool ExternalTextureGL::CopyPixelBuffer(size_t& width, size_t& height) { - const FlutterDesktopPixelBuffer* pixel_buffer = - texture_callback_(width, height, user_data_); - const auto& gl = GlProcs(); - if (!gl.valid || !pixel_buffer || !pixel_buffer->buffer) { - return false; - } - width = pixel_buffer->width; - height = pixel_buffer->height; - - if (state_->gl_texture == 0) { - gl.glGenTextures(1, &state_->gl_texture); - - gl.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); - -#ifdef USE_GLES3 - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); -#else - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, - GL_CLAMP_TO_BORDER_OES); - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, - GL_CLAMP_TO_BORDER_OES); -#endif - - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - } else { - gl.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); - } -#ifdef USE_GLES3 - gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, pixel_buffer->width, - pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, - pixel_buffer->buffer); -#else - gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width, - pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, - pixel_buffer->buffer); -#endif - return true; -} - -} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/external_texture_gl.h b/src/flutter/shell/platform/linux_embedded/external_texture_gl.h deleted file mode 100644 index 45819d6c..00000000 --- a/src/flutter/shell/platform/linux_embedded/external_texture_gl.h +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_GL_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_GL_H_ - -#include - -#include - -#include "flutter/shell/platform/common/public/flutter_texture_registrar.h" -#include "flutter/shell/platform/embedder/embedder.h" - -namespace flutter { - -typedef struct ExternalTextureGLState ExternalTextureGLState; - -// An abstraction of an OpenGL texture. -class ExternalTextureGL { - public: - ExternalTextureGL(FlutterDesktopPixelBufferTextureCallback texture_callback, - void* user_data); - - virtual ~ExternalTextureGL(); - - // Returns the unique id of this texture. - int64_t texture_id() { return reinterpret_cast(this); } - - void MarkFrameAvailable(); - - // Attempts to populate the specified |opengl_texture| with texture details - // such as the name, width, height and the pixel format upon successfully - // copying the buffer provided by |texture_callback_|. See |CopyPixelBuffer|. - // Returns true on success or false if the pixel buffer could not be copied. - bool PopulateTexture(size_t width, - size_t height, - FlutterOpenGLTexture* opengl_texture); - - private: - // Attempts to copy the pixel buffer returned by |texture_callback_| to - // OpenGL. - // The |width| and |height| will be set to the actual bounds of the copied - // pixel buffer. - // Returns true on success or false if the pixel buffer returned - // by |texture_callback_| was invalid. - bool CopyPixelBuffer(size_t& width, size_t& height); - - std::unique_ptr state_; - FlutterDesktopPixelBufferTextureCallback texture_callback_ = nullptr; - void* user_data_ = nullptr; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_GL_H_ diff --git a/src/flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.cc b/src/flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.cc new file mode 100644 index 00000000..1b962691 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.cc @@ -0,0 +1,82 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.h" + +namespace flutter { + +struct ExternalTexturePixelBufferState { + GLuint gl_texture = 0; +}; + +ExternalTexturePixelBuffer::ExternalTexturePixelBuffer( + FlutterDesktopPixelBufferTextureCallback texture_callback, + void* user_data, + const GlProcs& gl_procs) + : state_(std::make_unique()), + texture_callback_(texture_callback), + user_data_(user_data), + gl_(gl_procs) {} + +ExternalTexturePixelBuffer::~ExternalTexturePixelBuffer() { + if (state_->gl_texture != 0) { + gl_.glDeleteTextures(1, &state_->gl_texture); + } +} + +bool ExternalTexturePixelBuffer::PopulateTexture( + size_t width, + size_t height, + FlutterOpenGLTexture* opengl_texture) { + if (!CopyPixelBuffer(width, height)) { + return false; + } + + // Populate the texture object used by the engine. + opengl_texture->target = GL_TEXTURE_2D; + opengl_texture->name = state_->gl_texture; +#ifdef USE_GLES3 + opengl_texture->format = GL_RGBA8; +#else + opengl_texture->format = GL_RGBA8_OES; +#endif + opengl_texture->destruction_callback = nullptr; + opengl_texture->user_data = nullptr; + opengl_texture->width = width; + opengl_texture->height = height; + + return true; +} + +bool ExternalTexturePixelBuffer::CopyPixelBuffer(size_t& width, + size_t& height) { + const FlutterDesktopPixelBuffer* pixel_buffer = + texture_callback_(width, height, user_data_); + if (!pixel_buffer || !pixel_buffer->buffer) { + return false; + } + width = pixel_buffer->width; + height = pixel_buffer->height; + + if (state_->gl_texture == 0) { + gl_.glGenTextures(1, &state_->gl_texture); + + gl_.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + } else { + gl_.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); + } + gl_.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width, + pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, + pixel_buffer->buffer); + if (pixel_buffer->release_callback) { + pixel_buffer->release_callback(pixel_buffer->release_context); + } + return true; +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.h b/src/flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.h new file mode 100644 index 00000000..0d6dc67c --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.h @@ -0,0 +1,52 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_PIXELBUFFER_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_PIXELBUFFER_H_ + +#include + +#include + +#include "flutter/shell/platform/common/public/flutter_texture_registrar.h" + +#include "flutter/shell/platform/linux_embedded/external_texture.h" + +namespace flutter { + +typedef struct ExternalTexturePixelBufferState ExternalTexturePixelBufferState; + +// An abstraction of an pixel-buffer based texture. +class ExternalTexturePixelBuffer : public ExternalTexture { + public: + ExternalTexturePixelBuffer( + FlutterDesktopPixelBufferTextureCallback texture_callback, + void* user_data, + const GlProcs& gl_procs); + + virtual ~ExternalTexturePixelBuffer(); + + // |ExternalTexture| + bool PopulateTexture(size_t width, + size_t height, + FlutterOpenGLTexture* opengl_texture) override; + + private: + // Attempts to copy the pixel buffer returned by |texture_callback_| to + // OpenGL. + // The |width| and |height| will be set to the actual bounds of the copied + // pixel buffer. + // Returns true on success or false if the pixel buffer returned + // by |texture_callback_| was invalid. + bool CopyPixelBuffer(size_t& width, size_t& height); + + std::unique_ptr state_; + FlutterDesktopPixelBufferTextureCallback texture_callback_ = nullptr; + void* const user_data_ = nullptr; + const GlProcs& gl_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_EXTERNAL_TEXTURE_PIXELBUFFER_H_ diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes.cc b/src/flutter/shell/platform/linux_embedded/flutter_elinux.cc similarity index 54% rename from src/flutter/shell/platform/linux_embedded/flutter_linuxes.cc rename to src/flutter/shell/platform/linux_embedded/flutter_elinux.cc index 5f4edd9a..2d3587f3 100644 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes.cc +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/public/flutter_linuxes.h" +#include "flutter/shell/platform/linux_embedded/public/flutter_elinux.h" #include #include @@ -11,57 +11,56 @@ #include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h" #include "flutter/shell/platform/common/incoming_message_dispatcher.h" -#include "flutter/shell/platform/embedder/embedder.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_engine.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_state.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_view.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_engine.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_state.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_view.h" #include "flutter/shell/platform/linux_embedded/window_binding_handler.h" #if defined(DISPLAY_BACKEND_TYPE_DRM_GBM) -#include "flutter/shell/platform/linux_embedded/window/linuxes_window_drm.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window_drm.h" #include "flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.h" #elif defined(DISPLAY_BACKEND_TYPE_DRM_EGLSTREAM) -#include "flutter/shell/platform/linux_embedded/window/linuxes_window_drm.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window_drm.h" #include "flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.h" #elif defined(DISPLAY_BACKEND_TYPE_X11) -#include "flutter/shell/platform/linux_embedded/window/linuxes_window_x11.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window_x11.h" #else -#include "flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window_wayland.h" #endif static_assert(FLUTTER_ENGINE_VERSION == 1, ""); // Returns the engine corresponding to the given opaque API handle. -static flutter::FlutterLinuxesEngine* EngineFromHandle( +static flutter::FlutterELinuxEngine* EngineFromHandle( FlutterDesktopEngineRef ref) { - return reinterpret_cast(ref); + return reinterpret_cast(ref); } // Returns the opaque API handle for the given engine instance. static FlutterDesktopEngineRef HandleForEngine( - flutter::FlutterLinuxesEngine* engine) { + flutter::FlutterELinuxEngine* engine) { return reinterpret_cast(engine); } // Returns the view corresponding to the given opaque API handle. -static flutter::FlutterLinuxesView* ViewFromHandle(FlutterDesktopViewRef ref) { - return reinterpret_cast(ref); +static flutter::FlutterELinuxView* ViewFromHandle(FlutterDesktopViewRef ref) { + return reinterpret_cast(ref); } // Returns the opaque API handle for the given view instance. -static FlutterDesktopViewRef HandleForView(flutter::FlutterLinuxesView* view) { +static FlutterDesktopViewRef HandleForView(flutter::FlutterELinuxView* view) { return reinterpret_cast(view); } // Returns the texture registrar corresponding to the given opaque API handle. -static flutter::FlutterLinuxesTextureRegistrar* TextureRegistrarFromHandle( +static flutter::FlutterELinuxTextureRegistrar* TextureRegistrarFromHandle( FlutterDesktopTextureRegistrarRef ref) { - return reinterpret_cast(ref); + return reinterpret_cast(ref); } // Returns the opaque API handle for the given texture registrar instance. static FlutterDesktopTextureRegistrarRef HandleForTextureRegistrar( - flutter::FlutterLinuxesTextureRegistrar* registrar) { + flutter::FlutterELinuxTextureRegistrar* registrar) { return reinterpret_cast(registrar); } @@ -73,43 +72,38 @@ uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine) { } FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate( - const FlutterDesktopViewProperties& view_properties, + const FlutterDesktopViewProperties* view_properties, FlutterDesktopEngineRef engine) { std::unique_ptr window_wrapper = #if defined(DISPLAY_BACKEND_TYPE_DRM_GBM) - std::make_unique>( - view_properties.windw_display_mode, view_properties.width, - view_properties.height, view_properties.show_cursor); + std::make_unique>( + *view_properties); #elif defined(DISPLAY_BACKEND_TYPE_DRM_EGLSTREAM) std::make_unique< - flutter::LinuxesWindowDrm>( - view_properties.windw_display_mode, view_properties.width, - view_properties.height, view_properties.show_cursor); + flutter::ELinuxWindowDrm>( + *view_properties); #elif defined(DISPLAY_BACKEND_TYPE_X11) - std::make_unique( - view_properties.windw_display_mode, view_properties.width, - view_properties.height, view_properties.show_cursor); + std::make_unique(*view_properties); #else - std::make_unique( - view_properties.windw_display_mode, view_properties.width, - view_properties.height, view_properties.show_cursor); + std::make_unique(*view_properties); #endif auto state = std::make_unique(); state->view = - std::make_unique(std::move(window_wrapper)); - if (!state->view->CreateRenderSurface()) { - return nullptr; - } - + std::make_unique(std::move(window_wrapper)); // Take ownership of the engine, starting it if necessary. state->view->SetEngine( - std::unique_ptr(EngineFromHandle(engine))); + std::unique_ptr(EngineFromHandle(engine))); + state->view->CreateRenderSurface(); if (!state->view->GetEngine()->running()) { if (!state->view->GetEngine()->RunWithEntrypoint(nullptr)) { return nullptr; } + + const float text_scaling_factor = view_properties->text_scale_factor; + state->view->GetEngine()->SetSystemSettings( + text_scaling_factor, view_properties->enable_high_contrast); } // Must happen after engine is running. @@ -136,15 +130,19 @@ bool FlutterDesktopViewDispatchEvent(FlutterDesktopViewRef view) { return ViewFromHandle(view)->DispatchEvent(); } +int32_t FlutterDesktopViewGetFrameRate(FlutterDesktopViewRef view) { + return ViewFromHandle(view)->GetFrameRate(); +} + FlutterDesktopEngineRef FlutterDesktopEngineCreate( - const FlutterDesktopEngineProperties& engine_properties) { - flutter::FlutterProjectBundle project(engine_properties); - auto engine = std::make_unique(project); + const FlutterDesktopEngineProperties* engine_properties) { + flutter::FlutterProjectBundle project(*engine_properties); + auto engine = std::make_unique(project); return HandleForEngine(engine.release()); } bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref) { - flutter::FlutterLinuxesEngine* engine = EngineFromHandle(engine_ref); + flutter::FlutterELinuxEngine* engine = EngineFromHandle(engine_ref); bool result = true; if (engine->running()) { result = engine->Stop(); @@ -163,7 +161,8 @@ void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) { } FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar( - FlutterDesktopEngineRef engine, const char* plugin_name) { + FlutterDesktopEngineRef engine, + const char* plugin_name) { // Currently, one registrar acts as the registrar for all plugins, so the // name is ignored. It is part of the API to reduce churn in the future when // aligning more closely with the Flutter registrar system. @@ -200,36 +199,77 @@ void FlutterDesktopPluginRegistrarSetDestructionHandler( registrar->engine->SetPluginRegistrarDestructionCallback(callback); } -bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, - const char* channel, - const uint8_t* message, - const size_t message_size, - const FlutterDesktopBinaryReply reply, - void* user_data) { - return messenger->engine->SendPlatformMessage(channel, message, message_size, - reply, user_data); +bool FlutterDesktopMessengerSendWithReply( + FlutterDesktopMessengerRef messenger, + const char* channel, + const uint8_t* message, + const size_t message_size, + const FlutterDesktopBinaryReply reply, + void* user_data, + void (*cleanup)(void* captures_data)) { + // As we pass data to lambda and it's pointers we need to make sure that we + // send valid data + std::string channel_copy(channel); + std::vector message_copy(message, message + message_size); + + messenger->GetEngine()->task_runner()->PostTask([=]() { + if (!messenger->GetEngine()->SendPlatformMessage( + channel_copy.c_str(), message_copy.data(), message_copy.size(), + reply, user_data) && + user_data) { + cleanup(user_data); + } + }); + return true; } bool FlutterDesktopMessengerSend(FlutterDesktopMessengerRef messenger, - const char* channel, const uint8_t* message, + const char* channel, + const uint8_t* message, const size_t message_size) { - return FlutterDesktopMessengerSendWithReply(messenger, channel, message, - message_size, nullptr, nullptr); + return FlutterDesktopMessengerSendWithReply( + messenger, channel, message, message_size, nullptr, nullptr, nullptr); } void FlutterDesktopMessengerSendResponse( FlutterDesktopMessengerRef messenger, - const FlutterDesktopMessageResponseHandle* handle, const uint8_t* data, + const FlutterDesktopMessageResponseHandle* handle, + const uint8_t* data, size_t data_length) { - messenger->engine->SendPlatformMessageResponse(handle, data, data_length); + messenger->GetEngine()->SendPlatformMessageResponse(handle, data, + data_length); } void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger, const char* channel, FlutterDesktopMessageCallback callback, void* user_data) { - messenger->engine->message_dispatcher()->SetMessageCallback(channel, callback, - user_data); + messenger->GetEngine()->message_dispatcher()->SetMessageCallback( + channel, callback, user_data); +} + +FlutterDesktopMessengerRef FlutterDesktopMessengerAddRef( + FlutterDesktopMessengerRef messenger) { + messenger->AddRef(); + return messenger; +} + +void FlutterDesktopMessengerRelease(FlutterDesktopMessengerRef messenger) { + messenger->Release(); +} + +bool FlutterDesktopMessengerIsAvailable(FlutterDesktopMessengerRef messenger) { + return messenger->GetEngine() != nullptr; +} + +FlutterDesktopMessengerRef FlutterDesktopMessengerLock( + FlutterDesktopMessengerRef messenger) { + messenger->GetMutex().lock(); + return messenger; +} + +void FlutterDesktopMessengerUnlock(FlutterDesktopMessengerRef messenger) { + messenger->GetMutex().unlock(); } FlutterDesktopTextureRegistrarRef FlutterDesktopRegistrarGetTextureRegistrar( @@ -244,14 +284,31 @@ int64_t FlutterDesktopTextureRegistrarRegisterExternalTexture( ->RegisterTexture(texture_info); } -bool FlutterDesktopTextureRegistrarUnregisterExternalTexture( - FlutterDesktopTextureRegistrarRef texture_registrar, int64_t texture_id) { - return TextureRegistrarFromHandle(texture_registrar) - ->UnregisterTexture(texture_id); +void FlutterDesktopTextureRegistrarUnregisterExternalTexture( + FlutterDesktopTextureRegistrarRef texture_registrar, + int64_t texture_id, + void (*callback)(void* user_data), + void* user_data) { + auto registrar = TextureRegistrarFromHandle(texture_registrar); + if (callback) { + registrar->UnregisterTexture( + texture_id, [callback, user_data]() { callback(user_data); }); + return; + } + registrar->UnregisterTexture(texture_id); } bool FlutterDesktopTextureRegistrarMarkExternalTextureFrameAvailable( - FlutterDesktopTextureRegistrarRef texture_registrar, int64_t texture_id) { + FlutterDesktopTextureRegistrarRef texture_registrar, + int64_t texture_id) { return TextureRegistrarFromHandle(texture_registrar) ->MarkTextureFrameAvailable(texture_id); } + +void FlutterDesktopRegisterPlatformViewFactory( + FlutterDesktopPluginRegistrarRef registrar, + const char* view_type, + std::unique_ptr view_factory) { + registrar->engine->view()->RegisterPlatformViewFactory( + view_type, std::move(view_factory)); +} diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_engine.cc b/src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.cc similarity index 57% rename from src/flutter/shell/platform/linux_embedded/flutter_linuxes_engine.cc rename to src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.cc index 27547426..45a922e2 100644 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_engine.cc +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_engine.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_engine.h" #include @@ -12,7 +12,7 @@ #include "flutter/shell/platform/common/client_wrapper/binary_messenger_impl.h" #include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" #include "flutter/shell/platform/common/json_message_codec.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_view.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_view.h" #include "flutter/shell/platform/linux_embedded/logger.h" #include "flutter/shell/platform/linux_embedded/system_utils.h" #include "flutter/shell/platform/linux_embedded/task_runner.h" @@ -22,35 +22,55 @@ namespace flutter { namespace { // Creates and returns a FlutterRendererConfig that renders to the view (if any) -// of a FlutterLinuxesEngine, which should be the user_data received by the +// of a FlutterELinuxEngine, which should be the user_data received by the // render callbacks. FlutterRendererConfig GetRendererConfig() { FlutterRendererConfig config = {}; config.type = kOpenGL; config.open_gl.struct_size = sizeof(config.open_gl); config.open_gl.make_current = [](void* user_data) -> bool { - auto host = static_cast(user_data); + auto host = static_cast(user_data); if (!host->view()) { return false; } return host->view()->MakeCurrent(); }; config.open_gl.clear_current = [](void* user_data) -> bool { - auto host = static_cast(user_data); + auto host = static_cast(user_data); if (!host->view()) { return false; } return host->view()->ClearCurrent(); }; + config.open_gl.fbo_reset_after_present = true; +#if defined(USE_OPENGL_DIRTY_REGION_MANAGEMENT) + config.open_gl.present_with_info = + [](void* user_data, const FlutterPresentInfo* info) -> bool { + auto host = static_cast(user_data); + if (!host->view()) { + return false; + } + return host->view()->PresentWithInfo(info); + }; + config.open_gl.populate_existing_damage = + [](void* user_data, const intptr_t fbo_id, + FlutterDamage* existing_damage) -> void { + auto host = static_cast(user_data); + if (host->view()) { + host->view()->PopulateExistingDamage(fbo_id, existing_damage); + } + }; +#else config.open_gl.present = [](void* user_data) -> bool { - auto host = static_cast(user_data); + auto host = static_cast(user_data); if (!host->view()) { return false; } return host->view()->Present(); }; +#endif config.open_gl.fbo_callback = [](void* user_data) -> uint32_t { - auto host = static_cast(user_data); + auto host = static_cast(user_data); if (!host->view()) { return false; } @@ -58,14 +78,14 @@ FlutterRendererConfig GetRendererConfig() { }; config.open_gl.gl_proc_resolver = [](void* user_data, const char* name) -> void* { - auto host = static_cast(user_data); + auto host = static_cast(user_data); if (!host->view()) { return nullptr; } return host->view()->ProcResolver(name); }; config.open_gl.make_resource_current = [](void* user_data) -> bool { - auto host = static_cast(user_data); + auto host = static_cast(user_data); if (!host->view()) { return false; } @@ -74,13 +94,18 @@ FlutterRendererConfig GetRendererConfig() { config.open_gl.gl_external_texture_frame_callback = [](void* user_data, int64_t texture_id, size_t width, size_t height, FlutterOpenGLTexture* texture) -> bool { - auto host = static_cast(user_data); + auto host = static_cast(user_data); if (!host->texture_registrar()) { return false; } return host->texture_registrar()->PopulateTexture(texture_id, width, height, texture); }; + config.open_gl.surface_transformation = + [](void* user_data) -> FlutterTransformation { + auto host = static_cast(user_data); + return host->view()->GetRootSurfaceTransformation(); + }; return config; } @@ -98,7 +123,7 @@ static FlutterDesktopMessage ConvertToDesktopMessage( } // namespace -FlutterLinuxesEngine::FlutterLinuxesEngine(const FlutterProjectBundle& project) +FlutterELinuxEngine::FlutterELinuxEngine(const FlutterProjectBundle& project) : project_(std::make_unique(project)), aot_data_(nullptr) { embedder_api_.struct_size = sizeof(FlutterEngineProcTable); @@ -108,40 +133,51 @@ FlutterLinuxesEngine::FlutterLinuxesEngine(const FlutterProjectBundle& project) std::this_thread::get_id(), embedder_api_.GetCurrentTime, [this](const auto* task) { if (!engine_) { - LINUXES_LOG(ERROR) + ELINUX_LOG(ERROR) << "Cannot post an engine task when engine is not running."; return; } if (embedder_api_.RunTask(engine_, task) != kSuccess) { - LINUXES_LOG(ERROR) << "Failed to post an engine task."; + ELINUX_LOG(ERROR) << "Failed to post an engine task."; } }); + // Check for impeller support. + auto& switches = project_->GetSwitches(); + enable_impeller_ = std::find(switches.begin(), switches.end(), + "--enable-impeller=true") != switches.end(); + // Set up the legacy structs backing the API handles. - messenger_ = std::make_unique(); - messenger_->engine = this; + messenger_ = FlutterDesktopMessengerReferenceOwner( + FlutterDesktopMessengerAddRef(new FlutterDesktopMessenger()), + &FlutterDesktopMessengerRelease); + messenger_->SetEngine(this); plugin_registrar_ = std::make_unique(); plugin_registrar_->engine = this; messenger_wrapper_ = std::make_unique(messenger_.get()); message_dispatcher_ = std::make_unique(messenger_.get()); - texture_registrar_ = std::make_unique(this); - - // Set up internal channels. - // TODO: Replace this with an embedder.h API. See - // https://github.com/flutter/flutter/issues/71099 - settings_channel_ = - std::make_unique>( - messenger_wrapper_.get(), "flutter/settings", - &JsonMessageCodec::GetInstance()); + + FlutterELinuxTextureRegistrar::ResolveGlFunctions(gl_procs_); + texture_registrar_ = + std::make_unique(this, gl_procs_); + + vsync_waiter_ = std::make_unique(); } -FlutterLinuxesEngine::~FlutterLinuxesEngine() { Stop(); } +FlutterELinuxEngine::~FlutterELinuxEngine() { + Stop(); +} + +void FlutterELinuxEngine::SetSwitches( + const std::vector& switches) { + project_->SetSwitches(switches); +} -bool FlutterLinuxesEngine::RunWithEntrypoint(const char* entrypoint) { +bool FlutterELinuxEngine::RunWithEntrypoint(const char* entrypoint) { if (!project_->HasValidPaths()) { - LINUXES_LOG(ERROR) << "Missing or unresolvable paths to assets."; + ELINUX_LOG(ERROR) << "Missing or unresolvable paths to assets."; return false; } std::string assets_path_string = project_->assets_path(); @@ -149,7 +185,7 @@ bool FlutterLinuxesEngine::RunWithEntrypoint(const char* entrypoint) { if (embedder_api_.RunsAOTCompiledDartCode()) { aot_data_ = project_->LoadAotData(embedder_api_); if (!aot_data_) { - LINUXES_LOG(ERROR) << "Unable to start engine without AOT data."; + ELINUX_LOG(ERROR) << "Unable to start engine without AOT data."; return false; } } @@ -201,10 +237,20 @@ bool FlutterLinuxesEngine::RunWithEntrypoint(const char* entrypoint) { args.platform_message_callback = [](const FlutterPlatformMessage* engine_message, void* user_data) -> void { - auto host = static_cast(user_data); + auto host = static_cast(user_data); return host->HandlePlatformMessage(engine_message); }; - +#if defined(ENABLE_VSYNC) +// todo: add drm/x11 support. +// https://github.com/sony/flutter-embedded-linux/issues/136 +// https://github.com/sony/flutter-embedded-linux/issues/137 +#if defined(DISPLAY_BACKEND_TYPE_WAYLAND) + args.vsync_callback = [](void* user_data, intptr_t baton) -> void { + auto host = static_cast(user_data); + host->vsync_waiter_->NotifyWaitForVsync(baton); + }; +#endif +#endif args.custom_task_runners = &custom_task_runners; if (aot_data_) { @@ -214,20 +260,27 @@ bool FlutterLinuxesEngine::RunWithEntrypoint(const char* entrypoint) { args.custom_dart_entrypoint = entrypoint; } + args.log_message_callback = [](const char* tag, const char* message, + void* user_data) { + std::string str_tag(tag); + if (str_tag.size() > 0) { + std::cout << str_tag << ": "; + } + std::cout << message << std::endl; + }; + auto renderer_config = GetRendererConfig(); auto result = embedder_api_.Run(FLUTTER_ENGINE_VERSION, &renderer_config, &args, this, &engine_); if (result != kSuccess || engine_ == nullptr) { - LINUXES_LOG(ERROR) << "Failed to start Flutter engine: error " << result; + ELINUX_LOG(ERROR) << "Failed to start Flutter engine: error " << result; return false; } - SendSystemSettings(); - return true; } -bool FlutterLinuxesEngine::Stop() { +bool FlutterELinuxEngine::Stop() { if (engine_) { if (plugin_registrar_destruction_callback_) { plugin_registrar_destruction_callback_(plugin_registrar_.get()); @@ -239,41 +292,46 @@ bool FlutterLinuxesEngine::Stop() { return false; } -void FlutterLinuxesEngine::SetView(FlutterLinuxesView* view) { view_ = view; } +void FlutterELinuxEngine::SetView(FlutterELinuxView* view) { + view_ = view; +} // Returns the currently configured Plugin Registrar. -FlutterDesktopPluginRegistrarRef FlutterLinuxesEngine::GetRegistrar() { +FlutterDesktopPluginRegistrarRef FlutterELinuxEngine::GetRegistrar() { return plugin_registrar_.get(); } -void FlutterLinuxesEngine::SetPluginRegistrarDestructionCallback( +void FlutterELinuxEngine::SetPluginRegistrarDestructionCallback( FlutterDesktopOnPluginRegistrarDestroyed callback) { plugin_registrar_destruction_callback_ = callback; } -void FlutterLinuxesEngine::SendWindowMetricsEvent( +void FlutterELinuxEngine::SendWindowMetricsEvent( const FlutterWindowMetricsEvent& event) { if (engine_) { embedder_api_.SendWindowMetricsEvent(engine_, &event); } } -void FlutterLinuxesEngine::SendPointerEvent(const FlutterPointerEvent& event) { +void FlutterELinuxEngine::SendPointerEvent(const FlutterPointerEvent& event) { if (engine_) { embedder_api_.SendPointerEvent(engine_, &event, 1); } } -bool FlutterLinuxesEngine::SendPlatformMessage( - const char* channel, const uint8_t* message, const size_t message_size, - const FlutterDesktopBinaryReply reply, void* user_data) { +bool FlutterELinuxEngine::SendPlatformMessage( + const char* channel, + const uint8_t* message, + const size_t message_size, + const FlutterDesktopBinaryReply reply, + void* user_data) { FlutterPlatformMessageResponseHandle* response_handle = nullptr; if (reply != nullptr && user_data != nullptr) { FlutterEngineResult result = embedder_api_.PlatformMessageCreateResponseHandle( engine_, reply, user_data, &response_handle); if (result != kSuccess) { - LINUXES_LOG(ERROR) << "Failed to create response handle\n"; + ELINUX_LOG(ERROR) << "Failed to create response handle\n"; return false; } } @@ -295,71 +353,122 @@ bool FlutterLinuxesEngine::SendPlatformMessage( return message_result == kSuccess; } -void FlutterLinuxesEngine::SendPlatformMessageResponse( - const FlutterDesktopMessageResponseHandle* handle, const uint8_t* data, +void FlutterELinuxEngine::SendPlatformMessageResponse( + const FlutterDesktopMessageResponseHandle* handle, + const uint8_t* data, size_t data_length) { embedder_api_.SendPlatformMessageResponse(engine_, handle, data, data_length); } -void FlutterLinuxesEngine::HandlePlatformMessage( +void FlutterELinuxEngine::HandlePlatformMessage( const FlutterPlatformMessage* engine_message) { if (engine_message->struct_size != sizeof(FlutterPlatformMessage)) { - LINUXES_LOG(ERROR) << "Invalid message size received. Expected: " - << sizeof(FlutterPlatformMessage) << " but received " - << engine_message->struct_size; + ELINUX_LOG(ERROR) << "Invalid message size received. Expected: " + << sizeof(FlutterPlatformMessage) << " but received " + << engine_message->struct_size; return; } auto message = ConvertToDesktopMessage(*engine_message); - message_dispatcher_->HandleMessage( - message, [this] {}, [this] {}); + message_dispatcher_->HandleMessage(message, [this] {}, [this] {}); } -void FlutterLinuxesEngine::ReloadSystemFonts() { +void FlutterELinuxEngine::ReloadSystemFonts() { embedder_api_.ReloadSystemFonts(engine_); } -void FlutterLinuxesEngine::SendSystemSettings() { +void FlutterELinuxEngine::SetSystemSettings(float text_scaling_factor, + bool enable_high_contrast) { + if (text_scaling_factor == 0) { + ELINUX_LOG(WARNING) << "text-scaling-factor value must be greater than 0"; + text_scaling_factor = 1.0; + } + + view_->UpdateTextScaleFactor(text_scaling_factor); + view_->UpdateHighContrastEnabled(enable_high_contrast); + SendSystemLocales(); +} + +void FlutterELinuxEngine::SendSystemLocales() { auto languages = flutter::GetPreferredLanguageInfo(); auto flutter_locales = flutter::ConvertToFlutterLocale(languages); // Convert the locale list to the locale pointer list that must be provided. std::vector flutter_locale_list; flutter_locale_list.reserve(flutter_locales.size()); - std::transform( - flutter_locales.begin(), flutter_locales.end(), - std::back_inserter(flutter_locale_list), - [](const auto& arg) -> const auto* { return &arg; }); + std::transform(flutter_locales.begin(), flutter_locales.end(), + std::back_inserter(flutter_locale_list), + [](const auto& arg) -> const auto* { return &arg; }); auto result = embedder_api_.UpdateLocales(engine_, flutter_locale_list.data(), flutter_locale_list.size()); if (result != kSuccess) { - LINUXES_LOG(ERROR) << "Failed to set up Flutter locales."; + ELINUX_LOG(ERROR) << "Failed to set up Flutter locales."; } - - rapidjson::Document settings(rapidjson::kObjectType); - auto& allocator = settings.GetAllocator(); - // todo: Use set values instead of fixed values. - settings.AddMember("alwaysUse24HourFormat", true, allocator); - settings.AddMember("textScaleFactor", 1.0, allocator); - settings.AddMember("platformBrightness", "light", allocator); - settings_channel_->Send(settings); } -bool FlutterLinuxesEngine::RegisterExternalTexture(int64_t texture_id) { +bool FlutterELinuxEngine::RegisterExternalTexture(int64_t texture_id) { return (embedder_api_.RegisterExternalTexture(engine_, texture_id) == kSuccess); } -bool FlutterLinuxesEngine::UnregisterExternalTexture(int64_t texture_id) { +bool FlutterELinuxEngine::UnregisterExternalTexture(int64_t texture_id) { return (embedder_api_.UnregisterExternalTexture(engine_, texture_id) == kSuccess); } -bool FlutterLinuxesEngine::MarkExternalTextureFrameAvailable( +bool FlutterELinuxEngine::MarkExternalTextureFrameAvailable( int64_t texture_id) { return (embedder_api_.MarkExternalTextureFrameAvailable( engine_, texture_id) == kSuccess); } +bool FlutterELinuxEngine::PostRasterThreadTask(fml::closure callback) { + struct Captures { + fml::closure callback; + }; + auto captures = new Captures(); + captures->callback = std::move(callback); + if (embedder_api_.PostRenderThreadTask( + engine_, + [](void* opaque) { + auto captures = reinterpret_cast(opaque); + captures->callback(); + delete captures; + }, + captures) == kSuccess) { + return true; + } + delete captures; + return false; +} + +void FlutterELinuxEngine::OnVsync(uint64_t last_frame_time_nanos, + uint64_t vsync_interval_time_nanos) { + uint64_t current_time_nanos = embedder_api_.GetCurrentTime(); + uint64_t after_vsync_passed_time_nanos = + (current_time_nanos - last_frame_time_nanos) % vsync_interval_time_nanos; + uint64_t frame_start_time_nanos = + current_time_nanos + + (vsync_interval_time_nanos - after_vsync_passed_time_nanos); + uint64_t frame_target_time_nanos = + frame_start_time_nanos + vsync_interval_time_nanos; + + vsync_waiter_->NotifyVsync(engine_, &embedder_api_, frame_start_time_nanos, + frame_target_time_nanos); +} + +void FlutterELinuxEngine::UpdateAccessibilityFeatures( + FlutterAccessibilityFeature flags) { + embedder_api_.UpdateAccessibilityFeatures(engine_, flags); +} + +void FlutterELinuxEngine::UpdateDisplayInfo( + FlutterEngineDisplaysUpdateType update_type, + const FlutterEngineDisplay* displays, + size_t display_count) { + embedder_api_.NotifyDisplayUpdate(engine_, update_type, displays, + display_count); +} + } // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_engine.h b/src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.h similarity index 61% rename from src/flutter/shell/platform/linux_embedded/flutter_linuxes_engine.h rename to src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.h index 3ef50bd8..c7eeee4d 100644 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_engine.h +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux_engine.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_ENGINE_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_ENGINE_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_ENGINE_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_ENGINE_H_ #include @@ -16,24 +16,29 @@ #include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" #include "flutter/shell/platform/common/incoming_message_dispatcher.h" #include "flutter/shell/platform/embedder/embedder.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_state.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_state.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.h" #include "flutter/shell/platform/linux_embedded/flutter_project_bundle.h" -#include "flutter/shell/platform/linux_embedded/public/flutter_linuxes.h" +#include "flutter/shell/platform/linux_embedded/public/flutter_elinux.h" #include "flutter/shell/platform/linux_embedded/task_runner.h" +#include "flutter/shell/platform/linux_embedded/vsync_waiter.h" namespace flutter { -class FlutterLinuxesView; +class FlutterELinuxView; -class FlutterLinuxesEngine { +using FlutterDesktopMessengerReferenceOwner = + std::unique_ptr; + +class FlutterELinuxEngine { public: - explicit FlutterLinuxesEngine(const FlutterProjectBundle& project); - virtual ~FlutterLinuxesEngine(); + explicit FlutterELinuxEngine(const FlutterProjectBundle& project); + virtual ~FlutterELinuxEngine(); // Prevent copying. - FlutterLinuxesEngine(FlutterLinuxesEngine const&) = delete; - FlutterLinuxesEngine& operator=(FlutterLinuxesEngine const&) = delete; + FlutterELinuxEngine(FlutterELinuxEngine const&) = delete; + FlutterELinuxEngine& operator=(FlutterELinuxEngine const&) = delete; // Starts running the engine with the given entrypoint. If null, defaults to // main(). @@ -50,11 +55,11 @@ class FlutterLinuxesEngine { bool Stop(); // Sets the view that is displaying this engine's content. - void SetView(FlutterLinuxesView* view); + void SetView(FlutterELinuxView* view); // The view displaying this engine's content, if any. This will be null for // headless engines. - FlutterLinuxesView* view() { return view_; } + FlutterELinuxView* view() { return view_; } // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef GetRegistrar(); @@ -63,7 +68,12 @@ class FlutterLinuxesEngine { void SetPluginRegistrarDestructionCallback( FlutterDesktopOnPluginRegistrarDestroyed callback); - FlutterDesktopMessengerRef messenger() { return messenger_.get(); } + // Sets switches member to the given switches. + void SetSwitches(const std::vector& switches); + + FlutterDesktopMessengerRef messenger() SWIFT_RETURNS_UNRETAINED { + return messenger_.get(); + } IncomingMessageDispatcher* message_dispatcher() { return message_dispatcher_.get(); @@ -71,7 +81,7 @@ class FlutterLinuxesEngine { TaskRunner* task_runner() { return task_runner_.get(); } - FlutterLinuxesTextureRegistrar* texture_registrar() { + FlutterELinuxTextureRegistrar* texture_registrar() { return texture_registrar_.get(); } @@ -83,14 +93,16 @@ class FlutterLinuxesEngine { // Sends the given message to the engine, calling |reply| with |user_data| // when a reponse is received from the engine if they are non-null. - bool SendPlatformMessage(const char* channel, const uint8_t* message, + bool SendPlatformMessage(const char* channel, + const uint8_t* message, const size_t message_size, const FlutterDesktopBinaryReply reply, void* user_data); // Sends the given data as the response to an earlier platform message. void SendPlatformMessageResponse( - const FlutterDesktopMessageResponseHandle* handle, const uint8_t* data, + const FlutterDesktopMessageResponseHandle* handle, + const uint8_t* data, size_t data_length); // Callback passed to Flutter engine for notifying window of platform @@ -110,15 +122,36 @@ class FlutterLinuxesEngine { // given |texture_id|. bool MarkExternalTextureFrameAvailable(int64_t texture_id); + // Posts the given callback onto the raster thread. + bool PostRasterThreadTask(fml::closure callback); + + // Notifies the engine about the vsync event. + void OnVsync(uint64_t last_frame_time_nanos, + uint64_t vsync_interval_time_nanos); + + // Gets the status whether Impeller is enabled. + bool IsImpellerEnabled() const { return enable_impeller_; } + + // Sets system settings. + void SetSystemSettings(float text_scaling_factor, bool enable_high_contrast); + + // Updates accessibility, e.g. switch to high contrast mode + void UpdateAccessibilityFeatures(FlutterAccessibilityFeature flags); + + // Update display information. + void UpdateDisplayInfo(FlutterEngineDisplaysUpdateType update_type, + const FlutterEngineDisplay* displays, + size_t display_count); + private: // Allows swapping out embedder_api_ calls in tests. friend class EngineEmbedderApiModifier; - // Sends system settings (e.g., locale) to the engine. + // Sends system locales to the engine. // // Should be called just after the engine is run, and after any relevant // system changes. - void SendSystemSettings(); + void SendSystemLocales(); // The handle to the embedder.h engine instance. FLUTTER_API_SYMBOL(FlutterEngine) engine_ = nullptr; @@ -131,13 +164,14 @@ class FlutterLinuxesEngine { UniqueAotDataPtr aot_data_; // The view displaying the content running in this engine, if any. - FlutterLinuxesView* view_ = nullptr; + FlutterELinuxView* view_ = nullptr; // Task runner for tasks posted from the engine. std::unique_ptr task_runner_; // The plugin messenger handle given to API clients. - std::unique_ptr messenger_; + FlutterDesktopMessengerReferenceOwner messenger_ = { + nullptr, [](FlutterDesktopMessengerRef ref) {}}; // A wrapper around messenger_ for interacting with client_wrapper-level APIs. std::unique_ptr messenger_wrapper_; @@ -149,17 +183,22 @@ class FlutterLinuxesEngine { std::unique_ptr plugin_registrar_; // The texture registrar. - std::unique_ptr texture_registrar_; + std::unique_ptr texture_registrar_; - // The MethodChannel used for communication with the Flutter engine. - std::unique_ptr> settings_channel_; + // Resolved OpenGL functions used by external texture implementations. + GlProcs gl_procs_ = {}; // A callback to be called when the engine (and thus the plugin registrar) // is being destroyed. FlutterDesktopOnPluginRegistrarDestroyed plugin_registrar_destruction_callback_ = nullptr; -}; + + // The vsync waiter. + std::unique_ptr vsync_waiter_; + + bool enable_impeller_ = false; +} SWIFT_UNSAFE_REFERENCE; } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_ENGINE_H_ +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_ENGINE_H_ diff --git a/src/flutter/shell/platform/linux_embedded/flutter_elinux_state.h b/src/flutter/shell/platform/linux_embedded/flutter_elinux_state.h new file mode 100644 index 00000000..061f1e87 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux_state.h @@ -0,0 +1,91 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_STATE_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_STATE_H_ + +#include +#include +#include + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h" +#include "flutter/shell/platform/common/incoming_message_dispatcher.h" +#include "flutter/shell/platform/embedder/embedder.h" + +// Structs backing the opaque references used in the C API. +// +// DO NOT ADD ANY NEW CODE HERE. These are legacy, and are being phased out +// in favor of objects that own and manage the relevant functionality. + +namespace flutter { +struct FlutterELinuxEngine; +struct FlutterELinuxView; +} // namespace flutter + +// Wrapper to distinguish the view controller ref from the view ref given out +// in the C API. +struct FlutterDesktopViewControllerState { + // The view that backs this state object. + std::unique_ptr view; + + ~FlutterDesktopViewControllerState() = default; +}; + +// Wrapper to distinguish the plugin registrar ref from the engine ref given out +// in the C API. +struct FlutterDesktopPluginRegistrar { + // The engine that owns this state object. + flutter::FlutterELinuxEngine* engine = nullptr; +}; + +// Wrapper to distinguish the messenger ref from the engine ref given out +// in the C API. +struct FlutterDesktopMessenger { + FlutterDesktopMessenger() = default; + + /// Increments the reference count. + /// + /// Thread-safe. + void AddRef() { ref_count_.fetch_add(1); } + + /// Decrements the reference count and deletes the object if the count has + /// gone to zero. + /// + /// Thread-safe. + void Release() { + int32_t old_count = ref_count_.fetch_sub(1); + if (old_count <= 1) { + delete this; + } + } + + /// Getter for the engine field. + flutter::FlutterELinuxEngine* GetEngine() const { return engine_; } + + /// Setter for the engine field. + /// Thread-safe. + void SetEngine(flutter::FlutterELinuxEngine* engine) { + std::scoped_lock lock(mutex_); + engine_ = engine; + } + + /// Returns the mutex associated with the |FlutterDesktopMessenger|. + /// + /// This mutex is used to synchronize reading or writing state inside the + /// |FlutterDesktopMessenger| (ie |engine_|). + std::mutex& GetMutex() { return mutex_; } + + FlutterDesktopMessenger(const FlutterDesktopMessenger& value) = delete; + FlutterDesktopMessenger& operator=(const FlutterDesktopMessenger& value) = + delete; + + private: + // The engine that backs this messenger. + flutter::FlutterELinuxEngine* engine_; + std::atomic ref_count_ = 0; + std::mutex mutex_; +} SWIFT_SHARED_REFERENCE(FlutterDesktopMessengerAddRef, + FlutterDesktopMessengerRelease); + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_STATE_H_ diff --git a/src/flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.cc b/src/flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.cc new file mode 100644 index 00000000..66a603c3 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.cc @@ -0,0 +1,143 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.h" + +#include +#include + +#include "flutter/shell/platform/embedder/embedder_struct_macros.h" +#include "flutter/shell/platform/linux_embedded/external_texture_egl_image.h" +#include "flutter/shell/platform/linux_embedded/external_texture_pixelbuffer.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_engine.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_view.h" + +namespace { +static constexpr int64_t kInvalidTexture = -1; +} + +namespace flutter { + +FlutterELinuxTextureRegistrar::FlutterELinuxTextureRegistrar( + FlutterELinuxEngine* engine, + const GlProcs& gl_procs) + : engine_(engine), gl_procs_(gl_procs) {} + +int64_t FlutterELinuxTextureRegistrar::RegisterTexture( + const FlutterDesktopTextureInfo* texture_info) { + if (!gl_procs_.valid) { + return kInvalidTexture; + } + + if (texture_info->type == kFlutterDesktopPixelBufferTexture) { + if (!texture_info->pixel_buffer_config.callback) { + std::cerr << "Invalid pixel buffer texture callback." << std::endl; + return kInvalidTexture; + } + + return EmplaceTexture(std::make_unique( + texture_info->pixel_buffer_config.callback, + texture_info->pixel_buffer_config.user_data, gl_procs_)); + } else if (texture_info->type == kFlutterDesktopEGLImageTexture) { + if (!texture_info->egl_image_config.callback) { + std::cerr << "Invalid EGLImage texture callback." << std::endl; + return kInvalidTexture; + } + + return EmplaceTexture(std::make_unique( + texture_info->egl_image_config.callback, + texture_info->egl_image_config.user_data, gl_procs_)); + } else if (texture_info->type == kFlutterDesktopGpuSurfaceTexture) { + std::cerr << "GpuSurfaceTexture is not yet supported." << std::endl; + return kInvalidTexture; + } + + std::cerr << "Attempted to register texture of unsupport type." << std::endl; + return kInvalidTexture; +} + +int64_t FlutterELinuxTextureRegistrar::EmplaceTexture( + std::unique_ptr texture) { + int64_t texture_id = texture->texture_id(); + { + std::lock_guard lock(map_mutex_); + textures_[texture_id] = std::move(texture); + } + + engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() { + engine->RegisterExternalTexture(texture_id); + }); + + return texture_id; +} + +void FlutterELinuxTextureRegistrar::UnregisterTexture(int64_t texture_id, + fml::closure callback) { + engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() { + engine->UnregisterExternalTexture(texture_id); + }); + + bool posted = engine_->PostRasterThreadTask([this, texture_id, callback]() { + { + std::lock_guard lock(map_mutex_); + auto it = textures_.find(texture_id); + if (it != textures_.end()) { + textures_.erase(it); + } + } + if (callback) { + callback(); + } + }); + + if (!posted && callback) { + callback(); + } +} + +bool FlutterELinuxTextureRegistrar::MarkTextureFrameAvailable( + int64_t texture_id) { + engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() { + engine->MarkExternalTextureFrameAvailable(texture_id); + }); + return true; +} + +bool FlutterELinuxTextureRegistrar::PopulateTexture( + int64_t texture_id, + size_t width, + size_t height, + FlutterOpenGLTexture* opengl_texture) { + flutter::ExternalTexture* texture; + { + std::lock_guard lock(map_mutex_); + auto it = textures_.find(texture_id); + if (it == textures_.end()) { + return false; + } + texture = it->second.get(); + } + return texture->PopulateTexture(width, height, opengl_texture); +} + +void FlutterELinuxTextureRegistrar::ResolveGlFunctions(GlProcs& procs) { + procs.glGenTextures = + reinterpret_cast(eglGetProcAddress("glGenTextures")); + procs.glDeleteTextures = reinterpret_cast( + eglGetProcAddress("glDeleteTextures")); + procs.glBindTexture = + reinterpret_cast(eglGetProcAddress("glBindTexture")); + procs.glTexParameteri = reinterpret_cast( + eglGetProcAddress("glTexParameteri")); + procs.glTexImage2D = + reinterpret_cast(eglGetProcAddress("glTexImage2D")); + procs.glEGLImageTargetTexture2DOES = + reinterpret_cast( + eglGetProcAddress("glEGLImageTargetTexture2DOES")); + procs.valid = procs.glGenTextures && procs.glDeleteTextures && + procs.glBindTexture && procs.glTexParameteri && + procs.glTexImage2D && procs.glEGLImageTargetTexture2DOES; +} + +}; // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.h b/src/flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.h similarity index 59% rename from src/flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.h rename to src/flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.h index 84ff5873..6e75cb00 100644 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.h +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux_texture_registrar.h @@ -2,32 +2,34 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_TEXTURE_REGISTRAR_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_TEXTURE_REGISTRAR_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_TEXTURE_REGISTRAR_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_TEXTURE_REGISTRAR_H_ #include #include #include -#include "flutter/shell/platform/linux_embedded/external_texture_gl.h" +#include "flutter/fml/closure.h" +#include "flutter/shell/platform/common/public/flutter_texture_registrar.h" +#include "flutter/shell/platform/linux_embedded/external_texture.h" namespace flutter { -class FlutterLinuxesEngine; +class FlutterELinuxEngine; // An object managing the registration of an external texture. // Thread safety: All member methods are thread safe. -class FlutterLinuxesTextureRegistrar { +class FlutterELinuxTextureRegistrar { public: - explicit FlutterLinuxesTextureRegistrar(FlutterLinuxesEngine* engine); + explicit FlutterELinuxTextureRegistrar(FlutterELinuxEngine* engine, + const GlProcs& gl_procs); // Registers a texture described by the given |texture_info| object. // Returns the non-zero, positive texture id or -1 on error. int64_t RegisterTexture(const FlutterDesktopTextureInfo* texture_info); // Attempts to unregister the texture identified by |texture_id|. - // Returns true if the texture was successfully unregistered. - bool UnregisterTexture(int64_t texture_id); + void UnregisterTexture(int64_t texture_id, fml::closure callback = nullptr); // Notifies the engine about a new frame being available. // Returns true on success. @@ -41,15 +43,21 @@ class FlutterLinuxesTextureRegistrar { size_t height, FlutterOpenGLTexture* texture); + // Populates the OpenGL function pointers in |gl_procs|. + static void ResolveGlFunctions(GlProcs& gl_procs); + private: - FlutterLinuxesEngine* engine_ = nullptr; + FlutterELinuxEngine* engine_ = nullptr; + const GlProcs& gl_procs_; // All registered textures, keyed by their IDs. - std::unordered_map> + std::unordered_map> textures_; std::mutex map_mutex_; -}; + + int64_t EmplaceTexture(std::unique_ptr texture); +} SWIFT_UNSAFE_REFERENCE; }; // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_TEXTURE_REGISTRAR_H_ +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_TEXTURE_REGISTRAR_H_ diff --git a/src/flutter/shell/platform/linux_embedded/flutter_elinux_view.cc b/src/flutter/shell/platform/linux_embedded/flutter_elinux_view.cc new file mode 100644 index 00000000..9775d200 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux_view.cc @@ -0,0 +1,595 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/flutter_elinux_view.h" + +#include +#include + +#include "flutter/common/constants.h" +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +namespace { +constexpr int kMicrosecondsPerMillisecond = 1000; + +inline FlutterTransformation FlutterTransformationMake(const uint16_t& degree) { + double radian = degree * M_PI / 180.0; + FlutterTransformation transformation = {}; + transformation.scaleX = cos(radian); + transformation.skewX = -sin(radian); + transformation.transX = 0; + transformation.skewY = sin(radian); + transformation.scaleY = cos(radian); + transformation.transY = 0; + transformation.pers0 = 0; + transformation.pers1 = 0; + transformation.pers2 = 1; + return transformation; +} + +} // namespace + +FlutterELinuxView::FlutterELinuxView( + std::unique_ptr window_binding) { + // Take the binding handler, and give it a pointer back to self. + binding_handler_ = std::move(window_binding); + binding_handler_->SetView(this); +} + +FlutterELinuxView::~FlutterELinuxView() { + // Need to stop running the Engine before destroying surface. + if (engine_) { + engine_->Stop(); + } + DestroyRenderSurface(); +} + +bool FlutterELinuxView::DispatchEvent() { + return binding_handler_->DispatchEvent(); +} + +void FlutterELinuxView::SetEngine(std::unique_ptr engine) { + engine_ = std::move(engine); + + engine_->SetView(this); + + internal_plugin_registrar_ = + std::make_unique(engine_->GetRegistrar()); + + // Set up the system channel handlers. + auto internal_plugin_messenger = internal_plugin_registrar_->messenger(); + + // Set up internal channels. + // TODO: Replace this with an embedder.h API. See + // https://github.com/flutter/flutter/issues/71099 + keyboard_handler_ = + std::make_unique(internal_plugin_messenger); + textinput_handler_ = std::make_unique( + internal_plugin_messenger, binding_handler_.get()); + platform_handler_ = std::make_unique( + internal_plugin_messenger, binding_handler_.get()); + cursor_handler_ = std::make_unique( + internal_plugin_messenger, binding_handler_.get()); + lifecycle_handler_ = + std::make_unique(internal_plugin_messenger); + navigation_handler_ = + std::make_unique(internal_plugin_messenger); + platform_views_handler_ = + std::make_unique(internal_plugin_messenger); + settings_handler_ = std::make_unique( + internal_plugin_messenger, binding_handler_.get()); + + PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); + SendWindowMetrics(bounds.width, bounds.height, + binding_handler_->GetDpiScale()); +} + +void FlutterELinuxView::RegisterPlatformViewFactory( + const char* view_type, + std::unique_ptr factory) { + platform_views_handler_->RegisterViewFactory(view_type, std::move(factory)); +} + +void FlutterELinuxView::OnWindowSizeChanged(size_t width_px, + size_t height_px) const { + if (!GetRenderSurfaceTarget()->OnScreenSurfaceResize(width_px, height_px)) { + ELINUX_LOG(ERROR) << "Failed to change surface size."; + return; + } + SendWindowMetrics(width_px, height_px, binding_handler_->GetDpiScale()); +} + +void FlutterELinuxView::OnPointerMove(double x_px, double y_px) { + auto trimmed_xy = GetPointerRotation(x_px, y_px); + SendPointerMove(trimmed_xy.first, trimmed_xy.second); +} + +void FlutterELinuxView::OnPointerDown( + double x_px, + double y_px, + FlutterPointerMouseButtons flutter_button) { + if (flutter_button != 0) { + uint64_t mouse_buttons = mouse_state_.buttons | flutter_button; + auto trimmed_xy = GetPointerRotation(x_px, y_px); + SetMouseButtons(mouse_buttons); + SendPointerDown(trimmed_xy.first, trimmed_xy.second); + } +} + +void FlutterELinuxView::OnPointerUp(double x_px, + double y_px, + FlutterPointerMouseButtons flutter_button) { + if (flutter_button != 0) { + auto trimmed_xy = GetPointerRotation(x_px, y_px); + uint64_t mouse_buttons = mouse_state_.buttons & ~flutter_button; + SetMouseButtons(mouse_buttons); + SendPointerUp(trimmed_xy.first, trimmed_xy.second); + } +} + +void FlutterELinuxView::OnPointerLeave() { + SendPointerLeave(); +} + +void FlutterELinuxView::OnTouchDown(uint32_t time, + int32_t id, + double x, + double y) { + // Increase device-id to avoid + // "FML_DCHECK(states_.find(pointer_data.device) == states_.end());" + // exception in flutter/engine. + // This is because "device-id = 0" is used for mouse inputs. + // See engine/lib/ui/window/pointer_data_packet_converter.cc + id += 1; + + auto trimmed_xy = GetPointerRotation(x, y); + auto* point = GgeTouchPoint(id); + if (!point) { + return; + } + point->event_mask = TouchEvent::kDown; + point->x = trimmed_xy.first; + point->y = trimmed_xy.second; + + FlutterPointerEvent event = { + .struct_size = sizeof(event), + .phase = FlutterPointerPhase::kDown, + .timestamp = time * kMicrosecondsPerMillisecond, + .x = point->x, + .y = point->y, + .device = id, + .signal_kind = kFlutterPointerSignalKindNone, + .scroll_delta_x = 0, + .scroll_delta_y = 0, + .device_kind = kFlutterPointerDeviceKindTouch, + .buttons = 0, + // TODO: Use the correct view ID for pointer events once the + // eLinux embedder supports multiple views. + .view_id = flutter::kFlutterImplicitViewId, + }; + engine_->SendPointerEvent(event); +} + +void FlutterELinuxView::OnTouchUp(uint32_t time, int32_t id) { + // Increase device-id to avoid + // "FML_DCHECK(states_.find(pointer_data.device) == states_.end());" + // exception in flutter/engine. + // This is because "device-id = 0" is used for mouse inputs. + // See engine/lib/ui/window/pointer_data_packet_converter.cc + id += 1; + + auto* point = GgeTouchPoint(id); + if (!point) { + return; + } + + // Makes sure we have an existing touch pointer in down state to + // avoid "FML_DCHECK(iter != states_.end())" exception in flutter/engine. + // See engine/lib/ui/window/pointer_data_packet_converter.cc + if (point->event_mask != TouchEvent::kDown && + point->event_mask != TouchEvent::kMotion) { + return; + } + point->event_mask = TouchEvent::kUp; + + FlutterPointerEvent event = { + .struct_size = sizeof(event), + .phase = FlutterPointerPhase::kUp, + .timestamp = time * kMicrosecondsPerMillisecond, + .x = point->x, + .y = point->y, + .device = id, + .signal_kind = kFlutterPointerSignalKindNone, + .scroll_delta_x = 0, + .scroll_delta_y = 0, + .device_kind = kFlutterPointerDeviceKindTouch, + .buttons = 0, + // TODO: Use the correct view ID for pointer events once the + // eLinux embedder supports multiple views. + .view_id = flutter::kFlutterImplicitViewId, + }; + engine_->SendPointerEvent(event); +} + +void FlutterELinuxView::OnTouchMotion(uint32_t time, + int32_t id, + double x, + double y) { + // Increase device-id to avoid avoid + // "FML_DCHECK(states_.find(pointer_data.device) == states_.end());" + // exception in flutter/engine. + // This is because "device-id = 0" is used for mouse inputs. + // See engine/lib/ui/window/pointer_data_packet_converter.cc + id += 1; + + auto trimmed_xy = GetPointerRotation(x, y); + auto* point = GgeTouchPoint(id); + if (!point) { + return; + } + + // Makes sure we have an existing touch pointer in down state to + // avoid "FML_DCHECK(iter != states_.end())" exception in flutter/engine. + // See engine/lib/ui/window/pointer_data_packet_converter.cc + if (point->event_mask != TouchEvent::kDown && + point->event_mask != TouchEvent::kMotion) { + return; + } + point->event_mask = TouchEvent::kMotion; + point->x = trimmed_xy.first; + point->y = trimmed_xy.second; + + FlutterPointerEvent event = { + .struct_size = sizeof(event), + .phase = FlutterPointerPhase::kMove, + .timestamp = time * kMicrosecondsPerMillisecond, + .x = point->x, + .y = point->y, + .device = id, + .signal_kind = kFlutterPointerSignalKindNone, + .scroll_delta_x = 0, + .scroll_delta_y = 0, + .device_kind = kFlutterPointerDeviceKindTouch, + .buttons = 0, + // TODO: Use the correct view ID for pointer events once the + // eLinux embedder supports multiple views. + .view_id = flutter::kFlutterImplicitViewId, + }; + engine_->SendPointerEvent(event); +} + +void FlutterELinuxView::OnTouchCancel() {} + +void FlutterELinuxView::OnKeyMap(uint32_t format, int fd, uint32_t size) { + keyboard_handler_->OnKeymap(format, fd, size); +} + +void FlutterELinuxView::OnKey(uint32_t key, bool pressed) { + keyboard_handler_->OnKey(key, pressed); + if (pressed) { + auto code_point = keyboard_handler_->GetCodePoint(key); + if (!keyboard_handler_->IsTextInputSuppressed(code_point)) { + textinput_handler_->OnKeyPressed(key, code_point); + } + } +} + +void FlutterELinuxView::OnKeyModifiers(uint32_t mods_depressed, + uint32_t mods_latched, + uint32_t mods_locked, + uint32_t group) { + keyboard_handler_->OnModifiers(mods_depressed, mods_latched, mods_locked, + group); +} + +void FlutterELinuxView::OnVirtualKey(uint32_t code_point) { + // Since the keycode cannot be specified, set an invalid value(0). + constexpr uint32_t kCharKey = 0; + textinput_handler_->OnKeyPressed(kCharKey, code_point); +} + +void FlutterELinuxView::OnVirtualSpecialKey(uint32_t keycode) { + auto code_point = keyboard_handler_->GetCodePoint(keycode); + textinput_handler_->OnKeyPressed(keycode, code_point); +} + +void FlutterELinuxView::OnScroll(double x, + double y, + double delta_x, + double delta_y, + int scroll_offset_multiplier) { + auto trimmed_xy = GetPointerRotation(x, y); + SendScroll(trimmed_xy.first, trimmed_xy.second, delta_x, delta_y, + scroll_offset_multiplier); +} + +void FlutterELinuxView::OnVsync(uint64_t last_frame_time_nanos, + uint64_t vsync_interval_time_nanos) { + engine_->OnVsync(last_frame_time_nanos, vsync_interval_time_nanos); +} + +FlutterELinuxView::touch_point* FlutterELinuxView::GgeTouchPoint(int32_t id) { + const size_t nmemb = sizeof(touch_event_) / sizeof(struct touch_point); + int invalid = -1; + for (size_t i = 0; i < nmemb; ++i) { + if (touch_event_.points[i].id == id) { + return &touch_event_.points[i]; + } + if (invalid == -1 && !touch_event_.points[i].valid) { + invalid = i; + } + } + if (invalid == -1) { + return nullptr; + } + touch_event_.points[invalid].valid = true; + touch_event_.points[invalid].id = id; + return &touch_event_.points[invalid]; +} + +// Sends new size information to FlutterEngine. +void FlutterELinuxView::SendWindowMetrics(size_t width_px, + size_t height_px, + double dpiScale) const { + FlutterWindowMetricsEvent event = {}; + event.struct_size = sizeof(event); + event.width = width_px; + event.height = height_px; + event.pixel_ratio = dpiScale; + engine_->SendWindowMetricsEvent(event); +} + +void FlutterELinuxView::SendInitialBounds() { + PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); + SendWindowMetrics(bounds.width, bounds.height, + binding_handler_->GetDpiScale()); +} + +// Set's |event_data|'s phase to either kMove or kHover depending on the current +// primary mouse button state. +void FlutterELinuxView::SetEventPhaseFromCursorButtonState( + FlutterPointerEvent* event_data) const { + // For details about this logic, see FlutterPointerPhase in the embedder.h + // file. + event_data->phase = + mouse_state_.buttons == 0 ? mouse_state_.flutter_state_is_down + ? FlutterPointerPhase::kUp + : FlutterPointerPhase::kHover + : mouse_state_.flutter_state_is_down ? FlutterPointerPhase::kMove + : FlutterPointerPhase::kDown; +} + +void FlutterELinuxView::SendPointerMove(double x_px, double y_px) { + FlutterPointerEvent event = {}; + event.x = x_px; + event.y = y_px; + SetEventPhaseFromCursorButtonState(&event); + SendPointerEventWithData(event); +} + +void FlutterELinuxView::SendPointerDown(double x_px, double y_px) { + FlutterPointerEvent event = {}; + SetEventPhaseFromCursorButtonState(&event); + event.x = x_px; + event.y = y_px; + SendPointerEventWithData(event); + SetMouseFlutterStateDown(true); +} + +void FlutterELinuxView::SendPointerUp(double x_px, double y_px) { + FlutterPointerEvent event = {}; + SetEventPhaseFromCursorButtonState(&event); + event.x = x_px; + event.y = y_px; + SendPointerEventWithData(event); + if (event.phase == FlutterPointerPhase::kUp) { + SetMouseFlutterStateDown(false); + } +} + +void FlutterELinuxView::SendPointerLeave() { + FlutterPointerEvent event = {}; + event.phase = FlutterPointerPhase::kRemove; + SendPointerEventWithData(event); +} + +void FlutterELinuxView::SendScroll(double x, + double y, + double delta_x, + double delta_y, + int scroll_offset_multiplier) { + FlutterPointerEvent event = {}; + SetEventPhaseFromCursorButtonState(&event); + event.signal_kind = FlutterPointerSignalKind::kFlutterPointerSignalKindScroll; + event.x = x; + event.y = y; + event.scroll_delta_x = delta_x * scroll_offset_multiplier; + event.scroll_delta_y = delta_y * scroll_offset_multiplier; + SendPointerEventWithData(event); +} + +void FlutterELinuxView::SendPointerEventWithData( + const FlutterPointerEvent& event_data) { + // If sending anything other than an add, and the pointer isn't already added, + // synthesize an add to satisfy Flutter's expectations about events. + if (!mouse_state_.flutter_state_is_added && + event_data.phase != FlutterPointerPhase::kAdd) { + FlutterPointerEvent event = {}; + event.phase = FlutterPointerPhase::kAdd; + event.x = event_data.x; + event.y = event_data.y; + event.buttons = 0; + SendPointerEventWithData(event); + } + // Don't double-add (e.g., if events are delivered out of order, so an add has + // already been synthesized). + if (mouse_state_.flutter_state_is_added && + event_data.phase == FlutterPointerPhase::kAdd) { + return; + } + + FlutterPointerEvent event = event_data; + event.device_kind = kFlutterPointerDeviceKindMouse; + event.buttons = mouse_state_.buttons; + // TODO: Use the correct view ID for pointer events once the + // eLinux embedder supports multiple views. + event.view_id = flutter::kFlutterImplicitViewId; + + // Set metadata that's always the same regardless of the event. + event.struct_size = sizeof(event); + event.timestamp = + std::chrono::duration_cast( + std::chrono::high_resolution_clock::now().time_since_epoch()) + .count(); + + engine_->SendPointerEvent(event); + + if (event_data.phase == FlutterPointerPhase::kAdd) { + SetMouseFlutterStateAdded(true); + } else if (event_data.phase == FlutterPointerPhase::kRemove) { + SetMouseFlutterStateAdded(false); + ResetMouseState(); + } +} + +void* FlutterELinuxView::ProcResolver(const char* name) { + return GetRenderSurfaceTarget()->GlProcResolver(name); +} + +bool FlutterELinuxView::MakeCurrent() { + return GetRenderSurfaceTarget()->GLContextMakeCurrent(); +} + +bool FlutterELinuxView::ClearCurrent() { + return GetRenderSurfaceTarget()->GLContextClearCurrent(); +} + +bool FlutterELinuxView::Present() { + return GetRenderSurfaceTarget()->GLContextPresent(0); +} + +bool FlutterELinuxView::PresentWithInfo(const FlutterPresentInfo* info) { + return GetRenderSurfaceTarget()->GLContextPresentWithInfo(info); +} + +void FlutterELinuxView::PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage) { + GetRenderSurfaceTarget()->PopulateExistingDamage(fbo_id, existing_damage); +} + +uint32_t FlutterELinuxView::GetOnscreenFBO() { + return GetRenderSurfaceTarget()->GLContextFBO(); +} + +bool FlutterELinuxView::MakeResourceCurrent() { + return GetRenderSurfaceTarget()->ResourceContextMakeCurrent(); +} + +bool FlutterELinuxView::CreateRenderSurface() { + PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); + auto impeller_enable = engine_.get()->IsImpellerEnabled(); + return binding_handler_->CreateRenderSurface(bounds.width, bounds.height, + impeller_enable); +} + +void FlutterELinuxView::DestroyRenderSurface() { + binding_handler_->DestroyRenderSurface(); +} + +ELinuxRenderSurfaceTarget* FlutterELinuxView::GetRenderSurfaceTarget() const { + return binding_handler_->GetRenderSurfaceTarget(); +} + +FlutterELinuxEngine* FlutterELinuxView::GetEngine() { + return engine_.get(); +} + +int32_t FlutterELinuxView::GetFrameRate() { + return binding_handler_->GetFrameRate(); +} + +FlutterTransformation FlutterELinuxView::GetRootSurfaceTransformation() { + auto degree = binding_handler_->GetRotationDegree(); + if (view_rotation_degree_ != degree) { + view_rotation_transformation_ = FlutterTransformationMake(degree); + } + view_rotation_degree_ = degree; + + auto bounds = binding_handler_->GetPhysicalWindowBounds(); + switch (degree) { + case 90: + view_rotation_transformation_.transX = bounds.height; + break; + case 180: + view_rotation_transformation_.transX = bounds.width; + view_rotation_transformation_.transY = bounds.height; + break; + case 270: + view_rotation_transformation_.transY = bounds.width; + break; + default: + break; + } + return view_rotation_transformation_; +} + +std::pair FlutterELinuxView::GetPointerRotation(double x_px, + double y_px) { + auto degree = binding_handler_->GetRotationDegree(); + auto bounds = binding_handler_->GetPhysicalWindowBounds(); + std::pair res = {x_px, y_px}; + + if (degree == 90) { + res.first = y_px; + res.second = bounds.height - x_px; + } else if (degree == 180) { + res.first = bounds.width - x_px; + res.second = bounds.height - y_px; + } else if (degree == 270) { + res.first = bounds.width - y_px; + res.second = x_px; + } + return res; +} + +void FlutterELinuxView::UpdateHighContrastEnabled(bool enabled) { + int flags = 0; + if (enabled) { + flags |= + FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast; + } else { + flags &= + ~FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast; + } + engine_.get()->UpdateAccessibilityFeatures( + static_cast(flags)); + settings_handler_->UpdateHighContrastMode(enabled); +} + +void FlutterELinuxView::UpdateTextScaleFactor(float factor) { + settings_handler_->UpdateTextScaleFactor(factor); +} + +void FlutterELinuxView::UpdateDisplayInfo(double refresh_rate, + size_t width_px, + size_t height_px, + double pixel_ratio) { + const FlutterEngineDisplaysUpdateType update_type = + kFlutterEngineDisplaysUpdateTypeStartup; + const FlutterEngineDisplay displays = { + .struct_size = sizeof(FlutterEngineDisplay), + .display_id = 0, + .single_display = true, + .refresh_rate = refresh_rate, + .width = width_px, + .height = height_px, + .device_pixel_ratio = pixel_ratio, + }; + const size_t display_count = 1; + engine_.get()->UpdateDisplayInfo(update_type, &displays, display_count); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_view.h b/src/flutter/shell/platform/linux_embedded/flutter_elinux_view.h similarity index 53% rename from src/flutter/shell/platform/linux_embedded/flutter_linuxes_view.h rename to src/flutter/shell/platform/linux_embedded/flutter_elinux_view.h index fc7f2ce1..0d6ed8d5 100644 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_view.h +++ b/src/flutter/shell/platform/linux_embedded/flutter_elinux_view.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_VIEW_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_VIEW_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_VIEW_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_VIEW_H_ #include #include @@ -11,28 +11,33 @@ #include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h" #include "flutter/shell/platform/embedder/embedder.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_engine.h" -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_state.h" -#include "flutter/shell/platform/linux_embedded/plugin/key_event_plugin.h" -#include "flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.h" -#include "flutter/shell/platform/linux_embedded/plugin/platform_plugin.h" -#include "flutter/shell/platform/linux_embedded/plugin/text_input_plugin.h" -#include "flutter/shell/platform/linux_embedded/public/flutter_linuxes.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_engine.h" +#include "flutter/shell/platform/linux_embedded/flutter_elinux_state.h" +#include "flutter/shell/platform/linux_embedded/plugins/key_event_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/navigation_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/platform_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/settings_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/text_input_plugin.h" +#include "flutter/shell/platform/linux_embedded/public/flutter_elinux.h" +#include "flutter/shell/platform/linux_embedded/public/flutter_platform_views.h" #include "flutter/shell/platform/linux_embedded/window_binding_handler.h" #include "flutter/shell/platform/linux_embedded/window_binding_handler_delegate.h" namespace flutter { -class FlutterLinuxesView : public WindowBindingHandlerDelegate { +class FlutterELinuxView : public WindowBindingHandlerDelegate { public: - // Creates a FlutterLinuxesView with the given implementator of + // Creates a FlutterELinuxView with the given implementator of // WindowBindingHandler. // // In order for object to render Flutter content the SetEngine method must be - // called with a valid FlutterLinuxesEngine instance. - FlutterLinuxesView(std::unique_ptr window_binding); + // called with a valid FlutterELinuxEngine instance. + FlutterELinuxView(std::unique_ptr window_binding); - ~FlutterLinuxesView(); + ~FlutterELinuxView(); // Dispatches window events such as mouse and keyboard inputs. For Wayland, // you have to call this every time in the main loop. @@ -40,7 +45,12 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { // Configures the window instance with an instance of a running Flutter // engine. - void SetEngine(std::unique_ptr engine); + void SetEngine(std::unique_ptr engine); + + // Registers a factory of the platform view. + void RegisterPlatformViewFactory( + const char* view_type, + std::unique_ptr factory); // Creates rendering surface for Flutter engine to draw into. // Should be called before calling FlutterEngineRun using this view. @@ -49,17 +59,26 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { // Destroys current rendering surface if one has been allocated. void DestroyRenderSurface(); - // Return the currently configured LinuxesRenderSurfaceTarget. - LinuxesRenderSurfaceTarget* GetRenderSurfaceTarget() const; + // Return the currently configured ELinuxRenderSurfaceTarget. + ELinuxRenderSurfaceTarget* GetRenderSurfaceTarget() const; + + // Returns the FlutterTransformation of this view. + FlutterTransformation GetRootSurfaceTransformation(); // Returns the engine backing this view. - FlutterLinuxesEngine* GetEngine(); + FlutterELinuxEngine* GetEngine(); + + // Returns the frame rate of the display. + int32_t GetFrameRate(); // Callbacks for clearing context, settings context and swapping buffers. void* ProcResolver(const char* name); bool MakeCurrent(); bool ClearCurrent(); bool Present(); + bool PresentWithInfo(const FlutterPresentInfo* info); + void PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage); uint32_t GetOnscreenFBO(); bool MakeResourceCurrent(); @@ -67,17 +86,19 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { void SendInitialBounds(); // |WindowBindingHandlerDelegate| - void OnWindowSizeChanged(size_t width, size_t height) const override; + void OnWindowSizeChanged(size_t width_px, size_t height_px) const override; // |WindowBindingHandlerDelegate| - void OnPointerMove(double x, double y) override; + void OnPointerMove(double x_px, double y_px) override; // |WindowBindingHandlerDelegate| - void OnPointerDown(double x, double y, + void OnPointerDown(double x_px, + double y_px, FlutterPointerMouseButtons button) override; // |WindowBindingHandlerDelegate| - void OnPointerUp(double x, double y, + void OnPointerUp(double x_px, + double y_px, FlutterPointerMouseButtons button) override; // |WindowBindingHandlerDelegate| @@ -99,8 +120,10 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { void OnKeyMap(uint32_t format, int fd, uint32_t size) override; // |WindowBindingHandlerDelegate| - void OnKeyModifiers(uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) override; + void OnKeyModifiers(uint32_t mods_depressed, + uint32_t mods_latched, + uint32_t mods_locked, + uint32_t group) override; // |WindowBindingHandlerDelegate| void OnKey(uint32_t key, bool pressed) override; @@ -112,9 +135,28 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { void OnVirtualSpecialKey(uint32_t keycode) override; // |WindowBindingHandlerDelegate| - void OnScroll(double x, double y, double delta_x, double delta_y, + void OnScroll(double x, + double y, + double delta_x, + double delta_y, int scroll_offset_multiplier) override; + // |WindowBindingHandlerDelegate| + void OnVsync(uint64_t frame_start_time_nanos, + uint64_t frame_target_time_nanos) override; + + // |WindowBindingHandlerDelegate| + void UpdateHighContrastEnabled(bool enabled) override; + + // |WindowBindingHandlerDelegate| + void UpdateTextScaleFactor(float factor) override; + + // |WindowBindingHandlerDelegate| + void UpdateDisplayInfo(double refresh_rate, + size_t width_px, + size_t height_px, + double pixel_ratio) override; + private: // Struct holding the mouse state. The engine doesn't keep track of which // mouse buttons have been pressed, so it's the embedding's responsibility. @@ -150,25 +192,39 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { }; struct touch_event { - uint32_t time; - touch_point points[10]; + touch_point points[10] = { + {false, -1, 0, 0, 0}, {false, -1, 0, 0, 0}, {false, -1, 0, 0, 0}, + {false, -1, 0, 0, 0}, {false, -1, 0, 0, 0}, {false, -1, 0, 0, 0}, + {false, -1, 0, 0, 0}, {false, -1, 0, 0, 0}, {false, -1, 0, 0, 0}, + {false, -1, 0, 0, 0}, + }; }; // touch_point* GgeTouchPoint(int32_t id); // Sends a window metrics update to the Flutter engine using current window - // dimensions in physical - void SendWindowMetrics(size_t width, size_t height, double dpiscale) const; + // dimensions in physical pixels. + // @param[in] width_px Physical width of the window. + // @param[in] height_px Physical height of the window. + void SendWindowMetrics(size_t width_px, + size_t height_px, + double dpiscale) const; // Reports a mouse movement to Flutter engine. - void SendPointerMove(double x, double y); + // @param[in] x_px The x coordinate of the pointer event in physical pixels. + // @param[in] y_px The y coordinate of the pointer event in physical pixels. + void SendPointerMove(double x_px, double y_px); // Reports mouse press to Flutter engine. - void SendPointerDown(double x, double y); + // @param[in] x_px The x coordinate of the pointer event in physical pixels. + // @param[in] y_px The y coordinate of the pointer event in physical pixels. + void SendPointerDown(double x_px, double y_px); // Reports mouse release to Flutter engine. - void SendPointerUp(double x, double y); + // @param[in] x_px The x coordinate of the pointer event in physical pixels. + // @param[in] y_px The y coordinate of the pointer event in physical pixels. + void SendPointerUp(double x_px, double y_px); // Reports mouse left the window client area. // @@ -178,7 +234,10 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { void SendPointerLeave(); // Reports scroll wheel events to Flutter engine. - void SendScroll(double x, double y, double delta_x, double delta_y, + void SendScroll(double x, + double y, + double delta_x, + double delta_y, int scroll_offset_multiplier); // Sets |event_data|'s phase to either kMove or kHover depending on the @@ -209,8 +268,13 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { // Updates the currently pressed buttons. void SetMouseButtons(uint64_t buttons) { mouse_state_.buttons = buttons; } + // Returns a trimmed pointer of user inputs with the window rotation. + // @param[in] x_px The x coordinate of the pointer event in physical pixels. + // @param[in] y_px The y coordinate of the pointer event in physical pixels. + std::pair GetPointerRotation(double x_px, double y_px); + // The engine associated with this view. - std::unique_ptr engine_; + std::unique_ptr engine_; // Keeps track of mouse state in relation to the window. MouseState mouse_state_; @@ -230,13 +294,32 @@ class FlutterLinuxesView : public WindowBindingHandlerDelegate { // Handler for cursor events. std::unique_ptr cursor_handler_; + // Handler for app lifecycle events. + std::unique_ptr lifecycle_handler_; + + // Handler for flutter/navigation channel. + std::unique_ptr navigation_handler_; + + // Handler for flutter/platform_views channel. + std::unique_ptr platform_views_handler_; + + // Handler for flutter/settings channel. + std::unique_ptr settings_handler_; + // Currently configured WindowBindingHandler for view. std::unique_ptr binding_handler_; // Current user touch event status. touch_event touch_event_; -}; + + // Current view rotation (degree). + uint16_t view_rotation_degree_ = 0; + + // Current view rotation (FlutterTransformation). + FlutterTransformation view_rotation_transformation_ = { + 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; +} SWIFT_UNSAFE_REFERENCE; } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_VIEW_H_ +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_ELINUX_VIEW_H_ diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_state.h b/src/flutter/shell/platform/linux_embedded/flutter_linuxes_state.h deleted file mode 100644 index b361e82f..00000000 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_state.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_STATE_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_STATE_H_ - -#include - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h" -#include "flutter/shell/platform/common/incoming_message_dispatcher.h" -#include "flutter/shell/platform/embedder/embedder.h" - -// Structs backing the opaque references used in the C API. -// -// DO NOT ADD ANY NEW CODE HERE. These are legacy, and are being phased out -// in favor of objects that own and manage the relevant functionality. - -namespace flutter { -struct FlutterLinuxesEngine; -struct FlutterLinuxesView; -} // namespace flutter - -// Wrapper to distinguish the view controller ref from the view ref given out -// in the C API. -struct FlutterDesktopViewControllerState { - // The view that backs this state object. - std::unique_ptr view; -}; - -// Wrapper to distinguish the plugin registrar ref from the engine ref given out -// in the C API. -struct FlutterDesktopPluginRegistrar { - // The engine that owns this state object. - flutter::FlutterLinuxesEngine* engine = nullptr; -}; - -// Wrapper to distinguish the messenger ref from the engine ref given out -// in the C API. -struct FlutterDesktopMessenger { - // The engine that owns this state object. - flutter::FlutterLinuxesEngine* engine = nullptr; -}; - -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_FLUTTER_LINUXES_STATE_H_ \ No newline at end of file diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.cc b/src/flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.cc deleted file mode 100644 index d1c8235a..00000000 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.cc +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_texture_registrar.h" - -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_engine.h" - -#include -#include - -namespace flutter { - -FlutterLinuxesTextureRegistrar::FlutterLinuxesTextureRegistrar( - FlutterLinuxesEngine* engine) - : engine_(engine) {} - -int64_t FlutterLinuxesTextureRegistrar::RegisterTexture( - const FlutterDesktopTextureInfo* texture_info) { - if (texture_info->type != kFlutterDesktopPixelBufferTexture) { - std::cerr << "Attempted to register texture of unsupport type." - << std::endl; - return -1; - } - - if (!texture_info->pixel_buffer_config.callback) { - std::cerr << "Invalid pixel buffer texture callback." << std::endl; - return -1; - } - - auto texture_gl = std::make_unique( - texture_info->pixel_buffer_config.callback, - texture_info->pixel_buffer_config.user_data); - int64_t texture_id = texture_gl->texture_id(); - - { - std::lock_guard lock(map_mutex_); - textures_[texture_id] = std::move(texture_gl); - } - - engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() { - engine->RegisterExternalTexture(texture_id); - }); - - return texture_id; -} - -bool FlutterLinuxesTextureRegistrar::UnregisterTexture(int64_t texture_id) { - { - std::lock_guard lock(map_mutex_); - auto it = textures_.find(texture_id); - if (it == textures_.end()) { - return false; - } - textures_.erase(it); - } - - engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() { - engine->UnregisterExternalTexture(texture_id); - }); - return true; -} - -bool FlutterLinuxesTextureRegistrar::MarkTextureFrameAvailable( - int64_t texture_id) { - engine_->task_runner()->RunNowOrPostTask([engine = engine_, texture_id]() { - engine->MarkExternalTextureFrameAvailable(texture_id); - }); - return true; -} - -bool FlutterLinuxesTextureRegistrar::PopulateTexture( - int64_t texture_id, - size_t width, - size_t height, - FlutterOpenGLTexture* opengl_texture) { - flutter::ExternalTextureGL* texture; - { - std::lock_guard lock(map_mutex_); - auto it = textures_.find(texture_id); - if (it == textures_.end()) { - return false; - } - texture = it->second.get(); - } - return texture->PopulateTexture(width, height, opengl_texture); -} - -}; // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_view.cc b/src/flutter/shell/platform/linux_embedded/flutter_linuxes_view.cc deleted file mode 100644 index 69f3b5f7..00000000 --- a/src/flutter/shell/platform/linux_embedded/flutter_linuxes_view.cc +++ /dev/null @@ -1,384 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/linux_embedded/flutter_linuxes_view.h" - -#include - -#include "flutter/shell/platform/linux_embedded/logger.h" - -namespace flutter { - -FlutterLinuxesView::FlutterLinuxesView( - std::unique_ptr window_binding) { - // Take the binding handler, and give it a pointer back to self. - binding_handler_ = std::move(window_binding); - binding_handler_->SetView(this); -} - -FlutterLinuxesView::~FlutterLinuxesView() { - // Need to stop running the Engine before destroying surface. - if (engine_) { - engine_->Stop(); - } - DestroyRenderSurface(); -} - -bool FlutterLinuxesView::DispatchEvent() { - return binding_handler_->DispatchEvent(); -} - -void FlutterLinuxesView::SetEngine( - std::unique_ptr engine) { - engine_ = std::move(engine); - - engine_->SetView(this); - - internal_plugin_registrar_ = - std::make_unique(engine_->GetRegistrar()); - - // Set up the system channel handlers. - auto internal_plugin_messenger = internal_plugin_registrar_->messenger(); - keyboard_handler_ = - std::make_unique(internal_plugin_messenger); - textinput_handler_ = std::make_unique( - internal_plugin_messenger, binding_handler_.get()); - platform_handler_ = std::make_unique( - internal_plugin_messenger, binding_handler_.get()); - cursor_handler_ = std::make_unique( - internal_plugin_messenger, binding_handler_.get()); - - PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); - SendWindowMetrics(bounds.width, bounds.height, - binding_handler_->GetDpiScale()); -} - -void FlutterLinuxesView::OnWindowSizeChanged(size_t width, - size_t height) const { - if (!GetRenderSurfaceTarget()->OnScreenSurfaceResize(width, height)) { - LINUXES_LOG(ERROR) << "Failed to change surface size."; - return; - } - SendWindowMetrics(width, height, binding_handler_->GetDpiScale()); -} - -void FlutterLinuxesView::OnPointerMove(double x, double y) { - SendPointerMove(x, y); -} - -void FlutterLinuxesView::OnPointerDown( - double x, double y, FlutterPointerMouseButtons flutter_button) { - if (flutter_button != 0) { - uint64_t mouse_buttons = mouse_state_.buttons | flutter_button; - SetMouseButtons(mouse_buttons); - SendPointerDown(x, y); - } -} - -void FlutterLinuxesView::OnPointerUp( - double x, double y, FlutterPointerMouseButtons flutter_button) { - if (flutter_button != 0) { - uint64_t mouse_buttons = mouse_state_.buttons & ~flutter_button; - SetMouseButtons(mouse_buttons); - SendPointerUp(x, y); - } -} - -void FlutterLinuxesView::OnPointerLeave() { SendPointerLeave(); } - -void FlutterLinuxesView::OnTouchDown(uint32_t time, int32_t id, double x, - double y) { - auto* point = GgeTouchPoint(id); - if (!point) { - return; - } - point->event_mask = TouchEvent::kDown; - point->x = x; - point->y = y; - touch_event_.time = time; - - FlutterPointerEvent event = { - .struct_size = sizeof(event), - .phase = FlutterPointerPhase::kDown, - .timestamp = time, - .x = x, - .y = y, - .device = kFlutterPointerDeviceKindTouch, - .signal_kind = kFlutterPointerSignalKindNone, - .scroll_delta_x = 0, - .scroll_delta_y = 0, - .device_kind = kFlutterPointerDeviceKindTouch, - .buttons = 0, - }; - engine_->SendPointerEvent(event); -} - -void FlutterLinuxesView::OnTouchUp(uint32_t time, int32_t id) { - auto* point = GgeTouchPoint(id); - if (!point) { - return; - } - point->event_mask = TouchEvent::kUp; - - FlutterPointerEvent event = { - .struct_size = sizeof(event), - .phase = FlutterPointerPhase::kUp, - .timestamp = time, - .x = point->x, - .y = point->y, - .device = kFlutterPointerDeviceKindTouch, - .signal_kind = kFlutterPointerSignalKindNone, - .scroll_delta_x = 0, - .scroll_delta_y = 0, - .device_kind = kFlutterPointerDeviceKindTouch, - .buttons = 0, - }; - engine_->SendPointerEvent(event); -} - -void FlutterLinuxesView::OnTouchMotion(uint32_t time, int32_t id, double x, - double y) { - auto* point = GgeTouchPoint(id); - if (!point) { - return; - } - point->event_mask = TouchEvent::kMotion; - point->x = x; - point->y = y; - touch_event_.time = time; - - FlutterPointerEvent event = { - .struct_size = sizeof(event), - .phase = FlutterPointerPhase::kMove, - .timestamp = time, - .x = x, - .y = y, - .device = kFlutterPointerDeviceKindTouch, - .signal_kind = kFlutterPointerSignalKindNone, - .scroll_delta_x = 0, - .scroll_delta_y = 0, - .device_kind = kFlutterPointerDeviceKindTouch, - .buttons = 0, - }; - engine_->SendPointerEvent(event); -} - -void FlutterLinuxesView::OnTouchCancel() {} - -void FlutterLinuxesView::OnKeyMap(uint32_t format, int fd, uint32_t size) { - keyboard_handler_->OnKeymap(format, fd, size); -} - -void FlutterLinuxesView::OnKey(uint32_t key, bool pressed) { - keyboard_handler_->OnKey(key, pressed); - if (pressed) { - auto code_point = keyboard_handler_->GetCodePoint(key); - if (!keyboard_handler_->IsTextInputSuppressed(code_point)) { - textinput_handler_->OnKeyPressed(key, code_point); - } - } -} - -void FlutterLinuxesView::OnKeyModifiers(uint32_t mods_depressed, - uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) { - keyboard_handler_->OnModifiers(mods_depressed, mods_latched, mods_locked, - group); -} - -void FlutterLinuxesView::OnVirtualKey(uint32_t code_point) { - // Since the keycode cannot be specified, set an invalid value(0). - constexpr uint32_t kCharKey = 0; - textinput_handler_->OnKeyPressed(kCharKey, code_point); -} - -void FlutterLinuxesView::OnVirtualSpecialKey(uint32_t keycode) { - auto code_point = keyboard_handler_->GetCodePoint(keycode); - textinput_handler_->OnKeyPressed(keycode, code_point); -} - -void FlutterLinuxesView::OnScroll(double x, double y, double delta_x, - double delta_y, - int scroll_offset_multiplier) { - SendScroll(x, y, delta_x, delta_y, scroll_offset_multiplier); -} - -FlutterLinuxesView::touch_point* FlutterLinuxesView::GgeTouchPoint(int32_t id) { - const size_t nmemb = sizeof(touch_event_) / sizeof(struct touch_point); - int invalid = -1; - for (size_t i = 0; i < nmemb; ++i) { - if (touch_event_.points[i].id == id) { - return &touch_event_.points[i]; - } - if (invalid == -1 && !touch_event_.points[i].valid) { - invalid = i; - } - } - if (invalid == -1) { - return nullptr; - } - touch_event_.points[invalid].valid = true; - touch_event_.points[invalid].id = id; - return &touch_event_.points[invalid]; -} - -// Sends new size information to FlutterEngine. -void FlutterLinuxesView::SendWindowMetrics(size_t width, size_t height, - double dpiScale) const { - FlutterWindowMetricsEvent event = {}; - event.struct_size = sizeof(event); - event.width = width; - event.height = height; - event.pixel_ratio = dpiScale; - engine_->SendWindowMetricsEvent(event); -} - -void FlutterLinuxesView::SendInitialBounds() { - PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); - SendWindowMetrics(bounds.width, bounds.height, - binding_handler_->GetDpiScale()); -} - -// Set's |event_data|'s phase to either kMove or kHover depending on the current -// primary mouse button state. -void FlutterLinuxesView::SetEventPhaseFromCursorButtonState( - FlutterPointerEvent* event_data) const { - // For details about this logic, see FlutterPointerPhase in the embedder.h - // file. - event_data->phase = - mouse_state_.buttons == 0 - ? mouse_state_.flutter_state_is_down ? FlutterPointerPhase::kUp - : FlutterPointerPhase::kHover - : mouse_state_.flutter_state_is_down ? FlutterPointerPhase::kMove - : FlutterPointerPhase::kDown; -} - -void FlutterLinuxesView::SendPointerMove(double x, double y) { - FlutterPointerEvent event = {}; - event.x = x; - event.y = y; - SetEventPhaseFromCursorButtonState(&event); - SendPointerEventWithData(event); -} - -void FlutterLinuxesView::SendPointerDown(double x, double y) { - FlutterPointerEvent event = {}; - SetEventPhaseFromCursorButtonState(&event); - event.x = x; - event.y = y; - SendPointerEventWithData(event); - SetMouseFlutterStateDown(true); -} - -void FlutterLinuxesView::SendPointerUp(double x, double y) { - FlutterPointerEvent event = {}; - SetEventPhaseFromCursorButtonState(&event); - event.x = x; - event.y = y; - SendPointerEventWithData(event); - if (event.phase == FlutterPointerPhase::kUp) { - SetMouseFlutterStateDown(false); - } -} - -void FlutterLinuxesView::SendPointerLeave() { - FlutterPointerEvent event = {}; - event.phase = FlutterPointerPhase::kRemove; - SendPointerEventWithData(event); -} - -void FlutterLinuxesView::SendScroll(double x, double y, double delta_x, - double delta_y, - int scroll_offset_multiplier) { - FlutterPointerEvent event = {}; - SetEventPhaseFromCursorButtonState(&event); - event.signal_kind = FlutterPointerSignalKind::kFlutterPointerSignalKindScroll; - event.x = x; - event.y = y; - event.scroll_delta_x = delta_x * scroll_offset_multiplier; - event.scroll_delta_y = delta_y * scroll_offset_multiplier; - SendPointerEventWithData(event); -} - -void FlutterLinuxesView::SendPointerEventWithData( - const FlutterPointerEvent& event_data) { - // If sending anything other than an add, and the pointer isn't already added, - // synthesize an add to satisfy Flutter's expectations about events. - if (!mouse_state_.flutter_state_is_added && - event_data.phase != FlutterPointerPhase::kAdd) { - FlutterPointerEvent event = {}; - event.phase = FlutterPointerPhase::kAdd; - event.x = event_data.x; - event.y = event_data.y; - event.buttons = 0; - SendPointerEventWithData(event); - } - // Don't double-add (e.g., if events are delivered out of order, so an add has - // already been synthesized). - if (mouse_state_.flutter_state_is_added && - event_data.phase == FlutterPointerPhase::kAdd) { - return; - } - - FlutterPointerEvent event = event_data; - event.device_kind = kFlutterPointerDeviceKindMouse; - event.buttons = mouse_state_.buttons; - - // Set metadata that's always the same regardless of the event. - event.struct_size = sizeof(event); - event.timestamp = - std::chrono::duration_cast( - std::chrono::high_resolution_clock::now().time_since_epoch()) - .count(); - - engine_->SendPointerEvent(event); - - if (event_data.phase == FlutterPointerPhase::kAdd) { - SetMouseFlutterStateAdded(true); - } else if (event_data.phase == FlutterPointerPhase::kRemove) { - SetMouseFlutterStateAdded(false); - ResetMouseState(); - } -} - -void* FlutterLinuxesView::ProcResolver(const char* name) { - return GetRenderSurfaceTarget()->GlProcResolver(name); -} - -bool FlutterLinuxesView::MakeCurrent() { - return GetRenderSurfaceTarget()->GLContextMakeCurrent(); -} - -bool FlutterLinuxesView::ClearCurrent() { - return GetRenderSurfaceTarget()->GLContextClearCurrent(); -} - -bool FlutterLinuxesView::Present() { - return GetRenderSurfaceTarget()->GLContextPresent(0); -} - -uint32_t FlutterLinuxesView::GetOnscreenFBO() { - return GetRenderSurfaceTarget()->GLContextFBO(); -} - -bool FlutterLinuxesView::MakeResourceCurrent() { - return GetRenderSurfaceTarget()->ResourceContextMakeCurrent(); -} - -bool FlutterLinuxesView::CreateRenderSurface() { - PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); - return binding_handler_->CreateRenderSurface(bounds.width, bounds.height); -} - -void FlutterLinuxesView::DestroyRenderSurface() { - binding_handler_->DestroyRenderSurface(); -} - -LinuxesRenderSurfaceTarget* FlutterLinuxesView::GetRenderSurfaceTarget() const { - return binding_handler_->GetRenderSurfaceTarget(); -} - -FlutterLinuxesEngine* FlutterLinuxesView::GetEngine() { return engine_.get(); } - -} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.cc b/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.cc index 1ffbf152..31681244 100644 --- a/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.cc +++ b/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.cc @@ -47,7 +47,7 @@ FlutterProjectBundle::FlutterProjectBundle( aot_library_path_.compare(0, 1, "/") != 0)) { auto executable_location = GetExecutableDirectory(); if (executable_location.empty()) { - LINUXES_LOG(ERROR) + ELINUX_LOG(ERROR) << "Unable to find executable location to resolve resource paths."; } else { assets_path_ = executable_location + "/" + assets_path_; @@ -68,7 +68,7 @@ bool FlutterProjectBundle::HasValidPaths() { UniqueAotDataPtr FlutterProjectBundle::LoadAotData( const FlutterEngineProcTable& engine_procs) { if (aot_library_path_.empty()) { - LINUXES_LOG(ERROR) + ELINUX_LOG(ERROR) << "Attempted to load AOT data, but no aot_library_path was provided."; return nullptr; } @@ -79,14 +79,29 @@ UniqueAotDataPtr FlutterProjectBundle::LoadAotData( FlutterEngineAOTData data = nullptr; auto result = engine_procs.CreateAOTData(&source, &data); if (result != kSuccess) { - LINUXES_LOG(ERROR) << "Failed to load AOT data from: " << aot_library_path_; + ELINUX_LOG(ERROR) << "Failed to load AOT data from: " << aot_library_path_; return nullptr; } return UniqueAotDataPtr(data); } +void FlutterProjectBundle::SetSwitches( + const std::vector& switches) { + engine_switches_ = switches; +} + const std::vector FlutterProjectBundle::GetSwitches() { - return GetSwitchesFromEnvironment(); + if (engine_switches_.size() == 0) { + return GetSwitchesFromEnvironment(); + } + std::vector switches; + switches.insert(switches.end(), engine_switches_.begin(), + engine_switches_.end()); + + auto env_switches = GetSwitchesFromEnvironment(); + switches.insert(switches.end(), env_switches.begin(), env_switches.end()); + + return switches; } const std::string FlutterProjectBundle::GetExecutableDirectory() { diff --git a/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.h b/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.h index 0e014066..503fcd38 100644 --- a/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.h +++ b/src/flutter/shell/platform/linux_embedded/flutter_project_bundle.h @@ -10,7 +10,7 @@ #include #include "flutter/shell/platform/embedder/embedder.h" -#include "flutter/shell/platform/linux_embedded/public/flutter_linuxes.h" +#include "flutter/shell/platform/linux_embedded/public/flutter_elinux.h" namespace flutter { @@ -45,6 +45,9 @@ class FlutterProjectBundle { // Returns any switches that should be passed to the engine. const std::vector GetSwitches(); + // Sets engine switches. + void SetSwitches(const std::vector& switches); + // Attempts to load AOT data for this bundle. The returned data must be // retained until any engine instance it is passed to has been shut down. // @@ -69,6 +72,9 @@ class FlutterProjectBundle { // Dart entrypoint arguments. std::vector dart_entrypoint_arguments_; + + // Engine switches. + std::vector engine_switches_; }; } // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/logger.cc b/src/flutter/shell/platform/linux_embedded/logger.cc index a59125e2..200230ae 100644 --- a/src/flutter/shell/platform/linux_embedded/logger.cc +++ b/src/flutter/shell/platform/linux_embedded/logger.cc @@ -4,44 +4,79 @@ #include "logger.h" +#include +#include + namespace flutter { namespace { -#ifdef FLUTTER_RELEASE -constexpr int kFilterLogLevel = LINUXES_LOG_WARNING; -#else -constexpr int kFilterLogLevel = LINUXES_LOG_TRACE; -#endif +constexpr char kFlutterLogLevelsEnvironmentKey[] = "FLUTTER_LOG_LEVELS"; +constexpr char kFlutterLogLevelTrace[] = "TRACE"; +constexpr char kFlutterLogLevelDebug[] = "DEBUG"; +constexpr char kFlutterLogLevelInfo[] = "INFO"; +constexpr char kFlutterLogLevelWarning[] = "WARNING"; +constexpr char kFlutterLogLevelError[] = "ERROR"; +constexpr char kFlutterLogLevelFatal[] = "FATAL"; +constexpr char kFlutterLogLevelUnknown[] = "UNKNOWN"; + +const char* const kLogLevelNames[ELINUX_LOG_NUM] = { + kFlutterLogLevelTrace, kFlutterLogLevelDebug, kFlutterLogLevelInfo, + kFlutterLogLevelWarning, kFlutterLogLevelError, kFlutterLogLevelFatal}; -const char* const kLogLevelNames[LINUXES_LOG_NUM] = {"TRACE", "INFO", "WARNING", - "ERROR", "FATAL"}; +const std::unordered_map gLogLevelsMap{ + {kFlutterLogLevelTrace, ELINUX_LOG_TRACE}, + {kFlutterLogLevelDebug, ELINUX_LOG_DEBUG}, + {kFlutterLogLevelInfo, ELINUX_LOG_INFO}, + {kFlutterLogLevelWarning, ELINUX_LOG_WARNING}, + {kFlutterLogLevelError, ELINUX_LOG_ERROR}, + {kFlutterLogLevelFatal, ELINUX_LOG_FATAL}, +}; + +int gFilterLogLevel = -1; + +int GetCurrentLogLevel() { + if (gFilterLogLevel == -1) { + auto env_log_level = std::getenv(kFlutterLogLevelsEnvironmentKey); + if (!env_log_level || (env_log_level[0] == '\0')) { + gFilterLogLevel = ELINUX_LOG_WARNING; + } else { + if (gLogLevelsMap.find(env_log_level) != gLogLevelsMap.end()) { + gFilterLogLevel = gLogLevelsMap.at(env_log_level); + } else { + gFilterLogLevel = ELINUX_LOG_WARNING; + } + } + } + return gFilterLogLevel; +} const char* GetLogLevelName(int level) { - if (LINUXES_LOG_TRACE <= level && level < LINUXES_LOG_NUM) + if (ELINUX_LOG_TRACE <= level && level < ELINUX_LOG_NUM) return kLogLevelNames[level]; - return "UNKNOWN"; + return kFlutterLogLevelUnknown; } } // namespace Logger::Logger(int level, const char* file, int line) : level_(level) { - if (level_ < kFilterLogLevel) { + if (level_ < GetCurrentLogLevel()) { return; } + stream_ << "[" << GetLogLevelName(level_) << "]"; stream_ << "[" << file << "(" << line << ")] "; } Logger::~Logger() { - if (level_ < kFilterLogLevel) { + if (level_ < GetCurrentLogLevel()) { return; } stream_ << std::endl; std::cerr << stream_.str(); std::cerr.flush(); - if (level_ >= LINUXES_LOG_FATAL) { + if (level_ >= ELINUX_LOG_FATAL) { abort(); } } diff --git a/src/flutter/shell/platform/linux_embedded/logger.h b/src/flutter/shell/platform/linux_embedded/logger.h index bebf0667..e3666aba 100644 --- a/src/flutter/shell/platform/linux_embedded/logger.h +++ b/src/flutter/shell/platform/linux_embedded/logger.h @@ -12,18 +12,29 @@ namespace flutter { -constexpr int LINUXES_LOG_TRACE = 0; -constexpr int LINUXES_LOG_INFO = 1; -constexpr int LINUXES_LOG_WARNING = 2; -constexpr int LINUXES_LOG_ERROR = 3; -constexpr int LINUXES_LOG_FATAL = 4; -constexpr int LINUXES_LOG_NUM = 5; - +constexpr int ELINUX_LOG_TRACE = 0; +constexpr int ELINUX_LOG_DEBUG = 1; +constexpr int ELINUX_LOG_INFO = 2; +constexpr int ELINUX_LOG_WARNING = 3; +constexpr int ELINUX_LOG_ERROR = 4; +constexpr int ELINUX_LOG_FATAL = 5; +constexpr int ELINUX_LOG_NUM = 6; + +#if defined(ENABLE_ELINUX_EMBEDDER_LOG) +#if defined(NDEBUG) +// We don't use __FILE__ macro with release build. +#define ELINUX_LOG(level) \ + Logger(ELINUX_LOG_##level, __FUNCTION__, __LINE__).stream() +#else #define __LOG_FILE_NAME__ \ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) -#define LINUXES_LOG(level) \ - Logger(LINUXES_LOG_##level, __LOG_FILE_NAME__, __LINE__).stream() +#define ELINUX_LOG(level) \ + Logger(ELINUX_LOG_##level, __LOG_FILE_NAME__, __LINE__).stream() +#endif +#else +#define ELINUX_LOG(level) Logger(-1, "", 0).stream() +#endif class Logger { public: diff --git a/src/flutter/shell/platform/linux_embedded/plugin/key_event_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/key_event_plugin.cc similarity index 90% rename from src/flutter/shell/platform/linux_embedded/plugin/key_event_plugin.cc rename to src/flutter/shell/platform/linux_embedded/plugins/key_event_plugin.cc index d3be410a..dff808e2 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/key_event_plugin.cc +++ b/src/flutter/shell/platform/linux_embedded/plugins/key_event_plugin.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/plugin//key_event_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/key_event_plugin.h" #include #include @@ -13,8 +13,7 @@ #include #include "flutter/shell/platform/common/json_message_codec.h" -#include "flutter/shell/platform/linux_embedded/logger.h" -#include "flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.h" +#include "flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.h" #include "flutter/shell/platform/linux_embedded/window_binding_handler_delegate.h" namespace flutter { @@ -42,7 +41,9 @@ constexpr char kXkboptionsKey[] = "XKBOPTIONS"; KeyeventPlugin::KeyeventPlugin(BinaryMessenger* messenger) : channel_(std::make_unique>( - messenger, kChannelName, &flutter::JsonMessageCodec::GetInstance())), + messenger, + kChannelName, + &flutter::JsonMessageCodec::GetInstance())), xkb_context_(xkb_context_new(XKB_CONTEXT_NO_FLAGS)) { #if defined(DISPLAY_BACKEND_TYPE_WAYLAND) xkb_keymap_ = nullptr; @@ -106,16 +107,20 @@ void KeyeventPlugin::OnKey(uint32_t keycode, bool pressed) { SendKeyEvent(keyscancode, unicode, mods, pressed); } -void KeyeventPlugin::OnModifiers(uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) { +void KeyeventPlugin::OnModifiers(uint32_t mods_depressed, + uint32_t mods_latched, + uint32_t mods_locked, + uint32_t group) { xkb_state_update_mask(xkb_state_, mods_depressed, mods_latched, mods_locked, 0, 0, group); xkb_mods_mask_ = xkb_state_serialize_mods(xkb_state_, XKB_STATE_MODS_EFFECTIVE); } -void KeyeventPlugin::SendKeyEvent(uint32_t keycode, uint32_t unicode, - uint32_t modifiers, bool pressed) { +void KeyeventPlugin::SendKeyEvent(uint32_t keycode, + uint32_t unicode, + uint32_t modifiers, + bool pressed) { rapidjson::Document event(rapidjson::kObjectType); auto& allocator = event.GetAllocator(); event.AddMember(kKeyCodeKey, keycode, allocator); diff --git a/src/flutter/shell/platform/linux_embedded/plugin/key_event_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/key_event_plugin.h similarity index 71% rename from src/flutter/shell/platform/linux_embedded/plugin/key_event_plugin.h rename to src/flutter/shell/platform/linux_embedded/plugins/key_event_plugin.h index b71ce937..d7e4fe47 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/key_event_plugin.h +++ b/src/flutter/shell/platform/linux_embedded/plugins/key_event_plugin.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_KEY_EVENT_PLUGIN_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_KEY_EVENT_PLUGIN_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_KEY_EVENT_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_KEY_EVENT_PLUGIN_H_ #include #include @@ -25,15 +25,19 @@ class KeyeventPlugin { void OnKey(uint32_t keycode, bool pressed); - void OnModifiers(uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group); + void OnModifiers(uint32_t mods_depressed, + uint32_t mods_latched, + uint32_t mods_locked, + uint32_t group); uint32_t GetCodePoint(uint32_t keycode); bool IsTextInputSuppressed(uint32_t code_point); private: - void SendKeyEvent(uint32_t keycode, uint32_t unicode, uint32_t modifiers, + void SendKeyEvent(uint32_t keycode, + uint32_t unicode, + uint32_t modifiers, bool pressed); void OnModifiers(uint32_t keycode, bool pressed); xkb_keymap* CreateKeymap(xkb_context* context); @@ -49,4 +53,4 @@ class KeyeventPlugin { } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_KEY_EVENT_PLUGIN_H_ \ No newline at end of file +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_KEY_EVENT_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.cc b/src/flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.cc similarity index 98% rename from src/flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.cc rename to src/flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.cc index ad463668..c71377af 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.cc +++ b/src/flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.cc @@ -2,10 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.h" +#include "flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.h" #include -#include #include @@ -309,7 +308,7 @@ uint32_t GetGlfwKeycode(uint32_t xkb_keycode) { keycode_to_glfwkey_map.end()) { return keycode_to_glfwkey_map.at(xkb_keycode); } - LINUXES_LOG(ERROR) << "Unknown keycode: " << xkb_keycode; + ELINUX_LOG(ERROR) << "Unknown keycode: " << xkb_keycode; return xkb_keycode; } diff --git a/src/flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.h b/src/flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.h similarity index 66% rename from src/flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.h rename to src/flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.h index e07cbc46..06b133f0 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/keyboard_glfw_util.h +++ b/src/flutter/shell/platform/linux_embedded/plugins/keyboard_glfw_util.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_KEYBOARD_GLFW_UTIL_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_KEYBOARD_GLFW_UTIL_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_KEYBOARD_GLFW_UTIL_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_KEYBOARD_GLFW_UTIL_H_ #include @@ -17,4 +17,4 @@ uint32_t GetGlfwKeycode(uint32_t xkb_keycode); } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_KEYBOARD_GLFW_UTIL_H_ +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_KEYBOARD_GLFW_UTIL_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.cc new file mode 100644 index 00000000..002457db --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.cc @@ -0,0 +1,53 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.h" + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/string_message_codec.h" +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +namespace { +constexpr char kChannelName[] = "flutter/lifecycle"; +constexpr char kInactive[] = "AppLifecycleState.inactive"; +constexpr char kResumed[] = "AppLifecycleState.resumed"; +constexpr char kPaused[] = "AppLifecycleState.paused"; +constexpr char kDetached[] = "AppLifecycleState.detached"; +constexpr char kHidden[] = "AppLifecycleState.hidden"; + +} // namespace + +LifecyclePlugin::LifecyclePlugin(BinaryMessenger* messenger) + : channel_(std::make_unique>( + messenger, + kChannelName, + &StringMessageCodec::GetInstance())) {} + +void LifecyclePlugin::OnInactive() const { + ELINUX_LOG(DEBUG) << "App lifecycle changed to inactive state."; + channel_->Send(std::string(kInactive)); +} + +void LifecyclePlugin::OnResumed() const { + ELINUX_LOG(DEBUG) << "App lifecycle changed to resumed state."; + channel_->Send(std::string(kResumed)); +} + +void LifecyclePlugin::OnPaused() const { + ELINUX_LOG(DEBUG) << "App lifecycle changed to paused state."; + channel_->Send(std::string(kPaused)); +} + +void LifecyclePlugin::OnDetached() const { + ELINUX_LOG(DEBUG) << "App lifecycle changed to detached state."; + channel_->Send(std::string(kDetached)); +} + +void LifecyclePlugin::OnHidden() const { + ELINUX_LOG(DEBUG) << "App lifecycle changed to hidden state."; + channel_->Send(std::string(kHidden)); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.h new file mode 100644 index 00000000..784fbd61 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/lifecycle_plugin.h @@ -0,0 +1,36 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_LIFECYCLE_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_LIFECYCLE_PLUGIN_H_ + +#include + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" + +namespace flutter { + +class LifecyclePlugin { + public: + LifecyclePlugin(BinaryMessenger* messenger); + ~LifecyclePlugin() = default; + + void OnInactive() const; + + void OnResumed() const; + + void OnPaused() const; + + void OnDetached() const; + + void OnHidden() const; + + private: + std::unique_ptr> channel_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_LIFECYCLE_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.cc similarity index 92% rename from src/flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.cc rename to src/flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.cc index 931dd0e4..af695d45 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.cc +++ b/src/flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.h" #include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h" @@ -31,7 +31,9 @@ constexpr char kKindKey[] = "kind"; MouseCursorPlugin::MouseCursorPlugin(BinaryMessenger* messenger, WindowBindingHandler* delegate) : channel_(std::make_unique>( - messenger, kChannelName, &StandardMethodCodec::GetInstance())), + messenger, + kChannelName, + &StandardMethodCodec::GetInstance())), delegate_(delegate) { channel_->SetMethodCallHandler( [this](const MethodCall& call, diff --git a/src/flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.h similarity index 84% rename from src/flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.h rename to src/flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.h index 4dc28784..174947b7 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/mouse_cursor_plugin.h +++ b/src/flutter/shell/platform/linux_embedded/plugins/mouse_cursor_plugin.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_MOUSE_CURSOR_PLUGIN_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_MOUSE_CURSOR_PLUGIN_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_MOUSE_CURSOR_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_MOUSE_CURSOR_PLUGIN_H_ #include @@ -36,4 +36,4 @@ class MouseCursorPlugin { } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_MOUSE_CURSOR_PLUGIN_H_ \ No newline at end of file +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_MOUSE_CURSOR_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.cc new file mode 100644 index 00000000..e23b09c4 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.cc @@ -0,0 +1,55 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/plugins/navigation_plugin.h" + +#include "flutter/shell/platform/common/json_method_codec.h" +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +namespace { +constexpr char kChannelName[] = "flutter/navigation"; + +constexpr char kSetInitialRouteMethod[] = "setInitialRoute"; +constexpr char kPushRouteMethod[] = "pushRoute"; +constexpr char kPopRouteMethod[] = "popRoute"; +} // namespace + +NavigationPlugin::NavigationPlugin(BinaryMessenger* messenger) + : channel_(std::make_unique>( + messenger, + kChannelName, + &flutter::JsonMethodCodec::GetInstance())) {} + +void NavigationPlugin::SetInitialRoute(std::string route) const { + ELINUX_LOG(DEBUG) << "SetInitialRoute = " << route; + + auto args = std::make_unique(rapidjson::kObjectType); + args->Parse("\"" + route + "\""); + if (args->HasParseError()) { + ELINUX_LOG(ERROR) << "Failed to parse the initial route: " << route; + return; + } + channel_->InvokeMethod(kSetInitialRouteMethod, std::move(args)); +} + +void NavigationPlugin::PushRoute(std::string route) const { + ELINUX_LOG(DEBUG) << "PushRoute = " << route; + + auto args = std::make_unique(rapidjson::kObjectType); + args->Parse("\"" + route + "\""); + if (args->HasParseError()) { + ELINUX_LOG(ERROR) << "Failed to parse the route: " << route; + return; + } + channel_->InvokeMethod(kPushRouteMethod, std::move(args)); +} + +void NavigationPlugin::PopRoute() const { + ELINUX_LOG(DEBUG) << "PopRoute"; + channel_->InvokeMethod(kPopRouteMethod, nullptr); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.h new file mode 100644 index 00000000..4d7ac29b --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/navigation_plugin.h @@ -0,0 +1,35 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_NAVIGATION_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_NAVIGATION_PLUGIN_H_ + +#include + +#include +#include + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" + +namespace flutter { + +class NavigationPlugin { + public: + NavigationPlugin(BinaryMessenger* messenger); + ~NavigationPlugin() = default; + + void SetInitialRoute(std::string initial_route) const; + + void PushRoute(std::string route) const; + + void PopRoute() const; + + private: + std::unique_ptr> channel_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_NAVIGATION_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugin/platform_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.cc similarity index 95% rename from src/flutter/shell/platform/linux_embedded/plugin/platform_plugin.cc rename to src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.cc index 64651a7f..7670c157 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/platform_plugin.cc +++ b/src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/plugin/platform_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/platform_plugin.h" #include "flutter/shell/platform/common/json_method_codec.h" @@ -37,7 +37,9 @@ constexpr char kUnknownClipboardFormatError[] = PlatformPlugin::PlatformPlugin(BinaryMessenger* messenger, WindowBindingHandler* delegate) : channel_(std::make_unique>( - messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())), + messenger, + kChannelName, + &flutter::JsonMethodCodec::GetInstance())), delegate_(delegate) { channel_->SetMethodCallHandler( [this]( diff --git a/src/flutter/shell/platform/linux_embedded/plugin/platform_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.h similarity index 83% rename from src/flutter/shell/platform/linux_embedded/plugin/platform_plugin.h rename to src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.h index 4f45763a..4f0eb82a 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/platform_plugin.h +++ b/src/flutter/shell/platform/linux_embedded/plugins/platform_plugin.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_PLATFORM_PLUGIN_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_PLATFORM_PLUGIN_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_PLATFORM_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_PLATFORM_PLUGIN_H_ #include @@ -35,4 +35,4 @@ class PlatformPlugin { } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_PLATFORM_PLUGIN_H_ \ No newline at end of file +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_PLATFORM_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.cc new file mode 100644 index 00000000..fb2a513e --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.cc @@ -0,0 +1,265 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.h" + +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +namespace { +constexpr char kChannelName[] = "flutter/platform_views"; + +constexpr char kCreateMethod[] = "create"; +constexpr char kDisposeMethod[] = "dispose"; +constexpr char kResizeMethod[] = "resize"; +constexpr char kSetDirectionMethod[] = "setDirection"; +constexpr char kClearFocusMethod[] = "clearFocus"; +constexpr char kTouchMethod[] = "touch"; +constexpr char kAcceptGestureMethod[] = "acceptGesture"; +constexpr char kRejectGestureMethod[] = "rejectGesture"; +constexpr char kEnterMethod[] = "enter"; +constexpr char kExitMethod[] = "exit"; +constexpr char kOffsetMethod[] = "offset"; + +constexpr char kViewTypeKey[] = "viewType"; +constexpr char kIdKey[] = "id"; +constexpr char kWidthKey[] = "width"; +constexpr char kHeightKey[] = "height"; +constexpr char kParamsKey[] = "params"; + +constexpr char kTopKey[] = "top"; +constexpr char kLeftKey[] = "left"; +} // namespace + +PlatformViewsPlugin::PlatformViewsPlugin(BinaryMessenger* messenger) + : channel_( + std::make_unique>( + messenger, + kChannelName, + &flutter::StandardMethodCodec::GetInstance())), + current_view_id_(-1) { + channel_->SetMethodCallHandler( + [this](const flutter::MethodCall& call, + std::unique_ptr> + result) { HandleMethodCall(call, std::move(result)); }); +} + +PlatformViewsPlugin::~PlatformViewsPlugin() { + for (auto& itr : platform_views_) { + delete itr.second; + } + platform_views_.clear(); + + for (auto& itr : platform_view_factories_) { + itr.second->Dispose(); + } + platform_view_factories_.clear(); +} + +void PlatformViewsPlugin::RegisterViewFactory( + const char* view_type, + std::unique_ptr factory) { + if (platform_view_factories_.find(view_type) != + platform_view_factories_.end()) { + ELINUX_LOG(ERROR) << "Platform Views factory is already registered: " + << view_type; + return; + } + ELINUX_LOG(DEBUG) << view_type << " was registered"; + platform_view_factories_[view_type] = std::move(factory); +} + +void PlatformViewsPlugin::HandleMethodCall( + const flutter::MethodCall& method_call, + std::unique_ptr> result) { + const auto& method = method_call.method_name(); + const auto& arguments = *method_call.arguments(); + + // todo: support all of the methods on the platform views. + if (method.compare(kCreateMethod) == 0) { + PlatformViewsCreate(arguments, std::move(result)); + } else if (method.compare(kDisposeMethod) == 0) { + PlatformViewsDispose(arguments, std::move(result)); + } else if (method.compare(kResizeMethod) == 0) { + PlatformViewsResize(arguments, std::move(result)); + } else if (method.compare(kSetDirectionMethod) == 0) { + result->NotImplemented(); + } else if (method.compare(kClearFocusMethod) == 0) { + PlatformViewsClearFocus(arguments, std::move(result)); + } else if (method.compare(kTouchMethod) == 0) { + PlatformViewsTouch(arguments, std::move(result)); + } else if (method.compare(kAcceptGestureMethod) == 0) { + result->NotImplemented(); + } else if (method.compare(kRejectGestureMethod) == 0) { + result->NotImplemented(); + } else if (method.compare(kEnterMethod) == 0) { + result->NotImplemented(); + } else if (method.compare(kExitMethod) == 0) { + result->NotImplemented(); + } else if (method.compare(kOffsetMethod) == 0) { + PlatformViewsOffset(arguments, std::move(result)); + } else { + ELINUX_LOG(WARNING) << "Platform Views unexpected method is called: " + << method; + result->NotImplemented(); + } +} + +void PlatformViewsPlugin::PlatformViewsCreate( + const flutter::EncodableValue& arguments, + std::unique_ptr> result) { + auto view_type = LookupEncodableMap(arguments, kViewTypeKey); + if (view_type.empty()) { + result->Error("Couldn't find the view type in the arguments"); + return; + } + auto view_id = LookupEncodableMap(arguments, kIdKey); + auto view_width = LookupEncodableMap(arguments, kWidthKey); + if (!view_width) { + result->Error("Couldn't find width in the arguments"); + return; + } + auto view_height = LookupEncodableMap(arguments, kHeightKey); + if (!view_height) { + result->Error("Couldn't find height in the arguments"); + return; + } + ELINUX_LOG(DEBUG) << "Create the platform view: view_type = " << view_type + << ", id = " << view_id << ", width = " << view_width + << ", height = " << view_height; + + if (platform_view_factories_.find(view_type) == + platform_view_factories_.end()) { + result->Error("Couldn't find the view type"); + return; + } + + auto params = LookupEncodableMap>(arguments, kParamsKey); + auto view = platform_view_factories_[view_type]->Create(view_id, view_width, + view_height, params); + if (view) { + platform_views_[view_id] = view; + if (platform_views_.find(current_view_id_) != platform_views_.end()) { + platform_views_[current_view_id_]->SetFocus(false); + } + current_view_id_ = view_id; + + result->Success(flutter::EncodableValue(view->GetTextureId())); + } else { + result->Error("Failed to create a platform view"); + } +} + +void PlatformViewsPlugin::PlatformViewsDispose( + const flutter::EncodableValue& arguments, + std::unique_ptr> result) { + auto view_id = LookupEncodableMap(arguments, kIdKey); + ELINUX_LOG(DEBUG) << "Dispose the platform view: id = " << view_id; + if (platform_views_.find(view_id) == platform_views_.end()) { + result->Error("Couldn't find the view id in the arguments"); + return; + } + platform_views_[view_id]->Dispose(); + result->Success(); +} + +void PlatformViewsPlugin::PlatformViewsClearFocus( + const flutter::EncodableValue& arguments, + std::unique_ptr> result) { + auto view_id = LookupEncodableMap(arguments, kIdKey); + ELINUX_LOG(DEBUG) << "ClearFocus the platform view: id = " << view_id; + if (platform_views_.find(view_id) == platform_views_.end()) { + result->Error("Couldn't find the view id in the arguments"); + return; + } + platform_views_[view_id]->SetFocus(false); + platform_views_[view_id]->ClearFocus(); + + result->Success(); +} + +void PlatformViewsPlugin::PlatformViewsResize( + const flutter::EncodableValue& arguments, + std::unique_ptr> result) { + auto view_id = LookupEncodableMap(arguments, kIdKey); + auto view_width = LookupEncodableMap(arguments, kWidthKey); + if (!view_width) { + result->Error("Couldn't find width in the arguments"); + return; + } + auto view_height = LookupEncodableMap(arguments, kHeightKey); + if (!view_height) { + result->Error("Couldn't find height in the arguments"); + return; + } + + ELINUX_LOG(DEBUG) << "Resize the platform view: id = " << view_id + << ", width = " << view_width + << ", height = " << view_height; + if (platform_views_.find(view_id) == platform_views_.end()) { + result->Error("Couldn't find the view id in the arguments"); + return; + } + + if (!view_width || !view_height) { + result->Error("width and height must be greater than zero"); + return; + } + + platform_views_[view_id]->Resize(view_width, view_height); + + result->Success(arguments); +} + +void PlatformViewsPlugin::PlatformViewsTouch( + const flutter::EncodableValue& arguments, + std::unique_ptr> result) { + auto list = std::get(arguments); + auto view_id = std::get(list[0]); + auto event_type = std::get(list[3]); + auto device_id = std::get(list[11]); + + auto pointer_coords = std::get(list[6]); + if (pointer_coords.size() < 1) { + result->Error("Couldn't find the pointer_coords in the arguments"); + return; + } + auto pointer_coord = std::get(pointer_coords[0]); + auto x = std::get(pointer_coord[7]); + auto y = std::get(pointer_coord[8]); + + ELINUX_LOG(TRACE) << "Touch the platform view: id = " << view_id + << ", device_id = " << device_id + << ", event_type = " << event_type << ", x = " << x + << ", y = " << y; + if (platform_views_.find(view_id) == platform_views_.end()) { + result->Error("Couldn't find the view id in the arguments"); + return; + } + + platform_views_[view_id]->Touch(device_id, event_type, x, y); + + result->Success(); +} + +void PlatformViewsPlugin::PlatformViewsOffset( + const flutter::EncodableValue& arguments, + std::unique_ptr> result) { + auto view_id = LookupEncodableMap(arguments, kIdKey); + auto view_top = LookupEncodableMap(arguments, kTopKey); + auto view_left = LookupEncodableMap(arguments, kLeftKey); + ELINUX_LOG(DEBUG) << "Offset the platform view: " << "id = " << view_id + << ", top = " << view_top << ", left = " << view_left; + if (platform_views_.find(view_id) == platform_views_.end()) { + result->Error("Couldn't find the view id in the arguments"); + return; + } + + platform_views_[view_id]->Offset(view_top, view_left); + + result->Success(); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.h new file mode 100644 index 00000000..8c17e078 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/platform_views_plugin.h @@ -0,0 +1,92 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_PLATFORM_VIEWS_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_PLATFORM_VIEWS_PLUGIN_H_ + +#include +#include + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h" +#include "flutter/shell/platform/linux_embedded/public/flutter_platform_views.h" + +namespace flutter { + +class PlatformViewsPlugin { + public: + PlatformViewsPlugin(BinaryMessenger* messenger); + ~PlatformViewsPlugin(); + + // Registers a factory of the platform view. + void RegisterViewFactory( + const char* view_type, + std::unique_ptr factory); + + private: + // Called when a method is called on |channel_|; + void HandleMethodCall( + const flutter::MethodCall& method_call, + std::unique_ptr> result); + + // Decodes and gets a value from an argument of MethodCall. + template + T LookupEncodableMap(const flutter::EncodableValue& map, const char* key) { + auto values = std::get(map); + auto value = values[flutter::EncodableValue(key)]; + if (!std::holds_alternative(value)) { + return T(); + } + return std::get(value); + } + + // Called when "create" method is called + void PlatformViewsCreate( + const flutter::EncodableValue& arguments, + std::unique_ptr> result); + + // Called when "dispose" method is called + void PlatformViewsDispose( + const flutter::EncodableValue& arguments, + std::unique_ptr> result); + + // Called when "clearFocus" method is called + void PlatformViewsClearFocus( + const flutter::EncodableValue& arguments, + std::unique_ptr> result); + + // Called when "resize" method is called + void PlatformViewsResize( + const flutter::EncodableValue& arguments, + std::unique_ptr> result); + + // Called when "touch" method is called + void PlatformViewsTouch( + const flutter::EncodableValue& arguments, + std::unique_ptr> result); + + // Called when "offset" method is called + void PlatformViewsOffset( + const flutter::EncodableValue& arguments, + std::unique_ptr> result); + + // Method channel instance. + std::unique_ptr> channel_; + + // Factory of PlatformView class. + std::unordered_map> + platform_view_factories_; + + // Instances of platform views. + std::unordered_map platform_views_; + + // Shows the id of current view. + int current_view_id_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_PLATFORM_VIEWS_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.cc new file mode 100644 index 00000000..7603bfe9 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.cc @@ -0,0 +1,76 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/plugins/settings_plugin.h" + +#include "flutter/shell/platform/common/json_message_codec.h" + +namespace flutter { + +namespace { +constexpr char kChannelName[] = "flutter/settings"; + +constexpr char kAlwaysUse24HourFormat[] = "alwaysUse24HourFormat"; +constexpr char kTextScaleFactor[] = "textScaleFactor"; +constexpr char kPlatformBrightness[] = "platformBrightness"; + +constexpr char kPlatformBrightnessDark[] = "dark"; +constexpr char kPlatformBrightnessLight[] = "light"; +} // namespace + +SettingsPlugin::SettingsPlugin(BinaryMessenger* messenger, + WindowBindingHandler* delegate) + : channel_(std::make_unique>( + messenger, + kChannelName, + &JsonMessageCodec::GetInstance())), + delegate_(delegate) {} + +void SettingsPlugin::SendSettings() { + rapidjson::Document settings(rapidjson::kObjectType); + auto& allocator = settings.GetAllocator(); + settings.AddMember(kAlwaysUse24HourFormat, GetAlwaysUse24HourFormat(), + allocator); + settings.AddMember(kTextScaleFactor, GetTextScaleFactor(), allocator); + + if (GetPreferredBrightness() == PlatformBrightness::kDark) { + settings.AddMember(kPlatformBrightness, kPlatformBrightnessDark, allocator); + } else { + settings.AddMember(kPlatformBrightness, kPlatformBrightnessLight, + allocator); + } + channel_->Send(settings); +} + +bool SettingsPlugin::GetAlwaysUse24HourFormat() { + return is_always_use_24hour_format_; +} + +void SettingsPlugin::UpdateAlwaysUse24HourFormat( + bool is_always_use_24hour_format) { + is_always_use_24hour_format_ = is_always_use_24hour_format; + SendSettings(); +} + +float SettingsPlugin::GetTextScaleFactor() { + return text_scaling_factor_; +} + +void SettingsPlugin::UpdateTextScaleFactor(float factor) { + text_scaling_factor_ = factor; + SendSettings(); +} + +SettingsPlugin::PlatformBrightness SettingsPlugin::GetPreferredBrightness() { + // The current OS does not have brightness factor. + return PlatformBrightness::kLight; +} + +void SettingsPlugin::UpdateHighContrastMode(bool is_high_contrast) { + is_high_contrast_ = is_high_contrast; + SendSettings(); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.h new file mode 100644 index 00000000..03a1be37 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/plugins/settings_plugin.h @@ -0,0 +1,62 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_SETTINGS_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_SETTINGS_PLUGIN_H_ + +#include + +#include + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/linux_embedded/window_binding_handler.h" + +namespace flutter { + +// Abstract settings plugin. +// +// Used to look up and notify Flutter of user-configured system settings. +// These are typically set in the control panel. +class SettingsPlugin { + public: + enum struct PlatformBrightness { kDark, kLight }; + + explicit SettingsPlugin(BinaryMessenger* messenger, + WindowBindingHandler* delegate); + ~SettingsPlugin() = default; + + // Sends settings (e.g., platform brightness) to the engine. + void SendSettings(); + + // Update the always use 24hour-format status of the system. + void UpdateAlwaysUse24HourFormat(bool is_always_use_24hour_format); + + // Update the high contrast status of the system. + void UpdateHighContrastMode(bool is_high_contrast); + + // Update the text scale factor of the system. + void UpdateTextScaleFactor(float factor); + + private: + // Returns `true` if the user uses 24 hour time. + bool GetAlwaysUse24HourFormat(); + + // Returns the user-preferred text scale factor. + float GetTextScaleFactor(); + + // Returns the user-preferred brightness. + PlatformBrightness GetPreferredBrightness(); + + std::unique_ptr> channel_; + WindowBindingHandler* delegate_; + bool is_high_contrast_ = false; + bool is_always_use_24hour_format_ = true; + float text_scaling_factor_ = 1.0; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_SETTINGS_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/plugin/text_input_plugin.cc b/src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.cc similarity index 97% rename from src/flutter/shell/platform/linux_embedded/plugin/text_input_plugin.cc rename to src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.cc index d665d49a..3cc932c9 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/text_input_plugin.cc +++ b/src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/plugin/text_input_plugin.h" +#include "flutter/shell/platform/linux_embedded/plugins/text_input_plugin.h" #include @@ -98,7 +98,9 @@ void TextInputPlugin::OnKeyPressed(uint32_t keycode, uint32_t code_point) { TextInputPlugin::TextInputPlugin(BinaryMessenger* messenger, WindowBindingHandler* delegate) : channel_(std::make_unique>( - messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())), + messenger, + kChannelName, + &flutter::JsonMethodCodec::GetInstance())), delegate_(delegate), active_model_(nullptr) { channel_->SetMethodCallHandler( @@ -238,4 +240,4 @@ void TextInputPlugin::EnterPressed(TextInputModel* model) { channel_->InvokeMethod(kPerformActionMethod, std::move(args)); } -} // namespace flutter \ No newline at end of file +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/plugin/text_input_plugin.h b/src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.h similarity index 88% rename from src/flutter/shell/platform/linux_embedded/plugin/text_input_plugin.h rename to src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.h index 64964d97..3f48d9b6 100644 --- a/src/flutter/shell/platform/linux_embedded/plugin/text_input_plugin.h +++ b/src/flutter/shell/platform/linux_embedded/plugins/text_input_plugin.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_TEXT_INPUT_PLUGIN_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_TEXT_INPUT_PLUGIN_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_TEXT_INPUT_PLUGIN_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_TEXT_INPUT_PLUGIN_H_ #include @@ -39,7 +39,7 @@ class TextInputPlugin { std::unique_ptr> channel_; // The active client id. - int client_id_; + int client_id_ = 0; // The active model. nullptr if not set. std::unique_ptr active_model_; @@ -58,4 +58,4 @@ class TextInputPlugin { } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGIN_TEXT_INPUT_PLUGIN_H_ \ No newline at end of file +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PLUGINS_TEXT_INPUT_PLUGIN_H_ diff --git a/src/flutter/shell/platform/linux_embedded/public/flutter_linuxes.h b/src/flutter/shell/platform/linux_embedded/public/flutter_elinux.h similarity index 71% rename from src/flutter/shell/platform/linux_embedded/public/flutter_linuxes.h rename to src/flutter/shell/platform/linux_embedded/public/flutter_elinux.h index b534988a..eacb5269 100644 --- a/src/flutter/shell/platform/linux_embedded/public/flutter_linuxes.h +++ b/src/flutter/shell/platform/linux_embedded/public/flutter_elinux.h @@ -54,27 +54,72 @@ typedef struct { const char** dart_entrypoint_argv; } FlutterDesktopEngineProperties; -// The Window display mode. -enum FlutterWindowMode { - // Show the Flutter view by user specific size. - kNormal = 0, - // Show always the Flutter view by fullscreen. +// The View display mode. +enum FlutterDesktopViewMode { + // Shows the Flutter view by user specific size. + kNormalscreen = 0, + // Shows always the Flutter view by fullscreen. kFullscreen = 1, }; +// The View rotation setting. +enum FlutterDesktopViewRotation { + // Rotation constant: 0 degree rotation (natural orientation) + kRotation_0 = 0, + // Rotation constant: 90 degree rotation. + kRotation_90 = 1, + // Rotation constant: 180 degree rotation. + kRotation_180 = 2, + // Rotation constant: 270 degree rotation. + kRotation_270 = 3, +}; + // Properties for configuring a Flutter view instance. typedef struct { - // Window display mode. - FlutterWindowMode windw_display_mode; - - // Window width. + // View width in logical pixels. int width; - // Window height. + // View height in logical pixels. int height; - // Show mouse cursor. - bool show_cursor; + // View rotation setting. + FlutterDesktopViewRotation view_rotation; + + // View display mode. If you set kFullscreen, the parameters of both `width` + // and `height` will be ignored. + FlutterDesktopViewMode view_mode; + + // View title. + const char* title; + + // View XDG application ID. As a best practice, it is suggested to select an + // app ID that matches the basename of the application's .desktop file. + const char* app_id; + + // Uses mouse cursor. + bool use_mouse_cursor; + + // Uses the on-screen keyboard. + bool use_onscreen_keyboard; + + // Uses the window decoration such as toolbar and max/min buttons. + // This option is only active for Wayland backend. + bool use_window_decoration; + + // Text scaling factor. + double text_scale_factor; + + // Enable high contrast. Request that UI be rendered with darker colors. + bool enable_high_contrast; + + // Force scale factor specified by command line argument + bool force_scale_factor; + double scale_factor; + + // Enable Vsync. + // True: Sync to compositor redraw/v-blank (eglSwapInterval 1) + // False: Do not sync to compositor redraw/v-blank (eglSwapInterval 0) + bool enable_vsync; } FlutterDesktopViewProperties; // ========== View Controller ========== @@ -94,7 +139,7 @@ typedef struct { // an error. FLUTTER_EXPORT FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate( - const FlutterDesktopViewProperties& view_properties, + const FlutterDesktopViewProperties* view_properties, FlutterDesktopEngineRef engine); // Shuts down the engine instance associated with |controller|, and cleans up @@ -109,13 +154,17 @@ FLUTTER_EXPORT void FlutterDesktopViewControllerDestroy( // Its lifetime is the same as the |controller|'s. FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine( FlutterDesktopViewControllerRef controller); -// Returns the view managed by the given controller. +// Returns the view managed by the given controller. FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopViewControllerGetView(FlutterDesktopViewControllerRef controller); FLUTTER_EXPORT bool FlutterDesktopViewDispatchEvent(FlutterDesktopViewRef view); +// Returns the display frame rate by the given controller. +FLUTTER_EXPORT int32_t +FlutterDesktopViewGetFrameRate(FlutterDesktopViewRef view); + // ========== Engine ========== // Creates a Flutter engine with the given properties. @@ -123,7 +172,7 @@ FLUTTER_EXPORT bool FlutterDesktopViewDispatchEvent(FlutterDesktopViewRef view); // The caller owns the returned reference, and is responsible for calling // FlutterDesktopEngineDestroy. FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopEngineCreate( - const FlutterDesktopEngineProperties& engine_properties); + const FlutterDesktopEngineProperties* engine_properties); // Shuts down and destroys the given engine instance. Returns true if the // shutdown was successful, or if the engine was not running. @@ -162,14 +211,17 @@ FLUTTER_EXPORT FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine, const char* plugin_name); +// Returns the view associated with this registrar's engine instance. +FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( + FlutterDesktopPluginRegistrarRef registrar); + // Returns the messenger associated with the engine. -FLUTTER_EXPORT FlutterDesktopMessengerRef -FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine); +FLUTTER_EXPORT FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger( + FlutterDesktopEngineRef engine) SWIFT_RETURNS_UNRETAINED; // Returns the texture registrar associated with the engine. FLUTTER_EXPORT FlutterDesktopTextureRegistrarRef -FlutterDesktopEngineGetTextureRegistrar( - FlutterDesktopTextureRegistrarRef texture_registrar); +FlutterDesktopEngineGetTextureRegistrar(FlutterDesktopEngineRef engine); #if defined(__cplusplus) } // extern "C" diff --git a/src/flutter/shell/platform/linux_embedded/public/flutter_platform_views.h b/src/flutter/shell/platform/linux_embedded/public/flutter_platform_views.h new file mode 100644 index 00000000..0e7c9d52 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/public/flutter_platform_views.h @@ -0,0 +1,89 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PUBLIC_FLUTTER_PLATFORM_VIEWS_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PUBLIC_FLUTTER_PLATFORM_VIEWS_H_ + +#include +#include + +#include +#include + +#include +#include "flutter_export.h" +#include "flutter_messenger.h" + +class FlutterDesktopPlatformView { + public: + FlutterDesktopPlatformView(flutter::PluginRegistrar* registrar, int view_id) + : registrar_(registrar), + view_id_(view_id), + texture_id_(-1), + focused_(false) {} + virtual ~FlutterDesktopPlatformView() {} + + flutter::PluginRegistrar* GetPluginRegistrar() { return registrar_; } + + virtual void Dispose() = 0; + + int GetViewId() const { return view_id_; } + + void SetFocus(bool focus) { focused_ = focus; } + + virtual void ClearFocus() = 0; + + bool IsFocused() const { return focused_; } + + void SetTextureId(int64_t texture_id) { texture_id_ = texture_id; } + + int64_t GetTextureId() const { return texture_id_; } + + virtual void Resize(double width, double height) = 0; + + virtual void Touch(int device_id, int event_type, double x, double y) = 0; + + virtual void Offset(double top, double left) = 0; + + private: + flutter::PluginRegistrar* registrar_; + int view_id_; + int64_t texture_id_; + bool focused_; +}; + +class FlutterDesktopPlatformViewFactory { + public: + FlutterDesktopPlatformViewFactory(flutter::PluginRegistrar* registrar) + : registrar_(registrar) {} + virtual ~FlutterDesktopPlatformViewFactory() {} + + flutter::PluginRegistrar* GetPluginRegistrar() const { return registrar_; } + + virtual FlutterDesktopPlatformView* Create( + int view_id, + double width, + double height, + const std::vector& params) = 0; + + virtual void Dispose() = 0; + + private: + flutter::PluginRegistrar* registrar_; +}; + +#if defined(__cplusplus) +extern "C" { +#endif + +void FlutterDesktopRegisterPlatformViewFactory( + FlutterDesktopPluginRegistrarRef registrar, + const char* view_type, + std::unique_ptr view_factory); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_PUBLIC_FLUTTER_PLATFORM_VIEWS_H_ diff --git a/src/flutter/shell/platform/linux_embedded/surface/context_egl.cc b/src/flutter/shell/platform/linux_embedded/surface/context_egl.cc index 5fbd1f4e..855687c2 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/context_egl.cc +++ b/src/flutter/shell/platform/linux_embedded/surface/context_egl.cc @@ -10,31 +10,80 @@ namespace flutter { ContextEgl::ContextEgl(std::unique_ptr environment, + bool enable_impeller, EGLint egl_surface_type) : environment_(std::move(environment)), config_(nullptr) { EGLint config_count = 0; const EGLint attribs[] = { // clang-format off - EGL_SURFACE_TYPE, egl_surface_type, - EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, - EGL_RED_SIZE, 8, - EGL_GREEN_SIZE, 8, - EGL_BLUE_SIZE, 8, - EGL_ALPHA_SIZE, 8, - EGL_DEPTH_SIZE, 0, - EGL_STENCIL_SIZE, 0, - EGL_NONE + EGL_SURFACE_TYPE, egl_surface_type, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, +#if defined(ENABLE_EGL_ALPHA_COMPONENT_OF_COLOR_BUFFER) + EGL_ALPHA_SIZE, 8, +#endif + EGL_DEPTH_SIZE, 0, + EGL_STENCIL_SIZE, 0, + EGL_NONE // clang-format on }; - if (eglChooseConfig(environment_->Display(), attribs, &config_, 1, - &config_count) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to choose EGL surface config: " - << get_egl_error_cause(); - return; + const EGLint impeller_config_attributes[] = { + // clang-format off + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, +#if defined(ENABLE_EGL_ALPHA_COMPONENT_OF_COLOR_BUFFER) + EGL_ALPHA_SIZE, 8, +#endif + EGL_DEPTH_SIZE, 0, + EGL_STENCIL_SIZE, 8, + EGL_SAMPLE_BUFFERS, 1, + EGL_SAMPLES, 4, + EGL_NONE + // clang-format on + }; + const EGLint impeller_config_attributes_no_msaa[] = { + // clang-format off + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, +#if defined(ENABLE_EGL_ALPHA_COMPONENT_OF_COLOR_BUFFER) + EGL_ALPHA_SIZE, 8, +#endif + EGL_DEPTH_SIZE, 0, + EGL_STENCIL_SIZE, 8, + EGL_NONE + // clang-format on + }; + + if (enable_impeller) { + // First try the MSAA configuration. + if ((eglChooseConfig(environment_->Display(), impeller_config_attributes, + &config_, 1, &config_count) == EGL_FALSE) || + (config_count == 0)) { + // Next fall back to disabled MSAA. + if ((eglChooseConfig(environment_->Display(), + impeller_config_attributes_no_msaa, &config_, 1, + &config_count) == EGL_FALSE) || + (config_count == 0)) { + ELINUX_LOG(ERROR) << "Failed to choose EGL surface config: " + << get_egl_error_cause(); + return; + } + } + } else { + if (eglChooseConfig(environment_->Display(), attribs, &config_, 1, + &config_count) != EGL_TRUE) { + ELINUX_LOG(ERROR) << "Failed to choose EGL surface config: " + << get_egl_error_cause(); + return; + } } if (config_count == 0 || config_ == nullptr) { - LINUXES_LOG(ERROR) << "No matching configs: " << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "No matching configs: " << get_egl_error_cause(); return; } @@ -43,16 +92,16 @@ ContextEgl::ContextEgl(std::unique_ptr environment, context_ = eglCreateContext(environment_->Display(), config_, EGL_NO_CONTEXT, attribs); if (context_ == EGL_NO_CONTEXT) { - LINUXES_LOG(ERROR) << "Failed to create an onscreen context: " - << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to create an onscreen context: " + << get_egl_error_cause(); return; } resource_context_ = eglCreateContext(environment_->Display(), config_, context_, attribs); if (resource_context_ == EGL_NO_CONTEXT) { - LINUXES_LOG(ERROR) << "Failed to create an offscreen resouce context: " - << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to create an offscreen resouce context: " + << get_egl_error_cause(); return; } } @@ -60,20 +109,20 @@ ContextEgl::ContextEgl(std::unique_ptr environment, valid_ = true; } -std::unique_ptr ContextEgl::CreateOnscreenSurface( +std::unique_ptr ContextEgl::CreateOnscreenSurface( NativeWindow* window) const { const EGLint attribs[] = {EGL_NONE}; EGLSurface surface = eglCreateWindowSurface(environment_->Display(), config_, window->Window(), attribs); if (surface == EGL_NO_SURFACE) { - LINUXES_LOG(ERROR) << "Failed to create EGL window surface: " - << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to create EGL window surface: " + << get_egl_error_cause(); } - return std::make_unique(surface, environment_->Display(), - context_); + return std::make_unique(surface, environment_->Display(), + context_, window->EnableVsync()); } -std::unique_ptr ContextEgl::CreateOffscreenSurface( +std::unique_ptr ContextEgl::CreateOffscreenSurface( NativeWindow* window) const { #if defined(DISPLAY_BACKEND_TYPE_X11) || \ defined(DISPLAY_BACKEND_TYPE_DRM_EGLSTREAM) @@ -87,8 +136,8 @@ std::unique_ptr ContextEgl::CreateOffscreenSurface( EGLSurface surface = eglCreatePbufferSurface(environment_->Display(), config_, attribs); if (surface == EGL_NO_SURFACE) { - LINUXES_LOG(WARNING) << "Failed to create EGL off-screen surface." - << "(" << get_egl_error_cause() << ")"; + ELINUX_LOG(WARNING) << "Failed to create EGL off-screen surface." << "(" + << get_egl_error_cause() << ")"; } #else // eglCreatePbufferSurface isn't supported on both Wayland and GBM. @@ -97,15 +146,18 @@ std::unique_ptr ContextEgl::CreateOffscreenSurface( EGLSurface surface = eglCreateWindowSurface( environment_->Display(), config_, window->WindowOffscreen(), attribs); if (surface == EGL_NO_SURFACE) { - LINUXES_LOG(WARNING) << "Failed to create EGL off-screen surface." - << "(" << get_egl_error_cause() << ")"; + ELINUX_LOG(WARNING) << "Failed to create EGL off-screen surface." << "(" + << get_egl_error_cause() << ")"; } #endif - return std::make_unique(surface, environment_->Display(), - resource_context_); + return std::make_unique(surface, environment_->Display(), + resource_context_, + window->EnableVsync()); } -bool ContextEgl::IsValid() const { return valid_; } +bool ContextEgl::IsValid() const { + return valid_; +} bool ContextEgl::ClearCurrent() const { if (eglGetCurrentContext() != context_) { @@ -113,8 +165,8 @@ bool ContextEgl::ClearCurrent() const { } if (eglMakeCurrent(environment_->Display(), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to clear EGL context: " - << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to clear EGL context: " + << get_egl_error_cause(); return false; } return true; @@ -123,7 +175,7 @@ bool ContextEgl::ClearCurrent() const { void* ContextEgl::GlProcResolver(const char* name) const { auto address = eglGetProcAddress(name); if (!address) { - LINUXES_LOG(ERROR) << "Failed eglGetProcAddress: " << name; + ELINUX_LOG(ERROR) << "Failed eglGetProcAddress: " << name; return nullptr; } return reinterpret_cast(address); diff --git a/src/flutter/shell/platform/linux_embedded/surface/context_egl.h b/src/flutter/shell/platform/linux_embedded/surface/context_egl.h index d4cadf3c..4950a816 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/context_egl.h +++ b/src/flutter/shell/platform/linux_embedded/surface/context_egl.h @@ -9,8 +9,8 @@ #include +#include "flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h" #include "flutter/shell/platform/linux_embedded/surface/environment_egl.h" -#include "flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.h" #include "flutter/shell/platform/linux_embedded/window/native_window.h" namespace flutter { @@ -18,13 +18,14 @@ namespace flutter { class ContextEgl { public: ContextEgl(std::unique_ptr environment, + bool enable_impeller, EGLint egl_surface_type = EGL_WINDOW_BIT); ~ContextEgl() = default; - virtual std::unique_ptr CreateOnscreenSurface( + virtual std::unique_ptr CreateOnscreenSurface( NativeWindow* window) const; - std::unique_ptr CreateOffscreenSurface( + std::unique_ptr CreateOffscreenSurface( NativeWindow* window_resource) const; bool IsValid() const; diff --git a/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.cc b/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.cc index 8d916e89..4a13f498 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.cc +++ b/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.cc @@ -11,45 +11,45 @@ namespace flutter { ContextEglStream::ContextEglStream( std::unique_ptr environment) - : ContextEgl(std::move(environment), EGL_STREAM_BIT_KHR) { + : ContextEgl(std::move(environment), false, EGL_STREAM_BIT_KHR) { if (!valid_) { return; } if (!SetEglExtensionFunctionPointers()) { - LINUXES_LOG(ERROR) << "Failed to set extension function pointers"; + ELINUX_LOG(ERROR) << "Failed to set extension function pointers"; valid_ = false; } } -std::unique_ptr ContextEglStream::CreateOnscreenSurface( +std::unique_ptr ContextEglStream::CreateOnscreenSurface( NativeWindow* window) const { EGLint layer_count = 0; EGLOutputLayerEXT layer; EGLAttrib layer_attribs[] = { // clang-format off - EGL_DRM_PLANE_EXT, static_cast(window)->PlaneId(), + EGL_DRM_PLANE_EXT, static_cast(static_cast(window)->PlaneId()), EGL_NONE // clang-format on }; if (eglGetOutputLayersEXT_(environment_->Display(), layer_attribs, &layer, 1, &layer_count) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to get EGL output layers"; + ELINUX_LOG(ERROR) << "Failed to get EGL output layers"; } if (layer_count == 0 || layer == nullptr) { - LINUXES_LOG(ERROR) << "No matching layers"; + ELINUX_LOG(ERROR) << "No matching layers"; } EGLint stream_attribs[] = {EGL_NONE}; auto stream = eglCreateStreamKHR_(environment_->Display(), stream_attribs); if (stream == EGL_NO_STREAM_KHR) { - LINUXES_LOG(ERROR) << "Failed to create EGL stream"; + ELINUX_LOG(ERROR) << "Failed to create EGL stream"; } if (eglStreamConsumerOutputEXT_(environment_->Display(), stream, layer) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to create EGL stream consumer output"; + ELINUX_LOG(ERROR) << "Failed to create EGL stream consumer output"; } EGLint surface_attribs[] = { @@ -62,10 +62,10 @@ std::unique_ptr ContextEglStream::CreateOnscreenSurface( auto surface = eglCreateStreamProducerSurfaceKHR_( environment_->Display(), config_, stream, surface_attribs); if (surface == EGL_NO_SURFACE) { - LINUXES_LOG(ERROR) << "Failed to create EGL stream producer surface"; + ELINUX_LOG(ERROR) << "Failed to create EGL stream producer surface"; } - return std::make_unique(surface, environment_->Display(), - context_); + return std::make_unique(surface, environment_->Display(), + context_, window->EnableVsync()); } bool ContextEglStream::SetEglExtensionFunctionPointers() { diff --git a/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.h b/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.h index a360022d..4727bb81 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.h +++ b/src/flutter/shell/platform/linux_embedded/surface/context_egl_stream.h @@ -9,8 +9,8 @@ #include #include "flutter/shell/platform/linux_embedded/surface/context_egl.h" +#include "flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h" #include "flutter/shell/platform/linux_embedded/surface/environment_egl_stream.h" -#include "flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.h" namespace flutter { @@ -20,7 +20,7 @@ class ContextEglStream : public ContextEgl { ~ContextEglStream() = default; // |ContextEgl| - std::unique_ptr CreateOnscreenSurface( + std::unique_ptr CreateOnscreenSurface( NativeWindow* window) const override; private: diff --git a/src/flutter/shell/platform/linux_embedded/surface/egl_utils.cc b/src/flutter/shell/platform/linux_embedded/surface/egl_utils.cc index 619c51bf..23647bcd 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/egl_utils.cc +++ b/src/flutter/shell/platform/linux_embedded/surface/egl_utils.cc @@ -1,5 +1,5 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,6 +7,7 @@ #include +#include #include #include @@ -40,4 +41,14 @@ std::string get_egl_error_cause() { return nullptr; } -} // namespace flutter \ No newline at end of file +// Auxiliary function used to check if the given list of extensions contains the +// requested extension name. +bool has_egl_extension(const char* extensions, const char* name) { + const char* r = std::strstr(extensions, name); + auto len = std::strlen(name); + + // check that the extension name is terminated by space or null terminator + return r != nullptr && (r[len] == ' ' || r[len] == 0); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/surface/egl_utils.h b/src/flutter/shell/platform/linux_embedded/surface/egl_utils.h index a64196df..80692000 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/egl_utils.h +++ b/src/flutter/shell/platform/linux_embedded/surface/egl_utils.h @@ -1,4 +1,4 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -10,6 +10,7 @@ namespace flutter { std::string get_egl_error_cause(); +bool has_egl_extension(const char* extensions, const char* name); } // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.cc b/src/flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.cc new file mode 100644 index 00000000..914ab736 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.cc @@ -0,0 +1,217 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h" + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/surface/egl_utils.h" + +namespace flutter { + +constexpr size_t kInitialWindowWidthPx = 1280; +constexpr size_t kInitialWindowHeightPx = 720; + +// Maximum damage history - for triple buffering we need to store damage for +// last two frames; Some Android devices (Pixel 4) use quad buffering. +constexpr const int kMaxHistorySize = 10; + +ELinuxEGLSurface::ELinuxEGLSurface(EGLSurface surface, + EGLDisplay display, + EGLContext context, + bool vsync_enabled) + : surface_(surface), + display_(display), + context_(context), + vsync_enabled_(vsync_enabled), + width_px_(kInitialWindowWidthPx), + height_px_(kInitialWindowHeightPx) { + const char* extensions = eglQueryString(display_, EGL_EXTENSIONS); + + if (has_egl_extension(extensions, "EGL_KHR_partial_update")) { + eglSetDamageRegionKHR_ = reinterpret_cast( + eglGetProcAddress("eglSetDamageRegionKHR")); + } + + if (has_egl_extension(extensions, "EGL_EXT_swap_buffers_with_damage")) { + eglSwapBuffersWithDamageEXT_ = + reinterpret_cast( + eglGetProcAddress("eglSwapBuffersWithDamageEXT")); + } else if (has_egl_extension(extensions, + "EGL_KHR_swap_buffers_with_damage")) { + eglSwapBuffersWithDamageEXT_ = + reinterpret_cast( + eglGetProcAddress("eglSwapBuffersWithDamageKHR")); + } else { + // do nothing. + } +}; + +ELinuxEGLSurface::~ELinuxEGLSurface() { + if (surface_ != EGL_NO_SURFACE) { + if (eglDestroySurface(display_, surface_) != EGL_TRUE) { + ELINUX_LOG(ERROR) << "Failed to destory surface: " + << get_egl_error_cause(); + } + surface_ = EGL_NO_SURFACE; + } +} + +bool ELinuxEGLSurface::IsValid() const { + return surface_ != EGL_NO_SURFACE; +} + +void ELinuxEGLSurface::SurfaceResize(const size_t width_px, + const size_t height_px) { + width_px_ = width_px; + height_px_ = height_px; +} + +bool ELinuxEGLSurface::MakeCurrent() const { + if (eglMakeCurrent(display_, surface_, surface_, context_) != EGL_TRUE) { + ELINUX_LOG(ERROR) << "Failed to make the EGL context current: " + << get_egl_error_cause(); + return false; + } + + // Non-blocking when swappipping buffers on Wayland. + // OpenGL swap intervals can be used to prevent screen tearing. + // If enabled, the raster thread blocks until the v-blank. + // This is unnecessary if DWM composition is enabled. + // See: https://www.khronos.org/opengl/wiki/Swap_Interval + // See: https://learn.microsoft.com/windows/win32/dwm/composition-ovw + // + // However, we might encounter rendering problems on some Wayland compositors + // (e.g. weston 9.0). + // See also: + // - https://github.com/sony/flutter-embedded-linux/issues/230 + // - https://github.com/sony/flutter-embedded-linux/issues/234 + // - https://github.com/sony/flutter-embedded-linux/issues/220 + if (!vsync_enabled_) { + if (eglSwapInterval(display_, 0) != EGL_TRUE) { + ELINUX_LOG(ERROR) << "Failed to eglSwapInterval(Free): " + << get_egl_error_cause(); + } + } + + return true; +} + +bool ELinuxEGLSurface::SwapBuffers() const { + if (eglSwapBuffers(display_, surface_) != EGL_TRUE) { + ELINUX_LOG(ERROR) << "eglSwapBuffers failed: " << get_egl_error_cause(); + return false; + } + return true; +} + +// Reference of dirty region management: +// https://github.com/flutter/engine/blob/main/examples/glfw_drm/FlutterEmbedderGLFW.cc + +bool ELinuxEGLSurface::SwapBuffers(const FlutterPresentInfo* info) { + // Free the existing damage that was allocated to this frame. + if (existing_damage_map_.find(info->fbo_id) != existing_damage_map_.end() && + existing_damage_map_[info->fbo_id] != nullptr) { + free(existing_damage_map_[info->fbo_id]); + existing_damage_map_[info->fbo_id] = nullptr; + } + + // Set the buffer damage as the damage region. + if (eglSetDamageRegionKHR_) { + auto buffer_rects = RectToInts(info->buffer_damage.damage[0]); + if (eglSetDamageRegionKHR_(display_, surface_, buffer_rects.data(), 1) != + EGL_TRUE) { + ELINUX_LOG(ERROR) << "eglSetDamageRegionKHR failed: " + << get_egl_error_cause(); + return false; + } + } + + // Add frame damage to damage history + damage_history_.push_back(info->frame_damage.damage[0]); + if (damage_history_.size() > kMaxHistorySize) { + damage_history_.pop_front(); + } + + if (eglSwapBuffersWithDamageEXT_) { + auto frame_rects = RectToInts(info->frame_damage.damage[0]); + if (eglSwapBuffersWithDamageEXT_(display_, surface_, frame_rects.data(), + 1) != EGL_TRUE) { + ELINUX_LOG(ERROR) << "eglSwapBuffersWithDamageEXT failed: " + << get_egl_error_cause(); + return false; + } + } else { + // If the required extensions for partial repaint were not provided, do + // full repaint. + if (eglSwapBuffers(display_, surface_) != EGL_TRUE) { + ELINUX_LOG(ERROR) << "eglSwapBuffers failed: " << get_egl_error_cause(); + return false; + } + } + + return true; +} + +void ELinuxEGLSurface::PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage) { + // Given the FBO age, create existing damage region by joining all frame + // damages since FBO was last used + EGLint age = 0; + if (eglQuerySurface(display_, surface_, EGL_BUFFER_AGE_EXT, &age) != + EGL_TRUE || + age == 0) { + age = 4; // Virtually no driver should have a swapchain length > 4. + } + + existing_damage->num_rects = 1; + + // Allocate the array of rectangles for the existing damage. + existing_damage_map_[fbo_id] = static_cast( + malloc(sizeof(FlutterRect) * existing_damage->num_rects)); + + existing_damage_map_[fbo_id][0] = FlutterRect{ + 0, 0, static_cast(width_px_), static_cast(height_px_)}; + existing_damage->damage = existing_damage_map_[fbo_id]; + + if (age > 1) { + --age; + // join up to (age - 1) last rects from damage history + for (auto i = damage_history_.rbegin(); + i != damage_history_.rend() && age > 0; ++i, --age) { + if (i == damage_history_.rbegin()) { + if (i != damage_history_.rend()) { + existing_damage->damage[0] = {i->left, i->top, i->right, i->bottom}; + } + } else { + // Auxiliary function to union the damage regions comprised by two + // FlutterRect element. It saves the result of this join in the rect + // variable. + FlutterRect* rect = &(existing_damage->damage[0]); + const FlutterRect additional_rect = *i; + + rect->left = std::min(rect->left, additional_rect.left); + rect->top = std::min(rect->top, additional_rect.top); + rect->right = std::max(rect->right, additional_rect.right); + rect->bottom = std::max(rect->bottom, additional_rect.bottom); + } + } + } +} + +// Auxiliary function used to transform a FlutterRect into the format that is +// expected by the EGL functions (i.e. array of EGLint). +std::array ELinuxEGLSurface::RectToInts(const FlutterRect rect) { + EGLint height; + eglQuerySurface(display_, surface_, EGL_HEIGHT, &height); + + std::array res{ + static_cast(rect.left), + height - static_cast(rect.bottom), + static_cast(rect.right) - static_cast(rect.left), + static_cast(rect.bottom) - static_cast(rect.top), + }; + return res; +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h b/src/flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h new file mode 100644 index 00000000..885b1138 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h @@ -0,0 +1,67 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_ELINUX_EGL_SURFACE_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_ELINUX_EGL_SURFACE_H_ + +#include +#include + +#include +#include +#include + +#include "flutter/shell/platform/embedder/embedder.h" + +namespace flutter { + +class ELinuxEGLSurface { + public: + // Note that EGLSurface will be destroyed in this class's destructor. + ELinuxEGLSurface(EGLSurface surface, + EGLDisplay display, + EGLContext context, + bool vsync_enabled); + ~ELinuxEGLSurface(); + + bool IsValid() const; + + void SurfaceResize(const size_t width_px, const size_t height_px); + + bool MakeCurrent() const; + + bool SwapBuffers() const; + + bool SwapBuffers(const FlutterPresentInfo* info); + + void PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage); + + private: + // Auxiliary function used to transform a FlutterRect into the format that is + // expected by the EGL functions (i.e. array of EGLint). + std::array RectToInts(const FlutterRect rect); + + EGLDisplay display_; + EGLSurface surface_; + EGLContext context_; + bool vsync_enabled_; + + size_t width_px_; + size_t height_px_; + + PFNEGLSETDAMAGEREGIONKHRPROC eglSetDamageRegionKHR_ = nullptr; + PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC eglSwapBuffersWithDamageEXT_ = nullptr; + + // Keeps track of the most recent frame damages so that existing damage can + // be easily computed. + std::list damage_history_; + + // Keeps track of the existing damage associated with each FBO ID + std::unordered_map existing_damage_map_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_ELINUX_EGL_SURFACE_H_ diff --git a/src/flutter/shell/platform/linux_embedded/surface/environment_egl.h b/src/flutter/shell/platform/linux_embedded/surface/environment_egl.h index d7b4f7bf..83d94472 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/environment_egl.h +++ b/src/flutter/shell/platform/linux_embedded/surface/environment_egl.h @@ -14,25 +14,34 @@ namespace flutter { class EnvironmentEgl { public: - EnvironmentEgl(EGLNativeDisplayType platform_display) - : display_(EGL_NO_DISPLAY), valid_(false) { + EnvironmentEgl(EGLNativeDisplayType platform_display, + bool sub_environment = false) + : display_(EGL_NO_DISPLAY), sub_environment_(sub_environment) { display_ = eglGetDisplay(platform_display); if (display_ == EGL_NO_DISPLAY) { - LINUXES_LOG(ERROR) << "Failed to get the EGL display: " - << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to get the EGL display: " + << get_egl_error_cause(); return; } - valid_ = InitializeEgl(); + // sub_environment flag is used for window decorations such as toolbar and + // buttons. When this flag is active, EGLDisplay doesn't be initialized and + // finalized. + if (!sub_environment_) { + valid_ = InitializeEgl(); + } else { + valid_ = true; + } } - EnvironmentEgl() : display_(EGL_NO_DISPLAY), valid_(false) {} + EnvironmentEgl(bool sub_environment = false) + : display_(EGL_NO_DISPLAY), sub_environment_(sub_environment) {} ~EnvironmentEgl() { - if (display_ != EGL_NO_DISPLAY) { + if (display_ != EGL_NO_DISPLAY && !sub_environment_) { if (eglTerminate(display_) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to terminate the EGL display: " - << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to terminate the EGL display: " + << get_egl_error_cause(); } display_ = EGL_NO_DISPLAY; } @@ -40,13 +49,13 @@ class EnvironmentEgl { bool InitializeEgl() const { if (eglInitialize(display_, nullptr, nullptr) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to initialize the EGL display: " - << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to initialize the EGL display: " + << get_egl_error_cause(); return false; } if (eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to bind EGL API: " << get_egl_error_cause(); + ELINUX_LOG(ERROR) << "Failed to bind EGL API: " << get_egl_error_cause(); return false; } @@ -59,9 +68,10 @@ class EnvironmentEgl { protected: EGLDisplay display_; - bool valid_; + bool valid_ = false; + bool sub_environment_; }; } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_ENVIRONMENT_EGL_H_ \ No newline at end of file +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_ENVIRONMENT_EGL_H_ diff --git a/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.cc b/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.cc index b5af4088..67062daa 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.cc +++ b/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.cc @@ -10,21 +10,22 @@ namespace flutter { -EnvironmentEglStream::EnvironmentEglStream() : EnvironmentEgl() { +EnvironmentEglStream::EnvironmentEglStream(bool sub_environment) + : EnvironmentEgl(sub_environment) { if (!SetEglExtensionFunctionPointers()) { - LINUXES_LOG(ERROR) << "Failed to set extension function pointers"; + ELINUX_LOG(ERROR) << "Failed to set extension function pointers"; return; } auto device = GetEglDevice(); if (device == EGL_NO_DEVICE_EXT) { - LINUXES_LOG(ERROR) << "Couldn't find EGL device"; + ELINUX_LOG(ERROR) << "Couldn't find EGL device"; return; } display_ = eglGetPlatformDisplayEXT_(EGL_PLATFORM_DEVICE_EXT, device, NULL); if (display_ == EGL_NO_DISPLAY) { - LINUXES_LOG(ERROR) << "Failed to get the EGL display"; + ELINUX_LOG(ERROR) << "Failed to get the EGL display"; return; } @@ -46,23 +47,23 @@ bool EnvironmentEglStream::SetEglExtensionFunctionPointers() { EGLDeviceEXT EnvironmentEglStream::GetEglDevice() { EGLint num_devices; if (eglQueryDevicesEXT_(0, NULL, &num_devices) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to query EGL devices"; + ELINUX_LOG(ERROR) << "Failed to query EGL devices"; return EGL_NO_DEVICE_EXT; } if (num_devices < 1) { - LINUXES_LOG(ERROR) << "No EGL devices found"; + ELINUX_LOG(ERROR) << "No EGL devices found"; return EGL_NO_DEVICE_EXT; } auto devices = static_cast( std::calloc(num_devices, sizeof(EGLDeviceEXT))); if (!devices) { - LINUXES_LOG(ERROR) << "Failed to allocate memory"; + ELINUX_LOG(ERROR) << "Failed to allocate memory"; return EGL_NO_DEVICE_EXT; } if (eglQueryDevicesEXT_(num_devices, devices, &num_devices) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to query EGL devices"; + ELINUX_LOG(ERROR) << "Failed to query EGL devices"; std::free(devices); return EGL_NO_DEVICE_EXT; } diff --git a/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.h b/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.h index e119c3a9..decc5cd7 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.h +++ b/src/flutter/shell/platform/linux_embedded/surface/environment_egl_stream.h @@ -14,7 +14,7 @@ namespace flutter { class EnvironmentEglStream : public EnvironmentEgl { public: - EnvironmentEglStream(); + EnvironmentEglStream(bool sub_environment = false); ~EnvironmentEglStream() = default; private: diff --git a/src/flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.cc b/src/flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.cc deleted file mode 100644 index faf18b67..00000000 --- a/src/flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.cc +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.h" - -#include "flutter/shell/platform/linux_embedded/logger.h" -#include "flutter/shell/platform/linux_embedded/surface/egl_utils.h" - -namespace flutter { - -LinuxesEGLSurface::LinuxesEGLSurface(EGLSurface surface, EGLDisplay display, - EGLContext context) - : surface_(surface), display_(display), context_(context){}; - -LinuxesEGLSurface::~LinuxesEGLSurface() { - if (surface_ != EGL_NO_SURFACE) { - if (eglDestroySurface(display_, surface_) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to destory surface"; - } - surface_ = EGL_NO_SURFACE; - } -} - -bool LinuxesEGLSurface::IsValid() const { return surface_ != EGL_NO_SURFACE; } - -bool LinuxesEGLSurface::MakeCurrent() const { - if (eglMakeCurrent(display_, surface_, surface_, context_) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to make the EGL context current: " - << get_egl_error_cause(); - return false; - } - return true; -} - -bool LinuxesEGLSurface::SwapBuffers() const { - if (eglSwapBuffers(display_, surface_) != EGL_TRUE) { - LINUXES_LOG(ERROR) << "Failed to swap the EGL buffer: " - << get_egl_error_cause(); - return false; - } - return true; -} - -} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.h b/src/flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.h deleted file mode 100644 index e7105b9a..00000000 --- a/src/flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_LINUXES_EGL_SURFACE_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_LINUXES_EGL_SURFACE_H_ - -#include - -namespace flutter { - -class LinuxesEGLSurface { - public: - // Note that EGLSurface will be destroyed in this class's destructor. - LinuxesEGLSurface(EGLSurface surface, EGLDisplay display, EGLContext context); - ~LinuxesEGLSurface(); - - bool IsValid() const; - - bool MakeCurrent() const; - - bool SwapBuffers() const; - - private: - EGLDisplay display_; - EGLSurface surface_; - EGLContext context_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_LINUXES_EGL_SURFACE_H_ diff --git a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface.cc b/src/flutter/shell/platform/linux_embedded/surface/surface_base.cc similarity index 56% rename from src/flutter/shell/platform/linux_embedded/surface/linuxes_surface.cc rename to src/flutter/shell/platform/linux_embedded/surface/surface_base.cc index 63f1f973..91ca8a1d 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface.cc +++ b/src/flutter/shell/platform/linux_embedded/surface/surface_base.cc @@ -1,18 +1,18 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_base.h" #include "flutter/shell/platform/linux_embedded/logger.h" namespace flutter { -bool Surface::IsValid() const { +bool SurfaceBase::IsValid() const { return offscreen_surface_ && context_->IsValid(); }; -bool Surface::SetNativeWindow(NativeWindow* window) { +bool SurfaceBase::SetNativeWindow(NativeWindow* window) { native_window_ = window; onscreen_surface_ = context_->CreateOnscreenSurface(native_window_); @@ -29,9 +29,11 @@ bool Surface::SetNativeWindow(NativeWindow* window) { return true; }; -bool Surface::OnScreenSurfaceResize(const size_t width, const size_t height) { - if (!native_window_->Resize(width, height)) { - LINUXES_LOG(ERROR) << "Failed to resize."; +bool SurfaceBase::OnScreenSurfaceResize(const size_t width_px, + const size_t height_px) { + onscreen_surface_->SurfaceResize(width_px, height_px); + if (!native_window_->Resize(width_px, height_px)) { + ELINUX_LOG(ERROR) << "Failed to resize."; return false; } @@ -39,22 +41,25 @@ bool Surface::OnScreenSurfaceResize(const size_t width, const size_t height) { DestroyOnScreenContext(); onscreen_surface_ = context_->CreateOnscreenSurface(native_window_); if (!onscreen_surface_->IsValid()) { - LINUXES_LOG(WARNING) << "Failed to recreate on-screen surface."; + ELINUX_LOG(WARNING) << "Failed to recreate on-screen surface."; onscreen_surface_ = nullptr; return false; } + onscreen_surface_->SurfaceResize(width_px, height_px); } return true; }; -bool Surface::ClearCurrentContext() const { return context_->ClearCurrent(); }; +bool SurfaceBase::ClearCurrentContext() const { + return context_->ClearCurrent(); +}; -void Surface::DestroyOnScreenContext() { +void SurfaceBase::DestroyOnScreenContext() { context_->ClearCurrent(); onscreen_surface_ = nullptr; }; -bool Surface::ResourceContextMakeCurrent() const { +bool SurfaceBase::ResourceContextMakeCurrent() const { if (!offscreen_surface_) { return false; } diff --git a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface.h b/src/flutter/shell/platform/linux_embedded/surface/surface_base.h similarity index 64% rename from src/flutter/shell/platform/linux_embedded/surface/linuxes_surface.h rename to src/flutter/shell/platform/linux_embedded/surface/surface_base.h index a01168f0..1b7e23a8 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface.h +++ b/src/flutter/shell/platform/linux_embedded/surface/surface_base.h @@ -2,19 +2,22 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_BASE_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_BASE_H_ #include #include "flutter/shell/platform/linux_embedded/surface/context_egl.h" -#include "flutter/shell/platform/linux_embedded/surface/linuxes_egl_surface.h" +#include "flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h" #include "flutter/shell/platform/linux_embedded/window/native_window.h" namespace flutter { -class Surface { +class SurfaceBase { public: + SurfaceBase() = default; + virtual ~SurfaceBase() = default; + // Shows a surface is valid or not. bool IsValid() const; @@ -25,7 +28,9 @@ class Surface { // On-screen surface needs to be recreated after window size changed only when // using DRM-GBM backend. Because gbm-surface is recreated when the window // size changed. - bool OnScreenSurfaceResize(const size_t width, const size_t height); + // @param[in] width_px Physical width of the surface. + // @param[in] height_px Physical height of the surface. + bool OnScreenSurfaceResize(const size_t width_px, const size_t height_px); // Clears current on-screen context. bool ClearCurrentContext() const; @@ -39,10 +44,10 @@ class Surface { protected: std::unique_ptr context_; NativeWindow* native_window_ = nullptr; - std::unique_ptr onscreen_surface_ = nullptr; - std::unique_ptr offscreen_surface_ = nullptr; + std::unique_ptr onscreen_surface_ = nullptr; + std::unique_ptr offscreen_surface_ = nullptr; }; } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_H_ +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_BASE_H_ diff --git a/src/flutter/shell/platform/linux_embedded/surface/surface_decoration.cc b/src/flutter/shell/platform/linux_embedded/surface/surface_decoration.cc new file mode 100644 index 00000000..e33f92f8 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/surface/surface_decoration.cc @@ -0,0 +1,82 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/surface/surface_decoration.h" + +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +SurfaceDecoration::SurfaceDecoration(std::unique_ptr context) { + context_ = std::move(context); +} + +bool SurfaceDecoration::IsValid() const { + return context_->IsValid(); +}; + +bool SurfaceDecoration::SetNativeWindow(NativeWindow* window) { + native_window_ = window; + + surface_ = context_->CreateOnscreenSurface(native_window_); + if (!surface_->IsValid()) { + return false; + } + + return true; +}; + +bool SurfaceDecoration::Resize(const size_t width_px, const size_t height_px) { + if (!native_window_->Resize(width_px, height_px)) { + ELINUX_LOG(ERROR) << "Failed to resize."; + return false; + } + + if (native_window_->IsNeedRecreateSurfaceAfterResize()) { + DestroyContext(); + surface_ = context_->CreateOnscreenSurface(native_window_); + if (!surface_->IsValid()) { + ELINUX_LOG(WARNING) << "Failed to recreate decoration surface."; + surface_ = nullptr; + return false; + } + } + return true; +}; + +void SurfaceDecoration::DestroyContext() { + context_->ClearCurrent(); + surface_ = nullptr; +}; + +bool SurfaceDecoration::GLContextMakeCurrent() const { + return surface_->MakeCurrent(); +} + +bool SurfaceDecoration::GLContextClearCurrent() const { + return context_->ClearCurrent(); +} + +bool SurfaceDecoration::GLContextPresent(uint32_t fbo_id) const { + return surface_->SwapBuffers(); +} + +bool SurfaceDecoration::GLContextPresentWithInfo( + const FlutterPresentInfo* info) const { + return true; +} + +void SurfaceDecoration::PopulateExistingDamage( + const intptr_t fbo_id, + FlutterDamage* existing_damage) const {} + +uint32_t SurfaceDecoration::GLContextFBO() const { + return 0; +} + +void* SurfaceDecoration::GlProcResolver(const char* name) const { + return context_->GlProcResolver(name); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/surface/surface_decoration.h b/src/flutter/shell/platform/linux_embedded/surface/surface_decoration.h new file mode 100644 index 00000000..8c051712 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/surface/surface_decoration.h @@ -0,0 +1,67 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_DECORATION_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_DECORATION_H_ + +#include + +#include "flutter/shell/platform/linux_embedded/surface/context_egl.h" +#include "flutter/shell/platform/linux_embedded/surface/elinux_egl_surface.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl_delegate.h" +#include "flutter/shell/platform/linux_embedded/window/native_window.h" + +namespace flutter { + +// Surface for window decorations such as a toolbar and max/min buttons. +class SurfaceDecoration : public SurfaceGlDelegate { + public: + SurfaceDecoration(std::unique_ptr context); + ~SurfaceDecoration() = default; + + // Shows a surface is valid or not. + bool IsValid() const; + + // Sets a netive platform's window. + bool SetNativeWindow(NativeWindow* window); + + // Changes a decoration surface size. + // @param[in] width_px Physical width of the surface. + // @param[in] height_px Physical height of the surface. + bool Resize(const size_t width_px, const size_t height_px); + + // Clears and destroys current decoration context. + void DestroyContext(); + + // |SurfaceGlDelegate| + bool GLContextMakeCurrent() const override; + + // |SurfaceGlDelegate| + bool GLContextClearCurrent() const override; + + // |SurfaceGlDelegate| + bool GLContextPresent(uint32_t fbo_id) const override; + + // |SurfaceGlDelegate| + bool GLContextPresentWithInfo(const FlutterPresentInfo* info) const override; + + // |SurfaceGlDelegate| + void PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage) const override; + + // |SurfaceGlDelegate| + uint32_t GLContextFBO() const override; + + // |SurfaceGlDelegate| + void* GlProcResolver(const char* name) const override; + + protected: + std::unique_ptr context_; + NativeWindow* native_window_ = nullptr; + std::unique_ptr surface_ = nullptr; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_SURFACE_SURFACE_DECORATION_H_ diff --git a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.cc b/src/flutter/shell/platform/linux_embedded/surface/surface_gl.cc similarity index 53% rename from src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.cc rename to src/flutter/shell/platform/linux_embedded/surface/surface_gl.cc index 225e1064..cdd0eb40 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.cc +++ b/src/flutter/shell/platform/linux_embedded/surface/surface_gl.cc @@ -1,8 +1,8 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl.h" namespace flutter { @@ -26,7 +26,22 @@ bool SurfaceGl::GLContextPresent(uint32_t fbo_id) const { return true; } -uint32_t SurfaceGl::GLContextFBO() const { return 0; } +bool SurfaceGl::GLContextPresentWithInfo(const FlutterPresentInfo* info) const { + if (!onscreen_surface_->SwapBuffers(info)) { + return false; + } + native_window_->SwapBuffers(); + return true; +} + +void SurfaceGl::PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage) const { + onscreen_surface_->PopulateExistingDamage(fbo_id, existing_damage); +} + +uint32_t SurfaceGl::GLContextFBO() const { + return 0; +} void* SurfaceGl::GlProcResolver(const char* name) const { return context_->GlProcResolver(name); diff --git a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h b/src/flutter/shell/platform/linux_embedded/surface/surface_gl.h similarity index 62% rename from src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h rename to src/flutter/shell/platform/linux_embedded/surface/surface_gl.h index c38b65ee..44dee0bd 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h +++ b/src/flutter/shell/platform/linux_embedded/surface/surface_gl.h @@ -1,4 +1,4 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,12 +8,12 @@ #include #include "flutter/shell/platform/linux_embedded/surface/context_egl.h" -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface.h" -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl_delegate.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_base.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl_delegate.h" namespace flutter { -class SurfaceGl final : public Surface, public SurfaceGlDelegate { +class SurfaceGl final : public SurfaceBase, public SurfaceGlDelegate { public: SurfaceGl(std::unique_ptr context); ~SurfaceGl() = default; @@ -27,6 +27,13 @@ class SurfaceGl final : public Surface, public SurfaceGlDelegate { // |SurfaceGlDelegate| bool GLContextPresent(uint32_t fbo_id) const override; + // |SurfaceGlDelegate| + bool GLContextPresentWithInfo(const FlutterPresentInfo* info) const override; + + // |SurfaceGlDelegate| + void PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage) const override; + // |SurfaceGlDelegate| uint32_t GLContextFBO() const override; diff --git a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl_delegate.h b/src/flutter/shell/platform/linux_embedded/surface/surface_gl_delegate.h similarity index 67% rename from src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl_delegate.h rename to src/flutter/shell/platform/linux_embedded/surface/surface_gl_delegate.h index 392b277b..709185f7 100644 --- a/src/flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl_delegate.h +++ b/src/flutter/shell/platform/linux_embedded/surface/surface_gl_delegate.h @@ -1,5 +1,5 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,6 +8,8 @@ #include +#include "flutter/shell/platform/embedder/embedder.h" + namespace flutter { class SurfaceGlDelegate { @@ -18,6 +20,12 @@ class SurfaceGlDelegate { virtual bool GLContextPresent(uint32_t fbo_id) const = 0; + virtual bool GLContextPresentWithInfo( + const FlutterPresentInfo* info) const = 0; + + virtual void PopulateExistingDamage(const intptr_t fbo_id, + FlutterDamage* existing_damage) const = 0; + virtual uint32_t GLContextFBO() const = 0; virtual void* GlProcResolver(const char* name) const = 0; diff --git a/src/flutter/shell/platform/linux_embedded/task_runner.cc b/src/flutter/shell/platform/linux_embedded/task_runner.cc index fd28fe96..8a5b617a 100644 --- a/src/flutter/shell/platform/linux_embedded/task_runner.cc +++ b/src/flutter/shell/platform/linux_embedded/task_runner.cc @@ -5,8 +5,8 @@ #include "flutter/shell/platform/linux_embedded/task_runner.h" #include -#include #include +#include namespace flutter { @@ -22,7 +22,7 @@ bool TaskRunner::RunsTasksOnCurrentThread() const { } void TaskRunner::PostFlutterTask(FlutterTask flutter_task, - uint64_t flutter_target_time_nanos) { + uint64_t flutter_target_time_nanos) { Task task; task.fire_time = TimePointFromFlutterTime(flutter_target_time_nanos); task.variant = flutter_task; diff --git a/src/flutter/shell/platform/linux_embedded/task_runner.h b/src/flutter/shell/platform/linux_embedded/task_runner.h index 723a75ac..9b88d3d8 100644 --- a/src/flutter/shell/platform/linux_embedded/task_runner.h +++ b/src/flutter/shell/platform/linux_embedded/task_runner.h @@ -26,7 +26,8 @@ class TaskRunner { using TaskExpiredCallback = std::function; using TaskClosure = std::function; - TaskRunner(std::thread::id main_thread_id, CurrentTimeProc get_current_time, + TaskRunner(std::thread::id main_thread_id, + CurrentTimeProc get_current_time, const TaskExpiredCallback& on_task_expired); ~TaskRunner() = default; diff --git a/src/flutter/shell/platform/linux_embedded/vsync_waiter.cc b/src/flutter/shell/platform/linux_embedded/vsync_waiter.cc new file mode 100644 index 00000000..d6be9db4 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/vsync_waiter.cc @@ -0,0 +1,38 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/vsync_waiter.h" + +#include + +#include "flutter/shell/platform/embedder/embedder.h" +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +VsyncWaiter::VsyncWaiter() : event_counter_(0) {} + +void VsyncWaiter::NotifyWaitForVsync(intptr_t baton) { + std::lock_guard lk(mutex_); + baton_ = baton; + event_counter_++; +} + +void VsyncWaiter::NotifyVsync(FLUTTER_API_SYMBOL(FlutterEngine) engine, + FlutterEngineProcTable* embedder_api, + uint64_t frame_start_time_nanos, + uint64_t frame_target_time_nanos) { + std::lock_guard lk(mutex_); + if (event_counter_ > 0 && baton_ != 0) { + assert(event_counter_ == 1); + event_counter_--; + auto result = embedder_api->OnVsync(engine, baton_, frame_start_time_nanos, + frame_target_time_nanos); + if (result != kSuccess) { + ELINUX_LOG(ERROR) << "FlutterEngineOnVsync failed: batton = " << baton_; + } + } +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/vsync_waiter.h b/src/flutter/shell/platform/linux_embedded/vsync_waiter.h new file mode 100644 index 00000000..aa0f23be --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/vsync_waiter.h @@ -0,0 +1,35 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_VSYNC_WAITER_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_VSYNC_WAITER_H_ + +#include +#include + +#include "flutter/shell/platform/embedder/embedder.h" + +namespace flutter { + +class VsyncWaiter { + public: + VsyncWaiter(); + ~VsyncWaiter() = default; + + void NotifyWaitForVsync(intptr_t baton); + + void NotifyVsync(FLUTTER_API_SYMBOL(FlutterEngine) engine, + FlutterEngineProcTable* embedder_api, + uint64_t frame_start_time_nanos, + uint64_t frame_target_time_nanos); + + private: + intptr_t baton_; + uint32_t event_counter_; + std::mutex mutex_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_VSYNC_WAITER_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/elinux_window.h b/src/flutter/shell/platform/linux_embedded/window/elinux_window.h new file mode 100644 index 00000000..e9b6bf66 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/elinux_window.h @@ -0,0 +1,73 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_H_ + +#include + +#include "flutter/shell/platform/linux_embedded/public/flutter_elinux.h" +#include "flutter/shell/platform/linux_embedded/window_binding_handler.h" + +namespace flutter { + +class ELinuxWindow { + public: + ELinuxWindow() = default; + virtual ~ELinuxWindow() = default; + + protected: + virtual bool IsValid() const = 0; + + // Get current window width in physical pixels. + uint32_t GetCurrentWidth() const { + return view_properties_.width * current_scale_; + } + + // Get current window height in physical pixels. + uint32_t GetCurrentHeight() const { + return view_properties_.height * current_scale_; + } + + void SetRotation(FlutterDesktopViewRotation rotation) { + if (rotation == FlutterDesktopViewRotation::kRotation_90) { + current_rotation_ = 90; + } else if (rotation == FlutterDesktopViewRotation::kRotation_180) { + current_rotation_ = 180; + } else if (rotation == FlutterDesktopViewRotation::kRotation_270) { + current_rotation_ = 270; + } else { + current_rotation_ = 0; + } + } + + void NotifyDisplayInfoUpdates() const { + if (binding_handler_delegate_) { + binding_handler_delegate_->UpdateDisplayInfo( + std::trunc(1000000.0 / frame_rate_), GetCurrentWidth(), + GetCurrentHeight(), current_scale_); + } + } + + FlutterDesktopViewProperties view_properties_; + + // A pointer to a FlutterWindowsView that can be used to update engine + // windowing and input state. + WindowBindingHandlerDelegate* binding_handler_delegate_ = nullptr; + + int32_t display_max_width_ = -1; + int32_t display_max_height_ = -1; + int32_t frame_rate_ = 60000; + double current_scale_ = 1.0; + uint16_t current_rotation_ = 0; + // The x coordinate of the pointer in physical pixels. + double pointer_x_ = 0; + // The y coordinate of the pointer in physical pixels. + double pointer_y_ = 0; + std::string clipboard_data_ = ""; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_drm.h b/src/flutter/shell/platform/linux_embedded/window/elinux_window_drm.h similarity index 54% rename from src/flutter/shell/platform/linux_embedded/window/linuxes_window_drm.h rename to src/flutter/shell/platform/linux_embedded/window/elinux_window_drm.h index 2d89cd43..fa1fb9e9 100644 --- a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_drm.h +++ b/src/flutter/shell/platform/linux_embedded/window/elinux_window_drm.h @@ -1,21 +1,31 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_DRM_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_DRM_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_DRM_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_DRM_H_ #include #include #include -#include #include +#ifdef USE_LIBSYSTEMD +#include +#else +#include +#endif + +#include #include +#include +#include +#include +#include #include "flutter/shell/platform/linux_embedded/logger.h" -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h" -#include "flutter/shell/platform/linux_embedded/window/linuxes_window.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window.h" #include "flutter/shell/platform/linux_embedded/window/native_window_drm.h" #include "flutter/shell/platform/linux_embedded/window_binding_handler.h" @@ -27,24 +37,23 @@ constexpr char kDrmDeviceDefaultFilename[] = "/dev/dri/card0"; } // namespace template -class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { +class ELinuxWindowDrm : public ELinuxWindow, public WindowBindingHandler { public: - LinuxesWindowDrm(FlutterWindowMode window_mode, int32_t width, int32_t height, - bool show_cursor) + ELinuxWindowDrm(FlutterDesktopViewProperties view_properties) : display_valid_(false), is_pending_cursor_add_event_(false) { - window_mode_ = window_mode; - current_width_ = width; - current_height_ = height; - show_cursor_ = show_cursor; + view_properties_ = view_properties; + current_scale_ = + view_properties.force_scale_factor ? view_properties.scale_factor : 1.0; + SetRotation(view_properties_.view_rotation); auto udev = udev_new(); if (!udev) { - LINUXES_LOG(ERROR) << "Failed to create udev instance."; + ELINUX_LOG(ERROR) << "Failed to create udev instance."; return; } libinput_ = libinput_udev_create_context(&kLibinputInterface, NULL, udev); if (!libinput_) { - LINUXES_LOG(ERROR) << "Failed to create libinput instance."; + ELINUX_LOG(ERROR) << "Failed to create libinput instance."; udev_unref(udev); return; } @@ -53,42 +62,69 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { constexpr char kSeatId[] = "seat0"; auto ret = libinput_udev_assign_seat(libinput_, kSeatId); if (ret != 0) { - LINUXES_LOG(ERROR) << "Failed to assign udev seat to libinput instance."; + ELINUX_LOG(ERROR) << "Failed to assign udev seat to libinput instance."; return; } +#ifdef USE_LIBSYSTEMD ret = sd_event_new(&libinput_event_loop_); if (ret < 0) { - LINUXES_LOG(ERROR) << "Failed to create libinput event loop."; + ELINUX_LOG(ERROR) << "Failed to create libinput event loop."; return; } ret = sd_event_add_io(libinput_event_loop_, NULL, libinput_get_fd(libinput_), EPOLLIN | EPOLLRDHUP | EPOLLPRI, OnLibinputEvent, this); if (ret < 0) { - LINUXES_LOG(ERROR) << "Failed to listen for user input."; + ELINUX_LOG(ERROR) << "Failed to listen for user input."; libinput_event_loop_ = sd_event_unref(libinput_event_loop_); return; } +#else // #ifdef USE_LIBSYSTEMD + ret = uv_loop_init(&main_loop_); + if (ret) { + ELINUX_LOG(ERROR) << "Failed to create main event loop."; + return; + } + ret = uv_poll_init(&main_loop_, &libinput_event_loop_, + libinput_get_fd(libinput_)); + if (ret) { + ELINUX_LOG(ERROR) << "Failed to create libinput event loop."; + return; + } + libinput_event_loop_.data = this; + ret = uv_poll_start(&libinput_event_loop_, + UV_READABLE | UV_DISCONNECT | UV_PRIORITIZED, + OnLibinputEvent); + if (ret) { + ELINUX_LOG(ERROR) << "Failed to listen for user input."; + return; + } +#endif // #ifdef USE_LIBSYSTEMD } - ~LinuxesWindowDrm() { + ~ELinuxWindowDrm() { +#ifdef USE_LIBSYSTEMD if (udev_drm_event_loop_) { sd_event_unref(udev_drm_event_loop_); } +#endif if (udev_monitor_) { udev_monitor_unref(udev_monitor_); } +#ifdef USE_LIBSYSTEMD if (libinput_event_loop_) { sd_event_unref(libinput_event_loop_); } +#endif + libinput_unref(libinput_); display_valid_ = false; } - // |LinuxesWindow| + // |ELinuxWindow| bool IsValid() const override { if (!display_valid_ || !native_window_ || !render_surface_ || !native_window_->IsValid() || !render_surface_->IsValid()) { @@ -99,46 +135,72 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { // |FlutterWindowBindingHandler| bool DispatchEvent() override { +#ifdef USE_LIBSYSTEMD constexpr uint64_t kMaxWaitTime = 0; sd_event_run(libinput_event_loop_, kMaxWaitTime); sd_event_run(udev_drm_event_loop_, kMaxWaitTime); +#else + uv_run(&main_loop_, UV_RUN_NOWAIT); +#endif return true; } // |FlutterWindowBindingHandler| - bool CreateRenderSurface(int32_t width, int32_t height) override { + bool CreateRenderSurface(int32_t width, + int32_t height, + bool enable_impeller) override { + std::vector devices; auto device_filename = std::getenv(kFlutterDrmDeviceEnvironmentKey); - if ((!device_filename) || (device_filename[0] == '\0')) { - LINUXES_LOG(WARNING) << kFlutterDrmDeviceEnvironmentKey - << " is not set, use " << kDrmDeviceDefaultFilename; - device_filename = const_cast(kDrmDeviceDefaultFilename); - } - native_window_ = std::make_unique(device_filename); - if (!native_window_->IsValid()) { - LINUXES_LOG(ERROR) << "Failed to create the native window"; - return false; - } + if (device_filename && device_filename[0] != '\0') { + devices = split(std::string(device_filename), ':'); + } + if (devices.empty()) { + ELINUX_LOG(WARNING) << kFlutterDrmDeviceEnvironmentKey + << " is not set, use " << kDrmDeviceDefaultFilename; + devices.push_back(const_cast(kDrmDeviceDefaultFilename)); + } + + bool device_found = false; + for (auto i = 0; i < devices.size(); i++) { + native_window_ = std::make_unique( + devices[i].c_str(), current_rotation_, view_properties_.enable_vsync); + if (!native_window_->IsValid()) { + ELINUX_LOG(ERROR) << "Failed to create the native window (" + << devices[i] << ")."; + native_window_ = nullptr; + continue; + } - if (!RegisterUdevDrmEventLoop(device_filename)) { - LINUXES_LOG(ERROR) << "Failed to register udev drm event loop."; + if (!RegisterUdevDrmEventLoop(devices[i].c_str())) { + ELINUX_LOG(ERROR) << "Failed to register udev drm event loop (" + << devices[i] << ")."; + native_window_ = nullptr; + continue; + } + + device_found = true; + ELINUX_LOG(INFO) << devices[i] << " was selected as the DRM device."; + } + if (!device_found) { return false; } + display_valid_ = true; - render_surface_ = native_window_->CreateRenderSurface(); + render_surface_ = native_window_->CreateRenderSurface(enable_impeller); if (!render_surface_->SetNativeWindow(native_window_.get())) { return false; } - if (window_mode_ != FlutterWindowMode::kFullscreen) { - LINUXES_LOG(WARNING) + if (view_properties_.view_mode != FlutterDesktopViewMode::kFullscreen) { + ELINUX_LOG(WARNING) << "Normal mode is not supported, use fullscreen mode."; - window_mode_ = FlutterWindowMode::kFullscreen; + view_properties_.view_mode = FlutterDesktopViewMode::kFullscreen; } - current_width_ = native_window_->Width(); - current_height_ = native_window_->Height(); - LINUXES_LOG(INFO) << "Display output resolution: " << current_width_ << "x" - << current_height_; + view_properties_.width = native_window_->Width(); + view_properties_.height = native_window_->Height(); + ELINUX_LOG(INFO) << "Display output resolution: " << view_properties_.width + << "x" << view_properties_.height; if (is_pending_cursor_add_event_) { native_window_->ShowCursor(pointer_x_, pointer_y_); @@ -161,10 +223,13 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { } // |FlutterWindowBindingHandler| - LinuxesRenderSurfaceTarget* GetRenderSurfaceTarget() const override { + ELinuxRenderSurfaceTarget* GetRenderSurfaceTarget() const override { return render_surface_.get(); } + // |FlutterWindowBindingHandler| + uint16_t GetRotationDegree() const override { return current_rotation_; } + // |FlutterWindowBindingHandler| double GetDpiScale() override { return current_scale_; } @@ -173,9 +238,12 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { return {GetCurrentWidth(), GetCurrentHeight()}; } + // |FlutterWindowBindingHandler| + int32_t GetFrameRate() override { return 60000; } + // |FlutterWindowBindingHandler| void UpdateFlutterCursor(const std::string& cursor_name) override { - if (show_cursor_) { + if (view_properties_.use_mouse_cursor) { native_window_->UpdateCursor(cursor_name, pointer_x_, pointer_y_); } } @@ -195,11 +263,11 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { protected: static constexpr libinput_interface kLibinputInterface = { - .open_restricted = [](const char* path, int flags, - void* user_data) -> int { + .open_restricted = + [](const char* path, int flags, void* user_data) -> int { auto ret = open(path, flags | O_CLOEXEC); if (ret == -1) { - LINUXES_LOG(ERROR) + ELINUX_LOG(ERROR) << "Failed to open " << path << ", error: " << strerror(errno); } return ret; @@ -210,14 +278,14 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { bool RegisterUdevDrmEventLoop(const std::string& device_filename) { auto udev = udev_new(); if (!udev) { - LINUXES_LOG(ERROR) << "Failed to create udev instance."; + ELINUX_LOG(ERROR) << "Failed to create udev instance."; return false; } constexpr char kUdevMonitorSystemUdev[] = "udev"; udev_monitor_ = udev_monitor_new_from_netlink(udev, kUdevMonitorSystemUdev); if (!udev_monitor_) { - LINUXES_LOG(ERROR) << "Failed to create udev monitor."; + ELINUX_LOG(ERROR) << "Failed to create udev monitor."; udev_unref(udev); return false; } @@ -225,93 +293,132 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { constexpr char kUdevMonitorSubsystemDrm[] = "drm"; if (udev_monitor_filter_add_match_subsystem_devtype( udev_monitor_, kUdevMonitorSubsystemDrm, NULL) < 0) { - LINUXES_LOG(ERROR) << "Failed to filter udev monitor."; + ELINUX_LOG(ERROR) << "Failed to filter udev monitor."; udev_unref(udev); return false; } constexpr char kFileNameSeparator[] = "/"; - auto pos = device_filename.find_last_of(kFileNameSeparator); - if (pos == std::string::npos) { - LINUXES_LOG(ERROR) << "Failed to get device name position."; - udev_unref(udev); - return false; - } + if (device_filename != "drm-nvdc") { + auto pos = device_filename.find_last_of(kFileNameSeparator); + if (pos == std::string::npos) { + ELINUX_LOG(ERROR) << "Failed to get device name position."; + udev_unref(udev); + return false; + } - auto device_name = device_filename.substr(pos + 1); - auto device = udev_device_new_from_subsystem_sysname( - udev, kUdevMonitorSubsystemDrm, device_name.c_str()); - if (!device) { - LINUXES_LOG(ERROR) << "Failed to get device from " << device_name; - udev_unref(udev); - return false; - } + auto device_name = device_filename.substr(pos + 1); + auto device = udev_device_new_from_subsystem_sysname( + udev, kUdevMonitorSubsystemDrm, device_name.c_str()); + if (!device) { + ELINUX_LOG(ERROR) << "Failed to get device from " << device_name; + udev_unref(udev); + return false; + } - auto sysnum = udev_device_get_sysnum(device); - if (!sysnum) { - LINUXES_LOG(ERROR) << "Failed to get device id."; - udev_unref(udev); - return false; + auto sysnum = udev_device_get_sysnum(device); + if (!sysnum) { + ELINUX_LOG(ERROR) << "Failed to get device id."; + udev_unref(udev); + return false; + } + drm_device_id_ = std::atoi(sysnum); } - drm_device_id_ = std::atoi(sysnum); udev_unref(udev); +#ifdef USE_LIBSYSTEMD if (sd_event_new(&udev_drm_event_loop_) < 0) { - LINUXES_LOG(ERROR) << "Failed to create udev drm event loop."; + ELINUX_LOG(ERROR) << "Failed to create udev drm event loop."; return false; } if (sd_event_add_io( udev_drm_event_loop_, NULL, udev_monitor_get_fd(udev_monitor_), EPOLLIN | EPOLLRDHUP | EPOLLPRI, OnUdevDrmEvent, this) < 0) { - LINUXES_LOG(ERROR) << "Failed to listen for udev drm event."; + ELINUX_LOG(ERROR) << "Failed to listen for udev drm event."; + return false; + } +#else // #ifdef USE_LIBSYSTEMD + if (uv_poll_init(&main_loop_, &udev_drm_event_loop_, + udev_monitor_get_fd(udev_monitor_))) { + ELINUX_LOG(ERROR) << "Failed to create udev drm event loop."; + return false; + } + + udev_drm_event_loop_.data = this; + + if (uv_poll_start(&udev_drm_event_loop_, + UV_READABLE | UV_DISCONNECT | UV_PRIORITIZED, + OnUdevDrmEvent)) { + ELINUX_LOG(ERROR) << "Failed to listen for udev drm event."; return false; } +#endif // #ifdef USE_LIBSYSTEMD if (udev_monitor_enable_receiving(udev_monitor_) < 0) { - LINUXES_LOG(ERROR) << "Failed to enable udev monitor receiving."; + ELINUX_LOG(ERROR) << "Failed to enable udev monitor receiving."; return false; } return true; } - static int OnUdevDrmEvent(sd_event_source* source, int fd, uint32_t revents, +#ifdef USE_LIBSYSTEMD + static int OnUdevDrmEvent(sd_event_source* source, + int fd, + uint32_t revents, void* data) { - auto self = reinterpret_cast(data); + auto self = reinterpret_cast(data); auto device = udev_monitor_receive_device(self->udev_monitor_); if (!device) { - LINUXES_LOG(ERROR) << "Failed to receive udev device."; + ELINUX_LOG(ERROR) << "Failed to receive udev device."; return -1; } +#else + static void OnUdevDrmEvent(uv_poll_t* handle, int status, int events) { + auto self = reinterpret_cast(handle->data); + auto device = udev_monitor_receive_device(self->udev_monitor_); + if (!device) { + ELINUX_LOG(ERROR) << "Failed to receive udev device."; + return; + } +#endif if (self->IsUdevEventHotplug(*device) && - self->native_window_->ConfigureDisplay()) { - if (self->current_width_ != self->native_window_->Width() || - self->current_height_ != self->native_window_->Height()) { - self->current_width_ = self->native_window_->Width(); - self->current_height_ = self->native_window_->Height(); - LINUXES_LOG(INFO) << "Display output resolution: " - << self->current_width_ << "x" - << self->current_height_; + self->native_window_->ConfigureDisplay(self->current_rotation_)) { + auto width = self->native_window_->Width(); + auto height = self->native_window_->Height(); + if (self->current_rotation_ == 90 || self->current_rotation_ == 270) { + std::swap(width, height); + } + if (self->view_properties_.width != width || + self->view_properties_.height != height) { + self->view_properties_.width = width; + self->view_properties_.height = height; + ELINUX_LOG(INFO) << "Display output resolution: " + << self->view_properties_.width << "x" + << self->view_properties_.height; if (self->binding_handler_delegate_) { self->binding_handler_delegate_->OnWindowSizeChanged( - self->current_width_, self->current_height_); + self->view_properties_.width, self->view_properties_.height); } } } udev_device_unref(device); + +#ifdef USE_LIBSYSTEMD return 0; +#endif } bool IsUdevEventHotplug(udev_device& device) { auto sysnum = udev_device_get_sysnum(&device); if (!sysnum) { - LINUXES_LOG(ERROR) << "Failed to get device id."; + ELINUX_LOG(ERROR) << "Failed to get device id."; return false; - } else if (std::atoi(sysnum) != drm_device_id_) { - LINUXES_LOG(ERROR) << "Not expected device id."; + } else if (drm_device_id_ && std::atoi(sysnum) != *drm_device_id_) { + ELINUX_LOG(ERROR) << "Not expected device id."; return false; } @@ -319,7 +426,7 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { auto value = udev_device_get_property_value(&device, kUdevPropertyKeyHotplug); if (!value) { - LINUXES_LOG(ERROR) << "Failed to get udev device property value."; + ELINUX_LOG(ERROR) << "Failed to get udev device property value."; return false; } @@ -327,14 +434,26 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { return std::strcmp(value, kPropertyOn) == 0; } - static int OnLibinputEvent(sd_event_source* source, int fd, uint32_t revents, +#ifdef USE_LIBSYSTEMD + static int OnLibinputEvent(sd_event_source* source, + int fd, + uint32_t revents, void* data) { - auto self = reinterpret_cast(data); + auto self = reinterpret_cast(data); auto ret = libinput_dispatch(self->libinput_); if (ret < 0) { - LINUXES_LOG(ERROR) << "Failed to dispatch libinput events."; + ELINUX_LOG(ERROR) << "Failed to dispatch libinput events."; return -ret; } +#else + static void OnLibinputEvent(uv_poll_t* handle, int uv_status, int uv_events) { + auto self = reinterpret_cast(handle->data); + auto ret = libinput_dispatch(self->libinput_); + if (ret < 0) { + ELINUX_LOG(ERROR) << "Failed to dispatch libinput events."; + return; + } +#endif auto previous_pointer_x = self->pointer_x_; auto previous_pointer_y = self->pointer_y_; @@ -386,33 +505,49 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { libinput_event_destroy(event); } - if (self->show_cursor_ && ((self->pointer_x_ != previous_pointer_x) || - (self->pointer_y_ != previous_pointer_y))) { + if (self->view_properties_.use_mouse_cursor && + ((self->pointer_x_ != previous_pointer_x) || + (self->pointer_y_ != previous_pointer_y))) { self->native_window_->MoveCursor(self->pointer_x_, self->pointer_y_); } +#ifdef USE_LIBSYSTEMD return 0; +#endif } void OnDeviceAdded(libinput_event* event) { auto device = libinput_event_get_device(event); - if ((show_cursor_) && - (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))) { - // When launching the application, either route will be used depending on - // the timing. - if (native_window_) { - native_window_->ShowCursor(pointer_x_, pointer_y_); - } else { - is_pending_cursor_add_event_ = true; - } - } + auto device_data = std::make_unique(); + size_t timestamp = + std::chrono::duration_cast( + std::chrono::high_resolution_clock::now().time_since_epoch()) + .count(); + + device_data->id = timestamp; + device_data->is_pointer_device = false; + libinput_device_set_user_data(device, device_data.get()); + libinput_devices_[timestamp] = std::move(device_data); } void OnDeviceRemoved(libinput_event* event) { auto device = libinput_event_get_device(event); - if (show_cursor_ && - (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER))) { - native_window_->DismissCursor(); + auto device_data = reinterpret_cast( + libinput_device_get_user_data(device)); + + if (view_properties_.use_mouse_cursor && + libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { + if (device_data && device_data->is_pointer_device) { + if (--libinput_pointer_devices_ == 0) { + native_window_->DismissCursor(); + } + } + } + + if (device_data) { + if (libinput_devices_.count(device_data->id) > 0) { + libinput_devices_.erase(libinput_devices_.find(device_data->id)); + } } } @@ -428,19 +563,24 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { } void OnPointerMotion(libinput_event* event) { + DetectPointerDevice(event); if (binding_handler_delegate_) { + auto width = view_properties_.width; + auto height = view_properties_.height; + if (current_rotation_ == 90 || current_rotation_ == 270) { + std::swap(width, height); + } + auto pointer_event = libinput_event_get_pointer_event(event); auto dx = libinput_event_pointer_get_dx(pointer_event); auto dy = libinput_event_pointer_get_dy(pointer_event); auto new_pointer_x = pointer_x_ + dx; new_pointer_x = std::max(0.0, new_pointer_x); - new_pointer_x = - std::min(static_cast(current_width_ - 1), new_pointer_x); + new_pointer_x = std::min(static_cast(width - 1), new_pointer_x); auto new_pointer_y = pointer_y_ + dy; new_pointer_y = std::max(0.0, new_pointer_y); - new_pointer_y = - std::min(static_cast(current_height_ - 1), new_pointer_y); + new_pointer_y = std::min(static_cast(height - 1), new_pointer_y); binding_handler_delegate_->OnPointerMove(new_pointer_x, new_pointer_y); pointer_x_ = new_pointer_x; @@ -449,12 +589,19 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { } void OnPointerMotionAbsolute(libinput_event* event) { + auto width = view_properties_.width; + auto height = view_properties_.height; + if (current_rotation_ == 90 || current_rotation_ == 270) { + std::swap(width, height); + } + + DetectPointerDevice(event); if (binding_handler_delegate_) { auto pointer_event = libinput_event_get_pointer_event(event); - auto x = libinput_event_pointer_get_absolute_x_transformed( - pointer_event, current_width_); - auto y = libinput_event_pointer_get_absolute_y_transformed( - pointer_event, current_height_); + auto x = libinput_event_pointer_get_absolute_x_transformed(pointer_event, + width); + auto y = libinput_event_pointer_get_absolute_y_transformed(pointer_event, + height); binding_handler_delegate_->OnPointerMove(x, y); pointer_x_ = x; @@ -463,6 +610,7 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { } void OnPointerButton(libinput_event* event) { + DetectPointerDevice(event); if (binding_handler_delegate_) { auto pointer_event = libinput_event_get_pointer_event(event); auto button = libinput_event_pointer_get_button(pointer_event); @@ -486,7 +634,7 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { flutter_button = kFlutterPointerButtonMouseForward; break; default: - LINUXES_LOG(ERROR) << "Not expected button input: " << button; + ELINUX_LOG(ERROR) << "Not expected button input: " << button; return; } @@ -501,6 +649,7 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { } void OnPointerAxis(libinput_event* event) { + DetectPointerDevice(event); if (binding_handler_delegate_) { auto pointer_event = libinput_event_get_pointer_event(event); if (libinput_event_pointer_has_axis( @@ -516,15 +665,45 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { } } + void DetectPointerDevice(libinput_event* event) { + auto device = libinput_event_get_device(event); + auto device_data = reinterpret_cast( + libinput_device_get_user_data(device)); + + // Shows the mouse cursor only when connecting mouse devices. + // The mouse cursor is not displayed by touch inputs. + if (device_data && !device_data->is_pointer_device) { + device_data->is_pointer_device = true; + libinput_pointer_devices_++; + ShowMouseCursor(); + } + } + + void ShowMouseCursor() { + if (view_properties_.use_mouse_cursor && libinput_pointer_devices_ == 1) { + // When launching the application, either route will be used depending on + // the timing. + if (native_window_) { + native_window_->ShowCursor(pointer_x_, pointer_y_); + } else { + is_pending_cursor_add_event_ = true; + } + } + } + void OnTouchDown(libinput_event* event) { if (binding_handler_delegate_) { + auto width = view_properties_.width; + auto height = view_properties_.height; + if (current_rotation_ == 90 || current_rotation_ == 270) { + std::swap(width, height); + } + auto touch_event = libinput_event_get_touch_event(event); auto time = libinput_event_touch_get_time(touch_event); auto slot = libinput_event_touch_get_seat_slot(touch_event); - auto x = - libinput_event_touch_get_x_transformed(touch_event, current_width_); - auto y = - libinput_event_touch_get_y_transformed(touch_event, current_height_); + auto x = libinput_event_touch_get_x_transformed(touch_event, width); + auto y = libinput_event_touch_get_y_transformed(touch_event, height); binding_handler_delegate_->OnTouchDown(time, slot, x, y); } } @@ -540,13 +719,17 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { void OnTouchMotion(libinput_event* event) { if (binding_handler_delegate_) { + auto width = view_properties_.width; + auto height = view_properties_.height; + if (current_rotation_ == 90 || current_rotation_ == 270) { + std::swap(width, height); + } + auto touch_event = libinput_event_get_touch_event(event); auto time = libinput_event_touch_get_time(touch_event); auto slot = libinput_event_touch_get_seat_slot(touch_event); - auto x = - libinput_event_touch_get_x_transformed(touch_event, current_width_); - auto y = - libinput_event_touch_get_y_transformed(touch_event, current_height_); + auto x = libinput_event_touch_get_x_transformed(touch_event, width); + auto y = libinput_event_touch_get_y_transformed(touch_event, height); binding_handler_delegate_->OnTouchMotion(time, slot, x, y); } } @@ -576,7 +759,7 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { value = libinput_event_pointer_get_axis_value(pointer_event, axis); break; default: - LINUXES_LOG(ERROR) << "Not expected axis source: " << source; + ELINUX_LOG(ERROR) << "Not expected axis source: " << source; return; } @@ -588,24 +771,47 @@ class LinuxesWindowDrm : public LinuxesWindow, public WindowBindingHandler { kScrollOffsetMultiplier); } - // A pointer to a FlutterWindowsView that can be used to update engine - // windowing and input state. - WindowBindingHandlerDelegate* binding_handler_delegate_ = nullptr; + std::vector split(std::string s, char delim) { + std::vector res; + std::string item; + std::stringstream ss(s); + + while (getline(ss, item, delim)) { + if (!item.empty()) { + res.push_back(item); + } + } + return res; + } + + struct LibinputDeviceData { + size_t id; + bool is_pointer_device; + }; std::unique_ptr native_window_; std::unique_ptr render_surface_; bool display_valid_; bool is_pending_cursor_add_event_; - - sd_event* libinput_event_loop_; libinput* libinput_; + std::unordered_map> + libinput_devices_; + int libinput_pointer_devices_ = 0; - sd_event* udev_drm_event_loop_ = nullptr; udev_monitor* udev_monitor_ = nullptr; - int drm_device_id_; + std::optional drm_device_id_; + +#ifdef USE_LIBSYSTEMD + sd_event* libinput_event_loop_; + sd_event* udev_drm_event_loop_ = nullptr; +#else + uv_loop_t main_loop_; + uv_poll_t libinput_event_loop_; + uv_poll_t udev_drm_event_loop_; +#endif }; } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_DRM_H_ +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_DRM_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/elinux_window_wayland.cc b/src/flutter/shell/platform/linux_embedded/window/elinux_window_wayland.cc new file mode 100644 index 00000000..6467bf94 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/elinux_window_wayland.cc @@ -0,0 +1,1931 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/window/elinux_window_wayland.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/surface/context_egl.h" + +namespace flutter { + +namespace { +constexpr char kZwpTextInputManagerV1[] = "zwp_text_input_manager_v1"; +constexpr char kZwpTextInputManagerV3[] = "zwp_text_input_manager_v3"; +constexpr char kZxdgDecorationManagerV1[] = "zxdg_decoration_manager_v1"; + +constexpr char kWlCursorThemeBottomLeftCorner[] = "bottom_left_corner"; +constexpr char kWlCursorThemeBottomRightCorner[] = "bottom_right_corner"; +constexpr char kWlCursorThemeBottomSide[] = "bottom_side"; +constexpr char kWlCursorThemeGrabbing[] = "grabbing"; +constexpr char kWlCursorThemeLeftPtr[] = "left_ptr"; +constexpr char kWlCursorThemeLeftSide[] = "left_side"; +constexpr char kWlCursorThemeRightSide[] = "right_side"; +constexpr char kWlCursorThemeTopLeftCorner[] = "top_left_corner"; +constexpr char kWlCursorThemeTopRightCorner[] = "top_right_corner"; +constexpr char kWlCursorThemeTopSide[] = "top_side"; +constexpr char kWlCursorThemeXterm[] = "xterm"; +constexpr char kWlCursorThemeHand1[] = "hand1"; +constexpr char kWlCursorThemeWatch[] = "watch"; +constexpr char kCursorNameNone[] = "none"; + +constexpr char kClipboardMimeTypeText[] = "text/plain"; +} // namespace + +const wl_registry_listener ELinuxWindowWayland::kWlRegistryListener = { + .global = + [](void* data, + wl_registry* wl_registry, + uint32_t name, + const char* interface, + uint32_t version) { + ELINUX_LOG(TRACE) << "wl_registry_listener.global"; + + auto self = reinterpret_cast(data); + self->WlRegistryHandler(wl_registry, name, interface, version); + }, + .global_remove = + [](void* data, wl_registry* wl_registry, uint32_t name) { + ELINUX_LOG(TRACE) << "wl_registry_listener.global_remove"; + + auto self = reinterpret_cast(data); + self->WlUnRegistryHandler(wl_registry, name); + }, +}; + +const xdg_wm_base_listener ELinuxWindowWayland::kXdgWmBaseListener = { + .ping = + [](void* data, xdg_wm_base* xdg_wm_base, uint32_t serial) { + ELINUX_LOG(TRACE) << "xdg_wm_base_listener.ping"; + xdg_wm_base_pong(xdg_wm_base, serial); + }, +}; + +const xdg_surface_listener ELinuxWindowWayland::kXdgSurfaceListener = { + .configure = + [](void* data, xdg_surface* xdg_surface, uint32_t serial) { + ELINUX_LOG(TRACE) << "xdg_surface_listener.configure"; + + auto self = reinterpret_cast(data); + constexpr int32_t x = 0; + int32_t y = 0; + auto width = self->view_properties_.width; + auto height = self->view_properties_.height; + + if (self->window_decorations_) { + // Shift the window position to the bottom to show decoration + // even when the window is displayed in the upper left corner + // of the screen + y = -self->window_decorations_->Height(); + if (!self->maximised_) { + height -= y; + } + + // clip + if (self->display_max_height_ > 0) { + height = std::min(height, self->display_max_height_); + } + } + xdg_surface_set_window_geometry(xdg_surface, x, y, width, height); + + xdg_surface_ack_configure(xdg_surface, serial); + if (self->wait_for_configure_) { + self->wait_for_configure_ = false; + } + self->request_redraw_ = true; + }, +}; + +const xdg_toplevel_listener ELinuxWindowWayland::kXdgToplevelListener = { + .configure = + [](void* data, + xdg_toplevel* xdg_toplevel, + int32_t width, + int32_t height, + wl_array* states) { + ELINUX_LOG(TRACE) + << "xdg_toplevel_listener.configure: " << width << ", " << height; + + auto self = reinterpret_cast(data); + if (self->current_rotation_ == 90 || self->current_rotation_ == 270) { + std::swap(width, height); + } + + int32_t next_width_dip = width / self->current_scale_; + int32_t next_height_dip = height / self->current_scale_; + if (self->restore_window_required_) { + self->restore_window_required_ = false; + next_width_dip = self->restore_window_width_; + next_height_dip = self->restore_window_height_; + } + + if (!next_width_dip || !next_height_dip || + (self->view_properties_.width == next_width_dip && + self->view_properties_.height == next_height_dip)) { + return; + } + + ELINUX_LOG(TRACE) << "request redraw: " << next_width_dip << ", " + << next_height_dip; + self->view_properties_.width = next_width_dip; + self->view_properties_.height = next_height_dip; + self->request_redraw_ = true; + }, + .close = + [](void* data, xdg_toplevel* xdg_toplevel) { + ELINUX_LOG(TRACE) << "xdg_toplevel_listener.close"; + + auto self = reinterpret_cast(data); + self->running_ = false; + }}; + +const wl_surface_listener ELinuxWindowWayland::kWlSurfaceListener = { + .enter = + [](void* data, wl_surface* wl_surface, wl_output* output) { + ELINUX_LOG(TRACE) << "wl_surface_listener.enter"; + + // The compositor can send a null output: crbug.com/1332540 + if (!output) { + ELINUX_LOG(ERROR) << "cannot enter a NULL output"; + return; + } + + const uint32_t output_id = + wl_proxy_get_id(reinterpret_cast(output)); + ELINUX_LOG(TRACE) << "window entered output " << output_id; + auto self = reinterpret_cast(data); + + self->entered_outputs_.insert(output_id); + self->UpdateWindowScale(); + }, + .leave = + [](void* data, wl_surface* wl_surface, wl_output* output) { + ELINUX_LOG(TRACE) << "wl_surface_listener.leave"; + + // The compositor can send a null output: crbug.com/1332540 + if (!output) { + ELINUX_LOG(ERROR) << "cannot leave a NULL output"; + return; + } + + const uint32_t output_id = + wl_proxy_get_id(reinterpret_cast(output)); + ELINUX_LOG(TRACE) << "window left output " << output_id; + auto self = reinterpret_cast(data); + + self->entered_outputs_.erase(output_id); + self->UpdateWindowScale(); + }}; + +const wp_presentation_listener ELinuxWindowWayland::kWpPresentationListener = { + .clock_id = + [](void* data, wp_presentation* wp_presentation, uint32_t clk_id) { + ELINUX_LOG(TRACE) << "wp_presentation_listener.clock_id"; + + auto self = reinterpret_cast(data); + self->wp_presentation_clk_id_ = clk_id; + ELINUX_LOG(TRACE) << "clk_id = " << clk_id; + }, +}; + +const wp_presentation_feedback_listener + ELinuxWindowWayland::kWpPresentationFeedbackListener = { + .sync_output = + [](void* data, + struct wp_presentation_feedback* wp_presentation_feedback, + wl_output* output) { + ELINUX_LOG(TRACE) + << "wp_presentation_feedback_listener.sync_output"; + }, + .presented = + [](void* data, + struct wp_presentation_feedback* wp_presentation_feedback, + uint32_t tv_sec_hi, + uint32_t tv_sec_lo, + uint32_t tv_nsec, + uint32_t refresh, + uint32_t seq_hi, + uint32_t seq_lo, + uint32_t flags) { + ELINUX_LOG(TRACE) + << "wp_presentation_feedback_listener.presented"; + + auto self = reinterpret_cast(data); + self->last_frame_time_nanos_ = + (((static_cast(tv_sec_hi) << 32) + tv_sec_lo) * + 1000000000) + + tv_nsec; + self->frame_rate_ = + static_cast(std::round(1000000000000.0 / refresh)); + + if (self->window_decorations_) { + self->window_decorations_->Draw(); + } + + wp_presentation_feedback_destroy(wp_presentation_feedback); + wp_presentation_feedback_add_listener( + ::wp_presentation_feedback(self->wp_presentation_, + self->native_window_->Surface()), + &kWpPresentationFeedbackListener, data); + }, + .discarded = + [](void* data, + struct wp_presentation_feedback* wp_presentation_feedback) { + ELINUX_LOG(TRACE) + << "wp_presentation_feedback_listener.discarded"; + + auto self = reinterpret_cast(data); + if (self->window_decorations_) { + self->window_decorations_->Draw(); + } + + wp_presentation_feedback_destroy(wp_presentation_feedback); + wp_presentation_feedback_add_listener( + ::wp_presentation_feedback(self->wp_presentation_, + self->native_window_->Surface()), + &kWpPresentationFeedbackListener, data); + }, +}; + +const wl_callback_listener ELinuxWindowWayland::kWlSurfaceFrameListener = { + .done = + [](void* data, wl_callback* wl_callback, uint32_t time) { + ELINUX_LOG(TRACE) << "wl_callback_listener.done"; + + // The presentation-time is an extended protocol and isn't supported + // by all compositors. This path is for when it wasn't supported. + auto self = reinterpret_cast(data); + if (self->wp_presentation_clk_id_ != UINT32_MAX) { + return; + } + + if (self->window_decorations_) { + self->window_decorations_->Draw(); + } + + self->last_frame_time_nanos_ = static_cast(time) * 1000000; + + auto callback = wl_surface_frame(self->native_window_->Surface()); + wl_callback_destroy(wl_callback); + wl_callback_add_listener(callback, &kWlSurfaceFrameListener, data); + }, +}; + +const wl_seat_listener ELinuxWindowWayland::kWlSeatListener = { + .capabilities = [](void* data, wl_seat* seat, uint32_t caps) -> void { + ELINUX_LOG(TRACE) << "wl_seat_listener.capabilities"; + + auto self = reinterpret_cast(data); + auto seat_iter = self->seat_inputs_map_.find(seat); + if (seat_iter == self->seat_inputs_map_.end()) { + ELINUX_LOG(ERROR) << "Failed to find the seat"; + return; + } + auto& inputs = seat_iter->second; + + if ((caps & WL_SEAT_CAPABILITY_POINTER) && !inputs.pointer) { + inputs.pointer = wl_seat_get_pointer(seat); + wl_pointer_add_listener(inputs.pointer, &kWlPointerListener, self); + } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && inputs.pointer) { + wl_pointer_release(inputs.pointer); + inputs.pointer = nullptr; + } + + if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !inputs.touch) { + inputs.touch = wl_seat_get_touch(seat); + wl_touch_add_listener(inputs.touch, &kWlTouchListener, self); + } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && inputs.touch) { + wl_touch_release(inputs.touch); + inputs.touch = nullptr; + } + + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !inputs.keyboard) { + inputs.keyboard = wl_seat_get_keyboard(seat); + wl_keyboard_add_listener(inputs.keyboard, &kWlKeyboardListener, self); + } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && inputs.keyboard) { + wl_keyboard_release(inputs.keyboard); + inputs.keyboard = nullptr; + } + }, + .name = [](void* data, struct wl_seat* wl_seat, const char* name) -> void { + ELINUX_LOG(TRACE) << "wl_seat_listener.name"; + }, +}; + +const wl_pointer_listener ELinuxWindowWayland::kWlPointerListener = { + .enter = [](void* data, + wl_pointer* wl_pointer, + uint32_t serial, + wl_surface* surface, + wl_fixed_t surface_x, + wl_fixed_t surface_y) -> void { + ELINUX_LOG(TRACE) << "wl_pointer_listener.enter"; + + auto self = reinterpret_cast(data); + self->wl_current_surface_ = surface; + self->serial_ = serial; + + if (self->view_properties_.use_mouse_cursor) { + self->cursor_info_.pointer = wl_pointer; + self->cursor_info_.serial = serial; + } + + if (self->binding_handler_delegate_) { + double x_px = wl_fixed_to_double(surface_x) * self->current_scale_; + double y_px = wl_fixed_to_double(surface_y) * self->current_scale_; + self->binding_handler_delegate_->OnPointerMove(x_px, y_px); + self->pointer_x_ = x_px; + self->pointer_y_ = y_px; + } + + // Force redraw even though it is the same cursor name + auto copy = self->cursor_info_.cursor_name; + self->cursor_info_.cursor_name.clear(); + self->UpdateFlutterCursor(copy); + }, + .leave = [](void* data, + wl_pointer* pointer, + uint32_t serial, + wl_surface* surface) -> void { + ELINUX_LOG(TRACE) << "wl_pointer_listener.leave"; + + auto self = reinterpret_cast(data); + self->wl_current_surface_ = surface; + self->serial_ = serial; + + if (self->view_properties_.use_mouse_cursor) { + // Clear the cursor name in order to make sure it gets redrawn next time + // it enters the surface. + self->cursor_info_.cursor_name.clear(); + } + + if (self->binding_handler_delegate_) { + self->binding_handler_delegate_->OnPointerLeave(); + self->pointer_x_ = -1; + self->pointer_y_ = -1; + } + }, + .motion = [](void* data, + wl_pointer* pointer, + uint32_t time, + wl_fixed_t surface_x, + wl_fixed_t surface_y) -> void { + ELINUX_LOG(TRACE) << "wl_pointer_listener.motion"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_) { + double x_px = wl_fixed_to_double(surface_x) * self->current_scale_; + double y_px = wl_fixed_to_double(surface_y) * self->current_scale_; + self->binding_handler_delegate_->OnPointerMove(x_px, y_px); + self->pointer_x_ = x_px; + self->pointer_y_ = y_px; + } + }, + .button = [](void* data, + wl_pointer* pointer, + uint32_t serial, + uint32_t time, + uint32_t button, + uint32_t status) -> void { + ELINUX_LOG(TRACE) << "wl_pointer_listener.button"; + + auto self = reinterpret_cast(data); + self->serial_ = serial; + + if (button == BTN_LEFT && status == WL_POINTER_BUTTON_STATE_PRESSED) { + if (self->window_decorations_ && + self->window_decorations_->IsMatched( + self->wl_current_surface_, + WindowDecoration::DecorationType::TITLE_BAR)) { + for (auto& [seat, inputs] : self->seat_inputs_map_) { + if (inputs.pointer == pointer) { + xdg_toplevel_move(self->xdg_toplevel_, seat, serial); + return; + } + } + ELINUX_LOG(TRACE) + << "Failed to find the pointer for moving the window"; + return; + } + + if (self->window_decorations_ && + self->window_decorations_->IsMatched( + self->wl_current_surface_, + WindowDecoration::DecorationType::CLOSE_BUTTON)) { + self->running_ = false; + return; + } + + if (self->window_decorations_ && + self->window_decorations_->IsMatched( + self->wl_current_surface_, + WindowDecoration::DecorationType::MAXIMISE_BUTTON)) { + if (self->maximised_) { + xdg_toplevel_unset_maximized(self->xdg_toplevel_); + + // Requests to return to the original window size before maximizing. + self->restore_window_required_ = true; + } else { + // Stores original window size. + self->restore_window_width_ = self->view_properties_.width; + self->restore_window_height_ = self->view_properties_.height; + self->restore_window_required_ = false; + + xdg_toplevel_set_maximized(self->xdg_toplevel_); + } + self->maximised_ = !self->maximised_; + return; + } + + if (self->window_decorations_ && + self->window_decorations_->IsMatched( + self->wl_current_surface_, + flutter::WindowDecoration::DecorationType::MINIMISE_BUTTON)) { + xdg_toplevel_set_minimized(self->xdg_toplevel_); + return; + } + } + + if (self->binding_handler_delegate_) { + FlutterPointerMouseButtons flutter_button; + switch (button) { + case BTN_LEFT: + flutter_button = kFlutterPointerButtonMousePrimary; + break; + case BTN_RIGHT: + flutter_button = kFlutterPointerButtonMouseSecondary; + break; + case BTN_MIDDLE: + flutter_button = kFlutterPointerButtonMouseMiddle; + break; + case BTN_BACK: + flutter_button = kFlutterPointerButtonMouseBack; + break; + case BTN_FORWARD: + flutter_button = kFlutterPointerButtonMouseForward; + break; + default: + ELINUX_LOG(ERROR) << "Not expected button input: " << button; + return; + } + + if (status == WL_POINTER_BUTTON_STATE_PRESSED) { + self->binding_handler_delegate_->OnPointerDown( + self->pointer_x_, self->pointer_y_, flutter_button); + } else { + self->binding_handler_delegate_->OnPointerUp( + self->pointer_x_, self->pointer_y_, flutter_button); + } + } + }, + .axis = [](void* data, + wl_pointer* wl_pointer, + uint32_t time, + uint32_t axis, + wl_fixed_t value) -> void { + ELINUX_LOG(TRACE) << "wl_pointer_listener.axis"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_) { + double delta = wl_fixed_to_double(value); + constexpr int32_t kScrollOffsetMultiplier = 20; + self->binding_handler_delegate_->OnScroll( + self->pointer_x_, self->pointer_y_, + axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? 0 : delta, + axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? delta : 0, + kScrollOffsetMultiplier); + } + }, +}; + +const wl_touch_listener ELinuxWindowWayland::kWlTouchListener = { + .down = [](void* data, + wl_touch* wl_touch, + uint32_t serial, + uint32_t time, + wl_surface* surface, + int32_t id, + wl_fixed_t surface_x, + wl_fixed_t surface_y) -> void { + ELINUX_LOG(TRACE) << "wl_touch_listener.down"; + + auto self = reinterpret_cast(data); + self->serial_ = serial; + if (self->binding_handler_delegate_) { + double x = wl_fixed_to_double(surface_x); + double y = wl_fixed_to_double(surface_y); + self->binding_handler_delegate_->OnTouchDown(time, id, x, y); + } + }, + .up = [](void* data, + wl_touch* wl_touch, + uint32_t serial, + uint32_t time, + int32_t id) -> void { + ELINUX_LOG(TRACE) << "wl_touch_listener.up"; + + auto self = reinterpret_cast(data); + self->serial_ = serial; + if (self->binding_handler_delegate_) { + self->binding_handler_delegate_->OnTouchUp(time, id); + } + }, + .motion = [](void* data, + wl_touch* wl_touch, + uint32_t time, + int32_t id, + wl_fixed_t surface_x, + wl_fixed_t surface_y) -> void { + ELINUX_LOG(TRACE) << "wl_touch_listener.motion"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_) { + double x = wl_fixed_to_double(surface_x); + double y = wl_fixed_to_double(surface_y); + self->binding_handler_delegate_->OnTouchMotion(time, id, x, y); + } + }, + .frame = [](void* data, wl_touch* wl_touch) -> void {}, + .cancel = [](void* data, wl_touch* wl_touch) -> void { + ELINUX_LOG(TRACE) << "wl_touch_listener.cancel"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_) { + self->binding_handler_delegate_->OnTouchCancel(); + } + }, +}; + +const wl_keyboard_listener ELinuxWindowWayland::kWlKeyboardListener = { + .keymap = [](void* data, + wl_keyboard* wl_keyboard, + uint32_t format, + int fd, + uint32_t size) -> void { + ELINUX_LOG(TRACE) << "wl_keyboard_listener.keymap"; + + auto self = reinterpret_cast(data); + assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1); + if (self->binding_handler_delegate_) { + self->binding_handler_delegate_->OnKeyMap(format, fd, size); + } + }, + .enter = [](void* data, + wl_keyboard* wl_keyboard, + uint32_t serial, + wl_surface* surface, + wl_array* keys) -> void { + ELINUX_LOG(TRACE) << "wl_keyboard_listener.enter"; + + auto self = reinterpret_cast(data); + self->serial_ = serial; + }, + .leave = [](void* data, + wl_keyboard* wl_keyboard, + uint32_t serial, + wl_surface* surface) -> void { + ELINUX_LOG(TRACE) << "wl_keyboard_listener.leave"; + + auto self = reinterpret_cast(data); + self->serial_ = serial; + }, + .key = [](void* data, + wl_keyboard* wl_keyboard, + uint32_t serial, + uint32_t time, + uint32_t key, + uint32_t state) -> void { + ELINUX_LOG(TRACE) << "wl_keyboard_listener.key"; + + auto self = reinterpret_cast(data); + self->serial_ = serial; + if (self->binding_handler_delegate_) { + self->binding_handler_delegate_->OnKey( + key, state == WL_KEYBOARD_KEY_STATE_PRESSED); + } + }, + .modifiers = [](void* data, + wl_keyboard* wl_keyboard, + uint32_t serial, + uint32_t mods_depressed, + uint32_t mods_latched, + uint32_t mods_locked, + uint32_t group) -> void { + ELINUX_LOG(TRACE) << "wl_keyboard_listener.modifiers"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_) { + self->binding_handler_delegate_->OnKeyModifiers( + mods_depressed, mods_latched, mods_locked, group); + } + }, + .repeat_info = [](void* data, wl_keyboard* wl_keyboard, int rate, int delay) + -> void { ELINUX_LOG(TRACE) << "wl_keyboard_listener.repeat_info"; }, +}; + +const wl_output_listener ELinuxWindowWayland::kWlOutputListener = { + .geometry = [](void* data, + wl_output* wl_output, + int32_t x, + int32_t y, + int32_t physical_width, + int32_t physical_height, + int32_t subpixel, + const char* make, + const char* model, + int32_t output_transform) -> void { + ELINUX_LOG(TRACE) << "wl_output_listener.geometry"; + auto self = reinterpret_cast(data); + self->transform_ = output_transform; + }, + .mode = [](void* data, + wl_output* wl_output, + uint32_t flags, + int32_t width, + int32_t height, + int32_t refresh) -> void { + ELINUX_LOG(TRACE) << "wl_output_listener.mode"; + + auto self = reinterpret_cast(data); + if (flags & WL_OUTPUT_MODE_CURRENT) { + if (self->current_rotation_ == 90 || self->current_rotation_ == 270 || + self->transform_ == WL_OUTPUT_TRANSFORM_90 || + self->transform_ == WL_OUTPUT_TRANSFORM_270 || + self->transform_ == WL_OUTPUT_TRANSFORM_FLIPPED_90 || + self->transform_ == WL_OUTPUT_TRANSFORM_FLIPPED_270) { + std::swap(width, height); + } + + ELINUX_LOG(INFO) << "Display output info: width = " << width + << ", height = " << height + << ", refresh = " << refresh; + self->display_max_width_ = width; + self->display_max_height_ = height; + + // Some composers send 0 for the refresh value. + if (refresh != 0) { + self->frame_rate_ = refresh; + } + + if (self->view_properties_.view_mode == + FlutterDesktopViewMode::kFullscreen) { + self->view_properties_.width = width; + self->view_properties_.height = height; + self->request_redraw_ = true; + } + + if (self->view_properties_.width > width) { + ELINUX_LOG(WARNING) + << "Requested width size(" << self->view_properties_.width << ") " + << "is larger than display size(" << width + << ")" + ", clipping"; + self->view_properties_.width = width; + self->request_redraw_ = true; + } + if (self->view_properties_.height > height) { + ELINUX_LOG(WARNING) + << "Requested height size(" << self->view_properties_.height + << ") " << "is larger than display size(" << height + << ")" + ", clipping"; + self->view_properties_.height = height; + self->request_redraw_ = true; + } + } + }, + .done = [](void* data, wl_output* wl_output) -> void { + ELINUX_LOG(TRACE) << "wl_output_listener.done"; + }, + .scale = [](void* data, wl_output* wl_output, int32_t scale) -> void { + ELINUX_LOG(TRACE) << "wl_output_listener.scale"; + + auto self = reinterpret_cast(data); + const uint32_t output_id = + wl_proxy_get_id(reinterpret_cast(wl_output)); + + ELINUX_LOG(INFO) << "Display scale for output(" << output_id + << "): " << scale; + + self->wl_output_scale_factors_[output_id] = scale; + + self->UpdateWindowScale(); + }, +}; + +const zwp_text_input_v1_listener ELinuxWindowWayland::kZwpTextInputV1Listener = + { + .enter = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + wl_surface* surface) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.enter"; + + auto self = reinterpret_cast(data); + // If there is no input data, the backspace key cannot be used, + // so set dummy data. + if (self->zwp_text_input_v1_) { + zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, + " ", 1, 1); + } + }, + .leave = [](void* data, zwp_text_input_v1* zwp_text_input_v1) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.leave"; + + auto self = reinterpret_cast(data); + if (self->zwp_text_input_v1_) { + zwp_text_input_v1_hide_input_panel(self->zwp_text_input_v1_); + } + }, + .modifiers_map = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + wl_array* map) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.modifiers_map"; + }, + .input_panel_state = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + uint32_t state) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.input_panel_state"; + }, + .preedit_string = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + uint32_t serial, + const char* text, + const char* commit) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.preedit_string"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_ && strlen(text)) { + std::wstring_convert, char16_t> + utf8_converter; + std::u16string utf16_text = utf8_converter.from_bytes(text); + self->binding_handler_delegate_->OnVirtualKey(utf16_text[0]); + } + if (self->zwp_text_input_v1_) { + zwp_text_input_v1_reset(self->zwp_text_input_v1_); + // If there is no input data, the backspace key cannot be used, + // so set dummy data. + zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, + " ", 1, 1); + } + }, + .preedit_styling = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + uint32_t index, + uint32_t length, + uint32_t style) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.preedit_styling"; + }, + .preedit_cursor = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + int32_t index) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.preedit_cursor"; + }, + .commit_string = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + uint32_t serial, + const char* text) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.commit_string"; + + // commit_string is notified only when the space key is pressed. + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_ && strlen(text)) { + std::wstring_convert, char16_t> + utf8_converter; + std::u16string utf16_text = utf8_converter.from_bytes(text); + self->binding_handler_delegate_->OnVirtualKey(utf16_text[0]); + } + // If there is no input data, the backspace key cannot be used, + // so set dummy data. + if (self->zwp_text_input_v1_) { + zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, + " ", 1, 1); + } + }, + .cursor_position = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + int32_t index, + int32_t anchor) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.cursor_position"; + }, + .delete_surrounding_text = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + int32_t index, + uint32_t length) -> void { + ELINUX_LOG(TRACE) + << "zwp_text_input_v1_listener.delete_surrounding_text"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_) { + self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_BACKSPACE); + } + // If there is no input data, the backspace key cannot be used, + // so set dummy data. + if (self->zwp_text_input_v1_) { + zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, + " ", 1, 1); + } + }, + .keysym = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + uint32_t serial, + uint32_t time, + uint32_t sym, + uint32_t state, + uint32_t modifiers) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.keysym"; + + auto self = reinterpret_cast(data); + if ((state == WL_KEYBOARD_KEY_STATE_PRESSED) && + (self->binding_handler_delegate_)) { + switch (sym) { + case XKB_KEY_Left: + self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_LEFT); + break; + case XKB_KEY_Right: + self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_RIGHT); + break; + case XKB_KEY_Up: + self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_UP); + break; + case XKB_KEY_Down: + self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_DOWN); + break; + case XKB_KEY_Tab: + self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_TAB); + break; + case XKB_KEY_Return: + self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_ENTER); + break; + default: + break; + } + } + }, + .language = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + uint32_t serial, + const char* language) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.language"; + }, + .text_direction = [](void* data, + zwp_text_input_v1* zwp_text_input_v1, + uint32_t serial, + uint32_t direction) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v1_listener.text_direction"; + }, +}; + +const zwp_text_input_v3_listener ELinuxWindowWayland::kZwpTextInputV3Listener = + { + .enter = [](void* data, + zwp_text_input_v3* zwp_text_input_v3, + wl_surface* surface) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v3_listener.enter"; + + // To appear the on-screen keyboard when the user returns to a Flutter + // app which needs to show the on-screen keyboard. + auto self = reinterpret_cast(data); + if (self->is_requested_show_virtual_keyboard_) { + self->ShowVirtualKeyboard(); + } + }, + .leave = [](void* data, + zwp_text_input_v3* zwp_text_input_v3, + wl_surface* surface) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v3_listener.leave"; + }, + .preedit_string = [](void* data, + zwp_text_input_v3* zwp_text_input_v3, + const char* text, + int32_t cursor_begin, + int32_t cursor_end) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v3_listener.preedit_string"; + }, + .commit_string = [](void* data, + zwp_text_input_v3* zwp_text_input_v3, + const char* text) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v3_listener.commit_string"; + + auto self = reinterpret_cast(data); + if (self->binding_handler_delegate_ && strlen(text)) { + std::wstring_convert, char16_t> + utf8_converter; + std::u16string utf16_text = utf8_converter.from_bytes(text); + self->binding_handler_delegate_->OnVirtualKey(utf16_text[0]); + } + }, + .delete_surrounding_text = [](void* data, + zwp_text_input_v3* zwp_text_input_v3, + uint32_t before_length, + uint32_t after_length) -> void { + ELINUX_LOG(TRACE) + << "zwp_text_input_v3_listener.delete_surrounding_text"; + }, + .done = [](void* data, + zwp_text_input_v3* zwp_text_input_v3, + uint32_t serial) -> void { + ELINUX_LOG(TRACE) << "zwp_text_input_v3_listener.done"; + }, +}; + +const wl_data_device_listener ELinuxWindowWayland::kWlDataDeviceListener = { + .data_offer = [](void* data, + wl_data_device* wl_data_device, + wl_data_offer* offer) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.data_offer"; + }, + .enter = [](void* data, + wl_data_device* wl_data_device, + uint32_t serial, + wl_surface* surface, + wl_fixed_t x, + wl_fixed_t y, + wl_data_offer* offer) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.enter"; + }, + .leave = [](void* data, wl_data_device* wl_data_device) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.leave"; + }, + .motion = [](void* data, + wl_data_device* wl_data_device, + uint32_t time, + wl_fixed_t x, + wl_fixed_t y) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.motion"; + }, + .drop = [](void* data, wl_data_device* wl_data_device) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.drop"; + }, + .selection = [](void* data, + wl_data_device* wl_data_device, + wl_data_offer* offer) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.selection"; + + auto self = reinterpret_cast(data); + if (self->wl_data_offer_) { + wl_data_offer_destroy(self->wl_data_offer_); + } + self->wl_data_offer_ = offer; + }, +}; + +const wl_data_source_listener ELinuxWindowWayland::kWlDataSourceListener = { + .target = [](void* data, + wl_data_source* wl_data_source, + const char* mime_type) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.target"; + }, + .send = [](void* data, + wl_data_source* wl_data_source, + const char* mime_type, + int32_t fd) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.send"; + + if (strcmp(mime_type, kClipboardMimeTypeText)) { + ELINUX_LOG(ERROR) << "Not expected mime_type: " << mime_type; + return; + } + auto self = reinterpret_cast(data); + // Write the copied data to the clipboard. + write(fd, self->clipboard_data_.c_str(), + strlen(self->clipboard_data_.c_str())); + close(fd); + }, + .cancelled = [](void* data, wl_data_source* wl_data_source) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.cancelled"; + + auto self = reinterpret_cast(data); + self->clipboard_data_ = ""; + if (self->wl_data_source_) { + wl_data_source_destroy(self->wl_data_source_); + self->wl_data_source_ = nullptr; + } + }, + .dnd_drop_performed = [](void* data, + wl_data_source* wl_data_source) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.dnd_drop_performed"; + }, + .dnd_finished = [](void* data, wl_data_source* wl_data_source) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.dnd_finished"; + }, + .action = [](void* data, + wl_data_source* wl_data_source, + uint32_t dnd_action) -> void { + ELINUX_LOG(TRACE) << "wl_data_device_listener.action"; + }, +}; + +const zxdg_toplevel_decoration_v1_listener + ELinuxWindowWayland::kZxdgToplevelDecorationV1Listener = { + .configure = + [](void* data, + struct zxdg_toplevel_decoration_v1* zxdg_toplevel_decoration_v1, + uint32_t mode) -> void { + ELINUX_LOG(INFO) + << "zxdg_toplevel_decoration_v1_listener.configure: mode is " + << ((mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE) + ? "server-side" + : "client-side"); + auto self = reinterpret_cast(data); + if (self->view_properties_.use_window_decoration && + mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE && + !self->window_decorations_) { + int32_t width_dip = self->view_properties_.width; + int32_t height_dip = self->view_properties_.height; + self->CreateDecoration(width_dip, height_dip); + } + }, +}; + +ELinuxWindowWayland::ELinuxWindowWayland( + FlutterDesktopViewProperties view_properties) + : cursor_info_({"", 0, nullptr}), + display_valid_(false), + running_(false), + maximised_(false), + is_requested_show_virtual_keyboard_(false), + xdg_toplevel_(nullptr), + wl_compositor_(nullptr), + wl_subcompositor_(nullptr), + wl_current_surface_(nullptr), + wl_shm_(nullptr), + seat_inputs_map_(), + wl_data_device_manager_(nullptr), + wl_data_device_(nullptr), + wl_data_offer_(nullptr), + wl_data_source_(nullptr), + serial_(0), + zwp_text_input_manager_v1_(nullptr), + zwp_text_input_manager_v3_(nullptr), + zwp_text_input_v1_(nullptr), + zwp_text_input_v3_(nullptr), + wp_presentation_(nullptr), + wp_presentation_clk_id_(UINT32_MAX), + window_decorations_(nullptr) { + view_properties_ = view_properties; + current_scale_ = + view_properties.force_scale_factor ? view_properties.scale_factor : 1.0; + SetRotation(view_properties_.view_rotation); + + auto xcursor_size_string = std::getenv(kXcursorSizeEnvironmentKey); + cursor_size_ = + xcursor_size_string ? atoi(xcursor_size_string) : kDefaultPointerSize; + if (cursor_size_ <= 0) { + cursor_size_ = kDefaultPointerSize; + } + + wl_display_ = wl_display_connect(nullptr); + if (!wl_display_) { + ELINUX_LOG(ERROR) << "Failed to connect to the Wayland display."; + return; + } + + wl_registry_ = wl_display_get_registry(wl_display_); + if (!wl_registry_) { + ELINUX_LOG(ERROR) << "Failed to get the wayland registry."; + return; + } + + wl_registry_add_listener(wl_registry_, &kWlRegistryListener, this); + wl_display_roundtrip(wl_display_); + + for (auto& [seat, _] : seat_inputs_map_) { + if (wl_data_device_manager_ && seat) { + wl_data_device_ = + wl_data_device_manager_get_data_device(wl_data_device_manager_, seat); + wl_data_device_add_listener(wl_data_device_, &kWlDataDeviceListener, + this); + } + + // Setup text-input protocol for onscreen keyboard inputs. + { + if (zwp_text_input_manager_v3_ && seat) { + zwp_text_input_v3_ = zwp_text_input_manager_v3_get_text_input( + zwp_text_input_manager_v3_, seat); + if (!zwp_text_input_v3_) { + ELINUX_LOG(ERROR) << "Failed to create the text input manager v3."; + return; + } + zwp_text_input_v3_add_listener(zwp_text_input_v3_, + &kZwpTextInputV3Listener, this); + } else if (zwp_text_input_manager_v1_) { + zwp_text_input_v1_ = zwp_text_input_manager_v1_create_text_input( + zwp_text_input_manager_v1_); + if (!zwp_text_input_v1_) { + ELINUX_LOG(ERROR) << "Failed to create text input manager v1."; + return; + } + zwp_text_input_v1_add_listener(zwp_text_input_v1_, + &kZwpTextInputV1Listener, this); + } else { + // do nothing. + } + } + } + + display_valid_ = true; + running_ = true; +} + +ELinuxWindowWayland::~ELinuxWindowWayland() { + display_valid_ = false; + running_ = false; + + for (auto theme : wl_cursor_themes_) { + wl_cursor_theme_destroy(theme.second); + } + wl_cursor_themes_.clear(); + + { + if (zwp_text_input_v1_) { + zwp_text_input_v1_destroy(zwp_text_input_v1_); + zwp_text_input_v1_ = nullptr; + } + + if (zwp_text_input_manager_v1_) { + zwp_text_input_manager_v1_destroy(zwp_text_input_manager_v1_); + zwp_text_input_manager_v1_ = nullptr; + } + + if (zwp_text_input_v3_) { + zwp_text_input_v3_destroy(zwp_text_input_v3_); + zwp_text_input_v3_ = nullptr; + } + + if (zwp_text_input_manager_v3_) { + zwp_text_input_manager_v3_destroy(zwp_text_input_manager_v3_); + zwp_text_input_manager_v3_ = nullptr; + } + } + + { + if (zxdg_decoration_manager_v1_) { + zxdg_decoration_manager_v1_destroy(zxdg_decoration_manager_v1_); + zxdg_decoration_manager_v1_ = nullptr; + } + + if (zxdg_toplevel_decoration_v1_) { + zxdg_toplevel_decoration_v1_destroy(zxdg_toplevel_decoration_v1_); + zxdg_toplevel_decoration_v1_ = nullptr; + } + } + + if (wl_data_offer_) { + wl_data_offer_destroy(wl_data_offer_); + wl_data_offer_ = nullptr; + } + + if (wl_data_source_) { + wl_data_source_destroy(wl_data_source_); + wl_data_source_ = nullptr; + } + + if (wl_data_device_) { + if (wl_data_device_manager_version_ >= + WL_DATA_DEVICE_RELEASE_SINCE_VERSION) { + wl_data_device_release(wl_data_device_); + } else { + wl_data_device_destroy(wl_data_device_); + } + wl_data_device_ = nullptr; + } + + if (wl_data_device_manager_) { + wl_data_device_manager_destroy(wl_data_device_manager_); + wl_data_device_manager_ = nullptr; + } + + for (auto& [seat, inputs] : seat_inputs_map_) { + if (inputs.pointer) { + wl_pointer_destroy(inputs.pointer); + inputs.pointer = nullptr; + } + + if (inputs.touch) { + wl_touch_destroy(inputs.touch); + inputs.touch = nullptr; + } + + if (inputs.keyboard) { + wl_keyboard_destroy(inputs.keyboard); + inputs.keyboard = nullptr; + } + + if (seat) { + wl_seat_destroy(seat); + } + } + + seat_inputs_map_.clear(); + + if (wl_output_) { + wl_output_destroy(wl_output_); + wl_output_ = nullptr; + } + + if (wl_shm_) { + wl_shm_destroy(wl_shm_); + wl_shm_ = nullptr; + } + + if (xdg_toplevel_) { + xdg_toplevel_destroy(xdg_toplevel_); + xdg_toplevel_ = nullptr; + } + + if (xdg_wm_base_) { + xdg_wm_base_destroy(xdg_wm_base_); + xdg_wm_base_ = nullptr; + } + + if (wl_compositor_) { + wl_compositor_destroy(wl_compositor_); + wl_compositor_ = nullptr; + } + + if (wl_subcompositor_) { + wl_subcompositor_destroy(wl_subcompositor_); + wl_subcompositor_ = nullptr; + } + + if (wl_registry_) { + wl_registry_destroy(wl_registry_); + wl_registry_ = nullptr; + } + + if (wl_display_) { + wl_display_flush(wl_display_); + wl_display_disconnect(wl_display_); + wl_display_ = nullptr; + } +} + +void ELinuxWindowWayland::SetView(WindowBindingHandlerDelegate* window) { + binding_handler_delegate_ = window; +} + +ELinuxRenderSurfaceTarget* ELinuxWindowWayland::GetRenderSurfaceTarget() const { + return render_surface_.get(); +} + +uint16_t ELinuxWindowWayland::GetRotationDegree() const { + return current_rotation_; +} + +double ELinuxWindowWayland::GetDpiScale() { + return current_scale_; +} + +PhysicalWindowBounds ELinuxWindowWayland::GetPhysicalWindowBounds() { + return {GetCurrentWidth(), GetCurrentHeight()}; +} + +int32_t ELinuxWindowWayland::GetFrameRate() { + return frame_rate_; +} + +bool ELinuxWindowWayland::DispatchEvent() { + if (!IsValid()) { + ELINUX_LOG(ERROR) << "Wayland display is invalid."; + return false; + } + + if (!running_) { + return false; + } + + if (request_redraw_) { + request_redraw_ = false; + if (window_decorations_) { + window_decorations_->Resize(view_properties_.width, + view_properties_.height, current_scale_); + } + + if (binding_handler_delegate_) { + binding_handler_delegate_->OnWindowSizeChanged( + view_properties_.width * current_scale_, + view_properties_.height * current_scale_ - + WindowDecorationsPhysicalHeight()); + } + NotifyDisplayInfoUpdates(); + } + + // Prepare to call wl_display_read_events. + while (wl_display_prepare_read(wl_display_) != 0) { + // If Wayland compositor terminates, -1 is returned. + auto result = wl_display_dispatch_pending(wl_display_); + if (result == -1) { + return false; + } + } + wl_display_flush(wl_display_); + + // Handle Vsync. + if (binding_handler_delegate_) { + const uint64_t vsync_interval_time_nanos = 1000000000000 / frame_rate_; + binding_handler_delegate_->OnVsync(last_frame_time_nanos_, + vsync_interval_time_nanos); + } + + // Handle Wayland events. + pollfd fds[] = { + {wl_display_get_fd(wl_display_), POLLIN}, + }; + if (poll(fds, 1, 0) > 0) { + auto result = wl_display_read_events(wl_display_); + if (result == -1) { + return false; + } + + result = wl_display_dispatch_pending(wl_display_); + if (result == -1) { + return false; + } + } else { + wl_display_cancel_read(wl_display_); + } + + return true; +} + +bool ELinuxWindowWayland::CreateRenderSurface(int32_t width_px, + int32_t height_px, + bool enable_impeller) { + enable_impeller_ = enable_impeller; + + if (!display_valid_) { + ELINUX_LOG(ERROR) << "Wayland display is invalid."; + return false; + } + + if (!wl_compositor_) { + ELINUX_LOG(ERROR) << "Wl_compositor is invalid"; + return false; + } + + if (!xdg_wm_base_) { + ELINUX_LOG(ERROR) << "Xdg-shell is invalid"; + return false; + } + + if (view_properties_.view_mode == FlutterDesktopViewMode::kFullscreen) { + width_px = view_properties_.width * current_scale_; + height_px = view_properties_.height * current_scale_; + } + + ELINUX_LOG(TRACE) << "Created the Wayland surface: " << width_px << "x" + << height_px; + if (view_properties_.use_mouse_cursor) { + wl_cursor_surface_ = wl_compositor_create_surface(wl_compositor_); + if (!wl_cursor_surface_) { + ELINUX_LOG(ERROR) + << "Failed to create the compositor surface for cursor."; + return false; + } + } + + if (current_rotation_ == 90 || current_rotation_ == 270) { + std::swap(width_px, height_px); + } + native_window_ = std::make_unique( + wl_compositor_, width_px, height_px, view_properties_.enable_vsync); + + wl_surface_add_listener(native_window_->Surface(), &kWlSurfaceListener, this); + + xdg_surface_ = + xdg_wm_base_get_xdg_surface(xdg_wm_base_, native_window_->Surface()); + if (!xdg_surface_) { + ELINUX_LOG(ERROR) << "Failed to get the xdg surface."; + return false; + } + xdg_surface_add_listener(xdg_surface_, &kXdgSurfaceListener, this); + + xdg_toplevel_ = xdg_surface_get_toplevel(xdg_surface_); + if (view_properties_.title != nullptr) { + xdg_toplevel_set_title(xdg_toplevel_, view_properties_.title); + } + if (view_properties_.app_id != nullptr) { + xdg_toplevel_set_app_id(xdg_toplevel_, view_properties_.app_id); + } + xdg_toplevel_add_listener(xdg_toplevel_, &kXdgToplevelListener, this); + wl_surface_set_buffer_scale(native_window_->Surface(), current_scale_); + + { + auto* callback = wl_surface_frame(native_window_->Surface()); + wl_callback_add_listener(callback, &kWlSurfaceFrameListener, this); + + if (wp_presentation_) { + wp_presentation_feedback_add_listener( + ::wp_presentation_feedback(wp_presentation_, + native_window_->Surface()), + &kWpPresentationFeedbackListener, this); + } + } + + if (view_properties_.view_mode == FlutterDesktopViewMode::kFullscreen) { + xdg_toplevel_set_fullscreen(xdg_toplevel_, NULL); + } + + wait_for_configure_ = true; + wl_surface_commit(native_window_->Surface()); + + render_surface_ = std::make_unique(std::make_unique( + std::make_unique(wl_display_), enable_impeller)); + render_surface_->SetNativeWindow(native_window_.get()); + + if (view_properties_.use_window_decoration) { + if (zxdg_decoration_manager_v1_) { + ELINUX_LOG(INFO) << "Use server-side xdg-decoration mode"; + zxdg_toplevel_decoration_v1_ = + zxdg_decoration_manager_v1_get_toplevel_decoration( + zxdg_decoration_manager_v1_, xdg_toplevel_); + zxdg_toplevel_decoration_v1_set_mode( + zxdg_toplevel_decoration_v1_, + ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); + zxdg_toplevel_decoration_v1_add_listener( + zxdg_toplevel_decoration_v1_, &kZxdgToplevelDecorationV1Listener, + this); + } else { + int32_t width_dip = width_px / current_scale_; + int32_t height_dip = height_px / current_scale_; + CreateDecoration(width_dip, height_dip); + } + } + + // Wait for making sure that xdg_surface has been configured. + while (wait_for_configure_) { + wl_display_dispatch(wl_display_); + } + + return true; +} + +void ELinuxWindowWayland::CreateDecoration(int32_t width_dip, + int32_t height_dip) { + if (window_decorations_) { + ELINUX_LOG(WARNING) << "Window decoration has already created"; + return; + } + + window_decorations_ = std::make_unique( + wl_display_, wl_compositor_, wl_subcompositor_, native_window_->Surface(), + width_dip, height_dip, current_scale_, enable_impeller_, + view_properties_.enable_vsync); +} + +void ELinuxWindowWayland::DestroyRenderSurface() { + // destroy the main surface before destroying the client window on Wayland. + if (window_decorations_) { + window_decorations_ = nullptr; + } + render_surface_ = nullptr; + native_window_ = nullptr; + + if (xdg_surface_) { + xdg_surface_destroy(xdg_surface_); + xdg_surface_ = nullptr; + } + + if (wl_cursor_surface_) { + wl_surface_destroy(wl_cursor_surface_); + wl_cursor_surface_ = nullptr; + } +} + +void ELinuxWindowWayland::UpdateVirtualKeyboardStatus(const bool show) { + // Not supported virtual keyboard. + if (!(zwp_text_input_v1_ || zwp_text_input_v3_) || seat_inputs_map_.empty()) { + return; + } + + is_requested_show_virtual_keyboard_ = show; + if (is_requested_show_virtual_keyboard_) { + ShowVirtualKeyboard(); + } else { + DismissVirtualKeybaord(); + } +} + +void ELinuxWindowWayland::UpdateFlutterCursor(const std::string& cursor_name) { + if (view_properties_.use_mouse_cursor) { + if (cursor_name.compare(cursor_info_.cursor_name) == 0) { + return; + } + cursor_info_.cursor_name = cursor_name; + + if (cursor_name.compare(kCursorNameNone) == 0) { + // Turn off the cursor. + wl_pointer_set_cursor(cursor_info_.pointer, cursor_info_.serial, + wl_cursor_surface_, 0, 0); + wl_surface_attach(wl_cursor_surface_, nullptr, 0, 0); + wl_surface_damage(wl_cursor_surface_, 0, 0, 0, 0); + wl_surface_commit(wl_cursor_surface_); + return; + } + + auto wl_cursor = GetWlCursor(cursor_name, cursor_size_ * current_scale_); + if (!wl_cursor) { + return; + } + auto image = wl_cursor->images[0]; + auto buffer = wl_cursor_image_get_buffer(image); + if (buffer) { + wl_pointer_set_cursor( + cursor_info_.pointer, cursor_info_.serial, wl_cursor_surface_, + image->hotspot_x / current_scale_, image->hotspot_y / current_scale_); + wl_surface_attach(wl_cursor_surface_, buffer, 0, 0); + wl_surface_damage(wl_cursor_surface_, 0, 0, image->width, image->height); + wl_surface_set_buffer_scale(wl_cursor_surface_, current_scale_); + wl_surface_commit(wl_cursor_surface_); + } + } +} + +std::string ELinuxWindowWayland::GetClipboardData() { + std::string str = ""; + + if (wl_data_offer_) { + int fd[2]; + if (pipe2(fd, O_CLOEXEC) == -1) { + return str; + } + + wl_data_offer_receive(wl_data_offer_, kClipboardMimeTypeText, fd[1]); + close(fd[1]); + wl_display_dispatch(wl_display_); + + char buf[256]; + int len; + // Read data form the clipboard. + while ((len = read(fd[0], buf, sizeof(buf))) > 0) { + str.append(buf, len); + } + close(fd[0]); + return str; + } + + return str; +} + +void ELinuxWindowWayland::SetClipboardData(const std::string& data) { + clipboard_data_ = data; + if (wl_data_device_manager_) { + if (wl_data_source_) { + wl_data_source_destroy(wl_data_source_); + wl_data_source_ = nullptr; + } + + wl_data_source_ = + wl_data_device_manager_create_data_source(wl_data_device_manager_); + if (!wl_data_source_) { + return; + } + + wl_data_source_offer(wl_data_source_, kClipboardMimeTypeText); + wl_data_source_add_listener(wl_data_source_, &kWlDataSourceListener, this); + wl_data_device_set_selection(wl_data_device_, wl_data_source_, serial_); + } +} + +bool ELinuxWindowWayland::IsValid() const { + if (!display_valid_ || !native_window_ || !render_surface_ || + !native_window_->IsValid() || !render_surface_->IsValid()) { + return false; + } + return true; +} + +void ELinuxWindowWayland::WlRegistryHandler(wl_registry* wl_registry, + uint32_t name, + const char* interface, + uint32_t version) { + if (!strcmp(interface, wl_compositor_interface.name)) { + constexpr uint32_t kMaxVersion = 5; + wl_compositor_ = static_cast( + wl_registry_bind(wl_registry, name, &wl_compositor_interface, + std::min(kMaxVersion, version))); + return; + } + + if (!strcmp(interface, wl_subcompositor_interface.name)) { + constexpr uint32_t kMaxVersion = 1; + wl_subcompositor_ = static_cast(wl_registry_bind( + wl_registry, name, &wl_subcompositor_interface, kMaxVersion)); + } + + if (!strcmp(interface, xdg_wm_base_interface.name)) { + constexpr uint32_t kMaxVersion = 3; + xdg_wm_base_ = static_cast( + wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, + std::min(kMaxVersion, version))); + xdg_wm_base_add_listener(xdg_wm_base_, &kXdgWmBaseListener, this); + return; + } + + if (!strcmp(interface, wl_seat_interface.name)) { + constexpr uint32_t kMaxVersion = 4; + auto [inserted, _] = + seat_inputs_map_.emplace(static_cast(wl_registry_bind( + wl_registry, name, &wl_seat_interface, + std::min(kMaxVersion, version))), + seat_inputs()); + registry_names_to_seat_ptr_.emplace(name, inserted->first); + wl_seat_add_listener(inserted->first, &kWlSeatListener, this); + return; + } + + if (!strcmp(interface, wl_output_interface.name)) { + constexpr uint32_t kMaxVersion = 2; + wl_output_ = static_cast( + wl_registry_bind(wl_registry, name, &wl_output_interface, + std::min(kMaxVersion, version))); + wl_output_add_listener(wl_output_, &kWlOutputListener, this); + return; + } + + if (!strcmp(interface, wl_shm_interface.name)) { + if (view_properties_.use_mouse_cursor) { + constexpr uint32_t kMaxVersion = 1; + wl_shm_ = static_cast( + wl_registry_bind(wl_registry, name, &wl_shm_interface, kMaxVersion)); + } + return; + } + + if (!strcmp(interface, kZwpTextInputManagerV1)) { + if (view_properties_.use_onscreen_keyboard) { + constexpr uint32_t kMaxVersion = 1; + zwp_text_input_manager_v1_ = + static_cast(wl_registry_bind( + wl_registry, name, &zwp_text_input_manager_v1_interface, + kMaxVersion)); + } + return; + } + + if (!strcmp(interface, kZwpTextInputManagerV3)) { + if (view_properties_.use_onscreen_keyboard) { + constexpr uint32_t kMaxVersion = 1; + zwp_text_input_manager_v3_ = + static_cast(wl_registry_bind( + wl_registry, name, &zwp_text_input_manager_v3_interface, + std::min(kMaxVersion, version))); + } + return; + } + + if (!strcmp(interface, wl_data_device_manager_interface.name)) { + // Save the version of wl_data_device_manager because the release method of + // wl_data_device differs depending on it. Since wl_data_device_manager has + // been released up to version 3, set the upper limit to 3. + constexpr uint32_t kMaxVersion = 3; + wl_data_device_manager_version_ = std::min(kMaxVersion, version); + wl_data_device_manager_ = static_cast( + wl_registry_bind(wl_registry, name, &wl_data_device_manager_interface, + wl_data_device_manager_version_)); + return; + } + + if (!strcmp(interface, wp_presentation_interface.name)) { + constexpr uint32_t kMaxVersion = 1; + wp_presentation_ = static_cast(wl_registry_bind( + wl_registry, name, &wp_presentation_interface, kMaxVersion)); + wp_presentation_add_listener(wp_presentation_, &kWpPresentationListener, + this); + return; + } + + if (!strcmp(interface, kZxdgDecorationManagerV1)) { + constexpr uint32_t kMaxVersion = 1; + zxdg_decoration_manager_v1_ = + static_cast(wl_registry_bind( + wl_registry, name, &zxdg_decoration_manager_v1_interface, + std::min(kMaxVersion, version))); + return; + } +} + +void ELinuxWindowWayland::WlUnRegistryHandler(wl_registry* wl_registry, + uint32_t name) { + // Just remove seats for now, as we don't need to do anything else. + // But there is also no interface name here. + auto seat_iter = registry_names_to_seat_ptr_.find(name); + if (seat_iter != registry_names_to_seat_ptr_.end()) { + auto seat = seat_iter->second; + auto seat_inputs_iter = seat_inputs_map_.find(seat); + if (seat_inputs_iter != seat_inputs_map_.end()) { + auto& inputs = seat_inputs_iter->second; + if (inputs.pointer) { + wl_pointer_release(inputs.pointer); + inputs.pointer = nullptr; + } + if (inputs.touch) { + wl_touch_release(inputs.touch); + inputs.touch = nullptr; + } + if (inputs.keyboard) { + wl_keyboard_release(inputs.keyboard); + inputs.keyboard = nullptr; + } + seat_inputs_map_.erase(seat_inputs_iter); + } + if (seat) { + wl_seat_destroy(seat); + } + registry_names_to_seat_ptr_.erase(name); + } +} + +bool ELinuxWindowWayland::LoadCursorTheme(uint32_t size) { + if (!wl_shm_) { + ELINUX_LOG(ERROR) << "Failed to load cursor theme because shared memory " + "buffers are not available."; + return false; + } + + auto theme = wl_cursor_theme_load(nullptr, size, wl_shm_); + if (!theme) { + ELINUX_LOG(ERROR) << "Failed to load cursor theme for size: " << size; + return false; + } + + wl_cursor_themes_[size] = theme; + + std::vector wl_cursor_names{ + kWlCursorThemeLeftPtr, + kWlCursorThemeBottomLeftCorner, + kWlCursorThemeBottomRightCorner, + kWlCursorThemeBottomSide, + kWlCursorThemeGrabbing, + kWlCursorThemeLeftSide, + kWlCursorThemeRightSide, + kWlCursorThemeTopLeftCorner, + kWlCursorThemeTopRightCorner, + kWlCursorThemeTopSide, + kWlCursorThemeXterm, + kWlCursorThemeHand1, + kWlCursorThemeWatch, + }; + + std::unordered_map cursor_list; + + for (const auto& cursor_name : wl_cursor_names) { + auto wl_cursor = wl_cursor_theme_get_cursor(theme, cursor_name.c_str()); + if (!wl_cursor) { + ELINUX_LOG(ERROR) << "Unsupported cursor theme: " << cursor_name.c_str(); + continue; + } + cursor_list[cursor_name] = wl_cursor; + } + + supported_wl_cursor_list_.insert(std::make_pair(size, cursor_list)); + + return true; +} + +wl_cursor* ELinuxWindowWayland::GetWlCursor(const std::string& cursor_name, + uint32_t size) { + // Convert the cursor theme name from Flutter's cursor value to Wayland's one. + // However, Wayland has not all cursor themes corresponding to Flutter. + // If there is no Wayland's cursor theme corresponding to the Flutter's cursor + // name, it is defined as empty. + // If empty, the default cursor theme(left_ptr) will be displayed. + static const std::unordered_map + flutter_to_wayland_cursor_map = { + {"alias", ""}, + {"allScroll", ""}, + {"basic", kWlCursorThemeLeftPtr}, + {"cell", ""}, + {"click", kWlCursorThemeHand1}, + {"contextMenu", ""}, + {"copy", ""}, + {"forbidden", ""}, + {"grab", ""}, + {"grabbing", kWlCursorThemeGrabbing}, + {"help", ""}, + {"move", ""}, + {"noDrop", ""}, + {"precise", ""}, + {"progress", ""}, + {"text", kWlCursorThemeXterm}, + {"resizeColumn", ""}, + {"resizeDown", kWlCursorThemeBottomSide}, + {"resizeDownLeft", kWlCursorThemeBottomLeftCorner}, + {"resizeDownRight", kWlCursorThemeBottomRightCorner}, + {"resizeLeft", kWlCursorThemeLeftSide}, + {"resizeLeftRight", ""}, + {"resizeRight", kWlCursorThemeRightSide}, + {"resizeRow", ""}, + {"resizeUp", kWlCursorThemeTopSide}, + {"resizeUpDown", ""}, + {"resizeUpLeft", kWlCursorThemeTopLeftCorner}, + {"resizeUpRight", kWlCursorThemeTopRightCorner}, + {"resizeUpLeftDownRight", ""}, + {"resizeUpRightDownLeft", ""}, + {"verticalText", ""}, + {"wait", kWlCursorThemeWatch}, + {"zoomIn", ""}, + {"zoomOut", ""}, + }; + + if (supported_wl_cursor_list_.find(size) == supported_wl_cursor_list_.end()) { + if (!LoadCursorTheme(size)) { + return nullptr; + } + } + + auto cursor_list = supported_wl_cursor_list_.at(size); + + if (flutter_to_wayland_cursor_map.find(cursor_name) != + flutter_to_wayland_cursor_map.end()) { + auto theme = flutter_to_wayland_cursor_map.at(cursor_name); + if (!theme.empty() && cursor_list.find(theme) != cursor_list.end()) { + return cursor_list[theme]; + } + } + + ELINUX_LOG(ERROR) << "Unsupported cursor: " << cursor_name.c_str(); + return cursor_list[kWlCursorThemeLeftPtr]; +} + +void ELinuxWindowWayland::ShowVirtualKeyboard() { + if (zwp_text_input_v3_) { + // I'm not sure the reason, but enable needs to be called twice. + zwp_text_input_v3_enable(zwp_text_input_v3_); + zwp_text_input_v3_commit(zwp_text_input_v3_); + zwp_text_input_v3_enable(zwp_text_input_v3_); + zwp_text_input_v3_commit(zwp_text_input_v3_); + + zwp_text_input_v3_set_content_type( + zwp_text_input_v3_, ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE, + ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL); + zwp_text_input_v3_commit(zwp_text_input_v3_); + } else { + if (native_window_) { + zwp_text_input_v1_show_input_panel(zwp_text_input_v1_); + zwp_text_input_v1_activate(zwp_text_input_v1_, + seat_inputs_map_.begin()->first, + native_window_->Surface()); + } + } +} + +void ELinuxWindowWayland::DismissVirtualKeybaord() { + if (zwp_text_input_v3_) { + zwp_text_input_v3_disable(zwp_text_input_v3_); + zwp_text_input_v3_commit(zwp_text_input_v3_); + } else { + zwp_text_input_v1_deactivate(zwp_text_input_v1_, + seat_inputs_map_.begin()->first); + } +} + +void ELinuxWindowWayland::UpdateWindowScale() { + if (view_properties_.force_scale_factor) { + return; + } + + double scale_factor = 1.0; + for (auto output_id : entered_outputs_) { + if (wl_output_scale_factors_.find(output_id) == + wl_output_scale_factors_.end()) + continue; + + auto output_scale_factor = wl_output_scale_factors_.at(output_id); + if (output_scale_factor > scale_factor) + scale_factor = output_scale_factor; + } + + if (current_scale_ == scale_factor) { + return; + } + + ELINUX_LOG(TRACE) << "Window scale has changed: " << scale_factor; + current_scale_ = scale_factor; + + wl_surface_set_buffer_scale(native_window_->Surface(), current_scale_); + request_redraw_ = true; +} + +uint32_t ELinuxWindowWayland::WindowDecorationsPhysicalHeight() const { + if (!window_decorations_) { + return 0; + } + + return window_decorations_->Height() * current_scale_; +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/elinux_window_wayland.h b/src/flutter/shell/platform/linux_embedded/window/elinux_window_wayland.h new file mode 100644 index 00000000..8ccb84ba --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/elinux_window_wayland.h @@ -0,0 +1,222 @@ +// Copyright 2023 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_WAYLAND_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_WAYLAND_H_ + +#include +#include + +#include +#include +#include + +#include "flutter/shell/platform/linux_embedded/surface/surface_gl.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window.h" +#include "flutter/shell/platform/linux_embedded/window/native_window_wayland.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.h" +#include "flutter/shell/platform/linux_embedded/window_binding_handler.h" + +// These header files are automatically generated by the +// wayland-scanner. +extern "C" { +#include "wayland/protocols/presentation-time-protocol.h" +#include "wayland/protocols/text-input-unstable-v1-client-protocol.h" +#include "wayland/protocols/text-input-unstable-v3-client-protocol.h" +#include "wayland/protocols/xdg-decoration-unstable-v1-protocol.h" +#include "wayland/protocols/xdg-shell-client-protocol.h" +} + +namespace flutter { + +namespace { +constexpr char kXcursorSizeEnvironmentKey[] = "XCURSOR_SIZE"; +} // namespace + +// Track input devices for each seat +struct seat_inputs { + wl_pointer* pointer = nullptr; + wl_touch* touch = nullptr; + wl_keyboard* keyboard = nullptr; +}; + +class ELinuxWindowWayland : public ELinuxWindow, public WindowBindingHandler { + public: + ELinuxWindowWayland(FlutterDesktopViewProperties view_properties); + ~ELinuxWindowWayland(); + + // |ELinuxWindow| + bool IsValid() const override; + + // |FlutterWindowBindingHandler| + bool DispatchEvent() override; + + // |FlutterWindowBindingHandler| + bool CreateRenderSurface(int32_t width_px, + int32_t height_px, + bool enable_impeller) override; + + // |FlutterWindowBindingHandler| + void DestroyRenderSurface() override; + + // |FlutterWindowBindingHandler| + void SetView(WindowBindingHandlerDelegate* view) override; + + // |FlutterWindowBindingHandler| + ELinuxRenderSurfaceTarget* GetRenderSurfaceTarget() const override; + + // |FlutterWindowBindingHandler| + uint16_t GetRotationDegree() const override; + + // |FlutterWindowBindingHandler| + double GetDpiScale() override; + + // |FlutterWindowBindingHandler| + PhysicalWindowBounds GetPhysicalWindowBounds() override; + + // |FlutterWindowBindingHandler| + int32_t GetFrameRate() override; + + // |FlutterWindowBindingHandler| + void UpdateFlutterCursor(const std::string& cursor_name) override; + + // |FlutterWindowBindingHandler| + void UpdateVirtualKeyboardStatus(const bool show) override; + + // |FlutterWindowBindingHandler| + std::string GetClipboardData() override; + + // |FlutterWindowBindingHandler| + void SetClipboardData(const std::string& data) override; + + private: + struct CursorInfo { + std::string cursor_name; + uint32_t serial; + wl_pointer* pointer; + }; + + void WlRegistryHandler(wl_registry* wl_registry, + uint32_t name, + const char* interface, + uint32_t version); + + void WlUnRegistryHandler(wl_registry* wl_registry, uint32_t name); + + bool LoadCursorTheme(uint32_t size); + + wl_cursor* GetWlCursor(const std::string& cursor_name, uint32_t size); + + void ShowVirtualKeyboard(); + + void DismissVirtualKeybaord(); + + // Updates the surface scale of the window from the list of entered outputs. + void UpdateWindowScale(); + + void CreateDecoration(int32_t width_dip, int32_t height_dip); + + // Get window decorations height in physical pixels. + uint32_t WindowDecorationsPhysicalHeight() const; + + static const wl_registry_listener kWlRegistryListener; + static const xdg_wm_base_listener kXdgWmBaseListener; + static const xdg_surface_listener kXdgSurfaceListener; + static const xdg_toplevel_listener kXdgToplevelListener; + static const wl_surface_listener kWlSurfaceListener; + static const wl_seat_listener kWlSeatListener; + static const wl_pointer_listener kWlPointerListener; + static const wl_touch_listener kWlTouchListener; + static const wl_keyboard_listener kWlKeyboardListener; + static const wl_output_listener kWlOutputListener; + static const wl_data_device_listener kWlDataDeviceListener; + static const wl_data_source_listener kWlDataSourceListener; + static const zwp_text_input_v1_listener kZwpTextInputV1Listener; + static const zwp_text_input_v3_listener kZwpTextInputV3Listener; + static const wl_callback_listener kWlSurfaceFrameListener; + static const wp_presentation_listener kWpPresentationListener; + static const wp_presentation_feedback_listener + kWpPresentationFeedbackListener; + static const zxdg_toplevel_decoration_v1_listener + kZxdgToplevelDecorationV1Listener; + static constexpr size_t kDefaultPointerSize = 24; + + std::unique_ptr native_window_; + std::unique_ptr render_surface_; + + // decorations. + std::unique_ptr window_decorations_; + wl_surface* wl_current_surface_; + wl_subcompositor* wl_subcompositor_; + bool restore_window_required_ = false; + int32_t restore_window_width_; + int32_t restore_window_height_; + + bool display_valid_; + bool running_; + bool wait_for_configure_ = false; + bool request_redraw_ = false; + bool maximised_; + uint32_t last_frame_time_; + bool enable_impeller_ = false; + int32_t transform_ = WL_OUTPUT_TRANSFORM_NORMAL; + + // Indicates that exists a keyboard show request from Flutter Engine. + bool is_requested_show_virtual_keyboard_; + + wl_display* wl_display_; + wl_registry* wl_registry_; + wl_compositor* wl_compositor_; + wl_output* wl_output_; + wl_shm* wl_shm_; + std::unordered_map seat_inputs_map_; + std::unordered_map registry_names_to_seat_ptr_; + wl_surface* wl_cursor_surface_; + xdg_wm_base* xdg_wm_base_; + xdg_surface* xdg_surface_; + xdg_toplevel* xdg_toplevel_; + + // text-input protocol for onscreen keyboard inputs. + zwp_text_input_manager_v1* zwp_text_input_manager_v1_; + zwp_text_input_manager_v3* zwp_text_input_manager_v3_; + zwp_text_input_v1* zwp_text_input_v1_; + zwp_text_input_v3* zwp_text_input_v3_; + + // xdg-decoration protocol for window decoration (server-side). + zxdg_decoration_manager_v1* zxdg_decoration_manager_v1_ = nullptr; + zxdg_toplevel_decoration_v1* zxdg_toplevel_decoration_v1_ = nullptr; + + // Frame information for Vsync events. + wp_presentation* wp_presentation_; + uint32_t wp_presentation_clk_id_; + uint64_t last_frame_time_nanos_; + + CursorInfo cursor_info_; + size_t cursor_size_; + + // List of cursor name and wl_cursor supported by Wayland keyed by their + // (scaled) size. + std::unordered_map> + supported_wl_cursor_list_; + + // List of loaded Wayland cursor themes keyed by their (scaled) size. + std::unordered_map wl_cursor_themes_; + + // List of outputs that the window is currently rendered on. + std::unordered_set entered_outputs_; + + // List of output scale factors keyed by their ids. + std::unordered_map wl_output_scale_factors_; + + wl_data_device_manager* wl_data_device_manager_; + wl_data_device* wl_data_device_; + wl_data_offer* wl_data_offer_; + wl_data_source* wl_data_source_; + uint32_t wl_data_device_manager_version_; + uint32_t serial_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_WAYLAND_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_x11.cc b/src/flutter/shell/platform/linux_embedded/window/elinux_window_x11.cc similarity index 52% rename from src/flutter/shell/platform/linux_embedded/window/linuxes_window_x11.cc rename to src/flutter/shell/platform/linux_embedded/window/elinux_window_x11.cc index 447375b6..862c4d64 100644 --- a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_x11.cc +++ b/src/flutter/shell/platform/linux_embedded/window/elinux_window_x11.cc @@ -1,8 +1,8 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/linux_embedded/window/linuxes_window_x11.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window_x11.h" #include #include @@ -13,23 +13,30 @@ namespace flutter { -LinuxesWindowX11::LinuxesWindowX11(FlutterWindowMode window_mode, int32_t width, - int32_t height, bool show_cursor) { - window_mode_ = window_mode; - current_width_ = width; - current_height_ = height; - show_cursor_ = show_cursor; +namespace { +// Only Button1 - Button5 are defined in X11/X.h +constexpr int kButton6 = 6; +constexpr int kButton7 = 7; +constexpr int kButton8 = 8; +constexpr int kButton9 = 9; +} // namespace + +ELinuxWindowX11::ELinuxWindowX11(FlutterDesktopViewProperties view_properties) { + view_properties_ = view_properties; + current_scale_ = + view_properties.force_scale_factor ? view_properties.scale_factor : 1.0; + SetRotation(view_properties_.view_rotation); display_ = XOpenDisplay(NULL); if (!display_) { - LINUXES_LOG(ERROR) << "Failed to open display."; + ELINUX_LOG(ERROR) << "Failed to open display."; return; } display_valid_ = true; } -LinuxesWindowX11::~LinuxesWindowX11() { +ELinuxWindowX11::~ELinuxWindowX11() { display_valid_ = false; if (display_) { XSetCloseDownMode(display_, DestroyAll); @@ -37,7 +44,7 @@ LinuxesWindowX11::~LinuxesWindowX11() { } } -bool LinuxesWindowX11::IsValid() const { +bool ELinuxWindowX11::IsValid() const { if (!display_valid_ || !native_window_ || !render_surface_ || !native_window_->IsValid() || !render_surface_->IsValid()) { return false; @@ -45,7 +52,7 @@ bool LinuxesWindowX11::IsValid() const { return true; } -bool LinuxesWindowX11::DispatchEvent() { +bool ELinuxWindowX11::DispatchEvent() { while (XPending(display_)) { XEvent event; XNextEvent(display_, &event); @@ -85,13 +92,19 @@ bool LinuxesWindowX11::DispatchEvent() { } break; case ConfigureNotify: { - if (((event.xconfigure.width != current_width_) || - (event.xconfigure.height != current_height_))) { - current_width_ = event.xconfigure.width; - current_height_ = event.xconfigure.height; + auto width = event.xconfigure.width; + auto height = event.xconfigure.height; + if (current_rotation_ == 90 || current_rotation_ == 270) { + std::swap(width, height); + } + + if (((width != view_properties_.width) || + (height != view_properties_.height))) { + view_properties_.width = width; + view_properties_.height = height; if (binding_handler_delegate_) { - binding_handler_delegate_->OnWindowSizeChanged(current_width_, - current_height_); + binding_handler_delegate_->OnWindowSizeChanged( + view_properties_.width, view_properties_.height); } } } break; @@ -108,14 +121,21 @@ bool LinuxesWindowX11::DispatchEvent() { return true; } -bool LinuxesWindowX11::CreateRenderSurface(int32_t width, int32_t height) { - auto context_egl = - std::make_unique(std::make_unique(display_)); +bool ELinuxWindowX11::CreateRenderSurface(int32_t width, + int32_t height, + bool enable_impeller) { + auto context_egl = std::make_unique( + std::make_unique(display_), enable_impeller); + if (current_rotation_ == 90 || current_rotation_ == 270) { + std::swap(width, height); + } native_window_ = std::make_unique( - display_, context_egl->GetAttrib(EGL_NATIVE_VISUAL_ID), width, height); + display_, context_egl->GetAttrib(EGL_NATIVE_VISUAL_ID), + view_properties_.title, width, height, view_properties_.enable_vsync, + view_properties_.view_mode == FlutterDesktopViewMode::kFullscreen); if (!native_window_->IsValid()) { - LINUXES_LOG(ERROR) << "Failed to create the native window"; + ELINUX_LOG(ERROR) << "Failed to create the native window"; return false; } @@ -125,43 +145,56 @@ bool LinuxesWindowX11::CreateRenderSurface(int32_t width, int32_t height) { return true; } -void LinuxesWindowX11::DestroyRenderSurface() { +void ELinuxWindowX11::DestroyRenderSurface() { // destroy the main surface before destroying the client window on X11. render_surface_ = nullptr; native_window_ = nullptr; } -void LinuxesWindowX11::SetView(WindowBindingHandlerDelegate* window) { +void ELinuxWindowX11::SetView(WindowBindingHandlerDelegate* window) { binding_handler_delegate_ = window; } -LinuxesRenderSurfaceTarget* LinuxesWindowX11::GetRenderSurfaceTarget() const { +ELinuxRenderSurfaceTarget* ELinuxWindowX11::GetRenderSurfaceTarget() const { return render_surface_.get(); } -double LinuxesWindowX11::GetDpiScale() { return current_scale_; } +uint16_t ELinuxWindowX11::GetRotationDegree() const { + return current_rotation_; +} + +double ELinuxWindowX11::GetDpiScale() { + return current_scale_; +} -PhysicalWindowBounds LinuxesWindowX11::GetPhysicalWindowBounds() { +PhysicalWindowBounds ELinuxWindowX11::GetPhysicalWindowBounds() { return {GetCurrentWidth(), GetCurrentHeight()}; } -void LinuxesWindowX11::UpdateFlutterCursor(const std::string& cursor_name) { +int32_t ELinuxWindowX11::GetFrameRate() { + return 60000; +} + +void ELinuxWindowX11::UpdateFlutterCursor(const std::string& cursor_name) { // TODO: implement here } -void LinuxesWindowX11::UpdateVirtualKeyboardStatus(const bool show) { +void ELinuxWindowX11::UpdateVirtualKeyboardStatus(const bool show) { // currently not supported. } -std::string LinuxesWindowX11::GetClipboardData() { return clipboard_data_; } +std::string ELinuxWindowX11::GetClipboardData() { + return clipboard_data_; +} -void LinuxesWindowX11::SetClipboardData(const std::string& data) { +void ELinuxWindowX11::SetClipboardData(const std::string& data) { clipboard_data_ = data; } -void LinuxesWindowX11::HandlePointerButtonEvent(uint32_t button, - bool button_pressed, int16_t x, - int16_t y) { +void ELinuxWindowX11::HandlePointerButtonEvent(uint32_t button, + bool button_pressed, + int16_t x, + int16_t y) { if (binding_handler_delegate_) { FlutterPointerMouseButtons flutter_button; switch (button) { @@ -175,13 +208,25 @@ void LinuxesWindowX11::HandlePointerButtonEvent(uint32_t button, flutter_button = kFlutterPointerButtonMouseSecondary; break; case Button4: + case Button5: + case kButton6: + case kButton7: { + const bool vertical_scroll = (button == Button4 || button == Button5); + const double delta = button == Button5 ? 1 : -1; + constexpr int32_t kScrollOffsetMultiplier = 20; + binding_handler_delegate_->OnScroll(x, y, vertical_scroll ? 0 : delta, + vertical_scroll ? delta : 0, + kScrollOffsetMultiplier); + return; + } + case kButton8: flutter_button = kFlutterPointerButtonMouseBack; break; - case Button5: + case kButton9: flutter_button = kFlutterPointerButtonMouseForward; break; default: - LINUXES_LOG(ERROR) << "Not expected button input: " << button; + ELINUX_LOG(ERROR) << "Not expected button input: " << button; return; } if (button_pressed) { diff --git a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_x11.h b/src/flutter/shell/platform/linux_embedded/window/elinux_window_x11.h similarity index 56% rename from src/flutter/shell/platform/linux_embedded/window/linuxes_window_x11.h rename to src/flutter/shell/platform/linux_embedded/window/elinux_window_x11.h index e385436b..eb11171e 100644 --- a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_x11.h +++ b/src/flutter/shell/platform/linux_embedded/window/elinux_window_x11.h @@ -1,33 +1,34 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_X11_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_X11_H_ +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_X11_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_X11_H_ #include -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h" -#include "flutter/shell/platform/linux_embedded/window/linuxes_window.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl.h" +#include "flutter/shell/platform/linux_embedded/window/elinux_window.h" #include "flutter/shell/platform/linux_embedded/window/native_window_x11.h" #include "flutter/shell/platform/linux_embedded/window_binding_handler.h" namespace flutter { -class LinuxesWindowX11 : public LinuxesWindow, public WindowBindingHandler { +class ELinuxWindowX11 : public ELinuxWindow, public WindowBindingHandler { public: - LinuxesWindowX11(FlutterWindowMode window_mode, int32_t width, int32_t height, - bool show_cursor); - ~LinuxesWindowX11(); + ELinuxWindowX11(FlutterDesktopViewProperties view_properties); + ~ELinuxWindowX11(); - // |LinuxesWindow| + // |ELinuxWindow| bool IsValid() const override; // |FlutterWindowBindingHandler| bool DispatchEvent() override; // |FlutterWindowBindingHandler| - bool CreateRenderSurface(int32_t width, int32_t height) override; + bool CreateRenderSurface(int32_t width, + int32_t height, + bool enable_impeller) override; // |FlutterWindowBindingHandler| void DestroyRenderSurface() override; @@ -36,7 +37,10 @@ class LinuxesWindowX11 : public LinuxesWindow, public WindowBindingHandler { void SetView(WindowBindingHandlerDelegate* view) override; // |FlutterWindowBindingHandler| - LinuxesRenderSurfaceTarget* GetRenderSurfaceTarget() const override; + ELinuxRenderSurfaceTarget* GetRenderSurfaceTarget() const override; + + // |FlutterWindowBindingHandler| + uint16_t GetRotationDegree() const override; // |FlutterWindowBindingHandler| double GetDpiScale() override; @@ -44,6 +48,9 @@ class LinuxesWindowX11 : public LinuxesWindow, public WindowBindingHandler { // |FlutterWindowBindingHandler| PhysicalWindowBounds GetPhysicalWindowBounds() override; + // |FlutterWindowBindingHandler| + int32_t GetFrameRate() override; + // |FlutterWindowBindingHandler| void UpdateFlutterCursor(const std::string& cursor_name) override; @@ -58,13 +65,11 @@ class LinuxesWindowX11 : public LinuxesWindow, public WindowBindingHandler { private: // Handles the events of the mouse button. - void HandlePointerButtonEvent(uint32_t button, bool button_pressed, int16_t x, + void HandlePointerButtonEvent(uint32_t button, + bool button_pressed, + int16_t x, int16_t y); - // A pointer to a FlutterWindowsView that can be used to update engine - // windowing and input state. - WindowBindingHandlerDelegate* binding_handler_delegate_ = nullptr; - Display* display_ = nullptr; std::unique_ptr native_window_; std::unique_ptr render_surface_; @@ -74,4 +79,4 @@ class LinuxesWindowX11 : public LinuxesWindow, public WindowBindingHandler { } // namespace flutter -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_X11_H_ +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_ELINUX_WINDOW_X11_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/linuxes_window.h b/src/flutter/shell/platform/linux_embedded/window/linuxes_window.h deleted file mode 100644 index c3d344c6..00000000 --- a/src/flutter/shell/platform/linux_embedded/window/linuxes_window.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_H_ - -#include "flutter/shell/platform/linux_embedded/public/flutter_linuxes.h" - -namespace flutter { - -class LinuxesWindow { - public: - LinuxesWindow() = default; - virtual ~LinuxesWindow() = default; - - protected: - virtual bool IsValid() const = 0; - - uint32_t GetCurrentWidth() const { return current_width_; } - uint32_t GetCurrentHeight() const { return current_height_; } - - double current_scale_ = 1.0; - - int32_t current_width_ = -1; - int32_t current_height_ = -1; - - bool show_cursor_; - - FlutterWindowMode window_mode_; - - double pointer_x_ = 0; - double pointer_y_ = 0; - - std::string clipboard_data_ = ""; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.cc b/src/flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.cc deleted file mode 100644 index 10d3e843..00000000 --- a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.cc +++ /dev/null @@ -1,1083 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.h" - -#include -#include -#include -#include - -#include -#include - -#include "flutter/shell/platform/linux_embedded/logger.h" -#include "flutter/shell/platform/linux_embedded/surface/context_egl.h" - -namespace flutter { - -namespace { -constexpr char kWestonDesktopShell[] = "weston_desktop_shell"; -constexpr char kZwpTextInputManagerV1[] = "zwp_text_input_manager_v1"; -constexpr char kZwpTextInputManagerV3[] = "zwp_text_input_manager_v3"; - -constexpr char kWlCursorThemeBottomLeftCorner[] = "bottom_left_corner"; -constexpr char kWlCursorThemeBottomRightCorner[] = "bottom_right_corner"; -constexpr char kWlCursorThemeBottomSide[] = "bottom_side"; -constexpr char kWlCursorThemeGrabbing[] = "grabbing"; -constexpr char kWlCursorThemeLeftPtr[] = "left_ptr"; -constexpr char kWlCursorThemeLeftSide[] = "left_side"; -constexpr char kWlCursorThemeRightSide[] = "right_side"; -constexpr char kWlCursorThemeTopLeftCorner[] = "top_left_corner"; -constexpr char kWlCursorThemeTopRightCorner[] = "top_right_corner"; -constexpr char kWlCursorThemeTopSide[] = "top_side"; -constexpr char kWlCursorThemeXterm[] = "xterm"; -constexpr char kWlCursorThemeHand1[] = "hand1"; -constexpr char kWlCursorThemeWatch[] = "watch"; -constexpr char kCursorNameNone[] = "none"; - -constexpr char kClipboardMimeTypeText[] = "text/plain"; -} // namespace - -const wl_registry_listener LinuxesWindowWayland::kWlRegistryListener = { - .global = - [](void* data, wl_registry* wl_registry, uint32_t name, - const char* interface, uint32_t version) { - auto self = reinterpret_cast(data); - self->WlRegistryHandler(wl_registry, name, interface, version); - }, - .global_remove = - [](void* data, wl_registry* wl_registry, uint32_t name) { - auto self = reinterpret_cast(data); - self->WlUnRegistryHandler(wl_registry, name); - }, -}; - -const xdg_wm_base_listener LinuxesWindowWayland::kXdgWmBaseListener = { - .ping = [](void* data, xdg_wm_base* xdg_wm_base, - uint32_t serial) { xdg_wm_base_pong(xdg_wm_base, serial); }, -}; - -const xdg_surface_listener LinuxesWindowWayland::kXdgSurfaceListener = { - .configure = - [](void* data, xdg_surface* xdg_surface, uint32_t serial) { - xdg_surface_ack_configure(xdg_surface, serial); - }, -}; - -const wl_seat_listener LinuxesWindowWayland::kWlSeatListener = { - .capabilities = [](void* data, wl_seat* seat, uint32_t caps) -> void { - auto self = reinterpret_cast(data); - - if ((caps & WL_SEAT_CAPABILITY_POINTER) && !self->wl_pointer_) { - self->wl_pointer_ = wl_seat_get_pointer(seat); - wl_pointer_add_listener(self->wl_pointer_, &kWlPointerListener, self); - } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && self->wl_pointer_) { - wl_pointer_destroy(self->wl_pointer_); - self->wl_pointer_ = nullptr; - } - - if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !self->wl_touch_) { - self->wl_touch_ = wl_seat_get_touch(seat); - wl_touch_add_listener(self->wl_touch_, &kWlTouchListener, self); - } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && self->wl_touch_) { - wl_touch_destroy(self->wl_touch_); - self->wl_touch_ = nullptr; - } - - if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !self->wl_keyboard_) { - self->wl_keyboard_ = wl_seat_get_keyboard(seat); - wl_keyboard_add_listener(self->wl_keyboard_, &kWlKeyboardListener, - self); - } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && self->wl_keyboard_) { - wl_keyboard_destroy(self->wl_keyboard_); - self->wl_keyboard_ = nullptr; - } - }, -}; - -const wl_pointer_listener LinuxesWindowWayland::kWlPointerListener = { - .enter = [](void* data, wl_pointer* wl_pointer, uint32_t serial, - wl_surface* surface, wl_fixed_t surface_x, - wl_fixed_t surface_y) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - if (self->show_cursor_) { - self->cursor_info_.pointer = wl_pointer; - self->cursor_info_.serial = serial; - } - - if (self->binding_handler_delegate_) { - double x = wl_fixed_to_double(surface_x); - double y = wl_fixed_to_double(surface_y); - self->binding_handler_delegate_->OnPointerMove(x, y); - self->pointer_x_ = x; - self->pointer_y_ = y; - } - }, - .leave = [](void* data, wl_pointer* pointer, uint32_t serial, - wl_surface* surface) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnPointerLeave(); - self->pointer_x_ = -1; - self->pointer_y_ = -1; - } - }, - .motion = [](void* data, wl_pointer* pointer, uint32_t time, - wl_fixed_t surface_x, wl_fixed_t surface_y) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_) { - double x = wl_fixed_to_double(surface_x); - double y = wl_fixed_to_double(surface_y); - self->binding_handler_delegate_->OnPointerMove(x, y); - self->pointer_x_ = x; - self->pointer_y_ = y; - } - }, - .button = [](void* data, wl_pointer* pointer, uint32_t serial, - uint32_t time, uint32_t button, uint32_t status) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - if (self->binding_handler_delegate_) { - FlutterPointerMouseButtons flutter_button; - switch (button) { - case BTN_LEFT: - flutter_button = kFlutterPointerButtonMousePrimary; - break; - case BTN_RIGHT: - flutter_button = kFlutterPointerButtonMouseSecondary; - break; - case BTN_MIDDLE: - flutter_button = kFlutterPointerButtonMouseMiddle; - break; - case BTN_BACK: - flutter_button = kFlutterPointerButtonMouseBack; - break; - case BTN_FORWARD: - flutter_button = kFlutterPointerButtonMouseForward; - break; - default: - LINUXES_LOG(ERROR) << "Not expected button input: " << button; - return; - } - - if (status == WL_POINTER_BUTTON_STATE_PRESSED) { - self->binding_handler_delegate_->OnPointerDown( - self->pointer_x_, self->pointer_y_, flutter_button); - } else { - self->binding_handler_delegate_->OnPointerUp( - self->pointer_x_, self->pointer_y_, flutter_button); - } - } - }, - .axis = [](void* data, wl_pointer* wl_pointer, uint32_t time, uint32_t axis, - wl_fixed_t value) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_) { - double delta = wl_fixed_to_double(value); - constexpr int32_t kScrollOffsetMultiplier = 20; - self->binding_handler_delegate_->OnScroll( - self->pointer_x_, self->pointer_y_, - axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? 0 : delta, - axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? delta : 0, - kScrollOffsetMultiplier); - } - }, -}; - -const wl_touch_listener LinuxesWindowWayland::kWlTouchListener = { - .down = [](void* data, wl_touch* wl_touch, uint32_t serial, uint32_t time, - wl_surface* surface, int32_t id, wl_fixed_t surface_x, - wl_fixed_t surface_y) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - if (self->binding_handler_delegate_) { - double x = wl_fixed_to_double(surface_x); - double y = wl_fixed_to_double(surface_y); - self->binding_handler_delegate_->OnTouchDown(time, id, x, y); - } - }, - .up = [](void* data, wl_touch* wl_touch, uint32_t serial, uint32_t time, - int32_t id) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnTouchUp(time, id); - } - }, - .motion = [](void* data, wl_touch* wl_touch, uint32_t time, int32_t id, - wl_fixed_t surface_x, wl_fixed_t surface_y) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_) { - double x = wl_fixed_to_double(surface_x); - double y = wl_fixed_to_double(surface_y); - self->binding_handler_delegate_->OnTouchMotion(time, id, x, y); - } - }, - .frame = [](void* data, wl_touch* wl_touch) -> void {}, - .cancel = [](void* data, wl_touch* wl_touch) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnTouchCancel(); - } - }, -}; - -const wl_keyboard_listener LinuxesWindowWayland::kWlKeyboardListener = { - .keymap = [](void* data, wl_keyboard* wl_keyboard, uint32_t format, int fd, - uint32_t size) -> void { - auto self = reinterpret_cast(data); - assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1); - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnKeyMap(format, fd, size); - } - }, - .enter = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, - wl_surface* surface, wl_array* keys) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - }, - .leave = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, - wl_surface* surface) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - }, - .key = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, - uint32_t time, uint32_t key, uint32_t state) -> void { - auto self = reinterpret_cast(data); - self->serial_ = serial; - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnKey( - key, state == WL_KEYBOARD_KEY_STATE_PRESSED); - } - }, - .modifiers = [](void* data, wl_keyboard* wl_keyboard, uint32_t serial, - uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnKeyModifiers( - mods_depressed, mods_latched, mods_locked, group); - } - }, - .repeat_info = [](void* data, wl_keyboard* wl_keyboard, int rate, - int delay) -> void {}, -}; - -const wl_output_listener LinuxesWindowWayland::kWlOutputListener = { - .geometry = [](void* data, wl_output* wl_output, int32_t x, int32_t y, - int32_t physical_width, int32_t physical_height, - int32_t subpixel, const char* make, const char* model, - int32_t output_transform) -> void {}, - .mode = [](void* data, wl_output* wl_output, uint32_t flags, int32_t width, - int32_t height, int32_t refresh) -> void { - auto self = reinterpret_cast(data); - if (flags & WL_OUTPUT_MODE_CURRENT) { - LINUXES_LOG(INFO) << "Display output resolution: " << width << "x" - << height; - if (self->window_mode_ == FlutterWindowMode::kFullscreen) { - self->current_width_ = width; - self->current_height_ = height; - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnWindowSizeChanged(width, height); - } - } - } - }, - .done = [](void* data, wl_output* wl_output) -> void {}, - .scale = [](void* data, wl_output* wl_output, int32_t scale) -> void { - auto self = reinterpret_cast(data); - LINUXES_LOG(INFO) << "Display output scale: " << scale; - self->current_scale_ = scale; - }, -}; - -const zwp_text_input_v1_listener LinuxesWindowWayland::kZwpTextInputV1Listener = - { - .enter = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - wl_surface* surface) -> void { - auto self = reinterpret_cast(data); - // If there is no input data, the backspace key cannot be used, - // so set dummy data. - if (self->zwp_text_input_v1_) { - zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, - " ", 1, 1); - } - }, - .leave = [](void* data, zwp_text_input_v1* zwp_text_input_v1) -> void { - auto self = reinterpret_cast(data); - if (self->zwp_text_input_v1_) { - zwp_text_input_v1_hide_input_panel(self->zwp_text_input_v1_); - } - }, - .modifiers_map = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - wl_array* map) -> void {}, - .input_panel_state = [](void* data, - zwp_text_input_v1* zwp_text_input_v1, - uint32_t state) -> void {}, - .preedit_string = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - uint32_t serial, const char* text, - const char* commit) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_ && strlen(text)) { - self->binding_handler_delegate_->OnVirtualKey(text[0]); - } - if (self->zwp_text_input_v1_) { - zwp_text_input_v1_reset(self->zwp_text_input_v1_); - // If there is no input data, the backspace key cannot be used, - // so set dummy data. - zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, - " ", 1, 1); - } - }, - .preedit_styling = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - uint32_t index, uint32_t length, - uint32_t style) -> void {}, - .preedit_cursor = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - int32_t index) -> void {}, - .commit_string = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - uint32_t serial, const char* text) -> void { - // commit_string is notified only when the space key is pressed. - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_ && strlen(text)) { - self->binding_handler_delegate_->OnVirtualKey(text[0]); - } - // If there is no input data, the backspace key cannot be used, - // so set dummy data. - if (self->zwp_text_input_v1_) { - zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, - " ", 1, 1); - } - }, - .cursor_position = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - int32_t index, int32_t anchor) -> void {}, - .delete_surrounding_text = [](void* data, - zwp_text_input_v1* zwp_text_input_v1, - int32_t index, uint32_t length) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_) { - self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_BACKSPACE); - } - // If there is no input data, the backspace key cannot be used, - // so set dummy data. - if (self->zwp_text_input_v1_) { - zwp_text_input_v1_set_surrounding_text(self->zwp_text_input_v1_, - " ", 1, 1); - } - }, - .keysym = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - uint32_t serial, uint32_t time, uint32_t sym, - uint32_t state, uint32_t modifiers) -> void { - auto self = reinterpret_cast(data); - if ((state == WL_KEYBOARD_KEY_STATE_PRESSED) && - (self->binding_handler_delegate_)) { - switch (sym) { - case XKB_KEY_Left: - self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_LEFT); - break; - case XKB_KEY_Right: - self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_RIGHT); - break; - case XKB_KEY_Up: - self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_UP); - break; - case XKB_KEY_Down: - self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_DOWN); - break; - case XKB_KEY_Tab: - self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_TAB); - break; - case XKB_KEY_Return: - self->binding_handler_delegate_->OnVirtualSpecialKey(KEY_ENTER); - break; - default: - break; - } - } - }, - .language = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - uint32_t serial, const char* language) -> void {}, - .text_direction = [](void* data, zwp_text_input_v1* zwp_text_input_v1, - uint32_t serial, uint32_t direction) -> void {}, -}; - -const zwp_text_input_v3_listener LinuxesWindowWayland::kZwpTextInputV3Listener = - { - .enter = [](void* data, zwp_text_input_v3* zwp_text_input_v3, - wl_surface* surface) -> void { - // To appear the on-screen keyboard when the user returns to a Flutter - // app which needs to show the on-screen keyboard. - auto self = reinterpret_cast(data); - if (self->is_requested_show_virtual_keyboard_) { - self->ShowVirtualKeyboard(); - } - }, - .leave = [](void* data, zwp_text_input_v3* zwp_text_input_v3, - wl_surface* surface) -> void {}, - .preedit_string = [](void* data, zwp_text_input_v3* zwp_text_input_v3, - const char* text, int32_t cursor_begin, - int32_t cursor_end) -> void {}, - .commit_string = [](void* data, zwp_text_input_v3* zwp_text_input_v3, - const char* text) -> void { - auto self = reinterpret_cast(data); - if (self->binding_handler_delegate_ && strlen(text)) { - self->binding_handler_delegate_->OnVirtualKey(text[0]); - } - }, - .delete_surrounding_text = - [](void* data, zwp_text_input_v3* zwp_text_input_v3, - uint32_t before_length, uint32_t after_length) -> void {}, - .done = [](void* data, zwp_text_input_v3* zwp_text_input_v3, - uint32_t serial) -> void {}, -}; - -const wl_data_device_listener LinuxesWindowWayland::kWlDataDeviceListener = { - .data_offer = [](void* data, wl_data_device* wl_data_device, - wl_data_offer* offer) -> void {}, - .enter = [](void* data, wl_data_device* wl_data_device, uint32_t serial, - wl_surface* surface, wl_fixed_t x, wl_fixed_t y, - wl_data_offer* offer) -> void {}, - .leave = [](void* data, wl_data_device* wl_data_device) -> void {}, - .motion = [](void* data, wl_data_device* wl_data_device, uint32_t time, - wl_fixed_t x, wl_fixed_t y) -> void {}, - .drop = [](void* data, wl_data_device* wl_data_device) -> void {}, - .selection = [](void* data, wl_data_device* wl_data_device, - wl_data_offer* offer) -> void { - auto self = reinterpret_cast(data); - if (self->wl_data_offer_) { - wl_data_offer_destroy(self->wl_data_offer_); - } - self->wl_data_offer_ = offer; - }, -}; - -const wl_data_source_listener LinuxesWindowWayland::kWlDataSourceListener = { - .target = [](void* data, wl_data_source* wl_data_source, - const char* mime_type) -> void {}, - .send = [](void* data, wl_data_source* wl_data_source, - const char* mime_type, int32_t fd) -> void { - if (strcmp(mime_type, kClipboardMimeTypeText)) { - LINUXES_LOG(ERROR) << "Not expected mime_type: " << mime_type; - return; - } - auto self = reinterpret_cast(data); - // Write the copied data to the clipboard. - write(fd, self->clipboard_data_.c_str(), - strlen(self->clipboard_data_.c_str())); - close(fd); - }, - .cancelled = [](void* data, wl_data_source* wl_data_source) -> void { - auto self = reinterpret_cast(data); - self->clipboard_data_ = ""; - if (self->wl_data_source_) { - wl_data_source_destroy(self->wl_data_source_); - self->wl_data_source_ = nullptr; - } - }, - .dnd_drop_performed = [](void* data, - wl_data_source* wl_data_source) -> void {}, - .dnd_finished = [](void* data, wl_data_source* wl_data_source) -> void {}, - .action = [](void* data, wl_data_source* wl_data_source, - uint32_t dnd_action) -> void {}, -}; - -LinuxesWindowWayland::LinuxesWindowWayland(FlutterWindowMode window_mode, - int32_t width, int32_t height, - bool show_cursor) - : cursor_info_({"", 0, nullptr}), - display_valid_(false), - is_requested_show_virtual_keyboard_(false), - wl_seat_(nullptr), - wl_pointer_(nullptr), - wl_touch_(nullptr), - wl_keyboard_(nullptr), - wl_shm_(nullptr), - wl_data_device_manager_(nullptr), - wl_data_device_(nullptr), - wl_data_offer_(nullptr), - wl_data_source_(nullptr), - wl_cursor_theme_(nullptr), - zwp_text_input_manager_v1_(nullptr), - zwp_text_input_manager_v3_(nullptr), - zwp_text_input_v1_(nullptr), - zwp_text_input_v3_(nullptr), - serial_(0) { - window_mode_ = window_mode; - current_width_ = width; - current_height_ = height; - show_cursor_ = show_cursor; - - wl_display_ = wl_display_connect(nullptr); - if (!wl_display_) { - LINUXES_LOG(ERROR) << "Failed to connect to the Wayland display."; - return; - } - - wl_registry_ = wl_display_get_registry(wl_display_); - if (!wl_registry_) { - LINUXES_LOG(ERROR) << "Failed to get the wayland registry."; - return; - } - - wl_registry_add_listener(wl_registry_, &kWlRegistryListener, this); - wl_display_dispatch(wl_display_); - wl_display_roundtrip(wl_display_); - - if (wl_data_device_manager_ && wl_seat_) { - wl_data_device_ = wl_data_device_manager_get_data_device( - wl_data_device_manager_, wl_seat_); - wl_data_device_add_listener(wl_data_device_, &kWlDataDeviceListener, this); - } - - // Setup text-input protocol for onscreen keyboard inputs. - { - if (zwp_text_input_manager_v3_ && wl_seat_) { - zwp_text_input_v3_ = zwp_text_input_manager_v3_get_text_input( - zwp_text_input_manager_v3_, wl_seat_); - if (!zwp_text_input_v3_) { - LINUXES_LOG(ERROR) << "Failed to create the text input manager v3."; - return; - } - zwp_text_input_v3_add_listener(zwp_text_input_v3_, - &kZwpTextInputV3Listener, this); - } else if (zwp_text_input_manager_v1_) { - zwp_text_input_v1_ = zwp_text_input_manager_v1_create_text_input( - zwp_text_input_manager_v1_); - if (!zwp_text_input_v1_) { - LINUXES_LOG(ERROR) << "Failed to create text input manager v1."; - return; - } - zwp_text_input_v1_add_listener(zwp_text_input_v1_, - &kZwpTextInputV1Listener, this); - } else { - // do nothing. - } - } - - if (weston_desktop_shell_) { - weston_desktop_shell_desktop_ready(weston_desktop_shell_); - } - - display_valid_ = true; -} - -LinuxesWindowWayland::~LinuxesWindowWayland() { - display_valid_ = false; - - if (weston_desktop_shell_) { - weston_desktop_shell_destroy(weston_desktop_shell_); - weston_desktop_shell_ = nullptr; - } - - if (wl_cursor_theme_) { - wl_cursor_theme_destroy(wl_cursor_theme_); - wl_cursor_theme_ = nullptr; - } - - { - if (zwp_text_input_v1_) { - zwp_text_input_v1_destroy(zwp_text_input_v1_); - zwp_text_input_v1_ = nullptr; - } - - if (zwp_text_input_manager_v1_) { - zwp_text_input_manager_v1_destroy(zwp_text_input_manager_v1_); - zwp_text_input_manager_v1_ = nullptr; - } - - if (zwp_text_input_v3_) { - zwp_text_input_v3_destroy(zwp_text_input_v3_); - zwp_text_input_v3_ = nullptr; - } - - if (zwp_text_input_manager_v3_) { - zwp_text_input_manager_v3_destroy(zwp_text_input_manager_v3_); - zwp_text_input_manager_v3_ = nullptr; - } - } - - if (wl_data_offer_) { - wl_data_offer_destroy(wl_data_offer_); - wl_data_offer_ = nullptr; - } - - if (wl_data_source_) { - wl_data_source_destroy(wl_data_source_); - wl_data_source_ = nullptr; - } - - if (wl_data_device_) { - if (wl_data_device_manager_version_ >= - WL_DATA_DEVICE_RELEASE_SINCE_VERSION) { - wl_data_device_release(wl_data_device_); - } else { - wl_data_device_destroy(wl_data_device_); - } - wl_data_device_ = nullptr; - } - - if (wl_data_device_manager_) { - wl_data_device_manager_destroy(wl_data_device_manager_); - wl_data_device_manager_ = nullptr; - } - - if (wl_pointer_) { - wl_pointer_destroy(wl_pointer_); - wl_pointer_ = nullptr; - } - - if (wl_touch_) { - wl_touch_destroy(wl_touch_); - wl_touch_ = nullptr; - } - - if (wl_keyboard_) { - wl_keyboard_destroy(wl_keyboard_); - wl_keyboard_ = nullptr; - } - - if (wl_seat_) { - wl_seat_destroy(wl_seat_); - wl_seat_ = nullptr; - } - - if (wl_output_) { - wl_output_destroy(wl_output_); - wl_output_ = nullptr; - } - - if (wl_shm_) { - wl_shm_destroy(wl_shm_); - wl_shm_ = nullptr; - } - - if (xdg_toplevel_) { - xdg_toplevel_destroy(xdg_toplevel_); - xdg_toplevel_ = nullptr; - } - - if (xdg_wm_base_) { - xdg_wm_base_destroy(xdg_wm_base_); - xdg_wm_base_ = nullptr; - } - - if (wl_compositor_) { - wl_compositor_destroy(wl_compositor_); - wl_compositor_ = nullptr; - } - - if (wl_registry_) { - wl_registry_destroy(wl_registry_); - wl_registry_ = nullptr; - } - - if (wl_display_) { - wl_display_flush(wl_display_); - wl_display_disconnect(wl_display_); - wl_display_ = nullptr; - } -} - -void LinuxesWindowWayland::SetView(WindowBindingHandlerDelegate* window) { - binding_handler_delegate_ = window; -} - -LinuxesRenderSurfaceTarget* LinuxesWindowWayland::GetRenderSurfaceTarget() - const { - return render_surface_.get(); -} - -double LinuxesWindowWayland::GetDpiScale() { return current_scale_; } - -PhysicalWindowBounds LinuxesWindowWayland::GetPhysicalWindowBounds() { - return {GetCurrentWidth(), GetCurrentHeight()}; -} - -bool LinuxesWindowWayland::DispatchEvent() { - if (!IsValid()) { - LINUXES_LOG(ERROR) << "Wayland display is invalid."; - return false; - } - - // If Wayland compositor (Weston) terminates, -1 is returned. - return (wl_display_dispatch(wl_display_) != -1); -} - -bool LinuxesWindowWayland::CreateRenderSurface(int32_t width, int32_t height) { - if (!display_valid_) { - LINUXES_LOG(ERROR) << "Wayland display is invalid."; - return false; - } - - if (!wl_compositor_) { - LINUXES_LOG(ERROR) << "Wl_compositor is invalid"; - return false; - } - - if (!xdg_wm_base_) { - LINUXES_LOG(ERROR) << "Xdg-shell is invalid"; - return false; - } - - if (window_mode_ == FlutterWindowMode::kFullscreen) { - width = current_width_; - height = current_height_; - } - - LINUXES_LOG(TRACE) << "Created the Wayland surface: " << width << "x" - << height; - if (show_cursor_) { - wl_cursor_surface_ = wl_compositor_create_surface(wl_compositor_); - if (!wl_cursor_surface_) { - LINUXES_LOG(ERROR) - << "Failed to create the compositor surface for cursor."; - return false; - } - } - - native_window_ = - std::make_unique(wl_compositor_, width, height); - - xdg_surface_ = - xdg_wm_base_get_xdg_surface(xdg_wm_base_, native_window_->Surface()); - if (!xdg_surface_) { - LINUXES_LOG(ERROR) << "Failed to get the xdg surface."; - return false; - } - xdg_surface_add_listener(xdg_surface_, &kXdgSurfaceListener, this); - xdg_toplevel_ = xdg_surface_get_toplevel(xdg_surface_); - xdg_toplevel_set_title(xdg_toplevel_, "Flutter"); - wl_surface_commit(native_window_->Surface()); - - render_surface_ = std::make_unique(std::make_unique( - std::make_unique(wl_display_))); - render_surface_->SetNativeWindow(native_window_.get()); - - return true; -} - -void LinuxesWindowWayland::DestroyRenderSurface() { - // destroy the main surface before destroying the client window on Wayland. - { - render_surface_ = nullptr; - native_window_ = nullptr; - } - - if (xdg_surface_) { - xdg_surface_destroy(xdg_surface_); - xdg_surface_ = nullptr; - } - - if (wl_cursor_surface_) { - wl_surface_destroy(wl_cursor_surface_); - wl_cursor_surface_ = nullptr; - } -} - -void LinuxesWindowWayland::UpdateVirtualKeyboardStatus(const bool show) { - // Not supported virtual keyboard. - if (!(zwp_text_input_v1_ || zwp_text_input_v3_) || !wl_seat_) { - return; - } - - is_requested_show_virtual_keyboard_ = show; - if (is_requested_show_virtual_keyboard_) { - ShowVirtualKeyboard(); - } else { - DismissVirtualKeybaord(); - } -} - -void LinuxesWindowWayland::UpdateFlutterCursor(const std::string& cursor_name) { - if (show_cursor_) { - if (cursor_name.compare(cursor_info_.cursor_name) == 0) { - return; - } - cursor_info_.cursor_name = cursor_name; - - if (cursor_name.compare(kCursorNameNone) == 0) { - // Turn off the cursor. - wl_pointer_set_cursor(cursor_info_.pointer, cursor_info_.serial, - wl_cursor_surface_, 0, 0); - wl_surface_attach(wl_cursor_surface_, nullptr, 0, 0); - wl_surface_damage(wl_cursor_surface_, 0, 0, 0, 0); - wl_surface_commit(wl_cursor_surface_); - return; - } - - auto wl_cursor = GetWlCursor(cursor_name); - if (!wl_cursor) { - return; - } - auto image = wl_cursor->images[0]; - auto buffer = wl_cursor_image_get_buffer(image); - if (buffer) { - wl_pointer_set_cursor(cursor_info_.pointer, cursor_info_.serial, - wl_cursor_surface_, image->hotspot_x, - image->hotspot_y); - wl_surface_attach(wl_cursor_surface_, buffer, 0, 0); - wl_surface_damage(wl_cursor_surface_, 0, 0, image->width, image->height); - wl_surface_commit(wl_cursor_surface_); - } - } -} - -std::string LinuxesWindowWayland::GetClipboardData() { - std::string str = ""; - - if (wl_data_offer_) { - int fd[2]; - if (pipe2(fd, O_CLOEXEC) == -1) { - return str; - } - - wl_data_offer_receive(wl_data_offer_, kClipboardMimeTypeText, fd[1]); - close(fd[1]); - wl_display_dispatch(wl_display_); - - char buf[256]; - int len; - // Read data form the clipboard. - while ((len = read(fd[0], buf, sizeof(buf))) > 0) { - str.append(buf, len); - } - close(fd[0]); - return str; - } - - return str; -} - -void LinuxesWindowWayland::SetClipboardData(const std::string& data) { - clipboard_data_ = data; - if (wl_data_device_manager_) { - if (wl_data_source_) { - wl_data_source_destroy(wl_data_source_); - wl_data_source_ = nullptr; - } - - wl_data_source_ = - wl_data_device_manager_create_data_source(wl_data_device_manager_); - if (!wl_data_source_) { - return; - } - - wl_data_source_offer(wl_data_source_, kClipboardMimeTypeText); - wl_data_source_add_listener(wl_data_source_, &kWlDataSourceListener, this); - wl_data_device_set_selection(wl_data_device_, wl_data_source_, serial_); - } -} - -bool LinuxesWindowWayland::IsValid() const { - if (!display_valid_ || !native_window_ || !render_surface_ || - !native_window_->IsValid() || !render_surface_->IsValid()) { - return false; - } - return true; -} - -void LinuxesWindowWayland::WlRegistryHandler(wl_registry* wl_registry, - uint32_t name, - const char* interface, - uint32_t version) { - if (!strcmp(interface, wl_compositor_interface.name)) { - wl_compositor_ = static_cast( - wl_registry_bind(wl_registry, name, &wl_compositor_interface, 1)); - return; - } - - if (!strcmp(interface, xdg_wm_base_interface.name)) { - xdg_wm_base_ = static_cast( - wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, 1)); - xdg_wm_base_add_listener(xdg_wm_base_, &kXdgWmBaseListener, this); - return; - } - - if (!strcmp(interface, wl_seat_interface.name)) { - wl_seat_ = static_cast( - wl_registry_bind(wl_registry, name, &wl_seat_interface, 1)); - wl_seat_add_listener(wl_seat_, &kWlSeatListener, this); - return; - } - -#ifdef DESKTOP_SHELL - if (!strcmp(interface, kWestonDesktopShell)) { - weston_desktop_shell_ = - static_cast(wl_registry_bind( - wl_registry, name, &weston_desktop_shell_interface, 1)); - return; - } -#else - weston_desktop_shell_ = nullptr; -#endif - - if (!strcmp(interface, wl_output_interface.name)) { - wl_output_ = static_cast( - wl_registry_bind(wl_registry, name, &wl_output_interface, 1)); - wl_output_add_listener(wl_output_, &kWlOutputListener, this); - } - - if (!strcmp(interface, wl_shm_interface.name)) { - if (show_cursor_) { - wl_shm_ = static_cast( - wl_registry_bind(wl_registry, name, &wl_shm_interface, 1)); - wl_cursor_theme_ = wl_cursor_theme_load(nullptr, 32, wl_shm_); - if (!wl_cursor_theme_) { - LINUXES_LOG(ERROR) << "Failed to load cursor theme."; - return; - } - CreateSupportedWlCursorList(); - } - return; - } - -#ifdef USE_VIRTUAL_KEYBOARD - if (!strcmp(interface, kZwpTextInputManagerV1)) { - zwp_text_input_manager_v1_ = - static_cast(wl_registry_bind( - wl_registry, name, &zwp_text_input_manager_v1_interface, 1)); - return; - } - - if (!strcmp(interface, kZwpTextInputManagerV3)) { - zwp_text_input_manager_v3_ = - static_cast(wl_registry_bind( - wl_registry, name, &zwp_text_input_manager_v3_interface, 1)); - return; - } -#endif - - if (!strcmp(interface, wl_data_device_manager_interface.name)) { - // Save the version of wl_data_device_manager because the release method of - // wl_data_device differs depending on it. Since wl_data_device_manager has - // been released up to version 3, set the upper limit to 3. - constexpr uint32_t kMaxVersion = 3; - wl_data_device_manager_version_ = std::min(kMaxVersion, version); - wl_data_device_manager_ = static_cast( - wl_registry_bind(wl_registry, name, &wl_data_device_manager_interface, - wl_data_device_manager_version_)); - } -} - -void LinuxesWindowWayland::WlUnRegistryHandler(wl_registry* wl_registry, - uint32_t name) {} - -void LinuxesWindowWayland::CreateSupportedWlCursorList() { - std::vector wl_cursor_themes{ - kWlCursorThemeLeftPtr, - kWlCursorThemeBottomLeftCorner, - kWlCursorThemeBottomRightCorner, - kWlCursorThemeBottomSide, - kWlCursorThemeGrabbing, - kWlCursorThemeLeftSide, - kWlCursorThemeRightSide, - kWlCursorThemeTopLeftCorner, - kWlCursorThemeTopRightCorner, - kWlCursorThemeTopSide, - kWlCursorThemeXterm, - kWlCursorThemeHand1, - kWlCursorThemeWatch, - }; - - for (const auto& theme : wl_cursor_themes) { - auto wl_cursor = - wl_cursor_theme_get_cursor(wl_cursor_theme_, theme.c_str()); - if (!wl_cursor) { - LINUXES_LOG(ERROR) << "Unsupported cursor theme: " << theme.c_str(); - continue; - } - supported_wl_cursor_list_[theme] = wl_cursor; - } -} - -wl_cursor* LinuxesWindowWayland::GetWlCursor(const std::string& cursor_name) { - // Convert the cursor theme name from Flutter's cursor value to Wayland's one. - // However, Wayland has not all cursor themes corresponding to Flutter. - // If there is no Wayland's cursor theme corresponding to the Flutter's cursor - // name, it is defined as empty. - // If empty, the default cursor theme(left_ptr) will be displayed. - static const std::unordered_map - flutter_to_wayland_cursor_map = { - {"alias", ""}, - {"allScroll", ""}, - {"basic", kWlCursorThemeLeftPtr}, - {"cell", ""}, - {"click", kWlCursorThemeHand1}, - {"contextMenu", ""}, - {"copy", ""}, - {"forbidden", ""}, - {"grab", ""}, - {"grabbing", kWlCursorThemeGrabbing}, - {"help", ""}, - {"move", ""}, - {"noDrop", ""}, - {"precise", ""}, - {"progress", ""}, - {"text", kWlCursorThemeXterm}, - {"resizeColumn", ""}, - {"resizeDown", kWlCursorThemeBottomSide}, - {"resizeDownLeft", kWlCursorThemeBottomLeftCorner}, - {"resizeDownRight", kWlCursorThemeBottomRightCorner}, - {"resizeLeft", kWlCursorThemeLeftSide}, - {"resizeLeftRight", ""}, - {"resizeRight", kWlCursorThemeRightSide}, - {"resizeRow", ""}, - {"resizeUp", kWlCursorThemeTopSide}, - {"resizeUpDown", ""}, - {"resizeUpLeft", kWlCursorThemeTopLeftCorner}, - {"resizeUpRight", kWlCursorThemeTopRightCorner}, - {"resizeUpLeftDownRight", ""}, - {"resizeUpRightDownLeft", ""}, - {"verticalText", ""}, - {"wait", kWlCursorThemeWatch}, - {"zoomIn", ""}, - {"zoomOut", ""}, - }; - - if (flutter_to_wayland_cursor_map.find(cursor_name) != - flutter_to_wayland_cursor_map.end()) { - auto theme = flutter_to_wayland_cursor_map.at(cursor_name); - if (!theme.empty() && supported_wl_cursor_list_.find(theme) != - supported_wl_cursor_list_.end()) { - return supported_wl_cursor_list_[theme]; - } - } - - LINUXES_LOG(ERROR) << "Unsupported cursor: " << cursor_name.c_str(); - return supported_wl_cursor_list_[kWlCursorThemeLeftPtr]; -} - -void LinuxesWindowWayland::ShowVirtualKeyboard() { - if (zwp_text_input_v3_) { - // I'm not sure the reason, but enable needs to be called twice. - zwp_text_input_v3_enable(zwp_text_input_v3_); - zwp_text_input_v3_commit(zwp_text_input_v3_); - zwp_text_input_v3_enable(zwp_text_input_v3_); - zwp_text_input_v3_commit(zwp_text_input_v3_); - - zwp_text_input_v3_set_content_type( - zwp_text_input_v3_, ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE, - ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL); - zwp_text_input_v3_commit(zwp_text_input_v3_); - } else { - if (native_window_) { - zwp_text_input_v1_show_input_panel(zwp_text_input_v1_); - zwp_text_input_v1_activate(zwp_text_input_v1_, wl_seat_, - native_window_->Surface()); - } - } -} - -void LinuxesWindowWayland::DismissVirtualKeybaord() { - if (zwp_text_input_v3_) { - zwp_text_input_v3_disable(zwp_text_input_v3_); - zwp_text_input_v3_commit(zwp_text_input_v3_); - } else { - zwp_text_input_v1_deactivate(zwp_text_input_v1_, wl_seat_); - } -} - -} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.h b/src/flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.h deleted file mode 100644 index 9780f859..00000000 --- a/src/flutter/shell/platform/linux_embedded/window/linuxes_window_wayland.h +++ /dev/null @@ -1,156 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_WAYLAND_H_ -#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_WAYLAND_H_ - -#include -#include - -#include -#include -#include - -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h" -#include "flutter/shell/platform/linux_embedded/window/linuxes_window.h" -#include "flutter/shell/platform/linux_embedded/window/native_window_wayland.h" -#include "flutter/shell/platform/linux_embedded/window_binding_handler.h" - -// These header files are automatically generated by the -// wayland-scanner. -extern "C" { -#include "wayland/protocol/text-input-unstable-v1-client-protocol.h" -#include "wayland/protocol/text-input-unstable-v3-client-protocol.h" -#include "wayland/protocol/weston-desktop-shell-client-protocol.h" -#include "wayland/protocol/xdg-shell-client-protocol.h" -} - -namespace flutter { - -class LinuxesWindowWayland : public LinuxesWindow, public WindowBindingHandler { - public: - LinuxesWindowWayland(FlutterWindowMode window_mode, int32_t width, - int32_t height, bool show_cursor); - ~LinuxesWindowWayland(); - - // |LinuxesWindow| - bool IsValid() const override; - - // |FlutterWindowBindingHandler| - bool DispatchEvent() override; - - // |FlutterWindowBindingHandler| - bool CreateRenderSurface(int32_t width, int32_t height) override; - - // |FlutterWindowBindingHandler| - void DestroyRenderSurface() override; - - // |FlutterWindowBindingHandler| - void SetView(WindowBindingHandlerDelegate* view) override; - - // |FlutterWindowBindingHandler| - LinuxesRenderSurfaceTarget* GetRenderSurfaceTarget() const override; - - // |FlutterWindowBindingHandler| - double GetDpiScale() override; - - // |FlutterWindowBindingHandler| - PhysicalWindowBounds GetPhysicalWindowBounds() override; - - // |FlutterWindowBindingHandler| - void UpdateFlutterCursor(const std::string& cursor_name) override; - - // |FlutterWindowBindingHandler| - void UpdateVirtualKeyboardStatus(const bool show) override; - - // |FlutterWindowBindingHandler| - std::string GetClipboardData() override; - - // |FlutterWindowBindingHandler| - void SetClipboardData(const std::string& data) override; - - private: - struct CursorInfo { - std::string cursor_name; - uint32_t serial; - wl_pointer* pointer; - }; - - void WlRegistryHandler(wl_registry* wl_registry, uint32_t name, - const char* interface, uint32_t version); - - void WlUnRegistryHandler(wl_registry* wl_registry, uint32_t name); - - void CreateSupportedWlCursorList(); - - wl_cursor* GetWlCursor(const std::string& cursor_name); - - void ShowVirtualKeyboard(); - - void DismissVirtualKeybaord(); - - static const wl_registry_listener kWlRegistryListener; - static const xdg_wm_base_listener kXdgWmBaseListener; - static const xdg_surface_listener kXdgSurfaceListener; - static const wl_seat_listener kWlSeatListener; - static const wl_pointer_listener kWlPointerListener; - static const wl_touch_listener kWlTouchListener; - static const wl_keyboard_listener kWlKeyboardListener; - static const wl_output_listener kWlOutputListener; - static const wl_data_device_listener kWlDataDeviceListener; - static const wl_data_source_listener kWlDataSourceListener; - static const zwp_text_input_v1_listener kZwpTextInputV1Listener; - static const zwp_text_input_v3_listener kZwpTextInputV3Listener; - - // A pointer to a FlutterWindowsView that can be used to update engine - // windowing and input state. - WindowBindingHandlerDelegate* binding_handler_delegate_ = nullptr; - - std::unique_ptr native_window_; - std::unique_ptr render_surface_; - - bool display_valid_; - - // Indicates that exists a keyboard show request from Flutter Engine. - bool is_requested_show_virtual_keyboard_; - - wl_display* wl_display_; - wl_registry* wl_registry_; - wl_compositor* wl_compositor_; - wl_seat* wl_seat_; - wl_output* wl_output_; - wl_shm* wl_shm_; - wl_pointer* wl_pointer_; - wl_touch* wl_touch_; - wl_keyboard* wl_keyboard_; - wl_surface* wl_cursor_surface_; - wl_cursor_theme* wl_cursor_theme_; - xdg_wm_base* xdg_wm_base_; - xdg_surface* xdg_surface_; - xdg_toplevel* xdg_toplevel_; - - // text-input protocol for onscreen keyboard inputs. - zwp_text_input_manager_v1* zwp_text_input_manager_v1_; - zwp_text_input_manager_v3* zwp_text_input_manager_v3_; - zwp_text_input_v1* zwp_text_input_v1_; - zwp_text_input_v3* zwp_text_input_v3_; - - CursorInfo cursor_info_; - - // List of cursor name and wl_cursor supported by Wayland. - std::unordered_map supported_wl_cursor_list_; - - wl_data_device_manager* wl_data_device_manager_; - wl_data_device* wl_data_device_; - wl_data_offer* wl_data_offer_; - wl_data_source* wl_data_source_; - uint32_t wl_data_device_manager_version_; - uint32_t serial_; - - weston_desktop_shell* weston_desktop_shell_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_LINUXES_WINDOW_WAYLAND_H_ \ No newline at end of file diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window.h b/src/flutter/shell/platform/linux_embedded/window/native_window.h index 3ae40dc9..57c40362 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window.h +++ b/src/flutter/shell/platform/linux_embedded/window/native_window.h @@ -21,6 +21,7 @@ class NativeWindow { // Gets a window (GBM surface) for offscreen resource. EGLNativeWindowType WindowOffscreen() const { return window_offscreen_; } + // Get physical width of the window. int32_t Width() const { if (!valid_) { return -1; @@ -28,6 +29,7 @@ class NativeWindow { return width_; } + // Get physical height of the window. int32_t Height() const { if (!valid_) { return -1; @@ -35,19 +37,39 @@ class NativeWindow { return height_; } + bool EnableVsync() const { return enable_vsync_; } + virtual bool IsNeedRecreateSurfaceAfterResize() const { return false; } - virtual bool Resize(const size_t width, const size_t height) = 0; + // Sets a window position. Basically, this API is used for window decorations + // such as titlebar. + // @param[in] x_dip The x coordinate in logical pixels. + // @param[in] y_dip The y coordinate in logical pixels. + virtual void SetPosition(const int32_t x_dip, const int32_t y_dip) { + x_ = x_dip; + y_ = y_dip; + }; + + // @param[in] width_px Physical width of the window. + // @param[in] height_px Physical height of the window. + virtual bool Resize(const size_t width_px, const size_t height_px) = 0; // Swaps frame buffers. This API performs processing only for the DRM-GBM // backend. It is prepared to make the interface common. - virtual void SwapBuffers(){/* do nothing. */}; + virtual void SwapBuffers() { /* do nothing. */ }; protected: EGLNativeWindowType window_; EGLNativeWindowType window_offscreen_; + bool enable_vsync_; + // Physical width of the window. int32_t width_; + // Physical height of the window. int32_t height_; + // The x coordinate of the window in logical pixels. + int32_t x_; + // The y coordinate of the window in logical pixels. + int32_t y_; bool valid_ = false; }; diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc b/src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc index 31e17d08..73023fd2 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_drm.cc @@ -14,18 +14,50 @@ #include "flutter/shell/platform/linux_embedded/surface/cursor_data.h" namespace flutter { +namespace { +constexpr char kFlutterDrmConnectorEnvironmentKey[] = "FLUTTER_DRM_CONNECTOR"; +static const std::unordered_map connector_names = { + {DRM_MODE_CONNECTOR_Unknown, "Unknown"}, + {DRM_MODE_CONNECTOR_VGA, "VGA"}, + {DRM_MODE_CONNECTOR_DVII, "DVI-I"}, + {DRM_MODE_CONNECTOR_DVID, "DVI-D"}, + {DRM_MODE_CONNECTOR_DVIA, "DVI-A"}, + {DRM_MODE_CONNECTOR_Composite, "Composite"}, + {DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO"}, + {DRM_MODE_CONNECTOR_LVDS, "LVDS"}, + {DRM_MODE_CONNECTOR_Component, "Component"}, + {DRM_MODE_CONNECTOR_9PinDIN, "DIN"}, + {DRM_MODE_CONNECTOR_DisplayPort, "DP"}, + {DRM_MODE_CONNECTOR_HDMIA, "HDMI-A"}, + {DRM_MODE_CONNECTOR_HDMIB, "HDMI-B"}, + {DRM_MODE_CONNECTOR_TV, "TV"}, + {DRM_MODE_CONNECTOR_eDP, "eDP"}, + {DRM_MODE_CONNECTOR_VIRTUAL, "Virtual"}, + {DRM_MODE_CONNECTOR_DSI, "DSI"}, + {DRM_MODE_CONNECTOR_DPI, "DPI"}, + {DRM_MODE_CONNECTOR_WRITEBACK, "Writeback"}, + {DRM_MODE_CONNECTOR_SPI, "SPI"}, + {DRM_MODE_CONNECTOR_USB, "USB"}}; +} // namespace -NativeWindowDrm::NativeWindowDrm(const char* device_filename) { - drm_device_ = open(device_filename, O_RDWR | O_CLOEXEC); +NativeWindowDrm::NativeWindowDrm(const char* device_filename, + const uint16_t rotation, + bool enable_vsync) { + if (!strcmp("drm-nvdc", device_filename)) { + drm_device_ = drmOpen(device_filename, nullptr); + } else { + drm_device_ = open(device_filename, O_RDWR | O_CLOEXEC); + } if (drm_device_ == -1) { - LINUXES_LOG(ERROR) << "Couldn't open " << device_filename; + ELINUX_LOG(ERROR) << "Couldn't open " << device_filename; return; } - if (!ConfigureDisplay()) { + if (!ConfigureDisplay(rotation)) { return; } + enable_vsync_ = enable_vsync; valid_ = true; } @@ -40,22 +72,22 @@ bool NativeWindowDrm::MoveCursor(double x, double y) { drmModeMoveCursor(drm_device_, drm_crtc_->crtc_id, x - cursor_hotspot_.first, y - cursor_hotspot_.second); if (result < 0) { - LINUXES_LOG(ERROR) << "Couldn't move the mouse cursor: " << result; + ELINUX_LOG(ERROR) << "Couldn't move the mouse cursor: " << result; return false; } return true; } -bool NativeWindowDrm::ConfigureDisplay() { +bool NativeWindowDrm::ConfigureDisplay(const uint16_t rotation) { auto resources = drmModeGetResources(drm_device_); if (!resources) { - LINUXES_LOG(ERROR) << "Couldn't get resources"; + ELINUX_LOG(ERROR) << "Couldn't get resources"; return false; } auto connector = FindConnector(resources); if (!connector) { - LINUXES_LOG(ERROR) << "Couldn't find any connectors"; + ELINUX_LOG(ERROR) << "Couldn't find any connectors"; drmModeFreeResources(resources); return false; } @@ -64,17 +96,30 @@ bool NativeWindowDrm::ConfigureDisplay() { drm_mode_info_ = connector->modes[0]; width_ = drm_mode_info_.hdisplay; height_ = drm_mode_info_.vdisplay; - LINUXES_LOG(INFO) << "resolution: " << width_ << "x" << height_; + if (rotation == 90 || rotation == 270) { + std::swap(width_, height_); + } + ELINUX_LOG(INFO) << "resolution: " << width_ << "x" << height_; auto* encoder = FindEncoder(resources, connector); if (!encoder) { - LINUXES_LOG(ERROR) << "Couldn't find any encoders"; + ELINUX_LOG(ERROR) << "Couldn't find any encoders"; drmModeFreeConnector(connector); drmModeFreeResources(resources); return false; } - if (encoder->crtc_id) { - drm_crtc_ = drmModeGetCrtc(drm_device_, encoder->crtc_id); + if (!encoder->crtc_id) { + // if there is no current CRTC, make sure to attach a suitable one + for (int c = 0; c < resources->count_crtcs; c++) { + if (encoder->possible_crtcs & (1 << c)) { + encoder->crtc_id = resources->crtcs[c]; + break; + } + } + } + drm_crtc_ = drmModeGetCrtc(drm_device_, encoder->crtc_id); + if (!drm_crtc_) { + ELINUX_LOG(WARNING) << "Couldn't find a suitable crtc"; } drmModeFreeEncoder(encoder); @@ -84,11 +129,62 @@ bool NativeWindowDrm::ConfigureDisplay() { return true; } +std::string NativeWindowDrm::GetConnectorName(uint32_t connector_type, + uint32_t connector_type_id) { + auto it = connector_names.find(connector_type); + std::string name = it != connector_names.end() ? it->second : "Unknown"; + return name + "-" + std::to_string(connector_type_id); +} + +drmModeConnectorPtr NativeWindowDrm::GetConnectorByName( + drmModeResPtr resources, + const char* connector_name) { + for (int i = 0; i < resources->count_connectors; i++) { + auto connector = drmModeGetConnector(drm_device_, resources->connectors[i]); + if (!connector) { + continue; + } + auto other_connector_name = GetConnectorName(connector->connector_type, + connector->connector_type_id); + if (connector_name == other_connector_name) { + return connector; + } + drmModeFreeConnector(connector); + } + return nullptr; +} + drmModeConnectorPtr NativeWindowDrm::FindConnector(drmModeResPtr resources) { + auto connector_name = std::getenv(kFlutterDrmConnectorEnvironmentKey); + if (connector_name && connector_name[0] != '\0') { + auto connector = GetConnectorByName(resources, connector_name); + if (!connector) { + ELINUX_LOG(ERROR) << "Couldn't find connector with name " + << connector_name; + return nullptr; + } + + if (connector->connection == DRM_MODE_CONNECTED) { + ELINUX_LOG(DEBUG) << "Using connector " << connector_name; + return connector; + } else { + ELINUX_LOG(ERROR) << "Connector " << connector_name + << " is not connected."; + drmModeFreeConnector(connector); + return nullptr; + } + } + for (int i = 0; i < resources->count_connectors; i++) { auto connector = drmModeGetConnector(drm_device_, resources->connectors[i]); + if (!connector) { + continue; + } // pick the first connected connector if (connector->connection == DRM_MODE_CONNECTED) { + auto other_connetor_name = GetConnectorName(connector->connector_type, + connector->connector_type_id); + ELINUX_LOG(DEBUG) << "Using connector " << other_connetor_name; return connector; } drmModeFreeConnector(connector); @@ -99,11 +195,44 @@ drmModeConnectorPtr NativeWindowDrm::FindConnector(drmModeResPtr resources) { drmModeEncoder* NativeWindowDrm::FindEncoder(drmModeRes* resources, drmModeConnector* connector) { - if (connector->encoder_id) { - return drmModeGetEncoder(drm_device_, connector->encoder_id); + drmModeEncoder* encoder = nullptr; + // Find a suitable encoder + for (int e = 0; e < resources->count_encoders; e++) { + bool found = false; + encoder = drmModeGetEncoder(drm_device_, resources->encoders[e]); + for (int ce = 0; ce < connector->count_encoders; ce++) { + if (encoder && encoder->encoder_id == connector->encoders[ce]) { + ELINUX_LOG(DEBUG) << "Using encoder id " << encoder->encoder_id; + found = true; + break; + } + } + if (found) + break; + drmModeFreeEncoder(encoder); + encoder = nullptr; } - // no encoder found - return nullptr; + + // If encoder is not connected to the connector, + // try to find a suitable one + if (!encoder) { + for (int e = 0; e < connector->count_encoders; e++) { + encoder = drmModeGetEncoder(drm_device_, connector->encoders[e]); + for (int c = 0; c < resources->count_crtcs; c++) { + if (encoder->possible_crtcs & (1 << c)) { + encoder->crtc_id = resources->crtcs[c]; + break; + } + } + if (encoder->crtc_id) + break; + drmModeFreeEncoder(encoder); + encoder = nullptr; + } + } + + // Will return nullptr if a suitable encoder is still not found + return encoder; } const uint32_t* NativeWindowDrm::GetCursorData(const std::string& cursor_name) { @@ -157,8 +286,8 @@ const uint32_t* NativeWindowDrm::GetCursorData(const std::string& cursor_name) { if (!cursor_data) { if (!cursor_name.empty()) { - LINUXES_LOG(WARNING) << "Unsupported cursor: " << cursor_name.c_str() - << ", use LeftPtr cursor."; + ELINUX_LOG(WARNING) << "Unsupported cursor: " << cursor_name.c_str() + << ", use LeftPtr cursor."; } cursor_data = kCursorDataLeftPtr; } diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_drm.h b/src/flutter/shell/platform/linux_embedded/window/native_window_drm.h index ebaf8c9a..3d72f7ac 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_drm.h +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_drm.h @@ -1,4 +1,4 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,32 +7,42 @@ #include +#include +#include #include -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl.h" #include "flutter/shell/platform/linux_embedded/window/native_window.h" namespace flutter { class NativeWindowDrm : public NativeWindow { public: - NativeWindowDrm(const char* device_filename); + NativeWindowDrm(const char* device_filename, + const uint16_t rotation, + bool enable_vsync); virtual ~NativeWindowDrm(); - bool ConfigureDisplay(); + bool ConfigureDisplay(const uint16_t rotation); bool MoveCursor(double x, double y); virtual bool ShowCursor(double x, double y) = 0; - virtual bool UpdateCursor(const std::string& cursor_name, double x, + virtual bool UpdateCursor(const std::string& cursor_name, + double x, double y) = 0; virtual bool DismissCursor() = 0; - virtual std::unique_ptr CreateRenderSurface() = 0; + virtual std::unique_ptr CreateRenderSurface( + bool enable_impeller) = 0; protected: + std::string GetConnectorName(uint32_t connector_type, + uint32_t connector_type_id); + drmModeConnectorPtr GetConnectorByName(drmModeResPtr resources, + const char* connector_name); drmModeConnectorPtr FindConnector(drmModeResPtr resources); drmModeEncoder* FindEncoder(drmModeRes* resources, diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.cc b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.cc index 3eeda0b7..48f8e969 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.cc +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.cc @@ -1,4 +1,4 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -18,12 +18,15 @@ namespace { constexpr char kCursorNameNone[] = "none"; } // namespace -NativeWindowDrmEglstream::NativeWindowDrmEglstream(const char* device_filename) - : NativeWindowDrm(device_filename) { +NativeWindowDrmEglstream::NativeWindowDrmEglstream(const char* device_filename, + const uint16_t rotation, + bool enable_vsync) + : NativeWindowDrm(device_filename, rotation, enable_vsync) { if (!valid_) { return; } + enable_vsync_ = enable_vsync; valid_ = ConfigureDisplayAdditional(); // drmIsMaster() is a relatively new API, and the main target of EGLStream is @@ -55,11 +58,11 @@ NativeWindowDrmEglstream::~NativeWindowDrmEglstream() { bool NativeWindowDrmEglstream::Resize(const size_t width, const size_t height) { if (!valid_) { - LINUXES_LOG(ERROR) << "Failed to resize the window."; + ELINUX_LOG(ERROR) << "Failed to resize the window."; return false; } - LINUXES_LOG(ERROR) << "Not supported now."; + ELINUX_LOG(ERROR) << "Not supported now."; return false; } @@ -70,7 +73,8 @@ bool NativeWindowDrmEglstream::ShowCursor(double x, double y) { } bool NativeWindowDrmEglstream::UpdateCursor(const std::string& cursor_name, - double x, double y) { + double x, + double y) { if (cursor_name.compare(cursor_name_) == 0) { return true; } @@ -89,32 +93,33 @@ bool NativeWindowDrmEglstream::DismissCursor() { return true; } -std::unique_ptr NativeWindowDrmEglstream::CreateRenderSurface() { +std::unique_ptr NativeWindowDrmEglstream::CreateRenderSurface( + bool enable_impeller) { return std::make_unique(std::make_unique( std::make_unique())); } bool NativeWindowDrmEglstream::ConfigureDisplayAdditional() { if (!SetDrmClientCapabilities()) { - LINUXES_LOG(ERROR) << "Couldn't set drm client capability"; + ELINUX_LOG(ERROR) << "Couldn't set drm client capability"; return false; } auto plane_resources = drmModeGetPlaneResources(drm_device_); if (!plane_resources) { - LINUXES_LOG(ERROR) << "Couldn't get plane resources"; + ELINUX_LOG(ERROR) << "Couldn't get plane resources"; return false; } drm_plane_id_ = FindPrimaryPlaneId(plane_resources); drmModeFreePlaneResources(plane_resources); if (drm_plane_id_ == -1) { - LINUXES_LOG(ERROR) << "Couldn't find a plane."; + ELINUX_LOG(ERROR) << "Couldn't find a plane."; return false; } auto atomic = drmModeAtomicAlloc(); if (!atomic) { - LINUXES_LOG(ERROR) << "Couldn't allocate atomic"; + ELINUX_LOG(ERROR) << "Couldn't allocate atomic"; return false; } int result = -1; @@ -124,7 +129,7 @@ bool NativeWindowDrmEglstream::ConfigureDisplayAdditional() { } drmModeAtomicFree(atomic); if (result != 0) { - LINUXES_LOG(ERROR) << "Failed to commit an atomic property change request"; + ELINUX_LOG(ERROR) << "Failed to commit an atomic property change request"; return false; } @@ -133,11 +138,11 @@ bool NativeWindowDrmEglstream::ConfigureDisplayAdditional() { bool NativeWindowDrmEglstream::SetDrmClientCapabilities() { if (drmSetClientCap(drm_device_, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) != 0) { - LINUXES_LOG(ERROR) << "Couldn't set DRM_CLIENT_CAP_UNIVERSAL_PLANES"; + ELINUX_LOG(ERROR) << "Couldn't set DRM_CLIENT_CAP_UNIVERSAL_PLANES"; return false; } if (drmSetClientCap(drm_device_, DRM_CLIENT_CAP_ATOMIC, 1) != 0) { - LINUXES_LOG(ERROR) << "Couldn't set DRM_CLIENT_CAP_ATOMIC"; + ELINUX_LOG(ERROR) << "Couldn't set DRM_CLIENT_CAP_ATOMIC"; return false; } return true; @@ -162,7 +167,8 @@ uint32_t NativeWindowDrmEglstream::FindPrimaryPlaneId( return -1; } -uint64_t NativeWindowDrmEglstream::GetPropertyValue(uint32_t id, uint32_t type, +uint64_t NativeWindowDrmEglstream::GetPropertyValue(uint32_t id, + uint32_t type, const char* prop_name) { uint64_t value = -1; auto properties = drmModeObjectGetProperties(drm_device_, id, type); @@ -187,7 +193,7 @@ bool NativeWindowDrmEglstream::AssignAtomicRequest(drmModeAtomicReqPtr atomic) { if (drmModeCreatePropertyBlob(drm_device_, &drm_mode_info_, sizeof(drm_mode_info_), &drm_property_blob_) != 0) { - LINUXES_LOG(ERROR) << "Failed to create property blob"; + ELINUX_LOG(ERROR) << "Failed to create property blob"; return false; } @@ -233,7 +239,9 @@ bool NativeWindowDrmEglstream::AssignAtomicRequest(drmModeAtomicReqPtr atomic) { template bool NativeWindowDrmEglstream::AssignAtomicPropertyValue( - drmModeAtomicReqPtr atomic, uint32_t id, uint32_t type, + drmModeAtomicReqPtr atomic, + uint32_t id, + uint32_t type, NativeWindowDrmEglstream::DrmProperty (&table)[N]) { auto properties = drmModeObjectGetProperties(drm_device_, id, type); if (properties) { @@ -244,7 +252,7 @@ bool NativeWindowDrmEglstream::AssignAtomicPropertyValue( if (std::strcmp(table[j].name, property->name) == 0) { if (drmModeAtomicAddProperty(atomic, id, property->prop_id, table[j].value) < 0) { - LINUXES_LOG(ERROR) + ELINUX_LOG(ERROR) << "Failed to add " << table[j].name << " property"; drmModeFreeProperty(property); drmModeFreeObjectProperties(properties); diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.h b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.h index cd998fc5..4bbe59bc 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.h +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_eglstream.h @@ -1,4 +1,4 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -16,21 +16,24 @@ namespace flutter { class NativeWindowDrmEglstream : public NativeWindowDrm { public: - NativeWindowDrmEglstream(const char* device_filename); + NativeWindowDrmEglstream(const char* device_filename, + const uint16_t rotation, + bool enable_vsync); ~NativeWindowDrmEglstream(); // |NativeWindowDrm| bool ShowCursor(double x, double y) override; // |NativeWindowDrm| - bool UpdateCursor(const std::string& cursor_name, double x, + bool UpdateCursor(const std::string& cursor_name, + double x, double y) override; // |NativeWindowDrm| bool DismissCursor() override; // |NativeWindowDrm| - std::unique_ptr CreateRenderSurface() override; + std::unique_ptr CreateRenderSurface(bool enable_impeller) override; // |NativeWindow| bool Resize(const size_t width, const size_t height) override; @@ -55,7 +58,9 @@ class NativeWindowDrmEglstream : public NativeWindowDrm { template bool AssignAtomicPropertyValue( - drmModeAtomicReqPtr atomic, uint32_t id, uint32_t type, + drmModeAtomicReqPtr atomic, + uint32_t id, + uint32_t type, NativeWindowDrmEglstream::DrmProperty (&table)[N]); uint32_t drm_plane_id_; diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.cc b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.cc index 43eb7f28..4565fcb8 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.cc +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.cc @@ -1,4 +1,4 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -19,14 +19,17 @@ constexpr uint32_t kCursorBufferWidth = 64; constexpr uint32_t kCursorBufferHeight = 64; } // namespace -NativeWindowDrmGbm::NativeWindowDrmGbm(const char* device_filename) - : NativeWindowDrm(device_filename) { +NativeWindowDrmGbm::NativeWindowDrmGbm(const char* device_filename, + const uint16_t rotation, + bool enable_vsync) + : NativeWindowDrm(device_filename, rotation, enable_vsync) { if (!valid_) { return; } + enable_vsync_ = enable_vsync; if (!drmIsMaster(drm_device_)) { - LINUXES_LOG(ERROR) + ELINUX_LOG(ERROR) << "Couldn't become the DRM master. Please confirm if another display " "backend such as X11 and Wayland is not running."; valid_ = false; @@ -35,7 +38,7 @@ NativeWindowDrmGbm::NativeWindowDrmGbm(const char* device_filename) gbm_device_ = gbm_create_device(drm_device_); if (!gbm_device_) { - LINUXES_LOG(ERROR) << "Couldn't create the GBM device."; + ELINUX_LOG(ERROR) << "Couldn't create the GBM device."; valid_ = false; return; } @@ -86,13 +89,14 @@ bool NativeWindowDrmGbm::ShowCursor(double x, double y) { gbm_bo_get_handle(gbm_cursor_bo_).u32, kCursorBufferWidth, kCursorBufferHeight); if (result != 0) { - LINUXES_LOG(ERROR) << "Failed to set cursor buffer. (" << result << ")"; + ELINUX_LOG(ERROR) << "Failed to set cursor buffer. (" << result << ")"; return false; } return true; } -bool NativeWindowDrmGbm::UpdateCursor(const std::string& cursor_name, double x, +bool NativeWindowDrmGbm::UpdateCursor(const std::string& cursor_name, + double x, double y) { if (cursor_name.compare(cursor_name_) == 0) { return true; @@ -112,7 +116,7 @@ bool NativeWindowDrmGbm::UpdateCursor(const std::string& cursor_name, double x, gbm_bo_get_handle(gbm_cursor_bo_).u32, kCursorBufferWidth, kCursorBufferHeight); if (result != 0) { - LINUXES_LOG(ERROR) << "Failed to set cursor buffer. (" << result << ")"; + ELINUX_LOG(ERROR) << "Failed to set cursor buffer. (" << result << ")"; return false; } return true; @@ -121,15 +125,16 @@ bool NativeWindowDrmGbm::UpdateCursor(const std::string& cursor_name, double x, bool NativeWindowDrmGbm::DismissCursor() { auto result = drmModeSetCursor(drm_device_, drm_crtc_->crtc_id, 0, 0, 0); if (result != 0) { - LINUXES_LOG(ERROR) << "Failed to set cursor buffer. (" << result << ")"; + ELINUX_LOG(ERROR) << "Failed to set cursor buffer. (" << result << ")"; return false; } return true; } -std::unique_ptr NativeWindowDrmGbm::CreateRenderSurface() { +std::unique_ptr NativeWindowDrmGbm::CreateRenderSurface( + bool enable_impeller) { return std::make_unique(std::make_unique( - std::make_unique(gbm_device_))); + std::make_unique(gbm_device_), enable_impeller)); } bool NativeWindowDrmGbm::IsNeedRecreateSurfaceAfterResize() const { @@ -138,7 +143,7 @@ bool NativeWindowDrmGbm::IsNeedRecreateSurfaceAfterResize() const { bool NativeWindowDrmGbm::Resize(const size_t width, const size_t height) { if (!valid_) { - LINUXES_LOG(ERROR) << "Failed to resize the window."; + ELINUX_LOG(ERROR) << "Failed to resize the window."; return false; } @@ -148,7 +153,7 @@ bool NativeWindowDrmGbm::Resize(const size_t width, const size_t height) { return false; } - LINUXES_LOG(INFO) << "resize: " << width << "x" << height; + ELINUX_LOG(INFO) << "resize: " << width << "x" << height; drmModeRmFB(drm_device_, gbm_previous_fb_); gbm_surface_release_buffer(static_cast(window_), gbm_previous_bo_); @@ -171,12 +176,16 @@ void NativeWindowDrmGbm::SwapBuffers() { int result = drmModeAddFB(drm_device_, width, height, 24, 32, stride, handle, &fb); if (result != 0) { - LINUXES_LOG(ERROR) << "Failed to add a framebuffer. (" << result << ")"; - } - result = drmModeSetCrtc(drm_device_, drm_crtc_->crtc_id, fb, 0, 0, - &drm_connector_id_, 1, &drm_mode_info_); - if (result != 0) { - LINUXES_LOG(ERROR) << "Failed to set crct mode. (" << result << ")"; + ELINUX_LOG(ERROR) << "Failed to add a framebuffer. (" << result << ")"; + } + if (!drm_crtc_) { + ELINUX_LOG(ERROR) << "crtc is null, cannot set mode."; + } else { + result = drmModeSetCrtc(drm_device_, drm_crtc_->crtc_id, fb, 0, 0, + &drm_connector_id_, 1, &drm_mode_info_); + if (result != 0) { + ELINUX_LOG(ERROR) << "Failed to set crct mode. (" << result << ")"; + } } if (gbm_previous_bo_) { @@ -193,7 +202,7 @@ bool NativeWindowDrmGbm::CreateGbmSurface() { drm_mode_info_.vdisplay, GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); if (!window_) { - LINUXES_LOG(ERROR) << "Failed to create the gbm surface."; + ELINUX_LOG(ERROR) << "Failed to create the gbm surface."; valid_ = false; return false; } @@ -201,7 +210,7 @@ bool NativeWindowDrmGbm::CreateGbmSurface() { window_offscreen_ = gbm_surface_create(gbm_device_, 1, 1, GBM_FORMAT_ARGB8888, GBM_BO_USE_RENDERING); if (!window_offscreen_) { - LINUXES_LOG(ERROR) << "Failed to create the gbm surface for offscreen."; + ELINUX_LOG(ERROR) << "Failed to create the gbm surface for offscreen."; return false; } @@ -214,7 +223,7 @@ bool NativeWindowDrmGbm::CreateCursorBuffer(const std::string& cursor_name) { kCursorBufferHeight, GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); if (!gbm_cursor_bo_) { - LINUXES_LOG(ERROR) << "Failed to create cursor buffer"; + ELINUX_LOG(ERROR) << "Failed to create cursor buffer"; return false; } } @@ -228,7 +237,7 @@ bool NativeWindowDrmGbm::CreateCursorBuffer(const std::string& cursor_name) { auto result = gbm_bo_write(gbm_cursor_bo_, buf, sizeof(buf)); if (result != 0) { - LINUXES_LOG(ERROR) << "Failed to write cursor data. (" << result << ")"; + ELINUX_LOG(ERROR) << "Failed to write cursor data. (" << result << ")"; return false; } return true; diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.h b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.h index f30113b2..d0a2ca5f 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.h +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_drm_gbm.h @@ -1,4 +1,4 @@ -// Copyright 2021 Sony Corporation. All rights reserved. +// Copyright 2023 Sony Corporation. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -17,21 +17,24 @@ namespace flutter { class NativeWindowDrmGbm : public NativeWindowDrm { public: - NativeWindowDrmGbm(const char* device_filename); + NativeWindowDrmGbm(const char* device_filename, + const uint16_t rotation, + bool enable_vsync); ~NativeWindowDrmGbm(); // |NativeWindowDrm| bool ShowCursor(double x, double y) override; // |NativeWindowDrm| - bool UpdateCursor(const std::string& cursor_name, double x, + bool UpdateCursor(const std::string& cursor_name, + double x, double y) override; // |NativeWindowDrm| bool DismissCursor() override; // |NativeWindowDrm| - std::unique_ptr CreateRenderSurface() override; + std::unique_ptr CreateRenderSurface(bool enable_impeller) override; // |NativeWindow| bool IsNeedRecreateSurfaceAfterResize() const override; diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.cc b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.cc index c61cf2c7..46087281 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.cc +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.cc @@ -9,30 +9,41 @@ namespace flutter { NativeWindowWayland::NativeWindowWayland(wl_compositor* compositor, - const size_t width, - const size_t height) { + const size_t width_px, + const size_t height_px, + bool enable_vsync) { surface_ = wl_compositor_create_surface(compositor); if (!surface_) { - LINUXES_LOG(ERROR) << "Failed to create the compositor surface."; + ELINUX_LOG(ERROR) << "Failed to create the compositor surface."; return; } - window_ = wl_egl_window_create(surface_, width, height); + window_ = wl_egl_window_create(surface_, width_px, height_px); if (!window_) { - LINUXES_LOG(ERROR) << "Failed to create the EGL window."; + ELINUX_LOG(ERROR) << "Failed to create the EGL window."; return; } // The offscreen (resource) surface will not be mapped, but needs to be a // wl_surface because ONLY window EGL surfaces are supported on Wayland. - window_offscreen_ = wl_egl_window_create(surface_, 1, 1); - if (!window_offscreen_) { - LINUXES_LOG(ERROR) << "Failed to create the EGL window for offscreen."; - return; + { + surface_offscreen_ = wl_compositor_create_surface(compositor); + if (!surface_offscreen_) { + ELINUX_LOG(ERROR) + << "Failed to create the compositor surface for off-screen."; + return; + } + + window_offscreen_ = wl_egl_window_create(surface_offscreen_, 1, 1); + if (!window_offscreen_) { + ELINUX_LOG(ERROR) << "Failed to create the EGL window for offscreen."; + return; + } } - width_ = width; - height_ = height; + enable_vsync_ = enable_vsync; + width_ = width_px; + height_ = height_px; valid_ = true; } @@ -51,17 +62,23 @@ NativeWindowWayland::~NativeWindowWayland() { wl_surface_destroy(surface_); surface_ = nullptr; } + + if (surface_offscreen_) { + wl_surface_destroy(surface_offscreen_); + surface_offscreen_ = nullptr; + } } -bool NativeWindowWayland::Resize(const size_t width, const size_t height) { +bool NativeWindowWayland::Resize(const size_t width_px, + const size_t height_px) { if (!valid_) { - LINUXES_LOG(ERROR) << "Failed to resize the window."; + ELINUX_LOG(ERROR) << "Failed to resize the window."; return false; } - wl_egl_window_resize(window_, width, height, 0, 0); + wl_egl_window_resize(window_, width_px, height_px, 0, 0); - width_ = width; - height_ = height; + width_ = width_px; + height_ = height_px; return true; } diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.h b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.h index 5e9aa790..c7b8c275 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.h +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland.h @@ -13,17 +13,22 @@ namespace flutter { class NativeWindowWayland : public NativeWindow { public: - NativeWindowWayland(wl_compositor* compositor, const size_t width, - const size_t height); + // @param[in] width_px Physical width of the window. + // @param[in] height_px Physical height of the window. + NativeWindowWayland(wl_compositor* compositor, + const size_t width_px, + const size_t height_px, + bool enable_vsync); ~NativeWindowWayland(); // |NativeWindow| - bool Resize(const size_t width, const size_t height) override; + bool Resize(const size_t width_px, const size_t height_px) override; wl_surface* Surface() const { return surface_; } private: wl_surface* surface_ = nullptr; + wl_surface* surface_offscreen_ = nullptr; }; } // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.cc b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.cc new file mode 100644 index 00000000..5f813962 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.cc @@ -0,0 +1,91 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.h" + +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +NativeWindowWaylandDecoration::NativeWindowWaylandDecoration( + wl_compositor* compositor, + wl_subcompositor* subcompositor, + wl_surface* parent_surface, + const size_t width_px, + const size_t height_px, + bool enable_vsync) { + surface_ = wl_compositor_create_surface(compositor); + if (!surface_) { + ELINUX_LOG(ERROR) << "Failed to create the compositor surface."; + return; + } + + subsurface_ = + wl_subcompositor_get_subsurface(subcompositor, surface_, parent_surface); + if (!subsurface_) { + ELINUX_LOG(ERROR) << "Failed to get the subsurface."; + return; + } + wl_subsurface_set_desync(subsurface_); + wl_subsurface_set_position(subsurface_, 0, 0); + + window_ = wl_egl_window_create(surface_, 1, 1); + if (!window_) { + ELINUX_LOG(ERROR) << "Failed to create the EGL window."; + return; + } + + enable_vsync_ = enable_vsync; + width_ = width_px; + height_ = height_px; + valid_ = true; +} + +NativeWindowWaylandDecoration::~NativeWindowWaylandDecoration() { + if (window_) { + wl_egl_window_destroy(window_); + window_ = nullptr; + } + + if (surface_) { + wl_surface_destroy(surface_); + surface_ = nullptr; + } +} + +bool NativeWindowWaylandDecoration::Resize(const size_t width_px, + const size_t height_px) { + if (!valid_) { + ELINUX_LOG(ERROR) << "Failed to resize the window."; + return false; + } + + width_ = width_px; + height_ = height_px; + wl_egl_window_resize(window_, width_px, height_px, 0, 0); + return true; +} + +void NativeWindowWaylandDecoration::SetPosition(const int32_t x_dip, + const int32_t y_dip) { + if (!valid_) { + ELINUX_LOG(ERROR) << "Failed to set the position of the window."; + return; + } + + x_ = x_dip; + y_ = y_dip; + wl_subsurface_set_position(subsurface_, x_dip, y_dip); +} + +void NativeWindowWaylandDecoration::SetScaleFactor(float scale_factor) { + if (!valid_) { + ELINUX_LOG(ERROR) << "Failed to set the scale factor of the window."; + return; + } + + wl_surface_set_buffer_scale(surface_, scale_factor); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.h b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.h new file mode 100644 index 00000000..dc84f3f4 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.h @@ -0,0 +1,46 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_NATIVE_WINDOW_WAYLAND_DECORATION_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_NATIVE_WINDOW_WAYLAND_DECORATION_H_ + +#include +#include + +#include "flutter/shell/platform/linux_embedded/window/native_window.h" + +namespace flutter { + +class NativeWindowWaylandDecoration : public NativeWindow { + public: + // @param[in] width_px Physical width of the window. + // @param[in] height_px Physical height of the window. + NativeWindowWaylandDecoration(wl_compositor* compositor, + wl_subcompositor* subcompositor, + wl_surface* parent_surface, + const size_t width_px, + const size_t height_px, + bool enable_vsync); + ~NativeWindowWaylandDecoration(); + + // |NativeWindow| + bool Resize(const size_t width_px, const size_t height_px) override; + + // |NativeWindow| + void SetPosition(const int32_t x_dip, const int32_t y_dip) override; + + // Sets the scale factor for the next commit. Scale factor persists until a + // new one is set. + void SetScaleFactor(float scale_factor); + + wl_surface* Surface() const { return surface_; } + + private: + wl_surface* surface_ = nullptr; + wl_subsurface* subsurface_ = nullptr; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_NATIVE_WINDOW_WAYLAND_DECORATION_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_x11.cc b/src/flutter/shell/platform/linux_embedded/window/native_window_x11.cc index 883626c3..e6da1ea1 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_x11.cc +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_x11.cc @@ -16,11 +16,15 @@ namespace flutter { namespace { static constexpr char kWmDeleteWindow[] = "WM_DELETE_WINDOW"; -static constexpr char kWindowTitle[] = "Flutter for Embedded Linux"; } // namespace -NativeWindowX11::NativeWindowX11(Display* display, VisualID visual_id, - const size_t width, const size_t height) { +NativeWindowX11::NativeWindowX11(Display* display, + VisualID visual_id, + const char* title, + const size_t width, + const size_t height, + bool enable_vsync, + bool fullscreen) { XVisualInfo visualTemplate; visualTemplate.visualid = visual_id; @@ -28,7 +32,7 @@ NativeWindowX11::NativeWindowX11(Display* display, VisualID visual_id, auto* visual = XGetVisualInfo(display, VisualIDMask, &visualTemplate, &visualsCount); if (!visual) { - LINUXES_LOG(ERROR) << "Failed to get Visual info."; + ELINUX_LOG(ERROR) << "Failed to get Visual info."; return; } @@ -41,12 +45,22 @@ NativeWindowX11::NativeWindowX11(Display* display, VisualID visual_id, ButtonReleaseMask | PointerMotionMask | EnterWindowMask | LeaveWindowMask | FocusChangeMask | StructureNotifyMask; - window_ = - XCreateWindow(display, RootWindow(display, visual->screen), 0, 0, width, - height, 0, visual->depth, InputOutput, visual->visual, - CWBorderPixel | CWColormap | CWEventMask, &windowAttribs); + auto window_width = width; + auto window_height = height; + if (fullscreen) { + XWindowAttributes attr; + XGetWindowAttributes(display, RootWindow(display, visual->screen), &attr); + window_width = attr.width; + window_height = attr.height; + windowAttribs.override_redirect = True; + } + + window_ = XCreateWindow( + display, RootWindow(display, visual->screen), 0, 0, window_width, + window_height, 0, visual->depth, InputOutput, visual->visual, + CWBorderPixel | CWColormap | CWEventMask, &windowAttribs); if (!window_) { - LINUXES_LOG(ERROR) << "Failed to the create window."; + ELINUX_LOG(ERROR) << "Failed to the create window."; return; } @@ -57,16 +71,16 @@ NativeWindowX11::NativeWindowX11(Display* display, VisualID visual_id, // Set the window title. { XTextProperty property; - property.value = - reinterpret_cast(const_cast(kWindowTitle)); + property.value = reinterpret_cast(const_cast(title)); property.encoding = XA_STRING; property.format = 8; - property.nitems = std::strlen(kWindowTitle); + property.nitems = std::strlen(title); XSetWMName(display, window_, &property); } XMapWindow(display, window_); + enable_vsync_ = enable_vsync; width_ = width; height_ = height; valid_ = true; diff --git a/src/flutter/shell/platform/linux_embedded/window/native_window_x11.h b/src/flutter/shell/platform/linux_embedded/window/native_window_x11.h index 4a77c2c3..9cb9f786 100644 --- a/src/flutter/shell/platform/linux_embedded/window/native_window_x11.h +++ b/src/flutter/shell/platform/linux_embedded/window/native_window_x11.h @@ -13,8 +13,13 @@ namespace flutter { class NativeWindowX11 : public NativeWindow { public: - NativeWindowX11(Display* display, VisualID visual_id, const size_t width, - const size_t height); + NativeWindowX11(Display* display, + VisualID visual_id, + const char* title, + const size_t width, + const size_t height, + bool enable_vsync, + bool fullscreen); ~NativeWindowX11() = default; // |NativeWindow| diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.cc b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.cc new file mode 100644 index 00000000..86f3b57d --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.cc @@ -0,0 +1,70 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifdef USE_GLES3 +#include +#else +#include +#include +#endif +#include + +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.h" + +namespace flutter { + +namespace { + +struct GlProcs { + PFNGLUSEPROGRAMPROC glUseProgram; + bool valid; +}; + +static const GlProcs& GlProcs() { + static struct GlProcs procs = {}; + static bool initialized = false; + if (!initialized) { + procs.glUseProgram = reinterpret_cast( + eglGetProcAddress("glUseProgram")); + procs.valid = procs.glUseProgram; + if (!procs.valid) { + ELINUX_LOG(ERROR) << "Failed to load GlProcs"; + } + initialized = true; + } + return procs; +} + +} // namespace + +void ELinuxShader::LoadProgram(std::string vertex_code, + std::string fragment_code) { + auto vertex = + std::make_unique(vertex_code, GL_VERTEX_SHADER); + auto fragment = + std::make_unique(fragment_code, GL_FRAGMENT_SHADER); + program_ = std::make_unique(std::move(vertex), + std::move(fragment)); +} + +void ELinuxShader::Bind() { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + gl.glUseProgram(program_->Program()); +} + +void ELinuxShader::Unbind() { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + gl.glUseProgram(false); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.h b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.h new file mode 100644 index 00000000..ecb64b4a --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.h @@ -0,0 +1,41 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_ELINUX_SHADER_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_ELINUX_SHADER_H_ + +#ifdef USE_GLES3 +#include +#else +#include +#endif + +#include +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.h" + +namespace flutter { + +class ELinuxShader { + public: + ELinuxShader() = default; + ~ELinuxShader() = default; + + void LoadProgram(std::string vertex_code, std::string fragment_code); + void Bind(); + void Unbind(); + GLuint Program() const { return program_->Program(); } + + private: + std::unique_ptr program_; + std::unique_ptr vertex_; + std::unique_ptr fragment_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_ELINUX_SHADER_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.cc b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.cc new file mode 100644 index 00000000..c097b284 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.cc @@ -0,0 +1,99 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifdef USE_GLES3 +#include +#else +#include +#include +#endif +#include + +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.h" + +namespace flutter { + +namespace { + +struct GlProcs { + PFNGLCREATESHADERPROC glCreateShader; + PFNGLSHADERSOURCEPROC glShaderSource; + PFNGLCOMPILESHADERPROC glCompileShader; + PFNGLGETSHADERIVPROC glGetShaderiv; + PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog; + PFNGLDELETESHADERPROC glDeleteShader; + bool valid; +}; + +static const GlProcs& GlProcs() { + static struct GlProcs procs = {}; + static bool initialized = false; + if (!initialized) { + procs.glCreateShader = reinterpret_cast( + eglGetProcAddress("glCreateShader")); + procs.glShaderSource = reinterpret_cast( + eglGetProcAddress("glShaderSource")); + procs.glCompileShader = reinterpret_cast( + eglGetProcAddress("glCompileShader")); + procs.glGetShaderiv = reinterpret_cast( + eglGetProcAddress("glGetShaderiv")); + procs.glGetShaderInfoLog = reinterpret_cast( + eglGetProcAddress("glGetShaderInfoLog")); + procs.glDeleteShader = reinterpret_cast( + eglGetProcAddress("glDeleteShader")); + procs.valid = procs.glCreateShader && procs.glShaderSource && + procs.glCompileShader && procs.glGetShaderiv && + procs.glGetShaderInfoLog && procs.glDeleteShader; + if (!procs.valid) { + ELINUX_LOG(ERROR) << "Failed to load GlProcs"; + } + initialized = true; + } + return procs; +} + +} // namespace + +ELinuxShaderContext::ELinuxShaderContext(std::string code, GLenum type) + : shader_(0) { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + + shader_ = gl.glCreateShader(type); + if (!shader_) { + ELINUX_LOG(ERROR) << "Failed to create the shader"; + return; + } + + auto* c_code = code.c_str(); + gl.glShaderSource(shader_, 1, &c_code, nullptr); + gl.glCompileShader(shader_); + + GLint success; + gl.glGetShaderiv(shader_, GL_COMPILE_STATUS, &success); + if (!success) { + GLchar buf[1024]; + + gl.glGetShaderInfoLog(shader_, sizeof(buf), nullptr, buf); + ELINUX_LOG(ERROR) << "Couldn't compile the shader: " << buf; + + gl.glDeleteShader(shader_); + shader_ = 0; + } +} + +ELinuxShaderContext::~ELinuxShaderContext() { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + gl.glDeleteShader(shader_); +} + +}; // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.h b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.h new file mode 100644 index 00000000..463ead2a --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.h @@ -0,0 +1,33 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_ELINUX_SHADER_CONTEXT_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_ELINUX_SHADER_CONTEXT_H_ + +#ifdef USE_GLES3 +#include +#else +#include +#endif + +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" + +namespace flutter { + +class ELinuxShaderContext { + public: + ELinuxShaderContext(std::string code, GLenum type); + ~ELinuxShaderContext(); + + GLuint Shader() const { return shader_; } + + private: + GLuint shader_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_ELINUX_SHADER_CONTEXT_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.cc b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.cc new file mode 100644 index 00000000..07eb9a45 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.cc @@ -0,0 +1,103 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifdef USE_GLES3 +#include +#else +#include +#include +#endif +#include + +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.h" + +namespace flutter { + +namespace { + +struct GlProcs { + PFNGLCREATEPROGRAMPROC glCreateProgram; + PFNGLATTACHSHADERPROC glAttachShader; + PFNGLLINKPROGRAMPROC glLinkProgram; + PFNGLGETPROGRAMIVPROC glGetProgramiv; + PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog; + PFNGLDELETEPROGRAMPROC glDeleteProgram; + bool valid; +}; + +static const GlProcs& GlProcs() { + static struct GlProcs procs = {}; + static bool initialized = false; + if (!initialized) { + procs.glCreateProgram = reinterpret_cast( + eglGetProcAddress("glCreateProgram")); + procs.glAttachShader = reinterpret_cast( + eglGetProcAddress("glAttachShader")); + procs.glLinkProgram = reinterpret_cast( + eglGetProcAddress("glLinkProgram")); + procs.glGetProgramiv = reinterpret_cast( + eglGetProcAddress("glGetProgramiv")); + procs.glGetProgramInfoLog = reinterpret_cast( + eglGetProcAddress("glGetProgramInfoLog")); + procs.glDeleteProgram = reinterpret_cast( + eglGetProcAddress("glDeleteProgram")); + procs.valid = procs.glCreateProgram && procs.glAttachShader && + procs.glLinkProgram && procs.glGetProgramiv && + procs.glGetProgramInfoLog && procs.glDeleteProgram; + if (!procs.valid) { + ELINUX_LOG(ERROR) << "Failed to load GlProcs"; + } + initialized = true; + } + return procs; +} + +} // namespace + +ELinuxShaderProgram::ELinuxShaderProgram( + std::unique_ptr vertex_shader, + std::unique_ptr fragment_shader) + : vertex_shader_(std::move(vertex_shader)), + fragment_shader_(std::move(fragment_shader)), + program_(0) { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + + program_ = gl.glCreateProgram(); + if (!program_) { + ELINUX_LOG(ERROR) << "Failed to create a shader program"; + return; + } + + gl.glAttachShader(program_, vertex_shader_->Shader()); + gl.glAttachShader(program_, fragment_shader_->Shader()); + gl.glLinkProgram(program_); + + GLint success; + gl.glGetProgramiv(program_, GL_LINK_STATUS, &success); + if (success != GL_TRUE) { + GLchar buf[1024]; + + gl.glGetProgramInfoLog(program_, sizeof(buf), nullptr, buf); + ELINUX_LOG(ERROR) << "Couldn't link the program: " << buf; + + gl.glDeleteProgram(program_); + program_ = 0; + } +} + +ELinuxShaderProgram::~ELinuxShaderProgram() { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + gl.glDeleteProgram(program_); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.h b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.h new file mode 100644 index 00000000..6c93cea6 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_program.h @@ -0,0 +1,37 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_SHADER_PROGRAM_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_SHADER_PROGRAM_H_ + +#ifdef USE_GLES3 +#include +#else +#include +#endif + +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/elinux_shader_context.h" + +namespace flutter { + +class ELinuxShaderProgram { + public: + ELinuxShaderProgram(std::unique_ptr vertex_shader, + std::unique_ptr fragment_shader); + ~ELinuxShaderProgram(); + + GLuint Program() const { return program_; } + + private: + GLuint program_; + std::unique_ptr vertex_shader_; + std::unique_ptr fragment_shader_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_SHADER_PROGRAM_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration.h b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration.h new file mode 100644 index 00000000..9b36eebf --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration.h @@ -0,0 +1,57 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_H_ + +#include + +#include + +#include "flutter/shell/platform/linux_embedded/surface/surface_decoration.h" +#include "flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.h" + +namespace flutter { + +class WindowDecoration { + public: + enum DecorationType { + CLOSE_BUTTON = 0, + MAXIMISE_BUTTON, + MINIMISE_BUTTON, + TITLE_BAR, + }; + + WindowDecoration() = default; + virtual ~WindowDecoration() = default; + + virtual void Draw() = 0; + + // @param[in] x_dip The x coordinate in logical pixels. + // @param[in] y_dip The y coordinate in logical pixels. + virtual void SetPosition(const int32_t x_dip, const int32_t y_dip) = 0; + + // @param[in] width_px Physical width of the window. + // @param[in] height_px Physical height of the window. + virtual void Resize(const size_t width_px, const size_t height_px) = 0; + + // Sets the scale factor for the next commit. Scale factor persists until a + // new one is set. + virtual void SetScaleFactor(float scale_factor) = 0; + + void DestroyContext() const { render_surface_->DestroyContext(); }; + + wl_surface* Surface() const { return native_window_->Surface(); }; + + DecorationType Type() const { return decoration_type_; }; + + protected: + std::unique_ptr native_window_; + std::unique_ptr render_surface_; + DecorationType decoration_type_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.cc b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.cc new file mode 100644 index 00000000..a13b8954 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.cc @@ -0,0 +1,234 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.h" + +#ifdef USE_GLES3 +#include +#else +#include +#include +#endif +#include + +namespace flutter { + +namespace { +enum { ATTRIB_VERTEC = 0, ATTRIB_COLOR, NUM_ATTRIBUTES }; + +struct GlProcs { + PFNGLENABLEPROC glEnable; + PFNGLCLEARCOLORPROC glClearColor; + PFNGLCLEARPROC glClear; + PFNGLVIEWPORTPROC glViewport; + PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray; + PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer; + PFNGLDRAWARRAYSPROC glDrawArrays; + PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray; + PFNGLLINEWIDTHPROC glLineWidth; + PFNGLBINDATTRIBLOCATIONPROC glBindAttribLocation; + PFNGLUSEPROGRAMPROC glUseProgram; + bool valid; +}; + +static const GlProcs& GlProcs() { + static struct GlProcs procs = {}; + static bool initialized = false; + if (!initialized) { + procs.glEnable = + reinterpret_cast(eglGetProcAddress("glEnable")); + procs.glClearColor = reinterpret_cast( + eglGetProcAddress("glClearColor")); + procs.glClear = + reinterpret_cast(eglGetProcAddress("glClear")); + procs.glViewport = + reinterpret_cast(eglGetProcAddress("glViewport")); + procs.glEnableVertexAttribArray = + reinterpret_cast( + eglGetProcAddress("glEnableVertexAttribArray")); + procs.glLineWidth = + reinterpret_cast(eglGetProcAddress("glLineWidth")); + procs.glVertexAttribPointer = + reinterpret_cast( + eglGetProcAddress("glVertexAttribPointer")); + procs.glDrawArrays = reinterpret_cast( + eglGetProcAddress("glDrawArrays")); + procs.glDisableVertexAttribArray = + reinterpret_cast( + eglGetProcAddress("glDisableVertexAttribArray")); + procs.glBindAttribLocation = reinterpret_cast( + eglGetProcAddress("glBindAttribLocation")); + procs.glUseProgram = reinterpret_cast( + eglGetProcAddress("glUseProgram")); + procs.valid = procs.glEnable && procs.glClearColor && procs.glClear && + procs.glEnableVertexAttribArray && procs.glLineWidth && + procs.glVertexAttribPointer && procs.glDrawArrays && + procs.glDisableVertexAttribArray && procs.glViewport && + procs.glBindAttribLocation && procs.glUseProgram; + if (!procs.valid) { + ELINUX_LOG(ERROR) << "Failed to load GlProcs"; + } + initialized = true; + } + return procs; +} + +constexpr char kGlVertexPositionVar[] = "Position"; +constexpr char kGlFragmentColorVar[] = "SourceColor"; +constexpr char kGlVertexShader[] = + "attribute vec4 Position; \n" + "attribute vec4 SourceColor; \n" + "varying vec4 DestinationColor; \n" + "void main() { \n" + " gl_Position = Position; \n" + " DestinationColor = SourceColor; \n" + "} \n"; +constexpr char kGlFragmentShader[] = + "varying lowp vec4 DestinationColor; \n" + "void main() { \n" + " gl_FragColor = DestinationColor; \n" + "} \n"; +} // namespace + +WindowDecorationButton::WindowDecorationButton( + DecorationType decoration_type, + std::unique_ptr native_window, + std::unique_ptr render_surface) + : shader_(nullptr) { + decoration_type_ = decoration_type; + native_window_ = std::move(native_window); + render_surface_ = std::move(render_surface); + render_surface_->SetNativeWindow(native_window_.get()); + render_surface_->Resize(native_window_->Width(), native_window_->Height()); +} + +WindowDecorationButton::~WindowDecorationButton() { + render_surface_ = nullptr; + native_window_ = nullptr; + shader_ = nullptr; +} + +void WindowDecorationButton::Draw() { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + + render_surface_->GLContextMakeCurrent(); + { + gl.glClearColor(100 / 255.0f, 100 / 255.0f, 100 / 255.0f, 1.0f); + gl.glClear(GL_COLOR_BUFFER_BIT); + gl.glViewport(0, 0, native_window_->Width(), native_window_->Height()); + { + if (!shader_) { + LoadShader(); + } + shader_->Bind(); + gl.glEnableVertexAttribArray(ATTRIB_VERTEC); + gl.glEnableVertexAttribArray(ATTRIB_COLOR); + gl.glLineWidth(2); + if (decoration_type_ == DecorationType::CLOSE_BUTTON) { + constexpr GLfloat vertices[] = { + // clang-format off + -0.5f, -0.5f, + 0.5f, 0.5f, + 0.5f, -0.5f, + -0.5f, 0.5f, + // clang-format on + }; + constexpr GLfloat colors[] = { + // clang-format off + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + // clang-format on + }; + + gl.glVertexAttribPointer(ATTRIB_VERTEC, 2, GL_FLOAT, GL_FALSE, 0, + vertices); + gl.glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, + colors); + gl.glDrawArrays(GL_LINES, 0, 4); + } else if (decoration_type_ == DecorationType::MAXIMISE_BUTTON) { + constexpr GLfloat vertices[] = { + // clang-format off + -0.5f, -0.5f, + 0.5f, -0.5f, + 0.5f, 0.5f, + -0.5f, 0.5f, + // clang-format on + }; + constexpr GLfloat colors[] = { + // clang-format off + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + // clang-format on + }; + + gl.glVertexAttribPointer(ATTRIB_VERTEC, 2, GL_FLOAT, GL_FALSE, 0, + vertices); + gl.glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, + colors); + gl.glDrawArrays(GL_LINE_LOOP, 0, 4); + } else { + constexpr GLfloat vertices[] = { + // clang-format off + -0.5f, -0.4f, + 0.5f, -0.4f, + // clang-format on + }; + constexpr GLfloat colors[] = { + // clang-format off + 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, + // clang-format on + }; + + gl.glVertexAttribPointer(ATTRIB_VERTEC, 2, GL_FLOAT, GL_FALSE, 0, + vertices); + gl.glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, + colors); + gl.glDrawArrays(GL_LINES, 0, 2); + } + gl.glDisableVertexAttribArray(ATTRIB_VERTEC); + gl.glDisableVertexAttribArray(ATTRIB_COLOR); + shader_->Unbind(); + } + } + render_surface_->GLContextPresent(0); +} + +void WindowDecorationButton::SetPosition(const int32_t x_dip, + const int32_t y_dip) { + native_window_->SetPosition(x_dip, y_dip); +} + +void WindowDecorationButton::Resize(const size_t width_px, + const size_t height_px) { + render_surface_->Resize(width_px, height_px); +} + +void WindowDecorationButton::SetScaleFactor(float scale_factor) { + native_window_->SetScaleFactor(scale_factor); +} + +void WindowDecorationButton::LoadShader() { + if (shader_) { + return; + } + + shader_ = std::make_unique(); + shader_->LoadProgram(kGlVertexShader, kGlFragmentShader); + + const auto& gl = GlProcs(); + gl.glBindAttribLocation(shader_->Program(), ATTRIB_VERTEC, + kGlVertexPositionVar); + gl.glBindAttribLocation(shader_->Program(), ATTRIB_COLOR, + kGlFragmentColorVar); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.h b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.h new file mode 100644 index 00000000..abd4e04a --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.h @@ -0,0 +1,40 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_BUTTON_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_BUTTON_H_ + +#include "flutter/shell/platform/linux_embedded/window/renderer/elinux_shader.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decoration.h" + +namespace flutter { + +class WindowDecorationButton : public WindowDecoration { + public: + WindowDecorationButton( + DecorationType decoration_type, + std::unique_ptr native_window, + std::unique_ptr render_surface); + ~WindowDecorationButton(); + + void Draw() override; + + // |WindowDecoration| + void SetPosition(const int32_t x_dip, const int32_t y_dip) override; + + // |WindowDecoration| + void Resize(const size_t width_px, const size_t height_px) override; + + // |WindowDecoration| + void SetScaleFactor(float scale_factor) override; + + private: + void LoadShader(); + + std::unique_ptr shader_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_BUTTON_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.cc b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.cc new file mode 100644 index 00000000..fd9f2da3 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.cc @@ -0,0 +1,86 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.h" + +#ifdef USE_GLES3 +#include +#else +#include +#include +#endif +#include + +namespace flutter { + +namespace { +struct GlProcs { + PFNGLCLEARCOLORPROC glClearColor; + PFNGLCLEARPROC glClear; + bool valid; +}; + +static const GlProcs& GlProcs() { + static struct GlProcs procs = {}; + static bool initialized = false; + if (!initialized) { + procs.glClearColor = reinterpret_cast( + eglGetProcAddress("glClearColor")); + procs.glClear = + reinterpret_cast(eglGetProcAddress("glClear")); + procs.valid = procs.glClearColor && procs.glClear; + if (!procs.valid) { + ELINUX_LOG(ERROR) << "Failed to load GlProcs"; + } + initialized = true; + } + return procs; +} + +} // namespace + +WindowDecorationTitlebar::WindowDecorationTitlebar( + std::unique_ptr native_window, + std::unique_ptr render_surface) { + decoration_type_ = DecorationType::TITLE_BAR; + native_window_ = std::move(native_window); + render_surface_ = std::move(render_surface); + render_surface_->SetNativeWindow(native_window_.get()); + render_surface_->Resize(native_window_->Width(), native_window_->Height()); +} + +WindowDecorationTitlebar::~WindowDecorationTitlebar() { + render_surface_ = nullptr; + native_window_ = nullptr; +} + +void WindowDecorationTitlebar::Draw() { + const auto& gl = GlProcs(); + if (!gl.valid) { + return; + } + + render_surface_->GLContextMakeCurrent(); + { + gl.glClearColor(51 / 255.0f, 51 / 255.0f, 51 / 255.0f, 1.0f); + gl.glClear(GL_COLOR_BUFFER_BIT); + } + render_surface_->GLContextPresent(0); +} + +void WindowDecorationTitlebar::SetPosition(const int32_t x_dip, + const int32_t y_dip) { + native_window_->SetPosition(x_dip, y_dip); +} + +void WindowDecorationTitlebar::Resize(const size_t width_px, + const size_t height_px) { + render_surface_->Resize(width_px, height_px); +} + +void WindowDecorationTitlebar::SetScaleFactor(float scale_factor) { + native_window_->SetScaleFactor(scale_factor); +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.h b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.h new file mode 100644 index 00000000..a8ce9093 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.h @@ -0,0 +1,33 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_TITLEBAR_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_TITLEBAR_H_ + +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decoration.h" + +namespace flutter { + +class WindowDecorationTitlebar : public WindowDecoration { + public: + WindowDecorationTitlebar( + std::unique_ptr native_window, + std::unique_ptr render_surface); + ~WindowDecorationTitlebar(); + + void Draw() override; + + // |WindowDecoration| + void SetPosition(const int32_t x_dip, const int32_t y_dip) override; + + // |WindowDecoration| + void Resize(const size_t width_px, const size_t height_px) override; + + // |WindowDecoration| + void SetScaleFactor(float scale_factor) override; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATION_TITLEBAR_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.cc b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.cc new file mode 100644 index 00000000..b6c63c3b --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.cc @@ -0,0 +1,143 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.h" + +#include "flutter/shell/platform/linux_embedded/surface/environment_egl.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl.h" + +namespace flutter { + +namespace { +constexpr uint kTitleBarHeightDIP = 30; + +constexpr uint kButtonWidthDIP = 15; +constexpr uint kButtonHeightDIP = 15; +constexpr uint kButtonMarginDIP = 10; +} // namespace + +WindowDecorationsWayland::WindowDecorationsWayland( + wl_display* display, + wl_compositor* compositor, + wl_subcompositor* subcompositor, + wl_surface* root_surface, + int32_t width_dip, + int32_t height_dip, + double pixel_ratio, + bool enable_impeller, + bool enable_vsync) { + constexpr bool sub_egl_display = true; + + // title-bar. + titlebar_ = std::make_unique( + std::make_unique( + compositor, subcompositor, root_surface, width_dip * pixel_ratio, + kTitleBarHeightDIP * pixel_ratio, enable_vsync), + std::make_unique(std::make_unique( + std::make_unique(display, sub_egl_display), + enable_impeller))); + titlebar_->SetPosition(0, -kTitleBarHeightDIP); + + // close button. + auto type = WindowDecorationButton::DecorationType::CLOSE_BUTTON; + buttons_.push_back(std::make_unique( + type, + std::make_unique( + compositor, subcompositor, root_surface, + kButtonWidthDIP * pixel_ratio, kButtonHeightDIP * pixel_ratio, + enable_vsync), + std::make_unique(std::make_unique( + std::make_unique(display, sub_egl_display), + enable_impeller)))); + buttons_[type]->SetPosition( + width_dip * pixel_ratio - kButtonWidthDIP - kButtonMarginDIP, + -(kButtonHeightDIP + (kTitleBarHeightDIP - kButtonHeightDIP) / 2)); + + // maximise button. + type = WindowDecorationButton::DecorationType::MAXIMISE_BUTTON; + buttons_.push_back(std::make_unique( + type, + std::make_unique( + compositor, subcompositor, root_surface, + kButtonWidthDIP * pixel_ratio, kButtonHeightDIP * pixel_ratio, + enable_vsync), + std::make_unique(std::make_unique( + std::make_unique(display, sub_egl_display), + enable_impeller)))); + buttons_[type]->SetPosition( + width_dip * pixel_ratio - kButtonWidthDIP * 2 - kButtonMarginDIP * 2, + -(kButtonHeightDIP + (kTitleBarHeightDIP - kButtonHeightDIP) / 2)); + + // minimise button. + type = WindowDecorationButton::DecorationType::MINIMISE_BUTTON; + buttons_.push_back(std::make_unique( + type, + std::make_unique( + compositor, subcompositor, root_surface, + kButtonWidthDIP * pixel_ratio, kButtonHeightDIP * pixel_ratio, + enable_vsync), + std::make_unique(std::make_unique( + std::make_unique(display, sub_egl_display), + enable_impeller)))); + buttons_[type]->SetPosition( + width_dip * pixel_ratio - kButtonWidthDIP * 3 - kButtonMarginDIP * 3, + -(kButtonHeightDIP + (kTitleBarHeightDIP - kButtonHeightDIP) / 2)); +} + +WindowDecorationsWayland::~WindowDecorationsWayland() { + DestroyContext(); + titlebar_ = nullptr; + for (auto& b : buttons_) { + buttons_.pop_back(); + } +} + +void WindowDecorationsWayland::Draw() { + titlebar_->Draw(); + for (auto& b : buttons_) { + b->Draw(); + } +} + +void WindowDecorationsWayland::Resize(const int32_t width_dip, + const int32_t height_dip, + double pixel_ratio) { + titlebar_->SetScaleFactor(pixel_ratio); + titlebar_->SetPosition(0, -kTitleBarHeightDIP); + titlebar_->Resize(width_dip * pixel_ratio, kTitleBarHeightDIP * pixel_ratio); + + for (auto i = 0; i < buttons_.size(); i++) { + buttons_[i]->SetScaleFactor(pixel_ratio); + buttons_[i]->SetPosition( + width_dip * pixel_ratio - kButtonWidthDIP * (i + 1) - + kButtonMarginDIP * (i + 1), + -(kButtonHeightDIP + (kTitleBarHeightDIP - kButtonHeightDIP) / 2)); + buttons_[i]->Resize(kButtonWidthDIP * pixel_ratio, + kButtonHeightDIP * pixel_ratio); + } +} + +bool WindowDecorationsWayland::IsMatched( + wl_surface* surface, + WindowDecoration::DecorationType decoration_type) const { + switch (decoration_type) { + case WindowDecoration::DecorationType::TITLE_BAR: + return titlebar_->Surface() == surface; + default: + return buttons_[decoration_type]->Surface() == surface; + } +} + +void WindowDecorationsWayland::DestroyContext() { + titlebar_->DestroyContext(); + for (auto& b : buttons_) { + b->DestroyContext(); + } +} + +int32_t WindowDecorationsWayland::Height() const { + return kTitleBarHeightDIP; +} + +} // namespace flutter diff --git a/src/flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.h b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.h new file mode 100644 index 00000000..910bf009 --- /dev/null +++ b/src/flutter/shell/platform/linux_embedded/window/renderer/window_decorations_wayland.h @@ -0,0 +1,67 @@ +// Copyright 2021 Sony Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATIONS_WAYLAND_H_ +#define FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATIONS_WAYLAND_H_ + +#ifdef USE_GLES3 +#include +#else +#include +#endif + +#include + +#include +#include + +#include "flutter/shell/platform/linux_embedded/logger.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_decoration.h" +#include "flutter/shell/platform/linux_embedded/window/native_window_wayland_decoration.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decoration_button.h" +#include "flutter/shell/platform/linux_embedded/window/renderer/window_decoration_titlebar.h" + +namespace flutter { + +class WindowDecorationsWayland { + public: + // @param[in] width_dip Logical width of the window (i.e. surface width). + // @param[in] height_dip Logical height of the window (i.e. surface height). + // @param[in] pixel_ratio Physical / logical pixels ratio. + WindowDecorationsWayland(wl_display* display, + wl_compositor* compositor, + wl_subcompositor* subcompositor, + wl_surface* root_surface, + int32_t width_dip, + int32_t height_dip, + double pixel_ratio, + bool enable_impeller, + bool enable_vsync); + ~WindowDecorationsWayland(); + + void Draw(); + + // @param[in] width_dip Logical width of the window (i.e. surface width). + // @param[in] height_dip Logical height of the window (i.e. surface height). + // @param[in] pixel_ratio Physical / logical pixels ratio. + void Resize(const int32_t width_dip, + const int32_t height_dip, + double pixel_ratio); + + bool IsMatched(wl_surface* surface, + WindowDecoration::DecorationType decoration_type) const; + + // Get height in logical pixels. + int32_t Height() const; + + private: + void DestroyContext(); + + std::unique_ptr titlebar_; + std::vector> buttons_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_LINUX_EMBEDDED_WINDOW_RENDERER_WINDOW_DECORATIONS_WAYLAND_H_ diff --git a/src/flutter/shell/platform/linux_embedded/window_binding_handler.h b/src/flutter/shell/platform/linux_embedded/window_binding_handler.h index 9d3dfd87..34499df8 100644 --- a/src/flutter/shell/platform/linux_embedded/window_binding_handler.h +++ b/src/flutter/shell/platform/linux_embedded/window_binding_handler.h @@ -8,8 +8,8 @@ #include #include -#include "flutter/shell/platform/linux_embedded/public/flutter_linuxes.h" -#include "flutter/shell/platform/linux_embedded/surface/linuxes_surface_gl.h" +#include "flutter/shell/platform/linux_embedded/public/flutter_elinux.h" +#include "flutter/shell/platform/linux_embedded/surface/surface_gl.h" #include "flutter/shell/platform/linux_embedded/window_binding_handler_delegate.h" namespace flutter { @@ -22,7 +22,7 @@ struct PhysicalWindowBounds { size_t height; }; -using LinuxesRenderSurfaceTarget = SurfaceGl; +using ELinuxRenderSurfaceTarget = SurfaceGl; // Abstract class for binding Linux embedded platform windows to Flutter views. class WindowBindingHandler { @@ -34,25 +34,36 @@ class WindowBindingHandler { virtual bool DispatchEvent() = 0; // Create a surface. - virtual bool CreateRenderSurface(int32_t width, int32_t height) = 0; + // @param[in] width_px Physical width of the surface. + // @param[in] height_px Physical height of the surface. + // @param[in] enable_impeller Enable impeller. + virtual bool CreateRenderSurface(int32_t width_px, + int32_t height_px, + bool enable_impeller) = 0; // Destroy a surface which is currently used. virtual void DestroyRenderSurface() = 0; - // Returns a valid LinuxesRenderSurfaceTarget representing the backing + // Returns a valid ELinuxRenderSurfaceTarget representing the backing // window. - virtual LinuxesRenderSurfaceTarget* GetRenderSurfaceTarget() const = 0; + virtual ELinuxRenderSurfaceTarget* GetRenderSurfaceTarget() const = 0; // Sets the delegate used to communicate state changes from window to view // such as key presses, mouse position updates etc. virtual void SetView(WindowBindingHandlerDelegate* view) = 0; + // Returns the rotation(degree) for the backing window. + virtual uint16_t GetRotationDegree() const = 0; + // Returns the scale factor for the backing window. virtual double GetDpiScale() = 0; // Returns the bounds of the backing window in physical pixels. virtual PhysicalWindowBounds GetPhysicalWindowBounds() = 0; + // Returns the frame rate of the display. + virtual int32_t GetFrameRate() = 0; + // Sets the cursor that should be used when the mouse is over the Flutter // content. See mouse_cursor.dart for the values and meanings of cursor_name. virtual void UpdateFlutterCursor(const std::string& cursor_name) = 0; diff --git a/src/flutter/shell/platform/linux_embedded/window_binding_handler_delegate.h b/src/flutter/shell/platform/linux_embedded/window_binding_handler_delegate.h index 4187342f..ad51a5d2 100644 --- a/src/flutter/shell/platform/linux_embedded/window_binding_handler_delegate.h +++ b/src/flutter/shell/platform/linux_embedded/window_binding_handler_delegate.h @@ -11,22 +11,32 @@ namespace flutter { class WindowBindingHandlerDelegate { public: - // Notifies delegate that backing window size has changed. - // Typically called by currently configured WindowBindingHandler - virtual void OnWindowSizeChanged(size_t width, size_t height) const = 0; - - // Notifies delegate that backing window mouse has moved. - // Typically called by currently configured WindowBindingHandler - virtual void OnPointerMove(double x, double y) = 0; + // Notifies delegate that backing window size has changed. Typically called by + // currently configured WindowBindingHandler + // @param[in] width_px Physical width of the window. + // @param[in] height_px Physical height of the window. + virtual void OnWindowSizeChanged(size_t width_px, size_t height_px) const = 0; + + // Notifies delegate that backing window mouse has moved. Typically called by + // currently configured WindowBindingHandler + // @param[in] x_px The x coordinate of the pointer event in physical pixels. + // @param[in] y_px The y coordinate of the pointer event in physical pixels. + virtual void OnPointerMove(double x_px, double y_px) = 0; // Notifies delegate that backing window mouse pointer button has been // pressed. Typically called by currently configured WindowBindingHandler - virtual void OnPointerDown(double x, double y, + // @param[in] x_px The x coordinate of the pointer event in physical pixels. + // @param[in] y_px The y coordinate of the pointer event in physical pixels. + virtual void OnPointerDown(double x_px, + double y_px, FlutterPointerMouseButtons button) = 0; // Notifies delegate that backing window mouse pointer button has been // released. Typically called by currently configured WindowBindingHandler - virtual void OnPointerUp(double x, double y, + // @param[in] x_px The x coordinate of the pointer event in physical pixels. + // @param[in] y_px The y coordinate of the pointer event in physical pixels. + virtual void OnPointerUp(double x_px, + double y_px, FlutterPointerMouseButtons button) = 0; // Notifies delegate that backing window mouse pointer has left the window. @@ -35,14 +45,24 @@ class WindowBindingHandlerDelegate { // Notifies delegate that backing window touch pointer has been pressed. // Typically called by currently configured WindowBindingHandler + // @param[in] time Monotonically increasing timestamp in milliseconds. + // @param[in] id The unique id of this touch point. + // @param[in] x The Surface local x coordinate. + // @param[in] y The Surface local y coordinate. virtual void OnTouchDown(uint32_t time, int32_t id, double x, double y) = 0; // Notifies delegate that backing window touch pointer has been released. // Typically called by currently configured WindowBindingHandler + // @param[in] time Monotonically increasing timestamp in milliseconds. + // @param[in] id The unique id of this touch point. virtual void OnTouchUp(uint32_t time, int32_t id) = 0; // Notifies delegate that backing window touch pointer has moved. // Typically called by currently configured WindowBindingHandler + // @param[in] time Monotonically increasing timestamp in milliseconds. + // @param[in] id The unique id of this touch point. + // @param[in] x The Surface local x coordinate. + // @param[in] y The Surface local y coordinate. virtual void OnTouchMotion(uint32_t time, int32_t id, double x, double y) = 0; // Notifies delegate that backing window touch pointer has been canceled. @@ -55,8 +75,10 @@ class WindowBindingHandlerDelegate { // Notifies delegate that backing window key has been modifired. // Typically called by currently configured WindowBindingHandler - virtual void OnKeyModifiers(uint32_t mods_depressed, uint32_t mods_latched, - uint32_t mods_locked, uint32_t group) = 0; + virtual void OnKeyModifiers(uint32_t mods_depressed, + uint32_t mods_latched, + uint32_t mods_locked, + uint32_t group) = 0; // Notifies delegate that backing window key has been pressed. // Typically called by currently configured WindowBindingHandler @@ -72,8 +94,32 @@ class WindowBindingHandlerDelegate { // Notifies delegate that backing window size has recevied scroll. // Typically called by currently configured WindowBindingHandler - virtual void OnScroll(double x, double y, double delta_x, double delta_y, + virtual void OnScroll(double x, + double y, + double delta_x, + double delta_y, int scroll_offset_multiplier) = 0; + + // Notifies delegate that backing window vsync has happened. + // Typically called by currently configured WindowBindingHandler + virtual void OnVsync(uint64_t last_frame_time_nanos, + uint64_t vsync_interval_time_nanos) = 0; + + // Update the status of the high contrast feature + virtual void UpdateHighContrastEnabled(bool enabled) = 0; + + // Update the status of the text scaling factor feature + virtual void UpdateTextScaleFactor(float factor) = 0; + + // Update the status of the corresponding to display changes. + // @param[in] refresh_rate Refresh rate of the display. + // @param[in] width_px Physical width of the display. + // @param[in] height_px Physical height of the display. + // @param[in] pixel_ratio Pixel ratio of the display. + virtual void UpdateDisplayInfo(double refresh_rate, + size_t width_px, + size_t height_px, + double pixel_ratio) = 0; }; } // namespace flutter diff --git a/src/templates/app/linux-drm/main.cc b/src/templates/app/linux-drm/main.cc deleted file mode 100644 index ec4f968d..00000000 --- a/src/templates/app/linux-drm/main.cc +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include - -#include -#include -#include -#include -#include - -#include "command_options.h" - -int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); - if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); - return 0; - } - - // The project to run. - const auto bundle_path = options.GetValue("bundle"); - const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); - flutter::DartProject project(fl_path); - auto command_line_arguments = std::vector(); - project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - - // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - auto flutter_controller = std::make_unique( - flutter::FlutterViewController::ViewMode::kFullscreen, 0, 0, show_cursor, - project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { - return 0; - } - - // Main loop. - auto next_flutter_event_time = - std::chrono::steady_clock::time_point::clock::now(); - while (flutter_controller->view()->DispatchEvent()) { - // Wait until the next event. - { - auto wait_duration = - std::max(std::chrono::nanoseconds(0), - next_flutter_event_time - - std::chrono::steady_clock::time_point::clock::now()); - std::this_thread::sleep_for( - std::chrono::duration_cast(wait_duration)); - } - - // Processes any pending events in the Flutter engine, and returns the - // number of nanoseconds until the next scheduled event (or max, if none). - auto wait_duration = flutter_controller->engine()->ProcessMessages(); - { - auto next_event_time = std::chrono::steady_clock::time_point::max(); - if (wait_duration != std::chrono::nanoseconds::max()) { - auto next_wakeup = - std::max(std::chrono::steady_clock::time_point::max(), - std::chrono::steady_clock::time_point::clock::now() + - wait_duration); - next_event_time = std::min(next_event_time, next_wakeup); - } else { - // Wait 1/60 [sec] = 13 [msec] if no events. - next_event_time = std::chrono::steady_clock::time_point::clock::now() + - std::chrono::milliseconds(13); - } - next_flutter_event_time = - std::max(next_flutter_event_time, next_event_time); - } - } - - return 0; -} diff --git a/src/templates/app/linux-wayland-weston-desktop-shell/main.cc b/src/templates/app/linux-wayland-weston-desktop-shell/main.cc deleted file mode 100644 index f4c0b56d..00000000 --- a/src/templates/app/linux-wayland-weston-desktop-shell/main.cc +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include -#include - -#include -#include -#include - -int main(int argc, char** argv) { - // Works as a weston desktop shell. - bool show_cursor; - auto config_file = weston_config_get_name_from_env(); - auto config = weston_config_parse(config_file); - auto s = weston_config_get_section(config, "flutter_linux_wayland", nullptr, - nullptr); - char* path = nullptr; - if (s) { - weston_config_section_get_bool(s, "show-cursor", &show_cursor, true); - weston_config_section_get_string( - s, "flutter-project-path", &path, - "../../sample-app/sample/build/linux/x64/release/bundle"); - } - - std::wstring fl_path; - if (path != nullptr) { - // convert char* to wstring. - std::string str(path); - std::wstring ws_temp(str.begin(), str.end()); - if (!ws_temp.empty()) { - fl_path = ws_temp; - } - - free(path); - } - - // libweston-6 does not export weston_config_destroy(). - // So run free() instead. - if (config) { - free(config); - } - // weston_config_destroy(config); - - // The project to run. - flutter::DartProject project(fl_path); - auto command_line_arguments = std::vector(); - project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - - // The Flutter instance hosted by this window. - int width = 640; - int height = 480; - auto flutter_controller = std::make_unique( - flutter::FlutterViewController::ViewMode::kFullscreen, width, height, - show_cursor, project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { - return 0; - } - - // Main loop. - while (flutter_controller->view()->DispatchEvent()) { - flutter_controller->engine()->ProcessMessages(); - } - - return 0; -} diff --git a/src/templates/app/linux-wayland/main.cc b/src/templates/app/linux-wayland/main.cc deleted file mode 100644 index 459bc04b..00000000 --- a/src/templates/app/linux-wayland/main.cc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include - -#include -#include -#include - -#include "command_options.h" - -int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("fullscreen", "f", "Always full-screen display", - false); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); - options.AddInt("width", "w", "Flutter app window width", 1280, false); - options.AddInt("height", "h", "Flutter app window height", 720, false); - if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); - return 0; - } - - // The project to run. - const auto bundle_path = options.GetValue("bundle"); - const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); - flutter::DartProject project(fl_path); - auto command_line_arguments = std::vector(); - project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - - // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - const auto view_mode = - options.Exist("fullscreen") - ? flutter::FlutterViewController::ViewMode::kFullscreen - : flutter::FlutterViewController::ViewMode::kNormal; - const auto width = options.GetValue("width"); - const auto height = options.GetValue("height"); - auto flutter_controller = std::make_unique( - view_mode, width, height, show_cursor, project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { - return 0; - } - - // Main loop. - while (flutter_controller->view()->DispatchEvent()) { - flutter_controller->engine()->ProcessMessages(); - } - - return 0; -} diff --git a/src/templates/app/linux-x11/main.cc b/src/templates/app/linux-x11/main.cc deleted file mode 100644 index 7d099ba4..00000000 --- a/src/templates/app/linux-x11/main.cc +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2021 Sony Corporation. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include - -#include -#include -#include -#include -#include - -#include "command_options.h" - -int main(int argc, char** argv) { - commandline::CommandOptions options; - options.AddString("bundle", "b", "Path to Flutter app bundle", "./bundle", - true); - options.AddWithoutValue("fullscreen", "f", "Always full-screen display", - false); - options.AddWithoutValue("no-cursor", "n", "No mouse cursor/pointer", false); - options.AddInt("width", "w", "Flutter app window width", 1280, false); - options.AddInt("height", "h", "Flutter app window height", 720, false); - if (!options.Parse(argc, argv)) { - std::cerr << options.GetError() << std::endl; - std::cout << options.ShowHelp(); - return 0; - } - - // The project to run. - const auto bundle_path = options.GetValue("bundle"); - const std::wstring fl_path(bundle_path.begin(), bundle_path.end()); - flutter::DartProject project(fl_path); - auto command_line_arguments = std::vector(); - project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); - - // The Flutter instance hosted by this window. - const bool show_cursor = !options.Exist("no-cursor"); - const auto view_mode = - options.Exist("fullscreen") - ? flutter::FlutterViewController::ViewMode::kFullscreen - : flutter::FlutterViewController::ViewMode::kNormal; - const auto width = options.GetValue("width"); - const auto height = options.GetValue("height"); - auto flutter_controller = std::make_unique( - view_mode, width, height, show_cursor, project); - - // Ensure that basic setup of the controller was successful. - if (!flutter_controller->engine() || !flutter_controller->view()) { - return 0; - } - - // Main loop. - auto next_flutter_event_time = - std::chrono::steady_clock::time_point::clock::now(); - while (flutter_controller->view()->DispatchEvent()) { - // Wait until the next event. - { - auto wait_duration = - std::max(std::chrono::nanoseconds(0), - next_flutter_event_time - - std::chrono::steady_clock::time_point::clock::now()); - std::this_thread::sleep_for( - std::chrono::duration_cast(wait_duration)); - } - - // Processes any pending events in the Flutter engine, and returns the - // number of nanoseconds until the next scheduled event (or max, if none). - auto wait_duration = flutter_controller->engine()->ProcessMessages(); - { - auto next_event_time = std::chrono::steady_clock::time_point::max(); - if (wait_duration != std::chrono::nanoseconds::max()) { - auto next_wakeup = - std::max(std::chrono::steady_clock::time_point::max(), - std::chrono::steady_clock::time_point::clock::now() + - wait_duration); - next_event_time = std::min(next_event_time, next_wakeup); - } else { - // Wait 1/60 [sec] = 13 [msec] if no events. - next_event_time = std::chrono::steady_clock::time_point::clock::now() + - std::chrono::milliseconds(13); - } - next_flutter_event_time = - std::max(next_flutter_event_time, next_event_time); - } - } - - return 0; -} diff --git a/src/third_party/rapidjson/CHANGELOG.md b/src/third_party/rapidjson/CHANGELOG.md new file mode 100644 index 00000000..1c580bd1 --- /dev/null +++ b/src/third_party/rapidjson/CHANGELOG.md @@ -0,0 +1,158 @@ +# Change Log +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] + +## 1.1.0 - 2016-08-25 + +### Added +* Add GenericDocument ctor overload to specify JSON type (#369) +* Add FAQ (#372, #373, #374, #376) +* Add forward declaration header `fwd.h` +* Add @PlatformIO Library Registry manifest file (#400) +* Implement assignment operator for BigInteger (#404) +* Add comments support (#443) +* Adding coapp definition (#460) +* documenttest.cpp: EXPECT_THROW when checking empty allocator (470) +* GenericDocument: add implicit conversion to ParseResult (#480) +* Use with C++ linkage on Windows ARM (#485) +* Detect little endian for Microsoft ARM targets +* Check Nan/Inf when writing a double (#510) +* Add JSON Schema Implementation (#522) +* Add iostream wrapper (#530) +* Add Jsonx example for converting JSON into JSONx (a XML format) (#531) +* Add optional unresolvedTokenIndex parameter to Pointer::Get() (#532) +* Add encoding validation option for Writer/PrettyWriter (#534) +* Add Writer::SetMaxDecimalPlaces() (#536) +* Support {0, } and {0, m} in Regex (#539) +* Add Value::Get/SetFloat(), Value::IsLossLessFloat/Double() (#540) +* Add stream position check to reader unit tests (#541) +* Add Templated accessors and range-based for (#542) +* Add (Pretty)Writer::RawValue() (#543) +* Add Document::Parse(std::string), Document::Parse(const char*, size_t length) and related APIs. (#553) +* Add move constructor for GenericSchemaDocument (#554) +* Add VS2010 and VS2015 to AppVeyor CI (#555) +* Add parse-by-parts example (#556, #562) +* Support parse number as string (#564, #589) +* Add kFormatSingleLineArray for PrettyWriter (#577) +* Added optional support for trailing commas (#584) +* Added filterkey and filterkeydom examples (#615) +* Added npm docs (#639) +* Allow options for writing and parsing NaN/Infinity (#641) +* Add std::string overload to PrettyWriter::Key() when RAPIDJSON_HAS_STDSTRING is defined (#698) + +### Fixed +* Fix gcc/clang/vc warnings (#350, #394, #397, #444, #447, #473, #515, #582, #589, #595, #667) +* Fix documentation (#482, #511, #550, #557, #614, #635, #660) +* Fix emscripten alignment issue (#535) +* Fix missing allocator to uses of AddMember in document (#365) +* CMake will no longer complain that the minimum CMake version is not specified (#501) +* Make it usable with old VC8 (VS2005) (#383) +* Prohibit C++11 move from Document to Value (#391) +* Try to fix incorrect 64-bit alignment (#419) +* Check return of fwrite to avoid warn_unused_result build failures (#421) +* Fix UB in GenericDocument::ParseStream (#426) +* Keep Document value unchanged on parse error (#439) +* Add missing return statement (#450) +* Fix Document::Parse(const Ch*) for transcoding (#478) +* encodings.h: fix typo in preprocessor condition (#495) +* Custom Microsoft headers are necessary only for Visual Studio 2012 and lower (#559) +* Fix memory leak for invalid regex (26e69ffde95ba4773ab06db6457b78f308716f4b) +* Fix a bug in schema minimum/maximum keywords for 64-bit integer (e7149d665941068ccf8c565e77495521331cf390) +* Fix a crash bug in regex (#605) +* Fix schema "required" keyword cannot handle duplicated keys (#609) +* Fix cmake CMP0054 warning (#612) +* Added missing include guards in istreamwrapper.h and ostreamwrapper.h (#634) +* Fix undefined behaviour (#646) +* Fix buffer overrun using PutN (#673) +* Fix rapidjson::value::Get() may returns wrong data (#681) +* Add Flush() for all value types (#689) +* Handle malloc() fail in PoolAllocator (#691) +* Fix builds on x32 platform. #703 + +### Changed +* Clarify problematic JSON license (#392) +* Move Travis to container based infrastructure (#504, #558) +* Make whitespace array more compact (#513) +* Optimize Writer::WriteString() with SIMD (#544) +* x86-64 48-bit pointer optimization for GenericValue (#546) +* Define RAPIDJSON_HAS_CXX11_RVALUE_REFS directly in clang (#617) +* Make GenericSchemaDocument constructor explicit (#674) +* Optimize FindMember when use std::string (#690) + +## [1.0.2] - 2015-05-14 + +### Added +* Add Value::XXXMember(...) overloads for std::string (#335) + +### Fixed +* Include rapidjson.h for all internal/error headers. +* Parsing some numbers incorrectly in full-precision mode (`kFullPrecisionParseFlag`) (#342) +* Fix some numbers parsed incorrectly (#336) +* Fix alignment of 64bit platforms (#328) +* Fix MemoryPoolAllocator::Clear() to clear user-buffer (0691502573f1afd3341073dd24b12c3db20fbde4) + +### Changed +* CMakeLists for include as a thirdparty in projects (#334, #337) +* Change Document::ParseStream() to use stack allocator for Reader (ffbe38614732af8e0b3abdc8b50071f386a4a685) + +## [1.0.1] - 2015-04-25 + +### Added +* Changelog following [Keep a CHANGELOG](https://github.com/olivierlacan/keep-a-changelog) suggestions. + +### Fixed +* Parsing of some numbers (e.g. "1e-00011111111111") causing assertion (#314). +* Visual C++ 32-bit compilation error in `diyfp.h` (#317). + +## [1.0.0] - 2015-04-22 + +### Added +* 100% [Coverall](https://coveralls.io/r/Tencent/rapidjson?branch=master) coverage. +* Version macros (#311) + +### Fixed +* A bug in trimming long number sequence (4824f12efbf01af72b8cb6fc96fae7b097b73015). +* Double quote in unicode escape (#288). +* Negative zero roundtrip (double only) (#289). +* Standardize behavior of `memcpy()` and `malloc()` (0c5c1538dcfc7f160e5a4aa208ddf092c787be5a, #305, 0e8bbe5e3ef375e7f052f556878be0bd79e9062d). + +### Removed +* Remove an invalid `Document::ParseInsitu()` API (e7f1c6dd08b522cfcf9aed58a333bd9a0c0ccbeb). + +## 1.0-beta - 2015-04-8 + +### Added +* RFC 7159 (#101) +* Optional Iterative Parser (#76) +* Deep-copy values (#20) +* Error code and message (#27) +* ASCII Encoding (#70) +* `kParseStopWhenDoneFlag` (#83) +* `kParseFullPrecisionFlag` (881c91d696f06b7f302af6d04ec14dd08db66ceb) +* Add `Key()` to handler concept (#134) +* C++11 compatibility and support (#128) +* Optimized number-to-string and vice versa conversions (#137, #80) +* Short-String Optimization (#131) +* Local stream optimization by traits (#32) +* Travis & Appveyor Continuous Integration, with Valgrind verification (#24, #242) +* Redo all documentation (English, Simplified Chinese) + +### Changed +* Copyright ownership transferred to THL A29 Limited (a Tencent company). +* Migrating from Premake to CMAKE (#192) +* Resolve all warning reports + +### Removed +* Remove other JSON libraries for performance comparison (#180) + +## 0.11 - 2012-11-16 + +## 0.1 - 2011-11-18 + +[Unreleased]: https://github.com/Tencent/rapidjson/compare/v1.1.0...HEAD +[1.1.0]: https://github.com/Tencent/rapidjson/compare/v1.0.2...v1.1.0 +[1.0.2]: https://github.com/Tencent/rapidjson/compare/v1.0.1...v1.0.2 +[1.0.1]: https://github.com/Tencent/rapidjson/compare/v1.0.0...v1.0.1 +[1.0.0]: https://github.com/Tencent/rapidjson/compare/v1.0-beta...v1.0.0 diff --git a/src/third_party/rapidjson/allocators.h b/src/third_party/rapidjson/include/rapidjson/allocators.h similarity index 100% rename from src/third_party/rapidjson/allocators.h rename to src/third_party/rapidjson/include/rapidjson/allocators.h diff --git a/src/third_party/rapidjson/cursorstreamwrapper.h b/src/third_party/rapidjson/include/rapidjson/cursorstreamwrapper.h similarity index 100% rename from src/third_party/rapidjson/cursorstreamwrapper.h rename to src/third_party/rapidjson/include/rapidjson/cursorstreamwrapper.h diff --git a/src/third_party/rapidjson/document.h b/src/third_party/rapidjson/include/rapidjson/document.h similarity index 100% rename from src/third_party/rapidjson/document.h rename to src/third_party/rapidjson/include/rapidjson/document.h diff --git a/src/third_party/rapidjson/encodedstream.h b/src/third_party/rapidjson/include/rapidjson/encodedstream.h similarity index 100% rename from src/third_party/rapidjson/encodedstream.h rename to src/third_party/rapidjson/include/rapidjson/encodedstream.h diff --git a/src/third_party/rapidjson/encodings.h b/src/third_party/rapidjson/include/rapidjson/encodings.h similarity index 100% rename from src/third_party/rapidjson/encodings.h rename to src/third_party/rapidjson/include/rapidjson/encodings.h diff --git a/src/third_party/rapidjson/error/en.h b/src/third_party/rapidjson/include/rapidjson/error/en.h similarity index 100% rename from src/third_party/rapidjson/error/en.h rename to src/third_party/rapidjson/include/rapidjson/error/en.h diff --git a/src/third_party/rapidjson/error/error.h b/src/third_party/rapidjson/include/rapidjson/error/error.h similarity index 100% rename from src/third_party/rapidjson/error/error.h rename to src/third_party/rapidjson/include/rapidjson/error/error.h diff --git a/src/third_party/rapidjson/filereadstream.h b/src/third_party/rapidjson/include/rapidjson/filereadstream.h similarity index 100% rename from src/third_party/rapidjson/filereadstream.h rename to src/third_party/rapidjson/include/rapidjson/filereadstream.h diff --git a/src/third_party/rapidjson/filewritestream.h b/src/third_party/rapidjson/include/rapidjson/filewritestream.h similarity index 100% rename from src/third_party/rapidjson/filewritestream.h rename to src/third_party/rapidjson/include/rapidjson/filewritestream.h diff --git a/src/third_party/rapidjson/fwd.h b/src/third_party/rapidjson/include/rapidjson/fwd.h similarity index 100% rename from src/third_party/rapidjson/fwd.h rename to src/third_party/rapidjson/include/rapidjson/fwd.h diff --git a/src/third_party/rapidjson/internal/biginteger.h b/src/third_party/rapidjson/include/rapidjson/internal/biginteger.h similarity index 100% rename from src/third_party/rapidjson/internal/biginteger.h rename to src/third_party/rapidjson/include/rapidjson/internal/biginteger.h diff --git a/src/third_party/rapidjson/internal/diyfp.h b/src/third_party/rapidjson/include/rapidjson/internal/diyfp.h similarity index 100% rename from src/third_party/rapidjson/internal/diyfp.h rename to src/third_party/rapidjson/include/rapidjson/internal/diyfp.h diff --git a/src/third_party/rapidjson/internal/dtoa.h b/src/third_party/rapidjson/include/rapidjson/internal/dtoa.h similarity index 100% rename from src/third_party/rapidjson/internal/dtoa.h rename to src/third_party/rapidjson/include/rapidjson/internal/dtoa.h diff --git a/src/third_party/rapidjson/internal/ieee754.h b/src/third_party/rapidjson/include/rapidjson/internal/ieee754.h similarity index 100% rename from src/third_party/rapidjson/internal/ieee754.h rename to src/third_party/rapidjson/include/rapidjson/internal/ieee754.h diff --git a/src/third_party/rapidjson/internal/itoa.h b/src/third_party/rapidjson/include/rapidjson/internal/itoa.h similarity index 100% rename from src/third_party/rapidjson/internal/itoa.h rename to src/third_party/rapidjson/include/rapidjson/internal/itoa.h diff --git a/src/third_party/rapidjson/internal/meta.h b/src/third_party/rapidjson/include/rapidjson/internal/meta.h similarity index 100% rename from src/third_party/rapidjson/internal/meta.h rename to src/third_party/rapidjson/include/rapidjson/internal/meta.h diff --git a/src/third_party/rapidjson/internal/pow10.h b/src/third_party/rapidjson/include/rapidjson/internal/pow10.h similarity index 100% rename from src/third_party/rapidjson/internal/pow10.h rename to src/third_party/rapidjson/include/rapidjson/internal/pow10.h diff --git a/src/third_party/rapidjson/internal/regex.h b/src/third_party/rapidjson/include/rapidjson/internal/regex.h similarity index 100% rename from src/third_party/rapidjson/internal/regex.h rename to src/third_party/rapidjson/include/rapidjson/internal/regex.h diff --git a/src/third_party/rapidjson/internal/stack.h b/src/third_party/rapidjson/include/rapidjson/internal/stack.h similarity index 100% rename from src/third_party/rapidjson/internal/stack.h rename to src/third_party/rapidjson/include/rapidjson/internal/stack.h diff --git a/src/third_party/rapidjson/internal/strfunc.h b/src/third_party/rapidjson/include/rapidjson/internal/strfunc.h similarity index 100% rename from src/third_party/rapidjson/internal/strfunc.h rename to src/third_party/rapidjson/include/rapidjson/internal/strfunc.h diff --git a/src/third_party/rapidjson/internal/strtod.h b/src/third_party/rapidjson/include/rapidjson/internal/strtod.h similarity index 100% rename from src/third_party/rapidjson/internal/strtod.h rename to src/third_party/rapidjson/include/rapidjson/internal/strtod.h diff --git a/src/third_party/rapidjson/internal/swap.h b/src/third_party/rapidjson/include/rapidjson/internal/swap.h similarity index 100% rename from src/third_party/rapidjson/internal/swap.h rename to src/third_party/rapidjson/include/rapidjson/internal/swap.h diff --git a/src/third_party/rapidjson/istreamwrapper.h b/src/third_party/rapidjson/include/rapidjson/istreamwrapper.h similarity index 100% rename from src/third_party/rapidjson/istreamwrapper.h rename to src/third_party/rapidjson/include/rapidjson/istreamwrapper.h diff --git a/src/third_party/rapidjson/memorybuffer.h b/src/third_party/rapidjson/include/rapidjson/memorybuffer.h similarity index 100% rename from src/third_party/rapidjson/memorybuffer.h rename to src/third_party/rapidjson/include/rapidjson/memorybuffer.h diff --git a/src/third_party/rapidjson/memorystream.h b/src/third_party/rapidjson/include/rapidjson/memorystream.h similarity index 100% rename from src/third_party/rapidjson/memorystream.h rename to src/third_party/rapidjson/include/rapidjson/memorystream.h diff --git a/src/third_party/rapidjson/msinttypes/inttypes.h b/src/third_party/rapidjson/include/rapidjson/msinttypes/inttypes.h similarity index 100% rename from src/third_party/rapidjson/msinttypes/inttypes.h rename to src/third_party/rapidjson/include/rapidjson/msinttypes/inttypes.h diff --git a/src/third_party/rapidjson/msinttypes/stdint.h b/src/third_party/rapidjson/include/rapidjson/msinttypes/stdint.h similarity index 100% rename from src/third_party/rapidjson/msinttypes/stdint.h rename to src/third_party/rapidjson/include/rapidjson/msinttypes/stdint.h diff --git a/src/third_party/rapidjson/ostreamwrapper.h b/src/third_party/rapidjson/include/rapidjson/ostreamwrapper.h similarity index 100% rename from src/third_party/rapidjson/ostreamwrapper.h rename to src/third_party/rapidjson/include/rapidjson/ostreamwrapper.h diff --git a/src/third_party/rapidjson/pointer.h b/src/third_party/rapidjson/include/rapidjson/pointer.h similarity index 100% rename from src/third_party/rapidjson/pointer.h rename to src/third_party/rapidjson/include/rapidjson/pointer.h diff --git a/src/third_party/rapidjson/prettywriter.h b/src/third_party/rapidjson/include/rapidjson/prettywriter.h similarity index 100% rename from src/third_party/rapidjson/prettywriter.h rename to src/third_party/rapidjson/include/rapidjson/prettywriter.h diff --git a/src/third_party/rapidjson/rapidjson.h b/src/third_party/rapidjson/include/rapidjson/rapidjson.h similarity index 100% rename from src/third_party/rapidjson/rapidjson.h rename to src/third_party/rapidjson/include/rapidjson/rapidjson.h diff --git a/src/third_party/rapidjson/reader.h b/src/third_party/rapidjson/include/rapidjson/reader.h similarity index 100% rename from src/third_party/rapidjson/reader.h rename to src/third_party/rapidjson/include/rapidjson/reader.h diff --git a/src/third_party/rapidjson/schema.h b/src/third_party/rapidjson/include/rapidjson/schema.h similarity index 100% rename from src/third_party/rapidjson/schema.h rename to src/third_party/rapidjson/include/rapidjson/schema.h diff --git a/src/third_party/rapidjson/stream.h b/src/third_party/rapidjson/include/rapidjson/stream.h similarity index 100% rename from src/third_party/rapidjson/stream.h rename to src/third_party/rapidjson/include/rapidjson/stream.h diff --git a/src/third_party/rapidjson/stringbuffer.h b/src/third_party/rapidjson/include/rapidjson/stringbuffer.h similarity index 100% rename from src/third_party/rapidjson/stringbuffer.h rename to src/third_party/rapidjson/include/rapidjson/stringbuffer.h diff --git a/src/third_party/rapidjson/writer.h b/src/third_party/rapidjson/include/rapidjson/writer.h similarity index 100% rename from src/third_party/rapidjson/writer.h rename to src/third_party/rapidjson/include/rapidjson/writer.h diff --git a/src/third_party/rapidjson/license.txt b/src/third_party/rapidjson/license.txt new file mode 100644 index 00000000..7ccc161c --- /dev/null +++ b/src/third_party/rapidjson/license.txt @@ -0,0 +1,57 @@ +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License. +If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license. +A copy of the MIT License is included in this file. + +Other dependencies and licenses: + +Open Source Software Licensed Under the BSD License: +-------------------------------------------------------------------- + +The msinttypes r29 +Copyright (c) 2006-2013 Alexander Chemeris +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Open Source Software Licensed Under the JSON License: +-------------------------------------------------------------------- + +json.org +Copyright (c) 2002 JSON.org +All Rights Reserved. + +JSON_checker +Copyright (c) 2002 JSON.org +All Rights Reserved. + + +Terms of the JSON License: +--------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +Terms of the MIT License: +-------------------------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/third_party/rapidjson/readme.md b/src/third_party/rapidjson/readme.md new file mode 100644 index 00000000..78c9540d --- /dev/null +++ b/src/third_party/rapidjson/readme.md @@ -0,0 +1,160 @@ +![RapidJSON logo](doc/logo/rapidjson.png) + +![Release version](https://img.shields.io/badge/release-v1.1.0-blue.svg) + +## A fast JSON parser/generator for C++ with both SAX/DOM style API + +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +* [RapidJSON GitHub](https://github.com/Tencent/rapidjson/) +* RapidJSON Documentation + * [English](http://rapidjson.org/) + * [简体中文](http://rapidjson.org/zh-cn/) + * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) with downloadable PDF/EPUB/MOBI, without API reference. + +## Build status + +| [Linux][lin-link] | [Windows][win-link] | [Coveralls][cov-link] | +| :---------------: | :-----------------: | :-------------------: | +| ![lin-badge] | ![win-badge] | ![cov-badge] | + +[lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status" +[lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/l6qulgqahcayidrf/branch/master?svg=true "AppVeyor build status" +[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson-0fdqj/branch/master "AppVeyor build status" +[cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage" +[cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage" + +## Introduction + +RapidJSON is a JSON parser and generator for C++. It was inspired by [RapidXml](http://rapidxml.sourceforge.net/). + +* RapidJSON is **small** but **complete**. It supports both SAX and DOM style API. The SAX parser is only a half thousand lines of code. + +* RapidJSON is **fast**. Its performance can be comparable to `strlen()`. It also optionally supports SSE2/SSE4.2 for acceleration. + +* RapidJSON is **self-contained** and **header-only**. It does not depend on external libraries such as BOOST. It even does not depend on STL. + +* RapidJSON is **memory-friendly**. Each JSON value occupies exactly 16 bytes for most 32/64-bit machines (excluding text string). By default it uses a fast memory allocator, and the parser allocates memory compactly during parsing. + +* RapidJSON is **Unicode-friendly**. It supports UTF-8, UTF-16, UTF-32 (LE & BE), and their detection, validation and transcoding internally. For example, you can read a UTF-8 file and let RapidJSON transcode the JSON strings into UTF-16 in the DOM. It also supports surrogates and "\u0000" (null character). + +More features can be read [here](doc/features.md). + +JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in full compliance with RFC7159/ECMA-404, with optional support of relaxed syntax. More information about JSON can be obtained at +* [Introducing JSON](http://json.org/) +* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159) +* [Standard ECMA-404: The JSON Data Interchange Format](https://www.ecma-international.org/publications/standards/Ecma-404.htm) + +## Highlights in v1.1 (2016-8-25) + +* Added [JSON Pointer](doc/pointer.md) +* Added [JSON Schema](doc/schema.md) +* Added [relaxed JSON syntax](doc/dom.md) (comment, trailing comma, NaN/Infinity) +* Iterating array/object with [C++11 Range-based for loop](doc/tutorial.md) +* Reduce memory overhead of each `Value` from 24 bytes to 16 bytes in x86-64 architecture. + +For other changes please refer to [change log](CHANGELOG.md). + +## Compatibility + +RapidJSON is cross-platform. Some platform/compiler combinations which have been tested are shown as follows. +* Visual C++ 2008/2010/2013 on Windows (32/64-bit) +* GNU C++ 3.8.x on Cygwin +* Clang 3.4 on Mac OS X (32/64-bit) and iOS +* Clang 3.4 on Android NDK + +Users can build and run the unit tests on their platform/compiler. + +## Installation + +RapidJSON is a header-only C++ library. Just copy the `include/rapidjson` folder to system or project's include path. + +RapidJSON uses following software as its dependencies: +* [CMake](https://cmake.org/) as a general build tool +* (optional) [Doxygen](http://www.doxygen.org) to build documentation +* (optional) [googletest](https://github.com/google/googletest) for unit and performance testing + +To generate user documentation and run tests please proceed with the steps below: + +1. Execute `git submodule update --init` to get the files of thirdparty submodules (google test). +2. Create directory called `build` in rapidjson source directory. +3. Change to `build` directory and run `cmake ..` command to configure your build. Windows users can do the same with cmake-gui application. +4. On Windows, build the solution found in the build directory. On Linux, run `make` from the build directory. + +On successful build you will find compiled test and example binaries in `bin` +directory. The generated documentation will be available in `doc/html` +directory of the build tree. To run tests after finished build please run `make +test` or `ctest` from your build tree. You can get detailed output using `ctest +-V` command. + +It is possible to install library system-wide by running `make install` command +from the build tree with administrative privileges. This will install all files +according to system preferences. Once RapidJSON is installed, it is possible +to use it from other CMake projects by adding `find_package(RapidJSON)` line to +your CMakeLists.txt. + +## Usage at a glance + +This simple example parses a JSON string into a document (DOM), make a simple modification of the DOM, and finally stringify the DOM to a JSON string. + +~~~~~~~~~~cpp +// rapidjson/example/simpledom/simpledom.cpp` +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include + +using namespace rapidjson; + +int main() { + // 1. Parse a JSON string into DOM. + const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; + Document d; + d.Parse(json); + + // 2. Modify it by DOM. + Value& s = d["stars"]; + s.SetInt(s.GetInt() + 1); + + // 3. Stringify the DOM + StringBuffer buffer; + Writer writer(buffer); + d.Accept(writer); + + // Output {"project":"rapidjson","stars":11} + std::cout << buffer.GetString() << std::endl; + return 0; +} +~~~~~~~~~~ + +Note that this example did not handle potential errors. + +The following diagram shows the process. + +![simpledom](doc/diagram/simpledom.png) + +More [examples](https://github.com/Tencent/rapidjson/tree/master/example) are available: + +* DOM API + * [tutorial](https://github.com/Tencent/rapidjson/blob/master/example/tutorial/tutorial.cpp): Basic usage of DOM API. + +* SAX API + * [simplereader](https://github.com/Tencent/rapidjson/blob/master/example/simplereader/simplereader.cpp): Dumps all SAX events while parsing a JSON by `Reader`. + * [condense](https://github.com/Tencent/rapidjson/blob/master/example/condense/condense.cpp): A command line tool to rewrite a JSON, with all whitespaces removed. + * [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp): A command line tool to rewrite a JSON with indents and newlines by `PrettyWriter`. + * [capitalize](https://github.com/Tencent/rapidjson/blob/master/example/capitalize/capitalize.cpp): A command line tool to capitalize strings in JSON. + * [messagereader](https://github.com/Tencent/rapidjson/blob/master/example/messagereader/messagereader.cpp): Parse a JSON message with SAX API. + * [serialize](https://github.com/Tencent/rapidjson/blob/master/example/serialize/serialize.cpp): Serialize a C++ object into JSON with SAX API. + * [jsonx](https://github.com/Tencent/rapidjson/blob/master/example/jsonx/jsonx.cpp): Implements a `JsonxWriter` which stringify SAX events into [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html) (a kind of XML) format. The example is a command line tool which converts input JSON into JSONx format. + +* Schema + * [schemavalidator](https://github.com/Tencent/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp) : A command line tool to validate a JSON with a JSON schema. + +* Advanced + * [prettyauto](https://github.com/Tencent/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. + * [parsebyparts](https://github.com/Tencent/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. + * [filterkey](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key. + * [filterkeydom](https://github.com/Tencent/rapidjson/blob/master/example/filterkeydom/filterkeydom.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. diff --git a/src/third_party/rapidjson/readme.zh-cn.md b/src/third_party/rapidjson/readme.zh-cn.md new file mode 100644 index 00000000..ccf16699 --- /dev/null +++ b/src/third_party/rapidjson/readme.zh-cn.md @@ -0,0 +1,152 @@ +![RapidJSON logo](doc/logo/rapidjson.png) + +![Release version](https://img.shields.io/badge/release-v1.1.0-blue.svg) + +## 高效的 C++ JSON 解析/生成器,提供 SAX 及 DOM 风格 API + +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +* [RapidJSON GitHub](https://github.com/Tencent/rapidjson/) +* RapidJSON 文档 + * [English](http://rapidjson.org/) + * [简体中文](http://rapidjson.org/zh-cn/) + * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/details/zh-cn) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 + +## Build 状态 + +| [Linux][lin-link] | [Windows][win-link] | [Coveralls][cov-link] | +| :---------------: | :-----------------: | :-------------------: | +| ![lin-badge] | ![win-badge] | ![cov-badge] | + +[lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status" +[lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/l6qulgqahcayidrf/branch/master?svg=true "AppVeyor build status" +[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson-0fdqj/branch/master "AppVeyor build status" +[cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage" +[cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage" + +## 简介 + +RapidJSON 是一个 C++ 的 JSON 解析器及生成器。它的灵感来自 [RapidXml](http://rapidxml.sourceforge.net/)。 + +* RapidJSON 小而全。它同时支持 SAX 和 DOM 风格的 API。SAX 解析器只有约 500 行代码。 + +* RapidJSON 快。它的性能可与 `strlen()` 相比。可支持 SSE2/SSE4.2 加速。 + +* RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至不依赖于 STL。 + +* RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。 + +* RapidJSON 对 Unicode 友好。它支持 UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON 可以在分析一个 UTF-8 文件至 DOM 时,把当中的 JSON 字符串转码至 UTF-16。它也支持代理对(surrogate pair)及 `"\u0000"`(空字符)。 + +在 [这里](doc/features.zh-cn.md) 可读取更多特点。 + +JSON(JavaScript Object Notation)是一个轻量的数据交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404,并支持可选的放宽语法。 关于 JSON 的更多信息可参考: +* [Introducing JSON](http://json.org/) +* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159) +* [Standard ECMA-404: The JSON Data Interchange Format](https://www.ecma-international.org/publications/standards/Ecma-404.htm) + +## v1.1 中的亮点 (2016-8-25) + +* 加入 [JSON Pointer](doc/pointer.zh-cn.md) 功能,可更简单地访问及更改 DOM。 +* 加入 [JSON Schema](doc/schema.zh-cn.md) 功能,可在解析或生成 JSON 时进行校验。 +* 加入 [放宽的 JSON 语法](doc/dom.zh-cn.md) (注释、尾随逗号、NaN/Infinity) +* 使用 [C++11 范围 for 循环](doc/tutorial.zh-cn.md) 去遍历 array 和 object。 +* 在 x86-64 架构下,缩减每个 `Value` 的内存开销从 24 字节至 16 字节。 + +其他改动请参考 [change log](CHANGELOG.md). + +## 兼容性 + +RapidJSON 是跨平台的。以下是一些曾测试的平台/编译器组合: +* Visual C++ 2008/2010/2013 在 Windows (32/64-bit) +* GNU C++ 3.8.x 在 Cygwin +* Clang 3.4 在 Mac OS X (32/64-bit) 及 iOS +* Clang 3.4 在 Android NDK + +用户也可以在他们的平台上生成及执行单元测试。 + +## 安装 + +RapidJSON 是只有头文件的 C++ 库。只需把 `include/rapidjson` 目录复制至系统或项目的 include 目录中。 + +RapidJSON 依赖于以下软件: +* [CMake](https://cmake.org/) 作为通用生成工具 +* (optional) [Doxygen](http://www.doxygen.org) 用于生成文档 +* (optional) [googletest](https://github.com/google/googletest) 用于单元及性能测试 + +生成测试及例子的步骤: + +1. 执行 `git submodule update --init` 去获取 thirdparty submodules (google test)。 +2. 在 rapidjson 目录下,建立一个 `build` 目录。 +3. 在 `build` 目录下执行 `cmake ..` 命令以设置生成。Windows 用户可使用 cmake-gui 应用程序。 +4. 在 Windows 下,编译生成在 build 目录中的 solution。在 Linux 下,于 build 目录运行 `make`。 + +成功生成后,你会在 `bin` 的目录下找到编译后的测试及例子可执行文件。而生成的文档将位于 build 下的 `doc/html` 目录。要执行测试,请在 build 下执行 `make test` 或 `ctest`。使用 `ctest -V` 命令可获取详细的输出。 + +我们也可以把程序库安装至全系统中,只要在具管理权限下从 build 目录执行 `make install` 命令。这样会按系统的偏好设置安装所有文件。当安装 RapidJSON 后,其他的 CMake 项目需要使用它时,可以通过在 `CMakeLists.txt` 加入一句 `find_package(RapidJSON)`。 + +## 用法一览 + +此简单例子解析一个 JSON 字符串至一个 document (DOM),对 DOM 作出简单修改,最终把 DOM 转换(stringify)至 JSON 字符串。 + +~~~~~~~~~~cpp +// rapidjson/example/simpledom/simpledom.cpp` +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include + +using namespace rapidjson; + +int main() { + // 1. 把 JSON 解析至 DOM。 + const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; + Document d; + d.Parse(json); + + // 2. 利用 DOM 作出修改。 + Value& s = d["stars"]; + s.SetInt(s.GetInt() + 1); + + // 3. 把 DOM 转换(stringify)成 JSON。 + StringBuffer buffer; + Writer writer(buffer); + d.Accept(writer); + + // Output {"project":"rapidjson","stars":11} + std::cout << buffer.GetString() << std::endl; + return 0; +} +~~~~~~~~~~ + +注意此例子并没有处理潜在错误。 + +下图展示执行过程。 + +![simpledom](doc/diagram/simpledom.png) + +还有许多 [例子](https://github.com/Tencent/rapidjson/tree/master/example) 可供参考: + +* DOM API + * [tutorial](https://github.com/Tencent/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。 + +* SAX API + * [simplereader](https://github.com/Tencent/rapidjson/blob/master/example/simplereader/simplereader.cpp): 使用 `Reader` 解析 JSON 时,打印所有 SAX 事件。 + * [condense](https://github.com/Tencent/rapidjson/blob/master/example/condense/condense.cpp): 移除 JSON 中所有空白符的命令行工具。 + * [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp): 为 JSON 加入缩进与换行的命令行工具,当中使用了 `PrettyWriter`。 + * [capitalize](https://github.com/Tencent/rapidjson/blob/master/example/capitalize/capitalize.cpp): 把 JSON 中所有字符串改为大写的命令行工具。 + * [messagereader](https://github.com/Tencent/rapidjson/blob/master/example/messagereader/messagereader.cpp): 使用 SAX API 去解析一个 JSON 报文。 + * [serialize](https://github.com/Tencent/rapidjson/blob/master/example/serialize/serialize.cpp): 使用 SAX API 去序列化 C++ 对象,生成 JSON。 + * [jsonx](https://github.com/Tencent/rapidjson/blob/master/example/jsonx/jsonx.cpp): 实现了一个 `JsonxWriter`,它能把 SAX 事件写成 [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html)(一种 XML)格式。这个例子是把 JSON 输入转换成 JSONx 格式的命令行工具。 + +* Schema API + * [schemavalidator](https://github.com/Tencent/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp): 使用 JSON Schema 去校验 JSON 的命令行工具。 + +* 进阶 + * [prettyauto](https://github.com/Tencent/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 + * [parsebyparts](https://github.com/Tencent/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的 `AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 + * [filterkey](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): 移取使用者指定的键值的命令行工具。 + * [filterkeydom](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): 如上的工具,但展示如何使用生成器(generator)去填充一个 `Document`。 \ No newline at end of file diff --git a/src/wayland/protocol/weston-desktop-shell-client-protocol.h b/src/wayland/protocol/weston-desktop-shell-client-protocol.h deleted file mode 100644 index edffdd0e..00000000 --- a/src/wayland/protocol/weston-desktop-shell-client-protocol.h +++ /dev/null @@ -1,357 +0,0 @@ -/* Generated by wayland-scanner 1.18.0 */ - -#ifndef WESTON_DESKTOP_CLIENT_PROTOCOL_H -#define WESTON_DESKTOP_CLIENT_PROTOCOL_H - -#include -#include -#include "wayland-client.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @page page_weston_desktop The weston_desktop protocol - * @section page_ifaces_weston_desktop Interfaces - * - @subpage page_iface_weston_desktop_shell - create desktop widgets and helpers - * - @subpage page_iface_weston_screensaver - interface for implementing screensavers - */ -struct weston_desktop_shell; -struct weston_screensaver; -struct wl_output; -struct wl_surface; - -/** - * @page page_iface_weston_desktop_shell weston_desktop_shell - * @section page_iface_weston_desktop_shell_desc Description - * - * Traditional user interfaces can rely on this interface to define the - * foundations of typical desktops. Currently it's possible to set up - * background, panels and locking surfaces. - * @section page_iface_weston_desktop_shell_api API - * See @ref iface_weston_desktop_shell. - */ -/** - * @defgroup iface_weston_desktop_shell The weston_desktop_shell interface - * - * Traditional user interfaces can rely on this interface to define the - * foundations of typical desktops. Currently it's possible to set up - * background, panels and locking surfaces. - */ -extern const struct wl_interface weston_desktop_shell_interface; -/** - * @page page_iface_weston_screensaver weston_screensaver - * @section page_iface_weston_screensaver_desc Description - * - * Only one client can bind this interface at a time. - * @section page_iface_weston_screensaver_api API - * See @ref iface_weston_screensaver. - */ -/** - * @defgroup iface_weston_screensaver The weston_screensaver interface - * - * Only one client can bind this interface at a time. - */ -extern const struct wl_interface weston_screensaver_interface; - -#ifndef WESTON_DESKTOP_SHELL_CURSOR_ENUM -#define WESTON_DESKTOP_SHELL_CURSOR_ENUM -enum weston_desktop_shell_cursor { - WESTON_DESKTOP_SHELL_CURSOR_NONE = 0, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP = 1, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM = 2, - WESTON_DESKTOP_SHELL_CURSOR_ARROW = 3, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT = 4, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT = 5, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT = 6, - WESTON_DESKTOP_SHELL_CURSOR_MOVE = 7, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT = 8, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT = 9, - WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT = 10, - WESTON_DESKTOP_SHELL_CURSOR_BUSY = 11, -}; -#endif /* WESTON_DESKTOP_SHELL_CURSOR_ENUM */ - -#ifndef WESTON_DESKTOP_SHELL_PANEL_POSITION_ENUM -#define WESTON_DESKTOP_SHELL_PANEL_POSITION_ENUM -enum weston_desktop_shell_panel_position { - WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP = 0, - WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM = 1, - WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT = 2, - WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT = 3, -}; -#endif /* WESTON_DESKTOP_SHELL_PANEL_POSITION_ENUM */ - -#ifndef WESTON_DESKTOP_SHELL_ERROR_ENUM -#define WESTON_DESKTOP_SHELL_ERROR_ENUM -enum weston_desktop_shell_error { - /** - * an invalid argument was provided in a request - */ - WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT = 0, -}; -#endif /* WESTON_DESKTOP_SHELL_ERROR_ENUM */ - -/** - * @ingroup iface_weston_desktop_shell - * @struct weston_desktop_shell_listener - */ -struct weston_desktop_shell_listener { - /** - */ - void (*configure)(void *data, - struct weston_desktop_shell *weston_desktop_shell, - uint32_t edges, - struct wl_surface *surface, - int32_t width, - int32_t height); - /** - * tell the client to create, set the lock surface - * - * Tell the client we want it to create and set the lock surface, - * which is a GUI asking the user to unlock the screen. The lock - * surface is announced with 'set_lock_surface'. Whether or not the - * client actually implements locking, it MUST send 'unlock' - * request to let the normal desktop resume. - */ - void (*prepare_lock_surface)(void *data, - struct weston_desktop_shell *weston_desktop_shell); - /** - * tell client what cursor to show during a grab - * - * This event will be sent immediately before a fake enter event - * on the grab surface. - */ - void (*grab_cursor)(void *data, - struct weston_desktop_shell *weston_desktop_shell, - uint32_t cursor); -}; - -/** - * @ingroup iface_weston_desktop_shell - */ -static inline int -weston_desktop_shell_add_listener(struct weston_desktop_shell *weston_desktop_shell, - const struct weston_desktop_shell_listener *listener, void *data) -{ - return wl_proxy_add_listener((struct wl_proxy *) weston_desktop_shell, - (void (**)(void)) listener, data); -} - -#define WESTON_DESKTOP_SHELL_SET_BACKGROUND 0 -#define WESTON_DESKTOP_SHELL_SET_PANEL 1 -#define WESTON_DESKTOP_SHELL_SET_LOCK_SURFACE 2 -#define WESTON_DESKTOP_SHELL_UNLOCK 3 -#define WESTON_DESKTOP_SHELL_SET_GRAB_SURFACE 4 -#define WESTON_DESKTOP_SHELL_DESKTOP_READY 5 -#define WESTON_DESKTOP_SHELL_SET_PANEL_POSITION 6 - -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_CONFIGURE_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_PREPARE_LOCK_SURFACE_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_GRAB_CURSOR_SINCE_VERSION 1 - -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_SET_BACKGROUND_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_SET_PANEL_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_SET_LOCK_SURFACE_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_UNLOCK_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_SET_GRAB_SURFACE_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_DESKTOP_READY_SINCE_VERSION 1 -/** - * @ingroup iface_weston_desktop_shell - */ -#define WESTON_DESKTOP_SHELL_SET_PANEL_POSITION_SINCE_VERSION 1 - -/** @ingroup iface_weston_desktop_shell */ -static inline void -weston_desktop_shell_set_user_data(struct weston_desktop_shell *weston_desktop_shell, void *user_data) -{ - wl_proxy_set_user_data((struct wl_proxy *) weston_desktop_shell, user_data); -} - -/** @ingroup iface_weston_desktop_shell */ -static inline void * -weston_desktop_shell_get_user_data(struct weston_desktop_shell *weston_desktop_shell) -{ - return wl_proxy_get_user_data((struct wl_proxy *) weston_desktop_shell); -} - -static inline uint32_t -weston_desktop_shell_get_version(struct weston_desktop_shell *weston_desktop_shell) -{ - return wl_proxy_get_version((struct wl_proxy *) weston_desktop_shell); -} - -/** @ingroup iface_weston_desktop_shell */ -static inline void -weston_desktop_shell_destroy(struct weston_desktop_shell *weston_desktop_shell) -{ - wl_proxy_destroy((struct wl_proxy *) weston_desktop_shell); -} - -/** - * @ingroup iface_weston_desktop_shell - */ -static inline void -weston_desktop_shell_set_background(struct weston_desktop_shell *weston_desktop_shell, struct wl_output *output, struct wl_surface *surface) -{ - wl_proxy_marshal((struct wl_proxy *) weston_desktop_shell, - WESTON_DESKTOP_SHELL_SET_BACKGROUND, output, surface); -} - -/** - * @ingroup iface_weston_desktop_shell - */ -static inline void -weston_desktop_shell_set_panel(struct weston_desktop_shell *weston_desktop_shell, struct wl_output *output, struct wl_surface *surface) -{ - wl_proxy_marshal((struct wl_proxy *) weston_desktop_shell, - WESTON_DESKTOP_SHELL_SET_PANEL, output, surface); -} - -/** - * @ingroup iface_weston_desktop_shell - */ -static inline void -weston_desktop_shell_set_lock_surface(struct weston_desktop_shell *weston_desktop_shell, struct wl_surface *surface) -{ - wl_proxy_marshal((struct wl_proxy *) weston_desktop_shell, - WESTON_DESKTOP_SHELL_SET_LOCK_SURFACE, surface); -} - -/** - * @ingroup iface_weston_desktop_shell - */ -static inline void -weston_desktop_shell_unlock(struct weston_desktop_shell *weston_desktop_shell) -{ - wl_proxy_marshal((struct wl_proxy *) weston_desktop_shell, - WESTON_DESKTOP_SHELL_UNLOCK); -} - -/** - * @ingroup iface_weston_desktop_shell - * - * The surface set by this request will receive a fake - * pointer.enter event during grabs at position 0, 0 and is - * expected to set an appropriate cursor image as described by - * the grab_cursor event sent just before the enter event. - */ -static inline void -weston_desktop_shell_set_grab_surface(struct weston_desktop_shell *weston_desktop_shell, struct wl_surface *surface) -{ - wl_proxy_marshal((struct wl_proxy *) weston_desktop_shell, - WESTON_DESKTOP_SHELL_SET_GRAB_SURFACE, surface); -} - -/** - * @ingroup iface_weston_desktop_shell - * - * Tell the server, that enough desktop elements have been drawn - * to make the desktop look ready for use. During start-up, the - * server can wait for this request with a black screen before - * starting to fade in the desktop, for instance. If the client - * parts of a desktop take a long time to initialize, we avoid - * showing temporary garbage. - */ -static inline void -weston_desktop_shell_desktop_ready(struct weston_desktop_shell *weston_desktop_shell) -{ - wl_proxy_marshal((struct wl_proxy *) weston_desktop_shell, - WESTON_DESKTOP_SHELL_DESKTOP_READY); -} - -/** - * @ingroup iface_weston_desktop_shell - * - * Tell the shell which side of the screen the panel is - * located. This is so that new windows do not overlap the panel - * and maximized windows maximize properly. - */ -static inline void -weston_desktop_shell_set_panel_position(struct weston_desktop_shell *weston_desktop_shell, uint32_t position) -{ - wl_proxy_marshal((struct wl_proxy *) weston_desktop_shell, - WESTON_DESKTOP_SHELL_SET_PANEL_POSITION, position); -} - -#define WESTON_SCREENSAVER_SET_SURFACE 0 - - -/** - * @ingroup iface_weston_screensaver - */ -#define WESTON_SCREENSAVER_SET_SURFACE_SINCE_VERSION 1 - -/** @ingroup iface_weston_screensaver */ -static inline void -weston_screensaver_set_user_data(struct weston_screensaver *weston_screensaver, void *user_data) -{ - wl_proxy_set_user_data((struct wl_proxy *) weston_screensaver, user_data); -} - -/** @ingroup iface_weston_screensaver */ -static inline void * -weston_screensaver_get_user_data(struct weston_screensaver *weston_screensaver) -{ - return wl_proxy_get_user_data((struct wl_proxy *) weston_screensaver); -} - -static inline uint32_t -weston_screensaver_get_version(struct weston_screensaver *weston_screensaver) -{ - return wl_proxy_get_version((struct wl_proxy *) weston_screensaver); -} - -/** @ingroup iface_weston_screensaver */ -static inline void -weston_screensaver_destroy(struct weston_screensaver *weston_screensaver) -{ - wl_proxy_destroy((struct wl_proxy *) weston_screensaver); -} - -/** - * @ingroup iface_weston_screensaver - * - * A screensaver surface is normally hidden, and only visible after an - * idle timeout. - */ -static inline void -weston_screensaver_set_surface(struct weston_screensaver *weston_screensaver, struct wl_surface *surface, struct wl_output *output) -{ - wl_proxy_marshal((struct wl_proxy *) weston_screensaver, - WESTON_SCREENSAVER_SET_SURFACE, surface, output); -} - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/wayland/protocol/weston-desktop-shell-protocol.c b/src/wayland/protocol/weston-desktop-shell-protocol.c deleted file mode 100644 index 7bda93da..00000000 --- a/src/wayland/protocol/weston-desktop-shell-protocol.c +++ /dev/null @@ -1,67 +0,0 @@ -/* Generated by wayland-scanner 1.18.0 */ - -#include -#include -#include "wayland-util.h" - -#ifndef __has_attribute -# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ -#endif - -#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) -#define WL_PRIVATE __attribute__ ((visibility("hidden"))) -#else -#define WL_PRIVATE -#endif - -extern const struct wl_interface wl_output_interface; -extern const struct wl_interface wl_surface_interface; - -static const struct wl_interface *weston_desktop_types[] = { - NULL, - &wl_output_interface, - &wl_surface_interface, - &wl_output_interface, - &wl_surface_interface, - &wl_surface_interface, - &wl_surface_interface, - NULL, - &wl_surface_interface, - NULL, - NULL, - &wl_surface_interface, - &wl_output_interface, -}; - -static const struct wl_message weston_desktop_shell_requests[] = { - { "set_background", "oo", weston_desktop_types + 1 }, - { "set_panel", "oo", weston_desktop_types + 3 }, - { "set_lock_surface", "o", weston_desktop_types + 5 }, - { "unlock", "", weston_desktop_types + 0 }, - { "set_grab_surface", "o", weston_desktop_types + 6 }, - { "desktop_ready", "", weston_desktop_types + 0 }, - { "set_panel_position", "u", weston_desktop_types + 0 }, -}; - -static const struct wl_message weston_desktop_shell_events[] = { - { "configure", "uoii", weston_desktop_types + 7 }, - { "prepare_lock_surface", "", weston_desktop_types + 0 }, - { "grab_cursor", "u", weston_desktop_types + 0 }, -}; - -WL_PRIVATE const struct wl_interface weston_desktop_shell_interface = { - "weston_desktop_shell", 1, - 7, weston_desktop_shell_requests, - 3, weston_desktop_shell_events, -}; - -static const struct wl_message weston_screensaver_requests[] = { - { "set_surface", "oo", weston_desktop_types + 11 }, -}; - -WL_PRIVATE const struct wl_interface weston_screensaver_interface = { - "weston_screensaver", 1, - 1, weston_screensaver_requests, - 0, NULL, -}; -