Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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 "state_tracker/drm_driver.h"
  38.  
  39. #include "pipe/p_context.h"
  40. #include "state_tracker/st_context.h"
  41.  
  42. static void
  43. dri_pp_query(struct dri_context *ctx)
  44. {
  45.    unsigned int i;
  46.  
  47.    for (i = 0; i < PP_FILTERS; i++) {
  48.       ctx->pp_enabled[i] = driQueryOptioni(&ctx->optionCache, pp_filters[i].name);
  49.    }
  50. }
  51.  
  52. static void dri_fill_st_options(struct st_config_options *options,
  53.                                 const struct driOptionCache * optionCache)
  54. {
  55.    options->disable_blend_func_extended =
  56.       driQueryOptionb(optionCache, "disable_blend_func_extended");
  57.    options->disable_glsl_line_continuations =
  58.       driQueryOptionb(optionCache, "disable_glsl_line_continuations");
  59.    options->disable_shader_bit_encoding =
  60.       driQueryOptionb(optionCache, "disable_shader_bit_encoding");
  61.    options->force_glsl_extensions_warn =
  62.       driQueryOptionb(optionCache, "force_glsl_extensions_warn");
  63.    options->force_glsl_version =
  64.       driQueryOptioni(optionCache, "force_glsl_version");
  65.    options->force_s3tc_enable =
  66.       driQueryOptionb(optionCache, "force_s3tc_enable");
  67. }
  68.  
  69. GLboolean
  70. dri_create_context(gl_api api, const struct gl_config * visual,
  71.                    __DRIcontext * cPriv,
  72.                    unsigned major_version,
  73.                    unsigned minor_version,
  74.                    uint32_t flags,
  75.                    unsigned *error,
  76.                    void *sharedContextPrivate)
  77. {
  78.    __DRIscreen *sPriv = cPriv->driScreenPriv;
  79.    struct dri_screen *screen = dri_screen(sPriv);
  80.    struct st_api *stapi = screen->st_api;
  81.    struct dri_context *ctx = NULL;
  82.    struct st_context_iface *st_share = NULL;
  83.    struct st_context_attribs attribs;
  84.    enum st_context_error ctx_err = 0;
  85.  
  86.    memset(&attribs, 0, sizeof(attribs));
  87.    switch (api) {
  88.    case API_OPENGLES:
  89.       attribs.profile = ST_PROFILE_OPENGL_ES1;
  90.       break;
  91.    case API_OPENGLES2:
  92.       attribs.profile = ST_PROFILE_OPENGL_ES2;
  93.       break;
  94.    case API_OPENGL_COMPAT:
  95.    case API_OPENGL_CORE:
  96.       attribs.profile = api == API_OPENGL_COMPAT ? ST_PROFILE_DEFAULT
  97.                                                  : ST_PROFILE_OPENGL_CORE;
  98.       attribs.major = major_version;
  99.       attribs.minor = minor_version;
  100.  
  101.       if ((flags & __DRI_CTX_FLAG_DEBUG) != 0)
  102.          attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
  103.  
  104.       if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
  105.          attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
  106.       break;
  107.    default:
  108.       *error = __DRI_CTX_ERROR_BAD_API;
  109.       goto fail;
  110.    }
  111.  
  112.    if (sharedContextPrivate) {
  113.       st_share = ((struct dri_context *)sharedContextPrivate)->st;
  114.    }
  115.  
  116.    ctx = CALLOC_STRUCT(dri_context);
  117.    if (ctx == NULL) {
  118.       *error = __DRI_CTX_ERROR_NO_MEMORY;
  119.       goto fail;
  120.    }
  121.  
  122.    cPriv->driverPrivate = ctx;
  123.    ctx->cPriv = cPriv;
  124.    ctx->sPriv = sPriv;
  125.  
  126.    driParseConfigFiles(&ctx->optionCache,
  127.                        &screen->optionCacheDefaults,
  128.                        sPriv->myNum, driver_descriptor.name);
  129.  
  130.    dri_fill_st_options(&attribs.options, &ctx->optionCache);
  131.    dri_fill_st_visual(&attribs.visual, screen, visual);
  132.    ctx->st = stapi->create_context(stapi, &screen->base, &attribs, &ctx_err,
  133.                                    st_share);
  134.    if (ctx->st == NULL) {
  135.       switch (ctx_err) {
  136.       case ST_CONTEXT_SUCCESS:
  137.          *error = __DRI_CTX_ERROR_SUCCESS;
  138.          break;
  139.       case ST_CONTEXT_ERROR_NO_MEMORY:
  140.          *error = __DRI_CTX_ERROR_NO_MEMORY;
  141.          break;
  142.       case ST_CONTEXT_ERROR_BAD_API:
  143.          *error = __DRI_CTX_ERROR_BAD_API;
  144.          break;
  145.       case ST_CONTEXT_ERROR_BAD_VERSION:
  146.          *error = __DRI_CTX_ERROR_BAD_VERSION;
  147.          break;
  148.       case ST_CONTEXT_ERROR_BAD_FLAG:
  149.          *error = __DRI_CTX_ERROR_BAD_FLAG;
  150.          break;
  151.       case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE:
  152.          *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
  153.          break;
  154.       case ST_CONTEXT_ERROR_UNKNOWN_FLAG:
  155.          *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
  156.          break;
  157.       }
  158.       goto fail;
  159.    }
  160.    ctx->st->st_manager_private = (void *) ctx;
  161.    ctx->stapi = stapi;
  162.  
  163.    // Context successfully created. See if post-processing is requested.
  164.    dri_pp_query(ctx);
  165.  
  166.    if (ctx->st->cso_context) {
  167.       ctx->pp = pp_init(ctx->st->pipe, ctx->pp_enabled, ctx->st->cso_context);
  168.       ctx->hud = hud_create(ctx->st->pipe, ctx->st->cso_context);
  169.    }
  170.  
  171.    *error = __DRI_CTX_ERROR_SUCCESS;
  172.    return GL_TRUE;
  173.  
  174.  fail:
  175.    if (ctx && ctx->st)
  176.       ctx->st->destroy(ctx->st);
  177.  
  178.    free(ctx);
  179.    return GL_FALSE;
  180. }
  181.  
  182. void
  183. dri_destroy_context(__DRIcontext * cPriv)
  184. {
  185.    struct dri_context *ctx = dri_context(cPriv);
  186.  
  187.    if (ctx->hud) {
  188.       hud_destroy(ctx->hud);
  189.    }
  190.  
  191.    /* note: we are freeing values and nothing more because
  192.     * driParseConfigFiles allocated values only - the rest
  193.     * is owned by screen optionCacheDefaults.
  194.     */
  195.    free(ctx->optionCache.values);
  196.  
  197.    /* No particular reason to wait for command completion before
  198.     * destroying a context, but we flush the context here
  199.     * to avoid having to add code elsewhere to cope with flushing a
  200.     * partially destroyed context.
  201.     */
  202.    ctx->st->flush(ctx->st, 0, NULL);
  203.    ctx->st->destroy(ctx->st);
  204.  
  205.    if (ctx->pp) pp_free(ctx->pp);
  206.  
  207.    free(ctx);
  208. }
  209.  
  210. GLboolean
  211. dri_unbind_context(__DRIcontext * cPriv)
  212. {
  213.    /* dri_util.c ensures cPriv is not null */
  214.    struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
  215.    struct dri_context *ctx = dri_context(cPriv);
  216.    struct st_api *stapi = screen->st_api;
  217.  
  218.    if (--ctx->bind_count == 0) {
  219.       if (ctx->st == ctx->stapi->get_current(ctx->stapi)) {
  220.          /* For conformance, unbind is supposed to flush the context.
  221.           * However, if we do it here we might end up flushing a partially
  222.           * destroyed context. Instead, we flush in dri_make_current and
  223.           * in dri_destroy_context which should cover all the cases.
  224.           */
  225.          stapi->make_current(stapi, NULL, NULL, NULL);
  226.       }
  227.    }
  228.  
  229.    return GL_TRUE;
  230. }
  231.  
  232. GLboolean
  233. dri_make_current(__DRIcontext * cPriv,
  234.                  __DRIdrawable * driDrawPriv,
  235.                  __DRIdrawable * driReadPriv)
  236. {
  237.    /* dri_util.c ensures cPriv is not null */
  238.    struct dri_context *ctx = dri_context(cPriv);
  239.    struct dri_drawable *draw = dri_drawable(driDrawPriv);
  240.    struct dri_drawable *read = dri_drawable(driReadPriv);
  241.    struct st_context_iface *old_st = ctx->stapi->get_current(ctx->stapi);
  242.  
  243.    /* Flush the old context here so we don't have to flush on unbind() */
  244.    if (old_st && old_st != ctx->st)
  245.       old_st->flush(old_st, ST_FLUSH_FRONT, NULL);
  246.  
  247.    ++ctx->bind_count;
  248.  
  249.    if (!driDrawPriv && !driReadPriv)
  250.       return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL);
  251.    else if (!driDrawPriv || !driReadPriv)
  252.       return GL_FALSE;
  253.  
  254.    if (ctx->dPriv != driDrawPriv) {
  255.       ctx->dPriv = driDrawPriv;
  256.       draw->texture_stamp = driDrawPriv->lastStamp - 1;
  257.    }
  258.    if (ctx->rPriv != driReadPriv) {
  259.       ctx->rPriv = driReadPriv;
  260.       read->texture_stamp = driReadPriv->lastStamp - 1;
  261.    }
  262.  
  263.    ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
  264.  
  265.    // This is ok to call here. If they are already init, it's a no-op.
  266.    if (draw->textures[ST_ATTACHMENT_BACK_LEFT] && draw->textures[ST_ATTACHMENT_DEPTH_STENCIL]
  267.       && ctx->pp)
  268.          pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
  269.             draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
  270.  
  271.    return GL_TRUE;
  272. }
  273.  
  274. struct dri_context *
  275. dri_get_current(__DRIscreen *sPriv)
  276. {
  277.    struct dri_screen *screen = dri_screen(sPriv);
  278.    struct st_api *stapi = screen->st_api;
  279.    struct st_context_iface *st;
  280.  
  281.    st = stapi->get_current(stapi);
  282.  
  283.    return (struct dri_context *) (st) ? st->st_manager_private : NULL;
  284. }
  285.  
  286. /* vim: set sw=3 ts=8 sts=3 expandtab: */
  287.