Blame 0001-add-pflib-PixelFormat-conversion-library.patch

Justin M. Forbes a81953
From 09992bc6b432987ed3871dd7e4327ab6a589b865 Mon Sep 17 00:00:00 2001
Justin M. Forbes a81953
From: Gerd Hoffmann <kraxel@redhat.com>
Justin M. Forbes a81953
Date: Mon, 14 Jun 2010 09:54:27 +0200
Justin M. Forbes a81953
Subject: [PATCH 01/39] add pflib: PixelFormat conversion library.
Justin M. Forbes a81953
Justin M. Forbes a81953
---
Justin M. Forbes a81953
 Makefile.objs |    1 +
Justin M. Forbes a81953
 pflib.c       |  213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Justin M. Forbes a81953
 pflib.h       |    6 ++
Justin M. Forbes a81953
 3 files changed, 220 insertions(+), 0 deletions(-)
Justin M. Forbes a81953
 create mode 100644 pflib.c
Justin M. Forbes a81953
 create mode 100644 pflib.h
Justin M. Forbes a81953
Justin M. Forbes a81953
diff --git a/Makefile.objs b/Makefile.objs
Justin M. Forbes a81953
index dbee210..147051f 100644
Justin M. Forbes a81953
--- a/Makefile.objs
Justin M. Forbes a81953
+++ b/Makefile.objs
Justin M. Forbes a81953
@@ -84,6 +84,7 @@ common-obj-y += qemu-char.o savevm.o #aio.o
Justin M. Forbes a81953
 common-obj-y += msmouse.o ps2.o
Justin M. Forbes a81953
 common-obj-y += qdev.o qdev-properties.o
Justin M. Forbes a81953
 common-obj-y += block-migration.o
Justin M. Forbes a81953
+common-obj-y += pflib.o
Justin M. Forbes a81953
Justin M. Forbes a81953
 common-obj-$(CONFIG_BRLAPI) += baum.o
Justin M. Forbes a81953
 common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
Justin M. Forbes a81953
diff --git a/pflib.c b/pflib.c
Justin M. Forbes a81953
new file mode 100644
Justin M. Forbes a81953
index 0000000..1154d0c
Justin M. Forbes a81953
--- /dev/null
Justin M. Forbes a81953
+++ b/pflib.c
Justin M. Forbes a81953
@@ -0,0 +1,213 @@
Justin M. Forbes a81953
+/*
Justin M. Forbes a81953
+ * PixelFormat conversion library.
Justin M. Forbes a81953
+ *
Justin M. Forbes a81953
+ * Author: Gerd Hoffmann <kraxel@redhat.com>
Justin M. Forbes a81953
+ *
Justin M. Forbes a81953
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
Justin M. Forbes a81953
+ * the COPYING file in the top-level directory.
Justin M. Forbes a81953
+ *
Justin M. Forbes a81953
+ */
Justin M. Forbes a81953
+#include "qemu-common.h"
Justin M. Forbes a81953
+#include "console.h"
Justin M. Forbes a81953
+#include "pflib.h"
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+typedef struct QemuPixel QemuPixel;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+typedef void (*pf_convert)(QemuPfConv *conv,
Justin M. Forbes a81953
+                           void *dst, void *src, uint32_t cnt);
Justin M. Forbes a81953
+typedef void (*pf_convert_from)(PixelFormat *pf,
Justin M. Forbes a81953
+                                QemuPixel *dst, void *src, uint32_t cnt);
Justin M. Forbes a81953
+typedef void (*pf_convert_to)(PixelFormat *pf,
Justin M. Forbes a81953
+                              void *dst, QemuPixel *src, uint32_t cnt);
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+struct QemuPfConv {
Justin M. Forbes a81953
+    pf_convert        convert;
Justin M. Forbes a81953
+    PixelFormat       src;
Justin M. Forbes a81953
+    PixelFormat       dst;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    /* for copy_generic() */
Justin M. Forbes a81953
+    pf_convert_from   conv_from;
Justin M. Forbes a81953
+    pf_convert_to     conv_to;
Justin M. Forbes a81953
+    QemuPixel         *conv_buf;
Justin M. Forbes a81953
+    uint32_t          conv_cnt;
Justin M. Forbes a81953
+};
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+struct QemuPixel {
Justin M. Forbes a81953
+    uint8_t red;
Justin M. Forbes a81953
+    uint8_t green;
Justin M. Forbes a81953
+    uint8_t blue;
Justin M. Forbes a81953
+    uint8_t alpha;
Justin M. Forbes a81953
+};
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+/* ----------------------------------------------------------------------- */
Justin M. Forbes a81953
+/* PixelFormat -> QemuPixel conversions                                    */
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static void conv_16_to_pixel(PixelFormat *pf,
Justin M. Forbes a81953
+                             QemuPixel *dst, void *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    uint16_t *src16 = src;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    while (cnt > 0) {
Justin M. Forbes a81953
+        dst->red   = ((*src16 & pf->rmask) >> pf->rshift) << (8 - pf->rbits);
Justin M. Forbes a81953
+        dst->green = ((*src16 & pf->gmask) >> pf->gshift) << (8 - pf->gbits);
Justin M. Forbes a81953
+        dst->blue  = ((*src16 & pf->bmask) >> pf->bshift) << (8 - pf->bbits);
Justin M. Forbes a81953
+        dst->alpha = ((*src16 & pf->amask) >> pf->ashift) << (8 - pf->abits);
Justin M. Forbes a81953
+        dst++, src16++, cnt--;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+/* assumes pf->{r,g,b,a}bits == 8 */
Justin M. Forbes a81953
+static void conv_32_to_pixel_fast(PixelFormat *pf,
Justin M. Forbes a81953
+                                  QemuPixel *dst, void *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    uint32_t *src32 = src;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    while (cnt > 0) {
Justin M. Forbes a81953
+        dst->red   = (*src32 & pf->rmask) >> pf->rshift;
Justin M. Forbes a81953
+        dst->green = (*src32 & pf->gmask) >> pf->gshift;
Justin M. Forbes a81953
+        dst->blue  = (*src32 & pf->bmask) >> pf->bshift;
Justin M. Forbes a81953
+        dst->alpha = (*src32 & pf->amask) >> pf->ashift;
Justin M. Forbes a81953
+        dst++, src32++, cnt--;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static void conv_32_to_pixel_generic(PixelFormat *pf,
Justin M. Forbes a81953
+                                     QemuPixel *dst, void *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    uint32_t *src32 = src;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    while (cnt > 0) {
Justin M. Forbes a81953
+        if (pf->rbits < 8) {
Justin M. Forbes a81953
+            dst->red   = ((*src32 & pf->rmask) >> pf->rshift) << (8 - pf->rbits);
Justin M. Forbes a81953
+        } else {
Justin M. Forbes a81953
+            dst->red   = ((*src32 & pf->rmask) >> pf->rshift) >> (pf->rbits - 8);
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        if (pf->gbits < 8) {
Justin M. Forbes a81953
+            dst->green = ((*src32 & pf->gmask) >> pf->gshift) << (8 - pf->gbits);
Justin M. Forbes a81953
+        } else {
Justin M. Forbes a81953
+            dst->green = ((*src32 & pf->gmask) >> pf->gshift) >> (pf->gbits - 8);
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        if (pf->bbits < 8) {
Justin M. Forbes a81953
+            dst->blue  = ((*src32 & pf->bmask) >> pf->bshift) << (8 - pf->bbits);
Justin M. Forbes a81953
+        } else {
Justin M. Forbes a81953
+            dst->blue  = ((*src32 & pf->bmask) >> pf->bshift) >> (pf->bbits - 8);
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        if (pf->abits < 8) {
Justin M. Forbes a81953
+            dst->alpha = ((*src32 & pf->amask) >> pf->ashift) << (8 - pf->abits);
Justin M. Forbes a81953
+        } else {
Justin M. Forbes a81953
+            dst->alpha = ((*src32 & pf->amask) >> pf->ashift) >> (pf->abits - 8);
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        dst++, src32++, cnt--;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+/* ----------------------------------------------------------------------- */
Justin M. Forbes a81953
+/* QemuPixel -> PixelFormat conversions                                    */
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static void conv_pixel_to_16(PixelFormat *pf,
Justin M. Forbes a81953
+                             void *dst, QemuPixel *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    uint16_t *dst16 = dst;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    while (cnt > 0) {
Justin M. Forbes a81953
+        *dst16  = ((uint16_t)src->red   >> (8 - pf->rbits)) << pf->rshift;
Justin M. Forbes a81953
+        *dst16 |= ((uint16_t)src->green >> (8 - pf->gbits)) << pf->gshift;
Justin M. Forbes a81953
+        *dst16 |= ((uint16_t)src->blue  >> (8 - pf->bbits)) << pf->bshift;
Justin M. Forbes a81953
+        *dst16 |= ((uint16_t)src->alpha >> (8 - pf->abits)) << pf->ashift;
Justin M. Forbes a81953
+        dst16++, src++, cnt--;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static void conv_pixel_to_32(PixelFormat *pf,
Justin M. Forbes a81953
+                             void *dst, QemuPixel *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    uint32_t *dst32 = dst;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    while (cnt > 0) {
Justin M. Forbes a81953
+        *dst32  = ((uint32_t)src->red   >> (8 - pf->rbits)) << pf->rshift;
Justin M. Forbes a81953
+        *dst32 |= ((uint32_t)src->green >> (8 - pf->gbits)) << pf->gshift;
Justin M. Forbes a81953
+        *dst32 |= ((uint32_t)src->blue  >> (8 - pf->bbits)) << pf->bshift;
Justin M. Forbes a81953
+        *dst32 |= ((uint32_t)src->alpha >> (8 - pf->abits)) << pf->ashift;
Justin M. Forbes a81953
+        dst32++, src++, cnt--;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+/* ----------------------------------------------------------------------- */
Justin M. Forbes a81953
+/* PixelFormat -> PixelFormat conversions                                  */
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static void convert_copy(QemuPfConv *conv, void *dst, void *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    uint32_t bytes = cnt * conv->src.bytes_per_pixel;
Justin M. Forbes a81953
+    memcpy(dst, src, bytes);
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+static void convert_generic(QemuPfConv *conv, void *dst, void *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    if (conv->conv_cnt < cnt) {
Justin M. Forbes a81953
+        conv->conv_cnt = cnt;
Justin M. Forbes a81953
+        conv->conv_buf = qemu_realloc(conv->conv_buf, sizeof(QemuPixel) * conv->conv_cnt);
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+    conv->conv_from(&conv->src, conv->conv_buf, src, cnt);
Justin M. Forbes a81953
+    conv->conv_to(&conv->dst, dst, conv->conv_buf, cnt);
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+/* ----------------------------------------------------------------------- */
Justin M. Forbes a81953
+/* public interface                                                        */
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+QemuPfConv *qemu_pf_conv_get(PixelFormat *dst, PixelFormat *src)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    QemuPfConv *conv = qemu_mallocz(sizeof(QemuPfConv));
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    conv->src = *src;
Justin M. Forbes a81953
+    conv->dst = *dst;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+    if (memcmp(&conv->src, &conv->dst, sizeof(PixelFormat)) == 0) {
Justin M. Forbes a81953
+        /* formats identical, can simply copy */
Justin M. Forbes a81953
+        conv->convert = convert_copy;
Justin M. Forbes a81953
+    } else {
Justin M. Forbes a81953
+        /* generic two-step conversion: src -> QemuPixel -> dst  */
Justin M. Forbes a81953
+        switch (conv->src.bytes_per_pixel) {
Justin M. Forbes a81953
+        case 2:
Justin M. Forbes a81953
+            conv->conv_from = conv_16_to_pixel;
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        case 4:
Justin M. Forbes a81953
+            if (conv->src.rbits == 8 && conv->src.gbits == 8 && conv->src.bbits == 8) {
Justin M. Forbes a81953
+                conv->conv_from = conv_32_to_pixel_fast;
Justin M. Forbes a81953
+            } else {
Justin M. Forbes a81953
+                conv->conv_from = conv_32_to_pixel_generic;
Justin M. Forbes a81953
+            }
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        default:
Justin M. Forbes a81953
+            goto err;
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        switch (conv->dst.bytes_per_pixel) {
Justin M. Forbes a81953
+        case 2:
Justin M. Forbes a81953
+            conv->conv_to = conv_pixel_to_16;
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        case 4:
Justin M. Forbes a81953
+            conv->conv_to = conv_pixel_to_32;
Justin M. Forbes a81953
+            break;
Justin M. Forbes a81953
+        default:
Justin M. Forbes a81953
+            goto err;
Justin M. Forbes a81953
+        }
Justin M. Forbes a81953
+        conv->convert = convert_generic;
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+    return conv;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+err:
Justin M. Forbes a81953
+    qemu_free(conv);
Justin M. Forbes a81953
+    return NULL;
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+void qemu_pf_conv_run(QemuPfConv *conv, void *dst, void *src, uint32_t cnt)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    conv->convert(conv, dst, src, cnt);
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+void qemu_pf_conv_put(QemuPfConv *conv)
Justin M. Forbes a81953
+{
Justin M. Forbes a81953
+    if (conv) {
Justin M. Forbes a81953
+        qemu_free(conv->conv_buf);
Justin M. Forbes a81953
+        qemu_free(conv);
Justin M. Forbes a81953
+    }
Justin M. Forbes a81953
+}
Justin M. Forbes a81953
diff --git a/pflib.h b/pflib.h
Justin M. Forbes a81953
new file mode 100644
Justin M. Forbes a81953
index 0000000..8d73fdd
Justin M. Forbes a81953
--- /dev/null
Justin M. Forbes a81953
+++ b/pflib.h
Justin M. Forbes a81953
@@ -0,0 +1,6 @@
Justin M. Forbes a81953
+/* public */
Justin M. Forbes a81953
+typedef struct QemuPfConv QemuPfConv;
Justin M. Forbes a81953
+
Justin M. Forbes a81953
+QemuPfConv *qemu_pf_conv_get(PixelFormat *dst, PixelFormat *src);
Justin M. Forbes a81953
+void qemu_pf_conv_run(QemuPfConv *conv, void *dst, void *src, uint32_t cnt);
Justin M. Forbes a81953
+void qemu_pf_conv_put(QemuPfConv *conv);
Justin M. Forbes a81953
-- 
Justin M. Forbes a81953
1.7.2.3
Justin M. Forbes a81953