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 | |
Paul Geary | f8b9969 | 2013-04-15 10:55:17 +0100 | [diff] [blame] | 33 | #include "armsoc_driver.h" |
| 34 | #include "armsoc_exa.h" |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 35 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 36 | #include "dri2.h" |
| 37 | |
| 38 | /* any point to support earlier? */ |
| 39 | #if DRI2INFOREC_VERSION < 4 |
| 40 | # error "Requires newer DRI2" |
| 41 | #endif |
| 42 | |
Ray Smith | 3c33c3d | 2013-03-26 16:06:37 +0000 | [diff] [blame] | 43 | #include "drmmode_driver.h" |
| 44 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 45 | struct ARMSOCDRI2BufferRec { |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 46 | DRI2BufferRec base; |
| 47 | |
Rob Clark | 99ab80d | 2012-04-12 17:38:07 -0500 | [diff] [blame] | 48 | /** |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 49 | * Pixmap(s) that are backing the buffer |
Rob Clark | 99ab80d | 2012-04-12 17:38:07 -0500 | [diff] [blame] | 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 | */ |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 55 | PixmapPtr *pPixmaps; |
| 56 | |
| 57 | /** |
| 58 | * Pixmap that corresponds to the DRI2BufferRec.name, so wraps |
| 59 | * the buffer that will be used for DRI2GetBuffers calls and the |
| 60 | * next DRI2SwapBuffers call. |
| 61 | * |
| 62 | * When using more than double buffering this (and the name) are updated |
| 63 | * after a swap, before the next DRI2GetBuffers call. |
| 64 | */ |
| 65 | unsigned currentPixmap; |
| 66 | |
| 67 | /** |
| 68 | * Number of Pixmaps to use. |
| 69 | * |
| 70 | * This allows the number of back buffers used to be reduced, for |
| 71 | * example when allocation fails. It cannot be changed to increase the |
| 72 | * number of buffers as we would overflow the pPixmaps array. |
| 73 | */ |
| 74 | unsigned numPixmaps; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 75 | |
| 76 | /** |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 77 | * The DRI2 buffers are reference counted to avoid crashyness when the |
| 78 | * client detaches a dri2 drawable while we are still waiting for a |
| 79 | * page_flip event. |
| 80 | */ |
| 81 | int refcnt; |
| 82 | |
Raymond Smith | 4a0c0aa | 2012-06-20 10:50:39 +0100 | [diff] [blame] | 83 | /** |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 84 | * The value of canflip() for the previous frame. Used so that we can |
| 85 | * tell whether the buffer should be re-allocated, e.g into scanout-able |
Raymond Smith | 4a0c0aa | 2012-06-20 10:50:39 +0100 | [diff] [blame] | 86 | * memory if the buffer can now be flipped. |
| 87 | * |
| 88 | * We don't want to re-allocate every frame because it is unnecessary |
| 89 | * overhead most of the time apart from when we switch from flipping |
| 90 | * to blitting or vice versa. |
| 91 | * |
| 92 | * We should bump the serial number of the drawable if canflip() returns |
| 93 | * something different to what is stored here, so that the DRI2 buffers |
| 94 | * will get re-allocated. |
| 95 | */ |
| 96 | int previous_canflip; |
| 97 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 98 | }; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 99 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 100 | #define ARMSOCBUF(p) ((struct ARMSOCDRI2BufferRec *)(p)) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 101 | #define DRIBUF(p) ((DRI2BufferPtr)(&(p)->base)) |
| 102 | |
| 103 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 104 | static inline DrawablePtr |
| 105 | dri2draw(DrawablePtr pDraw, DRI2BufferPtr buf) |
| 106 | { |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 107 | if (buf->attachment == DRI2BufferFrontLeft) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 108 | return pDraw; |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 109 | else { |
| 110 | const unsigned curPix = ARMSOCBUF(buf)->currentPixmap; |
| 111 | return &(ARMSOCBUF(buf)->pPixmaps[curPix]->drawable); |
| 112 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 115 | static Bool |
| 116 | canflip(DrawablePtr pDraw) |
| 117 | { |
Dave Barnish | c50baf5 | 2012-11-29 14:39:36 +0000 | [diff] [blame] | 118 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 119 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 120 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
Dave Barnish | c50baf5 | 2012-11-29 14:39:36 +0000 | [diff] [blame] | 121 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 122 | if (pARMSOC->NoFlip) { |
Dave Barnish | c50baf5 | 2012-11-29 14:39:36 +0000 | [diff] [blame] | 123 | /* flipping is disabled by user option */ |
| 124 | return FALSE; |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 125 | } else { |
Dave Barnish | c50baf5 | 2012-11-29 14:39:36 +0000 | [diff] [blame] | 126 | return (pDraw->type == DRAWABLE_WINDOW) && |
| 127 | DRI2CanFlip(pDraw); |
| 128 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static inline Bool |
| 132 | exchangebufs(DrawablePtr pDraw, DRI2BufferPtr a, DRI2BufferPtr b) |
| 133 | { |
David Garbett | ae5a636 | 2012-07-02 10:15:47 +0100 | [diff] [blame] | 134 | PixmapPtr aPix = draw2pix(dri2draw(pDraw, a)); |
| 135 | PixmapPtr bPix = draw2pix(dri2draw(pDraw, b)); |
| 136 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 137 | ARMSOCPixmapExchange(aPix, bPix); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 138 | exchange(a->name, b->name); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 139 | return TRUE; |
| 140 | } |
| 141 | |
| 142 | static PixmapPtr |
| 143 | createpix(DrawablePtr pDraw) |
| 144 | { |
| 145 | ScreenPtr pScreen = pDraw->pScreen; |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 146 | int flags = canflip(pDraw) ? ARMSOC_CREATE_PIXMAP_SCANOUT : 0; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 147 | return pScreen->CreatePixmap(pScreen, |
| 148 | pDraw->width, pDraw->height, pDraw->depth, flags); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Create Buffer. |
| 153 | * |
| 154 | * Note that 'format' is used from the client side to specify the DRI buffer |
| 155 | * format, which could differ from the drawable format. For example, the |
| 156 | * drawable could be 32b RGB, but the DRI buffer some YUV format (video) or |
| 157 | * perhaps lower bit depth RGB (GL). The color conversion is handled when |
| 158 | * blitting to front buffer, and page-flipping (overlay or flipchain) can |
| 159 | * only be used if the display supports. |
| 160 | */ |
| 161 | static DRI2BufferPtr |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 162 | ARMSOCDRI2CreateBuffer(DrawablePtr pDraw, unsigned int attachment, |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 163 | unsigned int format) |
| 164 | { |
| 165 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 166 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 167 | struct ARMSOCDRI2BufferRec *buf = calloc(1, sizeof(*buf)); |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 168 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 169 | PixmapPtr pPixmap = NULL; |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 170 | struct armsoc_bo *bo; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 171 | int ret; |
| 172 | |
| 173 | DEBUG_MSG("pDraw=%p, attachment=%d, format=%08x", |
| 174 | pDraw, attachment, format); |
| 175 | |
| 176 | if (!buf) { |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 177 | ERROR_MSG("Couldn't allocate internal buffer structure"); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | if (attachment == DRI2BufferFrontLeft) { |
| 182 | pPixmap = draw2pix(pDraw); |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 183 | pPixmap->refcnt++; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 184 | } else { |
| 185 | pPixmap = createpix(pDraw); |
| 186 | } |
| 187 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 188 | if (!pPixmap) { |
Ray Smith | 2bc3bf6 | 2013-03-15 12:05:08 +0000 | [diff] [blame] | 189 | assert(attachment != DRI2BufferFrontLeft); |
| 190 | ERROR_MSG("Failed to create back buffer for window"); |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 191 | goto fail; |
Ray Smith | 2bc3bf6 | 2013-03-15 12:05:08 +0000 | [diff] [blame] | 192 | } |
| 193 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 194 | if (attachment == DRI2BufferBackLeft && pARMSOC->driNumBufs > 2) { |
| 195 | buf->pPixmaps = calloc(pARMSOC->driNumBufs-1, |
| 196 | sizeof(PixmapPtr)); |
| 197 | buf->numPixmaps = pARMSOC->driNumBufs-1; |
| 198 | } else { |
| 199 | buf->pPixmaps = malloc(sizeof(PixmapPtr)); |
| 200 | buf->numPixmaps = 1; |
| 201 | } |
| 202 | |
| 203 | if (!buf->pPixmaps) { |
| 204 | ERROR_MSG("Failed to allocate PixmapPtr array for DRI2Buffer"); |
| 205 | goto fail; |
| 206 | } |
| 207 | |
| 208 | buf->pPixmaps[0] = pPixmap; |
| 209 | assert(buf->currentPixmap == 0); |
| 210 | |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 211 | bo = ARMSOCPixmapBo(pPixmap); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 212 | if (!bo) { |
| 213 | ERROR_MSG( |
| 214 | "Attempting to DRI2 wrap a pixmap with no DRM buffer object backing"); |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 215 | goto fail; |
Raymond Smith | d15a734 | 2012-05-01 12:01:52 +0100 | [diff] [blame] | 216 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 217 | |
| 218 | DRIBUF(buf)->attachment = attachment; |
| 219 | DRIBUF(buf)->pitch = exaGetPixmapPitch(pPixmap); |
| 220 | DRIBUF(buf)->cpp = pPixmap->drawable.bitsPerPixel / 8; |
| 221 | DRIBUF(buf)->format = format; |
Raymond Smith | 2f93cb1 | 2012-05-10 07:41:34 +0100 | [diff] [blame] | 222 | DRIBUF(buf)->flags = 0; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 223 | buf->refcnt = 1; |
Liyou Zhou | dcf32bd | 2013-09-20 16:21:11 +0100 | [diff] [blame^] | 224 | buf->previous_canflip = canflip(pDraw); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 225 | |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 226 | ret = armsoc_bo_get_name(bo, &DRIBUF(buf)->name); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 227 | if (ret) { |
| 228 | ERROR_MSG("could not get buffer name: %d", ret); |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 229 | goto fail; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 230 | } |
| 231 | |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 232 | if (canflip(pDraw) && attachment != DRI2BufferFrontLeft) { |
Ray Smith | b4299f8 | 2013-03-13 10:08:36 +0000 | [diff] [blame] | 233 | /* Create an fb around this buffer. This will fail and we will |
| 234 | * fall back to blitting if the display controller hardware |
| 235 | * cannot scan out this buffer (for example, if it doesn't |
| 236 | * support the format or there was insufficient scanout memory |
| 237 | * at buffer creation time). */ |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 238 | int ret = armsoc_bo_add_fb(bo); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 239 | if (ret) { |
Ray Smith | b4299f8 | 2013-03-13 10:08:36 +0000 | [diff] [blame] | 240 | WARNING_MSG( |
| 241 | "Falling back to blitting a flippable window"); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
David Garbett | ae5a636 | 2012-07-02 10:15:47 +0100 | [diff] [blame] | 245 | /* Register Pixmap as having a buffer that can be accessed externally, |
| 246 | * so needs synchronised access */ |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 247 | ARMSOCRegisterExternalAccess(pPixmap); |
David Garbett | ae5a636 | 2012-07-02 10:15:47 +0100 | [diff] [blame] | 248 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 249 | return DRIBUF(buf); |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 250 | |
| 251 | fail: |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 252 | if (pPixmap != NULL) { |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 253 | if (attachment != DRI2BufferFrontLeft) |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 254 | pScreen->DestroyPixmap(pPixmap); |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 255 | else |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 256 | pPixmap->refcnt--; |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 257 | } |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 258 | free(buf->pPixmaps); |
Ray Smith | 9a3797b | 2013-03-15 15:33:04 +0000 | [diff] [blame] | 259 | free(buf); |
| 260 | |
| 261 | return NULL; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Destroy Buffer |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 266 | */ |
| 267 | static void |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 268 | ARMSOCDRI2DestroyBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 269 | { |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 270 | struct ARMSOCDRI2BufferRec *buf = ARMSOCBUF(buffer); |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 271 | /* Note: pDraw may already be deleted, so use the pPixmap here |
| 272 | * instead (since it is at least refcntd) |
| 273 | */ |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 274 | ScreenPtr pScreen = buf->pPixmaps[0]->drawable.pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 275 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 276 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
| 277 | int numBuffers, i; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 278 | |
| 279 | if (--buf->refcnt > 0) |
| 280 | return; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 281 | |
| 282 | DEBUG_MSG("pDraw=%p, buffer=%p", pDraw, buffer); |
| 283 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 284 | if (buffer->attachment == DRI2BufferBackLeft) { |
| 285 | assert(pARMSOC->driNumBufs > 1); |
| 286 | numBuffers = pARMSOC->driNumBufs-1; |
| 287 | } else |
| 288 | numBuffers = 1; |
David Garbett | ae5a636 | 2012-07-02 10:15:47 +0100 | [diff] [blame] | 289 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 290 | for (i = 0; i < numBuffers && buf->pPixmaps[i] != NULL; i++) { |
| 291 | ARMSOCDeregisterExternalAccess(buf->pPixmaps[i]); |
| 292 | pScreen->DestroyPixmap(buf->pPixmaps[i]); |
| 293 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 294 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 295 | free(buf->pPixmaps); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 296 | free(buf); |
| 297 | } |
| 298 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 299 | static void |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 300 | ARMSOCDRI2ReferenceBuffer(DRI2BufferPtr buffer) |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 301 | { |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 302 | struct ARMSOCDRI2BufferRec *buf = ARMSOCBUF(buffer); |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 303 | buf->refcnt++; |
| 304 | } |
| 305 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 306 | /** |
| 307 | * |
| 308 | */ |
| 309 | static void |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 310 | ARMSOCDRI2CopyRegion(DrawablePtr pDraw, RegionPtr pRegion, |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 311 | DRI2BufferPtr pDstBuffer, DRI2BufferPtr pSrcBuffer) |
| 312 | { |
| 313 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 314 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 315 | DrawablePtr pSrcDraw = dri2draw(pDraw, pSrcBuffer); |
| 316 | DrawablePtr pDstDraw = dri2draw(pDraw, pDstBuffer); |
| 317 | RegionPtr pCopyClip; |
| 318 | GCPtr pGC; |
| 319 | |
| 320 | DEBUG_MSG("pDraw=%p, pDstBuffer=%p (%p), pSrcBuffer=%p (%p)", |
| 321 | pDraw, pDstBuffer, pSrcDraw, pSrcBuffer, pDstDraw); |
| 322 | |
| 323 | pGC = GetScratchGC(pDstDraw->depth, pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 324 | if (!pGC) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 325 | return; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 326 | |
| 327 | pCopyClip = REGION_CREATE(pScreen, NULL, 0); |
| 328 | RegionCopy(pCopyClip, pRegion); |
| 329 | (*pGC->funcs->ChangeClip) (pGC, CT_REGION, pCopyClip, 0); |
| 330 | ValidateGC(pDstDraw, pGC); |
| 331 | |
| 332 | /* If the dst is the framebuffer, and we had a way to |
| 333 | * schedule a deferred blit synchronized w/ vsync, that |
| 334 | * would be a nice thing to do utilize here to avoid |
| 335 | * tearing.. when we have sync object support for GEM |
| 336 | * buffers, I think we could do something more clever |
| 337 | * here. |
| 338 | */ |
| 339 | |
| 340 | pGC->ops->CopyArea(pSrcDraw, pDstDraw, pGC, |
| 341 | 0, 0, pDraw->width, pDraw->height, 0, 0); |
| 342 | |
Rob Clark | e450d41 | 2012-01-08 19:33:48 -0600 | [diff] [blame] | 343 | FreeScratchGC(pGC); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Get current frame count and frame count timestamp, based on drawable's |
| 348 | * crtc. |
| 349 | */ |
| 350 | static int |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 351 | ARMSOCDRI2GetMSC(DrawablePtr pDraw, CARD64 *ust, CARD64 *msc) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 352 | { |
| 353 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 354 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 355 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 356 | drmVBlank vbl = { .request = { |
| 357 | .type = DRM_VBLANK_RELATIVE, |
| 358 | .sequence = 0, |
| 359 | } }; |
| 360 | int ret; |
| 361 | |
Dave Barnish | e762b12 | 2013-08-12 17:13:46 +0100 | [diff] [blame] | 362 | if (!pARMSOC->drmmode_interface->vblank_query_supported) |
| 363 | return FALSE; |
| 364 | |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 365 | ret = drmWaitVBlank(pARMSOC->drmFD, &vbl); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 366 | if (ret) { |
Dave Barnish | e762b12 | 2013-08-12 17:13:46 +0100 | [diff] [blame] | 367 | ERROR_MSG("get vblank counter failed: %s", strerror(errno)); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 368 | return FALSE; |
| 369 | } |
| 370 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 371 | if (ust) |
| 372 | *ust = ((CARD64)vbl.reply.tval_sec * 1000000) |
| 373 | + vbl.reply.tval_usec; |
| 374 | |
| 375 | if (msc) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 376 | *msc = vbl.reply.sequence; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 377 | |
| 378 | return TRUE; |
| 379 | } |
| 380 | |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 381 | #define ARMSOC_SWAP_FAKE_FLIP (1 << 0) |
| 382 | #define ARMSOC_SWAP_FAIL (1 << 1) |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 383 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 384 | struct ARMSOCDRISwapCmd { |
Rob Clark | a6762ef | 2012-04-12 17:38:39 -0500 | [diff] [blame] | 385 | int type; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 386 | ClientPtr client; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 387 | ScreenPtr pScreen; |
| 388 | /* Note: store drawable ID, rather than drawable. It's possible that |
| 389 | * the drawable can be destroyed while we wait for page flip event: |
| 390 | */ |
| 391 | XID draw_id; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 392 | DRI2BufferPtr pDstBuffer; |
| 393 | DRI2BufferPtr pSrcBuffer; |
| 394 | DRI2SwapEventPtr func; |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 395 | int swapCount; |
| 396 | int flags; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 397 | void *data; |
| 398 | }; |
| 399 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 400 | static const char * const swap_names[] = { |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 401 | [DRI2_EXCHANGE_COMPLETE] = "exchange", |
| 402 | [DRI2_BLIT_COMPLETE] = "blit", |
| 403 | [DRI2_FLIP_COMPLETE] = "flip," |
| 404 | }; |
| 405 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 406 | static Bool allocNextBuffer(DrawablePtr pDraw, PixmapPtr *ppPixmap, |
| 407 | uint32_t *name) { |
| 408 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 409 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 410 | struct armsoc_bo *bo; |
| 411 | PixmapPtr pPixmap; |
| 412 | int ret; |
| 413 | uint32_t new_name; |
| 414 | Bool extRegistered = FALSE; |
| 415 | |
| 416 | pPixmap = createpix(pDraw); |
| 417 | |
| 418 | if (!pPixmap) |
| 419 | goto error; |
| 420 | |
| 421 | bo = ARMSOCPixmapBo(pPixmap); |
| 422 | if (!bo) { |
| 423 | WARNING_MSG( |
| 424 | "Attempting to DRI2 wrap a pixmap with no DRM buffer object backing"); |
| 425 | goto error; |
| 426 | } |
| 427 | |
| 428 | ARMSOCRegisterExternalAccess(pPixmap); |
| 429 | extRegistered = TRUE; |
| 430 | |
| 431 | ret = armsoc_bo_get_name(bo, &new_name); |
| 432 | if (ret) { |
| 433 | ERROR_MSG("Could not get buffer name: %d", ret); |
| 434 | goto error; |
| 435 | } |
| 436 | |
| 437 | if (!armsoc_bo_get_fb(bo)) { |
| 438 | ret = armsoc_bo_add_fb(bo); |
| 439 | /* Should always be able to add fb, as we only add more buffers |
| 440 | * when flipping*/ |
| 441 | if (ret) { |
| 442 | ERROR_MSG( |
| 443 | "Could not add framebuffer to additional back buffer"); |
| 444 | goto error; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* No errors, update pixmap and name */ |
| 449 | *ppPixmap = pPixmap; |
| 450 | *name = new_name; |
| 451 | |
| 452 | return TRUE; |
| 453 | |
| 454 | error: |
| 455 | /* revert to existing pixmap */ |
| 456 | if (pPixmap) { |
| 457 | if (extRegistered) |
| 458 | ARMSOCDeregisterExternalAccess(pPixmap); |
| 459 | pScreen->DestroyPixmap(pPixmap); |
| 460 | } |
| 461 | |
| 462 | return FALSE; |
| 463 | } |
| 464 | |
| 465 | static void nextBuffer(DrawablePtr pDraw, struct ARMSOCDRI2BufferRec *backBuf) |
| 466 | { |
| 467 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 468 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 469 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
| 470 | |
| 471 | if (pARMSOC->driNumBufs <= 2) { |
| 472 | /*Only using double buffering, leave the pixmap as-is */ |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | backBuf->currentPixmap++; |
| 477 | backBuf->currentPixmap %= backBuf->numPixmaps; |
| 478 | |
| 479 | if (backBuf->pPixmaps[backBuf->currentPixmap]) { |
| 480 | /* Already allocated the next buffer - get the name and |
| 481 | * early-out */ |
| 482 | struct armsoc_bo *bo; |
| 483 | int ret; |
| 484 | |
| 485 | bo = ARMSOCPixmapBo(backBuf->pPixmaps[backBuf->currentPixmap]); |
| 486 | assert(bo); |
| 487 | ret = armsoc_bo_get_name(bo, &DRIBUF(backBuf)->name); |
| 488 | assert(!ret); |
| 489 | } else { |
| 490 | Bool ret; |
| 491 | PixmapPtr * const curBackPix = |
| 492 | &backBuf->pPixmaps[backBuf->currentPixmap]; |
| 493 | ret = allocNextBuffer(pDraw, curBackPix, |
| 494 | &DRIBUF(backBuf)->name); |
| 495 | if (!ret) { |
| 496 | /* can't have failed on the first buffer */ |
| 497 | assert(backBuf->currentPixmap > 0); |
| 498 | /* Fall back to last buffer */ |
| 499 | backBuf->currentPixmap--; |
| 500 | WARNING_MSG( |
| 501 | "Failed to use the requested %d-buffering due to an allocation failure.\n" |
| 502 | "Falling back to %d-buffering for this DRI2Drawable", |
| 503 | backBuf->numPixmaps+1, |
| 504 | backBuf->currentPixmap+2); |
| 505 | backBuf->numPixmaps = backBuf->currentPixmap+1; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static struct armsoc_bo *boFromBuffer(DRI2BufferPtr buf) |
| 511 | { |
| 512 | PixmapPtr pPixmap; |
| 513 | struct ARMSOCPixmapPrivRec *priv; |
| 514 | |
| 515 | pPixmap = ARMSOCBUF(buf)->pPixmaps[ARMSOCBUF(buf)->currentPixmap]; |
| 516 | priv = exaGetPixmapDriverPrivate(pPixmap); |
| 517 | return priv->bo; |
| 518 | } |
| 519 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 520 | void |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 521 | ARMSOCDRI2SwapComplete(struct ARMSOCDRISwapCmd *cmd) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 522 | { |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 523 | ScreenPtr pScreen = cmd->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 524 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 525 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 526 | DrawablePtr pDraw = NULL; |
| 527 | int status; |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 528 | struct armsoc_bo *old_src_bo, *old_dst_bo; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 529 | |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 530 | if (--cmd->swapCount > 0) |
| 531 | return; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 532 | |
Sean Paul | 269c971 | 2012-09-04 15:17:33 -0700 | [diff] [blame] | 533 | /* Save the old source bo for unreference below */ |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 534 | old_src_bo = boFromBuffer(cmd->pSrcBuffer); |
| 535 | old_dst_bo = boFromBuffer(cmd->pDstBuffer); |
Sean Paul | 269c971 | 2012-09-04 15:17:33 -0700 | [diff] [blame] | 536 | |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 537 | if ((cmd->flags & ARMSOC_SWAP_FAIL) == 0) { |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 538 | DEBUG_MSG("%s complete: %d -> %d", swap_names[cmd->type], |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 539 | cmd->pSrcBuffer->attachment, |
| 540 | cmd->pDstBuffer->attachment); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 541 | |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 542 | status = dixLookupDrawable(&pDraw, cmd->draw_id, serverClient, |
| 543 | M_ANY, DixWriteAccess); |
| 544 | |
| 545 | if (status == Success) { |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 546 | if (cmd->type != DRI2_BLIT_COMPLETE && |
| 547 | (cmd->flags & ARMSOC_SWAP_FAKE_FLIP) == 0) { |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 548 | assert(cmd->type == DRI2_FLIP_COMPLETE); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 549 | exchangebufs(pDraw, cmd->pSrcBuffer, |
| 550 | cmd->pDstBuffer); |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 551 | |
| 552 | if (cmd->pSrcBuffer->attachment == |
| 553 | DRI2BufferBackLeft) |
| 554 | nextBuffer(pDraw, |
| 555 | ARMSOCBUF(cmd->pSrcBuffer)); |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | DRI2SwapComplete(cmd->client, pDraw, 0, 0, 0, cmd->type, |
| 559 | cmd->func, cmd->data); |
| 560 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 561 | if (cmd->type != DRI2_BLIT_COMPLETE && |
| 562 | (cmd->flags & ARMSOC_SWAP_FAKE_FLIP) == 0) { |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 563 | assert(cmd->type == DRI2_FLIP_COMPLETE); |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 564 | set_scanout_bo(pScrn, |
| 565 | boFromBuffer(cmd->pDstBuffer)); |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 566 | } |
Raymond Smith | 4a0c0aa | 2012-06-20 10:50:39 +0100 | [diff] [blame] | 567 | } |
Raymond Smith | 4a0c0aa | 2012-06-20 10:50:39 +0100 | [diff] [blame] | 568 | } |
David Garbett | 3688b33 | 2012-05-11 12:17:34 +0100 | [diff] [blame] | 569 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 570 | /* drop extra refcnt we obtained prior to swap: |
| 571 | */ |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 572 | ARMSOCDRI2DestroyBuffer(pDraw, cmd->pSrcBuffer); |
| 573 | ARMSOCDRI2DestroyBuffer(pDraw, cmd->pDstBuffer); |
| 574 | armsoc_bo_unreference(old_src_bo); |
| 575 | armsoc_bo_unreference(old_dst_bo); |
| 576 | pARMSOC->pending_flips--; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 577 | |
| 578 | free(cmd); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * ScheduleSwap is responsible for requesting a DRM vblank event for the |
| 583 | * appropriate frame. |
| 584 | * |
| 585 | * In the case of a blit (e.g. for a windowed swap) or buffer exchange, |
| 586 | * the vblank requested can simply be the last queued swap frame + the swap |
| 587 | * interval for the drawable. |
| 588 | * |
| 589 | * In the case of a page flip, we request an event for the last queued swap |
| 590 | * frame + swap interval - 1, since we'll need to queue the flip for the frame |
| 591 | * immediately following the received event. |
| 592 | */ |
| 593 | static int |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 594 | ARMSOCDRI2ScheduleSwap(ClientPtr client, DrawablePtr pDraw, |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 595 | DRI2BufferPtr pDstBuffer, DRI2BufferPtr pSrcBuffer, |
| 596 | CARD64 *target_msc, CARD64 divisor, CARD64 remainder, |
| 597 | DRI2SwapEventPtr func, void *data) |
| 598 | { |
| 599 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 600 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 601 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
| 602 | struct ARMSOCDRI2BufferRec *src = ARMSOCBUF(pSrcBuffer); |
| 603 | struct ARMSOCDRI2BufferRec *dst = ARMSOCBUF(pDstBuffer); |
| 604 | struct ARMSOCDRISwapCmd *cmd = calloc(1, sizeof(*cmd)); |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 605 | struct armsoc_bo *src_bo, *dst_bo; |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 606 | int src_fb_id, dst_fb_id; |
Ray Smith | edccfd8 | 2013-05-31 15:57:12 +0100 | [diff] [blame] | 607 | int new_canflip, ret, do_flip; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 608 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 609 | if (!cmd) |
Paul Geary | 6fe52f3 | 2013-04-03 11:12:24 +0100 | [diff] [blame] | 610 | return FALSE; |
Paul Geary | 6fe52f3 | 2013-04-03 11:12:24 +0100 | [diff] [blame] | 611 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 612 | cmd->client = client; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 613 | cmd->pScreen = pScreen; |
| 614 | cmd->draw_id = pDraw->id; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 615 | cmd->pSrcBuffer = pSrcBuffer; |
| 616 | cmd->pDstBuffer = pDstBuffer; |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 617 | cmd->swapCount = 0; |
| 618 | cmd->flags = 0; |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 619 | cmd->func = func; |
| 620 | cmd->data = data; |
| 621 | |
| 622 | DEBUG_MSG("%d -> %d", pSrcBuffer->attachment, pDstBuffer->attachment); |
| 623 | |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 624 | /* obtain extra ref on buffers to avoid them going away while we await |
| 625 | * the page flip event: |
| 626 | */ |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 627 | ARMSOCDRI2ReferenceBuffer(pSrcBuffer); |
| 628 | ARMSOCDRI2ReferenceBuffer(pDstBuffer); |
| 629 | pARMSOC->pending_flips++; |
Rob Clark | 0bdd370 | 2012-04-20 14:47:51 -0500 | [diff] [blame] | 630 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 631 | src_bo = boFromBuffer(pSrcBuffer); |
| 632 | dst_bo = boFromBuffer(pDstBuffer); |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 633 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 634 | src_fb_id = armsoc_bo_get_fb(src_bo); |
| 635 | dst_fb_id = armsoc_bo_get_fb(dst_bo); |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 636 | |
Raymond Smith | 4a0c0aa | 2012-06-20 10:50:39 +0100 | [diff] [blame] | 637 | new_canflip = canflip(pDraw); |
Liyou Zhou | dcf32bd | 2013-09-20 16:21:11 +0100 | [diff] [blame^] | 638 | if ((src->previous_canflip != new_canflip) || |
| 639 | (dst->previous_canflip != new_canflip)) { |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 640 | /* The drawable has transitioned between being flippable and |
| 641 | * non-flippable or vice versa. Bump the serial number to force |
| 642 | * the DRI2 buffers to be re-allocated during the next frame so |
| 643 | * that: |
| 644 | * - It is able to be scanned out |
| 645 | * (if drawable is now flippable), or |
| 646 | * - It is not taking up possibly scarce scanout-able memory |
| 647 | * (if drawable is now not flippable) |
Raymond Smith | 4a0c0aa | 2012-06-20 10:50:39 +0100 | [diff] [blame] | 648 | */ |
| 649 | |
| 650 | PixmapPtr pPix = pScreen->GetWindowPixmap((WindowPtr)pDraw); |
| 651 | pPix->drawable.serialNumber = NEXT_SERIAL_NUMBER; |
| 652 | } |
| 653 | |
| 654 | src->previous_canflip = new_canflip; |
| 655 | dst->previous_canflip = new_canflip; |
| 656 | |
David Garbett | 4d37be3 | 2013-04-04 09:04:29 +0100 | [diff] [blame] | 657 | armsoc_bo_reference(src_bo); |
| 658 | armsoc_bo_reference(dst_bo); |
Ray Smith | edccfd8 | 2013-05-31 15:57:12 +0100 | [diff] [blame] | 659 | |
| 660 | do_flip = src_fb_id && dst_fb_id && canflip(pDraw); |
| 661 | |
Dave Barnish | e762b12 | 2013-08-12 17:13:46 +0100 | [diff] [blame] | 662 | /* After a resolution change the back buffer (src) will still be |
| 663 | * of the original size. We can't sensibly flip to a framebuffer of |
| 664 | * a different size to the current resolution (it will look corrupted) |
| 665 | * so we must do a copy for this frame (which will clip the contents |
| 666 | * as expected). |
Ray Smith | edccfd8 | 2013-05-31 15:57:12 +0100 | [diff] [blame] | 667 | * |
Dave Barnish | e762b12 | 2013-08-12 17:13:46 +0100 | [diff] [blame] | 668 | * Once the client calls DRI2GetBuffers again, it will receive a new |
| 669 | * back buffer of the same size as the new resolution, and subsequent |
| 670 | * DRI2SwapBuffers will result in a flip. |
Ray Smith | edccfd8 | 2013-05-31 15:57:12 +0100 | [diff] [blame] | 671 | */ |
Dave Barnish | e762b12 | 2013-08-12 17:13:46 +0100 | [diff] [blame] | 672 | do_flip = do_flip && |
| 673 | (armsoc_bo_width(src_bo) == armsoc_bo_width(dst_bo)); |
| 674 | do_flip = do_flip && |
| 675 | (armsoc_bo_height(src_bo) == armsoc_bo_height(dst_bo)); |
Ray Smith | edccfd8 | 2013-05-31 15:57:12 +0100 | [diff] [blame] | 676 | |
| 677 | if (do_flip) { |
David Garbett | 7d6a6e7 | 2012-05-11 11:52:45 +0100 | [diff] [blame] | 678 | DEBUG_MSG("can flip: %d -> %d", src_fb_id, dst_fb_id); |
Rob Clark | a6762ef | 2012-04-12 17:38:39 -0500 | [diff] [blame] | 679 | cmd->type = DRI2_FLIP_COMPLETE; |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 680 | /* TODO: MIDEGL-1461: Handle rollback if multiple CRTC flip is |
| 681 | * only partially successful |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 682 | */ |
| 683 | ret = drmmode_page_flip(pDraw, src_fb_id, cmd); |
| 684 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 685 | /* If using page flip events, we'll trigger an immediate |
| 686 | * completion in the case that no CRTCs were enabled to be |
| 687 | * flipped. If not using page flip events, trigger immediate |
| 688 | * completion unconditionally. |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 689 | */ |
| 690 | if (ret < 0) { |
| 691 | /* |
| 692 | * Error while flipping; bail. |
| 693 | */ |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 694 | cmd->flags |= ARMSOC_SWAP_FAIL; |
Ray Smith | 3c33c3d | 2013-03-26 16:06:37 +0000 | [diff] [blame] | 695 | |
Dave Barnish | de45ed4 | 2013-06-05 13:47:56 +0100 | [diff] [blame] | 696 | if (pARMSOC->drmmode_interface->use_page_flip_events) |
Ray Smith | 3c33c3d | 2013-03-26 16:06:37 +0000 | [diff] [blame] | 697 | cmd->swapCount = -(ret + 1); |
| 698 | else |
| 699 | cmd->swapCount = 0; |
| 700 | |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 701 | if (cmd->swapCount == 0) |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 702 | ARMSOCDRI2SwapComplete(cmd); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 703 | |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 704 | return FALSE; |
| 705 | } else { |
| 706 | if (ret == 0) |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 707 | cmd->flags |= ARMSOC_SWAP_FAKE_FLIP; |
Ray Smith | 3c33c3d | 2013-03-26 16:06:37 +0000 | [diff] [blame] | 708 | |
Dave Barnish | de45ed4 | 2013-06-05 13:47:56 +0100 | [diff] [blame] | 709 | if (pARMSOC->drmmode_interface->use_page_flip_events) |
Ray Smith | 3c33c3d | 2013-03-26 16:06:37 +0000 | [diff] [blame] | 710 | cmd->swapCount = ret; |
| 711 | else |
| 712 | cmd->swapCount = 0; |
| 713 | |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 714 | if (cmd->swapCount == 0) |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 715 | ARMSOCDRI2SwapComplete(cmd); |
John Sheu | 022833e | 2012-08-15 11:40:11 -0700 | [diff] [blame] | 716 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 717 | } else { |
| 718 | /* fallback to blit: */ |
| 719 | BoxRec box = { |
| 720 | .x1 = 0, |
| 721 | .y1 = 0, |
| 722 | .x2 = pDraw->width, |
| 723 | .y2 = pDraw->height, |
| 724 | }; |
| 725 | RegionRec region; |
| 726 | RegionInit(®ion, &box, 0); |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 727 | ARMSOCDRI2CopyRegion(pDraw, ®ion, pDstBuffer, pSrcBuffer); |
Rob Clark | a6762ef | 2012-04-12 17:38:39 -0500 | [diff] [blame] | 728 | cmd->type = DRI2_BLIT_COMPLETE; |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 729 | ARMSOCDRI2SwapComplete(cmd); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | return TRUE; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Request a DRM event when the requested conditions will be satisfied. |
| 737 | * |
| 738 | * We need to handle the event and ask the server to wake up the client when |
| 739 | * we receive it. |
| 740 | */ |
| 741 | static int |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 742 | ARMSOCDRI2ScheduleWaitMSC(ClientPtr client, DrawablePtr pDraw, |
| 743 | CARD64 target_msc, CARD64 divisor, CARD64 remainder) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 744 | { |
| 745 | ScreenPtr pScreen = pDraw->pScreen; |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 746 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 747 | |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 748 | ERROR_MSG("not implemented"); |
| 749 | return FALSE; |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * The DRI2 ScreenInit() function.. register our handler fxns w/ DRI2 core |
| 754 | */ |
| 755 | Bool |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 756 | ARMSOCDRI2ScreenInit(ScreenPtr pScreen) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 757 | { |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 758 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 759 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 760 | DRI2InfoRec info = { |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 761 | .version = 5, |
| 762 | .fd = pARMSOC->drmFD, |
| 763 | .driverName = "armsoc", |
| 764 | .deviceName = pARMSOC->deviceName, |
| 765 | .CreateBuffer = ARMSOCDRI2CreateBuffer, |
| 766 | .DestroyBuffer = ARMSOCDRI2DestroyBuffer, |
| 767 | .CopyRegion = ARMSOCDRI2CopyRegion, |
| 768 | .ScheduleSwap = ARMSOCDRI2ScheduleSwap, |
| 769 | .ScheduleWaitMSC = ARMSOCDRI2ScheduleWaitMSC, |
| 770 | .GetMSC = ARMSOCDRI2GetMSC, |
| 771 | .AuthMagic = drmAuthMagic, |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 772 | }; |
| 773 | int minor = 1, major = 0; |
| 774 | |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 775 | if (xf86LoaderCheckSymbol("DRI2Version")) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 776 | DRI2Version(&major, &minor); |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 777 | |
| 778 | if (minor < 1) { |
| 779 | WARNING_MSG("DRI2 requires DRI2 module version 1.1.0 or later"); |
| 780 | return FALSE; |
| 781 | } |
| 782 | |
| 783 | return DRI2ScreenInit(pScreen, &info); |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * The DRI2 CloseScreen() function.. unregister ourself w/ DRI2 core. |
| 788 | */ |
| 789 | void |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 790 | ARMSOCDRI2CloseScreen(ScreenPtr pScreen) |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 791 | { |
Daniel Kurtz | 0361e00 | 2013-08-14 21:07:01 +0800 | [diff] [blame] | 792 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); |
Dave Barnish | 2e99895 | 2013-06-11 16:31:10 +0100 | [diff] [blame] | 793 | struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); |
Paul Geary | 8ffd91c | 2013-04-11 16:03:15 +0100 | [diff] [blame] | 794 | while (pARMSOC->pending_flips > 0) { |
Rob Clark | 67b875f | 2012-04-20 19:13:57 -0500 | [diff] [blame] | 795 | DEBUG_MSG("waiting.."); |
| 796 | drmmode_wait_for_event(pScrn); |
| 797 | } |
Rob Clark | 4b8f30a | 2011-08-28 12:51:26 -0500 | [diff] [blame] | 798 | DRI2CloseScreen(pScreen); |
| 799 | } |