[gtk/wip/chergert/glproto] remove use of garray in batches



commit 3bcfd5069d41d7c772a8513def901306366eb10b
Author: Christian Hergert <chergert redhat com>
Date:   Wed Feb 10 16:49:31 2021 -0800

    remove use of garray in batches
    
     - remove the extra pointer dereference
     - remove function calls to set_size()

 gsk/next/gskglcommandqueue.c        | 57 ++++++++++++++++++++-----------------
 gsk/next/gskglcommandqueueprivate.h | 12 ++++----
 2 files changed, 36 insertions(+), 33 deletions(-)
---
diff --git a/gsk/next/gskglcommandqueue.c b/gsk/next/gskglcommandqueue.c
index a17ca72adc..ba57c20c7c 100644
--- a/gsk/next/gskglcommandqueue.c
+++ b/gsk/next/gskglcommandqueue.c
@@ -237,7 +237,7 @@ gsk_gl_command_queue_dispose (GObject *object)
   g_clear_object (&self->profiler);
   g_clear_object (&self->gl_profiler);
   g_clear_object (&self->context);
-  g_clear_pointer (&self->batches, g_array_unref);
+  g_clear_pointer (&self->batches, g_free);
   g_clear_pointer (&self->attachments, gsk_gl_attachment_state_unref);
   g_clear_pointer (&self->uniforms, gsk_gl_uniform_state_unref);
   g_clear_pointer (&self->batch_draws, g_array_unref);
@@ -262,7 +262,8 @@ gsk_gl_command_queue_init (GskGLCommandQueue *self)
 {
   self->max_texture_size = -1;
 
-  self->batches = g_array_new (FALSE, FALSE, sizeof (GskGLCommandBatch));
+  self->batches_len = 128;
+  self->batches = g_new0 (GskGLCommandBatch, self->batches_len);
   self->batch_draws = g_array_new (FALSE, FALSE, sizeof (GskGLCommandDraw));
   self->batch_binds = g_array_new (FALSE, FALSE, sizeof (GskGLCommandBind));
   self->batch_uniforms = g_array_new (FALSE, FALSE, sizeof (GskGLCommandUniform));
@@ -296,18 +297,20 @@ gsk_gl_command_queue_new (GdkGLContext      *context,
   return g_steal_pointer (&self);
 }
 
-static GskGLCommandBatch *
+static inline GskGLCommandBatch *
 begin_next_batch (GskGLCommandQueue *self)
 {
   GskGLCommandBatch *batch;
-  guint index;
 
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
 
-  index = self->batches->len;
-  g_array_set_size (self->batches, index + 1);
+  if G_UNLIKELY (self->n_batches == self->batches_len)
+    {
+      self->batches_len *= 2;
+      self->batches = g_realloc_n (self->batches, self->batches_len, sizeof (GskGLCommandBatch));
+    }
 
-  batch = &g_array_index (self->batches, GskGLCommandBatch, index);
+  batch = &self->batches[self->n_batches++];
   batch->any.next_batch_index = -1;
 
   return batch;
@@ -316,17 +319,17 @@ begin_next_batch (GskGLCommandQueue *self)
 static void
 enqueue_batch (GskGLCommandQueue *self)
 {
-  GskGLCommandBatch *prev;
   guint index;
 
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
-  g_assert (self->batches->len > 0);
+  g_assert (self->n_batches > 0);
 
-  index = self->batches->len - 1;
+  index = self->n_batches - 1;
 
   if (self->tail_batch_index != -1)
     {
-      prev = &g_array_index (self->batches, GskGLCommandBatch, self->tail_batch_index);
+      GskGLCommandBatch *prev = &self->batches[self->tail_batch_index];
+
       prev->any.next_batch_index = index;
     }
 
@@ -337,9 +340,9 @@ static void
 discard_batch (GskGLCommandQueue *self)
 {
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
-  g_assert (self->batches->len > 0);
+  g_assert (self->n_batches > 0);
 
-  self->batches->len--;
+  self->n_batches--;
 }
 
 void
@@ -397,10 +400,10 @@ gsk_gl_command_queue_end_draw (GskGLCommandQueue *self)
   guint n_changed;
 
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
-  g_assert (self->batches->len > 0);
+  g_assert (self->n_batches > 0);
   g_assert (self->in_draw == TRUE);
 
-  batch = &g_array_index (self->batches, GskGLCommandBatch, self->batches->len - 1);
+  batch = &self->batches[self->n_batches - 1];
 
   g_assert (batch->any.kind == GSK_GL_COMMAND_KIND_DRAW);
 
@@ -469,8 +472,8 @@ gsk_gl_command_queue_end_draw (GskGLCommandQueue *self)
         }
     }
 
-  if (self->batches->len > 1)
-    last_batch = &g_array_index (self->batches, GskGLCommandBatch, self->batches->len - 2);
+  if (self->n_batches > 1)
+    last_batch = &self->batches[self->n_batches - 2];
   else
     last_batch = NULL;
 
@@ -520,12 +523,12 @@ gsk_gl_command_queue_split_draw (GskGLCommandQueue *self)
   graphene_rect_t viewport;
 
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
-  g_assert (self->batches->len > 0);
+  g_assert (self->n_batches > 0);
   g_assert (self->in_draw == TRUE);
 
   program = self->program_info;
 
-  batch = &g_array_index (self->batches, GskGLCommandBatch, self->batches->len-1);
+  batch = &self->batches[self->n_batches - 1];
 
   g_assert (batch->any.kind == GSK_GL_COMMAND_KIND_DRAW);
 
@@ -547,7 +550,7 @@ gsk_gl_command_queue_clear (GskGLCommandQueue     *self,
 
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
   g_assert (self->in_draw == FALSE);
-  g_assert (self->batches->len < G_MAXINT);
+  g_assert (self->n_batches < G_MAXINT);
 
   if (clear_bits == 0)
     clear_bits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
@@ -575,7 +578,7 @@ gsk_gl_command_queue_push_debug_group (GskGLCommandQueue *self,
 
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
   g_assert (self->in_draw == FALSE);
-  g_assert (self->batches->len < G_MAXINT);
+  g_assert (self->n_batches < G_MAXINT);
 
   batch = begin_next_batch (self);
   batch->any.kind = GSK_GL_COMMAND_KIND_PUSH_DEBUG_GROUP;
@@ -595,7 +598,7 @@ gsk_gl_command_queue_pop_debug_group (GskGLCommandQueue *self)
 
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
   g_assert (self->in_draw == FALSE);
-  g_assert (self->batches->len < G_MAXINT);
+  g_assert (self->n_batches < G_MAXINT);
 
   batch = begin_next_batch (self);
   batch->any.kind = GSK_GL_COMMAND_KIND_POP_DEBUG_GROUP;
@@ -813,7 +816,7 @@ gsk_gl_command_queue_execute (GskGLCommandQueue    *self,
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
   g_assert (self->in_draw == FALSE);
 
-  if (self->batches->len == 0)
+  if (self->n_batches == 0)
     return;
 
   gsk_gl_command_queue_make_current (self);
@@ -868,8 +871,10 @@ gsk_gl_command_queue_execute (GskGLCommandQueue    *self,
 
   while (next_batch_index >= 0)
     {
-      const GskGLCommandBatch *batch = &g_array_index (self->batches, GskGLCommandBatch, next_batch_index);
+      const GskGLCommandBatch *batch = &self->batches[next_batch_index];
 
+      g_assert (next_batch_index >= 0);
+      g_assert (next_batch_index < self->n_batches);
       g_assert (batch->any.next_batch_index != next_batch_index);
 
       count++;
@@ -1010,7 +1015,7 @@ void
 gsk_gl_command_queue_begin_frame (GskGLCommandQueue *self)
 {
   g_assert (GSK_IS_GL_COMMAND_QUEUE (self));
-  g_assert (self->batches->len == 0);
+  g_assert (self->n_batches == 0);
 
   gsk_gl_command_queue_make_current (self);
 
@@ -1056,7 +1061,7 @@ gsk_gl_command_queue_end_frame (GskGLCommandQueue *self)
 
   g_string_chunk_clear (self->debug_groups);
 
-  self->batches->len = 0;
+  self->n_batches = 0;
   self->batch_draws->len = 0;
   self->batch_uniforms->len = 0;
   self->batch_binds->len = 0;
diff --git a/gsk/next/gskglcommandqueueprivate.h b/gsk/next/gskglcommandqueueprivate.h
index b073556d6f..d2dc5aa06d 100644
--- a/gsk/next/gskglcommandqueueprivate.h
+++ b/gsk/next/gskglcommandqueueprivate.h
@@ -177,7 +177,9 @@ struct _GskGLCommandQueue
    * together. The idea here is that we reduce the need for pointers so that
    * using g_realloc()'d arrays is fine.
    */
-  GArray *batches;
+  GskGLCommandBatch *batches;
+  gsize batches_len;
+  gsize n_batches;
 
   /* Contains array of vertices and some wrapper code to help upload them
    * to the GL driver. We can also tweak this to use double buffered arrays
@@ -312,12 +314,8 @@ void               gsk_gl_command_queue_split_draw           (GskGLCommandQueue
 static inline GskGLDrawVertex *
 gsk_gl_command_queue_add_vertices (GskGLCommandQueue *self)
 {
-  GskGLCommandBatch *batch = &g_array_index (self->batches, GskGLCommandBatch, self->batches->len - 1);
-  GskGLDrawVertex *dest = gsk_gl_buffer_advance (&self->vertices, GSK_GL_N_VERTICES);
-
-  batch->draw.vbo_count += GSK_GL_N_VERTICES;
-
-  return dest;
+  self->batches[self->n_batches - 1].draw.vbo_count += GSK_GL_N_VERTICES;
+  return gsk_gl_buffer_advance (&self->vertices, GSK_GL_N_VERTICES);
 }
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]