Skip to content

Commit c86f3ed

Browse files
committed
small refactoring to improve readability and fixing cursor flicker when no buffer available
1 parent 32bb11d commit c86f3ed

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

app/src/main/cpp/lorie/InitOutput.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern DeviceIntPtr lorieMouse, lorieKeyboard;
4848
#define CREATE_PIXMAP_USAGE_LORIEBUFFER_BACKED 5
4949

5050
struct vblank {
51-
struct xorg_list list;
51+
struct xorg_list link;
5252
uint64_t id, msc;
5353
};
5454

@@ -99,6 +99,8 @@ typedef struct {
9999
void *mem;
100100
} LoriePixmapPriv;
101101

102+
#define LORIE_BUFFER_FROM_PIXMAP(pixmap) (pixmap ? ((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pixmap))->buffer : NULL)
103+
102104
void OsVendorInit(void) {
103105
pthread_mutexattr_t mutex_attr;
104106
pthread_condattr_t cond_attr;
@@ -128,8 +130,9 @@ void OsVendorInit(void) {
128130
}
129131

130132
void lorieActivityConnected(void) {
133+
pvfb->state->drawRequested = pvfb->state->cursor.updated = true;
131134
lorieSendSharedServerState(pvfb->stateFd);
132-
lorieSendRootWindowBuffer(((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pScreenPtr->devPrivate))->buffer);
135+
lorieSendRootWindowBuffer(LORIE_BUFFER_FROM_PIXMAP(pScreenPtr->devPrivate));
133136
}
134137

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

407-
if (nonEmpty && priv && priv->buffer) {
410+
if (!priv)
411+
// Impossible situation, but let's skip this step
412+
return TRUE;
413+
414+
if (nonEmpty && priv->buffer) {
408415
// We should unlock and lock buffer in order to update texture content on some devices
409416
// In most cases AHardwareBuffer uses DMA memory which is shared between CPU and GPU
410417
// and this is not needed. But according to docs we should do it for any case.
@@ -420,6 +427,8 @@ static Bool lorieRedraw(__unused ClientPtr pClient, __unused void *closure) {
420427
}
421428

422429
if (pvfb->state->drawRequested || pvfb->state->cursor.moved || pvfb->state->cursor.updated) {
430+
pvfb->state->rootWindowTextureID = LorieBuffer_description(priv->buffer)->id;
431+
423432
// Sending signal about pending root window changes to renderer thread.
424433
// We do not explicitly lock the pvfb->state->lock here because we do not want to wait
425434
// for all drawing operations to be finished.
@@ -448,7 +457,7 @@ static Bool lorieCreateScreenResources(ScreenPtr pScreen) {
448457
DamageRegister(&(*pScreen->GetScreenPixmap)(pScreen)->drawable, pvfb->damage);
449458
pvfb->fpsTimer = TimerSet(NULL, 0, 5000, lorieFramecounter, pScreen);
450459

451-
lorieSendRootWindowBuffer(((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pScreen->devPrivate))->buffer);
460+
lorieSendRootWindowBuffer(LORIE_BUFFER_FROM_PIXMAP(pScreen->devPrivate));
452461

453462
return TRUE;
454463
}
@@ -508,7 +517,7 @@ static Bool lorieRRScreenSetSize(ScreenPtr pScreen, CARD16 width, CARD16 height,
508517
pScreen->DestroyPixmap(oldPixmap);
509518
}
510519

511-
lorieSendRootWindowBuffer(((LoriePixmapPriv*) exaGetPixmapDriverPrivate(pScreen->devPrivate))->buffer);
520+
lorieSendRootWindowBuffer(LORIE_BUFFER_FROM_PIXMAP(pScreen->devPrivate));
512521

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

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

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

711-
xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, list) {
720+
xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, link) {
712721
if (vblank->id == id) {
713-
xorg_list_del(&vblank->list);
722+
xorg_list_del(&vblank->link);
714723
free (vblank);
715724
break;
716725
}
@@ -722,11 +731,11 @@ static void loriePerformVblanks(void) {
722731
uint64_t ust, msc;
723732
pvfb->current_msc++;
724733

725-
xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, list) {
734+
xorg_list_for_each_entry_safe(vblank, tmp, &pvfb->vblank_queue, link) {
726735
if (vblank->msc <= pvfb->current_msc) {
727736
loriePresentGetUstMsc(NULL, &ust, &msc);
728737
present_event_notify(vblank->id, ust, msc);
729-
xorg_list_del(&vblank->list);
738+
xorg_list_del(&vblank->link);
730739
free (vblank);
731740
}
732741
}

app/src/main/cpp/lorie/activity.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ static void connect_(__unused JNIEnv* env, __unused jobject cls, jint fd) {
216216
if (conn_fd != -1) {
217217
ALooper_removeFd(ALooper_forThread(), conn_fd);
218218
close(conn_fd);
219+
rendererSetSharedState(NULL);
220+
rendererSetBuffer(NULL);
221+
log(DEBUG, "disconnected");
219222
}
220223

221224
if ((conn_fd = fd) != -1) {

app/src/main/cpp/lorie/lorie.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void lorieSendSharedServerState(int memfd);
3535
void lorieSendRootWindowBuffer(LorieBuffer* buffer);
3636
bool lorieConnectionAlive(void);
3737

38-
__unused int rendererInit(JNIEnv* env);
38+
__unused void rendererInit(JNIEnv* env);
3939
__unused void rendererTestCapabilities(int* legacy_drawing, uint8_t* flip);
4040
__unused void rendererSetBuffer(LorieBuffer* buf);
4141
__unused void rendererSetWindow(ANativeWindow* newWin);
@@ -162,6 +162,9 @@ struct lorie_shared_server_state {
162162
*/
163163
pthread_cond_t cond; // initialized at X server side.
164164

165+
/* ID of root window texture to be drawn. */
166+
unsigned long rootWindowTextureID;
167+
165168
/* A signal to renderer to update root window texture content from shared fragment if needed */
166169
volatile uint8_t drawRequested;
167170

app/src/main/cpp/lorie/renderer.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ int rendererInitThread(JavaVM *vm) {
229229
return 1;
230230
}
231231

232-
int rendererInit(JNIEnv* env) {
232+
void rendererInit(JNIEnv* env) {
233233
pthread_t t;
234234
JavaVM *vm;
235235

236236
if (ctx)
237-
return 1;
237+
return;
238238

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

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

245245
pthread_create(&t, NULL, (void*(*)(void*)) rendererInitThread, vm);
246-
return 1;
247246
}
248247

249248
void rendererTestCapabilities(int* legacy_drawing, uint8_t* flip) {
@@ -508,12 +507,15 @@ static void rendererRenewImage(void) {
508507
void rendererRedrawLocked(JNIEnv* env) {
509508
EGLSync fence;
510509

510+
if (!buffer)
511+
return;
512+
511513
// We should signal X server to not use root window while we actively copy it
512514
lorie_mutex_lock(&state->lock, &state->lockingPid);
513515
state->drawRequested = FALSE;
514516

515517
LorieBuffer_bindTexture(buffer);
516-
draw(0, -1.f, -1.f, 1.f, 1.f, LorieBuffer_isRgba(buffer));
518+
draw(0, -1.f, -1.f, 1.f, 1.f, LorieBuffer_isRgba(buffer));
517519
fence = eglCreateSyncKHR(egl_display, EGL_SYNC_FENCE_KHR, NULL);
518520
glFlush();
519521

0 commit comments

Comments
 (0)