From 9747cf2c8f469568da378731f70ba35e2c61c6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Thu, 4 Jan 2024 01:51:51 -0500 Subject: [PATCH] st/mesa: merge 3 unlikely blocks in _mesa_get_bufferobj_reference Reviewed-by: Pierre-Eric Pelloux-Prayer Reviewed-By: Mike Blumenkrantz Part-of: --- src/mesa/main/bufferobj.h | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/mesa/main/bufferobj.h b/src/mesa/main/bufferobj.h index d6db0c17f7c..d3729c68d39 100644 --- a/src/mesa/main/bufferobj.h +++ b/src/mesa/main/bufferobj.h @@ -44,26 +44,32 @@ _mesa_get_bufferobj_reference(struct gl_context *ctx, struct gl_buffer_object *o struct pipe_resource *buffer = obj->buffer; - if (unlikely(!buffer)) - return NULL; - /* Only one context is using the fast path. All other contexts must use * the slow path. */ - if (unlikely(obj->private_refcount_ctx != ctx)) { - p_atomic_inc(&buffer->reference.count); + if (unlikely(obj->private_refcount_ctx != ctx || + obj->private_refcount <= 0)) { + if (buffer) { + if (obj->private_refcount_ctx != ctx) { + p_atomic_inc(&buffer->reference.count); + } else { + /* This is the number of atomic increments we will skip. */ + const unsigned count = 100000000; + p_atomic_add(&buffer->reference.count, count); + + /* Remove the reference that we return. */ + assert(obj->private_refcount == 0); + obj->private_refcount = count - 1; + } + } return buffer; } - if (unlikely(obj->private_refcount <= 0)) { - assert(obj->private_refcount == 0); - - /* This is the number of atomic increments we will skip. */ - obj->private_refcount = 100000000; - p_atomic_add(&buffer->reference.count, obj->private_refcount); - } - - /* Return a buffer reference while decrementing the private refcount. */ + /* Return a buffer reference while decrementing the private refcount. + * The buffer must be non-NULL, which is implied by private_refcount_ctx + * being non-NULL. + */ + assert(buffer); obj->private_refcount--; return buffer; }