exynos: introduce g2d_add_base_addr helper function
[platform/upstream/libdrm.git] / exynos / exynos_fimg2d.c
index a565910..03cf182 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <xf86drm.h>
 
+#include "libdrm.h"
 #include "exynos_drm.h"
 #include "fimg2d_reg.h"
 #include "fimg2d.h"
 
 #define MIN(a, b)      ((a) < (b) ? (a) : (b))
 
+enum g2d_base_addr_reg {
+       g2d_dst = 0,
+       g2d_src
+};
+
+static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
+{
+       /*
+        * The G2D hw scaling factor is a normalized inverse of the scaling factor.
+        * For example: When source width is 100 and destination width is 200
+        * (scaling of 2x), then the hw factor is NC * 100 / 200.
+        * The normalization factor (NC) is 2^16 = 0x10000.
+        */
+
+       return ((src << 16) / dst);
+}
+
 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
 {
        union g2d_blend_func_val val;
@@ -121,6 +139,26 @@ static int g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
 }
 
 /*
+ * g2d_add_base_addr - helper function to set dst/src base address register.
+ *
+ * @ctx: a pointer to g2d_context structure.
+ * @img: a pointer to the dst/src g2d_image structure.
+ * @reg: the register that should be set.
+ */
+static void g2d_add_base_addr(struct g2d_context *ctx, struct g2d_image *img,
+                       enum g2d_base_addr_reg reg)
+{
+       const unsigned long cmd = (reg == g2d_dst) ?
+               DST_BASE_ADDR_REG : SRC_BASE_ADDR_REG;
+
+       if (img->buf_type == G2D_IMGBUF_USERPTR)
+               g2d_add_cmd(ctx, cmd | G2D_BUF_USERPTR,
+                               (unsigned long)&img->user_ptr[0]);
+       else
+               g2d_add_cmd(ctx, cmd, img->bo[0]);
+}
+
+/*
  * g2d_reset - reset fimg2d hardware.
  *
  * @ctx: a pointer to g2d_context structure.
@@ -135,13 +173,13 @@ static void g2d_reset(struct g2d_context *ctx)
 }
 
 /*
- * g2d_flush - summit all commands and values in user side command buffer
+ * g2d_flush - submit all commands and values in user side command buffer
  *             to command queue aware of fimg2d dma.
  *
  * @ctx: a pointer to g2d_context structure.
  *
  * This function should be called after all commands and values to user
- * side command buffer is set to summit that buffer to kernel side driver.
+ * side command buffer are set. It submits that buffer to the kernel side driver.
  */
 static int g2d_flush(struct g2d_context *ctx)
 {
@@ -182,9 +220,9 @@ static int g2d_flush(struct g2d_context *ctx)
 /**
  * g2d_init - create a new g2d context and get hardware version.
  *
- * fd: a file descriptor to drm device driver opened.
+ * fd: a file descriptor to an opened drm device.
  */
-struct g2d_context *g2d_init(int fd)
+drm_public struct g2d_context *g2d_init(int fd)
 {
        struct drm_exynos_g2d_get_ver ver;
        struct g2d_context *ctx;
@@ -212,7 +250,7 @@ struct g2d_context *g2d_init(int fd)
        return ctx;
 }
 
-void g2d_fini(struct g2d_context *ctx)
+drm_public void g2d_fini(struct g2d_context *ctx)
 {
        if (ctx)
                free(ctx);
@@ -223,7 +261,7 @@ void g2d_fini(struct g2d_context *ctx)
  *
  * @ctx: a pointer to g2d_context structure.
  */
-int g2d_exec(struct g2d_context *ctx)
+drm_public int g2d_exec(struct g2d_context *ctx)
 {
        struct drm_exynos_g2d_exec exec;
        int ret;
@@ -255,7 +293,8 @@ int g2d_exec(struct g2d_context *ctx)
  * @w: width value to buffer filled with given color data.
  * @h: height value to buffer filled with given color data.
  */
-int g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
+drm_public int
+g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
                        unsigned int x, unsigned int y, unsigned int w,
                        unsigned int h)
 {
@@ -264,13 +303,7 @@ int g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
 
        g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
        g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
-
-       if (img->buf_type == G2D_IMGBUF_USERPTR)
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
-                               (unsigned long)&img->user_ptr[0]);
-       else
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG, img->bo[0]);
-
+       g2d_add_base_addr(ctx, img, g2d_dst);
        g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
 
        if (x + w > img->width)
@@ -315,7 +348,8 @@ int g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
  * @w: width value to source and destination buffers.
  * @h: height value to source and destination buffers.
  */
-int g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
+drm_public int
+g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
                struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
                unsigned int dst_x, unsigned dst_y, unsigned int w,
                unsigned int h)
@@ -326,22 +360,12 @@ int g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
 
        g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
        g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
-       if (dst->buf_type == G2D_IMGBUF_USERPTR)
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
-                               (unsigned long)&dst->user_ptr[0]);
-       else
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
-
+       g2d_add_base_addr(ctx, dst, g2d_dst);
        g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
 
        g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
        g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
-       if (src->buf_type == G2D_IMGBUF_USERPTR)
-               g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
-                               (unsigned long)&src->user_ptr[0]);
-       else
-               g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
-
+       g2d_add_base_addr(ctx, src, g2d_src);
        g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
 
        src_w = w;
@@ -414,7 +438,8 @@ int g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
  * @negative: indicate that it uses color negative to source and
  *     destination buffers.
  */
-int g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
+drm_public int
+g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
                                struct g2d_image *dst, unsigned int src_x,
                                unsigned int src_y, unsigned int src_w,
                                unsigned int src_h, unsigned int dst_x,
@@ -424,34 +449,24 @@ int g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
        union g2d_rop4_val rop4;
        union g2d_point_val pt;
        unsigned int scale;
-       double scale_x = 0.0f, scale_y = 0.0f;
+       unsigned int scale_x, scale_y;
 
        g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
        g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
-       if (dst->buf_type == G2D_IMGBUF_USERPTR)
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
-                               (unsigned long)&dst->user_ptr[0]);
-       else
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
-
+       g2d_add_base_addr(ctx, dst, g2d_dst);
        g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
 
        g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
        g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
-       if (src->buf_type == G2D_IMGBUF_USERPTR)
-               g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
-                               (unsigned long)&src->user_ptr[0]);
-       else
-               g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
-
+       g2d_add_base_addr(ctx, src, g2d_src);
        g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
 
        if (src_w == dst_w && src_h == dst_h)
                scale = 0;
        else {
                scale = 1;
-               scale_x = (double)src_w / (double)dst_w;
-               scale_y = (double)src_w / (double)dst_h;
+               scale_x = g2d_get_scaling(src_w, dst_w);
+               scale_y = g2d_get_scaling(src_h, dst_h);
        }
 
        if (src_x + src_w > src->width)
@@ -483,8 +498,8 @@ int g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
 
        if (scale) {
                g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
-               g2d_add_cmd(ctx, SRC_XSCALE_REG, G2D_DOUBLE_TO_FIXED(scale_x));
-               g2d_add_cmd(ctx, SRC_YSCALE_REG, G2D_DOUBLE_TO_FIXED(scale_y));
+               g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
+               g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
        }
 
        pt.val = 0;
@@ -511,7 +526,7 @@ int g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
 }
 
 /**
- * g2d_blend - blend image data in source and destion buffers
+ * g2d_blend - blend image data in source and destination buffers.
  *
  * @ctx: a pointer to g2d_context structure.
  * @src: a pointer to g2d_image structure including image and buffer
@@ -526,7 +541,8 @@ int g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
  * @h: height value to source and destination buffer.
  * @op: blend operation type.
  */
-int g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
+drm_public int
+g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
                struct g2d_image *dst, unsigned int src_x,
                unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
                unsigned int w, unsigned int h, enum e_g2d_op op)
@@ -545,12 +561,7 @@ int g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
                g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
 
        g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
-       if (dst->buf_type == G2D_IMGBUF_USERPTR)
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
-                               (unsigned long)&dst->user_ptr[0]);
-       else
-               g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
-
+       g2d_add_base_addr(ctx, dst, g2d_dst);
        g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
 
        g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
@@ -558,12 +569,7 @@ int g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
 
        switch (src->select_mode) {
        case G2D_SELECT_MODE_NORMAL:
-               if (src->buf_type == G2D_IMGBUF_USERPTR)
-                       g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
-                                       (unsigned long)&src->user_ptr[0]);
-               else
-                       g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
-
+               g2d_add_base_addr(ctx, src, g2d_src);
                g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
                break;
        case G2D_SELECT_MODE_FGCOLOR: