summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/glx/dri/dri_drawable.c
blob: b712acda88eadda5780e2e85e1521019e67f302c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**************************************************************************
 *
 * Copyright 2009, VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "dri_screen.h"
#include "dri_context.h"
#include "dri_swapbuffers.h"

#include "pipe/p_context.h"
#include "state_tracker/st_public.h"
#include "state_tracker/st_context.h"
#include "state_tracker/st_cb_fbo.h"


static void
blit_swapbuffers(__DRIdrawablePrivate *dPriv,
                 __DRIcontextPrivate *cPriv,
		 struct pipe_surface *src,
		 const drm_clip_rect_t *rect)
{
   struct dri_screen *screen = dri_screen(dPriv->driScreenPriv);
   struct dri_drawable *fb = dri_drawable(dPriv);
   struct dri_context *context = dri_context(cPriv);

   const int nbox = dPriv->numClipRects;
   const drm_clip_rect_t *pbox = dPriv->pClipRects;

   struct pipe_surface *dest = fb->front_surface;
   const int backWidth = fb->stfb->Base.Width;
   const int backHeight = fb->stfb->Base.Height;
   int i;

   for (i = 0; i < nbox; i++, pbox++) {
      drm_clip_rect_t box;
      drm_clip_rect_t sbox;
	 
      if (pbox->x1 > pbox->x2 ||
	  pbox->y1 > pbox->y2 ||
	  (pbox->x2 - pbox->x1) > dest->width || 
	  (pbox->y2 - pbox->y1) > dest->height) 
	 continue;

      box = *pbox;

      if (rect) {
	 drm_clip_rect_t rrect;

	 rrect.x1 = dPriv->x + rect->x1;
	 rrect.y1 = (dPriv->h - rect->y1 - rect->y2) + dPriv->y;
	 rrect.x2 = rect->x2 + rrect.x1;
	 rrect.y2 = rect->y2 + rrect.y1;
	 if (rrect.x1 > box.x1)
	    box.x1 = rrect.x1;
	 if (rrect.y1 > box.y1)
	    box.y1 = rrect.y1;
	 if (rrect.x2 < box.x2)
	    box.x2 = rrect.x2;
	 if (rrect.y2 < box.y2)
	    box.y2 = rrect.y2;

	 if (box.x1 > box.x2 || box.y1 > box.y2)
	    continue;
      }

      /* restrict blit to size of actually rendered area */
      if (box.x2 - box.x1 > backWidth)
	 box.x2 = backWidth + box.x1;
      if (box.y2 - box.y1 > backHeight)
	 box.y2 = backHeight + box.y1;

      debug_printf("%s: box %d,%d-%d,%d\n", __FUNCTION__,
                   box.x1, box.y1, box.x2, box.y2);

      sbox.x1 = box.x1 - dPriv->x;
      sbox.y1 = box.y1 - dPriv->y;

      ctx->st->pipe->surface_copy( ctx->st->pipe,
                                   FALSE,
                                   dest,
                                   box.x1, box.y1,
                                   src,
                                   sbox.x1, sbox.y1,
                                   box.x2 - box.x1, 
                                   box.y2 - box.y1 );
   }
}

/**
 * Display a colorbuffer surface in an X window.
 * Used for SwapBuffers and flushing front buffer rendering.
 *
 * \param dPriv  the window/drawable to display into
 * \param surf  the surface to display
 * \param rect  optional subrect of surface to display (may be NULL).
 */
static void
dri_display_surface(__DRIdrawablePrivate *dPriv,
                    struct pipe_surface *source,
                    const drm_clip_rect_t *rect)
{
   struct dri_drawable *drawable = dri_drawable(dPriv);
   struct dri_screen *screen = dri_screen(dPriv->driScreenPriv);
   struct dri_context *context = screen->dummy_context;
   struct pipe_winsys *winsys = screen->winsys;
      
   if (!context) 
      return;

   if (drawable->last_swap_fence) {
      winsys->fence_finish( winsys,
                            drawable->last_swap_fence,
                            0 );

      winsys->fence_reference( winsys,
                               &drawable->last_swap_fence,
                               NULL );
   }

   drawable->last_swap_fence = drawable->first_swap_fence;
   drawable->first_swap_fence = NULL;

   /* Call lock_hardware to update dPriv cliprects. 
    */
   dri_lock_hardware(context, drawable);
   {
      if (dPriv->numClipRects) {
         blit_swapbuffers( context, dPriv, source, rect );
      }
   }
   dri_unlock_hardware(context);

   if (drawble->stamp != drawable->dPriv->lastStamp) {
      dri_update_window_size( dpriv );
   }
}



/**
 * This will be called a drawable is known to have moved/resized.
 */
void
dri_update_window_size(__DRIdrawablePrivate *dPriv)
{
   struct dri_drawable *drawable = dri_drawable(dPriv);
   st_resize_framebuffer(drawable->stfb, dPriv->w, dPriv->h);
   drawable->stamp = dPriv->lastStamp;
}



void
dri_swap_buffers(__DRIdrawablePrivate * dPriv)
{
   struct dri_drawable *drawable = dri_drawable(dPriv);
   struct pipe_surface *back_surf;

   assert(drawable);
   assert(drawable->stfb);

   back_surf = st_get_framebuffer_surface(drawable->stfb,
                                          ST_SURFACE_BACK_LEFT);
   if (back_surf) {
      st_notify_swapbuffers(drawable->stfb);
      dri_display_surface(dPriv, back_surf, NULL);
      st_notify_swapbuffers_complete(drawable->stfb);
   }
}


/**
 * Called via glXCopySubBufferMESA() to copy a subrect of the back
 * buffer to the front buffer/screen.
 */
void
dri_copy_sub_buffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
{
   struct dri_drawable *drawable = dri_drawable(dPriv);
   struct pipe_surface *back_surf;

   assert(drawable);
   assert(drawable->stfb);

   back_surf = st_get_framebuffer_surface(drawable->stfb,
                                          ST_SURFACE_BACK_LEFT);
   if (back_surf) {
      drm_clip_rect_t rect;
      rect.x1 = x;
      rect.y1 = y;
      rect.x2 = w;
      rect.y2 = h;

      st_notify_swapbuffers(drawable->stfb);
      dri_display_surface(dPriv, back_surf, &rect);
   }
}



/*
 * The state tracker keeps track of whether the fake frontbuffer has
 * been touched by any rendering since the last time we copied its
 * contents to the real frontbuffer.  Our task is easy:
 */
static void
dri_flush_frontbuffer( struct pipe_winsys *winsys,
                       struct pipe_surface *surf,
                       void *context_private)
{
   struct dri_context *dri = (struct dri_context *) context_private;
   __DRIdrawablePrivate *dPriv = dri->driDrawable;

   dri_display_surface(dPriv, surf, NULL);
}



/* Need to create a surface which wraps the front surface to support
 * client-side swapbuffers.
 */
static void
dri_create_front_surface(struct dri_screen *screen, 
                         struct pipe_winsys *winsys, 
                         unsigned handle)
{
   struct pipe_screen *pipe_screen = screen->pipe_screen;
   struct pipe_texture *texture;
   struct pipe_texture templat;
   struct pipe_surface *surface;
   struct pipe_buffer *buffer;
   unsigned pitch;

   assert(screen->front.cpp == 4);

//   buffer = dri_buffer_from_handle(screen->winsys,
//                                        "front", handle);

   if (!buffer)
      return;

   screen->front.buffer = dri_bo(buffer);

   memset(&templat, 0, sizeof(templat));
   templat.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
   templat.target = PIPE_TEXTURE_2D;
   templat.last_level = 0;
   templat.depth[0] = 1;
   templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
   templat.width[0] = screen->front.width;
   templat.height[0] = screen->front.height;
   pf_get_block(templat.format, &templat.block);
   pitch = screen->front.pitch;

   texture = pipe_screen->texture_blanket(pipe_screen,
                                          &templat,
                                          &pitch,
                                          buffer);

   /* Unref the buffer we don't need it anyways */
   pipe_buffer_reference(screen, &buffer, NULL);

   surface = pipe_screen->get_tex_surface(pipe_screen,
                                          texture,
                                          0,
                                          0,
                                          0,
                                          PIPE_BUFFER_USAGE_GPU_WRITE);

   screen->front.texture = texture;
   screen->front.surface = surface;
}

/**
 * This is called when we need to set up GL rendering to a new X window.
 */
static boolean
dri_create_buffer(__DRIscreenPrivate *sPriv,
                  __DRIdrawablePrivate *dPriv,
                  const __GLcontextModes *visual,
                  boolean isPixmap)
{
   enum pipe_format colorFormat, depthFormat, stencilFormat;
   struct dri_drawable *drawable;

   if (isPixmap) 
      goto fail;          /* not implemented */

   drawable = CALLOC_STRUCT(dri_drawable);
   if (drawable == NULL)
      goto fail;

   /* XXX: todo: use the pipe_screen queries to figure out which
    * render targets are supportable.
    */
   if (visual->redBits == 5)
      colorFormat = PIPE_FORMAT_R5G6B5_UNORM;
   else
      colorFormat = PIPE_FORMAT_A8R8G8B8_UNORM;

   if (visual->depthBits == 16)
      depthFormat = PIPE_FORMAT_Z16_UNORM;
   else if (visual->depthBits == 24) {
      if (visual->stencilBits == 8)
         depthFormat = PIPE_FORMAT_S8Z24_UNORM;
      else
         depthFormat = PIPE_FORMAT_X8Z24_UNORM;
   }

   drawable->stfb = st_create_framebuffer(visual,
                                    colorFormat,
                                    depthFormat,
                                    dPriv->w,
                                    dPriv->h,
                                    (void*) drawable);
   if (drawable->stfb == NULL)
      goto fail;

   dPriv->driverPrivate = (void *) drawable;
   return GL_TRUE;

fail:
   FREE(drawable);
   return GL_FALSE;
}

static void
dri_destroy_buffer(__DRIdrawablePrivate *dPriv)
{
   struct dri_drawable *drawable = dri_drawable(dPriv);

   /* No particular need to wait on fences before dereferencing them:
    */
   winsys->fence_reference( winsys, &ctx->last_swap_fence, NULL );
   winsys->fence_reference( winsys, &ctx->first_swap_fence, NULL );

   st_unreference_framebuffer(drawable->stfb);

   FREE(drawable);
}