Commit b553f9ce authored by Ryan Pavlik's avatar Ryan Pavlik
Browse files

comp/android: WIP surface callback

parent cc567d8d
......@@ -11,8 +11,11 @@
#include "xrt/xrt_compiler.h"
#include "xrt/xrt_config_build.h"
#include "xrt/xrt_instance.h"
#include "xrt/xrt_android.h"
#include "util/u_misc.h"
#include "os/os_threading.h"
#include "android/android_globals.h"
#include "android/android_custom_surface.h"
......@@ -43,6 +46,23 @@ struct comp_window_android
{
struct comp_target_swapchain base;
uint32_t width;
uint32_t height;
VkFormat color_format;
VkColorSpaceKHR color_space;
VkPresentModeKHR present_mode;
void (*real_create_images)(struct comp_target *ct,
uint32_t preferred_width,
uint32_t preferred_height,
VkFormat preferred_color_format,
VkColorSpaceKHR preferred_color_space,
VkPresentModeKHR present_mode);
bool needs_create_images;
ANativeWindow *native_window;
struct os_mutex surface_mutex;
struct android_custom_surface *custom_surface;
};
......@@ -87,17 +107,6 @@ comp_window_android_init_pre_vulkan(struct comp_target *ct)
return true;
}
static void
comp_window_android_destroy(struct comp_target *ct)
{
struct comp_window_android *cwa = (struct comp_window_android *)ct;
comp_target_swapchain_cleanup(&cwa->base);
android_custom_surface_destroy(&cwa->custom_surface);
free(ct);
}
static void
comp_window_android_update_window_title(struct comp_target *ct, const char *title)
......@@ -138,6 +147,9 @@ comp_window_android_init_post_vulkan(struct comp_target *ct, uint32_t width, uin
struct comp_window_android *cwa = (struct comp_window_android *)ct;
VkResult ret;
cwa->width = width;
cwa->height = height;
#ifdef XRT_FEATURE_SERVICE
/* Out of process: Getting cached surface */
ANativeWindow *window = (ANativeWindow *)android_globals_get_window();
......@@ -165,21 +177,117 @@ comp_window_android_flush(struct comp_target *ct)
(void)ct;
}
static void
comp_window_android_create_images_stub(struct comp_target *ct,
uint32_t width,
uint32_t height,
VkFormat color_format,
VkColorSpaceKHR color_space,
VkPresentModeKHR present_mode)
{
struct comp_window_android *cwa = (struct comp_window_android *)ct;
if (cwa->native_window != NULL) {
cwa->real_create_images(ct, width, height, color_format, color_space, present_mode);
return;
}
cwa->width = width;
cwa->height = height;
cwa->color_format = color_format;
cwa->color_space = color_space;
cwa->present_mode = present_mode;
cwa->needs_create_images = true;
}
static bool
comp_window_android_handle_surface_acquired(struct xrt_instance *xinst,
struct _ANativeWindow *window,
enum xrt_android_surface_event event,
void *userdata)
{
struct comp_window_android *cwa = (struct comp_window_android *)userdata;
COMP_INFO(cwa->base.base.c, "comp_window_android_handle_surface_acquired: got a surface!");
if (cwa->native_window == NULL) {
cwa->native_window = (ANativeWindow *)window;
VkResult ret = comp_window_android_create_surface(cwa, cwa->native_window, &cwa->base.surface.handle);
if (ret != VK_SUCCESS) {
COMP_ERROR(cwa->base.base.c, "Failed to create surface '%s'!", vk_result_string(ret));
return true;
}
if (cwa->needs_create_images) {
cwa->needs_create_images = false;
cwa->real_create_images(&cwa->base.base, cwa->width, cwa->height, cwa->color_format,
cwa->color_space, cwa->present_mode);
}
}
return true;
}
static bool
comp_window_android_handle_surface_lost(struct xrt_instance *xinst,
struct _ANativeWindow *window,
enum xrt_android_surface_event event,
void *userdata)
{
struct comp_window_android *cwa = (struct comp_window_android *)userdata;
COMP_INFO(cwa->base.base.c, "comp_window_android_handle_surface_lost: oh noes!");
if (cwa->native_window == (ANativeWindow *)window) {
// yeah, we're losing this surface.
os_mutex_lock(&cwa->surface_mutex);
comp_target_swapchain_cleanup(&cwa->base);
cwa->native_window = NULL;
os_mutex_unlock(&cwa->surface_mutex);
}
return true;
}
static void
comp_window_android_destroy(struct comp_target *ct)
{
struct comp_window_android *cwa = (struct comp_window_android *)ct;
struct xrt_instance *xinst = cwa->base.base.c->xinst;
xinst->android_instance->remove_surface_callback(xinst, comp_window_android_handle_surface_acquired,
XRT_ANDROID_SURFACE_EVENT_ACQUIRED, (void *)cwa);
xinst->android_instance->remove_surface_callback(xinst, comp_window_android_handle_surface_lost,
XRT_ANDROID_SURFACE_EVENT_LOST, (void *)cwa);
os_mutex_destroy(&cwa->surface_mutex);
comp_target_swapchain_cleanup(&cwa->base);
android_custom_surface_destroy(&cwa->custom_surface);
free(ct);
}
struct comp_target *
comp_window_android_create(struct comp_compositor *c)
{
struct comp_window_android *w = U_TYPED_CALLOC(struct comp_window_android);
struct comp_window_android *cwa = U_TYPED_CALLOC(struct comp_window_android);
// The display timing code hasn't been tested on Android and may be broken.
comp_target_swapchain_init_and_set_fnptrs(&w->base, COMP_TARGET_FORCE_FAKE_DISPLAY_TIMING);
comp_target_swapchain_init_and_set_fnptrs(&cwa->base, COMP_TARGET_FORCE_FAKE_DISPLAY_TIMING);
cwa->base.base.name = "Android";
cwa->base.base.destroy = comp_window_android_destroy;
cwa->base.base.flush = comp_window_android_flush;
cwa->base.base.init_pre_vulkan = comp_window_android_init_pre_vulkan;
cwa->base.base.init_post_vulkan = comp_window_android_init_post_vulkan;
cwa->base.base.set_title = comp_window_android_update_window_title;
cwa->base.base.c = c;
// Intercept this call
cwa->real_create_images = cwa->base.base.create_images;
cwa->base.base.create_images = comp_window_android_create_images_stub;
os_mutex_init(&cwa->surface_mutex);
w->base.base.name = "Android";
w->base.base.destroy = comp_window_android_destroy;
w->base.base.flush = comp_window_android_flush;
w->base.base.init_pre_vulkan = comp_window_android_init_pre_vulkan;
w->base.base.init_post_vulkan = comp_window_android_init_post_vulkan;
w->base.base.set_title = comp_window_android_update_window_title;
w->base.base.c = c;
struct xrt_instance *xinst = c->xinst;
xinst->android_instance->register_surface_callback(xinst, comp_window_android_handle_surface_acquired,
XRT_ANDROID_SURFACE_EVENT_ACQUIRED, (void *)cwa);
xinst->android_instance->register_surface_callback(xinst, comp_window_android_handle_surface_lost,
XRT_ANDROID_SURFACE_EVENT_LOST, (void *)cwa);
return &w->base.base;
return &cwa->base.base;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment