xref: /OK3568_Linux_fs/external/xserver/exa/exa_driver.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 /* This file holds the driver allocated pixmaps specific implementation. */
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static _X_INLINE void *
ExaGetPixmapAddress(PixmapPtr p)37*4882a593Smuzhiyun ExaGetPixmapAddress(PixmapPtr p)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun     ExaPixmapPriv(p);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun     return pExaPixmap->sys_ptr;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun  * exaCreatePixmap() creates a new pixmap.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * Pixmaps are always marked as pinned, because exa has no control over them.
48*4882a593Smuzhiyun  */
49*4882a593Smuzhiyun PixmapPtr
exaCreatePixmap_driver(ScreenPtr pScreen,int w,int h,int depth,unsigned usage_hint)50*4882a593Smuzhiyun exaCreatePixmap_driver(ScreenPtr pScreen, int w, int h, int depth,
51*4882a593Smuzhiyun                        unsigned usage_hint)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun     PixmapPtr pPixmap;
54*4882a593Smuzhiyun     ExaPixmapPrivPtr pExaPixmap;
55*4882a593Smuzhiyun     int bpp;
56*4882a593Smuzhiyun     size_t paddedWidth, datasize;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun     if (w > 32767 || h > 32767)
61*4882a593Smuzhiyun         return NullPixmap;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun     swap(pExaScr, pScreen, CreatePixmap);
64*4882a593Smuzhiyun     pPixmap = pScreen->CreatePixmap(pScreen, 0, 0, depth, usage_hint);
65*4882a593Smuzhiyun     swap(pExaScr, pScreen, CreatePixmap);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun     if (!pPixmap)
68*4882a593Smuzhiyun         return NULL;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun     pExaPixmap = ExaGetPixmapPriv(pPixmap);
71*4882a593Smuzhiyun     pExaPixmap->driverPriv = NULL;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun     bpp = pPixmap->drawable.bitsPerPixel;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun     /* Set this before driver hooks, to allow for driver pixmaps without gpu
76*4882a593Smuzhiyun      * memory to back it. These pixmaps have a valid pointer at all times.
77*4882a593Smuzhiyun      */
78*4882a593Smuzhiyun     pPixmap->devPrivate.ptr = NULL;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun     if (pExaScr->info->CreatePixmap2) {
81*4882a593Smuzhiyun         int new_pitch = 0;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun         pExaPixmap->driverPriv =
84*4882a593Smuzhiyun             pExaScr->info->CreatePixmap2(pScreen, w, h, depth, usage_hint, bpp,
85*4882a593Smuzhiyun                                          &new_pitch);
86*4882a593Smuzhiyun         paddedWidth = pExaPixmap->fb_pitch = new_pitch;
87*4882a593Smuzhiyun     }
88*4882a593Smuzhiyun     else {
89*4882a593Smuzhiyun         paddedWidth = ((w * bpp + FB_MASK) >> FB_SHIFT) * sizeof(FbBits);
90*4882a593Smuzhiyun         if (paddedWidth / 4 > 32767 || h > 32767)
91*4882a593Smuzhiyun             return NullPixmap;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun         exaSetFbPitch(pExaScr, pExaPixmap, w, h, bpp);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun         if (paddedWidth < pExaPixmap->fb_pitch)
96*4882a593Smuzhiyun             paddedWidth = pExaPixmap->fb_pitch;
97*4882a593Smuzhiyun         datasize = h * paddedWidth;
98*4882a593Smuzhiyun         pExaPixmap->driverPriv =
99*4882a593Smuzhiyun             pExaScr->info->CreatePixmap(pScreen, datasize, 0);
100*4882a593Smuzhiyun     }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun     if (!pExaPixmap->driverPriv) {
103*4882a593Smuzhiyun         swap(pExaScr, pScreen, DestroyPixmap);
104*4882a593Smuzhiyun         pScreen->DestroyPixmap(pPixmap);
105*4882a593Smuzhiyun         swap(pExaScr, pScreen, DestroyPixmap);
106*4882a593Smuzhiyun         return NULL;
107*4882a593Smuzhiyun     }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun     /* Allow ModifyPixmapHeader to set sys_ptr appropriately. */
110*4882a593Smuzhiyun     pExaPixmap->score = EXA_PIXMAP_SCORE_PINNED;
111*4882a593Smuzhiyun     pExaPixmap->fb_ptr = NULL;
112*4882a593Smuzhiyun     pExaPixmap->pDamage = NULL;
113*4882a593Smuzhiyun     pExaPixmap->sys_ptr = NULL;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun     (*pScreen->ModifyPixmapHeader) (pPixmap, w, h, 0, 0, paddedWidth, NULL);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun     pExaPixmap->area = NULL;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun     exaSetAccelBlock(pExaScr, pExaPixmap, w, h, bpp);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun     pExaPixmap->use_gpu_copy = exaPixmapHasGpuCopy(pPixmap);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun     /* During a fallback we must prepare access. */
124*4882a593Smuzhiyun     if (pExaScr->fallback_counter)
125*4882a593Smuzhiyun         exaPrepareAccess(&pPixmap->drawable, EXA_PREPARE_AUX_DEST);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun     return pPixmap;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun Bool
exaModifyPixmapHeader_driver(PixmapPtr pPixmap,int width,int height,int depth,int bitsPerPixel,int devKind,void * pPixData)131*4882a593Smuzhiyun exaModifyPixmapHeader_driver(PixmapPtr pPixmap, int width, int height,
132*4882a593Smuzhiyun                              int depth, int bitsPerPixel, int devKind,
133*4882a593Smuzhiyun                              void *pPixData)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun     ScreenPtr pScreen;
136*4882a593Smuzhiyun     ExaScreenPrivPtr pExaScr;
137*4882a593Smuzhiyun     ExaPixmapPrivPtr pExaPixmap;
138*4882a593Smuzhiyun     Bool ret;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun     if (!pPixmap)
141*4882a593Smuzhiyun         return FALSE;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun     pScreen = pPixmap->drawable.pScreen;
144*4882a593Smuzhiyun     pExaScr = ExaGetScreenPriv(pScreen);
145*4882a593Smuzhiyun     pExaPixmap = ExaGetPixmapPriv(pPixmap);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun     if (pExaPixmap) {
148*4882a593Smuzhiyun         if (pPixData)
149*4882a593Smuzhiyun             pExaPixmap->sys_ptr = pPixData;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun         if (devKind > 0)
152*4882a593Smuzhiyun             pExaPixmap->sys_pitch = devKind;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun         if (width > 0 && height > 0 && bitsPerPixel > 0) {
155*4882a593Smuzhiyun             exaSetFbPitch(pExaScr, pExaPixmap, width, height, bitsPerPixel);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun             exaSetAccelBlock(pExaScr, pExaPixmap, width, height, bitsPerPixel);
158*4882a593Smuzhiyun         }
159*4882a593Smuzhiyun     }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun     if (pExaScr->info->ModifyPixmapHeader) {
162*4882a593Smuzhiyun         ret = pExaScr->info->ModifyPixmapHeader(pPixmap, width, height, depth,
163*4882a593Smuzhiyun                                                 bitsPerPixel, devKind,
164*4882a593Smuzhiyun                                                 pPixData);
165*4882a593Smuzhiyun         /* For EXA_HANDLES_PIXMAPS, we set pPixData to NULL.
166*4882a593Smuzhiyun          * If pPixmap->devPrivate.ptr is non-NULL, then we've got a
167*4882a593Smuzhiyun          * !has_gpu_copy pixmap. We need to store the pointer,
168*4882a593Smuzhiyun          * because PrepareAccess won't be called.
169*4882a593Smuzhiyun          */
170*4882a593Smuzhiyun         if (!pPixData && pPixmap->devPrivate.ptr && pPixmap->devKind) {
171*4882a593Smuzhiyun             pExaPixmap->sys_ptr = pPixmap->devPrivate.ptr;
172*4882a593Smuzhiyun             pExaPixmap->sys_pitch = pPixmap->devKind;
173*4882a593Smuzhiyun         }
174*4882a593Smuzhiyun         if (ret == TRUE)
175*4882a593Smuzhiyun             goto out;
176*4882a593Smuzhiyun     }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun     swap(pExaScr, pScreen, ModifyPixmapHeader);
179*4882a593Smuzhiyun     ret = pScreen->ModifyPixmapHeader(pPixmap, width, height, depth,
180*4882a593Smuzhiyun                                       bitsPerPixel, devKind, pPixData);
181*4882a593Smuzhiyun     swap(pExaScr, pScreen, ModifyPixmapHeader);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun  out:
184*4882a593Smuzhiyun     /* Always NULL this, we don't want lingering pointers. */
185*4882a593Smuzhiyun     pPixmap->devPrivate.ptr = NULL;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun     return ret;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun Bool
exaDestroyPixmap_driver(PixmapPtr pPixmap)191*4882a593Smuzhiyun exaDestroyPixmap_driver(PixmapPtr pPixmap)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun     ScreenPtr pScreen = pPixmap->drawable.pScreen;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
196*4882a593Smuzhiyun     Bool ret;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun     if (pPixmap->refcnt == 1) {
199*4882a593Smuzhiyun         ExaPixmapPriv(pPixmap);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun         exaDestroyPixmap(pPixmap);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun         if (pExaPixmap->driverPriv)
204*4882a593Smuzhiyun             pExaScr->info->DestroyPixmap(pScreen, pExaPixmap->driverPriv);
205*4882a593Smuzhiyun         pExaPixmap->driverPriv = NULL;
206*4882a593Smuzhiyun     }
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun     swap(pExaScr, pScreen, DestroyPixmap);
209*4882a593Smuzhiyun     ret = pScreen->DestroyPixmap(pPixmap);
210*4882a593Smuzhiyun     swap(pExaScr, pScreen, DestroyPixmap);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun     return ret;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun Bool
exaPixmapHasGpuCopy_driver(PixmapPtr pPixmap)216*4882a593Smuzhiyun exaPixmapHasGpuCopy_driver(PixmapPtr pPixmap)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun     ScreenPtr pScreen = pPixmap->drawable.pScreen;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
221*4882a593Smuzhiyun     void *saved_ptr;
222*4882a593Smuzhiyun     Bool ret;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun     saved_ptr = pPixmap->devPrivate.ptr;
225*4882a593Smuzhiyun     pPixmap->devPrivate.ptr = ExaGetPixmapAddress(pPixmap);
226*4882a593Smuzhiyun     ret = pExaScr->info->PixmapIsOffscreen(pPixmap);
227*4882a593Smuzhiyun     pPixmap->devPrivate.ptr = saved_ptr;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun     return ret;
230*4882a593Smuzhiyun }
231