dri_context.c revision e532b6288f01b63d8d8ba8c8dc08292967e65490
1/**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
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 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.
25 *
26 **************************************************************************/
27/*
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30 */
31
32#include "utils.h"
33
34#include "dri_screen.h"
35#include "dri_drawable.h"
36#include "dri_context.h"
37
38#include "pipe/p_context.h"
39#include "state_tracker/st_context.h"
40
41static void
42dri_pp_query(struct dri_context *ctx)
43{
44   unsigned int i;
45
46   for (i = 0; i < PP_FILTERS; i++) {
47      ctx->pp_enabled[i] = driQueryOptioni(&ctx->optionCache, pp_filters[i].name);
48   }
49}
50
51GLboolean
52dri_create_context(gl_api api, const struct gl_config * visual,
53		   __DRIcontext * cPriv,
54		   unsigned major_version,
55		   unsigned minor_version,
56		   uint32_t flags,
57		   unsigned *error,
58		   void *sharedContextPrivate)
59{
60   __DRIscreen *sPriv = cPriv->driScreenPriv;
61   struct dri_screen *screen = dri_screen(sPriv);
62   struct st_api *stapi = screen->st_api;
63   struct dri_context *ctx = NULL;
64   struct st_context_iface *st_share = NULL;
65   struct st_context_attribs attribs;
66   enum st_context_error ctx_err = 0;
67
68   memset(&attribs, 0, sizeof(attribs));
69   switch (api) {
70   case API_OPENGLES:
71      attribs.profile = ST_PROFILE_OPENGL_ES1;
72      break;
73   case API_OPENGLES2:
74      attribs.profile = ST_PROFILE_OPENGL_ES2;
75      break;
76   case API_OPENGL:
77      attribs.profile = ST_PROFILE_DEFAULT;
78      attribs.major = major_version;
79      attribs.minor = minor_version;
80
81      if ((flags & __DRI_CTX_FLAG_DEBUG) != 0)
82	 attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
83
84      if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
85	 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
86      break;
87   default:
88      *error = __DRI_CTX_ERROR_BAD_API;
89      goto fail;
90   }
91
92   if (sharedContextPrivate) {
93      st_share = ((struct dri_context *)sharedContextPrivate)->st;
94   }
95
96   ctx = CALLOC_STRUCT(dri_context);
97   if (ctx == NULL) {
98      *error = __DRI_CTX_ERROR_NO_MEMORY;
99      goto fail;
100   }
101
102   cPriv->driverPrivate = ctx;
103   ctx->cPriv = cPriv;
104   ctx->sPriv = sPriv;
105
106   driParseConfigFiles(&ctx->optionCache,
107		       &screen->optionCache, sPriv->myNum, "dri");
108
109   dri_fill_st_visual(&attribs.visual, screen, visual);
110   ctx->st = stapi->create_context(stapi, &screen->base, &attribs, &ctx_err,
111				   st_share);
112   if (ctx->st == NULL) {
113      switch (ctx_err) {
114      case ST_CONTEXT_SUCCESS:
115	 *error = __DRI_CTX_ERROR_SUCCESS;
116	 break;
117      case ST_CONTEXT_ERROR_NO_MEMORY:
118	 *error = __DRI_CTX_ERROR_NO_MEMORY;
119	 break;
120      case ST_CONTEXT_ERROR_BAD_API:
121	 *error = __DRI_CTX_ERROR_BAD_API;
122	 break;
123      case ST_CONTEXT_ERROR_BAD_VERSION:
124	 *error = __DRI_CTX_ERROR_BAD_VERSION;
125	 break;
126      case ST_CONTEXT_ERROR_BAD_FLAG:
127	 *error = __DRI_CTX_ERROR_BAD_FLAG;
128	 break;
129      case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE:
130	 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
131	 break;
132      case ST_CONTEXT_ERROR_UNKNOWN_FLAG:
133	 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
134	 break;
135      }
136      goto fail;
137   }
138   ctx->st->st_manager_private = (void *) ctx;
139   ctx->stapi = stapi;
140
141   // Context successfully created. See if post-processing is requested.
142   dri_pp_query(ctx);
143
144   ctx->pp = pp_init(screen->base.screen, ctx->pp_enabled);
145
146   *error = __DRI_CTX_ERROR_SUCCESS;
147   return GL_TRUE;
148
149 fail:
150   if (ctx && ctx->st)
151      ctx->st->destroy(ctx->st);
152
153   FREE(ctx);
154   return GL_FALSE;
155}
156
157void
158dri_destroy_context(__DRIcontext * cPriv)
159{
160   struct dri_context *ctx = dri_context(cPriv);
161
162   /* note: we are freeing values and nothing more because
163    * driParseConfigFiles allocated values only - the rest
164    * is owned by screen optionCache.
165    */
166   FREE(ctx->optionCache.values);
167
168   /* No particular reason to wait for command completion before
169    * destroying a context, but it is probably worthwhile flushing it
170    * to avoid having to add code elsewhere to cope with flushing a
171    * partially destroyed context.
172    */
173   ctx->st->flush(ctx->st, 0, NULL);
174   ctx->st->destroy(ctx->st);
175
176   if (ctx->pp) pp_free(ctx->pp);
177
178   FREE(ctx);
179}
180
181GLboolean
182dri_unbind_context(__DRIcontext * cPriv)
183{
184   /* dri_util.c ensures cPriv is not null */
185   struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
186   struct dri_context *ctx = dri_context(cPriv);
187   struct st_api *stapi = screen->st_api;
188
189   if (--ctx->bind_count == 0) {
190      if (ctx->st == ctx->stapi->get_current(ctx->stapi)) {
191         ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL);
192         stapi->make_current(stapi, NULL, NULL, NULL);
193      }
194   }
195
196   return GL_TRUE;
197}
198
199GLboolean
200dri_make_current(__DRIcontext * cPriv,
201		 __DRIdrawable * driDrawPriv,
202		 __DRIdrawable * driReadPriv)
203{
204   /* dri_util.c ensures cPriv is not null */
205   struct dri_context *ctx = dri_context(cPriv);
206   struct dri_drawable *draw = dri_drawable(driDrawPriv);
207   struct dri_drawable *read = dri_drawable(driReadPriv);
208   struct st_context_iface *old_st = ctx->stapi->get_current(ctx->stapi);
209
210   if (old_st && old_st != ctx->st)
211      old_st->flush(old_st, ST_FLUSH_FRONT, NULL);
212
213   ++ctx->bind_count;
214
215   if (!driDrawPriv && !driReadPriv)
216      return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL);
217   else if (!driDrawPriv || !driReadPriv)
218      return GL_FALSE;
219
220   if (ctx->dPriv != driDrawPriv) {
221      ctx->dPriv = driDrawPriv;
222      draw->texture_stamp = driDrawPriv->lastStamp - 1;
223   }
224   if (ctx->rPriv != driReadPriv) {
225      ctx->rPriv = driReadPriv;
226      read->texture_stamp = driReadPriv->lastStamp - 1;
227   }
228
229   ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
230
231   // This is ok to call here. If they are already init, it's a no-op.
232   if (draw->textures[ST_ATTACHMENT_BACK_LEFT] && draw->textures[ST_ATTACHMENT_DEPTH_STENCIL]
233      && ctx->pp)
234         pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
235            draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0,
236            draw->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
237
238   return GL_TRUE;
239}
240
241struct dri_context *
242dri_get_current(__DRIscreen *sPriv)
243{
244   struct dri_screen *screen = dri_screen(sPriv);
245   struct st_api *stapi = screen->st_api;
246   struct st_context_iface *st;
247
248   st = stapi->get_current(stapi);
249
250   return (struct dri_context *) (st) ? st->st_manager_private : NULL;
251}
252
253/* vim: set sw=3 ts=8 sts=3 expandtab: */
254