From b0f94e1c5cfb58071e8bf16b9f5ef9108d8d24ce Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 30 Apr 2025 19:06:06 +0200 Subject: [PATCH] dix: add CreateScreenResources callback mechanism Right now, extensions that need to be called after the CreateScreenResources proc had been run, must wrap the screen proc vector directly (all of them forming kind of daisy chain), and so - when called - temporarily restore the previous one, call it, wrap again, and if the call was successful finally doing it's own stuff. (same is done for many other procs) While that approach is looking nice and elegant on the drawing board, it's complicated, dangerous like a chainsaw and makes debugging hard, leading to pretty blurred API borders. Instead introducing a simple approach for letting extension hook into a post-CreateScreenResources callback list safely, w/o having to care much about side effects with the call chain. Extensions now can simply register their business logic and get called back - w/o ever having to mess with the ScreenRec's internal structures. Note that these hooks are executed *AFTER* the original CreateScreenResources() proc had been called SUCCESSFULLY (returned TRUE), so callees can rely on the DDX/driver had already done it's job. Signed-off-by: Enrico Weigelt, metux IT consult --- dix/dix_priv.h | 12 +++++++++++ dix/main.c | 6 ++---- dix/screen_hooks.c | 19 +++++++++++++++++ dix/screen_hooks_priv.h | 33 +++++++++++++++++++++++++++++ hw/xfree86/common/xf86platformBus.c | 3 +-- include/scrnintstr.h | 4 ++++ 6 files changed, 71 insertions(+), 6 deletions(-) diff --git a/dix/dix_priv.h b/dix/dix_priv.h index 1ba1fc451..8e38b8517 100644 --- a/dix/dix_priv.h +++ b/dix/dix_priv.h @@ -323,6 +323,18 @@ void dixScreenRaiseClose(ScreenPtr pScreen); */ void dixScreenRaisePixmapDestroy(PixmapPtr pPixmap); +/* + * @brief call screen's CreateScreenResources chain + * @see dixScreenHookPostCreateResources + * @param pScreen the screen to run on + * + * Call the screen's CreateScreenResources() proc, and if it returns TRUE, + * run the installed hooks afterwards. + * + * Should only be called by DIX itself. + */ +Bool dixScreenRaiseCreateResources(ScreenPtr pScreen); + /* * @brief mark event ID as critical * @param event the event to add to the critical events bitmap diff --git a/dix/main.c b/dix/main.c index 759b8a2a2..55eeda7de 100644 --- a/dix/main.c +++ b/dix/main.c @@ -210,8 +210,7 @@ dix_main(int argc, char *argv[], char *envp[]) ScreenPtr pScreen = screenInfo.gpuscreens[i]; if (!PixmapScreenInit(pScreen)) FatalError("failed to create screen pixmap properties"); - if (pScreen->CreateScreenResources && - !(*pScreen->CreateScreenResources) (pScreen)) + if (!dixScreenRaiseCreateResources(pScreen)) FatalError("failed to create screen resources"); } @@ -220,8 +219,7 @@ dix_main(int argc, char *argv[], char *envp[]) if (!PixmapScreenInit(pScreen)) FatalError("failed to create screen pixmap properties"); - if (pScreen->CreateScreenResources && - !(*pScreen->CreateScreenResources) (pScreen)) + if (!dixScreenRaiseCreateResources(pScreen)) FatalError("failed to create screen resources"); if (!CreateGCperDepth(i)) FatalError("failed to create scratch GCs"); diff --git a/dix/screen_hooks.c b/dix/screen_hooks.c index 64d43ac7e..1c96fa211 100644 --- a/dix/screen_hooks.c +++ b/dix/screen_hooks.c @@ -5,6 +5,8 @@ #include +#include + #include "dix/dix_priv.h" #include "dix/screen_hooks_priv.h" #include "include/dix.h" @@ -27,6 +29,8 @@ DECLARE_HOOK_PROC(WindowDestroy, hookWindowDestroy, XorgScreenWindowDestroyProcP DECLARE_HOOK_PROC(WindowPosition, hookWindowPosition, XorgScreenWindowPositionProcPtr); DECLARE_HOOK_PROC(Close, hookClose, XorgScreenCloseProcPtr); DECLARE_HOOK_PROC(PixmapDestroy, hookPixmapDestroy, XorgScreenPixmapDestroyProcPtr); +DECLARE_HOOK_PROC(PostCreateResources, hookPostCreateResources, + XorgScreenPostCreateResourcesProcPtr); int dixScreenRaiseWindowDestroy(WindowPtr pWin) { @@ -78,3 +82,18 @@ void dixScreenRaisePixmapDestroy(PixmapPtr pPixmap) CallCallbacks(&pScreen->hookPixmapDestroy, pPixmap); /* we must not call the original ScreenRec->DestroyPixmap() here */ } + +Bool dixScreenRaiseCreateResources(ScreenPtr pScreen) +{ + if (!pScreen) + return FALSE; + + if (pScreen->CreateScreenResources) { + if (!pScreen->CreateScreenResources(pScreen)) + return FALSE; + } + + Bool ret = TRUE; + CallCallbacks(&pScreen->hookPostCreateResources, &ret); + return ret; +} diff --git a/dix/screen_hooks_priv.h b/dix/screen_hooks_priv.h index 17754f062..01e4d8d9b 100644 --- a/dix/screen_hooks_priv.h +++ b/dix/screen_hooks_priv.h @@ -20,6 +20,7 @@ #ifndef XORG_DIX_SCREEN_HOOKS_H #define XORG_DIX_SCREEN_HOOKS_H +#include #include #include "include/callback.h" /* CallbackListPtr */ @@ -185,4 +186,36 @@ _X_EXPORT void dixScreenUnhookPixmapDestroy(ScreenPtr pScreen, XorgScreenPixmapDestroyProcPtr func); +/* prototype of screen close notification handler */ +typedef void (*XorgScreenPostCreateResourcesProcPtr)(CallbackListPtr *pcbl, + ScreenPtr pScreen, + Bool *ret); + +/** + * @brief register post-CreateScreenResources hook on the given screen + * + * @param pScreen pointer to the screen to register the notify hook into + * @param func pointer to the hook function + * @param arg opaque pointer passed to the hook + * + * This hook is called when CreateScreenResources() had been called + * and returned TRUE. + **/ +_X_EXPORT +void dixScreenHookPostCreateResources(ScreenPtr pScreen, + XorgScreenPostCreateResourcesProcPtr func); + +/** + * @brief unregister a post-CreateScreenResources hook on the given screen + * + * @param pScreen pointer to the screen to unregister the hook from + * @param func pointer to the hook function + * @param arg opaque pointer passed to the destructor + * + * @see dixScreenHookPostCreateResources + **/ +_X_EXPORT +void dixScreenUnhookPostCreateResources(ScreenPtr pScreen, + XorgScreenPostCreateResourcesProcPtr func); + #endif /* DIX_SCREEN_HOOKS_H */ diff --git a/hw/xfree86/common/xf86platformBus.c b/hw/xfree86/common/xf86platformBus.c index 8b499041b..b57620a58 100644 --- a/hw/xfree86/common/xf86platformBus.c +++ b/hw/xfree86/common/xf86platformBus.c @@ -725,8 +725,7 @@ xf86platformAddDevice(const char *driver_name, int index) PixmapScreenInit(xf86GPUScreens[i]->pScreen); - if (xf86GPUScreens[i]->pScreen->CreateScreenResources && - !(*xf86GPUScreens[i]->pScreen->CreateScreenResources) (xf86GPUScreens[i]->pScreen)) { + if (dixScreenRaiseCreateResources(xf86GPUScreens[i]->pScreen)) { RemoveGPUScreen(xf86GPUScreens[i]->pScreen); xf86DeleteScreen(xf86GPUScreens[i]); xf86UnclaimPlatformSlot(&xf86_platform_devices[index], NULL); diff --git a/include/scrnintstr.h b/include/scrnintstr.h index df7b2d9cf..ebd336bb2 100644 --- a/include/scrnintstr.h +++ b/include/scrnintstr.h @@ -683,6 +683,10 @@ typedef struct _Screen { /* additional pixmap destroy notify hooks (replaces wrapping DestroyPixmap) should NOT be touched outside of DIX core */ CallbackListPtr hookPixmapDestroy; + + /* hooks run right after SUCCESSFUL CreateScreenResources + should NOT be touched outside of DIX core */ + CallbackListPtr hookPostCreateResources; } ScreenRec; static inline RegionPtr