randr12: remember the edid from detection for getting modes later
[nouveau] / src / nv04_xv_blit.c
1 /*
2  * Copyright 2007 Arthur Huillet
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "xf86xv.h"
28 #include <X11/extensions/Xv.h>
29 #include "exa.h"
30 #include "damage.h"
31 #include "dixstruct.h"
32 #include "fourcc.h"
33
34 #include "nv_include.h"
35 #include "nv_dma.h"
36
37 #define FOURCC_RGB 0x0000003
38
39 extern Atom xvSetDefaults, xvSyncToVBlank;
40
41 /**
42  * NVPutBlitImage
43  * 
44  * @param pScrn screen
45  * @param src_offset offset of image data in VRAM
46  * @param id pixel format
47  * @param src_pitch source pitch
48  * @param dstBox 
49  * @param x1
50  * @param y1
51  * @param x2
52  * @param y2
53  * @param width
54  * @param height
55  * @param src_w
56  * @param src_h
57  * @param drw_w
58  * @param drw_h
59  * @param clipBoxes
60  * @param pDraw
61  */
62 void
63 NVPutBlitImage(ScrnInfoPtr pScrn, struct nouveau_bo *src, int src_offset,
64                int id, int src_pitch, BoxPtr dstBox,
65                int x1, int y1, int x2, int y2,
66                short width, short height,
67                short src_w, short src_h,
68                short drw_w, short drw_h,
69                RegionPtr clipBoxes, PixmapPtr ppix)
70 {
71         NVPtr          pNv   = NVPTR(pScrn);
72         NVPortPrivPtr  pPriv = GET_BLIT_PRIVATE(pNv);
73         BoxPtr         pbox;
74         int            nbox;
75         CARD32         dsdx, dtdy;
76         CARD32         dst_size, dst_point;
77         CARD32         src_point, src_format;
78         struct nouveau_channel *chan = pNv->chan;
79         struct nouveau_grobj *surf2d = pNv->NvContextSurfaces;
80         struct nouveau_grobj *rect = pNv->NvRectangle;
81         struct nouveau_grobj *sifm = pNv->NvScaledImage;
82         unsigned int crtcs;
83         int dst_format;
84
85         NVAccelGetCtxSurf2DFormatFromPixmap(ppix, &dst_format);
86         BEGIN_RING(chan, surf2d, NV04_CONTEXT_SURFACES_2D_FORMAT, 4);
87         OUT_RING  (chan, dst_format);
88         OUT_RING  (chan, (exaGetPixmapPitch(ppix) << 16) | exaGetPixmapPitch(ppix));
89         OUT_PIXMAPl(chan, ppix, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
90         OUT_PIXMAPl(chan, ppix, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
91
92         pbox = REGION_RECTS(clipBoxes);
93         nbox = REGION_NUM_RECTS(clipBoxes);
94
95         dsdx = (src_w << 20) / drw_w;
96         dtdy = (src_h << 20) / drw_h;
97
98         dst_size  = ((dstBox->y2 - dstBox->y1) << 16) |
99                      (dstBox->x2 - dstBox->x1);
100         dst_point = (dstBox->y1 << 16) | dstBox->x1;
101
102         src_pitch |= (NV04_SCALED_IMAGE_FROM_MEMORY_FORMAT_ORIGIN_CENTER |
103                       NV04_SCALED_IMAGE_FROM_MEMORY_FORMAT_FILTER_BILINEAR);
104         src_point = ((y1 << 4) & 0xffff0000) | (x1 >> 12);
105
106         switch(id) {
107         case FOURCC_RGB:
108                 src_format =
109                         NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_X8R8G8B8;
110                 break;
111         case FOURCC_UYVY:
112                 src_format =
113                         NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_YB8V8YA8U8;
114                 break;
115         default:
116                 src_format =
117                         NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT_V8YB8U8YA8;
118                 break;
119         }
120
121         if(pPriv->SyncToVBlank) {
122                 crtcs = nv_window_belongs_to_crtc(pScrn, dstBox->x1, dstBox->y1,
123                         dstBox->x2, dstBox->y2);
124
125                 FIRE_RING (chan);
126                 if (crtcs & 0x1)
127                         NVWaitVSync(pScrn, 0);
128                 else if (crtcs & 0x2)
129                         NVWaitVSync(pScrn, 1);
130         }
131
132         if(pNv->BlendingPossible) {
133                 BEGIN_RING(chan, sifm,
134                                  NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT, 2);
135                 OUT_RING  (chan, src_format);
136                 OUT_RING  (chan, NV04_SCALED_IMAGE_FROM_MEMORY_OPERATION_SRCCOPY);
137         } else {
138                 BEGIN_RING(chan, sifm,
139                                  NV04_SCALED_IMAGE_FROM_MEMORY_COLOR_FORMAT, 2);
140                 OUT_RING  (chan, src_format);
141         }
142
143         while(nbox--) {
144                 BEGIN_RING(chan, rect, NV04_GDI_RECTANGLE_TEXT_COLOR1_A, 1);
145                 OUT_RING  (chan, 0);
146
147                 BEGIN_RING(chan, sifm,
148                                  NV04_SCALED_IMAGE_FROM_MEMORY_CLIP_POINT, 6);
149                 OUT_RING  (chan, (pbox->y1 << 16) | pbox->x1);
150                 OUT_RING  (chan, ((pbox->y2 - pbox->y1) << 16) |
151                                  (pbox->x2 - pbox->x1));
152                 OUT_RING  (chan, dst_point);
153                 OUT_RING  (chan, dst_size);
154                 OUT_RING  (chan, dsdx);
155                 OUT_RING  (chan, dtdy);
156
157                 BEGIN_RING(chan, sifm, NV04_SCALED_IMAGE_FROM_MEMORY_SIZE, 4);
158                 OUT_RING  (chan, (height << 16) | width);
159                 OUT_RING  (chan, src_pitch);
160                 OUT_RELOCl(chan, src, src_offset,
161                                  NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
162                 OUT_RING  (chan, src_point);
163                 pbox++;
164         }
165
166         FIRE_RING (chan);
167
168         exaMarkSync(pScrn->pScreen);
169
170         pPriv->videoStatus = FREE_TIMER;
171         pPriv->videoTime = currentTime.milliseconds + FREE_DELAY;
172         extern void NVVideoTimerCallback(ScrnInfoPtr, Time);
173         pNv->VideoTimerCallback = NVVideoTimerCallback;
174 }
175
176
177 /**
178  * NVSetBlitPortAttribute
179  * sets the attribute "attribute" of port "data" to value "value"
180  * supported attributes:
181  * - xvSyncToVBlank (values: 0,1)
182  * - xvSetDefaults (values: NA; SyncToVBlank will be set, if hardware supports it)
183  * 
184  * @param pScrenInfo
185  * @param attribute attribute to set
186  * @param value value to which attribute is to be set
187  * @param data port from which the attribute is to be set
188  * 
189  * @return Success, if setting is successful
190  * BadValue/BadMatch, if value/attribute are invalid
191  */
192 int
193 NVSetBlitPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
194                        INT32 value, pointer data)
195 {
196         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
197         NVPtr           pNv = NVPTR(pScrn);
198
199         if ((attribute == xvSyncToVBlank) && pNv->WaitVSyncPossible) {
200                 if ((value < 0) || (value > 1))
201                         return BadValue;
202                 pPriv->SyncToVBlank = value;
203         } else
204         if (attribute == xvSetDefaults) {
205                 pPriv->SyncToVBlank = pNv->WaitVSyncPossible;
206         } else
207                 return BadMatch;
208
209         return Success;
210 }
211
212 /**
213  * NVGetBlitPortAttribute
214  * reads the value of attribute "attribute" from port "data" into INT32 "*value"
215  * currently only one attribute supported: xvSyncToVBlank
216  * 
217  * @param pScrn unused
218  * @param attribute attribute to be read
219  * @param value value of attribute will be stored here
220  * @param data port from which attribute will be read
221  * @return Success, if queried attribute exists
222  */
223 int
224 NVGetBlitPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
225                        INT32 *value, pointer data)
226 {
227         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
228
229         if(attribute == xvSyncToVBlank)
230                 *value = (pPriv->SyncToVBlank) ? 1 : 0;
231         else
232                 return BadMatch;
233
234         return Success;
235 }
236
237 /**
238  * NVStopBlitVideo
239  */
240 void
241 NVStopBlitVideo(ScrnInfoPtr pScrn, pointer data, Bool Exit)
242 {
243 }
244