etnaviv: add helper to work out RS alignment
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_resource.c
index a709482c1b6b3b9c755bc3b197d44dbf8f8fd4fd..1cd906d4afa5dd08232bc007d065f7a7ff79cc36 100644 (file)
@@ -180,11 +180,8 @@ etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
                         &paddingY, &halign);
    assert(paddingX && paddingY);
 
-   if (templat->target != PIPE_BUFFER) {
-      unsigned min_paddingY = 4 * screen->specs.pixel_pipes;
-      if (paddingY < min_paddingY)
-         paddingY = min_paddingY;
-   }
+   if (templat->target != PIPE_BUFFER)
+      etna_adjust_rs_align(screen->specs.pixel_pipes, NULL, &paddingY);
 
    struct etna_resource *rsc = CALLOC_STRUCT(etna_resource);
 
@@ -214,8 +211,20 @@ etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
    rsc->bo = bo;
    rsc->ts_bo = 0; /* TS is only created when first bound to surface */
 
-   if (templat->bind & PIPE_BIND_SCANOUT)
-      rsc->scanout = renderonly_scanout_for_resource(&rsc->base, screen->ro);
+   if (templat->bind & PIPE_BIND_SCANOUT) {
+      struct winsys_handle handle;
+      rsc->scanout = renderonly_scanout_for_resource(&rsc->base, screen->ro,
+                                                     &handle);
+      if (!rsc->scanout)
+         goto free_rsc;
+
+      rsc->external = pscreen->resource_from_handle(pscreen, &rsc->base,
+                                                    &handle,
+                                                    PIPE_HANDLE_USAGE_WRITE);
+      close(handle.handle);
+      if (!rsc->external)
+         goto free_rsc;
+   }
 
    if (DBG_ENABLED(ETNA_DBG_ZERO)) {
       void *map = etna_bo_map(bo);
@@ -310,6 +319,7 @@ etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
    list_delinit(&rsc->list);
 
    pipe_resource_reference(&rsc->texture, NULL);
+   pipe_resource_reference(&rsc->external, NULL);
 
    FREE(rsc);
 }
@@ -355,11 +365,10 @@ etna_resource_from_handle(struct pipe_screen *pscreen,
 
    /* We will be using the RS to copy with this resource, so we must
     * ensure that it is appropriately aligned for the RS requirements. */
-   unsigned paddingX = ETNA_RS_WIDTH_MASK + 1;
-   unsigned paddingY = (ETNA_RS_HEIGHT_MASK + 1) * screen->specs.pixel_pipes;
+   level->padded_width = level->width;
+   level->padded_height = level->height;
+   etna_adjust_rs_align(&level->padded_width, &level->padded_height);
 
-   level->padded_width = align(level->width, paddingX);
-   level->padded_height = align(level->height, paddingY);
    level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format,
                                                                   level->padded_height);
 
@@ -379,16 +388,12 @@ etna_resource_from_handle(struct pipe_screen *pscreen,
       /* Render targets are linear in Xorg but must be tiled
       * here. It would be nice if dri_drawable_get_format()
       * set scanout for these buffers too. */
-      struct etna_resource *tiled;
 
       ptiled = etna_resource_create(pscreen, tmpl);
       if (!ptiled)
          goto fail;
 
-      tiled = etna_resource(ptiled);
-      tiled->scanout = renderonly_scanout_for_prime(prsc, screen->ro);
-      if (!tiled->scanout)
-         goto fail;
+      etna_resource(ptiled)->external = prsc;
 
       return ptiled;
    }
@@ -410,9 +415,18 @@ etna_resource_get_handle(struct pipe_screen *pscreen,
                          struct winsys_handle *handle, unsigned usage)
 {
    struct etna_resource *rsc = etna_resource(prsc);
+   /* Scanout is always attached to the base resource */
+   struct renderonly_scanout *scanout = rsc->scanout;
+
+   /*
+    * External resources are preferred, so a import->export chain of
+    * render/sampler incompatible buffers yield the same handle.
+    */
+   if (rsc->external)
+      rsc = etna_resource(rsc->external);
 
    if (handle->type == DRM_API_HANDLE_TYPE_KMS &&
-       renderonly_get_handle(rsc->scanout, handle))
+       renderonly_get_handle(scanout, handle))
       return TRUE;
 
    return etna_screen_bo_get_handle(pscreen, rsc->bo, rsc->levels[0].stride,