Skip to content

Commit

Permalink
small refactoring to improve readability and fixing cursor flicker wh…
Browse files Browse the repository at this point in the history
…en no buffer available
  • Loading branch information
twaik committed Feb 12, 2025
1 parent 32bb11d commit c86f3ed
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
29 changes: 19 additions & 10 deletions app/src/main/cpp/lorie/InitOutput.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern DeviceIntPtr lorieMouse, lorieKeyboard;
#define CREATE_PIXMAP_USAGE_LORIEBUFFER_BACKED 5

struct vblank {
struct xorg_list list;
struct xorg_list link;
uint64_t id, msc;
};

Expand Down Expand Up @@ -99,6 +99,8 @@ typedef struct {
void *mem;
} LoriePixmapPriv;

#define LORIE_BUFFER_FROM_PIXMAP(pixmap) (pixmap ? ((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pixmap))->buffer : NULL)

void OsVendorInit(void) {
pthread_mutexattr_t mutex_attr;
pthread_condattr_t cond_attr;
Expand Down Expand Up @@ -128,8 +130,9 @@ void OsVendorInit(void) {
}

void lorieActivityConnected(void) {
pvfb->state->drawRequested = pvfb->state->cursor.updated = true;
lorieSendSharedServerState(pvfb->stateFd);
lorieSendRootWindowBuffer(((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pScreenPtr->devPrivate))->buffer);
lorieSendRootWindowBuffer(LORIE_BUFFER_FROM_PIXMAP(pScreenPtr->devPrivate));
}

static LoriePixmapPriv* lorieRootWindowPixmapPriv(void) {
Expand Down Expand Up @@ -404,7 +407,11 @@ static Bool lorieRedraw(__unused ClientPtr pClient, __unused void *closure) {
nonEmpty = RegionNotEmpty(DamageRegion(pvfb->damage));
priv = lorieRootWindowPixmapPriv();

if (nonEmpty && priv && priv->buffer) {
if (!priv)
// Impossible situation, but let's skip this step
return TRUE;

if (nonEmpty && priv->buffer) {
// We should unlock and lock buffer in order to update texture content on some devices
// In most cases AHardwareBuffer uses DMA memory which is shared between CPU and GPU
// and this is not needed. But according to docs we should do it for any case.
Expand All @@ -420,6 +427,8 @@ static Bool lorieRedraw(__unused ClientPtr pClient, __unused void *closure) {
}

if (pvfb->state->drawRequested || pvfb->state->cursor.moved || pvfb->state->cursor.updated) {
pvfb->state->rootWindowTextureID = LorieBuffer_description(priv->buffer)->id;

// Sending signal about pending root window changes to renderer thread.
// We do not explicitly lock the pvfb->state->lock here because we do not want to wait
// for all drawing operations to be finished.
Expand Down Expand Up @@ -448,7 +457,7 @@ static Bool lorieCreateScreenResources(ScreenPtr pScreen) {
DamageRegister(&(*pScreen->GetScreenPixmap)(pScreen)->drawable, pvfb->damage);
pvfb->fpsTimer = TimerSet(NULL, 0, 5000, lorieFramecounter, pScreen);

lorieSendRootWindowBuffer(((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pScreen->devPrivate))->buffer);
lorieSendRootWindowBuffer(LORIE_BUFFER_FROM_PIXMAP(pScreen->devPrivate));

return TRUE;
}
Expand Down Expand Up @@ -508,7 +517,7 @@ static Bool lorieRRScreenSetSize(ScreenPtr pScreen, CARD16 width, CARD16 height,
pScreen->DestroyPixmap(oldPixmap);
}

lorieSendRootWindowBuffer(((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pScreen->devPrivate))->buffer);
lorieSendRootWindowBuffer(LORIE_BUFFER_FROM_PIXMAP(pScreen->devPrivate));

pScreen->ResizeWindow(pScreen->root, 0, 0, width, height, NULL);
RegionReset(&pScreen->root->winSize, &box);
Expand Down Expand Up @@ -699,7 +708,7 @@ static Bool loriePresentQueueVblank(__unused RRCrtcPtr crtc, uint64_t event_id,
return BadAlloc;

*vblank = (struct vblank) { .id = event_id, .msc = msc };
xorg_list_add(&vblank->list, &pvfb->vblank_queue);
xorg_list_add(&vblank->link, &pvfb->vblank_queue);

return Success;
#pragma clang diagnostic pop
Expand All @@ -708,9 +717,9 @@ static Bool loriePresentQueueVblank(__unused RRCrtcPtr crtc, uint64_t event_id,
static void loriePresentAbortVblank(__unused RRCrtcPtr crtc, uint64_t id, __unused uint64_t msc) {
struct vblank *vblank, *tmp;

xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, list) {
xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, link) {
if (vblank->id == id) {
xorg_list_del(&vblank->list);
xorg_list_del(&vblank->link);
free (vblank);
break;
}
Expand All @@ -722,11 +731,11 @@ static void loriePerformVblanks(void) {
uint64_t ust, msc;
pvfb->current_msc++;

xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, list) {
xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, link) {
if (vblank->msc <= pvfb->current_msc) {
loriePresentGetUstMsc(NULL, &ust, &msc);
present_event_notify(vblank->id, ust, msc);
xorg_list_del(&vblank->list);
xorg_list_del(&vblank->link);
free (vblank);
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/cpp/lorie/activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ static void connect_(__unused JNIEnv* env, __unused jobject cls, jint fd) {
if (conn_fd != -1) {
ALooper_removeFd(ALooper_forThread(), conn_fd);
close(conn_fd);
rendererSetSharedState(NULL);
rendererSetBuffer(NULL);
log(DEBUG, "disconnected");
}

if ((conn_fd = fd) != -1) {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/cpp/lorie/lorie.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void lorieSendSharedServerState(int memfd);
void lorieSendRootWindowBuffer(LorieBuffer* buffer);
bool lorieConnectionAlive(void);

__unused int rendererInit(JNIEnv* env);
__unused void rendererInit(JNIEnv* env);
__unused void rendererTestCapabilities(int* legacy_drawing, uint8_t* flip);
__unused void rendererSetBuffer(LorieBuffer* buf);
__unused void rendererSetWindow(ANativeWindow* newWin);
Expand Down Expand Up @@ -162,6 +162,9 @@ struct lorie_shared_server_state {
*/
pthread_cond_t cond; // initialized at X server side.

/* ID of root window texture to be drawn. */
unsigned long rootWindowTextureID;

/* A signal to renderer to update root window texture content from shared fragment if needed */
volatile uint8_t drawRequested;

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/cpp/lorie/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ int rendererInitThread(JavaVM *vm) {
return 1;
}

int rendererInit(JNIEnv* env) {
void rendererInit(JNIEnv* env) {
pthread_t t;
JavaVM *vm;

if (ctx)
return 1;
return;

(*env)->GetJavaVM(env, &vm);

Expand All @@ -243,7 +243,6 @@ int rendererInit(JNIEnv* env) {
pthread_cond_init(&stateChangeFinishCond, NULL);

pthread_create(&t, NULL, (void*(*)(void*)) rendererInitThread, vm);
return 1;
}

void rendererTestCapabilities(int* legacy_drawing, uint8_t* flip) {
Expand Down Expand Up @@ -508,12 +507,15 @@ static void rendererRenewImage(void) {
void rendererRedrawLocked(JNIEnv* env) {
EGLSync fence;

if (!buffer)
return;

// We should signal X server to not use root window while we actively copy it
lorie_mutex_lock(&state->lock, &state->lockingPid);
state->drawRequested = FALSE;

LorieBuffer_bindTexture(buffer);
draw(0, -1.f, -1.f, 1.f, 1.f, LorieBuffer_isRgba(buffer));
draw(0, -1.f, -1.f, 1.f, 1.f, LorieBuffer_isRgba(buffer));
fence = eglCreateSyncKHR(egl_display, EGL_SYNC_FENCE_KHR, NULL);
glFlush();

Expand Down

0 comments on commit c86f3ed

Please sign in to comment.