freedreno: use renderonly path for buffers allocated with modifiers
[mesa.git] / src / gallium / drivers / freedreno / freedreno_resource.c
index ca48fc9cee1f2fbdce2268fafe924176a50d8ebb..144d725fdec8b1fc0283155b6576cbfafed72ea1 100644 (file)
@@ -43,7 +43,7 @@
 #include "freedreno_query_hw.h"
 #include "freedreno_util.h"
 
-#include <drm_fourcc.h>
+#include "drm-uapi/drm_fourcc.h"
 #include <errno.h>
 
 /* XXX this should go away, needed for 'struct winsys_handle' */
@@ -373,7 +373,9 @@ flush_resource(struct fd_context *ctx, struct fd_resource *rsc, unsigned usage)
 {
        struct fd_batch *write_batch = NULL;
 
-       fd_batch_reference(&write_batch, rsc->write_batch);
+       mtx_lock(&ctx->screen->lock);
+       fd_batch_reference_locked(&write_batch, rsc->write_batch);
+       mtx_unlock(&ctx->screen->lock);
 
        if (usage & PIPE_TRANSFER_WRITE) {
                struct fd_batch *batch, *batches[32] = {};
@@ -387,7 +389,7 @@ flush_resource(struct fd_context *ctx, struct fd_resource *rsc, unsigned usage)
                mtx_lock(&ctx->screen->lock);
                batch_mask = rsc->batch_mask;
                foreach_batch(batch, &ctx->screen->batch_cache, batch_mask)
-                       fd_batch_reference(&batches[batch->idx], batch);
+                       fd_batch_reference_locked(&batches[batch->idx], batch);
                mtx_unlock(&ctx->screen->lock);
 
                foreach_batch(batch, &ctx->screen->batch_cache, batch_mask)
@@ -501,7 +503,10 @@ fd_resource_transfer_map(struct pipe_context *pctx,
                                fd_blit_to_staging(ctx, trans);
 
                                struct fd_batch *batch = NULL;
-                               fd_batch_reference(&batch, staging_rsc->write_batch);
+
+                               fd_context_lock(ctx);
+                               fd_batch_reference_locked(&batch, staging_rsc->write_batch);
+                               fd_context_unlock(ctx);
 
                                /* we can't fd_bo_cpu_prep() until the blit to staging
                                 * is submitted to kernel.. in that case write_batch
@@ -550,7 +555,9 @@ fd_resource_transfer_map(struct pipe_context *pctx,
                struct fd_batch *write_batch = NULL;
 
                /* hold a reference, so it doesn't disappear under us: */
-               fd_batch_reference(&write_batch, rsc->write_batch);
+               fd_context_lock(ctx);
+               fd_batch_reference_locked(&write_batch, rsc->write_batch);
+               fd_context_unlock(ctx);
 
                if ((usage & PIPE_TRANSFER_WRITE) && write_batch &&
                                write_batch->back_blit) {
@@ -663,6 +670,9 @@ fd_resource_destroy(struct pipe_screen *pscreen,
        fd_bc_invalidate_resource(rsc, true);
        if (rsc->bo)
                fd_bo_del(rsc->bo);
+       if (rsc->scanout)
+               renderonly_scanout_destroy(rsc->scanout, fd_screen(pscreen)->ro);
+
        util_range_destroy(&rsc->valid_buffer_range);
        FREE(rsc);
 }
@@ -688,7 +698,7 @@ fd_resource_get_handle(struct pipe_screen *pscreen,
 
        handle->modifier = fd_resource_modifier(rsc);
 
-       return fd_screen_bo_get_handle(pscreen, rsc->bo,
+       return fd_screen_bo_get_handle(pscreen, rsc->bo, rsc->scanout,
                        rsc->slices[0].pitch * rsc->cpp, handle);
 }
 
@@ -845,11 +855,43 @@ fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
                const uint64_t *modifiers, int count)
 {
        struct fd_screen *screen = fd_screen(pscreen);
-       struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
-       struct pipe_resource *prsc = &rsc->base;
+       struct fd_resource *rsc;
+       struct pipe_resource *prsc;
        enum pipe_format format = tmpl->format;
        uint32_t size;
 
+       /* when using kmsro, scanout buffers are allocated on the display device
+        * create_with_modifiers() doesn't give us usage flags, so we have to
+        * assume that all calls with modifiers are scanout-possible
+        */
+       if (screen->ro &&
+               ((tmpl->bind & PIPE_BIND_SCANOUT) ||
+                !(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID))) {
+               struct pipe_resource scanout_templat = *tmpl;
+               struct renderonly_scanout *scanout;
+               struct winsys_handle handle;
+
+               scanout = renderonly_scanout_for_resource(&scanout_templat,
+                                                                                                 screen->ro, &handle);
+               if (!scanout)
+                       return NULL;
+
+               renderonly_scanout_destroy(scanout, screen->ro);
+
+               assert(handle.type == WINSYS_HANDLE_TYPE_FD);
+               rsc = fd_resource(pscreen->resource_from_handle(pscreen, tmpl,
+                                                                                                               &handle,
+                                                                                                               PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
+               close(handle.handle);
+               if (!rsc)
+                       return NULL;
+
+               return &rsc->base;
+       }
+
+       rsc = CALLOC_STRUCT(fd_resource);
+       prsc = &rsc->base;
+
        DBG("%p: target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
                        "nr_samples=%u, usage=%u, bind=%x, flags=%x", prsc,
                        tmpl->target, util_format_name(format),
@@ -885,7 +927,6 @@ fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
 
        if (screen->tile_mode &&
                        (tmpl->target != PIPE_BUFFER) &&
-                       (tmpl->bind & PIPE_BIND_SAMPLER_VIEW) &&
                        !linear) {
                rsc->tile_mode = screen->tile_mode(tmpl);
        }
@@ -898,8 +939,7 @@ fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
 
        rsc->internal_format = format;
        rsc->cpp = util_format_get_blocksize(format);
-       prsc->nr_samples = MAX2(1, prsc->nr_samples);
-       rsc->cpp *= prsc->nr_samples;
+       rsc->cpp *= fd_resource_nr_samples(prsc);
 
        assert(rsc->cpp);
 
@@ -1024,9 +1064,9 @@ fd_resource_from_handle(struct pipe_screen *pscreen,
        if (!rsc->bo)
                goto fail;
 
-       prsc->nr_samples = MAX2(1, prsc->nr_samples);
        rsc->internal_format = tmpl->format;
-       rsc->cpp = prsc->nr_samples * util_format_get_blocksize(tmpl->format);
+       rsc->cpp = util_format_get_blocksize(tmpl->format);
+       rsc->cpp *= fd_resource_nr_samples(prsc);
        slice->pitch = handle->stride / rsc->cpp;
        slice->offset = handle->offset;
        slice->size0 = handle->stride * prsc->height0;
@@ -1050,6 +1090,12 @@ fd_resource_from_handle(struct pipe_screen *pscreen,
 
        assert(rsc->cpp);
 
+       if (screen->ro) {
+               rsc->scanout =
+                       renderonly_create_gpu_import_for_resource(prsc, screen->ro, NULL);
+               /* failure is expected in some cases.. */
+       }
+
        return prsc;
 
 fail:
@@ -1076,102 +1122,33 @@ fd_render_condition_check(struct pipe_context *pctx)
        return true;
 }
 
-/**
- * Optimal hardware path for blitting pixels.
- * Scaling, format conversion, up- and downsampling (resolve) are allowed.
- */
-static void
-fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
-{
-       struct fd_context *ctx = fd_context(pctx);
-       struct pipe_blit_info info = *blit_info;
-
-       if (info.render_condition_enable && !fd_render_condition_check(pctx))
-               return;
-
-       if (info.mask & PIPE_MASK_S) {
-               DBG("cannot blit stencil, skipping");
-               info.mask &= ~PIPE_MASK_S;
-       }
-
-       if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
-               DBG("blit unsupported %s -> %s",
-                               util_format_short_name(info.src.resource->format),
-                               util_format_short_name(info.dst.resource->format));
-               return;
-       }
-
-       if (!(ctx->blit && ctx->blit(ctx, &info)))
-               fd_blitter_blit(ctx, &info);
-}
-
-void
-fd_blitter_pipe_begin(struct fd_context *ctx, bool render_cond, bool discard,
-               enum fd_render_stage stage)
-{
-       fd_fence_ref(ctx->base.screen, &ctx->last_fence, NULL);
-
-       util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,
-                       ctx->constbuf[PIPE_SHADER_FRAGMENT].cb);
-       util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
-       util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
-       util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp);
-       util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,
-                       ctx->streamout.targets);
-       util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
-       util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
-       util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
-       util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp);
-       util_blitter_save_blend(ctx->blitter, ctx->blend);
-       util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
-       util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
-       util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
-       util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
-       util_blitter_save_fragment_sampler_states(ctx->blitter,
-                       ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers,
-                       (void **)ctx->tex[PIPE_SHADER_FRAGMENT].samplers);
-       util_blitter_save_fragment_sampler_views(ctx->blitter,
-                       ctx->tex[PIPE_SHADER_FRAGMENT].num_textures,
-                       ctx->tex[PIPE_SHADER_FRAGMENT].textures);
-       if (!render_cond)
-               util_blitter_save_render_condition(ctx->blitter,
-                       ctx->cond_query, ctx->cond_cond, ctx->cond_mode);
-
-       if (ctx->batch)
-               fd_batch_set_stage(ctx->batch, stage);
-
-       ctx->in_blit = discard;
-}
-
-void
-fd_blitter_pipe_end(struct fd_context *ctx)
-{
-       if (ctx->batch)
-               fd_batch_set_stage(ctx->batch, FD_STAGE_NULL);
-       ctx->in_blit = false;
-}
-
 static void
 fd_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
 {
+       struct fd_context *ctx = fd_context(pctx);
        struct fd_resource *rsc = fd_resource(prsc);
 
        /*
         * TODO I guess we could track that the resource is invalidated and
         * use that as a hint to realloc rather than stall in _transfer_map(),
         * even in the non-DISCARD_WHOLE_RESOURCE case?
+        *
+        * Note: we set dirty bits to trigger invalidate logic fd_draw_vbo
         */
 
        if (rsc->write_batch) {
                struct fd_batch *batch = rsc->write_batch;
                struct pipe_framebuffer_state *pfb = &batch->framebuffer;
 
-               if (pfb->zsbuf && pfb->zsbuf->texture == prsc)
+               if (pfb->zsbuf && pfb->zsbuf->texture == prsc) {
                        batch->resolve &= ~(FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
+                       ctx->dirty |= FD_DIRTY_ZSA;
+               }
 
                for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
                        if (pfb->cbufs[i] && pfb->cbufs[i]->texture == prsc) {
                                batch->resolve &= ~(PIPE_CLEAR_COLOR0 << i);
+                               ctx->dirty |= FD_DIRTY_FRAMEBUFFER;
                        }
                }
        }