[Cogl] [PATCH] onscreen: Adds swap_buffers_with_damage api

Robert Bragg robert at sixbynine.org
Thu Apr 25 09:36:53 PDT 2013


From: Robert Bragg <robert at linux.intel.com>

This adds api to be able requests a swap_buffers and also pass a list of
damage rectangles that can be passed on to a compositor to enable it to
minimize how much of the screen it needs to recompose.
---
 cogl/cogl-onscreen.c                            | 19 +++++++++-
 cogl/cogl-onscreen.h                            | 49 +++++++++++++++++++++++++
 cogl/winsys/cogl-winsys-egl-feature-functions.h | 13 +++++++
 cogl/winsys/cogl-winsys-egl-kms.c               | 29 +++++++++++++--
 cogl/winsys/cogl-winsys-egl-wayland.c           | 27 +++++++++++++-
 cogl/winsys/cogl-winsys-egl.c                   | 41 ++++++++++++++++++++-
 cogl/winsys/cogl-winsys-private.h               |  5 +++
 doc/reference/cogl2/cogl2-sections.txt          |  1 +
 8 files changed, 175 insertions(+), 9 deletions(-)

diff --git a/cogl/cogl-onscreen.c b/cogl/cogl-onscreen.c
index 338f747..017f11c 100644
--- a/cogl/cogl-onscreen.c
+++ b/cogl/cogl-onscreen.c
@@ -132,7 +132,9 @@ _cogl_onscreen_queue_event (CoglOnscreen *onscreen,
 }
 
 void
-cogl_onscreen_swap_buffers (CoglOnscreen *onscreen)
+cogl_onscreen_swap_buffers_with_damage (CoglOnscreen *onscreen,
+                                        const int *rectangles,
+                                        int n_rectangles)
 {
   CoglFramebuffer *framebuffer = COGL_FRAMEBUFFER (onscreen);
   const CoglWinsysVtable *winsys;
@@ -147,7 +149,14 @@ cogl_onscreen_swap_buffers (CoglOnscreen *onscreen)
   _cogl_framebuffer_flush_journal (framebuffer);
 
   winsys = _cogl_framebuffer_get_winsys (framebuffer);
-  winsys->onscreen_swap_buffers (COGL_ONSCREEN (framebuffer));
+
+  if (winsys->onscreen_swap_buffers_with_damage)
+    winsys->onscreen_swap_buffers_with_damage (onscreen,
+                                               rectangles,
+                                               n_rectangles);
+  else
+    winsys->onscreen_swap_buffers (onscreen);
+
   cogl_framebuffer_discard_buffers (framebuffer,
                                     COGL_BUFFER_BIT_COLOR |
                                     COGL_BUFFER_BIT_DEPTH |
@@ -171,6 +180,12 @@ cogl_onscreen_swap_buffers (CoglOnscreen *onscreen)
 }
 
 void
+cogl_onscreen_swap_buffers (CoglOnscreen *onscreen)
+{
+  cogl_onscreen_swap_buffers_with_damage (onscreen, NULL, 0);
+}
+
+void
 cogl_onscreen_swap_region (CoglOnscreen *onscreen,
                            const int *rectangles,
                            int n_rectangles)
diff --git a/cogl/cogl-onscreen.h b/cogl/cogl-onscreen.h
index ed4e25c..526befe 100644
--- a/cogl/cogl-onscreen.h
+++ b/cogl/cogl-onscreen.h
@@ -381,6 +381,55 @@ int
 cogl_onscreen_get_buffer_age (CoglOnscreen *onscreen);
 
 /**
+ * cogl_onscreen_swap_buffers_with_damage:
+ * @onscreen: A #CoglOnscreen framebuffer
+ * @rectangles: An array of integer 4-tuples representing damaged
+ *              rectangles as (x, y, width, height) tuples.
+ * @n_rectangles: The number of 4-tuples to be read from @rectangles
+ *
+ * Swaps the current back buffer being rendered too, to the front for
+ * display and provides information to any system compositor about
+ * what regions of the buffer have changed with respect to the last
+ * swapped buffer.
+ *
+ * This function also implicitly discards the contents of the color,
+ * depth and stencil buffers as if cogl_framebuffer_discard_buffers()
+ * were used. The significance of the discard is that you should not
+ * expect to be able to start a new frame that incrementally builds on
+ * the contents of the previous frame. If you want to perform
+ * incremental updates to older back buffers then please refer to the
+ * cogl_onscreen_get_buffer_age() api.
+ *
+ * This function has the same semantics as
+ * cogl_framebuffer_swap_buffers() except that it additionally allows
+ * applications to pass a list of damaged rectangles which may be
+ * passed on to a compositor so that it can minimize how much of the
+ * screen is redrawn in response to this applications newly swapped
+ * front buffer.
+ *
+ * For example if your application is only animating a small object in
+ * the corner of the screen and everything else is remaining static
+ * then it can help the compositor to know that only the bottom right
+ * corner of your newly swapped buffer has really changed with respect
+ * to your previously swapped front buffer.
+ *
+ * Whenever possible it is recommended that applications use this
+ * function instead of cogl_onscreen_swap_buffers() to improve
+ * performance when running under a compositor.
+ *
+ * <note>It is highly recommended to use this API in conjunction with
+ * the cogl_onscreen_get_buffer_age() api so that your application can
+ * perform incremental rendering based on old back buffers.</note>
+ *
+ * Since: 1.16
+ * Stability: unstable
+ */
+void
+cogl_onscreen_swap_buffers_with_damage (CoglOnscreen *onscreen,
+                                        const int *rectangles,
+                                        int n_rectangles);
+
+/**
  * cogl_onscreen_swap_region:
  * @onscreen: A #CoglOnscreen framebuffer
  * @rectangles: An array of integer 4-tuples representing rectangles as
diff --git a/cogl/winsys/cogl-winsys-egl-feature-functions.h b/cogl/winsys/cogl-winsys-egl-feature-functions.h
index 5068090..cd531fa 100644
--- a/cogl/winsys/cogl-winsys-egl-feature-functions.h
+++ b/cogl/winsys/cogl-winsys-egl-feature-functions.h
@@ -101,3 +101,16 @@ COGL_WINSYS_FEATURE_BEGIN (buffer_age,
 COGL_WINSYS_FEATURE_END ()
 
 #endif
+
+#ifdef EGL_EXT_swap_buffers_with_damage
+COGL_WINSYS_FEATURE_BEGIN (swap_buffers_with_damage,
+                           "EXT\0",
+                           "swap_buffers_with_damage\0",
+                           0)
+COGL_WINSYS_FEATURE_FUNCTION (EGLBoolean, eglSwapBuffersWithDamage,
+                              (EGLDisplay dpy,
+                               EGLSurface surface,
+                               const EGLint *rects,
+                               EGLint n_rects))
+COGL_WINSYS_FEATURE_END ()
+#endif
diff --git a/cogl/winsys/cogl-winsys-egl-kms.c b/cogl/winsys/cogl-winsys-egl-kms.c
index 76f0990..8729234 100644
--- a/cogl/winsys/cogl-winsys-egl-kms.c
+++ b/cogl/winsys/cogl-winsys-egl-kms.c
@@ -654,7 +654,9 @@ handle_drm_event (CoglRendererKMS *kms_renderer)
 }
 
 static void
-_cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
+_cogl_winsys_onscreen_swap_buffers_real (CoglOnscreen *onscreen,
+                                         const int *rectangles,
+                                         int n_rectangles)
 {
   CoglContext *context = COGL_FRAMEBUFFER (onscreen)->context;
   CoglDisplayEGL *egl_display = context->display->winsys;
@@ -672,8 +674,13 @@ _cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
   while (kms_onscreen->next_fb_id != 0)
     handle_drm_event (kms_renderer);
 
-  /* First chain-up. This will call eglSwapBuffers */
-  parent_vtable->onscreen_swap_buffers (onscreen);
+  /* First chain-up. */
+  if (rectangles == NULL)
+    parent_vtable->onscreen_swap_buffers (onscreen);
+  else
+    parent_vtable->onscreen_swap_buffers_with_damage (onscreen,
+                                                      rectangles,
+                                                      n_rectangles);
 
   /* Now we need to set the CRTC to whatever is the front buffer */
   kms_onscreen->next_bo = gbm_surface_lock_front_buffer (kms_onscreen->surface);
@@ -748,6 +755,20 @@ _cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
     }
 }
 
+static void
+_cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
+{
+  _cogl_winsys_onscreen_swap_buffers_real (onscreen, NULL, 0);
+}
+
+static void
+_cogl_winsys_onscreen_swap_buffers_with_damage (CoglOnscreen *onscreen,
+                                                const int *rectangles,
+                                                int n_rectangles)
+{
+  _cogl_winsys_onscreen_swap_buffers_real (onscreen, rectangles, n_rectangles);
+}
+
 static CoglBool
 _cogl_winsys_egl_context_init (CoglContext *context,
                                CoglError **error)
@@ -970,6 +991,8 @@ _cogl_winsys_egl_kms_get_vtable (void)
       /* The KMS winsys doesn't support swap region */
       vtable.onscreen_swap_region = NULL;
       vtable.onscreen_swap_buffers = _cogl_winsys_onscreen_swap_buffers;
+      vtable.onscreen_swap_buffers_with_damage =
+        _cogl_winsys_onscreen_swap_buffers_with_damage;
 
       vtable.poll_get_info = _cogl_winsys_poll_get_info;
       vtable.poll_dispatch = _cogl_winsys_poll_dispatch;
diff --git a/cogl/winsys/cogl-winsys-egl-wayland.c b/cogl/winsys/cogl-winsys-egl-wayland.c
index d7f7dc0..e44b91b 100644
--- a/cogl/winsys/cogl-winsys-egl-wayland.c
+++ b/cogl/winsys/cogl-winsys-egl-wayland.c
@@ -384,7 +384,9 @@ _cogl_winsys_egl_onscreen_deinit (CoglOnscreen *onscreen)
 }
 
 static void
-_cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
+_cogl_winsys_onscreen_swap_buffers_real (CoglOnscreen *onscreen,
+                                         const int *rectangles,
+                                         int n_rectangles)
 {
   CoglFramebuffer *fb = COGL_FRAMEBUFFER (onscreen);
   CoglContext *context = fb->context;
@@ -409,7 +411,12 @@ _cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
     }
 
   /* chain-up */
-  parent_vtable->onscreen_swap_buffers (onscreen);
+  if (rectangles == NULL)
+    parent_vtable->onscreen_swap_buffers (onscreen);
+  else
+    parent_vtable->onscreen_swap_buffers_with_damage (onscreen,
+                                                      rectangles,
+                                                      n_rectangles);
 
   /*
    * The implementation of eglSwapBuffers may do a flush however the semantics
@@ -420,6 +427,20 @@ _cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
   wl_display_flush (wayland_renderer->wayland_display);
 }
 
+static void
+_cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
+{
+  _cogl_winsys_onscreen_swap_buffers_real (onscreen, NULL, 0);
+}
+
+static void
+_cogl_winsys_onscreen_swap_buffers_with_damage (CoglOnscreen *onscreen,
+                                                const int *rectangles,
+                                                int n_rectangles)
+{
+  _cogl_winsys_onscreen_swap_buffers_real (onscreen, rectangles, n_rectangles);
+}
+
 void
 cogl_wayland_renderer_set_foreign_display (CoglRenderer *renderer,
                                            struct wl_display *display)
@@ -639,6 +660,8 @@ _cogl_winsys_egl_wayland_get_vtable (void)
       vtable.renderer_disconnect = _cogl_winsys_renderer_disconnect;
 
       vtable.onscreen_swap_buffers = _cogl_winsys_onscreen_swap_buffers;
+      vtable.onscreen_swap_buffers_with_damage =
+        _cogl_winsys_onscreen_swap_buffers_with_damage;
 
       vtable.poll_get_info = _cogl_winsys_poll_get_info;
       vtable.poll_dispatch = _cogl_winsys_poll_dispatch;
diff --git a/cogl/winsys/cogl-winsys-egl.c b/cogl/winsys/cogl-winsys-egl.c
index f9e3ae4..3af67ce 100644
--- a/cogl/winsys/cogl-winsys-egl.c
+++ b/cogl/winsys/cogl-winsys-egl.c
@@ -784,7 +784,9 @@ _cogl_winsys_onscreen_swap_region (CoglOnscreen *onscreen,
 }
 
 static void
-_cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
+_cogl_winsys_onscreen_swap_buffers_with_damage (CoglOnscreen *onscreen,
+                                                const int *rectangles,
+                                                int n_rectangles)
 {
   CoglContext *context = COGL_FRAMEBUFFER (onscreen)->context;
   CoglRenderer *renderer = context->display->renderer;
@@ -800,7 +802,40 @@ _cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
                                  COGL_FRAMEBUFFER (onscreen),
                                  COGL_FRAMEBUFFER_STATE_BIND);
 
-  eglSwapBuffers (egl_renderer->edpy, egl_onscreen->egl_surface);
+#ifdef EGL_EXT_swap_buffers_with_damage
+  if (egl_renderer->pf_eglSwapBuffersWithDamage)
+    {
+      CoglFramebuffer *fb = COGL_FRAMEBUFFER (onscreen);
+      size_t size = n_rectangles * sizeof (int) * 4;
+      int *flipped = alloca (size);
+      int i;
+
+      memcpy (flipped, rectangles, size);
+      for (i = 0; i < n_rectangles; i++)
+        {
+          const int *rect = rectangles + 4 * i;
+          int *flip_rect = flipped + 4 * i;
+          flip_rect[1] = fb->height - rect[1] - rect[3];
+        }
+
+      if (egl_renderer->pf_eglSwapBuffersWithDamage (egl_renderer->edpy,
+                                                     egl_onscreen->egl_surface,
+                                                     flipped,
+                                                     n_rectangles) == EGL_FALSE)
+        g_warning ("Error reported by eglSwapBuffersWithDamage");
+    }
+  else
+#endif
+    eglSwapBuffers (egl_renderer->edpy, egl_onscreen->egl_surface);
+}
+
+static void
+_cogl_winsys_onscreen_swap_buffers (CoglOnscreen *onscreen)
+{
+  CoglFramebuffer *fb = COGL_FRAMEBUFFER (onscreen);
+  int rect[] = {0, 0, fb->width, fb->height};
+
+  _cogl_winsys_onscreen_swap_buffers_with_damage (onscreen, rect, 1);
 }
 
 static void
@@ -902,6 +937,8 @@ static CoglWinsysVtable _cogl_winsys_vtable =
     .onscreen_deinit = _cogl_winsys_onscreen_deinit,
     .onscreen_bind = _cogl_winsys_onscreen_bind,
     .onscreen_swap_buffers = _cogl_winsys_onscreen_swap_buffers,
+    .onscreen_swap_buffers_with_damage =
+      _cogl_winsys_onscreen_swap_buffers_with_damage,
     .onscreen_swap_region = _cogl_winsys_onscreen_swap_region,
     .onscreen_get_buffer_age = _cogl_winsys_onscreen_get_buffer_age,
     .onscreen_update_swap_throttled =
diff --git a/cogl/winsys/cogl-winsys-private.h b/cogl/winsys/cogl-winsys-private.h
index 36676e3..e80cac5 100644
--- a/cogl/winsys/cogl-winsys-private.h
+++ b/cogl/winsys/cogl-winsys-private.h
@@ -126,6 +126,11 @@ typedef struct _CoglWinsysVtable
   (*context_get_clock_time) (CoglContext *context);
 
   void
+  (*onscreen_swap_buffers_with_damage) (CoglOnscreen *onscreen,
+                                        const int *rectangles,
+                                        int n_rectangles);
+
+  void
   (*onscreen_swap_region) (CoglOnscreen *onscreen,
                            const int *rectangles,
                            int n_rectangles);
diff --git a/doc/reference/cogl2/cogl2-sections.txt b/doc/reference/cogl2/cogl2-sections.txt
index e160a74..f6e7ee7 100644
--- a/doc/reference/cogl2/cogl2-sections.txt
+++ b/doc/reference/cogl2/cogl2-sections.txt
@@ -561,6 +561,7 @@ cogl_onscreen_remove_resize_callback
 
 <SUBSECTION>
 cogl_onscreen_swap_buffers
+cogl_onscreen_swap_buffers_with_damage
 cogl_onscreen_swap_region
 cogl_onscreen_set_swap_throttled
 </SECTION>
-- 
1.8.2.1



More information about the Cogl mailing list