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