1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**************************************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2009, VMware, Inc.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software.
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Author: Keith Whitwell <keithw@vmware.com>
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "utils.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "dri_screen.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "dri_drawable.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "dri_context.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/drm_driver.h"
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_context.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_context.h"
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdri_pp_query(struct dri_context *ctx)
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned int i;
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < PP_FILTERS; i++) {
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ctx->pp_enabled[i] = driQueryOptioni(&ctx->optionCache, pp_filters[i].name);
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void dri_fill_st_options(struct st_config_options *options,
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                const struct driOptionCache * optionCache)
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   options->force_glsl_extensions_warn =
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      driQueryOptionb(optionCache, "force_glsl_extensions_warn");
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGLboolean
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdri_create_context(gl_api api, const struct gl_config * visual,
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		   __DRIcontext * cPriv,
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		   unsigned major_version,
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		   unsigned minor_version,
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		   uint32_t flags,
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		   unsigned *error,
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		   void *sharedContextPrivate)
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   __DRIscreen *sPriv = cPriv->driScreenPriv;
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_screen *screen = dri_screen(sPriv);
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_api *stapi = screen->st_api;
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_context *ctx = NULL;
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context_iface *st_share = NULL;
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context_attribs attribs;
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum st_context_error ctx_err = 0;
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memset(&attribs, 0, sizeof(attribs));
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   switch (api) {
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case API_OPENGLES:
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attribs.profile = ST_PROFILE_OPENGL_ES1;
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case API_OPENGLES2:
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attribs.profile = ST_PROFILE_OPENGL_ES2;
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case API_OPENGL:
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attribs.profile = ST_PROFILE_DEFAULT;
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attribs.major = major_version;
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attribs.minor = minor_version;
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if ((flags & __DRI_CTX_FLAG_DEBUG) != 0)
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   default:
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *error = __DRI_CTX_ERROR_BAD_API;
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto fail;
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (sharedContextPrivate) {
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      st_share = ((struct dri_context *)sharedContextPrivate)->st;
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx = CALLOC_STRUCT(dri_context);
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ctx == NULL) {
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *error = __DRI_CTX_ERROR_NO_MEMORY;
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto fail;
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   cPriv->driverPrivate = ctx;
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->cPriv = cPriv;
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->sPriv = sPriv;
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   driParseConfigFiles(&ctx->optionCache,
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		       &screen->optionCache, sPriv->myNum, driver_descriptor.name);
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   dri_fill_st_options(&attribs.options, &ctx->optionCache);
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   dri_fill_st_visual(&attribs.visual, screen, visual);
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->st = stapi->create_context(stapi, &screen->base, &attribs, &ctx_err,
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				   st_share);
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ctx->st == NULL) {
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      switch (ctx_err) {
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case ST_CONTEXT_SUCCESS:
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 *error = __DRI_CTX_ERROR_SUCCESS;
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case ST_CONTEXT_ERROR_NO_MEMORY:
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 *error = __DRI_CTX_ERROR_NO_MEMORY;
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case ST_CONTEXT_ERROR_BAD_API:
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 *error = __DRI_CTX_ERROR_BAD_API;
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case ST_CONTEXT_ERROR_BAD_VERSION:
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 *error = __DRI_CTX_ERROR_BAD_VERSION;
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case ST_CONTEXT_ERROR_BAD_FLAG:
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 *error = __DRI_CTX_ERROR_BAD_FLAG;
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE:
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case ST_CONTEXT_ERROR_UNKNOWN_FLAG:
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 break;
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto fail;
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->st->st_manager_private = (void *) ctx;
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->stapi = stapi;
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   // Context successfully created. See if post-processing is requested.
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   dri_pp_query(ctx);
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->pp = pp_init(screen->base.screen, ctx->pp_enabled);
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   *error = __DRI_CTX_ERROR_SUCCESS;
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return GL_TRUE;
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fail:
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ctx && ctx->st)
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ctx->st->destroy(ctx->st);
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   FREE(ctx);
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return GL_FALSE;
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdri_destroy_context(__DRIcontext * cPriv)
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_context *ctx = dri_context(cPriv);
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* note: we are freeing values and nothing more because
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * driParseConfigFiles allocated values only - the rest
173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * is owned by screen optionCache.
174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   FREE(ctx->optionCache.values);
176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* No particular reason to wait for command completion before
178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * destroying a context, but we flush the context here
179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * to avoid having to add code elsewhere to cope with flushing a
180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * partially destroyed context.
181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->st->flush(ctx->st, 0, NULL);
183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->st->destroy(ctx->st);
184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ctx->pp) pp_free(ctx->pp);
186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   FREE(ctx);
188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGLboolean
191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdri_unbind_context(__DRIcontext * cPriv)
192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* dri_util.c ensures cPriv is not null */
194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_context *ctx = dri_context(cPriv);
196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_api *stapi = screen->st_api;
197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (--ctx->bind_count == 0) {
199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (ctx->st == ctx->stapi->get_current(ctx->stapi)) {
200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* For conformance, unbind is supposed to flush the context.
201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * However, if we do it here we might end up flushing a partially
202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * destroyed context. Instead, we flush in dri_make_current and
203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * in dri_destroy_context which should cover all the cases.
204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         stapi->make_current(stapi, NULL, NULL, NULL);
206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return GL_TRUE;
210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGLboolean
213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdri_make_current(__DRIcontext * cPriv,
214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		 __DRIdrawable * driDrawPriv,
215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		 __DRIdrawable * driReadPriv)
216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* dri_util.c ensures cPriv is not null */
218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_context *ctx = dri_context(cPriv);
219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_drawable *draw = dri_drawable(driDrawPriv);
220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_drawable *read = dri_drawable(driReadPriv);
221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context_iface *old_st = ctx->stapi->get_current(ctx->stapi);
222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Flush the old context here so we don't have to flush on unbind() */
224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (old_st && old_st != ctx->st)
225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      old_st->flush(old_st, ST_FLUSH_FRONT, NULL);
226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ++ctx->bind_count;
228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!driDrawPriv && !driReadPriv)
230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL);
231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else if (!driDrawPriv || !driReadPriv)
232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return GL_FALSE;
233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ctx->dPriv != driDrawPriv) {
235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ctx->dPriv = driDrawPriv;
236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw->texture_stamp = driDrawPriv->lastStamp - 1;
237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ctx->rPriv != driReadPriv) {
239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ctx->rPriv = driReadPriv;
240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      read->texture_stamp = driReadPriv->lastStamp - 1;
241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   // This is ok to call here. If they are already init, it's a no-op.
246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (draw->textures[ST_ATTACHMENT_BACK_LEFT] && draw->textures[ST_ATTACHMENT_DEPTH_STENCIL]
247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      && ctx->pp)
248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return GL_TRUE;
252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct dri_context *
255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdri_get_current(__DRIscreen *sPriv)
256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct dri_screen *screen = dri_screen(sPriv);
258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_api *stapi = screen->st_api;
259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context_iface *st;
260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   st = stapi->get_current(stapi);
262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return (struct dri_context *) (st) ? st->st_manager_private : NULL;
264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* vim: set sw=3 ts=8 sts=3 expandtab: */
267