Blame SOURCES/0003-pixel-buffer-Add-the-concept-of-device-rotation.patch

a802d9
From 0e4e268844ea38075535eb5b233dda325da4481d Mon Sep 17 00:00:00 2001
a802d9
From: Hans de Goede <hdegoede@redhat.com>
a802d9
Date: Wed, 6 Dec 2017 17:37:12 +0100
a802d9
Subject: [PATCH 3/6] pixel-buffer: Add the concept of device rotation
a802d9
a802d9
On some devices the LCD panel is mounted in the casing in such a way
a802d9
that the up/top side of the panel does not match with the top side of
a802d9
the device (e.g. it is mounted upside-down).
a802d9
a802d9
This commit adds support to the ply-pixel-buffer code to create
a802d9
buffers which take device rotation into account and which will rotate
a802d9
the picture to compensate.
a802d9
a802d9
https://bugs.freedesktop.org/show_bug.cgi?id=104714
a802d9
---
a802d9
 src/libply-splash-core/ply-pixel-buffer.c | 109 ++++++++++++++++++++--
a802d9
 src/libply-splash-core/ply-pixel-buffer.h |   9 ++
a802d9
 2 files changed, 110 insertions(+), 8 deletions(-)
a802d9
a802d9
diff --git a/src/libply-splash-core/ply-pixel-buffer.c b/src/libply-splash-core/ply-pixel-buffer.c
a802d9
index 52a3f86..a337407 100644
a802d9
--- a/src/libply-splash-core/ply-pixel-buffer.c
a802d9
+++ b/src/libply-splash-core/ply-pixel-buffer.c
a802d9
@@ -50,6 +50,7 @@ struct _ply_pixel_buffer
a802d9
         ply_region_t   *updated_areas; /* in device pixels */
a802d9
         uint32_t        is_opaque : 1;
a802d9
         int             device_scale;
a802d9
+        int             device_rotation;
a802d9
 };
a802d9
 
a802d9
 static inline void ply_pixel_buffer_blend_value_at_pixel (ply_pixel_buffer_t *buffer,
a802d9
@@ -153,6 +154,52 @@ make_pixel_value_translucent (uint32_t pixel_value,
a802d9
         return (alpha << 24) | (red << 16) | (green << 8) | blue;
a802d9
 }
a802d9
 
a802d9
+static inline void ply_pixel_buffer_set_pixel (ply_pixel_buffer_t *buffer,
a802d9
+                                               int                 x,
a802d9
+                                               int                 y,
a802d9
+                                               uint32_t            pixel_value)
a802d9
+{
a802d9
+        switch (buffer->device_rotation) {
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_UPRIGHT:
a802d9
+                buffer->bytes[y * buffer->area.width + x] = pixel_value;
a802d9
+                break;
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_UPSIDE_DOWN:
a802d9
+                x = (buffer->area.width - 1) - x;
a802d9
+                y = (buffer->area.height - 1) - y;
a802d9
+                buffer->bytes[y * buffer->area.width + x] = pixel_value;
a802d9
+                break;
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_CLOCKWISE:
a802d9
+                y = (buffer->area.height - 1) - y;
a802d9
+                buffer->bytes[x * buffer->area.height + y] = pixel_value;
a802d9
+                break;
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_COUNTER_CLOCKWISE:
a802d9
+                x = (buffer->area.width - 1) - x;
a802d9
+                buffer->bytes[x * buffer->area.height + y] = pixel_value;
a802d9
+                break;
a802d9
+        }
a802d9
+}
a802d9
+
a802d9
+static inline uint32_t ply_pixel_buffer_get_pixel (ply_pixel_buffer_t *buffer,
a802d9
+                                                   int                 x,
a802d9
+                                                   int                 y)
a802d9
+{
a802d9
+        switch (buffer->device_rotation) {
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_UPRIGHT:
a802d9
+                return buffer->bytes[y * buffer->area.width + x];
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_UPSIDE_DOWN:
a802d9
+                x = (buffer->area.width - 1) - x;
a802d9
+                y = (buffer->area.height - 1) - y;
a802d9
+                return buffer->bytes[y * buffer->area.width + x];
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_CLOCKWISE:
a802d9
+                y = (buffer->area.height - 1) - y;
a802d9
+                return buffer->bytes[x * buffer->area.height + y];
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_COUNTER_CLOCKWISE:
a802d9
+                x = (buffer->area.width - 1) - x;
a802d9
+                return buffer->bytes[x * buffer->area.height + y];
a802d9
+        }
a802d9
+        return 0;
a802d9
+}
a802d9
+
a802d9
 static inline void
a802d9
 ply_pixel_buffer_blend_value_at_pixel (ply_pixel_buffer_t *buffer,
a802d9
                                        int                 x,
a802d9
@@ -162,12 +209,12 @@ ply_pixel_buffer_blend_value_at_pixel (ply_pixel_buffer_t *buffer,
a802d9
         uint32_t old_pixel_value;
a802d9
 
a802d9
         if ((pixel_value >> 24) != 0xff) {
a802d9
-                old_pixel_value = buffer->bytes[y * buffer->area.width + x];
a802d9
+                old_pixel_value = ply_pixel_buffer_get_pixel (buffer, x, y);
a802d9
 
a802d9
                 pixel_value = blend_two_pixel_values (pixel_value, old_pixel_value);
a802d9
         }
a802d9
 
a802d9
-        buffer->bytes[y * buffer->area.width + x] = pixel_value;
a802d9
+        ply_pixel_buffer_set_pixel (buffer, x, y, pixel_value);
a802d9
 }
a802d9
 
a802d9
 static void
a802d9
@@ -222,6 +269,35 @@ ply_pixel_buffer_crop_area_to_clip_area (ply_pixel_buffer_t *buffer,
a802d9
         }
a802d9
 }
a802d9
 
a802d9
+static void ply_pixel_buffer_add_updated_area (ply_pixel_buffer_t *buffer,
a802d9
+                                               ply_rectangle_t    *area)
a802d9
+{
a802d9
+        ply_rectangle_t updated_area = *area;
a802d9
+
a802d9
+        switch (buffer->device_rotation) {
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_UPRIGHT:
a802d9
+                break;
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_UPSIDE_DOWN:
a802d9
+                updated_area.x = buffer->area.width - area->width - area->x;
a802d9
+                updated_area.y = buffer->area.height - area->height - area->y;
a802d9
+                break;
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_CLOCKWISE:
a802d9
+                updated_area.x = buffer->area.height - area->height - area->y;
a802d9
+                updated_area.y = area->x;
a802d9
+                updated_area.height = area->width;
a802d9
+                updated_area.width = area->height;
a802d9
+                break;
a802d9
+        case PLY_PIXEL_BUFFER_ROTATE_COUNTER_CLOCKWISE:
a802d9
+                updated_area.x = area->y;
a802d9
+                updated_area.y = buffer->area.width - area->width - area->x;
a802d9
+                updated_area.height = area->width;
a802d9
+                updated_area.width = area->height;
a802d9
+                break;
a802d9
+        }
a802d9
+
a802d9
+        ply_region_add_rectangle (buffer->updated_areas, &updated_area);
a802d9
+}
a802d9
+
a802d9
 static void
a802d9
 ply_pixel_buffer_fill_area_with_pixel_value (ply_pixel_buffer_t *buffer,
a802d9
                                              ply_rectangle_t    *fill_area,
a802d9
@@ -251,7 +327,7 @@ ply_pixel_buffer_fill_area_with_pixel_value (ply_pixel_buffer_t *buffer,
a802d9
                 }
a802d9
         }
a802d9
 
a802d9
-        ply_region_add_rectangle (buffer->updated_areas, &cropped_area);
a802d9
+        ply_pixel_buffer_add_updated_area (buffer, &cropped_area);
a802d9
 }
a802d9
 
a802d9
 void
a802d9
@@ -281,9 +357,24 @@ ply_pixel_buffer_pop_clip_area (ply_pixel_buffer_t *buffer)
a802d9
 ply_pixel_buffer_t *
a802d9
 ply_pixel_buffer_new (unsigned long width,
a802d9
                       unsigned long height)
a802d9
+{
a802d9
+        return ply_pixel_buffer_new_with_device_rotation (
a802d9
+                        width, height, PLY_PIXEL_BUFFER_ROTATE_UPRIGHT);
a802d9
+}
a802d9
+
a802d9
+ply_pixel_buffer_t *
a802d9
+ply_pixel_buffer_new_with_device_rotation (unsigned long width,
a802d9
+                                           unsigned long height,
a802d9
+                                           int device_rotation)
a802d9
 {
a802d9
         ply_pixel_buffer_t *buffer;
a802d9
 
a802d9
+        if (device_rotation >= PLY_PIXEL_BUFFER_ROTATE_CLOCKWISE) {
a802d9
+                unsigned long tmp = width;
a802d9
+                width = height;
a802d9
+                height = tmp;
a802d9
+        }
a802d9
+
a802d9
         buffer = calloc (1, sizeof(ply_pixel_buffer_t));
a802d9
 
a802d9
         buffer->updated_areas = ply_region_new ();
a802d9
@@ -292,6 +383,7 @@ ply_pixel_buffer_new (unsigned long width,
a802d9
         buffer->area.height = height;
a802d9
         buffer->logical_area = buffer->area;
a802d9
         buffer->device_scale = 1;
a802d9
+        buffer->device_rotation = device_rotation;
a802d9
 
a802d9
         buffer->clip_areas = ply_list_new ();
a802d9
         ply_pixel_buffer_push_clip_area (buffer, &buffer->area);
a802d9
@@ -447,7 +539,7 @@ ply_pixel_buffer_fill_with_gradient (ply_pixel_buffer_t *buffer,
a802d9
 
a802d9
         for (y = buffer->area.y; y < buffer->area.y + buffer->area.height; y++) {
a802d9
                 if (cropped_area.y <= y && y < cropped_area.y + cropped_area.height) {
a802d9
-                        if (cropped_area.width < UNROLLED_PIXEL_COUNT) {
a802d9
+                        if (cropped_area.width < UNROLLED_PIXEL_COUNT || buffer->device_rotation) {
a802d9
                                 for (x = cropped_area.x; x < cropped_area.x + cropped_area.width; x++) {
a802d9
                                         pixel = 0xff000000;
a802d9
                                         RANDOMIZE (noise);
a802d9
@@ -457,7 +549,7 @@ ply_pixel_buffer_fill_with_gradient (ply_pixel_buffer_t *buffer,
a802d9
                                         RANDOMIZE (noise);
a802d9
                                         pixel |= (((blue + noise) & COLOR_MASK) >> BLUE_SHIFT);
a802d9
 
a802d9
-                                        buffer->bytes[y * buffer->area.width + x] = pixel;
a802d9
+                                        ply_pixel_buffer_set_pixel (buffer, x, y, pixel);
a802d9
                                 }
a802d9
                         } else {
a802d9
                                 uint32_t shaded_set[UNROLLED_PIXEL_COUNT];
a802d9
@@ -485,7 +577,7 @@ ply_pixel_buffer_fill_with_gradient (ply_pixel_buffer_t *buffer,
a802d9
                 blue += blue_step;
a802d9
         }
a802d9
 
a802d9
-        ply_region_add_rectangle (buffer->updated_areas, &cropped_area);
a802d9
+        ply_pixel_buffer_add_updated_area (buffer, &cropped_area);
a802d9
 }
a802d9
 
a802d9
 void
a802d9
@@ -671,7 +763,7 @@ ply_pixel_buffer_fill_with_argb32_data_at_opacity_with_clip_and_scale (ply_pixel
a802d9
                 }
a802d9
         }
a802d9
 
a802d9
-        ply_region_add_rectangle (buffer->updated_areas, &cropped_area);
a802d9
+        ply_pixel_buffer_add_updated_area (buffer, &cropped_area);
a802d9
 }
a802d9
 
a802d9
 void
a802d9
@@ -756,7 +848,8 @@ ply_pixel_buffer_fill_with_buffer_at_opacity_with_clip (ply_pixel_buffer_t *canv
a802d9
 
a802d9
         /* Fast path to memcpy if we need no blending or scaling */
a802d9
         if (opacity == 1.0 && ply_pixel_buffer_is_opaque (source) &&
a802d9
-            canvas->device_scale == source->device_scale) {
a802d9
+            canvas->device_scale == source->device_scale &&
a802d9
+            canvas->device_rotation == PLY_PIXEL_BUFFER_ROTATE_UPRIGHT) {
a802d9
                 ply_rectangle_t cropped_area;
a802d9
 
a802d9
                 cropped_area.x = x_offset;
a802d9
diff --git a/src/libply-splash-core/ply-pixel-buffer.h b/src/libply-splash-core/ply-pixel-buffer.h
a802d9
index 595e9bd..7736dd3 100644
a802d9
--- a/src/libply-splash-core/ply-pixel-buffer.h
a802d9
+++ b/src/libply-splash-core/ply-pixel-buffer.h
a802d9
@@ -37,9 +37,18 @@ typedef struct _ply_pixel_buffer ply_pixel_buffer_t;
a802d9
          | ((uint8_t) (CLAMP (g * 255.0, 0.0, 255.0)) << 8)                      \
a802d9
          | ((uint8_t) (CLAMP (b * 255.0, 0.0, 255.0))))
a802d9
 
a802d9
+#define PLY_PIXEL_BUFFER_ROTATE_UPRIGHT             0
a802d9
+#define PLY_PIXEL_BUFFER_ROTATE_UPSIDE_DOWN         1
a802d9
+#define PLY_PIXEL_BUFFER_ROTATE_CLOCKWISE           2
a802d9
+#define PLY_PIXEL_BUFFER_ROTATE_COUNTER_CLOCKWISE   3
a802d9
+
a802d9
 #ifndef PLY_HIDE_FUNCTION_DECLARATIONS
a802d9
 ply_pixel_buffer_t *ply_pixel_buffer_new (unsigned long width,
a802d9
                                           unsigned long height);
a802d9
+ply_pixel_buffer_t *
a802d9
+ply_pixel_buffer_new_with_device_rotation (unsigned long width,
a802d9
+                                           unsigned long height,
a802d9
+                                           int device_rotation);
a802d9
 void ply_pixel_buffer_free (ply_pixel_buffer_t *buffer);
a802d9
 void ply_pixel_buffer_get_size (ply_pixel_buffer_t *buffer,
a802d9
                                 ply_rectangle_t    *size);
a802d9
-- 
a802d9
2.17.0
a802d9