radeon: handle query buffer allocation and mapping failures
authorNicolai Hähnle <nicolai.haehnle@amd.com>
Wed, 20 Apr 2016 14:11:16 +0000 (09:11 -0500)
committerNicolai Hähnle <nicolai.haehnle@amd.com>
Fri, 22 Apr 2016 03:33:12 +0000 (22:33 -0500)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=94984
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
src/gallium/drivers/radeon/r600_query.c
src/gallium/drivers/radeon/r600_query.h

index 6e53c76ae556ae1ab4a67e462c01b5d6b67f01db..38c7026a784e1584cce1c75275e194248c1cc348 100644 (file)
@@ -271,14 +271,20 @@ static struct r600_resource *r600_new_query_buffer(struct r600_common_context *c
        struct r600_resource *buf = (struct r600_resource*)
                pipe_buffer_create(ctx->b.screen, PIPE_BIND_CUSTOM,
                                   PIPE_USAGE_STAGING, buf_size);
+       if (!buf)
+               return NULL;
 
-       if (query->flags & R600_QUERY_HW_FLAG_PREDICATE)
-               query->ops->prepare_buffer(ctx, query, buf);
+       if (query->flags & R600_QUERY_HW_FLAG_PREDICATE) {
+               if (!query->ops->prepare_buffer(ctx, query, buf)) {
+                       pipe_resource_reference((struct pipe_resource **)&buf, NULL);
+                       return NULL;
+               }
+       }
 
        return buf;
 }
 
-static void r600_query_hw_prepare_buffer(struct r600_common_context *ctx,
+static bool r600_query_hw_prepare_buffer(struct r600_common_context *ctx,
                                         struct r600_query_hw *query,
                                         struct r600_resource *buffer)
 {
@@ -286,6 +292,8 @@ static void r600_query_hw_prepare_buffer(struct r600_common_context *ctx,
        uint32_t *results = ctx->ws->buffer_map(buffer->buf, NULL,
                                                PIPE_TRANSFER_WRITE |
                                                PIPE_TRANSFER_UNSYNCHRONIZED);
+       if (!results)
+               return false;
 
        memset(results, 0, buffer->b.b.width0);
 
@@ -306,6 +314,8 @@ static void r600_query_hw_prepare_buffer(struct r600_common_context *ctx,
                        results += 4 * ctx->max_db;
                }
        }
+
+       return true;
 }
 
 static struct r600_query_ops query_hw_ops = {
@@ -496,6 +506,9 @@ static void r600_query_hw_emit_start(struct r600_common_context *ctx,
 {
        uint64_t va;
 
+       if (!query->buffer.buf)
+               return; // previous buffer allocation failure
+
        r600_update_occlusion_query_state(ctx, query->b.type, 1);
        r600_update_prims_generated_query_state(ctx, query->b.type, 1);
 
@@ -506,9 +519,11 @@ static void r600_query_hw_emit_start(struct r600_common_context *ctx,
        if (query->buffer.results_end + query->result_size > query->buffer.buf->b.b.width0) {
                struct r600_query_buffer *qbuf = MALLOC_STRUCT(r600_query_buffer);
                *qbuf = query->buffer;
-               query->buffer.buf = r600_new_query_buffer(ctx, query);
                query->buffer.results_end = 0;
                query->buffer.previous = qbuf;
+               query->buffer.buf = r600_new_query_buffer(ctx, query);
+               if (!query->buffer.buf)
+                       return;
        }
 
        /* emit begin query */
@@ -575,6 +590,9 @@ static void r600_query_hw_emit_stop(struct r600_common_context *ctx,
 {
        uint64_t va;
 
+       if (!query->buffer.buf)
+               return; // previous buffer allocation failure
+
        /* The queries which need begin already called this in begin_query. */
        if (query->flags & R600_QUERY_HW_FLAG_NO_START) {
                ctx->need_gfx_cs_space(&ctx->b, query->num_cs_dw_end, FALSE);
@@ -694,6 +712,9 @@ static void r600_query_hw_reset_buffers(struct r600_common_context *rctx,
                FREE(qbuf);
        }
 
+       query->buffer.results_end = 0;
+       query->buffer.previous = NULL;
+
        if (query->flags & R600_QUERY_HW_FLAG_PREDICATE) {
                /* Obtain a new buffer if the current one can't be mapped without a stall. */
                if (r600_rings_is_buffer_referenced(rctx, query->buffer.buf->buf, RADEON_USAGE_READWRITE) ||
@@ -701,12 +722,10 @@ static void r600_query_hw_reset_buffers(struct r600_common_context *rctx,
                        pipe_resource_reference((struct pipe_resource**)&query->buffer.buf, NULL);
                        query->buffer.buf = r600_new_query_buffer(rctx, query);
                } else {
-                       query->ops->prepare_buffer(rctx, query, query->buffer.buf);
+                       if (!query->ops->prepare_buffer(rctx, query, query->buffer.buf))
+                               pipe_resource_reference((struct pipe_resource**)&query->buffer.buf, NULL);
                }
        }
-
-       query->buffer.results_end = 0;
-       query->buffer.previous = NULL;
 }
 
 boolean r600_query_hw_begin(struct r600_common_context *rctx,
@@ -722,6 +741,8 @@ boolean r600_query_hw_begin(struct r600_common_context *rctx,
        r600_query_hw_reset_buffers(rctx, query);
 
        r600_query_hw_emit_start(rctx, query);
+       if (!query->buffer.buf)
+               return false;
 
        LIST_ADDTAIL(&query->list, &rctx->active_queries);
        return true;
@@ -748,6 +769,9 @@ bool r600_query_hw_end(struct r600_common_context *rctx,
        if (!(query->flags & R600_QUERY_HW_FLAG_NO_START))
                LIST_DELINIT(&query->list);
 
+       if (!query->buffer.buf)
+               return false;
+
        return true;
 }
 
index b4f069f50ceae17a786f83f6afc67dbeb71bddcc..a3c9aa29ab65eb10f4f5b75b1b93a37f1bc14c7a 100644 (file)
@@ -91,7 +91,7 @@ enum {
 };
 
 struct r600_query_hw_ops {
-       void (*prepare_buffer)(struct r600_common_context *,
+       bool (*prepare_buffer)(struct r600_common_context *,
                               struct r600_query_hw *,
                               struct r600_resource *);
        void (*emit_start)(struct r600_common_context *,