From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Geminux Project Date: Tue, 24 Nov 2026 10:25:00 +0200 Subject: [PATCH] geminux: CPU TearFree with direct 16x16 tiled rotation and shadow copy skip --- diff --git a/hw/xfree86/drivers/video/modesetting/driver.c b/hw/xfree86/drivers/video/modesetting/driver.c --- a/hw/xfree86/drivers/video/modesetting/driver.c +++ b/hw/xfree86/drivers/video/modesetting/driver.c @@ -1543,8 +1543,9 @@ if (pScrn->is_gpu) { xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "TearFree cannot synchronize PRIME; use 'PRIME Synchronization' instead\n"); - } else if (ms->drmmode.glamor_gbm) { + } else if (ms->drmmode.glamor_gbm || ms->drmmode.shadow_enable) { if (!ms->drmmode.pageflip || cap_universal_planes) { ms->drmmode.tearfree_enable = TRUE; - xf86DrvMsg(pScrn->scrnIndex, X_INFO, "TearFree: enabled\n"); + xf86DrvMsg(pScrn->scrnIndex, X_INFO, "TearFree: enabled%s\n", + ms->drmmode.glamor_gbm ? "" : " (CPU/ShadowFB)"); } else { @@ -1666,6 +1667,31 @@ { ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); modesettingPtr ms = modesettingPTR(pScrn); + xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn); + + if (xf86_config && xf86_config->num_crtc > 0) { + int enabled_crtcs = 0; + Bool all_direct_scanout = TRUE; + + for (int c = 0; c < xf86_config->num_crtc; c++) { + xf86CrtcPtr crtc = xf86_config->crtc[c]; + drmmode_crtc_private_ptr drmmode_crtc; + if (!crtc || !crtc->enabled) + continue; + enabled_crtcs++; + drmmode_crtc = crtc->driver_private; + if (!(drmmode_crtc && drmmode_crtc->tearfree.buf[0].px) && + (!crtc->transform_in_use || !crtc->rotatedPixmap)) { + all_direct_scanout = FALSE; + break; + } + } + if (enabled_crtcs > 0 && all_direct_scanout) { + DamageEmpty(pBuf->pDamage); + return; + } + } + bool use_3224 = ms->drmmode.force_24_32 && pScrn->bitsPerPixel == 32; if (ms->drmmode.shadow_enable2 && ms->drmmode.shadow_fb2) do { diff --git a/hw/xfree86/drivers/video/modesetting/drmmode_display.c b/hw/xfree86/drivers/video/modesetting/drmmode_display.c --- a/hw/xfree86/drivers/video/modesetting/drmmode_display.c +++ b/hw/xfree86/drivers/video/modesetting/drmmode_display.c @@ -1429,19 +1429,24 @@ /* Copy the screen's pixmap into the destination pixmap */ if (crtc->rotatedPixmap) { src = &crtc->rotatedPixmap->drawable; xf86RotateCrtcRedisplay(crtc, dst, src, dmg, FALSE); + } else if ((crtc->driverIsPerformingTransform & XF86DriverTransformOutput) && + crtc->transform_in_use) { + src = &pScreen->GetScreenPixmap(pScreen)->drawable; + xf86RotateCrtcRedisplay(crtc, dst, src, dmg, TRUE); } else { src = &pScreen->GetScreenPixmap(pScreen)->drawable; PixmapDirtyCopyArea(dst, src, 0, 0, -crtc->x, -crtc->y, dmg); } /* Reset the damages if requested */ if (empty) RegionEmpty(dmg); #ifdef GLAMOR /* Wait until the GC operations finish */ - modesettingPTR(crtc->scrn)->glamor.finish(pScreen); + if (modesettingPTR(crtc->scrn)->drmmode.glamor_gbm) + modesettingPTR(crtc->scrn)->glamor.finish(pScreen); #endif } @@ -1582,7 +1587,9 @@ crtc->x = x; crtc->y = y; crtc->rotation = rotation; + if (drmmode->tearfree_enable) + crtc->driverIsPerformingTransform |= XF86DriverTransformOutput; if (!xf86CrtcRotate(crtc)) { goto done; } @@ -2863,6 +2870,8 @@ drmmode_crtc->vblank_pipe = drmmode_crtc_vblank_pipe(num); xorg_list_init(&drmmode_crtc->mode_list); xorg_list_init(&drmmode_crtc->tearfree.dri_flip_list); drmmode_crtc->next_msc = UINT64_MAX; + if (drmmode->tearfree_enable) + crtc->driverIsPerformingTransform |= XF86DriverTransformOutput; /* Setup the fallback cursor immediately. */ @@ -4324,7 +4333,9 @@ crtc->mode = crtc->desiredMode; crtc->rotation = crtc->desiredRotation; crtc->x = crtc->desiredX; crtc->y = crtc->desiredY; + if (drmmode->tearfree_enable) + crtc->driverIsPerformingTransform |= XF86DriverTransformOutput; if (!xf86CrtcRotate(crtc)) return FALSE; } diff --git a/hw/xfree86/drivers/video/modesetting/pageflip.c b/hw/xfree86/drivers/video/modesetting/pageflip.c --- a/hw/xfree86/drivers/video/modesetting/pageflip.c +++ b/hw/xfree86/drivers/video/modesetting/pageflip.c @@ -414,6 +414,9 @@ return TRUE; } + if (!ms->drmmode.glamor_gbm) + goto error_free_event; + ms->glamor.block_handler(screen); new_front_bo = ms->glamor.gbm_bo_from_pixmap(screen, new_front); diff --git a/hw/xfree86/modes/xf86Rotate.c b/hw/xfree86/modes/xf86Rotate.c --- a/hw/xfree86/modes/xf86Rotate.c +++ b/hw/xfree86/modes/xf86Rotate.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,174 @@ #include "xf86Modes.h" #include "xf86RandR12.h" +static inline void +xf86RotateBlitRect32(Rotation rot, + const uint32_t *src_base, size_t src_stride32, + uint32_t *dst_base, size_t dst_stride32, + int crtc_x, int crtc_y, int W, int H, + int x1, int y1, int x2, int y2) +{ + if (rot == RR_Rotate_270) { + for (int ty = y1; ty < y2; ty += 16) { + int bh = (y2 - ty < 16) ? (y2 - ty) : 16; + for (int tx = x1; tx < x2; tx += 16) { + int bw = (x2 - tx < 16) ? (x2 - tx) : 16; + uint32_t tile[16][16]; + + for (int bx = 0; bx < bw; bx++) { + const uint32_t *srow = src_base + + (size_t)(crtc_y + (W - 1 - (tx + bx))) * src_stride32 + + (size_t)(crtc_x + ty); + for (int by = 0; by < bh; by++) + tile[by][bx] = srow[by]; + } + for (int by = 0; by < bh; by++) { + uint32_t *drow = dst_base + + (size_t)(ty + by) * dst_stride32 + (size_t)tx; + memcpy(drow, tile[by], (size_t)bw * sizeof(uint32_t)); + } + } + } + } else if (rot == RR_Rotate_90) { + for (int ty = y1; ty < y2; ty += 16) { + int bh = (y2 - ty < 16) ? (y2 - ty) : 16; + for (int tx = x1; tx < x2; tx += 16) { + int bw = (x2 - tx < 16) ? (x2 - tx) : 16; + uint32_t tile[16][16]; + + for (int bx = 0; bx < bw; bx++) { + const uint32_t *srow = src_base + + (size_t)(crtc_y + tx + bx) * src_stride32 + + (size_t)(crtc_x + H - ty - bh); + for (int k = 0; k < bh; k++) + tile[bh - 1 - k][bx] = srow[k]; + } + for (int by = 0; by < bh; by++) { + uint32_t *drow = dst_base + + (size_t)(ty + by) * dst_stride32 + (size_t)tx; + memcpy(drow, tile[by], (size_t)bw * sizeof(uint32_t)); + } + } + } + } else { + for (int dy = y1; dy < y2; dy++) { + const uint32_t *srow = src_base + + (size_t)(crtc_y + H - 1 - dy) * src_stride32 + + (size_t)(crtc_x + W - 1); + uint32_t *drow = dst_base + (size_t)dy * dst_stride32; + uint32_t rowbuf[64]; + + for (int tx = x1; tx < x2; tx += 64) { + int bw = (x2 - tx < 64) ? (x2 - tx) : 64; + for (int k = 0; k < bw; k++) + rowbuf[k] = srow[-(tx + k)]; + memcpy(drow + tx, rowbuf, (size_t)bw * sizeof(uint32_t)); + } + } + } +} + +static Bool +xf86RotateTryFast32(xf86CrtcPtr crtc, PixmapPtr dst_pixmap, + DrawableRec *src_drawable, RegionPtr region, + Bool transform_src) +{ + ScrnInfoPtr scrn = crtc->scrn; + ScreenPtr screen = scrn->pScreen; + Rotation rot = crtc->rotation & 0xf; + int W = crtc->mode.HDisplay; + int H = crtc->mode.VDisplay; + PixmapPtr src_pixmap; + const uint32_t *src_base; + uint32_t *dst_base; + size_t src_stride32, dst_stride32; + int n = RegionNumRects(region); + BoxPtr b = RegionRects(region); + + if (!transform_src || crtc->transformPresent || crtc->filter) + return FALSE; + if (crtc->rotation & (RR_Reflect_X | RR_Reflect_Y)) + return FALSE; + if (rot != RR_Rotate_270 && rot != RR_Rotate_90 && rot != RR_Rotate_180) + return FALSE; + if (W <= 0 || H <= 0) + return FALSE; + + if (src_drawable->type == DRAWABLE_PIXMAP) + src_pixmap = (PixmapPtr)src_drawable; + else + src_pixmap = (*screen->GetWindowPixmap)((WindowPtr)src_drawable); + + if (!src_pixmap || !dst_pixmap || + !src_pixmap->devPrivate.ptr || !dst_pixmap->devPrivate.ptr) + return FALSE; + if (src_pixmap->drawable.bitsPerPixel != 32 || + dst_pixmap->drawable.bitsPerPixel != 32) + return FALSE; + if ((src_pixmap->devKind & 3) != 0 || (dst_pixmap->devKind & 3) != 0) + return FALSE; + if (src_drawable->x != 0 || src_drawable->y != 0) + return FALSE; + if (src_drawable->type != DRAWABLE_PIXMAP && + (src_pixmap->screen_x != 0 || src_pixmap->screen_y != 0)) + return FALSE; + if (W > dst_pixmap->drawable.width || H > dst_pixmap->drawable.height) + return FALSE; + + if (rot == RR_Rotate_90 || rot == RR_Rotate_270) { + if (crtc->x < 0 || crtc->y < 0 || + crtc->x + H > src_pixmap->drawable.width || + crtc->y + W > src_pixmap->drawable.height) + return FALSE; + } else { + if (crtc->x < 0 || crtc->y < 0 || + crtc->x + W > src_pixmap->drawable.width || + crtc->y + H > src_pixmap->drawable.height) + return FALSE; + } + + if (screen->SourceValidate) + (*screen->SourceValidate)(src_drawable, 0, 0, + src_drawable->width, src_drawable->height, + IncludeInferiors); + + src_base = (const uint32_t *)src_pixmap->devPrivate.ptr; + dst_base = (uint32_t *)dst_pixmap->devPrivate.ptr; + src_stride32 = (size_t)src_pixmap->devKind >> 2; + dst_stride32 = (size_t)dst_pixmap->devKind >> 2; + + if (crtc->shadowClear) { + xf86RotateBlitRect32(rot, src_base, src_stride32, dst_base, dst_stride32, + crtc->x, crtc->y, W, H, 0, 0, W, H); + crtc->shadowClear = FALSE; + return TRUE; + } + + while (n--) { + BoxRec dst_box = *b++; + + pixman_f_transform_bounds(&crtc->f_framebuffer_to_crtc, &dst_box); + if (dst_box.x1 < 0) + dst_box.x1 = 0; + if (dst_box.y1 < 0) + dst_box.y1 = 0; + if (dst_box.x2 > W) + dst_box.x2 = W; + if (dst_box.y2 > H) + dst_box.y2 = H; + + if (dst_box.x1 < dst_box.x2 && dst_box.y1 < dst_box.y2) { + xf86RotateBlitRect32(rot, src_base, src_stride32, + dst_base, dst_stride32, + crtc->x, crtc->y, W, H, + dst_box.x1, dst_box.y1, + dst_box.x2, dst_box.y2); + } + } + + return TRUE; +} + void xf86RotateCrtcRedisplay(xf86CrtcPtr crtc, PixmapPtr dst_pixmap, DrawableRec *src_drawable, RegionPtr region, @@ -57,6 +226,11 @@ - if (crtc->driverIsPerformingTransform & XF86DriverTransformOutput) + if ((crtc->driverIsPerformingTransform & XF86DriverTransformOutput) && + (!dst_pixmap || dst_pixmap == crtc->rotatedPixmap)) return; + if (xf86RotateTryFast32(crtc, dst_pixmap, src_drawable, region, + transform_src)) + return; + src = CreatePicture(None, src_drawable, format,