1/* 2 * Copyright � 2009 Maarten Maathuis 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 * 23 */ 24 25#ifdef HAVE_DIX_CONFIG_H 26#include <dix-config.h> 27#endif 28 29#include <string.h> 30 31#include "exa_priv.h" 32#include "exa.h" 33 34/* This file holds the driver allocated pixmaps + better initial placement code. 35 */ 36 37static _X_INLINE void* 38ExaGetPixmapAddress(PixmapPtr p) 39{ 40 ExaPixmapPriv(p); 41 42 return pExaPixmap->sys_ptr; 43} 44 45/** 46 * exaCreatePixmap() creates a new pixmap. 47 */ 48PixmapPtr 49exaCreatePixmap_mixed(ScreenPtr pScreen, int w, int h, int depth, 50 unsigned usage_hint) 51{ 52 PixmapPtr pPixmap; 53 ExaPixmapPrivPtr pExaPixmap; 54 int bpp; 55 size_t paddedWidth; 56 ExaScreenPriv(pScreen); 57 58 if (w > 32767 || h > 32767) 59 return NullPixmap; 60 61 swap(pExaScr, pScreen, CreatePixmap); 62 pPixmap = pScreen->CreatePixmap(pScreen, 0, 0, depth, usage_hint); 63 swap(pExaScr, pScreen, CreatePixmap); 64 65 if (!pPixmap) 66 return NULL; 67 68 pExaPixmap = ExaGetPixmapPriv(pPixmap); 69 pExaPixmap->driverPriv = NULL; 70 71 bpp = pPixmap->drawable.bitsPerPixel; 72 73 paddedWidth = ((w * bpp + FB_MASK) >> FB_SHIFT) * sizeof(FbBits); 74 if (paddedWidth / 4 > 32767 || h > 32767) 75 return NullPixmap; 76 77 /* We will allocate the system pixmap later if needed. */ 78 pPixmap->devPrivate.ptr = NULL; 79 pExaPixmap->sys_ptr = NULL; 80 pExaPixmap->sys_pitch = paddedWidth; 81 82 pExaPixmap->area = NULL; 83 pExaPixmap->fb_ptr = NULL; 84 pExaPixmap->pDamage = NULL; 85 86 exaSetFbPitch(pExaScr, pExaPixmap, w, h, bpp); 87 exaSetAccelBlock(pExaScr, pExaPixmap, 88 w, h, bpp); 89 90 (*pScreen->ModifyPixmapHeader)(pPixmap, w, h, 0, 0, 91 paddedWidth, NULL); 92 93 /* A scratch pixmap will become a driver pixmap right away. */ 94 if (!w || !h) { 95 exaCreateDriverPixmap_mixed(pPixmap); 96 pExaPixmap->use_gpu_copy = exaPixmapHasGpuCopy(pPixmap); 97 } else { 98 pExaPixmap->use_gpu_copy = FALSE; 99 100 if (w == 1 && h == 1) { 101 pExaPixmap->sys_ptr = malloc(paddedWidth); 102 103 /* Set up damage tracking */ 104 pExaPixmap->pDamage = DamageCreate(exaDamageReport_mixed, NULL, 105 DamageReportNonEmpty, TRUE, 106 pPixmap->drawable.pScreen, 107 pPixmap); 108 109 DamageRegister(&pPixmap->drawable, pExaPixmap->pDamage); 110 /* This ensures that pending damage reflects the current operation. */ 111 /* This is used by exa to optimize migration. */ 112 DamageSetReportAfterOp(pExaPixmap->pDamage, TRUE); 113 } 114 } 115 116 /* During a fallback we must prepare access. */ 117 if (pExaScr->fallback_counter) 118 exaPrepareAccess(&pPixmap->drawable, EXA_PREPARE_AUX_DEST); 119 120 return pPixmap; 121} 122 123Bool 124exaModifyPixmapHeader_mixed(PixmapPtr pPixmap, int width, int height, int depth, 125 int bitsPerPixel, int devKind, pointer pPixData) 126{ 127 ScreenPtr pScreen; 128 ExaScreenPrivPtr pExaScr; 129 ExaPixmapPrivPtr pExaPixmap; 130 Bool ret, has_gpu_copy; 131 132 if (!pPixmap) 133 return FALSE; 134 135 pScreen = pPixmap->drawable.pScreen; 136 pExaScr = ExaGetScreenPriv(pScreen); 137 pExaPixmap = ExaGetPixmapPriv(pPixmap); 138 139 if (pPixData) { 140 if (pExaPixmap->driverPriv) { 141 if (pExaPixmap->pDamage) { 142 DamageUnregister(&pPixmap->drawable, pExaPixmap->pDamage); 143 DamageDestroy(pExaPixmap->pDamage); 144 pExaPixmap->pDamage = NULL; 145 } 146 147 pExaScr->info->DestroyPixmap(pScreen, pExaPixmap->driverPriv); 148 pExaPixmap->driverPriv = NULL; 149 } 150 151 pExaPixmap->use_gpu_copy = FALSE; 152 pExaPixmap->score = EXA_PIXMAP_SCORE_PINNED; 153 } 154 155 has_gpu_copy = exaPixmapHasGpuCopy(pPixmap); 156 157 if (width <= 0) 158 width = pPixmap->drawable.width; 159 160 if (height <= 0) 161 height = pPixmap->drawable.height; 162 163 if (bitsPerPixel <= 0) { 164 if (depth <= 0) 165 bitsPerPixel = pPixmap->drawable.bitsPerPixel; 166 else 167 bitsPerPixel = BitsPerPixel(depth); 168 } 169 170 if (depth <= 0) 171 depth = pPixmap->drawable.depth; 172 173 if (width != pPixmap->drawable.width || 174 height != pPixmap->drawable.height || 175 depth != pPixmap->drawable.depth || 176 bitsPerPixel != pPixmap->drawable.bitsPerPixel) { 177 if (pExaPixmap->driverPriv) { 178 if (devKind > 0) 179 pExaPixmap->fb_pitch = devKind; 180 else 181 exaSetFbPitch(pExaScr, pExaPixmap, width, height, bitsPerPixel); 182 183 exaSetAccelBlock(pExaScr, pExaPixmap, 184 width, height, bitsPerPixel); 185 RegionEmpty(&pExaPixmap->validFB); 186 } 187 188 /* Need to re-create system copy if there's also a GPU copy */ 189 if (has_gpu_copy) { 190 if (pExaPixmap->sys_ptr) { 191 free(pExaPixmap->sys_ptr); 192 pExaPixmap->sys_ptr = NULL; 193 DamageUnregister(&pPixmap->drawable, pExaPixmap->pDamage); 194 DamageDestroy(pExaPixmap->pDamage); 195 pExaPixmap->pDamage = NULL; 196 RegionEmpty(&pExaPixmap->validSys); 197 198 if (pExaScr->deferred_mixed_pixmap == pPixmap) 199 pExaScr->deferred_mixed_pixmap = NULL; 200 } 201 202 pExaPixmap->sys_pitch = PixmapBytePad(width, depth); 203 } 204 } 205 206 if (has_gpu_copy) { 207 pPixmap->devPrivate.ptr = pExaPixmap->fb_ptr; 208 pPixmap->devKind = pExaPixmap->fb_pitch; 209 } else { 210 pPixmap->devPrivate.ptr = pExaPixmap->sys_ptr; 211 pPixmap->devKind = pExaPixmap->sys_pitch; 212 } 213 214 /* Only pass driver pixmaps to the driver. */ 215 if (pExaScr->info->ModifyPixmapHeader && pExaPixmap->driverPriv) { 216 ret = pExaScr->info->ModifyPixmapHeader(pPixmap, width, height, depth, 217 bitsPerPixel, devKind, pPixData); 218 if (ret == TRUE) 219 goto out; 220 } 221 222 swap(pExaScr, pScreen, ModifyPixmapHeader); 223 ret = pScreen->ModifyPixmapHeader(pPixmap, width, height, depth, 224 bitsPerPixel, devKind, pPixData); 225 swap(pExaScr, pScreen, ModifyPixmapHeader); 226 227out: 228 if (has_gpu_copy) { 229 pExaPixmap->fb_ptr = pPixmap->devPrivate.ptr; 230 pExaPixmap->fb_pitch = pPixmap->devKind; 231 } else { 232 pExaPixmap->sys_ptr = pPixmap->devPrivate.ptr; 233 pExaPixmap->sys_pitch = pPixmap->devKind; 234 } 235 /* Always NULL this, we don't want lingering pointers. */ 236 pPixmap->devPrivate.ptr = NULL; 237 238 return ret; 239} 240 241Bool 242exaDestroyPixmap_mixed(PixmapPtr pPixmap) 243{ 244 ScreenPtr pScreen = pPixmap->drawable.pScreen; 245 ExaScreenPriv(pScreen); 246 Bool ret; 247 248 if (pPixmap->refcnt == 1) 249 { 250 ExaPixmapPriv (pPixmap); 251 252 exaDestroyPixmap(pPixmap); 253 254 if (pExaScr->deferred_mixed_pixmap == pPixmap) 255 pExaScr->deferred_mixed_pixmap = NULL; 256 257 if (pExaPixmap->driverPriv) 258 pExaScr->info->DestroyPixmap(pScreen, pExaPixmap->driverPriv); 259 pExaPixmap->driverPriv = NULL; 260 261 if (pExaPixmap->pDamage) { 262 free(pExaPixmap->sys_ptr); 263 pExaPixmap->sys_ptr = NULL; 264 pExaPixmap->pDamage = NULL; 265 } 266 } 267 268 swap(pExaScr, pScreen, DestroyPixmap); 269 ret = pScreen->DestroyPixmap (pPixmap); 270 swap(pExaScr, pScreen, DestroyPixmap); 271 272 return ret; 273} 274 275Bool 276exaPixmapHasGpuCopy_mixed(PixmapPtr pPixmap) 277{ 278 ScreenPtr pScreen = pPixmap->drawable.pScreen; 279 ExaScreenPriv(pScreen); 280 ExaPixmapPriv(pPixmap); 281 pointer saved_ptr; 282 Bool ret; 283 284 if (!pExaPixmap->driverPriv) 285 return FALSE; 286 287 saved_ptr = pPixmap->devPrivate.ptr; 288 pPixmap->devPrivate.ptr = ExaGetPixmapAddress(pPixmap); 289 ret = pExaScr->info->PixmapIsOffscreen(pPixmap); 290 pPixmap->devPrivate.ptr = saved_ptr; 291 292 return ret; 293} 294