Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**********************************************************
  2.  * Copyright 2010 VMware, Inc.  All rights reserved.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person
  5.  * obtaining a copy of this software and associated documentation
  6.  * files (the "Software"), to deal in the Software without
  7.  * restriction, including without limitation the rights to use, copy,
  8.  * modify, merge, publish, distribute, sublicense, and/or sell copies
  9.  * of the Software, and to permit persons to whom the Software is
  10.  * furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice shall be
  13.  * included in all copies or substantial portions of the Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19.  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20.  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  *
  24.  **********************************************************/
  25.  
  26.  
  27. #include "wrapper_sw_winsys.h"
  28.  
  29. #include "pipe/p_format.h"
  30. #include "pipe/p_state.h"
  31.  
  32. #include "state_tracker/sw_winsys.h"
  33.  
  34. #include "util/u_memory.h"
  35. #include "util/u_inlines.h"
  36.  
  37. /*
  38.  * This code wraps a pipe_screen and exposes a sw_winsys interface for use
  39.  * with software resterizers. This code is used by the DRM based winsys to
  40.  * allow access to the drm driver.
  41.  *
  42.  * We must borrow the whole stack because only the pipe screen knows how
  43.  * to decode the content of a buffer. Or how to create a buffer that
  44.  * can still be used by drivers using real hardware (as the case is
  45.  * with software st/xorg but hw st/dri).
  46.  *
  47.  * We also need a pipe context for the transfers.
  48.  */
  49.  
  50. struct wrapper_sw_winsys
  51. {
  52.    struct sw_winsys base;
  53.    struct pipe_screen *screen;
  54.    struct pipe_context *pipe;
  55.    enum pipe_texture_target target;
  56. };
  57.  
  58. struct wrapper_sw_displaytarget
  59. {
  60.    struct wrapper_sw_winsys *winsys;
  61.    struct pipe_resource *tex;
  62.    struct pipe_transfer *transfer;
  63.  
  64.    unsigned map_count;
  65.    unsigned stride; /**< because we get stride at create */
  66.    void *ptr;
  67. };
  68.  
  69. static INLINE struct wrapper_sw_winsys *
  70. wrapper_sw_winsys(struct sw_winsys *ws)
  71. {
  72.    return (struct wrapper_sw_winsys *)ws;
  73. }
  74.  
  75. static INLINE struct wrapper_sw_displaytarget *
  76. wrapper_sw_displaytarget(struct sw_displaytarget *dt)
  77. {
  78.    return (struct wrapper_sw_displaytarget *)dt;
  79. }
  80.  
  81.  
  82. /*
  83.  * Functions
  84.  */
  85.  
  86.  
  87. static boolean
  88. wsw_is_dt_format_supported(struct sw_winsys *ws,
  89.                            unsigned tex_usage,
  90.                            enum pipe_format format)
  91. {
  92.    struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
  93.  
  94.    return wsw->screen->is_format_supported(wsw->screen, format,
  95.                                            PIPE_TEXTURE_2D, 0,
  96.                                            PIPE_BIND_RENDER_TARGET |
  97.                                            PIPE_BIND_DISPLAY_TARGET);
  98. }
  99.  
  100. static boolean
  101. wsw_dt_get_stride(struct wrapper_sw_displaytarget *wdt, unsigned *stride)
  102. {
  103.    struct pipe_context *pipe = wdt->winsys->pipe;
  104.    struct pipe_resource *tex = wdt->tex;
  105.    struct pipe_transfer *tr;
  106.    void *map;
  107.  
  108.    map = pipe_transfer_map(pipe, tex, 0, 0,
  109.                            PIPE_TRANSFER_READ_WRITE,
  110.                            0, 0, wdt->tex->width0, wdt->tex->height0, &tr);
  111.    if (!map)
  112.       return FALSE;
  113.  
  114.    *stride = tr->stride;
  115.    wdt->stride = tr->stride;
  116.  
  117.    pipe->transfer_unmap(pipe, tr);
  118.  
  119.    return TRUE;
  120. }
  121.  
  122. static struct sw_displaytarget *
  123. wsw_dt_wrap_texture(struct wrapper_sw_winsys *wsw,
  124.                     struct pipe_resource *tex, unsigned *stride)
  125. {
  126.    struct wrapper_sw_displaytarget *wdt = CALLOC_STRUCT(wrapper_sw_displaytarget);
  127.    if (!wdt)
  128.       goto err_unref;
  129.  
  130.    wdt->tex = tex;
  131.    wdt->winsys = wsw;
  132.  
  133.    if (!wsw_dt_get_stride(wdt, stride))
  134.       goto err_free;
  135.  
  136.    return (struct sw_displaytarget *)wdt;
  137.  
  138. err_free:
  139.    FREE(wdt);
  140. err_unref:
  141.    pipe_resource_reference(&tex, NULL);
  142.    return NULL;
  143. }
  144.  
  145. static struct sw_displaytarget *
  146. wsw_dt_create(struct sw_winsys *ws,
  147.               unsigned bind,
  148.               enum pipe_format format,
  149.               unsigned width, unsigned height,
  150.               unsigned alignment,
  151.               unsigned *stride)
  152. {
  153.    struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
  154.    struct pipe_resource templ;
  155.    struct pipe_resource *tex;
  156.  
  157.    /*
  158.     * XXX Why don't we just get the template.
  159.     */
  160.    memset(&templ, 0, sizeof(templ));
  161.    templ.target = wsw->target;
  162.    templ.width0 = width;
  163.    templ.height0 = height;
  164.    templ.depth0 = 1;
  165.    templ.array_size = 1;
  166.    templ.format = format;
  167.    templ.bind = bind;
  168.  
  169.    /* XXX alignment: we can't do anything about this */
  170.  
  171.    tex = wsw->screen->resource_create(wsw->screen, &templ);
  172.    if (!tex)
  173.       return NULL;
  174.  
  175.    return wsw_dt_wrap_texture(wsw, tex, stride);
  176. }
  177.  
  178. static struct sw_displaytarget *
  179. wsw_dt_from_handle(struct sw_winsys *ws,
  180.                    const struct pipe_resource *templ,
  181.                    struct winsys_handle *whandle,
  182.                    unsigned *stride)
  183. {
  184.    struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
  185.    struct pipe_resource *tex;
  186.  
  187.    tex = wsw->screen->resource_from_handle(wsw->screen, templ, whandle);
  188.    if (!tex)
  189.       return NULL;
  190.  
  191.    return wsw_dt_wrap_texture(wsw, tex, stride);
  192. }
  193.  
  194. static boolean
  195. wsw_dt_get_handle(struct sw_winsys *ws,
  196.                   struct sw_displaytarget *dt,
  197.                   struct winsys_handle *whandle)
  198. {
  199.    struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
  200.    struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
  201.    struct pipe_resource *tex = wdt->tex;
  202.  
  203.    return wsw->screen->resource_get_handle(wsw->screen, tex, whandle);
  204. }
  205.  
  206. static void *
  207. wsw_dt_map(struct sw_winsys *ws,
  208.            struct sw_displaytarget *dt,
  209.            unsigned flags)
  210. {
  211.    struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
  212.    struct pipe_context *pipe = wdt->winsys->pipe;
  213.    struct pipe_resource *tex = wdt->tex;
  214.    struct pipe_transfer *tr;
  215.    void *ptr;
  216.  
  217.    if (!wdt->map_count) {
  218.  
  219.       assert(!wdt->transfer);
  220.  
  221.       ptr = pipe_transfer_map(pipe, tex, 0, 0,
  222.                               PIPE_TRANSFER_READ_WRITE,
  223.                               0, 0, wdt->tex->width0, wdt->tex->height0, &tr);
  224.       if (!ptr)
  225.         goto err;
  226.  
  227.       wdt->transfer = tr;
  228.       wdt->ptr = ptr;
  229.  
  230.       /* XXX Handle this case */
  231.       assert(tr->stride == wdt->stride);
  232.    }
  233.  
  234.    wdt->map_count++;
  235.  
  236.    return wdt->ptr;
  237.  
  238. err:
  239.    pipe->transfer_unmap(pipe, tr);
  240.    return NULL;
  241. }
  242.  
  243. static void
  244. wsw_dt_unmap(struct sw_winsys *ws,
  245.              struct sw_displaytarget *dt)
  246. {
  247.    struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
  248.    struct pipe_context *pipe = wdt->winsys->pipe;
  249.  
  250.    assert(wdt->transfer);
  251.  
  252.    wdt->map_count--;
  253.  
  254.    if (wdt->map_count)
  255.       return;
  256.  
  257.    pipe->transfer_unmap(pipe, wdt->transfer);
  258.    pipe->flush(pipe, NULL, 0);
  259.    wdt->transfer = NULL;
  260. }
  261.  
  262. static void
  263. wsw_dt_destroy(struct sw_winsys *ws,
  264.                struct sw_displaytarget *dt)
  265. {
  266.    struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
  267.  
  268.    pipe_resource_reference(&wdt->tex, NULL);
  269.  
  270.    FREE(wdt);
  271. }
  272.  
  273. static void
  274. wsw_destroy(struct sw_winsys *ws)
  275. {
  276.    struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
  277.  
  278.    wsw->pipe->destroy(wsw->pipe);
  279.    wsw->screen->destroy(wsw->screen);
  280.  
  281.    FREE(wsw);
  282. }
  283.  
  284. struct sw_winsys *
  285. wrapper_sw_winsys_wrap_pipe_screen(struct pipe_screen *screen)
  286. {
  287.    struct wrapper_sw_winsys *wsw = CALLOC_STRUCT(wrapper_sw_winsys);
  288.  
  289.    if (!wsw)
  290.       goto err;
  291.  
  292.    wsw->base.is_displaytarget_format_supported = wsw_is_dt_format_supported;
  293.    wsw->base.displaytarget_create = wsw_dt_create;
  294.    wsw->base.displaytarget_from_handle = wsw_dt_from_handle;
  295.    wsw->base.displaytarget_get_handle = wsw_dt_get_handle;
  296.    wsw->base.displaytarget_map = wsw_dt_map;
  297.    wsw->base.displaytarget_unmap = wsw_dt_unmap;
  298.    wsw->base.displaytarget_destroy = wsw_dt_destroy;
  299.    wsw->base.destroy = wsw_destroy;
  300.  
  301.    wsw->screen = screen;
  302.    wsw->pipe = screen->context_create(screen, NULL);
  303.    if (!wsw->pipe)
  304.       goto err_free;
  305.  
  306.    if(screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES))
  307.       wsw->target = PIPE_TEXTURE_2D;
  308.    else
  309.       wsw->target = PIPE_TEXTURE_RECT;
  310.  
  311.    return &wsw->base;
  312.  
  313. err_free:
  314.    FREE(wsw);
  315. err:
  316.    return NULL;
  317. }
  318.  
  319. struct pipe_screen *
  320. wrapper_sw_winsys_dewrap_pipe_screen(struct sw_winsys *ws)
  321. {
  322.    struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
  323.    struct pipe_screen *screen = wsw->screen;
  324.  
  325.    wsw->pipe->destroy(wsw->pipe);
  326.    /* don't destroy the screen its needed later on */
  327.  
  328.    FREE(wsw);
  329.    return screen;
  330. }
  331.