i915: Added EGL winsys
[mesa.git] / src / gallium / winsys / egl_drm / intel / intel_swapbuffers.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "intel_screen.h"
29 #include "intel_context.h"
30 #include "intel_swapbuffers.h"
31 #include "intel_batchbuffer.h"
32 #include "intel_reg.h"
33 #include "intel_winsys.h"
34
35 #include "pipe/p_context.h"
36 #include "state_tracker/st_public.h"
37 #include "state_tracker/st_context.h"
38 #include "state_tracker/st_cb_fbo.h"
39 #include "intel_egl.h"
40
41
42 static void
43 intel_display_surface(struct egl_drm_drawable *draw,
44 struct pipe_surface *surf);
45
46 void intel_swap_buffers(struct egl_drm_drawable *draw)
47 {
48 struct intel_framebuffer *intel_fb = (struct intel_framebuffer *)draw->priv;
49 struct pipe_surface *back_surf;
50
51 assert(intel_fb);
52 assert(intel_fb->stfb);
53
54 back_surf = st_get_framebuffer_surface(intel_fb->stfb, ST_SURFACE_BACK_LEFT);
55 if (back_surf) {
56 st_notify_swapbuffers(intel_fb->stfb);
57 intel_display_surface(draw, back_surf);
58 st_notify_swapbuffers_complete(intel_fb->stfb);
59 }
60 }
61
62 static void
63 intel_display_surface(struct egl_drm_drawable *draw,
64 struct pipe_surface *surf)
65 {
66 struct intel_screen *intel = (struct intel_screen *)draw->device->priv;
67 struct intel_framebuffer *intel_fb = (struct intel_framebuffer *)draw->priv;
68 struct _DriFenceObject *fence;
69
70 //const int srcWidth = surf->width;
71 //const int srcHeight = surf->height;
72 const int srcPitch = surf->pitch;
73
74 const int dstWidth = intel_fb->front->width;
75 const int dstHeight = intel_fb->front->height;
76 const int dstPitch = intel_fb->front->pitch / 4;//draw->front.cpp;
77
78 const int cpp = 4;//intel_fb->front->cpp;
79
80 int BR13, CMD;
81 //int i;
82
83
84 BR13 = (dstPitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
85 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
86 XY_SRC_COPY_BLT_WRITE_RGB);
87
88 printf("srcPitch: %u, dstWidth: %u, dstHeight: %u, dstPitch: %u, cpp: %u\n", srcPitch, dstWidth, dstHeight, dstPitch, cpp);
89 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
90 OUT_BATCH(CMD);
91 OUT_BATCH(BR13);
92 OUT_BATCH((0 << 16) | 0);
93 OUT_BATCH((dstHeight << 16) | dstWidth);
94
95 OUT_RELOC(intel_fb->front_buffer,
96 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE,
97 DRM_BO_MASK_MEM | DRM_BO_FLAG_WRITE, 0);
98
99 OUT_BATCH((0 << 16) | 0);
100 OUT_BATCH((srcPitch * cpp) & 0xffff);
101 OUT_RELOC(dri_bo(surf->buffer),
102 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
103 DRM_BO_MASK_MEM | DRM_BO_FLAG_READ, 0);
104
105 ADVANCE_BATCH();
106
107 fence = intel_batchbuffer_flush(intel->batch);
108 driFenceUnReference(&fence);
109 intel_batchbuffer_finish(intel->batch);
110 }
111
112 #if 0
113 /**
114 * Display a colorbuffer surface in an X window.
115 * Used for SwapBuffers and flushing front buffer rendering.
116 *
117 * \param dPriv the window/drawable to display into
118 * \param surf the surface to display
119 * \param rect optional subrect of surface to display (may be NULL).
120 */
121 void
122 intelDisplaySurface(__DRIdrawablePrivate *dPriv,
123 struct pipe_surface *surf,
124 const drm_clip_rect_t *rect)
125 {
126 struct intel_screen *intelScreen = intel_screen(dPriv->driScreenPriv);
127 //struct intel_context *intel = intelScreen->dummyContext;
128
129 DBG(SWAP, "%s\n", __FUNCTION__);
130
131 #if 0
132 if (!intel) {
133 /* XXX this is where some kind of extra/meta context could be useful */
134 return;
135 }
136 #endif
137
138 if (intel->last_swap_fence) {
139 driFenceFinish(intel->last_swap_fence, DRM_FENCE_TYPE_EXE, TRUE);
140 driFenceUnReference(&intel->last_swap_fence);
141 intel->last_swap_fence = NULL;
142 }
143 intel->last_swap_fence = intel->first_swap_fence;
144 intel->first_swap_fence = NULL;
145
146 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets
147 * should work regardless.
148 */
149 LOCK_HARDWARE(intel);
150 /* if this drawable isn't currently bound the LOCK_HARDWARE done on the
151 * current context (which is what intelScreenContext should return) might
152 * not get a contended lock and thus cliprects not updated (tests/manywin)
153 */
154 if (intel_context(dPriv->driContextPriv) != intel)
155 DRI_VALIDATE_DRAWABLE_INFO(intel->driScreen, dPriv);
156
157
158 if (dPriv && dPriv->numClipRects) {
159 const int srcWidth = surf->width;
160 const int srcHeight = surf->height;
161 const int nbox = dPriv->numClipRects;
162 const drm_clip_rect_t *pbox = dPriv->pClipRects;
163 const int pitch = intelScreen->front.pitch / intelScreen->front.cpp;
164 const int cpp = intelScreen->front.cpp;
165 const int srcpitch = surf->pitch;
166 int BR13, CMD;
167 int i;
168
169 ASSERT(surf->buffer);
170 ASSERT(surf->cpp == cpp);
171
172 DBG(SWAP, "screen pitch %d src surface pitch %d\n",
173 pitch, surf->pitch);
174
175 if (cpp == 2) {
176 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24);
177 CMD = XY_SRC_COPY_BLT_CMD;
178 }
179 else {
180 BR13 = (pitch * cpp) | (0xCC << 16) | (1 << 24) | (1 << 25);
181 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
182 XY_SRC_COPY_BLT_WRITE_RGB);
183 }
184
185 for (i = 0; i < nbox; i++, pbox++) {
186 drm_clip_rect_t box;
187 drm_clip_rect_t sbox;
188
189 if (pbox->x1 > pbox->x2 ||
190 pbox->y1 > pbox->y2 ||
191 pbox->x2 > intelScreen->front.width ||
192 pbox->y2 > intelScreen->front.height) {
193 /* invalid cliprect, skip it */
194 continue;
195 }
196
197 box = *pbox;
198
199 if (rect) {
200 /* intersect cliprect with user-provided src rect */
201 drm_clip_rect_t rrect;
202
203 rrect.x1 = dPriv->x + rect->x1;
204 rrect.y1 = (dPriv->h - rect->y1 - rect->y2) + dPriv->y;
205 rrect.x2 = rect->x2 + rrect.x1;
206 rrect.y2 = rect->y2 + rrect.y1;
207 if (rrect.x1 > box.x1)
208 box.x1 = rrect.x1;
209 if (rrect.y1 > box.y1)
210 box.y1 = rrect.y1;
211 if (rrect.x2 < box.x2)
212 box.x2 = rrect.x2;
213 if (rrect.y2 < box.y2)
214 box.y2 = rrect.y2;
215
216 if (box.x1 > box.x2 || box.y1 > box.y2)
217 continue;
218 }
219
220 /* restrict blit to size of actually rendered area */
221 if (box.x2 - box.x1 > srcWidth)
222 box.x2 = srcWidth + box.x1;
223 if (box.y2 - box.y1 > srcHeight)
224 box.y2 = srcHeight + box.y1;
225
226 DBG(SWAP, "box x1 x2 y1 y2 %d %d %d %d\n",
227 box.x1, box.x2, box.y1, box.y2);
228
229 sbox.x1 = box.x1 - dPriv->x;
230 sbox.y1 = box.y1 - dPriv->y;
231
232 assert(box.x1 < box.x2);
233 assert(box.y1 < box.y2);
234
235 /* XXX this could be done with pipe->surface_copy() */
236 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
237 OUT_BATCH(CMD);
238 OUT_BATCH(BR13);
239 OUT_BATCH((box.y1 << 16) | box.x1);
240 OUT_BATCH((box.y2 << 16) | box.x2);
241
242 OUT_RELOC(intelScreen->front.buffer,
243 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE,
244 DRM_BO_MASK_MEM | DRM_BO_FLAG_WRITE, 0);
245 OUT_BATCH((sbox.y1 << 16) | sbox.x1);
246 OUT_BATCH((srcpitch * cpp) & 0xffff);
247 OUT_RELOC(dri_bo(surf->buffer),
248 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
249 DRM_BO_MASK_MEM | DRM_BO_FLAG_READ, 0);
250
251 ADVANCE_BATCH();
252 }
253
254 if (intel->first_swap_fence)
255 driFenceUnReference(&intel->first_swap_fence);
256 intel->first_swap_fence = intel_batchbuffer_flush(intel->batch);
257 }
258
259 UNLOCK_HARDWARE(intel);
260
261 if (intel->lastStamp != dPriv->lastStamp) {
262 intelUpdateWindowSize(dPriv);
263 intel->lastStamp = dPriv->lastStamp;
264 }
265 }
266
267
268
269 /**
270 * This will be called whenever the currently bound window is moved/resized.
271 */
272 void
273 intelUpdateWindowSize(__DRIdrawablePrivate *dPriv)
274 {
275 struct intel_framebuffer *intelfb = intel_framebuffer(dPriv);
276 assert(intelfb->stfb);
277 st_resize_framebuffer(intelfb->stfb, dPriv->w, dPriv->h);
278 }
279
280
281
282 void
283 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
284 {
285 struct intel_framebuffer *intel_fb = intel_framebuffer(dPriv);
286 struct pipe_surface *back_surf;
287
288 assert(intel_fb);
289 assert(intel_fb->stfb);
290
291 back_surf = st_get_framebuffer_surface(intel_fb->stfb,
292 ST_SURFACE_BACK_LEFT);
293 if (back_surf) {
294 st_notify_swapbuffers(intel_fb->stfb);
295 intelDisplaySurface(dPriv, back_surf, NULL);
296 st_notify_swapbuffers_complete(intel_fb->stfb);
297 }
298 }
299
300
301 /**
302 * Called via glXCopySubBufferMESA() to copy a subrect of the back
303 * buffer to the front buffer/screen.
304 */
305 void
306 intelCopySubBuffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
307 {
308 struct intel_framebuffer *intel_fb = intel_framebuffer(dPriv);
309 struct pipe_surface *back_surf;
310
311 assert(intel_fb);
312 assert(intel_fb->stfb);
313
314 back_surf = st_get_framebuffer_surface(intel_fb->stfb,
315 ST_SURFACE_BACK_LEFT);
316 if (back_surf) {
317 drm_clip_rect_t rect;
318 rect.x1 = x;
319 rect.y1 = y;
320 rect.x2 = w;
321 rect.y2 = h;
322
323 st_notify_swapbuffers(intel_fb->stfb);
324 intelDisplaySurface(dPriv, back_surf, &rect);
325 }
326 }
327 #endif