Subversion Repositories Kolibri OS

Rev

Rev 4358 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4358 Rev 5063
1
/*
1
/*
2
 * Mesa 3-D graphics library
2
 * Mesa 3-D graphics library
3
 *
3
 *
4
 * Copyright (C) 2010 LunarG Inc.
4
 * Copyright (C) 2010 LunarG Inc.
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included
13
 * The above copyright notice and this permission notice shall be included
14
 * in all copies or substantial portions of the Software.
14
 * in all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 *
23
 *
24
 * Authors:
24
 * Authors:
25
 *    Chia-I Wu 
25
 *    Chia-I Wu 
26
 */
26
 */
27
 
27
 
28
#include "main/mtypes.h"
28
#include "main/mtypes.h"
29
#include "main/context.h"
29
#include "main/context.h"
30
#include "main/texobj.h"
30
#include "main/texobj.h"
31
#include "main/teximage.h"
31
#include "main/teximage.h"
32
#include "main/texstate.h"
32
#include "main/texstate.h"
33
#include "main/framebuffer.h"
33
#include "main/framebuffer.h"
34
#include "main/fbobject.h"
34
#include "main/fbobject.h"
35
#include "main/renderbuffer.h"
35
#include "main/renderbuffer.h"
36
#include "main/version.h"
36
#include "main/version.h"
37
#include "st_texture.h"
37
#include "st_texture.h"
38
 
38
 
39
#include "st_context.h"
39
#include "st_context.h"
40
#include "st_format.h"
40
#include "st_format.h"
41
#include "st_cb_fbo.h"
41
#include "st_cb_fbo.h"
42
#include "st_cb_flush.h"
42
#include "st_cb_flush.h"
43
#include "st_manager.h"
43
#include "st_manager.h"
44
 
44
 
45
#include "state_tracker/st_gl_api.h"
45
#include "state_tracker/st_gl_api.h"
46
 
46
 
47
#include "pipe/p_context.h"
47
#include "pipe/p_context.h"
48
#include "pipe/p_screen.h"
48
#include "pipe/p_screen.h"
49
#include "util/u_format.h"
49
#include "util/u_format.h"
50
#include "util/u_pointer.h"
50
#include "util/u_pointer.h"
51
#include "util/u_inlines.h"
51
#include "util/u_inlines.h"
52
#include "util/u_atomic.h"
52
#include "util/u_atomic.h"
53
#include "util/u_surface.h"
53
#include "util/u_surface.h"
54
 
54
 
55
/**
55
/**
56
 * Cast wrapper to convert a struct gl_framebuffer to an st_framebuffer.
56
 * Cast wrapper to convert a struct gl_framebuffer to an st_framebuffer.
57
 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
57
 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
58
 * We'll only return non-null for window system framebuffers.
58
 * We'll only return non-null for window system framebuffers.
59
 * Note that this function may fail.
59
 * Note that this function may fail.
60
 */
60
 */
61
static INLINE struct st_framebuffer *
61
static INLINE struct st_framebuffer *
62
st_ws_framebuffer(struct gl_framebuffer *fb)
62
st_ws_framebuffer(struct gl_framebuffer *fb)
63
{
63
{
64
   /* FBO cannot be casted.  See st_new_framebuffer */
64
   /* FBO cannot be casted.  See st_new_framebuffer */
65
   if (fb && _mesa_is_winsys_fbo(fb))
65
   if (fb && _mesa_is_winsys_fbo(fb))
66
      return (struct st_framebuffer *) fb;
66
      return (struct st_framebuffer *) fb;
67
   return NULL;
67
   return NULL;
68
}
68
}
69
 
69
 
70
/**
70
/**
71
 * Map an attachment to a buffer index.
71
 * Map an attachment to a buffer index.
72
 */
72
 */
73
static INLINE gl_buffer_index
73
static INLINE gl_buffer_index
74
attachment_to_buffer_index(enum st_attachment_type statt)
74
attachment_to_buffer_index(enum st_attachment_type statt)
75
{
75
{
76
   gl_buffer_index index;
76
   gl_buffer_index index;
77
 
77
 
78
   switch (statt) {
78
   switch (statt) {
79
   case ST_ATTACHMENT_FRONT_LEFT:
79
   case ST_ATTACHMENT_FRONT_LEFT:
80
      index = BUFFER_FRONT_LEFT;
80
      index = BUFFER_FRONT_LEFT;
81
      break;
81
      break;
82
   case ST_ATTACHMENT_BACK_LEFT:
82
   case ST_ATTACHMENT_BACK_LEFT:
83
      index = BUFFER_BACK_LEFT;
83
      index = BUFFER_BACK_LEFT;
84
      break;
84
      break;
85
   case ST_ATTACHMENT_FRONT_RIGHT:
85
   case ST_ATTACHMENT_FRONT_RIGHT:
86
      index = BUFFER_FRONT_RIGHT;
86
      index = BUFFER_FRONT_RIGHT;
87
      break;
87
      break;
88
   case ST_ATTACHMENT_BACK_RIGHT:
88
   case ST_ATTACHMENT_BACK_RIGHT:
89
      index = BUFFER_BACK_RIGHT;
89
      index = BUFFER_BACK_RIGHT;
90
      break;
90
      break;
91
   case ST_ATTACHMENT_DEPTH_STENCIL:
91
   case ST_ATTACHMENT_DEPTH_STENCIL:
92
      index = BUFFER_DEPTH;
92
      index = BUFFER_DEPTH;
93
      break;
93
      break;
94
   case ST_ATTACHMENT_ACCUM:
94
   case ST_ATTACHMENT_ACCUM:
95
      index = BUFFER_ACCUM;
95
      index = BUFFER_ACCUM;
96
      break;
96
      break;
97
   case ST_ATTACHMENT_SAMPLE:
97
   case ST_ATTACHMENT_SAMPLE:
98
   default:
98
   default:
99
      index = BUFFER_COUNT;
99
      index = BUFFER_COUNT;
100
      break;
100
      break;
101
   }
101
   }
102
 
102
 
103
   return index;
103
   return index;
104
}
104
}
105
 
105
 
106
/**
106
/**
107
 * Map a buffer index to an attachment.
107
 * Map a buffer index to an attachment.
108
 */
108
 */
109
static INLINE enum st_attachment_type
109
static INLINE enum st_attachment_type
110
buffer_index_to_attachment(gl_buffer_index index)
110
buffer_index_to_attachment(gl_buffer_index index)
111
{
111
{
112
   enum st_attachment_type statt;
112
   enum st_attachment_type statt;
113
 
113
 
114
   switch (index) {
114
   switch (index) {
115
   case BUFFER_FRONT_LEFT:
115
   case BUFFER_FRONT_LEFT:
116
      statt = ST_ATTACHMENT_FRONT_LEFT;
116
      statt = ST_ATTACHMENT_FRONT_LEFT;
117
      break;
117
      break;
118
   case BUFFER_BACK_LEFT:
118
   case BUFFER_BACK_LEFT:
119
      statt = ST_ATTACHMENT_BACK_LEFT;
119
      statt = ST_ATTACHMENT_BACK_LEFT;
120
      break;
120
      break;
121
   case BUFFER_FRONT_RIGHT:
121
   case BUFFER_FRONT_RIGHT:
122
      statt = ST_ATTACHMENT_FRONT_RIGHT;
122
      statt = ST_ATTACHMENT_FRONT_RIGHT;
123
      break;
123
      break;
124
   case BUFFER_BACK_RIGHT:
124
   case BUFFER_BACK_RIGHT:
125
      statt = ST_ATTACHMENT_BACK_RIGHT;
125
      statt = ST_ATTACHMENT_BACK_RIGHT;
126
      break;
126
      break;
127
   case BUFFER_DEPTH:
127
   case BUFFER_DEPTH:
128
      statt = ST_ATTACHMENT_DEPTH_STENCIL;
128
      statt = ST_ATTACHMENT_DEPTH_STENCIL;
129
      break;
129
      break;
130
   case BUFFER_ACCUM:
130
   case BUFFER_ACCUM:
131
      statt = ST_ATTACHMENT_ACCUM;
131
      statt = ST_ATTACHMENT_ACCUM;
132
      break;
132
      break;
133
   default:
133
   default:
134
      statt = ST_ATTACHMENT_INVALID;
134
      statt = ST_ATTACHMENT_INVALID;
135
      break;
135
      break;
136
   }
136
   }
137
 
137
 
138
   return statt;
138
   return statt;
139
}
139
}
140
 
140
 
141
/**
141
/**
142
 * Make sure a context picks up the latest cached state of the
142
 * Make sure a context picks up the latest cached state of the
143
 * drawables it binds to.
143
 * drawables it binds to.
144
 */
144
 */
145
static void
145
static void
146
st_context_validate(struct st_context *st,
146
st_context_validate(struct st_context *st,
147
                    struct st_framebuffer *stdraw,
147
                    struct st_framebuffer *stdraw,
148
                    struct st_framebuffer *stread)
148
                    struct st_framebuffer *stread)
149
{
149
{
150
    if (stdraw && stdraw->stamp != st->draw_stamp) {
150
    if (stdraw && stdraw->stamp != st->draw_stamp) {
151
       st->dirty.st |= ST_NEW_FRAMEBUFFER;
151
       st->dirty.st |= ST_NEW_FRAMEBUFFER;
152
       _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
152
       _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
153
                                stdraw->Base.Width,
153
                                stdraw->Base.Width,
154
                                stdraw->Base.Height);
154
                                stdraw->Base.Height);
155
       st->draw_stamp = stdraw->stamp;
155
       st->draw_stamp = stdraw->stamp;
156
    }
156
    }
157
 
157
 
158
    if (stread && stread->stamp != st->read_stamp) {
158
    if (stread && stread->stamp != st->read_stamp) {
159
       if (stread != stdraw) {
159
       if (stread != stdraw) {
160
          st->dirty.st |= ST_NEW_FRAMEBUFFER;
160
          st->dirty.st |= ST_NEW_FRAMEBUFFER;
161
          _mesa_resize_framebuffer(st->ctx, &stread->Base,
161
          _mesa_resize_framebuffer(st->ctx, &stread->Base,
162
                                   stread->Base.Width,
162
                                   stread->Base.Width,
163
                                   stread->Base.Height);
163
                                   stread->Base.Height);
164
       }
164
       }
165
       st->read_stamp = stread->stamp;
165
       st->read_stamp = stread->stamp;
166
    }
166
    }
167
}
167
}
168
 
168
 
169
/**
169
/**
170
 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
170
 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
171
 * The context is only used for creating pipe surfaces and for calling
171
 * The context is only used for creating pipe surfaces and for calling
172
 * _mesa_resize_framebuffer().
172
 * _mesa_resize_framebuffer().
173
 * (That should probably be rethought, since those surfaces become
173
 * (That should probably be rethought, since those surfaces become
174
 * drawable state, not context state, and can be freed by another pipe
174
 * drawable state, not context state, and can be freed by another pipe
175
 * context).
175
 * context).
176
 */
176
 */
177
static void
177
static void
178
st_framebuffer_validate(struct st_framebuffer *stfb,
178
st_framebuffer_validate(struct st_framebuffer *stfb,
179
                        struct st_context *st)
179
                        struct st_context *st)
180
{
180
{
181
   struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
181
   struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
182
   uint width, height;
182
   uint width, height;
183
   unsigned i;
183
   unsigned i;
184
   boolean changed = FALSE;
184
   boolean changed = FALSE;
185
   int32_t new_stamp = p_atomic_read(&stfb->iface->stamp);
185
   int32_t new_stamp = p_atomic_read(&stfb->iface->stamp);
186
 
186
 
187
   if (stfb->iface_stamp == new_stamp)
187
   if (stfb->iface_stamp == new_stamp)
188
      return;
188
      return;
189
 
189
 
190
   /* validate the fb */
190
   /* validate the fb */
191
   do {
191
   do {
192
      if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
192
      if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
193
				 stfb->num_statts, textures))
193
				 stfb->num_statts, textures))
194
	 return;
194
	 return;
195
 
195
 
196
      stfb->iface_stamp = new_stamp;
196
      stfb->iface_stamp = new_stamp;
197
      new_stamp = p_atomic_read(&stfb->iface->stamp);
197
      new_stamp = p_atomic_read(&stfb->iface->stamp);
198
   } while(stfb->iface_stamp != new_stamp);
198
   } while(stfb->iface_stamp != new_stamp);
199
 
199
 
200
   width = stfb->Base.Width;
200
   width = stfb->Base.Width;
201
   height = stfb->Base.Height;
201
   height = stfb->Base.Height;
202
 
202
 
203
   for (i = 0; i < stfb->num_statts; i++) {
203
   for (i = 0; i < stfb->num_statts; i++) {
204
      struct st_renderbuffer *strb;
204
      struct st_renderbuffer *strb;
205
      struct pipe_surface *ps, surf_tmpl;
205
      struct pipe_surface *ps, surf_tmpl;
206
      gl_buffer_index idx;
206
      gl_buffer_index idx;
207
 
207
 
208
      if (!textures[i])
208
      if (!textures[i])
209
         continue;
209
         continue;
210
 
210
 
211
      idx = attachment_to_buffer_index(stfb->statts[i]);
211
      idx = attachment_to_buffer_index(stfb->statts[i]);
212
      if (idx >= BUFFER_COUNT) {
212
      if (idx >= BUFFER_COUNT) {
213
         pipe_resource_reference(&textures[i], NULL);
213
         pipe_resource_reference(&textures[i], NULL);
214
         continue;
214
         continue;
215
      }
215
      }
216
 
216
 
217
      strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
217
      strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
218
      assert(strb);
218
      assert(strb);
219
      if (strb->texture == textures[i]) {
219
      if (strb->texture == textures[i]) {
220
         pipe_resource_reference(&textures[i], NULL);
220
         pipe_resource_reference(&textures[i], NULL);
221
         continue;
221
         continue;
222
      }
222
      }
223
 
223
 
224
      u_surface_default_template(&surf_tmpl, textures[i]);
224
      u_surface_default_template(&surf_tmpl, textures[i]);
225
      ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
225
      ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
226
      if (ps) {
226
      if (ps) {
227
         pipe_surface_reference(&strb->surface, ps);
227
         pipe_surface_reference(&strb->surface, ps);
228
         pipe_resource_reference(&strb->texture, ps->texture);
228
         pipe_resource_reference(&strb->texture, ps->texture);
229
         /* ownership transfered */
229
         /* ownership transfered */
230
         pipe_surface_reference(&ps, NULL);
230
         pipe_surface_reference(&ps, NULL);
231
 
231
 
232
         changed = TRUE;
232
         changed = TRUE;
233
 
233
 
234
         strb->Base.Width = strb->surface->width;
234
         strb->Base.Width = strb->surface->width;
235
         strb->Base.Height = strb->surface->height;
235
         strb->Base.Height = strb->surface->height;
236
 
236
 
237
         width = strb->Base.Width;
237
         width = strb->Base.Width;
238
         height = strb->Base.Height;
238
         height = strb->Base.Height;
239
      }
239
      }
240
 
240
 
241
      pipe_resource_reference(&textures[i], NULL);
241
      pipe_resource_reference(&textures[i], NULL);
242
   }
242
   }
243
 
243
 
244
   if (changed) {
244
   if (changed) {
245
      ++stfb->stamp;
245
      ++stfb->stamp;
246
      _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
246
      _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
247
   }
247
   }
248
}
248
}
249
 
249
 
250
/**
250
/**
251
 * Update the attachments to validate by looping the existing renderbuffers.
251
 * Update the attachments to validate by looping the existing renderbuffers.
252
 */
252
 */
253
static void
253
static void
254
st_framebuffer_update_attachments(struct st_framebuffer *stfb)
254
st_framebuffer_update_attachments(struct st_framebuffer *stfb)
255
{
255
{
256
   gl_buffer_index idx;
256
   gl_buffer_index idx;
257
 
257
 
258
   stfb->num_statts = 0;
258
   stfb->num_statts = 0;
259
   for (idx = 0; idx < BUFFER_COUNT; idx++) {
259
   for (idx = 0; idx < BUFFER_COUNT; idx++) {
260
      struct st_renderbuffer *strb;
260
      struct st_renderbuffer *strb;
261
      enum st_attachment_type statt;
261
      enum st_attachment_type statt;
262
 
262
 
263
      strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
263
      strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
264
      if (!strb || strb->software)
264
      if (!strb || strb->software)
265
         continue;
265
         continue;
266
 
266
 
267
      statt = buffer_index_to_attachment(idx);
267
      statt = buffer_index_to_attachment(idx);
268
      if (statt != ST_ATTACHMENT_INVALID &&
268
      if (statt != ST_ATTACHMENT_INVALID &&
269
          st_visual_have_buffers(stfb->iface->visual, 1 << statt))
269
          st_visual_have_buffers(stfb->iface->visual, 1 << statt))
270
         stfb->statts[stfb->num_statts++] = statt;
270
         stfb->statts[stfb->num_statts++] = statt;
271
   }
271
   }
272
   stfb->stamp++;
272
   stfb->stamp++;
273
}
273
}
274
 
274
 
275
/**
275
/**
276
 * Add a renderbuffer to the framebuffer.
276
 * Add a renderbuffer to the framebuffer.
277
 */
277
 */
278
static boolean
278
static boolean
279
st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
279
st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
280
                                gl_buffer_index idx)
280
                                gl_buffer_index idx)
281
{
281
{
282
   struct gl_renderbuffer *rb;
282
   struct gl_renderbuffer *rb;
283
   enum pipe_format format;
283
   enum pipe_format format;
284
   boolean sw;
284
   boolean sw;
285
 
285
 
286
   if (!stfb->iface)
286
   if (!stfb->iface)
287
      return FALSE;
287
      return FALSE;
288
 
288
 
289
   /* do not distinguish depth/stencil buffers */
289
   /* do not distinguish depth/stencil buffers */
290
   if (idx == BUFFER_STENCIL)
290
   if (idx == BUFFER_STENCIL)
291
      idx = BUFFER_DEPTH;
291
      idx = BUFFER_DEPTH;
292
 
292
 
293
   switch (idx) {
293
   switch (idx) {
294
   case BUFFER_DEPTH:
294
   case BUFFER_DEPTH:
295
      format = stfb->iface->visual->depth_stencil_format;
295
      format = stfb->iface->visual->depth_stencil_format;
296
      sw = FALSE;
296
      sw = FALSE;
297
      break;
297
      break;
298
   case BUFFER_ACCUM:
298
   case BUFFER_ACCUM:
299
      format = stfb->iface->visual->accum_format;
299
      format = stfb->iface->visual->accum_format;
300
      sw = TRUE;
300
      sw = TRUE;
301
      break;
301
      break;
302
   default:
302
   default:
303
      format = stfb->iface->visual->color_format;
303
      format = stfb->iface->visual->color_format;
304
      sw = FALSE;
304
      sw = FALSE;
305
      break;
305
      break;
306
   }
306
   }
307
 
307
 
308
   if (format == PIPE_FORMAT_NONE)
308
   if (format == PIPE_FORMAT_NONE)
309
      return FALSE;
309
      return FALSE;
310
 
310
 
311
   rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
311
   rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
312
   if (!rb)
312
   if (!rb)
313
      return FALSE;
313
      return FALSE;
314
 
314
 
315
   if (idx != BUFFER_DEPTH) {
315
   if (idx != BUFFER_DEPTH) {
316
      _mesa_add_renderbuffer(&stfb->Base, idx, rb);
316
      _mesa_add_renderbuffer(&stfb->Base, idx, rb);
317
   }
317
   }
318
   else {
318
   else {
319
      if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
319
      if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
320
         _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
320
         _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
321
      if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
321
      if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
322
         _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, rb);
322
         _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, rb);
323
   }
323
   }
324
 
324
 
325
   return TRUE;
325
   return TRUE;
326
}
326
}
327
 
327
 
328
/**
328
/**
329
 * Intialize a struct gl_config from a visual.
329
 * Intialize a struct gl_config from a visual.
330
 */
330
 */
331
static void
331
static void
332
st_visual_to_context_mode(const struct st_visual *visual,
332
st_visual_to_context_mode(const struct st_visual *visual,
333
                          struct gl_config *mode)
333
                          struct gl_config *mode)
334
{
334
{
335
   memset(mode, 0, sizeof(*mode));
335
   memset(mode, 0, sizeof(*mode));
336
 
336
 
337
   if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
337
   if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
338
      mode->doubleBufferMode = GL_TRUE;
338
      mode->doubleBufferMode = GL_TRUE;
339
   if (st_visual_have_buffers(visual,
339
   if (st_visual_have_buffers(visual,
340
            ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
340
            ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
341
      mode->stereoMode = GL_TRUE;
341
      mode->stereoMode = GL_TRUE;
342
 
342
 
343
   if (visual->color_format != PIPE_FORMAT_NONE) {
343
   if (visual->color_format != PIPE_FORMAT_NONE) {
344
      mode->rgbMode = GL_TRUE;
344
      mode->rgbMode = GL_TRUE;
345
 
345
 
346
      mode->redBits =
346
      mode->redBits =
347
         util_format_get_component_bits(visual->color_format,
347
         util_format_get_component_bits(visual->color_format,
348
               UTIL_FORMAT_COLORSPACE_RGB, 0);
348
               UTIL_FORMAT_COLORSPACE_RGB, 0);
349
      mode->greenBits =
349
      mode->greenBits =
350
         util_format_get_component_bits(visual->color_format,
350
         util_format_get_component_bits(visual->color_format,
351
               UTIL_FORMAT_COLORSPACE_RGB, 1);
351
               UTIL_FORMAT_COLORSPACE_RGB, 1);
352
      mode->blueBits =
352
      mode->blueBits =
353
         util_format_get_component_bits(visual->color_format,
353
         util_format_get_component_bits(visual->color_format,
354
               UTIL_FORMAT_COLORSPACE_RGB, 2);
354
               UTIL_FORMAT_COLORSPACE_RGB, 2);
355
      mode->alphaBits =
355
      mode->alphaBits =
356
         util_format_get_component_bits(visual->color_format,
356
         util_format_get_component_bits(visual->color_format,
357
               UTIL_FORMAT_COLORSPACE_RGB, 3);
357
               UTIL_FORMAT_COLORSPACE_RGB, 3);
358
 
358
 
359
      mode->rgbBits = mode->redBits +
359
      mode->rgbBits = mode->redBits +
360
         mode->greenBits + mode->blueBits + mode->alphaBits;
360
         mode->greenBits + mode->blueBits + mode->alphaBits;
361
   }
361
   }
362
 
362
 
363
   if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
363
   if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
364
      mode->depthBits =
364
      mode->depthBits =
365
         util_format_get_component_bits(visual->depth_stencil_format,
365
         util_format_get_component_bits(visual->depth_stencil_format,
366
               UTIL_FORMAT_COLORSPACE_ZS, 0);
366
               UTIL_FORMAT_COLORSPACE_ZS, 0);
367
      mode->stencilBits =
367
      mode->stencilBits =
368
         util_format_get_component_bits(visual->depth_stencil_format,
368
         util_format_get_component_bits(visual->depth_stencil_format,
369
               UTIL_FORMAT_COLORSPACE_ZS, 1);
369
               UTIL_FORMAT_COLORSPACE_ZS, 1);
370
 
370
 
371
      mode->haveDepthBuffer = mode->depthBits > 0;
371
      mode->haveDepthBuffer = mode->depthBits > 0;
372
      mode->haveStencilBuffer = mode->stencilBits > 0;
372
      mode->haveStencilBuffer = mode->stencilBits > 0;
373
   }
373
   }
374
 
374
 
375
   if (visual->accum_format != PIPE_FORMAT_NONE) {
375
   if (visual->accum_format != PIPE_FORMAT_NONE) {
376
      mode->haveAccumBuffer = GL_TRUE;
376
      mode->haveAccumBuffer = GL_TRUE;
377
 
377
 
378
      mode->accumRedBits =
378
      mode->accumRedBits =
379
         util_format_get_component_bits(visual->accum_format,
379
         util_format_get_component_bits(visual->accum_format,
380
               UTIL_FORMAT_COLORSPACE_RGB, 0);
380
               UTIL_FORMAT_COLORSPACE_RGB, 0);
381
      mode->accumGreenBits =
381
      mode->accumGreenBits =
382
         util_format_get_component_bits(visual->accum_format,
382
         util_format_get_component_bits(visual->accum_format,
383
               UTIL_FORMAT_COLORSPACE_RGB, 1);
383
               UTIL_FORMAT_COLORSPACE_RGB, 1);
384
      mode->accumBlueBits =
384
      mode->accumBlueBits =
385
         util_format_get_component_bits(visual->accum_format,
385
         util_format_get_component_bits(visual->accum_format,
386
               UTIL_FORMAT_COLORSPACE_RGB, 2);
386
               UTIL_FORMAT_COLORSPACE_RGB, 2);
387
      mode->accumAlphaBits =
387
      mode->accumAlphaBits =
388
         util_format_get_component_bits(visual->accum_format,
388
         util_format_get_component_bits(visual->accum_format,
389
               UTIL_FORMAT_COLORSPACE_RGB, 3);
389
               UTIL_FORMAT_COLORSPACE_RGB, 3);
390
   }
390
   }
391
 
391
 
392
   if (visual->samples > 1) {
392
   if (visual->samples > 1) {
393
      mode->sampleBuffers = 1;
393
      mode->sampleBuffers = 1;
394
      mode->samples = visual->samples;
394
      mode->samples = visual->samples;
395
   }
395
   }
396
}
396
}
397
 
397
 
398
/**
398
/**
399
 * Create a framebuffer from a manager interface.
399
 * Create a framebuffer from a manager interface.
400
 */
400
 */
401
static struct st_framebuffer *
401
static struct st_framebuffer *
402
st_framebuffer_create(struct st_framebuffer_iface *stfbi)
402
st_framebuffer_create(struct st_framebuffer_iface *stfbi)
403
{
403
{
404
   struct st_framebuffer *stfb;
404
   struct st_framebuffer *stfb;
405
   struct gl_config mode;
405
   struct gl_config mode;
406
   gl_buffer_index idx;
406
   gl_buffer_index idx;
407
 
407
 
408
   if (!stfbi)
408
   if (!stfbi)
409
      return NULL;
409
      return NULL;
410
 
410
 
411
   stfb = CALLOC_STRUCT(st_framebuffer);
411
   stfb = CALLOC_STRUCT(st_framebuffer);
412
   if (!stfb)
412
   if (!stfb)
413
      return NULL;
413
      return NULL;
414
 
414
 
415
   st_visual_to_context_mode(stfbi->visual, &mode);
415
   st_visual_to_context_mode(stfbi->visual, &mode);
416
   _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
416
   _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
417
 
417
 
418
   stfb->iface = stfbi;
418
   stfb->iface = stfbi;
419
   stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
419
   stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
420
 
420
 
421
   /* add the color buffer */
421
   /* add the color buffer */
422
   idx = stfb->Base._ColorDrawBufferIndexes[0];
422
   idx = stfb->Base._ColorDrawBufferIndexes[0];
423
   if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
423
   if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
424
      free(stfb);
424
      free(stfb);
425
      return NULL;
425
      return NULL;
426
   }
426
   }
427
 
427
 
428
   st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
428
   st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
429
   st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
429
   st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
430
 
430
 
431
   stfb->stamp = 0;
431
   stfb->stamp = 0;
432
   st_framebuffer_update_attachments(stfb);
432
   st_framebuffer_update_attachments(stfb);
433
 
433
 
434
   return stfb;
434
   return stfb;
435
}
435
}
436
 
436
 
437
/**
437
/**
438
 * Reference a framebuffer.
438
 * Reference a framebuffer.
439
 */
439
 */
440
static void
440
static void
441
st_framebuffer_reference(struct st_framebuffer **ptr,
441
st_framebuffer_reference(struct st_framebuffer **ptr,
442
                         struct st_framebuffer *stfb)
442
                         struct st_framebuffer *stfb)
443
{
443
{
444
   struct gl_framebuffer *fb = &stfb->Base;
444
   struct gl_framebuffer *fb = &stfb->Base;
445
   _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
445
   _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
446
}
446
}
447
 
447
 
448
static void
448
static void
449
st_context_flush(struct st_context_iface *stctxi, unsigned flags,
449
st_context_flush(struct st_context_iface *stctxi, unsigned flags,
450
                 struct pipe_fence_handle **fence)
450
                 struct pipe_fence_handle **fence)
451
{
451
{
452
   struct st_context *st = (struct st_context *) stctxi;
452
   struct st_context *st = (struct st_context *) stctxi;
453
   unsigned pipe_flags = 0;
453
   unsigned pipe_flags = 0;
454
 
454
 
455
   if (flags & ST_FLUSH_END_OF_FRAME) {
455
   if (flags & ST_FLUSH_END_OF_FRAME) {
456
      pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
456
      pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
457
   }
457
   }
458
 
458
 
459
   st_flush(st, fence, pipe_flags);
459
   st_flush(st, fence, pipe_flags);
460
   if (flags & ST_FLUSH_FRONT)
460
   if (flags & ST_FLUSH_FRONT)
461
      st_manager_flush_frontbuffer(st);
461
      st_manager_flush_frontbuffer(st);
462
}
462
}
463
 
463
 
464
static boolean
464
static boolean
465
st_context_teximage(struct st_context_iface *stctxi,
465
st_context_teximage(struct st_context_iface *stctxi,
466
                    enum st_texture_type tex_type,
466
                    enum st_texture_type tex_type,
467
                    int level, enum pipe_format pipe_format,
467
                    int level, enum pipe_format pipe_format,
468
                    struct pipe_resource *tex, boolean mipmap)
468
                    struct pipe_resource *tex, boolean mipmap)
469
{
469
{
470
   struct st_context *st = (struct st_context *) stctxi;
470
   struct st_context *st = (struct st_context *) stctxi;
471
   struct gl_context *ctx = st->ctx;
471
   struct gl_context *ctx = st->ctx;
472
   struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
472
   struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
473
   struct gl_texture_object *texObj;
473
   struct gl_texture_object *texObj;
474
   struct gl_texture_image *texImage;
474
   struct gl_texture_image *texImage;
475
   struct st_texture_object *stObj;
475
   struct st_texture_object *stObj;
476
   struct st_texture_image *stImage;
476
   struct st_texture_image *stImage;
477
   GLenum internalFormat;
477
   GLenum internalFormat;
478
   GLuint width, height, depth;
478
   GLuint width, height, depth;
479
   GLenum target;
479
   GLenum target;
480
 
480
 
481
   switch (tex_type) {
481
   switch (tex_type) {
482
   case ST_TEXTURE_1D:
482
   case ST_TEXTURE_1D:
483
      target = GL_TEXTURE_1D;
483
      target = GL_TEXTURE_1D;
484
      break;
484
      break;
485
   case ST_TEXTURE_2D:
485
   case ST_TEXTURE_2D:
486
      target = GL_TEXTURE_2D;
486
      target = GL_TEXTURE_2D;
487
      break;
487
      break;
488
   case ST_TEXTURE_3D:
488
   case ST_TEXTURE_3D:
489
      target = GL_TEXTURE_3D;
489
      target = GL_TEXTURE_3D;
490
      break;
490
      break;
491
   case ST_TEXTURE_RECT:
491
   case ST_TEXTURE_RECT:
492
      target = GL_TEXTURE_RECTANGLE_ARB;
492
      target = GL_TEXTURE_RECTANGLE_ARB;
493
      break;
493
      break;
494
   default:
494
   default:
495
      return FALSE;
495
      return FALSE;
496
   }
496
   }
497
 
497
 
498
   texObj = _mesa_select_tex_object(ctx, texUnit, target);
498
   texObj = _mesa_select_tex_object(ctx, texUnit, target);
499
   _mesa_lock_texture(ctx, texObj);
499
   _mesa_lock_texture(ctx, texObj);
500
 
500
 
501
   stObj = st_texture_object(texObj);
501
   stObj = st_texture_object(texObj);
502
   /* switch to surface based */
502
   /* switch to surface based */
503
   if (!stObj->surface_based) {
503
   if (!stObj->surface_based) {
504
      _mesa_clear_texture_object(ctx, texObj);
504
      _mesa_clear_texture_object(ctx, texObj);
505
      stObj->surface_based = GL_TRUE;
505
      stObj->surface_based = GL_TRUE;
506
   }
506
   }
507
 
507
 
508
   texImage = _mesa_get_tex_image(ctx, texObj, target, level);
508
   texImage = _mesa_get_tex_image(ctx, texObj, target, level);
509
   stImage = st_texture_image(texImage);
509
   stImage = st_texture_image(texImage);
510
   if (tex) {
510
   if (tex) {
511
      gl_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
511
      gl_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
512
 
512
 
513
      if (util_format_has_alpha(tex->format))
513
      if (util_format_has_alpha(tex->format))
514
         internalFormat = GL_RGBA;
514
         internalFormat = GL_RGBA;
515
      else
515
      else
516
         internalFormat = GL_RGB;
516
         internalFormat = GL_RGB;
517
 
517
 
518
      _mesa_init_teximage_fields(ctx, texImage,
518
      _mesa_init_teximage_fields(ctx, texImage,
519
                                 tex->width0, tex->height0, 1, 0,
519
                                 tex->width0, tex->height0, 1, 0,
520
                                 internalFormat, texFormat);
520
                                 internalFormat, texFormat);
521
 
521
 
522
      width = tex->width0;
522
      width = tex->width0;
523
      height = tex->height0;
523
      height = tex->height0;
524
      depth = tex->depth0;
524
      depth = tex->depth0;
525
 
525
 
526
      /* grow the image size until we hit level = 0 */
526
      /* grow the image size until we hit level = 0 */
527
      while (level > 0) {
527
      while (level > 0) {
528
         if (width != 1)
528
         if (width != 1)
529
            width <<= 1;
529
            width <<= 1;
530
         if (height != 1)
530
         if (height != 1)
531
            height <<= 1;
531
            height <<= 1;
532
         if (depth != 1)
532
         if (depth != 1)
533
            depth <<= 1;
533
            depth <<= 1;
534
         level--;
534
         level--;
535
      }
535
      }
536
   }
536
   }
537
   else {
537
   else {
538
      _mesa_clear_texture_image(ctx, texImage);
538
      _mesa_clear_texture_image(ctx, texImage);
539
      width = height = depth = 0;
539
      width = height = depth = 0;
540
   }
540
   }
541
 
541
 
542
   pipe_resource_reference(&stImage->pt, tex);
542
   pipe_resource_reference(&stImage->pt, tex);
543
   stObj->width0 = width;
543
   stObj->width0 = width;
544
   stObj->height0 = height;
544
   stObj->height0 = height;
545
   stObj->depth0 = depth;
545
   stObj->depth0 = depth;
546
   stObj->surface_format = pipe_format;
546
   stObj->surface_format = pipe_format;
547
 
547
 
548
   _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
548
   _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
549
   _mesa_unlock_texture(ctx, texObj);
549
   _mesa_unlock_texture(ctx, texObj);
550
   
550
 
551
   return TRUE;
551
   return TRUE;
552
}
552
}
553
 
553
 
554
static void
554
static void
555
st_context_copy(struct st_context_iface *stctxi,
555
st_context_copy(struct st_context_iface *stctxi,
556
                struct st_context_iface *stsrci, unsigned mask)
556
                struct st_context_iface *stsrci, unsigned mask)
557
{
557
{
558
   struct st_context *st = (struct st_context *) stctxi;
558
   struct st_context *st = (struct st_context *) stctxi;
559
   struct st_context *src = (struct st_context *) stsrci;
559
   struct st_context *src = (struct st_context *) stsrci;
560
 
560
 
561
   _mesa_copy_context(src->ctx, st->ctx, mask);
561
   _mesa_copy_context(src->ctx, st->ctx, mask);
562
}
562
}
563
 
563
 
564
static boolean
564
static boolean
565
st_context_share(struct st_context_iface *stctxi,
565
st_context_share(struct st_context_iface *stctxi,
566
                 struct st_context_iface *stsrci)
566
                 struct st_context_iface *stsrci)
567
{
567
{
568
   struct st_context *st = (struct st_context *) stctxi;
568
   struct st_context *st = (struct st_context *) stctxi;
569
   struct st_context *src = (struct st_context *) stsrci;
569
   struct st_context *src = (struct st_context *) stsrci;
570
 
570
 
571
   return _mesa_share_state(st->ctx, src->ctx);
571
   return _mesa_share_state(st->ctx, src->ctx);
572
}
572
}
573
 
573
 
574
static void
574
static void
575
st_context_destroy(struct st_context_iface *stctxi)
575
st_context_destroy(struct st_context_iface *stctxi)
576
{
576
{
577
   struct st_context *st = (struct st_context *) stctxi;
577
   struct st_context *st = (struct st_context *) stctxi;
578
   st_destroy_context(st);
578
   st_destroy_context(st);
579
}
579
}
580
 
580
 
581
static struct st_context_iface *
581
static struct st_context_iface *
582
st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
582
st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
583
                      const struct st_context_attribs *attribs,
583
                      const struct st_context_attribs *attribs,
584
                      enum st_context_error *error,
584
                      enum st_context_error *error,
585
                      struct st_context_iface *shared_stctxi)
585
                      struct st_context_iface *shared_stctxi)
586
{
586
{
587
   struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
587
   struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
588
   struct st_context *st;
588
   struct st_context *st;
589
   struct pipe_context *pipe;
589
   struct pipe_context *pipe;
590
   struct gl_config mode;
590
   struct gl_config mode;
591
   gl_api api;
591
   gl_api api;
592
 
592
 
593
   if (!(stapi->profile_mask & (1 << attribs->profile)))
593
   if (!(stapi->profile_mask & (1 << attribs->profile)))
594
      return NULL;
594
      return NULL;
595
 
595
 
596
   switch (attribs->profile) {
596
   switch (attribs->profile) {
597
   case ST_PROFILE_DEFAULT:
597
   case ST_PROFILE_DEFAULT:
598
      api = API_OPENGL_COMPAT;
598
      api = API_OPENGL_COMPAT;
599
      break;
599
      break;
600
   case ST_PROFILE_OPENGL_ES1:
600
   case ST_PROFILE_OPENGL_ES1:
601
      api = API_OPENGLES;
601
      api = API_OPENGLES;
602
      break;
602
      break;
603
   case ST_PROFILE_OPENGL_ES2:
603
   case ST_PROFILE_OPENGL_ES2:
604
      api = API_OPENGLES2;
604
      api = API_OPENGLES2;
605
      break;
605
      break;
606
   case ST_PROFILE_OPENGL_CORE:
606
   case ST_PROFILE_OPENGL_CORE:
607
      api = API_OPENGL_CORE;
607
      api = API_OPENGL_CORE;
608
      break;
608
      break;
609
   default:
609
   default:
610
      *error = ST_CONTEXT_ERROR_BAD_API;
610
      *error = ST_CONTEXT_ERROR_BAD_API;
611
      return NULL;
611
      return NULL;
612
      break;
612
      break;
613
   }
613
   }
614
 
614
 
615
   pipe = smapi->screen->context_create(smapi->screen, NULL);
615
   pipe = smapi->screen->context_create(smapi->screen, NULL);
616
   if (!pipe) {
616
   if (!pipe) {
617
      *error = ST_CONTEXT_ERROR_NO_MEMORY;
617
      *error = ST_CONTEXT_ERROR_NO_MEMORY;
618
      return NULL;
618
      return NULL;
619
   }
619
   }
620
 
620
 
621
   st_visual_to_context_mode(&attribs->visual, &mode);
621
   st_visual_to_context_mode(&attribs->visual, &mode);
622
   st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
622
   st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
623
   if (!st) {
623
   if (!st) {
624
      *error = ST_CONTEXT_ERROR_NO_MEMORY;
624
      *error = ST_CONTEXT_ERROR_NO_MEMORY;
625
      pipe->destroy(pipe);
625
      pipe->destroy(pipe);
626
      return NULL;
626
      return NULL;
627
   }
627
   }
628
 
628
 
629
   if (attribs->flags & ST_CONTEXT_FLAG_DEBUG)
629
   if (attribs->flags & ST_CONTEXT_FLAG_DEBUG)
630
      st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
630
      st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
631
   if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
631
   if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
632
      st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
632
      st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
-
 
633
 
633
 
634
#if 0
634
   /* need to perform version check */
635
   /* need to perform version check */
635
   if (attribs->major > 1 || attribs->minor > 0) {
636
   if (attribs->major > 1 || attribs->minor > 0) {
636
      /* Is the actual version less than the requested version?
637
      /* Is the actual version less than the requested version?
637
       */
638
       */
638
      if (st->ctx->Version < attribs->major * 10 + attribs->minor) {
639
      if (st->ctx->Version < attribs->major * 10 + attribs->minor) {
639
	 *error = ST_CONTEXT_ERROR_BAD_VERSION;
640
	 *error = ST_CONTEXT_ERROR_BAD_VERSION;
640
         st_destroy_context(st);
641
         st_destroy_context(st);
641
         return NULL;
642
         return NULL;
642
      }
643
      }
643
   }
644
   }
-
 
645
#endif
644
 
646
 
645
   st->invalidate_on_gl_viewport =
647
   st->invalidate_on_gl_viewport =
646
      smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
648
      smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
647
 
649
 
648
   st->iface.destroy = st_context_destroy;
650
   st->iface.destroy = st_context_destroy;
649
   st->iface.flush = st_context_flush;
651
   st->iface.flush = st_context_flush;
650
   st->iface.teximage = st_context_teximage;
652
   st->iface.teximage = st_context_teximage;
651
   st->iface.copy = st_context_copy;
653
   st->iface.copy = st_context_copy;
652
   st->iface.share = st_context_share;
654
   st->iface.share = st_context_share;
653
   st->iface.st_context_private = (void *) smapi;
655
   st->iface.st_context_private = (void *) smapi;
654
   st->iface.cso_context = st->cso_context;
656
   st->iface.cso_context = st->cso_context;
655
   st->iface.pipe = st->pipe;
657
   st->iface.pipe = st->pipe;
656
 
658
 
657
   *error = ST_CONTEXT_SUCCESS;
659
   *error = ST_CONTEXT_SUCCESS;
658
   return &st->iface;
660
   return &st->iface;
659
}
661
}
660
 
662
 
661
static struct st_context_iface *
663
static struct st_context_iface *
662
st_api_get_current(struct st_api *stapi)
664
st_api_get_current(struct st_api *stapi)
663
{
665
{
664
   GET_CURRENT_CONTEXT(ctx);
666
   GET_CURRENT_CONTEXT(ctx);
665
   struct st_context *st = (ctx) ? ctx->st : NULL;
667
   struct st_context *st = (ctx) ? ctx->st : NULL;
666
 
668
 
667
   return (st) ? &st->iface : NULL;
669
   return (st) ? &st->iface : NULL;
668
}
670
}
669
 
671
 
670
static struct st_framebuffer *
672
static struct st_framebuffer *
671
st_framebuffer_reuse_or_create(struct gl_framebuffer *fb,
673
st_framebuffer_reuse_or_create(struct gl_framebuffer *fb,
672
                               struct st_framebuffer_iface *stfbi)
674
                               struct st_framebuffer_iface *stfbi)
673
{
675
{
674
   struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
676
   struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
675
 
677
 
676
   /* dummy framebuffers cant be used as st_framebuffer */
678
   /* dummy framebuffers cant be used as st_framebuffer */
677
   if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
679
   if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
678
       cur->iface == stfbi) {
680
       cur->iface == stfbi) {
679
      /* reuse the current stfb */
681
      /* reuse the current stfb */
680
      st_framebuffer_reference(&stfb, cur);
682
      st_framebuffer_reference(&stfb, cur);
681
   }
683
   }
682
   else {
684
   else {
683
      /* create a new one */
685
      /* create a new one */
684
      stfb = st_framebuffer_create(stfbi);
686
      stfb = st_framebuffer_create(stfbi);
685
   }
687
   }
686
 
688
 
687
   return stfb;
689
   return stfb;
688
}
690
}
689
 
691
 
690
static boolean
692
static boolean
691
st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
693
st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
692
                    struct st_framebuffer_iface *stdrawi,
694
                    struct st_framebuffer_iface *stdrawi,
693
                    struct st_framebuffer_iface *streadi)
695
                    struct st_framebuffer_iface *streadi)
694
{
696
{
695
   struct st_context *st = (struct st_context *) stctxi;
697
   struct st_context *st = (struct st_context *) stctxi;
696
   struct st_framebuffer *stdraw, *stread;
698
   struct st_framebuffer *stdraw, *stread;
697
   boolean ret;
699
   boolean ret;
698
 
700
 
699
   _glapi_check_multithread();
701
   _glapi_check_multithread();
700
 
702
 
701
   if (st) {
703
   if (st) {
702
      /* reuse or create the draw fb */
704
      /* reuse or create the draw fb */
703
      stdraw = st_framebuffer_reuse_or_create(st->ctx->WinSysDrawBuffer,
705
      stdraw = st_framebuffer_reuse_or_create(st->ctx->WinSysDrawBuffer,
704
                                              stdrawi);
706
                                              stdrawi);
705
      if (streadi != stdrawi) {
707
      if (streadi != stdrawi) {
706
         /* do the same for the read fb */
708
         /* do the same for the read fb */
707
         stread = st_framebuffer_reuse_or_create(st->ctx->WinSysReadBuffer,
709
         stread = st_framebuffer_reuse_or_create(st->ctx->WinSysReadBuffer,
708
                                                 streadi);
710
                                                 streadi);
709
      }
711
      }
710
      else {
712
      else {
711
         stread = NULL;
713
         stread = NULL;
712
         /* reuse the draw fb for the read fb */
714
         /* reuse the draw fb for the read fb */
713
         if (stdraw)
715
         if (stdraw)
714
            st_framebuffer_reference(&stread, stdraw);
716
            st_framebuffer_reference(&stread, stdraw);
715
      }
717
      }
716
 
718
 
717
      if (stdraw && stread) {
719
      if (stdraw && stread) {
718
         st_framebuffer_validate(stdraw, st);
720
         st_framebuffer_validate(stdraw, st);
719
         if (stread != stdraw)
721
         if (stread != stdraw)
720
            st_framebuffer_validate(stread, st);
722
            st_framebuffer_validate(stread, st);
721
 
723
 
722
         ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
724
         ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
723
 
725
 
724
         st->draw_stamp = stdraw->stamp - 1;
726
         st->draw_stamp = stdraw->stamp - 1;
725
         st->read_stamp = stread->stamp - 1;
727
         st->read_stamp = stread->stamp - 1;
726
         st_context_validate(st, stdraw, stread);
728
         st_context_validate(st, stdraw, stread);
727
      }
729
      }
728
      else {
730
      else {
729
         struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
731
         struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
730
         ret = _mesa_make_current(st->ctx, incomplete, incomplete);
732
         ret = _mesa_make_current(st->ctx, incomplete, incomplete);
731
      }
733
      }
732
 
734
 
733
      st_framebuffer_reference(&stdraw, NULL);
735
      st_framebuffer_reference(&stdraw, NULL);
734
      st_framebuffer_reference(&stread, NULL);
736
      st_framebuffer_reference(&stread, NULL);
735
   }
737
   }
736
   else {
738
   else {
737
      ret = _mesa_make_current(NULL, NULL, NULL);
739
      ret = _mesa_make_current(NULL, NULL, NULL);
738
   }
740
   }
739
 
741
 
740
   return ret;
742
   return ret;
741
}
743
}
742
 
744
 
743
static st_proc_t
745
static st_proc_t
744
st_api_get_proc_address(struct st_api *stapi, const char *procname)
746
st_api_get_proc_address(struct st_api *stapi, const char *procname)
745
{
747
{
746
   return (st_proc_t) _glapi_get_proc_address(procname);
748
   return (st_proc_t) _glapi_get_proc_address(procname);
747
}
749
}
748
 
750
 
749
static void
751
static void
750
st_api_destroy(struct st_api *stapi)
752
st_api_destroy(struct st_api *stapi)
751
{
753
{
752
}
754
}
753
 
755
 
754
/**
756
/**
755
 * Flush the front buffer if the current context renders to the front buffer.
757
 * Flush the front buffer if the current context renders to the front buffer.
756
 */
758
 */
757
void
759
void
758
st_manager_flush_frontbuffer(struct st_context *st)
760
st_manager_flush_frontbuffer(struct st_context *st)
759
{
761
{
760
   struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
762
   struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
761
   struct st_renderbuffer *strb = NULL;
763
   struct st_renderbuffer *strb = NULL;
762
 
764
 
763
   if (stfb)
765
   if (stfb)
764
      strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
766
      strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
765
   if (!strb)
767
   if (!strb)
766
      return;
768
      return;
767
 
769
 
768
   /* never a dummy fb */
770
   /* never a dummy fb */
769
   assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
771
   assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
770
   stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
772
   stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
771
}
773
}
772
 
774
 
773
/**
775
/**
774
 * Return the surface of an EGLImage.
776
 * Return the surface of an EGLImage.
775
 * FIXME: I think this should operate on resources, not surfaces
777
 * FIXME: I think this should operate on resources, not surfaces
776
 */
778
 */
777
struct pipe_surface *
779
struct pipe_surface *
778
st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
780
st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
779
{
781
{
780
   struct st_manager *smapi =
782
   struct st_manager *smapi =
781
      (struct st_manager *) st->iface.st_context_private;
783
      (struct st_manager *) st->iface.st_context_private;
782
   struct st_egl_image stimg;
784
   struct st_egl_image stimg;
783
   struct pipe_surface *ps, surf_tmpl;
785
   struct pipe_surface *ps, surf_tmpl;
784
 
786
 
785
   if (!smapi || !smapi->get_egl_image)
787
   if (!smapi || !smapi->get_egl_image)
786
      return NULL;
788
      return NULL;
787
 
789
 
788
   memset(&stimg, 0, sizeof(stimg));
790
   memset(&stimg, 0, sizeof(stimg));
789
   if (!smapi->get_egl_image(smapi, eglimg, &stimg))
791
   if (!smapi->get_egl_image(smapi, eglimg, &stimg))
790
      return NULL;
792
      return NULL;
791
 
793
 
792
   u_surface_default_template(&surf_tmpl, stimg.texture);
794
   u_surface_default_template(&surf_tmpl, stimg.texture);
793
   surf_tmpl.u.tex.level = stimg.level;
795
   surf_tmpl.u.tex.level = stimg.level;
794
   surf_tmpl.u.tex.first_layer = stimg.layer;
796
   surf_tmpl.u.tex.first_layer = stimg.layer;
795
   surf_tmpl.u.tex.last_layer = stimg.layer;
797
   surf_tmpl.u.tex.last_layer = stimg.layer;
796
   ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
798
   ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
797
   pipe_resource_reference(&stimg.texture, NULL);
799
   pipe_resource_reference(&stimg.texture, NULL);
798
 
800
 
799
   return ps;
801
   return ps;
800
}
802
}
801
 
803
 
802
/**
804
/**
803
 * Re-validate the framebuffers.
805
 * Re-validate the framebuffers.
804
 */
806
 */
805
void
807
void
806
st_manager_validate_framebuffers(struct st_context *st)
808
st_manager_validate_framebuffers(struct st_context *st)
807
{
809
{
808
   struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
810
   struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
809
   struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
811
   struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
810
 
812
 
811
   if (stdraw)
813
   if (stdraw)
812
      st_framebuffer_validate(stdraw, st);
814
      st_framebuffer_validate(stdraw, st);
813
   if (stread && stread != stdraw)
815
   if (stread && stread != stdraw)
814
      st_framebuffer_validate(stread, st);
816
      st_framebuffer_validate(stread, st);
815
 
817
 
816
   st_context_validate(st, stdraw, stread);
818
   st_context_validate(st, stdraw, stread);
817
}
819
}
818
 
820
 
819
/**
821
/**
820
 * Add a color renderbuffer on demand.
822
 * Add a color renderbuffer on demand.
821
 */
823
 */
822
boolean
824
boolean
823
st_manager_add_color_renderbuffer(struct st_context *st,
825
st_manager_add_color_renderbuffer(struct st_context *st,
824
                                  struct gl_framebuffer *fb,
826
                                  struct gl_framebuffer *fb,
825
                                  gl_buffer_index idx)
827
                                  gl_buffer_index idx)
826
{
828
{
827
   struct st_framebuffer *stfb = st_ws_framebuffer(fb);
829
   struct st_framebuffer *stfb = st_ws_framebuffer(fb);
828
 
830
 
829
   /* FBO */
831
   /* FBO */
830
   if (!stfb)
832
   if (!stfb)
831
      return FALSE;
833
      return FALSE;
832
 
834
 
833
   if (stfb->Base.Attachment[idx].Renderbuffer)
835
   if (stfb->Base.Attachment[idx].Renderbuffer)
834
      return TRUE;
836
      return TRUE;
835
 
837
 
836
   switch (idx) {
838
   switch (idx) {
837
   case BUFFER_FRONT_LEFT:
839
   case BUFFER_FRONT_LEFT:
838
   case BUFFER_BACK_LEFT:
840
   case BUFFER_BACK_LEFT:
839
   case BUFFER_FRONT_RIGHT:
841
   case BUFFER_FRONT_RIGHT:
840
   case BUFFER_BACK_RIGHT:
842
   case BUFFER_BACK_RIGHT:
841
      break;
843
      break;
842
   default:
844
   default:
843
      return FALSE;
845
      return FALSE;
844
      break;
846
      break;
845
   }
847
   }
846
 
848
 
847
   if (!st_framebuffer_add_renderbuffer(stfb, idx))
849
   if (!st_framebuffer_add_renderbuffer(stfb, idx))
848
      return FALSE;
850
      return FALSE;
849
 
851
 
850
   st_framebuffer_update_attachments(stfb);
852
   st_framebuffer_update_attachments(stfb);
851
 
853
 
852
   /*
854
   /*
853
    * Force a call to the state tracker manager to validate the
855
    * Force a call to the state tracker manager to validate the
854
    * new renderbuffer. It might be that there is a window system
856
    * new renderbuffer. It might be that there is a window system
855
    * renderbuffer available.
857
    * renderbuffer available.
856
    */
858
    */
857
   if(stfb->iface)
859
   if(stfb->iface)
858
      stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
860
      stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
859
 
861
 
860
   st_invalidate_state(st->ctx, _NEW_BUFFERS);
862
   st_invalidate_state(st->ctx, _NEW_BUFFERS);
861
 
863
 
862
   return TRUE;
864
   return TRUE;
863
}
865
}
864
 
866
 
865
static const struct st_api st_gl_api = {
867
static const struct st_api st_gl_api = {
866
   "Mesa " PACKAGE_VERSION,
868
   "Mesa " PACKAGE_VERSION,
867
   ST_API_OPENGL,
869
   ST_API_OPENGL,
868
   ST_PROFILE_DEFAULT_MASK |
870
   ST_PROFILE_DEFAULT_MASK |
869
   ST_PROFILE_OPENGL_CORE_MASK |
871
   ST_PROFILE_OPENGL_CORE_MASK |
870
   ST_PROFILE_OPENGL_ES1_MASK |
872
   ST_PROFILE_OPENGL_ES1_MASK |
871
   ST_PROFILE_OPENGL_ES2_MASK |
873
   ST_PROFILE_OPENGL_ES2_MASK |
872
   0,
874
   0,
873
   ST_API_FEATURE_MS_VISUALS_MASK,
875
   ST_API_FEATURE_MS_VISUALS_MASK,
874
   st_api_destroy,
876
   st_api_destroy,
875
   st_api_get_proc_address,
877
   st_api_get_proc_address,
876
   st_api_create_context,
878
   st_api_create_context,
877
   st_api_make_current,
879
   st_api_make_current,
878
   st_api_get_current,
880
   st_api_get_current,
879
};
881
};
880
 
882
 
881
struct st_api *
883
struct st_api *
882
st_gl_api_create(void)
884
st_gl_api_create(void)
883
{
885
{
884
   return (struct st_api *) &st_gl_api;
886
   return (struct st_api *) &st_gl_api;
885
}
887
}