1fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com/**********************************************************
2fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * Copyright 2010 VMware, Inc.  All rights reserved.
3fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com *
4fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * Permission is hereby granted, free of charge, to any person
5fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * obtaining a copy of this software and associated documentation
6fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * files (the "Software"), to deal in the Software without
7b45a1b46ee25e9b19800b028bb1ca925212ac7b4caryclark@google.com * restriction, including without limitation the rights to use, copy,
8fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * modify, merge, publish, distribute, sublicense, and/or sell copies
9fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * of the Software, and to permit persons to whom the Software is
10fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * furnished to do so, subject to the following conditions:
11fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com *
1215fa138f2276a77679530fb608463ff5b4133f7bcaryclark@google.com * The above copyright notice and this permission notice shall be
1315fa138f2276a77679530fb608463ff5b4133f7bcaryclark@google.com * included in all copies or substantial portions of the Software.
1415fa138f2276a77679530fb608463ff5b4133f7bcaryclark@google.com *
15b45a1b46ee25e9b19800b028bb1ca925212ac7b4caryclark@google.com * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16b45a1b46ee25e9b19800b028bb1ca925212ac7b4caryclark@google.com * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1715fa138f2276a77679530fb608463ff5b4133f7bcaryclark@google.com * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1815fa138f2276a77679530fb608463ff5b4133f7bcaryclark@google.com * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1915fa138f2276a77679530fb608463ff5b4133f7bcaryclark@google.com * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2047580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2115fa138f2276a77679530fb608463ff5b4133f7bcaryclark@google.com * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * SOFTWARE.
2347580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com *
2447580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com **********************************************************/
2547580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com
2647580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com
2747580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com#include "wrapper_sw_winsys.h"
28f839c0359c308fd06895d9f73fc12c4f3869e399caryclark@google.com
290b7da433fe0eaa2833d1b2900715b013b36d93dacaryclark@google.com#include "pipe/p_format.h"
308f9f468b0555e95b8fc3cf4e6ee1f1fbf5492a1bcaryclark@google.com#include "pipe/p_state.h"
3173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
32a461ff0866526bc51dbd4c4f9f066a727ec21510caryclark@google.com#include "state_tracker/sw_winsys.h"
3347580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com
34f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com#include "util/u_memory.h"
3547580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com#include "util/u_inlines.h"
3631143cf37fa38dc98f71c71e518ecc21c83b5e27caryclark@google.com
37fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com/*
3847580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com * This code wraps a pipe_screen and exposes a sw_winsys interface for use
39fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * with software resterizers. This code is used by the DRM based winsys to
4047580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com * allow access to the drm driver.
414eeda37a7456876cb8d509a4ea43c7f4c684477acaryclark@google.com *
42fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com * We must borrow the whole stack because only the pipe screen knows how
4347580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com * to decode the content of a buffer. Or how to create a buffer that
44c899ad9c7fa28234d99479ab09afb6866bbd8dc3caryclark@google.com * can still be used by drivers using real hardware (as the case is
45e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com * with software st/xorg but hw st/dri).
4647580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com *
478dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com * We also need a pipe context for the transfers.
48e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com */
4947580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com
50f839c0359c308fd06895d9f73fc12c4f3869e399caryclark@google.comstruct wrapper_sw_winsys
51729e1c46cea63dfaa6e4a05608b8f3be41e19dcecaryclark@google.com{
5247580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com   struct sw_winsys base;
538f9f468b0555e95b8fc3cf4e6ee1f1fbf5492a1bcaryclark@google.com   struct pipe_screen *screen;
54afe56de6361a81eef537ddd8f6d5626c8546d4c7caryclark@google.com   struct pipe_context *pipe;
558dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   enum pipe_texture_target target;
568f9f468b0555e95b8fc3cf4e6ee1f1fbf5492a1bcaryclark@google.com};
57fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
58fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comstruct wrapper_sw_displaytarget
59fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
6047580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com   struct wrapper_sw_winsys *winsys;
61fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct pipe_resource *tex;
62c91dfe417a51f73c28ecf2708df1e0bee942c6eacaryclark@google.com   struct pipe_transfer *transfer;
634eeda37a7456876cb8d509a4ea43c7f4c684477acaryclark@google.com
646aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.com   unsigned map_count;
656aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.com   unsigned stride; /**< because we get stride at create */
663350c3c68ab75cd08721da3a938b8d2b10096d70caryclark@google.com   void *ptr;
67e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com};
683350c3c68ab75cd08721da3a938b8d2b10096d70caryclark@google.com
69534aa5b9460639a09b9dc30d29e77782e44b8fffcaryclark@google.comstatic INLINE struct wrapper_sw_winsys *
70e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.comwrapper_sw_winsys(struct sw_winsys *ws)
713350c3c68ab75cd08721da3a938b8d2b10096d70caryclark@google.com{
7265f9f0a1664a9cb38157ccfbcc3e0e936af0a58ecaryclark@google.com   return (struct wrapper_sw_winsys *)ws;
73729e1c46cea63dfaa6e4a05608b8f3be41e19dcecaryclark@google.com}
7447580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com
758f9f468b0555e95b8fc3cf4e6ee1f1fbf5492a1bcaryclark@google.comstatic INLINE struct wrapper_sw_displaytarget *
76afe56de6361a81eef537ddd8f6d5626c8546d4c7caryclark@google.comwrapper_sw_displaytarget(struct sw_displaytarget *dt)
7747580694fbe974a065caf7c39c3d2075708c2018caryclark@google.com{
788f9f468b0555e95b8fc3cf4e6ee1f1fbf5492a1bcaryclark@google.com   return (struct wrapper_sw_displaytarget *)dt;
79fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com}
80fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
81fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
826aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.com/*
83027de226c144d9e6b7a76acb2e904952b5620a5ecaryclark@google.com * Functions
84fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com */
85fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
8665f9f0a1664a9cb38157ccfbcc3e0e936af0a58ecaryclark@google.com
87fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comstatic boolean
88fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comwsw_dt_get_stride(struct wrapper_sw_displaytarget *wdt, unsigned *stride)
89fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
90fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct pipe_context *pipe = wdt->winsys->pipe;
918dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   struct pipe_resource *tex = wdt->tex;
928dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   struct pipe_transfer *tr;
938dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com
948dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   tr = pipe_get_transfer(pipe, tex, 0, 0,
9532546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com                          PIPE_TRANSFER_READ_WRITE,
9632546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com                          0, 0, wdt->tex->width0, wdt->tex->height0);
9732546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   if (!tr)
9832546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com      return FALSE;
9932546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
10032546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   *stride = tr->stride;
10132546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   wdt->stride = tr->stride;
10232546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
10332546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   pipe->transfer_destroy(pipe, tr);
104fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
105fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   return TRUE;
10632546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com}
10732546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
108fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comstatic struct sw_displaytarget *
109fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comwsw_dt_wrap_texture(struct wrapper_sw_winsys *wsw,
110fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com                    struct pipe_resource *tex, unsigned *stride)
111fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
112fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct wrapper_sw_displaytarget *wdt = CALLOC_STRUCT(wrapper_sw_displaytarget);
11332546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   if (!wdt)
11432546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com      goto err_unref;
1153350c3c68ab75cd08721da3a938b8d2b10096d70caryclark@google.com
116fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wdt->tex = tex;
117fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wdt->winsys = wsw;
11832546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
119fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   if (!wsw_dt_get_stride(wdt, stride))
12032546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com      goto err_free;
12132546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
12273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   return (struct sw_displaytarget *)wdt;
123fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
124fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comerr_free:
125fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   FREE(wdt);
126fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comerr_unref:
12732546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   pipe_resource_reference(&tex, NULL);
12832546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   return NULL;
129235f56a92f6eb6accbb243e11b3c45e3798f38f2caryclark@google.com}
130235f56a92f6eb6accbb243e11b3c45e3798f38f2caryclark@google.com
131235f56a92f6eb6accbb243e11b3c45e3798f38f2caryclark@google.comstatic struct sw_displaytarget *
132235f56a92f6eb6accbb243e11b3c45e3798f38f2caryclark@google.comwsw_dt_create(struct sw_winsys *ws,
133fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com              unsigned bind,
134235f56a92f6eb6accbb243e11b3c45e3798f38f2caryclark@google.com              enum pipe_format format,
13532546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com              unsigned width, unsigned height,
136fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com              unsigned alignment,
137fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com              unsigned *stride)
13873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com{
13973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
140fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct pipe_resource templ;
14132546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   struct pipe_resource *tex;
14273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
14373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   /*
14473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    * XXX Why don't we just get the template.
14573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    */
14673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   memset(&templ, 0, sizeof(templ));
14773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   templ.target = wsw->target;
14873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   templ.width0 = width;
14932546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   templ.height0 = height;
15073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   templ.depth0 = 1;
15173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   templ.array_size = 1;
15273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com   templ.format = format;
153fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   templ.bind = bind;
15473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
155fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   /* XXX alignment: we can't do anything about this */
156fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
157fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   tex = wsw->screen->resource_create(wsw->screen, &templ);
158fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   if (!tex)
159fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      return NULL;
16032546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
161fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   return wsw_dt_wrap_texture(wsw, tex, stride);
162fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com}
163fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
164fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comstatic struct sw_displaytarget *
165fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comwsw_dt_from_handle(struct sw_winsys *ws,
16632546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com                   const struct pipe_resource *templ,
167fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com                   struct winsys_handle *whandle,
168fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com                   unsigned *stride)
169fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
170fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
171fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct pipe_resource *tex;
17232546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
173fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   tex = wsw->screen->resource_from_handle(wsw->screen, templ, whandle);
174fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   if (!tex)
175fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      return NULL;
176e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com
177e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com   return wsw_dt_wrap_texture(wsw, tex, stride);
178e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com}
179e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com
180e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.comstatic boolean
181e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.comwsw_dt_get_handle(struct sw_winsys *ws,
182e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com                  struct sw_displaytarget *dt,
183e7bd5f4041701cbab87f6e779eb18fbb9fe74216caryclark@google.com                  struct winsys_handle *whandle)
1848dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com{
1858dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
18632546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
1878dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   struct pipe_resource *tex = wdt->tex;
1888dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com
1898dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   return wsw->screen->resource_get_handle(wsw->screen, tex, whandle);
1908dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com}
1918dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com
19232546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.comstatic void *
1938dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.comwsw_dt_map(struct sw_winsys *ws,
1948dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com           struct sw_displaytarget *dt,
1958dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com           unsigned flags)
1968dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com{
1978dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
19832546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   struct pipe_context *pipe = wdt->winsys->pipe;
1998dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   struct pipe_resource *tex = wdt->tex;
200fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct pipe_transfer *tr;
201fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   void *ptr;
2028dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com
2038dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   if (!wdt->map_count) {
2048dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com
2058dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com      assert(!wdt->transfer);
2068dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com
2078dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com      tr = pipe_get_transfer(pipe, tex, 0, 0,
2088dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com                             PIPE_TRANSFER_READ_WRITE,
2098dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com                             0, 0, wdt->tex->width0, wdt->tex->height0);
210fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      if (!tr)
21132546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com         return NULL;
212fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
213fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      ptr = pipe->transfer_map(pipe, tr);
214fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      if (!ptr)
215fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com        goto err;
216fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
217fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      wdt->transfer = tr;
218f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com      wdt->ptr = ptr;
219f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com
220f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com      /* XXX Handle this case */
221f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com      assert(tr->stride == wdt->stride);
222f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com   }
223fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
22432546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   wdt->map_count++;
225fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
226fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   return wdt->ptr;
227fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
228fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comerr:
229fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   pipe->transfer_destroy(pipe, tr);
230fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   return NULL;
2316aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.com}
2326aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.com
2336aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.comstatic void
2346aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.comwsw_dt_unmap(struct sw_winsys *ws,
2356aea33f92c611d6fdc88bc2352c5c966168af83bcaryclark@google.com             struct sw_displaytarget *dt)
236fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
23732546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
238fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct pipe_context *pipe = wdt->winsys->pipe;
239fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
240fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   assert(wdt->transfer);
241fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
242fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wdt->map_count--;
243fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
244f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com   if (wdt->map_count)
245f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com      return;
246f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com
247f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com   pipe->transfer_unmap(pipe, wdt->transfer);
248f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com   pipe->transfer_destroy(pipe, wdt->transfer);
249fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   pipe->flush(pipe, NULL);
250fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wdt->transfer = NULL;
251fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com}
252fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
253fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comstatic void
254fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comwsw_dt_destroy(struct sw_winsys *ws,
255fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com               struct sw_displaytarget *dt)
256f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com{
257f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com   struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
258f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com
259f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com   pipe_resource_reference(&wdt->tex, NULL);
260f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com
261f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com   FREE(wdt);
262f9502d7dfad5b361a3cdaa42eb75b593c95f79d8caryclark@google.com}
263fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
26432546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.comstatic void
265fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comwsw_destroy(struct sw_winsys *ws)
266fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
267fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
268fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
269fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->pipe->destroy(wsw->pipe);
270fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->screen->destroy(wsw->screen);
27132546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com
272fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   FREE(wsw);
273fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com}
274fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
275fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comstruct sw_winsys *
276fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comwrapper_sw_winsys_wrap_pipe_screen(struct pipe_screen *screen)
277fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
27832546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   struct wrapper_sw_winsys *wsw = CALLOC_STRUCT(wrapper_sw_winsys);
279fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
280fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   if (!wsw)
281fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      goto err;
282fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
283fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->base.displaytarget_create = wsw_dt_create;
284fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->base.displaytarget_from_handle = wsw_dt_from_handle;
285fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->base.displaytarget_get_handle = wsw_dt_get_handle;
286fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->base.displaytarget_map = wsw_dt_map;
287fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->base.displaytarget_unmap = wsw_dt_unmap;
288fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->base.displaytarget_destroy = wsw_dt_destroy;
289fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->base.destroy = wsw_destroy;
290fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
291fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->screen = screen;
29232546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   wsw->pipe = screen->context_create(screen, NULL);
293fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   if (!wsw->pipe)
294fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      goto err_free;
295fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
296fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   if(screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES))
297fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com      wsw->target = PIPE_TEXTURE_2D;
298fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   else
29932546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com      wsw->target = PIPE_TEXTURE_RECT;
300fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
301fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   return &wsw->base;
302fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
303fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comerr_free:
304fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   FREE(wsw);
305fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comerr:
30632546db1494a6c6433a7919844133a6ff5b5c7b2caryclark@google.com   return NULL;
307fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com}
308fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
309fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comstruct pipe_screen *
310fa0588ff672564af1c235a63589573829035a60bcaryclark@google.comwrapper_sw_winsys_dewrap_pipe_screen(struct sw_winsys *ws)
311fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com{
312fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
313fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   struct pipe_screen *screen = wsw->screen;
314fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
315fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   wsw->pipe->destroy(wsw->pipe);
316fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   /* don't destroy the screen its needed later on */
317fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com
318fa0588ff672564af1c235a63589573829035a60bcaryclark@google.com   FREE(wsw);
3198dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com   return screen;
3208dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com}
3218dcf114db9762c02d217beba6e29dffa4e92d298caryclark@google.com