summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/dri2/dri_drawable.c
blob: 120d45bc036140fcbcc9485f2824de768e4ee97f (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
/**************************************************************************
 *
 * 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 VMWARE 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.
 *
 **************************************************************************/
/*
 * Author: Keith Whitwell <keithw@vmware.com>
 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
 */

#include "dri_screen.h"
#include "dri_context.h"
#include "dri_drawable.h"

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

#include "util/u_memory.h"


static struct pipe_surface *
dri_surface_from_handle(struct pipe_screen *screen,
                        unsigned handle,
                        enum pipe_format format,
                        unsigned width,
                        unsigned height,
                        unsigned pitch)
{
   struct pipe_surface *surface = NULL;
   struct pipe_texture *texture = NULL;
   struct pipe_texture templat;
   struct pipe_buffer *buf = NULL;

   buf = drm_api_hocks.buffer_from_handle(screen, "dri2 buffer", handle);
   if (!buf)
      return NULL;

   memset(&templat, 0, sizeof(templat));
   templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
   templat.target = PIPE_TEXTURE_2D;
   templat.last_level = 0;
   templat.depth[0] = 1;
   templat.format = format;
   templat.width[0] = width;
   templat.height[0] = height;
   pf_get_block(templat.format, &templat.block);

   texture = screen->texture_blanket(screen,
                                     &templat,
                                     &pitch,
                                     buf);

   /* we don't need the buffer from this point on */
   pipe_buffer_reference(screen, &buf, NULL);

   if (!texture)
      return NULL;

   surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
                                     PIPE_BUFFER_USAGE_GPU_READ |
                                     PIPE_BUFFER_USAGE_GPU_WRITE);

   /* we don't need the texture from this point on */
   pipe_texture_reference(&texture, NULL);
   return surface;
}


/**
 * This will be called a drawable is known to have been resized.
 */
void
dri_get_buffers(__DRIdrawablePrivate *dPriv)
{
   struct dri_drawable *drawable = dri_drawable(dPriv);
   struct pipe_surface *surface = NULL;
   struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
   __DRIbuffer *buffers = NULL;
   __DRIscreen *dri_screen = drawable->sPriv;
   __DRIdrawable *dri_drawable = drawable->dPriv;
   boolean have_depth = FALSE;
   int i, count;

   buffers = (*dri_screen->dri2.loader->getBuffers)(dri_drawable,
                                                    &dri_drawable->w,
                                                    &dri_drawable->h,
                                                    drawable->attachments,
                                                    drawable->num_attachments,
                                                    &count,
                                                    dri_drawable->loaderPrivate);

   if (buffers == NULL) {
      return;
   }

   /* set one cliprect to cover the whole dri_drawable */
   dri_drawable->x = 0;
   dri_drawable->y = 0;
   dri_drawable->backX = 0;
   dri_drawable->backY = 0;
   dri_drawable->numClipRects = 1;
   dri_drawable->pClipRects[0].x1 = 0;
   dri_drawable->pClipRects[0].y1 = 0;
   dri_drawable->pClipRects[0].x2 = dri_drawable->w;
   dri_drawable->pClipRects[0].y2 = dri_drawable->h;
   dri_drawable->numBackClipRects = 1;
   dri_drawable->pBackClipRects[0].x1 = 0;
   dri_drawable->pBackClipRects[0].y1 = 0;
   dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
   dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;

   for (i = 0; i < count; i++) {
      enum pipe_format format = 0;
      int index = 0;

      switch (buffers[i].attachment) {
         case __DRI_BUFFER_FRONT_LEFT:
            index = ST_SURFACE_FRONT_LEFT;
            format = PIPE_FORMAT_A8R8G8B8_UNORM;
            break;
         case __DRI_BUFFER_BACK_LEFT:
            index = ST_SURFACE_BACK_LEFT;
            format = PIPE_FORMAT_A8R8G8B8_UNORM;
            break;
         case __DRI_BUFFER_DEPTH:
            index = ST_SURFACE_DEPTH;
            format = PIPE_FORMAT_Z24S8_UNORM;
            break;
         case __DRI_BUFFER_STENCIL:
            index = ST_SURFACE_DEPTH;
            format = PIPE_FORMAT_Z24S8_UNORM;
            break;
         case __DRI_BUFFER_ACCUM:
         default:
            assert(0);
      }
      assert(buffers[i].cpp == 4);

      if (index == ST_SURFACE_DEPTH) {
         if (have_depth)
            continue;
         else
            have_depth = TRUE;
      }

      surface = dri_surface_from_handle(screen,
                                        buffers[i].name,
                                        format,
                                        dri_drawable->w,
                                        dri_drawable->h,
                                        buffers[i].pitch);

      st_set_framebuffer_surface(drawable->stfb, index, surface);
      pipe_surface_reference(&surface, NULL);
   }
   /* this needed, or else the state tracker fails to pick the new buffers */
   st_resize_framebuffer(drawable->stfb, dri_drawable->w, dri_drawable->h);
}


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

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

   st_get_framebuffer_surface(drawable->stfb,
                              ST_SURFACE_BACK_LEFT,
                              &back_surf);
   if (back_surf) {
      st_notify_swapbuffers(drawable->stfb);
      /* TODO do stuff here */
      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);

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

      /* do stuff here */
   }
}


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

   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.
    */
   assert(visual->redBits == 8);
   assert(visual->depthBits == 24 || visual->depthBits == 0);
   assert(visual->stencilBits == 8 || visual->stencilBits == 0);

   colorFormat = PIPE_FORMAT_A8R8G8B8_UNORM;

   if (visual->depthBits)
      depthFormat = PIPE_FORMAT_S8Z24_UNORM;
   else
      depthFormat = PIPE_FORMAT_NONE;

   if (visual->stencilBits)
      stencilFormat = PIPE_FORMAT_S8Z24_UNORM;
   else
      stencilFormat = PIPE_FORMAT_NONE;

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

   drawable->sPriv = sPriv;
   drawable->dPriv = dPriv;
   dPriv->driverPrivate = (void *) drawable;

   /* setup dri2 buffers information */
   i = 0;
   drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
   drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
   if (visual->depthBits)
      drawable->attachments[i++] = __DRI_BUFFER_DEPTH;
   if (visual->stencilBits)
      drawable->attachments[i++] = __DRI_BUFFER_STENCIL;
   drawable->num_attachments = i;

   return GL_TRUE;
fail:
   FREE(drawable);
   return GL_FALSE;
}


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

   st_unreference_framebuffer(drawable->stfb);

   FREE(drawable);
}

/* vim: set sw=3 ts=8 sts=3 expandtab: */