Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / winsys / drm / nouveau / drm / nouveau_drm_api.c
index 395b21ec7ab7e36acad6f58ebb67f12fa384956d..117ca6059bbfcaadfee03f611c68c1d9dca66736 100644 (file)
 #include "nouveau/nouveau_screen.h"
 
 static struct pipe_surface *
-dri_surface_from_handle(struct pipe_screen *screen,
-                        unsigned handle,
-                        enum pipe_format format,
-                        unsigned width,
-                        unsigned height,
-                        unsigned pitch)
+dri_surface_from_handle(struct drm_api *api, struct pipe_screen *pscreen,
+                        unsigned handle, enum pipe_format format,
+                        unsigned width, unsigned height, unsigned pitch)
 {
-   struct pipe_surface *surface = NULL;
-   struct pipe_texture *texture = NULL;
-   struct pipe_texture templat;
-   struct pipe_buffer *buf = NULL;
-
-   buf = drm_api_hooks.buffer_from_handle(screen, "front buffer", handle);
-   if (!buf)
-      return NULL;
-
-   memset(&templat, 0, sizeof(templat));
-   templat.tex_usage = PIPE_TEXTURE_USAGE_PRIMARY |
-                       NOUVEAU_TEXTURE_USAGE_LINEAR;
-   templat.target = PIPE_TEXTURE_2D;
-   templat.last_level = 0;
-   templat.depth[0] = 1;
-   templat.format = format;
-   templat.width[0] = width;
-   templat.height[0] = height;
-   pf_get_block(templat.format, &templat.block);
-
-   texture = screen->texture_blanket(screen,
-                                     &templat,
-                                     &pitch,
-                                     buf);
-
-   /* we don't need the buffer from this point on */
-   pipe_buffer_reference(&buf, NULL);
-
-   if (!texture)
-      return NULL;
-
-   surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
-                                     PIPE_BUFFER_USAGE_GPU_READ |
-                                     PIPE_BUFFER_USAGE_GPU_WRITE);
-
-   /* we don't need the texture from this point on */
-   pipe_texture_reference(&texture, NULL);
-   return surface;
+       struct pipe_surface *ps = NULL;
+       struct pipe_texture *pt = NULL;
+       struct pipe_texture tmpl;
+
+       memset(&tmpl, 0, sizeof(tmpl));
+       tmpl.tex_usage = PIPE_TEXTURE_USAGE_PRIMARY |
+                        NOUVEAU_TEXTURE_USAGE_LINEAR;
+       tmpl.target = PIPE_TEXTURE_2D;
+       tmpl.last_level = 0;
+       tmpl.depth[0] = 1;
+       tmpl.format = format;
+       tmpl.width[0] = width;
+       tmpl.height[0] = height;
+       pf_get_block(tmpl.format, &tmpl.block);
+
+       pt = api->texture_from_shared_handle(api, pscreen, &tmpl,
+                                            "front buffer", pitch, handle);
+       if (!pt)
+               return NULL;
+
+       ps = pscreen->get_tex_surface(pscreen, pt, 0, 0, 0,
+                                     PIPE_BUFFER_USAGE_GPU_READ |
+                                     PIPE_BUFFER_USAGE_GPU_WRITE);
+
+       /* we don't need the texture from this point on */
+       pipe_texture_reference(&pt, NULL);
+       return ps;
 }
 
 static struct pipe_surface *
@@ -70,7 +56,8 @@ static struct dri1_api nouveau_dri1_api = {
 };
 
 static struct pipe_screen *
-nouveau_drm_create_screen(int fd, struct drm_create_screen_arg *arg)
+nouveau_drm_create_screen(struct drm_api *api, int fd,
+                         struct drm_create_screen_arg *arg)
 {
        struct dri1_create_screen_arg *dri1 = (void *)arg;
        struct nouveau_winsys *nvws;
@@ -125,7 +112,7 @@ nouveau_drm_create_screen(int fd, struct drm_create_screen_arg *arg)
                return NULL;
        }
 
-       if (arg->mode == DRM_CREATE_DRI1) {
+       if (arg && arg->mode == DRM_CREATE_DRI1) {
                struct nouveau_dri *nvdri = dri1->ddx_info;
                enum pipe_format format;
 
@@ -134,13 +121,12 @@ nouveau_drm_create_screen(int fd, struct drm_create_screen_arg *arg)
                else
                        format = PIPE_FORMAT_A8R8G8B8_UNORM;
 
-               nvws->front = dri_surface_from_handle(nvws->pscreen,
-                                                      nvdri->front_offset,
-                                                      format,
-                                                      nvdri->width,
-                                                      nvdri->height,
-                                                      nvdri->front_pitch *
-                                                      (nvdri->bpp / 8));
+               nvws->front = dri_surface_from_handle(api, nvws->pscreen,
+                                                     nvdri->front_offset,
+                                                     format, nvdri->width,
+                                                     nvdri->height,
+                                                     nvdri->front_pitch *
+                                                     (nvdri->bpp / 8));
                if (!nvws->front) {
                        debug_printf("%s: error referencing front buffer\n",
                                     __func__);
@@ -155,7 +141,7 @@ nouveau_drm_create_screen(int fd, struct drm_create_screen_arg *arg)
 }
 
 static struct pipe_context *
-nouveau_drm_create_context(struct pipe_screen *pscreen)
+nouveau_drm_create_context(struct drm_api *api, struct pipe_screen *pscreen)
 {
        struct nouveau_winsys *nvws = nouveau_winsys_screen(pscreen);
        struct pipe_context *(*init)(struct pipe_screen *, unsigned);
@@ -205,16 +191,10 @@ nouveau_drm_create_context(struct pipe_screen *pscreen)
        return nvws->pctx[i];
 }
 
-static boolean
-nouveau_drm_pb_from_pt(struct pipe_texture *pt, struct pipe_buffer **ppb,
-                      unsigned *stride)
-{
-       return false;
-}
-
-static struct pipe_buffer *
-nouveau_drm_pb_from_handle(struct pipe_screen *pscreen, const char *name,
-                          unsigned handle)
+static struct pipe_texture *
+nouveau_drm_pt_from_name(struct drm_api *api, struct pipe_screen *pscreen,
+                        struct pipe_texture *templ, const char *name,
+                        unsigned stride, unsigned handle)
 {
        struct nouveau_device *dev = nouveau_screen(pscreen)->device;
        struct pipe_buffer *pb;
@@ -238,40 +218,46 @@ nouveau_drm_pb_from_handle(struct pipe_screen *pscreen, const char *name,
        pb->usage = PIPE_BUFFER_USAGE_GPU_READ_WRITE |
                    PIPE_BUFFER_USAGE_CPU_READ_WRITE;
        pb->size = nouveau_bo(pb)->size;
-       return pb;
+       return pscreen->texture_blanket(pscreen, templ, &stride, pb);
 }
 
 static boolean
-nouveau_drm_handle_from_pb(struct pipe_screen *pscreen, struct pipe_buffer *pb,
-                          unsigned *handle)
+nouveau_drm_name_from_pt(struct drm_api *api, struct pipe_screen *pscreen,
+                        struct pipe_texture *pt, unsigned *stride,
+                        unsigned *handle)
 {
-       struct nouveau_bo *bo = nouveau_bo(pb);
+       struct nouveau_miptree *mt = nouveau_miptree(pt);
 
-       if (!bo)
-               return FALSE;
+       if (!mt || !mt->bo)
+               return false;
 
-       *handle = bo->handle;
-       return TRUE;
+       return nouveau_bo_handle_get(mt->bo, handle) == 0;
 }
 
 static boolean
-nouveau_drm_name_from_pb(struct pipe_screen *pscreen, struct pipe_buffer *pb,
-                        unsigned *handle)
+nouveau_drm_handle_from_pt(struct drm_api *api, struct pipe_screen *pscreen,
+                          struct pipe_texture *pt, unsigned *stride,
+                          unsigned *handle)
 {
-       struct nouveau_bo *bo = nouveau_bo(pb);
+       struct nouveau_miptree *mt = nouveau_miptree(pt);
 
-       if (!bo)
-               return FALSE;
+       if (!mt || !mt->bo)
+               return false;
 
-       return nouveau_bo_handle_get(bo, handle) == 0;
+       *handle = mt->bo->handle;
+       return true;
 }
 
 struct drm_api drm_api_hooks = {
        .create_screen = nouveau_drm_create_screen,
        .create_context = nouveau_drm_create_context,
-       .buffer_from_texture = nouveau_drm_pb_from_pt,
-       .buffer_from_handle = nouveau_drm_pb_from_handle,
-       .handle_from_buffer = nouveau_drm_handle_from_pb,
-       .global_handle_from_buffer = nouveau_drm_name_from_pb,
+       .texture_from_shared_handle = nouveau_drm_pt_from_name,
+       .shared_handle_from_texture = nouveau_drm_name_from_pt,
+       .local_handle_from_texture = nouveau_drm_handle_from_pt,
 };
 
+struct drm_api *
+drm_api_create() {
+       return &drm_api_hooks;
+}
+