Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 1 | /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright © 2011 Texas Instruments, Inc |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | * |
| 25 | * Authors: |
| 26 | * Rob Clark <rob@ti.com> |
| 27 | */ |
| 28 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
| 32 | |
| 33 | #include "omap_driver.h" |
Rob Clark | 0fdd91f | 2011-10-20 09:56:11 -0500 | [diff] [blame] | 34 | #include "omap_exa.h" |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 35 | |
| 36 | #include "xf86drmMode.h" |
| 37 | #include "dri2.h" |
| 38 | |
| 39 | /* any point to support earlier? */ |
| 40 | #if DRI2INFOREC_VERSION < 4 |
| 41 | # error "Requires newer DRI2" |
| 42 | #endif |
| 43 | |
| 44 | |
| 45 | typedef struct { |
| 46 | DRI2BufferRec base; |
| 47 | |
Rob Clark | 99ab80d | 2012-04-12 17:38:07 -0500 | [diff] [blame] | 48 | /** |
| 49 | * Pixmap that is backing the buffer |
| 50 | * |
| 51 | * NOTE: don't track the pixmap ptr for the front buffer if it is |
| 52 | * a window.. this could get reallocated from beneath us, so we should |
| 53 | * always use draw2pix to be sure to have the correct one |
| 54 | */ |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 55 | PixmapPtr pPixmap; |
| 56 | |
| 57 | /** |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 58 | * The DRI2 buffers are reference counted to avoid crashyness when the |
| 59 | * client detaches a dri2 drawable while we are still waiting for a |
| 60 | * page_flip event. |
| 61 | */ |
| 62 | int refcnt; |
| 63 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 64 | } OMAPDRI2BufferRec, *OMAPDRI2BufferPtr; |
| 65 | |
| 66 | #define OMAPBUF(p) ((OMAPDRI2BufferPtr)(p)) |
| 67 | #define DRIBUF(p) ((DRI2BufferPtr)(&(p)->base)) |
| 68 | |
| 69 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 70 | static inline DrawablePtr |
| 71 | dri2draw(DrawablePtr pDraw, DRI2BufferPtr buf) |
| 72 | { |
| 73 | if (buf->attachment == DRI2BufferFrontLeft) { |
| 74 | return pDraw; |
| 75 | } else { |
| 76 | return &(OMAPBUF(buf)->pPixmap->drawable); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static inline Bool |
Rob Clark | 99ab80d | 2012-04-12 17:38:07 -0500 | [diff] [blame] | 81 | canexchange(DrawablePtr pDraw, DRI2BufferPtr a, DRI2BufferPtr b) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 82 | { |
Rob Clark | 99ab80d | 2012-04-12 17:38:07 -0500 | [diff] [blame] | 83 | DrawablePtr da = dri2draw(pDraw, a); |
| 84 | DrawablePtr db = dri2draw(pDraw, b); |
| 85 | |
| 86 | return DRI2CanFlip(pDraw) && |
| 87 | (da->width == db->width) && |
| 88 | (da->height == db->height) && |
| 89 | (da->depth == db->depth); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static Bool |
| 93 | canflip(DrawablePtr pDraw) |
| 94 | { |
| 95 | return (pDraw->type == DRAWABLE_WINDOW) && |
Rob Clark | 99ab80d | 2012-04-12 17:38:07 -0500 | [diff] [blame] | 96 | DRI2CanFlip(pDraw); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static inline Bool |
| 100 | exchangebufs(DrawablePtr pDraw, DRI2BufferPtr a, DRI2BufferPtr b) |
| 101 | { |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 102 | OMAPPixmapExchange(draw2pix(dri2draw(pDraw, a)), |
| 103 | draw2pix(dri2draw(pDraw, b))); |
| 104 | exchange(a->name, b->name); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 105 | return TRUE; |
| 106 | } |
| 107 | |
| 108 | static PixmapPtr |
| 109 | createpix(DrawablePtr pDraw) |
| 110 | { |
| 111 | ScreenPtr pScreen = pDraw->pScreen; |
| 112 | int flags = canflip(pDraw) ? OMAP_CREATE_PIXMAP_SCANOUT : 0; |
| 113 | return pScreen->CreatePixmap(pScreen, |
| 114 | pDraw->width, pDraw->height, pDraw->depth, flags); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Create Buffer. |
| 119 | * |
| 120 | * Note that 'format' is used from the client side to specify the DRI buffer |
| 121 | * format, which could differ from the drawable format. For example, the |
| 122 | * drawable could be 32b RGB, but the DRI buffer some YUV format (video) or |
| 123 | * perhaps lower bit depth RGB (GL). The color conversion is handled when |
| 124 | * blitting to front buffer, and page-flipping (overlay or flipchain) can |
| 125 | * only be used if the display supports. |
| 126 | */ |
| 127 | static DRI2BufferPtr |
| 128 | OMAPDRI2CreateBuffer(DrawablePtr pDraw, unsigned int attachment, |
| 129 | unsigned int format) |
| 130 | { |
| 131 | ScreenPtr pScreen = pDraw->pScreen; |
| 132 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
| 133 | OMAPPtr pOMAP = OMAPPTR(pScrn); |
| 134 | OMAPDRI2BufferPtr buf = calloc(1, sizeof(*buf)); |
| 135 | PixmapPtr pPixmap; |
| 136 | struct omap_bo *bo; |
| 137 | int ret; |
| 138 | |
| 139 | DEBUG_MSG("pDraw=%p, attachment=%d, format=%08x", |
| 140 | pDraw, attachment, format); |
| 141 | |
| 142 | if (!buf) { |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | if (attachment == DRI2BufferFrontLeft) { |
| 147 | pPixmap = draw2pix(pDraw); |
| 148 | |
Rob Clark | 834f646 | 2012-04-20 15:40:51 -0500 | [diff] [blame] | 149 | /* to do flipping, if we don't have DMM, then we need a scanout |
| 150 | * capable (physically contiguous) buffer.. this bit of gymnastics |
| 151 | * ensures that. |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 152 | * |
Rob Clark | 834f646 | 2012-04-20 15:40:51 -0500 | [diff] [blame] | 153 | * TODO we may want to re-allocate and switch back to non-scanout |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 154 | * buffer when client disconnects from drawable.. |
| 155 | */ |
Raymond Smith | 2b4620b | 2012-05-01 12:00:41 +0100 | [diff] [blame] | 156 | |
| 157 | /* TODO: We don't have enough memory to allocate three physically contiguous buffers at the same time! Because all our |
| 158 | * buffers are scanout-able we'll not bother allocating *another* scanout buffer and just use the one we already have |
| 159 | * and save that extra buffer size */ |
| 160 | #if 0 |
Rob Clark | 834f646 | 2012-04-20 15:40:51 -0500 | [diff] [blame] | 161 | if (canflip(pDraw) && !has_dmm(pOMAP) && |
| 162 | (OMAPPixmapBo(pPixmap) != pOMAP->scanout)) { |
| 163 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 164 | /* need to re-allocate pixmap to get a scanout capable buffer */ |
| 165 | PixmapPtr pNewPix = createpix(pDraw); |
| 166 | |
| 167 | // TODO copy contents.. |
| 168 | |
| 169 | OMAPPixmapExchange(pPixmap, pNewPix); |
| 170 | |
| 171 | pScreen->DestroyPixmap(pNewPix); |
| 172 | } |
Raymond Smith | 2b4620b | 2012-05-01 12:00:41 +0100 | [diff] [blame] | 173 | #endif |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 174 | |
| 175 | pPixmap->refcnt++; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 176 | } else { |
| 177 | pPixmap = createpix(pDraw); |
| 178 | } |
| 179 | |
| 180 | bo = OMAPPixmapBo(pPixmap); |
Raymond Smith | d15a734 | 2012-05-01 12:01:52 +0100 | [diff] [blame] | 181 | if (!bo) |
| 182 | { |
| 183 | ERROR_MSG("Attempting to DRI2 wrap a pixmap with no DRM buffer object backing"); |
| 184 | /* TODO: Returning NULL here ends up in a segfault all the way in pixman which has no backtrace. We get |
| 185 | * a more friendly segfault if we just let it be dereferenced in a few lines */ |
| 186 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 187 | |
| 188 | DRIBUF(buf)->attachment = attachment; |
| 189 | DRIBUF(buf)->pitch = exaGetPixmapPitch(pPixmap); |
| 190 | DRIBUF(buf)->cpp = pPixmap->drawable.bitsPerPixel / 8; |
| 191 | DRIBUF(buf)->format = format; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 192 | buf->refcnt = 1; |
| 193 | buf->pPixmap = pPixmap; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 194 | |
| 195 | ret = omap_bo_get_name(bo, &DRIBUF(buf)->name); |
| 196 | if (ret) { |
| 197 | ERROR_MSG("could not get buffer name: %d", ret); |
| 198 | /* TODO cleanup */ |
| 199 | return NULL; |
| 200 | } |
| 201 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 202 | /* Q: how to know across OMAP generations what formats that the display |
| 203 | * can support directly? |
| 204 | * A: attempt to create a drm_framebuffer, and if that fails then the |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 205 | * hw must not support.. then fall back to blitting |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 206 | */ |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 207 | if (canflip(pDraw) && attachment != DRI2BufferFrontLeft) { |
| 208 | uint32_t new_fb_id; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 209 | int ret = drmModeAddFB(pOMAP->drmFD, pDraw->width, pDraw->height, |
| 210 | pDraw->depth, pDraw->bitsPerPixel, DRIBUF(buf)->pitch, |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 211 | omap_bo_handle(bo), &new_fb_id); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 212 | if (ret) { |
| 213 | /* to-bad, so-sad, we can't flip */ |
| 214 | WARNING_MSG("could not create fb: %d", ret); |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 215 | } else { |
| 216 | omap_bo_set_fb(bo, new_fb_id); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | return DRIBUF(buf); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Destroy Buffer |
| 225 | * |
| 226 | * TODO: depending on how flipping ends up working, we may need a refcnt or |
| 227 | * something like this to defer destroying a buffer that is currently being |
| 228 | * scanned out.. |
| 229 | */ |
| 230 | static void |
| 231 | OMAPDRI2DestroyBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer) |
| 232 | { |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 233 | OMAPDRI2BufferPtr buf = OMAPBUF(buffer); |
| 234 | /* Note: pDraw may already be deleted, so use the pPixmap here |
| 235 | * instead (since it is at least refcntd) |
| 236 | */ |
| 237 | ScreenPtr pScreen = buf->pPixmap->drawable.pScreen; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 238 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
| 239 | OMAPPtr pOMAP = OMAPPTR(pScrn); |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 240 | |
| 241 | if (--buf->refcnt > 0) |
| 242 | return; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 243 | |
| 244 | DEBUG_MSG("pDraw=%p, buffer=%p", pDraw, buffer); |
| 245 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 246 | pScreen->DestroyPixmap(buf->pPixmap); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 247 | |
| 248 | free(buf); |
| 249 | } |
| 250 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 251 | static void |
| 252 | OMAPDRI2ReferenceBuffer(DRI2BufferPtr buffer) |
| 253 | { |
| 254 | OMAPDRI2BufferPtr buf = OMAPBUF(buffer); |
| 255 | buf->refcnt++; |
| 256 | } |
| 257 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 258 | /** |
| 259 | * |
| 260 | */ |
| 261 | static void |
| 262 | OMAPDRI2CopyRegion(DrawablePtr pDraw, RegionPtr pRegion, |
| 263 | DRI2BufferPtr pDstBuffer, DRI2BufferPtr pSrcBuffer) |
| 264 | { |
| 265 | ScreenPtr pScreen = pDraw->pScreen; |
| 266 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
| 267 | DrawablePtr pSrcDraw = dri2draw(pDraw, pSrcBuffer); |
| 268 | DrawablePtr pDstDraw = dri2draw(pDraw, pDstBuffer); |
| 269 | RegionPtr pCopyClip; |
| 270 | GCPtr pGC; |
| 271 | |
| 272 | DEBUG_MSG("pDraw=%p, pDstBuffer=%p (%p), pSrcBuffer=%p (%p)", |
| 273 | pDraw, pDstBuffer, pSrcDraw, pSrcBuffer, pDstDraw); |
| 274 | |
| 275 | pGC = GetScratchGC(pDstDraw->depth, pScreen); |
| 276 | if (!pGC) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | pCopyClip = REGION_CREATE(pScreen, NULL, 0); |
| 281 | RegionCopy(pCopyClip, pRegion); |
| 282 | (*pGC->funcs->ChangeClip) (pGC, CT_REGION, pCopyClip, 0); |
| 283 | ValidateGC(pDstDraw, pGC); |
| 284 | |
| 285 | /* If the dst is the framebuffer, and we had a way to |
| 286 | * schedule a deferred blit synchronized w/ vsync, that |
| 287 | * would be a nice thing to do utilize here to avoid |
| 288 | * tearing.. when we have sync object support for GEM |
| 289 | * buffers, I think we could do something more clever |
| 290 | * here. |
| 291 | */ |
| 292 | |
| 293 | pGC->ops->CopyArea(pSrcDraw, pDstDraw, pGC, |
| 294 | 0, 0, pDraw->width, pDraw->height, 0, 0); |
| 295 | |
Rob Clark | e450d41 | 2012-01-08 19:33:48 -0600 | [diff] [blame] | 296 | FreeScratchGC(pGC); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Get current frame count and frame count timestamp, based on drawable's |
| 301 | * crtc. |
| 302 | */ |
| 303 | static int |
| 304 | OMAPDRI2GetMSC(DrawablePtr pDraw, CARD64 *ust, CARD64 *msc) |
| 305 | { |
| 306 | ScreenPtr pScreen = pDraw->pScreen; |
| 307 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
| 308 | OMAPPtr pOMAP = OMAPPTR(pScrn); |
| 309 | drmVBlank vbl = { .request = { |
| 310 | .type = DRM_VBLANK_RELATIVE, |
| 311 | .sequence = 0, |
| 312 | } }; |
| 313 | int ret; |
| 314 | |
| 315 | ret = drmWaitVBlank(pOMAP->drmFD, &vbl); |
| 316 | if (ret) { |
| 317 | static int limit = 5; |
| 318 | if (limit) { |
| 319 | ERROR_MSG("get vblank counter failed: %s", strerror(errno)); |
| 320 | limit--; |
| 321 | } |
| 322 | return FALSE; |
| 323 | } |
| 324 | |
| 325 | if (ust) { |
| 326 | *ust = ((CARD64)vbl.reply.tval_sec * 1000000) + vbl.reply.tval_usec; |
| 327 | } |
| 328 | if (msc) { |
| 329 | *msc = vbl.reply.sequence; |
| 330 | } |
| 331 | |
| 332 | return TRUE; |
| 333 | } |
| 334 | |
| 335 | struct _OMAPDRISwapCmd { |
Rob Clark | a6762ef | 2012-04-12 17:38:39 -0500 | [diff] [blame] | 336 | int type; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 337 | ClientPtr client; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 338 | ScreenPtr pScreen; |
| 339 | /* Note: store drawable ID, rather than drawable. It's possible that |
| 340 | * the drawable can be destroyed while we wait for page flip event: |
| 341 | */ |
| 342 | XID draw_id; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 343 | DRI2BufferPtr pDstBuffer; |
| 344 | DRI2BufferPtr pSrcBuffer; |
| 345 | DRI2SwapEventPtr func; |
| 346 | void *data; |
| 347 | }; |
| 348 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 349 | static const char *swap_names[] = { |
| 350 | [DRI2_EXCHANGE_COMPLETE] = "exchange", |
| 351 | [DRI2_BLIT_COMPLETE] = "blit", |
| 352 | [DRI2_FLIP_COMPLETE] = "flip," |
| 353 | }; |
| 354 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 355 | void |
| 356 | OMAPDRI2SwapComplete(OMAPDRISwapCmd *cmd) |
| 357 | { |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 358 | ScreenPtr pScreen = cmd->pScreen; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 359 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
Rob Clark | 67b875f | 2012-04-20 19:13:57 -0500 | [diff] [blame] | 360 | OMAPPtr pOMAP = OMAPPTR(pScrn); |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 361 | DrawablePtr pDraw = NULL; |
| 362 | int status; |
David Garbett | 3688b33 | 2012-05-11 12:17:34 +0100 | [diff] [blame^] | 363 | OMAPPixmapPrivPtr dst_priv; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 364 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 365 | DEBUG_MSG("%s complete: %d -> %d", swap_names[cmd->type], |
| 366 | cmd->pSrcBuffer->attachment, cmd->pDstBuffer->attachment); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 367 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 368 | status = dixLookupDrawable(&pDraw, cmd->draw_id, serverClient, |
| 369 | M_ANY, DixWriteAccess); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 370 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 371 | if (status == Success) { |
| 372 | if (cmd->type != DRI2_BLIT_COMPLETE) |
| 373 | exchangebufs(pDraw, cmd->pSrcBuffer, cmd->pDstBuffer); |
| 374 | |
| 375 | DRI2SwapComplete(cmd->client, pDraw, 0, 0, 0, cmd->type, |
| 376 | cmd->func, cmd->data); |
| 377 | } |
| 378 | |
David Garbett | 3688b33 | 2012-05-11 12:17:34 +0100 | [diff] [blame^] | 379 | dst_priv = exaGetPixmapDriverPrivate(draw2pix(dri2draw(pDraw, cmd->pDstBuffer))); |
| 380 | |
| 381 | set_scanout_bo(pScrn, dst_priv->bo); |
| 382 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 383 | /* drop extra refcnt we obtained prior to swap: |
| 384 | */ |
| 385 | OMAPDRI2DestroyBuffer(pDraw, cmd->pSrcBuffer); |
| 386 | OMAPDRI2DestroyBuffer(pDraw, cmd->pDstBuffer); |
Rob Clark | 67b875f | 2012-04-20 19:13:57 -0500 | [diff] [blame] | 387 | pOMAP->pending_flips--; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 388 | |
| 389 | free(cmd); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * ScheduleSwap is responsible for requesting a DRM vblank event for the |
| 394 | * appropriate frame. |
| 395 | * |
| 396 | * In the case of a blit (e.g. for a windowed swap) or buffer exchange, |
| 397 | * the vblank requested can simply be the last queued swap frame + the swap |
| 398 | * interval for the drawable. |
| 399 | * |
| 400 | * In the case of a page flip, we request an event for the last queued swap |
| 401 | * frame + swap interval - 1, since we'll need to queue the flip for the frame |
| 402 | * immediately following the received event. |
| 403 | */ |
| 404 | static int |
| 405 | OMAPDRI2ScheduleSwap(ClientPtr client, DrawablePtr pDraw, |
| 406 | DRI2BufferPtr pDstBuffer, DRI2BufferPtr pSrcBuffer, |
| 407 | CARD64 *target_msc, CARD64 divisor, CARD64 remainder, |
| 408 | DRI2SwapEventPtr func, void *data) |
| 409 | { |
| 410 | ScreenPtr pScreen = pDraw->pScreen; |
| 411 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
Rob Clark | 67b875f | 2012-04-20 19:13:57 -0500 | [diff] [blame] | 412 | OMAPPtr pOMAP = OMAPPTR(pScrn); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 413 | OMAPDRI2BufferPtr src = OMAPBUF(pSrcBuffer); |
| 414 | OMAPDRI2BufferPtr dst = OMAPBUF(pDstBuffer); |
| 415 | OMAPDRISwapCmd *cmd = calloc(1, sizeof(*cmd)); |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 416 | int src_fb_id, dst_fb_id; |
| 417 | OMAPPixmapPrivPtr src_priv, dst_priv; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 418 | |
| 419 | cmd->client = client; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 420 | cmd->pScreen = pScreen; |
| 421 | cmd->draw_id = pDraw->id; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 422 | cmd->pSrcBuffer = pSrcBuffer; |
| 423 | cmd->pDstBuffer = pDstBuffer; |
| 424 | cmd->func = func; |
| 425 | cmd->data = data; |
| 426 | |
| 427 | DEBUG_MSG("%d -> %d", pSrcBuffer->attachment, pDstBuffer->attachment); |
| 428 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 429 | /* obtain extra ref on buffers to avoid them going away while we await |
| 430 | * the page flip event: |
| 431 | */ |
| 432 | OMAPDRI2ReferenceBuffer(pSrcBuffer); |
| 433 | OMAPDRI2ReferenceBuffer(pDstBuffer); |
Rob Clark | 67b875f | 2012-04-20 19:13:57 -0500 | [diff] [blame] | 434 | pOMAP->pending_flips++; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 435 | |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 436 | src_priv = exaGetPixmapDriverPrivate(src->pPixmap); |
| 437 | dst_priv = exaGetPixmapDriverPrivate(dst->pPixmap); |
| 438 | |
| 439 | src_fb_id = omap_bo_get_fb(src_priv->bo); |
| 440 | dst_fb_id = omap_bo_get_fb(dst_priv->bo); |
| 441 | |
| 442 | if (src_fb_id && dst_fb_id) { |
| 443 | DEBUG_MSG("can flip: %d -> %d", src_fb_id, dst_fb_id); |
Rob Clark | a6762ef | 2012-04-12 17:38:39 -0500 | [diff] [blame] | 444 | cmd->type = DRI2_FLIP_COMPLETE; |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 445 | drmmode_page_flip(pDraw, src_fb_id, cmd); |
Rob Clark | 99ab80d | 2012-04-12 17:38:07 -0500 | [diff] [blame] | 446 | } else if (canexchange(pDraw, pSrcBuffer, pDstBuffer)) { |
| 447 | /* we can get away w/ pointer swap.. yah! */ |
Rob Clark | a6762ef | 2012-04-12 17:38:39 -0500 | [diff] [blame] | 448 | cmd->type = DRI2_EXCHANGE_COMPLETE; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 449 | OMAPDRI2SwapComplete(cmd); |
| 450 | } else { |
| 451 | /* fallback to blit: */ |
| 452 | BoxRec box = { |
| 453 | .x1 = 0, |
| 454 | .y1 = 0, |
| 455 | .x2 = pDraw->width, |
| 456 | .y2 = pDraw->height, |
| 457 | }; |
| 458 | RegionRec region; |
| 459 | RegionInit(®ion, &box, 0); |
| 460 | OMAPDRI2CopyRegion(pDraw, ®ion, pDstBuffer, pSrcBuffer); |
Rob Clark | a6762ef | 2012-04-12 17:38:39 -0500 | [diff] [blame] | 461 | cmd->type = DRI2_BLIT_COMPLETE; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 462 | OMAPDRI2SwapComplete(cmd); |
| 463 | } |
| 464 | |
| 465 | return TRUE; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Request a DRM event when the requested conditions will be satisfied. |
| 470 | * |
| 471 | * We need to handle the event and ask the server to wake up the client when |
| 472 | * we receive it. |
| 473 | */ |
| 474 | static int |
| 475 | OMAPDRI2ScheduleWaitMSC(ClientPtr client, DrawablePtr pDraw, CARD64 target_msc, |
| 476 | CARD64 divisor, CARD64 remainder) |
| 477 | { |
| 478 | ScreenPtr pScreen = pDraw->pScreen; |
| 479 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
| 480 | // OMAPPtr pOMAP = OMAPPTR(pScrn); |
| 481 | |
| 482 | #if 0 |
| 483 | #endif |
| 484 | ERROR_MSG("not implemented"); |
| 485 | return FALSE; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * The DRI2 ScreenInit() function.. register our handler fxns w/ DRI2 core |
| 490 | */ |
| 491 | Bool |
| 492 | OMAPDRI2ScreenInit(ScreenPtr pScreen) |
| 493 | { |
| 494 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
| 495 | OMAPPtr pOMAP = OMAPPTR(pScrn); |
| 496 | DRI2InfoRec info = { |
| 497 | .version = 5, |
| 498 | .fd = pOMAP->drmFD, |
| 499 | .driverName = "omap", |
| 500 | .deviceName = pOMAP->deviceName, |
| 501 | .CreateBuffer = OMAPDRI2CreateBuffer, |
| 502 | .DestroyBuffer = OMAPDRI2DestroyBuffer, |
| 503 | .CopyRegion = OMAPDRI2CopyRegion, |
| 504 | .ScheduleSwap = OMAPDRI2ScheduleSwap, |
| 505 | .ScheduleWaitMSC = OMAPDRI2ScheduleWaitMSC, |
| 506 | .GetMSC = OMAPDRI2GetMSC, |
| 507 | .AuthMagic = drmAuthMagic, |
| 508 | }; |
| 509 | int minor = 1, major = 0; |
| 510 | |
| 511 | if (xf86LoaderCheckSymbol("DRI2Version")) { |
| 512 | DRI2Version(&major, &minor); |
| 513 | } |
| 514 | |
| 515 | if (minor < 1) { |
| 516 | WARNING_MSG("DRI2 requires DRI2 module version 1.1.0 or later"); |
| 517 | return FALSE; |
| 518 | } |
| 519 | |
| 520 | return DRI2ScreenInit(pScreen, &info); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * The DRI2 CloseScreen() function.. unregister ourself w/ DRI2 core. |
| 525 | */ |
| 526 | void |
| 527 | OMAPDRI2CloseScreen(ScreenPtr pScreen) |
| 528 | { |
Rob Clark | 67b875f | 2012-04-20 19:13:57 -0500 | [diff] [blame] | 529 | ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; |
| 530 | OMAPPtr pOMAP = OMAPPTR(pScrn); |
| 531 | while (pOMAP->pending_flips > 0) { |
| 532 | DEBUG_MSG("waiting.."); |
| 533 | drmmode_wait_for_event(pScrn); |
| 534 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 535 | DRI2CloseScreen(pScreen); |
| 536 | } |