dri_context.c revision 8e3b658b7fdc1c2a2b9b6bd942a811adbf1ac4ab
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_init_extensions(struct dri_context *ctx)
43{
44   struct st_context *st = (struct st_context *) ctx->st;
45
46   /* New extensions should be added in mesa/state_tracker/st_extensions.c
47    * and not in this file. */
48   driInitExtensions(st->ctx, NULL, GL_FALSE);
49}
50
51GLboolean
52dri_create_context(gl_api api, const __GLcontextModes * visual,
53		   __DRIcontext * cPriv, void *sharedContextPrivate)
54{
55   __DRIscreen *sPriv = cPriv->driScreenPriv;
56   struct dri_screen *screen = dri_screen(sPriv);
57   struct st_api *stapi;
58   struct dri_context *ctx = NULL;
59   struct st_context_iface *st_share = NULL;
60   struct st_visual stvis;
61
62   assert(api <= API_OPENGLES2);
63   stapi = screen->st_api[api];
64   if (!stapi)
65      return GL_FALSE;
66
67   if (sharedContextPrivate) {
68      st_share = ((struct dri_context *)sharedContextPrivate)->st;
69   }
70
71   ctx = CALLOC_STRUCT(dri_context);
72   if (ctx == NULL)
73      goto fail;
74
75   cPriv->driverPrivate = ctx;
76   ctx->api = api;
77   ctx->cPriv = cPriv;
78   ctx->sPriv = sPriv;
79   ctx->lock = screen->drmLock;
80
81   driParseConfigFiles(&ctx->optionCache,
82		       &screen->optionCache, sPriv->myNum, "dri");
83
84   dri_fill_st_visual(&stvis, screen, visual);
85   ctx->st = stapi->create_context(stapi, &screen->base, &stvis, st_share);
86   if (ctx->st == NULL)
87      goto fail;
88   ctx->st->st_manager_private = (void *) ctx;
89
90   dri_init_extensions(ctx);
91
92   return GL_TRUE;
93
94 fail:
95   if (ctx && ctx->st)
96      ctx->st->destroy(ctx->st);
97
98   FREE(ctx);
99   return GL_FALSE;
100}
101
102void
103dri_destroy_context(__DRIcontext * cPriv)
104{
105   struct dri_context *ctx = dri_context(cPriv);
106
107   /* note: we are freeing values and nothing more because
108    * driParseConfigFiles allocated values only - the rest
109    * is owned by screen optionCache.
110    */
111   FREE(ctx->optionCache.values);
112
113   /* No particular reason to wait for command completion before
114    * destroying a context, but it is probably worthwhile flushing it
115    * to avoid having to add code elsewhere to cope with flushing a
116    * partially destroyed context.
117    */
118   ctx->st->flush(ctx->st, 0, NULL);
119   ctx->st->destroy(ctx->st);
120
121   FREE(ctx);
122}
123
124GLboolean
125dri_unbind_context(__DRIcontext * cPriv)
126{
127   /* dri_util.c ensures cPriv is not null */
128   struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
129   struct dri_context *ctx = dri_context(cPriv);
130   struct st_api *stapi = screen->st_api[ctx->api];
131
132   if (--ctx->bind_count == 0) {
133      if (ctx->st == stapi->get_current(stapi)) {
134         ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
135         stapi->make_current(stapi, NULL, NULL, NULL);
136      }
137   }
138
139   return GL_TRUE;
140}
141
142GLboolean
143dri_make_current(__DRIcontext * cPriv,
144		 __DRIdrawable * driDrawPriv,
145		 __DRIdrawable * driReadPriv)
146{
147   /* dri_util.c ensures cPriv is not null */
148   struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
149   struct dri_context *ctx = dri_context(cPriv);
150   struct st_api *stapi = screen->st_api[ctx->api];
151   struct dri_drawable *draw = dri_drawable(driDrawPriv);
152   struct dri_drawable *read = dri_drawable(driReadPriv);
153   struct st_context_iface *old_st = stapi->get_current(stapi);
154
155   if (old_st && old_st != ctx->st)
156      old_st->flush(old_st, PIPE_FLUSH_RENDER_CACHE, NULL);
157
158   ++ctx->bind_count;
159
160   if (ctx->dPriv != driDrawPriv) {
161      ctx->dPriv = driDrawPriv;
162      draw->texture_stamp = driDrawPriv->lastStamp - 1;
163   }
164   if (ctx->rPriv != driReadPriv) {
165      ctx->rPriv = driReadPriv;
166      read->texture_stamp = driReadPriv->lastStamp - 1;
167   }
168
169   stapi->make_current(stapi, ctx->st, &draw->base, &read->base);
170
171   return GL_TRUE;
172}
173
174struct dri_context *
175dri_get_current(__DRIscreen *sPriv)
176{
177   struct dri_screen *screen = dri_screen(sPriv);
178   struct st_api *stapi;
179   struct st_context_iface *st = NULL;
180   gl_api api;
181
182   /* XXX: How do we do this when the screen supports
183      multiple rendering API's? Pick the first one,
184      like this? (NB: all three API's use the same
185      implementation of get_current (see st_manager.c),
186      so maybe it doesn't matter right now since
187      they'll all return the same result.) */
188   for (api = API_OPENGL; api <= API_OPENGLES2; ++api) {
189      stapi = screen->st_api[api];
190      if (!stapi)
191         continue;
192      st = stapi->get_current(stapi);
193      if (st)
194         break;
195   }
196
197   return (struct dri_context *) (st) ? st->st_manager_private : NULL;
198}
199
200/* vim: set sw=3 ts=8 sts=3 expandtab: */
201