1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
34 #include "dri_screen.h"
35 #include "dri_drawable.h"
36 #include "dri_context.h"
37 #include "state_tracker/drm_driver.h"
39 #include "pipe/p_context.h"
40 #include "state_tracker/st_context.h"
43 dri_create_context(gl_api api
, const struct gl_config
* visual
,
45 unsigned major_version
,
46 unsigned minor_version
,
50 void *sharedContextPrivate
)
52 __DRIscreen
*sPriv
= cPriv
->driScreenPriv
;
53 struct dri_screen
*screen
= dri_screen(sPriv
);
54 struct st_api
*stapi
= screen
->st_api
;
55 struct dri_context
*ctx
= NULL
;
56 struct st_context_iface
*st_share
= NULL
;
57 struct st_context_attribs attribs
;
58 enum st_context_error ctx_err
= 0;
59 unsigned allowed_flags
= __DRI_CTX_FLAG_DEBUG
|
60 __DRI_CTX_FLAG_FORWARD_COMPATIBLE
;
62 if (screen
->has_reset_status_query
)
63 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
65 if (flags
& ~allowed_flags
) {
66 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
70 if (!screen
->has_reset_status_query
&& notify_reset
) {
71 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
75 memset(&attribs
, 0, sizeof(attribs
));
78 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
81 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
83 case API_OPENGL_COMPAT
:
85 attribs
.profile
= api
== API_OPENGL_COMPAT
? ST_PROFILE_DEFAULT
86 : ST_PROFILE_OPENGL_CORE
;
87 attribs
.major
= major_version
;
88 attribs
.minor
= minor_version
;
90 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
91 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
94 *error
= __DRI_CTX_ERROR_BAD_API
;
98 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0)
99 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
101 if (flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
)
102 attribs
.flags
|= ST_CONTEXT_FLAG_ROBUST_ACCESS
;
105 attribs
.flags
|= ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
;
107 if (sharedContextPrivate
) {
108 st_share
= ((struct dri_context
*)sharedContextPrivate
)->st
;
111 ctx
= CALLOC_STRUCT(dri_context
);
113 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
117 cPriv
->driverPrivate
= ctx
;
121 attribs
.options
= screen
->options
;
122 dri_fill_st_visual(&attribs
.visual
, screen
, visual
);
123 ctx
->st
= stapi
->create_context(stapi
, &screen
->base
, &attribs
, &ctx_err
,
125 if (ctx
->st
== NULL
) {
127 case ST_CONTEXT_SUCCESS
:
128 *error
= __DRI_CTX_ERROR_SUCCESS
;
130 case ST_CONTEXT_ERROR_NO_MEMORY
:
131 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
133 case ST_CONTEXT_ERROR_BAD_API
:
134 *error
= __DRI_CTX_ERROR_BAD_API
;
136 case ST_CONTEXT_ERROR_BAD_VERSION
:
137 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
139 case ST_CONTEXT_ERROR_BAD_FLAG
:
140 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
142 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
143 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
145 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
146 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
151 ctx
->st
->st_manager_private
= (void *) ctx
;
154 if (ctx
->st
->cso_context
) {
155 ctx
->pp
= pp_init(ctx
->st
->pipe
, screen
->pp_enabled
, ctx
->st
->cso_context
);
156 ctx
->hud
= hud_create(ctx
->st
->pipe
, ctx
->st
->cso_context
);
159 *error
= __DRI_CTX_ERROR_SUCCESS
;
164 ctx
->st
->destroy(ctx
->st
);
171 dri_destroy_context(__DRIcontext
* cPriv
)
173 struct dri_context
*ctx
= dri_context(cPriv
);
176 hud_destroy(ctx
->hud
);
182 /* No particular reason to wait for command completion before
183 * destroying a context, but we flush the context here
184 * to avoid having to add code elsewhere to cope with flushing a
185 * partially destroyed context.
187 ctx
->st
->flush(ctx
->st
, 0, NULL
);
188 ctx
->st
->destroy(ctx
->st
);
193 dri_unbind_context(__DRIcontext
* cPriv
)
195 /* dri_util.c ensures cPriv is not null */
196 struct dri_screen
*screen
= dri_screen(cPriv
->driScreenPriv
);
197 struct dri_context
*ctx
= dri_context(cPriv
);
198 struct st_api
*stapi
= screen
->st_api
;
200 if (--ctx
->bind_count
== 0) {
201 if (ctx
->st
== ctx
->stapi
->get_current(ctx
->stapi
)) {
202 /* For conformance, unbind is supposed to flush the context.
203 * However, if we do it here we might end up flushing a partially
204 * destroyed context. Instead, we flush in dri_make_current and
205 * in dri_destroy_context which should cover all the cases.
207 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
215 dri_make_current(__DRIcontext
* cPriv
,
216 __DRIdrawable
* driDrawPriv
,
217 __DRIdrawable
* driReadPriv
)
219 /* dri_util.c ensures cPriv is not null */
220 struct dri_context
*ctx
= dri_context(cPriv
);
221 struct dri_drawable
*draw
= dri_drawable(driDrawPriv
);
222 struct dri_drawable
*read
= dri_drawable(driReadPriv
);
223 struct st_context_iface
*old_st
= ctx
->stapi
->get_current(ctx
->stapi
);
225 /* Flush the old context here so we don't have to flush on unbind() */
226 if (old_st
&& old_st
!= ctx
->st
)
227 old_st
->flush(old_st
, ST_FLUSH_FRONT
, NULL
);
232 return ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, NULL
, NULL
);
233 else if (!draw
|| !read
)
236 if (ctx
->dPriv
!= driDrawPriv
) {
237 ctx
->dPriv
= driDrawPriv
;
238 draw
->texture_stamp
= driDrawPriv
->lastStamp
- 1;
240 if (ctx
->rPriv
!= driReadPriv
) {
241 ctx
->rPriv
= driReadPriv
;
242 read
->texture_stamp
= driReadPriv
->lastStamp
- 1;
245 ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, &draw
->base
, &read
->base
);
247 // This is ok to call here. If they are already init, it's a no-op.
248 if (draw
->textures
[ST_ATTACHMENT_BACK_LEFT
] && draw
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]
250 pp_init_fbos(ctx
->pp
, draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->width0
,
251 draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->height0
);
257 dri_get_current(__DRIscreen
*sPriv
)
259 struct dri_screen
*screen
= dri_screen(sPriv
);
260 struct st_api
*stapi
= screen
->st_api
;
261 struct st_context_iface
*st
;
263 st
= stapi
->get_current(stapi
);
265 return (struct dri_context
*) st
? st
->st_manager_private
: NULL
;
268 /* vim: set sw=3 ts=8 sts=3 expandtab: */