Skip to content

Commit

Permalink
enhancement: share server state and root window texture with MainActi…
Browse files Browse the repository at this point in the history
…vity

This change is needed to move renderer from X server process to activity's JVM process.
  • Loading branch information
twaik committed Dec 23, 2024
1 parent d239d79 commit fdd8c31
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/src/main/cpp/lorie/InitOutput.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ __attribute((constructor())) static void initSharedServerState() {
pthread_mutex_init(&lorieScreen.state->lock, &mutex_attr);
}

void lorieActivityConnected(void) {
lorieSendSharedServerState(pvfb->stateFd);
lorieSendRootWindowBuffer(pvfb->root.buffer);
}

static Bool TrueNoop() { return TRUE; }
static Bool FalseNoop() { return FALSE; }
static void VoidNoop() {}
Expand Down Expand Up @@ -459,6 +464,7 @@ static void lorieUpdateBuffer(void) {
pScreenPtr->ModifyPixmapHeader(pScreenPtr->devPrivate, d0.width, d0.height, 32, 32, d0.stride * 4, data0);

renderer_set_buffer(pvfb->env, new);
lorieSendRootWindowBuffer(pvfb->root.buffer);
}

if (old) {
Expand Down Expand Up @@ -534,8 +540,12 @@ static Bool lorieRedraw(__unused ClientPtr pClient, __unused void *closure) {
redrawn = renderer_redraw(pvfb->env, pvfb->root.flip);
if (loriePixmapLock(pScreenPtr->devPrivate) && redrawn)
DamageEmpty(pvfb->damage);
} else if (pvfb->cursorMoved)
if (redrawn)
lorieRequestRender();
} else if (pvfb->cursorMoved) {
renderer_redraw(pvfb->env, pvfb->root.flip);
lorieRequestRender();
}

pvfb->cursorMoved = FALSE;
return TRUE;
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/cpp/lorie/activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <errno.h>
#include <jni.h>
#include <wchar.h>
Expand All @@ -30,6 +31,9 @@ static struct {
jmethodID toString;
} CharBuffer = {0};

static struct lorie_shared_server_state* state = NULL;
static AHardwareBuffer* sharedBuffer = NULL;

static jclass FindClassOrDie(JNIEnv *env, const char* name) {
jclass clazz = (*env)->FindClass(env, name);
if (!clazz) {
Expand Down Expand Up @@ -124,6 +128,27 @@ Java_com_termux_x11_LorieView_handleXEvents(JNIEnv *env, jobject thiz) {
(*env)->CallVoidMethod(env, thiz, (*env)->GetMethodID(env, (*env)->GetObjectClass(env, thiz), "requestClipboard", "()V"));
break;
}
case EVENT_SHARED_SERVER_STATE: {
int fd = ancil_recv_fd(conn_fd);

if (!(state = mmap(NULL, sizeof(*state), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)))
log(ERROR, "Failed to map server state.");

close(fd); // Closing file descriptor does not unmmap shared memory fragment.
break;
}
case EVENT_SHARED_ROOT_WINDOW_BUFFER: {
if (sharedBuffer)
AHardwareBuffer_release(sharedBuffer);
int error = AHardwareBuffer_recvHandleFromUnixSocket(conn_fd, &sharedBuffer);
AHardwareBuffer_Desc desc = {0};
if (sharedBuffer)
AHardwareBuffer_describe(sharedBuffer, &desc);
log(INFO, "Received shared buffer width %d height %d format %d", desc.width, desc.height, desc.format);
}
case EVENT_REQUEST_RENDER: {
// Currently not implemented, renderer is still running in X server process.
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions app/src/main/cpp/lorie/cmdentrypoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,33 @@ void lorieRequestClipboard(void) {
static Bool addFd(__unused ClientPtr pClient, void *closure) {
InputThreadRegisterDev((int) (int64_t) closure, handleLorieEvents, NULL);
conn_fd = (int) (int64_t) closure;
lorieActivityConnected();
return TRUE;
}

void lorieSendSharedServerState(int memfd) {
if (conn_fd != -1) {
lorieEvent e = { .type = EVENT_SHARED_SERVER_STATE };
write(conn_fd, &e, sizeof(e));
ancil_send_fd(conn_fd, memfd);
}
}

void lorieSendRootWindowBuffer(AHardwareBuffer* buffer) {
if (conn_fd != -1 && buffer) {
lorieEvent e = { .type = EVENT_SHARED_ROOT_WINDOW_BUFFER };
write(conn_fd, &e, sizeof(e));
AHardwareBuffer_sendHandleToUnixSocket(buffer, conn_fd);
}
}

void lorieRequestRender(void) {
if (conn_fd != -1) {
lorieEvent e = { .type = EVENT_REQUEST_RENDER };
write(conn_fd, &e, sizeof(e));
}
}

JNIEXPORT jobject JNICALL
Java_com_termux_x11_CmdEntryPoint_getXConnection(JNIEnv *env, __unused jobject cls) {
int client[2];
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/cpp/lorie/lorie.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <android/hardware_buffer.h>
#include <android/choreographer.h>
#include <android/log.h>

Expand All @@ -23,8 +24,16 @@ Bool lorieInitDri3(ScreenPtr pScreen);
void lorieSetStylusEnabled(Bool enabled);
void lorieTriggerWorkingQueue(void);
void lorieChoreographerFrameCallback(__unused long t, AChoreographer* d);
void lorieActivityConnected(void);
void lorieSendSharedServerState(int memfd);
void lorieSendRootWindowBuffer(AHardwareBuffer* buffer);
void lorieRequestRender(void);

typedef enum {
EVENT_UNKNOWN,
EVENT_SHARED_SERVER_STATE,
EVENT_SHARED_ROOT_WINDOW_BUFFER,
EVENT_REQUEST_RENDER,
EVENT_SCREEN_SIZE,
EVENT_TOUCH,
EVENT_MOUSE,
Expand Down

0 comments on commit fdd8c31

Please sign in to comment.