xref: /OK3568_Linux_fs/external/xserver/exa/exa_migration_mixed.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2009 Maarten Maathuis
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*4882a593Smuzhiyun  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21*4882a593Smuzhiyun  * SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
26*4882a593Smuzhiyun #include <dix-config.h>
27*4882a593Smuzhiyun #endif
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <string.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include "exa_priv.h"
32*4882a593Smuzhiyun #include "exa.h"
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun void
exaCreateDriverPixmap_mixed(PixmapPtr pPixmap)35*4882a593Smuzhiyun exaCreateDriverPixmap_mixed(PixmapPtr pPixmap)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun     ScreenPtr pScreen = pPixmap->drawable.pScreen;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
40*4882a593Smuzhiyun     ExaPixmapPriv(pPixmap);
41*4882a593Smuzhiyun     int w = pPixmap->drawable.width, h = pPixmap->drawable.height;
42*4882a593Smuzhiyun     int depth = pPixmap->drawable.depth, bpp = pPixmap->drawable.bitsPerPixel;
43*4882a593Smuzhiyun     int usage_hint = pPixmap->usage_hint;
44*4882a593Smuzhiyun     int paddedWidth = pExaPixmap->sys_pitch;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun     /* Already done. */
47*4882a593Smuzhiyun     if (pExaPixmap->driverPriv)
48*4882a593Smuzhiyun         return;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun     if (exaPixmapIsPinned(pPixmap))
51*4882a593Smuzhiyun         return;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun     /* Can't accel 1/4 bpp. */
54*4882a593Smuzhiyun     if (pExaPixmap->accel_blocked || bpp < 8)
55*4882a593Smuzhiyun         return;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun     if (pExaScr->info->CreatePixmap2) {
58*4882a593Smuzhiyun         int new_pitch = 0;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun         pExaPixmap->driverPriv =
61*4882a593Smuzhiyun             pExaScr->info->CreatePixmap2(pScreen, w, h, depth, usage_hint, bpp,
62*4882a593Smuzhiyun                                          &new_pitch);
63*4882a593Smuzhiyun         paddedWidth = pExaPixmap->fb_pitch = new_pitch;
64*4882a593Smuzhiyun     }
65*4882a593Smuzhiyun     else {
66*4882a593Smuzhiyun         if (paddedWidth < pExaPixmap->fb_pitch)
67*4882a593Smuzhiyun             paddedWidth = pExaPixmap->fb_pitch;
68*4882a593Smuzhiyun         pExaPixmap->driverPriv =
69*4882a593Smuzhiyun             pExaScr->info->CreatePixmap(pScreen, paddedWidth * h, 0);
70*4882a593Smuzhiyun     }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun     if (!pExaPixmap->driverPriv)
73*4882a593Smuzhiyun         return;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun     (*pScreen->ModifyPixmapHeader) (pPixmap, w, h, 0, 0, paddedWidth, NULL);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun void
exaDoMigration_mixed(ExaMigrationPtr pixmaps,int npixmaps,Bool can_accel)79*4882a593Smuzhiyun exaDoMigration_mixed(ExaMigrationPtr pixmaps, int npixmaps, Bool can_accel)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun     int i;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun     /* If anything is pinned in system memory, we won't be able to
84*4882a593Smuzhiyun      * accelerate.
85*4882a593Smuzhiyun      */
86*4882a593Smuzhiyun     for (i = 0; i < npixmaps; i++) {
87*4882a593Smuzhiyun         if (exaPixmapIsPinned(pixmaps[i].pPix) &&
88*4882a593Smuzhiyun             !exaPixmapHasGpuCopy(pixmaps[i].pPix)) {
89*4882a593Smuzhiyun             can_accel = FALSE;
90*4882a593Smuzhiyun             break;
91*4882a593Smuzhiyun         }
92*4882a593Smuzhiyun     }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun     /* We can do nothing. */
95*4882a593Smuzhiyun     if (!can_accel)
96*4882a593Smuzhiyun         return;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun     for (i = 0; i < npixmaps; i++) {
99*4882a593Smuzhiyun         PixmapPtr pPixmap = pixmaps[i].pPix;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun         ExaPixmapPriv(pPixmap);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun         if (!pExaPixmap->driverPriv)
104*4882a593Smuzhiyun             exaCreateDriverPixmap_mixed(pPixmap);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun         if (pExaPixmap->pDamage && exaPixmapHasGpuCopy(pPixmap)) {
107*4882a593Smuzhiyun             ExaScreenPriv(pPixmap->drawable.pScreen);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun             /* This pitch is needed for proper acceleration. For some reason
110*4882a593Smuzhiyun              * there are pixmaps without pDamage and a bad fb_pitch value.
111*4882a593Smuzhiyun              * So setting devKind when only exaPixmapHasGpuCopy() is true
112*4882a593Smuzhiyun              * causes corruption. Pixmaps without pDamage are not migrated
113*4882a593Smuzhiyun              * and should have a valid devKind at all times, so that's why this
114*4882a593Smuzhiyun              * isn't causing problems. Pixmaps have their gpu pitch set the
115*4882a593Smuzhiyun              * first time in the MPH call from exaCreateDriverPixmap_mixed().
116*4882a593Smuzhiyun              */
117*4882a593Smuzhiyun             pPixmap->devKind = pExaPixmap->fb_pitch;
118*4882a593Smuzhiyun             exaCopyDirtyToFb(pixmaps + i);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun             if (pExaScr->deferred_mixed_pixmap == pPixmap &&
121*4882a593Smuzhiyun                 !pixmaps[i].as_dst && !pixmaps[i].pReg)
122*4882a593Smuzhiyun                 pExaScr->deferred_mixed_pixmap = NULL;
123*4882a593Smuzhiyun         }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun         pExaPixmap->use_gpu_copy = exaPixmapHasGpuCopy(pPixmap);
126*4882a593Smuzhiyun     }
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun void
exaMoveInPixmap_mixed(PixmapPtr pPixmap)130*4882a593Smuzhiyun exaMoveInPixmap_mixed(PixmapPtr pPixmap)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun     ExaMigrationRec pixmaps[1];
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun     pixmaps[0].as_dst = FALSE;
135*4882a593Smuzhiyun     pixmaps[0].as_src = TRUE;
136*4882a593Smuzhiyun     pixmaps[0].pPix = pPixmap;
137*4882a593Smuzhiyun     pixmaps[0].pReg = NULL;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun     exaDoMigration(pixmaps, 1, TRUE);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun void
exaDamageReport_mixed(DamagePtr pDamage,RegionPtr pRegion,void * closure)143*4882a593Smuzhiyun exaDamageReport_mixed(DamagePtr pDamage, RegionPtr pRegion, void *closure)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun     PixmapPtr pPixmap = closure;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun     ExaPixmapPriv(pPixmap);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun     /* Move back results of software rendering on system memory copy of mixed driver
150*4882a593Smuzhiyun      * pixmap (see exaPrepareAccessReg_mixed).
151*4882a593Smuzhiyun      *
152*4882a593Smuzhiyun      * Defer moving the destination back into the driver pixmap, to try and save
153*4882a593Smuzhiyun      * overhead on multiple subsequent software fallbacks.
154*4882a593Smuzhiyun      */
155*4882a593Smuzhiyun     if (!pExaPixmap->use_gpu_copy && exaPixmapHasGpuCopy(pPixmap)) {
156*4882a593Smuzhiyun         ExaScreenPriv(pPixmap->drawable.pScreen);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun         if (pExaScr->deferred_mixed_pixmap &&
159*4882a593Smuzhiyun             pExaScr->deferred_mixed_pixmap != pPixmap)
160*4882a593Smuzhiyun             exaMoveInPixmap_mixed(pExaScr->deferred_mixed_pixmap);
161*4882a593Smuzhiyun         pExaScr->deferred_mixed_pixmap = pPixmap;
162*4882a593Smuzhiyun     }
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* With mixed pixmaps, if we fail to get direct access to the driver pixmap, we
166*4882a593Smuzhiyun  * use the DownloadFromScreen hook to retrieve contents to a copy in system
167*4882a593Smuzhiyun  * memory, perform software rendering on that and move back the results with the
168*4882a593Smuzhiyun  * UploadToScreen hook (see exaDamageReport_mixed).
169*4882a593Smuzhiyun  */
170*4882a593Smuzhiyun void
exaPrepareAccessReg_mixed(PixmapPtr pPixmap,int index,RegionPtr pReg)171*4882a593Smuzhiyun exaPrepareAccessReg_mixed(PixmapPtr pPixmap, int index, RegionPtr pReg)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun     ExaPixmapPriv(pPixmap);
174*4882a593Smuzhiyun     Bool has_gpu_copy = exaPixmapHasGpuCopy(pPixmap);
175*4882a593Smuzhiyun     Bool success;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun     success = ExaDoPrepareAccess(pPixmap, index);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun     if (success && has_gpu_copy && pExaPixmap->pDamage) {
180*4882a593Smuzhiyun         /* You cannot do accelerated operations while a buffer is mapped. */
181*4882a593Smuzhiyun         exaFinishAccess(&pPixmap->drawable, index);
182*4882a593Smuzhiyun         /* Update the gpu view of both deferred destination pixmaps and of
183*4882a593Smuzhiyun          * source pixmaps that were migrated with a bounding region.
184*4882a593Smuzhiyun          */
185*4882a593Smuzhiyun         exaMoveInPixmap_mixed(pPixmap);
186*4882a593Smuzhiyun         success = ExaDoPrepareAccess(pPixmap, index);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun         if (success) {
189*4882a593Smuzhiyun             /* We have a gpu pixmap that can be accessed, we don't need the cpu
190*4882a593Smuzhiyun              * copy anymore. Drivers that prefer DFS, should fail prepare
191*4882a593Smuzhiyun              * access.
192*4882a593Smuzhiyun              */
193*4882a593Smuzhiyun             DamageDestroy(pExaPixmap->pDamage);
194*4882a593Smuzhiyun             pExaPixmap->pDamage = NULL;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun             free(pExaPixmap->sys_ptr);
197*4882a593Smuzhiyun             pExaPixmap->sys_ptr = NULL;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun             return;
200*4882a593Smuzhiyun         }
201*4882a593Smuzhiyun     }
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun     if (!success) {
204*4882a593Smuzhiyun         ExaMigrationRec pixmaps[1];
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun         /* Do we need to allocate our system buffer? */
207*4882a593Smuzhiyun         if (!pExaPixmap->sys_ptr) {
208*4882a593Smuzhiyun             pExaPixmap->sys_ptr = xallocarray(pExaPixmap->sys_pitch,
209*4882a593Smuzhiyun                                               pPixmap->drawable.height);
210*4882a593Smuzhiyun             if (!pExaPixmap->sys_ptr)
211*4882a593Smuzhiyun                 FatalError("EXA: malloc failed for size %d bytes\n",
212*4882a593Smuzhiyun                            pExaPixmap->sys_pitch * pPixmap->drawable.height);
213*4882a593Smuzhiyun         }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun         if (index == EXA_PREPARE_DEST || index == EXA_PREPARE_AUX_DEST) {
216*4882a593Smuzhiyun             pixmaps[0].as_dst = TRUE;
217*4882a593Smuzhiyun             pixmaps[0].as_src = FALSE;
218*4882a593Smuzhiyun         }
219*4882a593Smuzhiyun         else {
220*4882a593Smuzhiyun             pixmaps[0].as_dst = FALSE;
221*4882a593Smuzhiyun             pixmaps[0].as_src = TRUE;
222*4882a593Smuzhiyun         }
223*4882a593Smuzhiyun         pixmaps[0].pPix = pPixmap;
224*4882a593Smuzhiyun         pixmaps[0].pReg = pReg;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun         if (!pExaPixmap->pDamage &&
227*4882a593Smuzhiyun             (has_gpu_copy || !exaPixmapIsPinned(pPixmap))) {
228*4882a593Smuzhiyun             Bool as_dst = pixmaps[0].as_dst;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun             /* Set up damage tracking */
231*4882a593Smuzhiyun             pExaPixmap->pDamage = DamageCreate(exaDamageReport_mixed, NULL,
232*4882a593Smuzhiyun                                                DamageReportNonEmpty, TRUE,
233*4882a593Smuzhiyun                                                pPixmap->drawable.pScreen,
234*4882a593Smuzhiyun                                                pPixmap);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun             if (pExaPixmap->pDamage) {
237*4882a593Smuzhiyun                 DamageRegister(&pPixmap->drawable, pExaPixmap->pDamage);
238*4882a593Smuzhiyun                 /* This ensures that pending damage reflects the current
239*4882a593Smuzhiyun                  * operation. This is used by exa to optimize migration.
240*4882a593Smuzhiyun                  */
241*4882a593Smuzhiyun                 DamageSetReportAfterOp(pExaPixmap->pDamage, TRUE);
242*4882a593Smuzhiyun             }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun             if (has_gpu_copy) {
245*4882a593Smuzhiyun                 exaPixmapDirty(pPixmap, 0, 0, pPixmap->drawable.width,
246*4882a593Smuzhiyun                                pPixmap->drawable.height);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun                 /* We don't know which region of the destination will be damaged,
249*4882a593Smuzhiyun                  * have to assume all of it
250*4882a593Smuzhiyun                  */
251*4882a593Smuzhiyun                 if (as_dst) {
252*4882a593Smuzhiyun                     pixmaps[0].as_dst = FALSE;
253*4882a593Smuzhiyun                     pixmaps[0].as_src = TRUE;
254*4882a593Smuzhiyun                     pixmaps[0].pReg = NULL;
255*4882a593Smuzhiyun                 }
256*4882a593Smuzhiyun                 exaCopyDirtyToSys(pixmaps);
257*4882a593Smuzhiyun             }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun             if (as_dst)
260*4882a593Smuzhiyun                 exaPixmapDirty(pPixmap, 0, 0, pPixmap->drawable.width,
261*4882a593Smuzhiyun                                pPixmap->drawable.height);
262*4882a593Smuzhiyun         }
263*4882a593Smuzhiyun         else if (has_gpu_copy)
264*4882a593Smuzhiyun             exaCopyDirtyToSys(pixmaps);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun         pPixmap->devPrivate.ptr = pExaPixmap->sys_ptr;
267*4882a593Smuzhiyun         pPixmap->devKind = pExaPixmap->sys_pitch;
268*4882a593Smuzhiyun         pExaPixmap->use_gpu_copy = FALSE;
269*4882a593Smuzhiyun     }
270*4882a593Smuzhiyun }
271