Own Android platform services and narrow legacy fallback
This commit is contained in:
316
src/platform_android/android_platform_services.cpp
Normal file
316
src/platform_android/android_platform_services.cpp
Normal file
@@ -0,0 +1,316 @@
|
||||
#include "platform_android/android_platform_services.h"
|
||||
|
||||
#include "platform_api/network_tls_policy.h"
|
||||
#include "platform_api/platform_policy.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
#include <GLES3/gl3.h>
|
||||
#endif
|
||||
|
||||
namespace pp::platform::android {
|
||||
namespace {
|
||||
|
||||
constexpr auto kPlatformFamily = pp::platform::PlatformFamily::android;
|
||||
|
||||
class AndroidPlatformServices final : public pp::platform::PlatformServices {
|
||||
public:
|
||||
explicit AndroidPlatformServices(AndroidPlatformServicesConfig config)
|
||||
: bridge_(std::move(config.bridge))
|
||||
, storage_paths_(std::move(config.storage_paths))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] PlatformStoragePaths prepare_storage_paths() override
|
||||
{
|
||||
return storage_paths_ ? *storage_paths_ : PlatformStoragePaths {};
|
||||
}
|
||||
|
||||
void log_stacktrace() override
|
||||
{
|
||||
}
|
||||
|
||||
void trigger_crash_test() override
|
||||
{
|
||||
#if defined(__ANDROID__)
|
||||
int* x = nullptr;
|
||||
*x = 42;
|
||||
#endif
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string clipboard_text() override
|
||||
{
|
||||
if (bridge_.clipboard_text)
|
||||
return bridge_.clipboard_text();
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] bool set_clipboard_text(std::string_view text) override
|
||||
{
|
||||
if (bridge_.set_clipboard_text)
|
||||
return bridge_.set_clipboard_text(text);
|
||||
return false;
|
||||
}
|
||||
|
||||
void set_cursor_visible(bool visible) override
|
||||
{
|
||||
(void)visible;
|
||||
}
|
||||
|
||||
void set_virtual_keyboard_visible(bool visible) override
|
||||
{
|
||||
if (bridge_.set_virtual_keyboard_visible)
|
||||
bridge_.set_virtual_keyboard_visible(visible);
|
||||
}
|
||||
|
||||
void attach_ui_thread() override
|
||||
{
|
||||
if (bridge_.attach_ui_thread)
|
||||
bridge_.attach_ui_thread();
|
||||
}
|
||||
|
||||
void detach_ui_thread() override
|
||||
{
|
||||
if (bridge_.detach_ui_thread)
|
||||
bridge_.detach_ui_thread();
|
||||
}
|
||||
|
||||
void acquire_render_context() override
|
||||
{
|
||||
if (bridge_.acquire_render_context)
|
||||
bridge_.acquire_render_context();
|
||||
}
|
||||
|
||||
void release_render_context() override
|
||||
{
|
||||
if (bridge_.release_render_context)
|
||||
bridge_.release_render_context();
|
||||
}
|
||||
|
||||
void present_render_context() override
|
||||
{
|
||||
if (bridge_.present_render_context)
|
||||
bridge_.present_render_context();
|
||||
}
|
||||
|
||||
void bind_default_render_target() override
|
||||
{
|
||||
#if defined(__ANDROID__)
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void bind_main_render_target() override
|
||||
{
|
||||
bind_default_render_target();
|
||||
}
|
||||
|
||||
void apply_render_platform_hints() override
|
||||
{
|
||||
}
|
||||
|
||||
void install_render_debug_callback() override
|
||||
{
|
||||
}
|
||||
|
||||
void begin_render_capture_frame() override
|
||||
{
|
||||
}
|
||||
|
||||
void end_render_capture_frame() override
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] bool deletes_recorded_files_on_clear() override
|
||||
{
|
||||
return platform_deletes_recorded_files_on_clear(kPlatformFamily);
|
||||
}
|
||||
|
||||
void clear_recorded_files(std::string_view recording_path) override
|
||||
{
|
||||
(void)recording_path;
|
||||
}
|
||||
|
||||
void publish_exported_image(std::string_view path) override
|
||||
{
|
||||
(void)path;
|
||||
}
|
||||
|
||||
void flush_persistent_storage() override
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<std::string> document_browse_roots(
|
||||
std::string_view work_path,
|
||||
std::string_view data_path) override
|
||||
{
|
||||
return platform_document_browse_roots(kPlatformFamily, work_path, data_path);
|
||||
}
|
||||
|
||||
void save_ui_state() override
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] bool enables_live_asset_reloading() override
|
||||
{
|
||||
return platform_enables_live_asset_reloading(kPlatformFamily);
|
||||
}
|
||||
|
||||
void update_platform_frame(float delta_time_seconds) override
|
||||
{
|
||||
(void)delta_time_seconds;
|
||||
}
|
||||
|
||||
void report_rendered_frames(int frames) override
|
||||
{
|
||||
(void)frames;
|
||||
}
|
||||
|
||||
void display_file(std::string_view path) override
|
||||
{
|
||||
(void)path;
|
||||
}
|
||||
|
||||
void share_file(std::string_view path) override
|
||||
{
|
||||
(void)path;
|
||||
}
|
||||
|
||||
void request_app_close() override
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] bool start_vr_mode() override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void stop_vr_mode() override
|
||||
{
|
||||
}
|
||||
|
||||
void pick_image(PickedPathCallback callback) override
|
||||
{
|
||||
if (bridge_.pick_file)
|
||||
bridge_.pick_file(std::move(callback));
|
||||
}
|
||||
|
||||
void pick_file(std::vector<std::string> file_types, PickedPathCallback callback) override
|
||||
{
|
||||
(void)file_types;
|
||||
if (bridge_.pick_file)
|
||||
bridge_.pick_file(std::move(callback));
|
||||
}
|
||||
|
||||
void pick_save_file(std::vector<std::string> file_types, PickedPathCallback callback) override
|
||||
{
|
||||
(void)file_types;
|
||||
if (bridge_.pick_save_file)
|
||||
bridge_.pick_save_file(std::move(callback));
|
||||
}
|
||||
|
||||
void pick_directory(PickedPathCallback callback) override
|
||||
{
|
||||
(void)callback;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool supports_working_directory_picker() override
|
||||
{
|
||||
return platform_supports_working_directory_picker(kPlatformFamily);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string format_working_directory_path(std::string_view path) override
|
||||
{
|
||||
return std::string(path);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool uses_prepared_file_writes() override
|
||||
{
|
||||
return platform_uses_prepared_file_writes(kPlatformFamily);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool uses_work_directory_document_export_collections() override
|
||||
{
|
||||
return platform_uses_work_directory_document_export_collections(kPlatformFamily);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool disables_network_tls_verification() override
|
||||
{
|
||||
return default_disables_network_tls_verification();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool uses_ppbr_export_data_directory_override() override
|
||||
{
|
||||
return platform_uses_ppbr_export_data_directory_override(kPlatformFamily);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool supports_sonarpen() override
|
||||
{
|
||||
return platform_supports_sonarpen(kPlatformFamily);
|
||||
}
|
||||
|
||||
void start_sonarpen() override
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] int default_canvas_resolution() override
|
||||
{
|
||||
return platform_default_canvas_resolution(kPlatformFamily);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool draws_canvas_tip_for_pointer(
|
||||
bool is_mouse,
|
||||
bool is_stylus,
|
||||
bool is_left_button_release) override
|
||||
{
|
||||
return platform_draws_canvas_tip_for_pointer(
|
||||
kPlatformFamily,
|
||||
is_mouse,
|
||||
is_stylus,
|
||||
is_left_button_release);
|
||||
}
|
||||
|
||||
[[nodiscard]] float adjust_canvas_input_pressure(float pressure) override
|
||||
{
|
||||
return pressure;
|
||||
}
|
||||
|
||||
[[nodiscard]] PreparedFileTarget prepare_writable_file(
|
||||
std::string_view type,
|
||||
std::string_view default_name,
|
||||
std::string_view data_path,
|
||||
std::string_view temporary_path) override
|
||||
{
|
||||
return plan_platform_writable_file(
|
||||
kPlatformFamily,
|
||||
type,
|
||||
default_name,
|
||||
data_path,
|
||||
temporary_path);
|
||||
}
|
||||
|
||||
void save_prepared_file(
|
||||
std::string_view path,
|
||||
std::string_view suggested_name,
|
||||
PreparedFileCallback callback) override
|
||||
{
|
||||
(void)suggested_name;
|
||||
callback(std::string(path), false);
|
||||
}
|
||||
|
||||
private:
|
||||
AndroidPlatformBridge bridge_;
|
||||
std::shared_ptr<pp::platform::PlatformStoragePaths> storage_paths_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<pp::platform::PlatformServices> create_platform_services(
|
||||
AndroidPlatformServicesConfig config)
|
||||
{
|
||||
return std::make_unique<AndroidPlatformServices>(std::move(config));
|
||||
}
|
||||
|
||||
}
|
||||
32
src/platform_android/android_platform_services.h
Normal file
32
src/platform_android/android_platform_services.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "platform_api/platform_services.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
namespace pp::platform::android {
|
||||
|
||||
struct AndroidPlatformBridge {
|
||||
std::function<std::string()> clipboard_text;
|
||||
std::function<bool(std::string_view text)> set_clipboard_text;
|
||||
std::function<void(bool visible)> set_virtual_keyboard_visible;
|
||||
std::function<void()> attach_ui_thread;
|
||||
std::function<void()> detach_ui_thread;
|
||||
std::function<void()> acquire_render_context;
|
||||
std::function<void()> release_render_context;
|
||||
std::function<void()> present_render_context;
|
||||
std::function<void(PickedPathCallback callback)> pick_file;
|
||||
std::function<void(PickedPathCallback callback)> pick_save_file;
|
||||
};
|
||||
|
||||
struct AndroidPlatformServicesConfig {
|
||||
AndroidPlatformBridge bridge;
|
||||
std::shared_ptr<pp::platform::PlatformStoragePaths> storage_paths;
|
||||
};
|
||||
|
||||
[[nodiscard]] std::unique_ptr<pp::platform::PlatformServices> create_platform_services(
|
||||
AndroidPlatformServicesConfig config = {});
|
||||
|
||||
}
|
||||
@@ -9,9 +9,7 @@
|
||||
#include "platform_api/platform_policy.h"
|
||||
#include "renderer_gl/opengl_capabilities.h"
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include "main.h"
|
||||
#elif __LINUX__
|
||||
#ifdef __LINUX__
|
||||
#include <tinyfiledialogs.h>
|
||||
#include "platform_linux/linux_platform_services.h"
|
||||
std::string linux_home_path();
|
||||
@@ -33,10 +31,6 @@ class LegacyPlatformServices final : public pp::platform::PlatformServices {
|
||||
public:
|
||||
explicit LegacyPlatformServices(pp::platform::legacy::LegacyPlatformServicesConfig config = {})
|
||||
: glfw_shell_(std::move(config.glfw_shell))
|
||||
, android_bridge_(std::move(config.android_bridge))
|
||||
#ifdef __ANDROID__
|
||||
, android_storage_paths_(std::move(config.android_storage_paths))
|
||||
#endif
|
||||
, web_platform_services_(config.web_platform_services)
|
||||
{
|
||||
}
|
||||
@@ -72,8 +66,6 @@ public:
|
||||
data_path + "/frames",
|
||||
{},
|
||||
};
|
||||
#elif defined(__ANDROID__)
|
||||
return android_storage_paths_ ? *android_storage_paths_ : pp::platform::PlatformStoragePaths {};
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
@@ -85,34 +77,18 @@ public:
|
||||
|
||||
void trigger_crash_test() override
|
||||
{
|
||||
#if defined(__ANDROID__)
|
||||
int *x = nullptr; *x = 42;
|
||||
LOG("%d", *x);
|
||||
#endif
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string clipboard_text() override
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
if (android_bridge_.clipboard_text)
|
||||
return android_bridge_.clipboard_text();
|
||||
return {};
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
[[nodiscard]] bool set_clipboard_text(std::string_view text) override
|
||||
{
|
||||
const std::string value(text);
|
||||
#ifdef __ANDROID__
|
||||
if (android_bridge_.set_clipboard_text)
|
||||
return android_bridge_.set_clipboard_text(value);
|
||||
return false;
|
||||
#else
|
||||
(void)value;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_cursor_visible(bool visible) override
|
||||
@@ -122,54 +98,31 @@ public:
|
||||
|
||||
void set_virtual_keyboard_visible(bool visible) override
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
if (android_bridge_.set_virtual_keyboard_visible)
|
||||
android_bridge_.set_virtual_keyboard_visible(visible);
|
||||
#else
|
||||
(void)visible;
|
||||
#endif
|
||||
}
|
||||
|
||||
void attach_ui_thread() override
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
if (android_bridge_.attach_ui_thread)
|
||||
android_bridge_.attach_ui_thread();
|
||||
#endif
|
||||
}
|
||||
|
||||
void detach_ui_thread() override
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
if (android_bridge_.detach_ui_thread)
|
||||
android_bridge_.detach_ui_thread();
|
||||
#endif
|
||||
}
|
||||
|
||||
void acquire_render_context() override
|
||||
{
|
||||
#if __ANDROID__
|
||||
if (android_bridge_.acquire_render_context)
|
||||
android_bridge_.acquire_render_context();
|
||||
#elif __LINUX__ || __WEB__
|
||||
#if __LINUX__ || __WEB__
|
||||
invoke_legacy_glfw_shell_callback(glfw_shell_.acquire_render_context);
|
||||
#endif
|
||||
}
|
||||
|
||||
void release_render_context() override
|
||||
{
|
||||
#if __ANDROID__
|
||||
if (android_bridge_.release_render_context)
|
||||
android_bridge_.release_render_context();
|
||||
#endif
|
||||
}
|
||||
|
||||
void present_render_context() override
|
||||
{
|
||||
#if __ANDROID__
|
||||
if (android_bridge_.present_render_context)
|
||||
android_bridge_.present_render_context();
|
||||
#elif __LINUX__ || __WEB__
|
||||
#if __LINUX__ || __WEB__
|
||||
invoke_legacy_glfw_shell_callback(glfw_shell_.present_render_context);
|
||||
#endif
|
||||
}
|
||||
@@ -279,10 +232,7 @@ public:
|
||||
|
||||
void pick_image(pp::platform::PickedPathCallback callback) override
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
if (android_bridge_.pick_file)
|
||||
android_bridge_.pick_file(std::move(callback));
|
||||
#elif __LINUX__
|
||||
#ifdef __LINUX__
|
||||
if (auto p = tinyfd_openFileDialog("Open File", "", 0, nullptr, nullptr, false))
|
||||
invoke_picked_path_if_selected(p, callback);
|
||||
#elif __WEB__
|
||||
@@ -294,10 +244,7 @@ public:
|
||||
|
||||
void pick_file(std::vector<std::string> file_types, pp::platform::PickedPathCallback callback) override
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
if (android_bridge_.pick_file)
|
||||
android_bridge_.pick_file(std::move(callback));
|
||||
#elif __LINUX__
|
||||
#ifdef __LINUX__
|
||||
if (auto p = tinyfd_openFileDialog("Open File", "", 0, nullptr, nullptr, false))
|
||||
invoke_picked_path_if_selected(p, callback);
|
||||
#elif __WEB__
|
||||
@@ -310,13 +257,8 @@ public:
|
||||
|
||||
void pick_save_file(std::vector<std::string> file_types, pp::platform::PickedPathCallback callback) override
|
||||
{
|
||||
#if __ANDROID__
|
||||
if (android_bridge_.pick_save_file)
|
||||
android_bridge_.pick_save_file(std::move(callback));
|
||||
#else
|
||||
(void)file_types;
|
||||
(void)callback;
|
||||
#endif
|
||||
}
|
||||
|
||||
void pick_directory(pp::platform::PickedPathCallback callback) override
|
||||
@@ -472,10 +414,6 @@ private:
|
||||
}
|
||||
|
||||
pp::platform::legacy::LegacyGlfwPlatformShell glfw_shell_;
|
||||
pp::platform::legacy::LegacyAndroidPlatformBridge android_bridge_;
|
||||
#ifdef __ANDROID__
|
||||
std::shared_ptr<pp::platform::PlatformStoragePaths> android_storage_paths_;
|
||||
#endif
|
||||
pp::platform::WebPlatformServices* web_platform_services_ = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -13,23 +13,8 @@ struct LegacyGlfwPlatformShell {
|
||||
std::function<void()> request_app_close;
|
||||
};
|
||||
|
||||
struct LegacyAndroidPlatformBridge {
|
||||
std::function<std::string()> clipboard_text;
|
||||
std::function<bool(std::string_view text)> set_clipboard_text;
|
||||
std::function<void(bool visible)> set_virtual_keyboard_visible;
|
||||
std::function<void()> attach_ui_thread;
|
||||
std::function<void()> detach_ui_thread;
|
||||
std::function<void()> acquire_render_context;
|
||||
std::function<void()> release_render_context;
|
||||
std::function<void()> present_render_context;
|
||||
std::function<void(PickedPathCallback callback)> pick_file;
|
||||
std::function<void(PickedPathCallback callback)> pick_save_file;
|
||||
};
|
||||
|
||||
struct LegacyPlatformServicesConfig {
|
||||
LegacyGlfwPlatformShell glfw_shell;
|
||||
LegacyAndroidPlatformBridge android_bridge;
|
||||
std::shared_ptr<pp::platform::PlatformStoragePaths> android_storage_paths;
|
||||
pp::platform::WebPlatformServices* web_platform_services = nullptr;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user