[gtk/wip/chergert/macos-iosurface: 1/7] gsk/gl: support non-standard default framebuffer




commit 02c99a9ee310750c4571bb8fc50fd11a0a9ab7b1
Author: Christian Hergert <christian hergert me>
Date:   Thu Feb 10 21:51:15 2022 -0800

    gsk/gl: support non-standard default framebuffer
    
    There are situations where our "default framebuffer" is not actually
    zero, yet we still want to apply a scissor rect.
    
    Generally, 0 is the default framebuffer. But on platforms where we need
    to bind a platform-specific feature to a GL_FRAMEBUFFER, we might have a
    default that is not 0. For example, on macOS we bind an IOSurfaceRef to
    a GL_TEXTURE_RECTANGLE which then is assigned as the backing store for a
    framebuffer. This is different than using gsk_gl_renderer_render_texture()
    in that we don't want to incur an extra copy to the destination surface
    nor do we even have a way to pass a texture_id into render_texture().

 gsk/gl/gskglcommandqueue.c        | 29 ++++++++++++++++++++---------
 gsk/gl/gskglcommandqueueprivate.h |  3 ++-
 gsk/gl/gskglrenderjob.c           | 28 ++++++++++++++++++++++++++--
 3 files changed, 48 insertions(+), 12 deletions(-)
---
diff --git a/gsk/gl/gskglcommandqueue.c b/gsk/gl/gskglcommandqueue.c
index 3af8c1eb09..266a158822 100644
--- a/gsk/gl/gskglcommandqueue.c
+++ b/gsk/gl/gskglcommandqueue.c
@@ -675,9 +675,9 @@ gsk_gl_command_queue_split_draw (GskGLCommandQueue *self)
 }
 
 void
-gsk_gl_command_queue_clear (GskGLCommandQueue    *self,
-                             guint                  clear_bits,
-                             const graphene_rect_t *viewport)
+gsk_gl_command_queue_clear (GskGLCommandQueue     *self,
+                            guint                  clear_bits,
+                            const graphene_rect_t *viewport)
 {
   GskGLCommandBatch *batch;
 
@@ -750,11 +750,12 @@ static inline void
 apply_scissor (gboolean              *state,
                guint                  framebuffer,
                const graphene_rect_t *scissor,
-               gboolean               has_scissor)
+               gboolean               has_scissor,
+               guint                  default_framebuffer)
 {
   g_assert (framebuffer != (guint)-1);
 
-  if (framebuffer != 0 || !has_scissor)
+  if (framebuffer != default_framebuffer || !has_scissor)
     {
       if (*state != FALSE)
         {
@@ -935,15 +936,25 @@ gsk_gl_command_queue_sort_batches (GskGLCommandQueue *self)
  * @self: a `GskGLCommandQueue`
  * @surface_height: the height of the backing surface
  * @scale_factor: the scale factor of the backing surface
- * #scissor: (nullable): the scissor clip if any
+ * @scissor: (nullable): the scissor clip if any
+ * @default_framebuffer: the default framebuffer id if not zero
  *
  * Executes all of the batches in the command queue.
+ *
+ * Typically, the scissor rect is only applied id rendering to the
+ * default framebuffer which is zero in most cases. However, if
+ * default_framebuffer is not zero, it will be checked to see if the
+ * rendering target matches so that the scissor rect is applied. This
+ * should be used in cases where rendering to the backbuffer for display
+ * is not the default GL framebuffer of zero. The current example of
+ * this is rendering to IOSurface on macOS.
  */
 void
 gsk_gl_command_queue_execute (GskGLCommandQueue    *self,
                               guint                 surface_height,
                               guint                 scale_factor,
-                              const cairo_region_t *scissor)
+                              const cairo_region_t *scissor,
+                              guint                 default_framebuffer)
 {
   G_GNUC_UNUSED guint count = 0;
   graphene_rect_t scissor_test;
@@ -1049,7 +1060,7 @@ gsk_gl_command_queue_execute (GskGLCommandQueue    *self,
         case GSK_GL_COMMAND_KIND_CLEAR:
           if (apply_framebuffer (&framebuffer, batch->clear.framebuffer))
             {
-              apply_scissor (&scissor_state, framebuffer, &scissor_test, has_scissor);
+              apply_scissor (&scissor_state, framebuffer, &scissor_test, has_scissor, default_framebuffer);
               n_fbos++;
             }
 
@@ -1073,7 +1084,7 @@ gsk_gl_command_queue_execute (GskGLCommandQueue    *self,
 
           if (apply_framebuffer (&framebuffer, batch->draw.framebuffer))
             {
-              apply_scissor (&scissor_state, framebuffer, &scissor_test, has_scissor);
+              apply_scissor (&scissor_state, framebuffer, &scissor_test, has_scissor, default_framebuffer);
               n_fbos++;
             }
 
diff --git a/gsk/gl/gskglcommandqueueprivate.h b/gsk/gl/gskglcommandqueueprivate.h
index 4147283e6d..48911305a8 100644
--- a/gsk/gl/gskglcommandqueueprivate.h
+++ b/gsk/gl/gskglcommandqueueprivate.h
@@ -278,7 +278,8 @@ void                gsk_gl_command_queue_end_frame            (GskGLCommandQueue
 void                gsk_gl_command_queue_execute              (GskGLCommandQueue    *self,
                                                                guint                 surface_height,
                                                                guint                 scale_factor,
-                                                               const cairo_region_t *scissor);
+                                                               const cairo_region_t *scissor,
+                                                               guint                 default_framebuffer);
 int                 gsk_gl_command_queue_upload_texture       (GskGLCommandQueue    *self,
                                                                GdkTexture           *texture,
                                                                int                   min_filter,
diff --git a/gsk/gl/gskglrenderjob.c b/gsk/gl/gskglrenderjob.c
index 0efa8ae108..236b3439fc 100644
--- a/gsk/gl/gskglrenderjob.c
+++ b/gsk/gl/gskglrenderjob.c
@@ -123,6 +123,7 @@ struct _GskGLRenderJob
    * GL context.
    */
   guint framebuffer;
+  guint default_framebuffer;
 
   /* The viewport we are using. This state is updated as we process render
    * nodes in the specific visitor callbacks.
@@ -4053,7 +4054,7 @@ gsk_gl_render_job_render_flipped (GskGLRenderJob *job,
   gsk_gl_render_job_end_draw (job);
 
   gdk_gl_context_push_debug_group (job->command_queue->context, "Executing command queue");
-  gsk_gl_command_queue_execute (job->command_queue, surface_height, 1, NULL);
+  gsk_gl_command_queue_execute (job->command_queue, surface_height, 1, NULL, job->default_framebuffer);
   gdk_gl_context_pop_debug_group (job->command_queue->context);
 
   glDeleteFramebuffers (1, &framebuffer_id);
@@ -4102,7 +4103,7 @@ gsk_gl_render_job_render (GskGLRenderJob *job,
   start_time = GDK_PROFILER_CURRENT_TIME;
   gsk_gl_command_queue_make_current (job->command_queue);
   gdk_gl_context_push_debug_group (job->command_queue->context, "Executing command queue");
-  gsk_gl_command_queue_execute (job->command_queue, surface_height, scale_factor, job->region);
+  gsk_gl_command_queue_execute (job->command_queue, surface_height, scale_factor, job->region, 
job->default_framebuffer);
   gdk_gl_context_pop_debug_group (job->command_queue->context);
   gdk_profiler_add_mark (start_time, GDK_PROFILER_CURRENT_TIME-start_time, "Execute GL command queue", "");
 }
@@ -4150,17 +4151,40 @@ gsk_gl_render_job_new (GskGLDriver           *driver,
   const graphene_rect_t *clip_rect = viewport;
   graphene_rect_t transformed_extents;
   GskGLRenderJob *job;
+  GLint default_framebuffer = 0;
 
   g_return_val_if_fail (GSK_IS_GL_DRIVER (driver), NULL);
   g_return_val_if_fail (viewport != NULL, NULL);
   g_return_val_if_fail (scale_factor > 0, NULL);
 
+  /* Check for non-standard framebuffer binding as we might not be using
+   * the default framebuffer on systems like macOS where we've bound an
+   * IOSurface to a GL_TEXTURE_RECTANGLE. Otherwise, no scissor clip will
+   * be applied in the command queue causing overdrawing.
+   */
+  if (framebuffer == 0)
+    {
+      GLint framebuffer_type = 0;
+
+      glGetFramebufferAttachmentParameteriv (GL_FRAMEBUFFER,
+                                             GL_COLOR_ATTACHMENT0,
+                                             GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
+                                             &framebuffer_type);
+      if (framebuffer_type != GL_NONE)
+        glGetFramebufferAttachmentParameteriv (GL_FRAMEBUFFER,
+                                               GL_COLOR_ATTACHMENT0,
+                                               GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
+                                               &default_framebuffer);
+      framebuffer = default_framebuffer;
+    }
+
   job = g_slice_new0 (GskGLRenderJob);
   job->driver = g_object_ref (driver);
   job->command_queue = job->driver->command_queue;
   job->clip = g_array_sized_new (FALSE, FALSE, sizeof (GskGLRenderClip), 16);
   job->modelview = g_array_sized_new (FALSE, FALSE, sizeof (GskGLRenderModelview), 16);
   job->framebuffer = framebuffer;
+  job->default_framebuffer = default_framebuffer;
   job->offset_x = 0;
   job->offset_y = 0;
   job->scale_x = scale_factor;


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