2 * Copyright (C) 2013 Samsung Electronics Co.Ltd
4 * Inki Dae <inki.dae@samsung.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
24 #include <linux/stddef.h>
28 #include "libdrm_macros.h"
29 #include "exynos_drm.h"
30 #include "fimg2d_reg.h"
31 #include "exynos_fimg2d.h"
33 #define SET_BF(val, sc, si, scsa, scda, dc, di, dcsa, dcda) \
34 val.data.src_coeff = sc; \
35 val.data.inv_src_color_coeff = si; \
36 val.data.src_coeff_src_a = scsa; \
37 val.data.src_coeff_dst_a = scda; \
38 val.data.dst_coeff = dc; \
39 val.data.inv_dst_color_coeff = di; \
40 val.data.dst_coeff_src_a = dcsa; \
41 val.data.dst_coeff_dst_a = dcda;
43 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 #define MSG_PREFIX "exynos/fimg2d: "
47 #define G2D_MAX_CMD_NR 64
48 #define G2D_MAX_GEM_CMD_NR 64
49 #define G2D_MAX_CMD_LIST_NR 64
55 struct drm_exynos_g2d_cmd cmd[G2D_MAX_CMD_NR];
56 struct drm_exynos_g2d_cmd cmd_buf[G2D_MAX_GEM_CMD_NR];
58 unsigned int cmd_buf_nr;
59 unsigned int cmdlist_nr;
63 enum g2d_base_addr_reg {
69 G2D_DIR_MODE_POSITIVE = 0,
70 G2D_DIR_MODE_NEGATIVE = 1
73 union g2d_direction_val {
76 /* SRC_MSK_DIRECT_REG [0:1] (source) */
77 enum e_g2d_dir_mode src_x_direction:1;
78 enum e_g2d_dir_mode src_y_direction:1;
80 /* SRC_MSK_DIRECT_REG [2:3] */
81 unsigned int reversed1:2;
83 /* SRC_MSK_DIRECT_REG [4:5] (mask) */
84 enum e_g2d_dir_mode mask_x_direction:1;
85 enum e_g2d_dir_mode mask_y_direction:1;
87 /* SRC_MSK_DIRECT_REG [6:31] */
88 unsigned int padding1:26;
90 /* DST_PAT_DIRECT_REG [0:1] (destination) */
91 enum e_g2d_dir_mode dst_x_direction:1;
92 enum e_g2d_dir_mode dst_y_direction:1;
94 /* DST_PAT_DIRECT_REG [2:3] */
95 unsigned int reversed2:2;
97 /* DST_PAT_DIRECT_REG [4:5] (pattern) */
98 enum e_g2d_dir_mode pat_x_direction:1;
99 enum e_g2d_dir_mode pat_y_direction:1;
101 /* DST_PAT_DIRECT_REG [6:31] */
102 unsigned int padding2:26;
106 static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
109 * The G2D hw scaling factor is a normalized inverse of the scaling factor.
110 * For example: When source width is 100 and destination width is 200
111 * (scaling of 2x), then the hw factor is NC * 100 / 200.
112 * The normalization factor (NC) is 2^16 = 0x10000.
115 return ((src << 16) / dst);
118 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
120 union g2d_blend_func_val val;
125 * The switch statement is missing the default branch since
126 * we assume that the caller checks the blending operation
127 * via g2d_validate_blending_op() first.
131 case G2D_OP_DISJOINT_CLEAR:
132 case G2D_OP_CONJOINT_CLEAR:
133 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
137 case G2D_OP_DISJOINT_SRC:
138 case G2D_OP_CONJOINT_SRC:
139 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
143 case G2D_OP_DISJOINT_DST:
144 case G2D_OP_CONJOINT_DST:
145 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
149 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
150 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
152 case G2D_OP_INTERPOLATE:
153 SET_BF(val, G2D_COEFF_MODE_SRC_ALPHA, 0, 0, 0,
154 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
162 * g2d_check_space - check if command buffers have enough space left.
164 * @ctx: a pointer to g2d_context structure.
165 * @num_cmds: number of (regular) commands.
166 * @num_gem_cmds: number of GEM commands.
168 static unsigned int g2d_check_space(const struct g2d_context *ctx,
169 unsigned int num_cmds, unsigned int num_gem_cmds)
171 if (ctx->cmd_nr + num_cmds >= G2D_MAX_CMD_NR ||
172 ctx->cmd_buf_nr + num_gem_cmds >= G2D_MAX_GEM_CMD_NR)
179 * g2d_validate_select_mode - validate select mode.
181 * @mode: the mode to validate
183 * Returns zero for an invalid mode and one otherwise.
185 static int g2d_validate_select_mode(
186 enum e_g2d_select_mode mode)
189 case G2D_SELECT_MODE_NORMAL:
190 case G2D_SELECT_MODE_FGCOLOR:
191 case G2D_SELECT_MODE_BGCOLOR:
199 * g2d_validate_blending_op - validate blending operation.
201 * @operation: the operation to validate
203 * Returns zero for an invalid mode and one otherwise.
205 static int g2d_validate_blending_op(
206 enum e_g2d_op operation)
213 case G2D_OP_INTERPOLATE:
214 case G2D_OP_DISJOINT_CLEAR:
215 case G2D_OP_DISJOINT_SRC:
216 case G2D_OP_DISJOINT_DST:
217 case G2D_OP_CONJOINT_CLEAR:
218 case G2D_OP_CONJOINT_SRC:
219 case G2D_OP_CONJOINT_DST:
227 * g2d_add_cmd - set given command and value to user side command buffer.
229 * @ctx: a pointer to g2d_context structure.
230 * @cmd: command data.
231 * @value: value data.
233 * The caller has to make sure that the commands buffers have enough space
234 * left to hold the command. Use g2d_check_space() to ensure this.
236 static void g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
239 switch (cmd & ~(G2D_BUF_USERPTR)) {
240 case SRC_BASE_ADDR_REG:
241 case SRC_PLANE2_BASE_ADDR_REG:
242 case DST_BASE_ADDR_REG:
243 case DST_PLANE2_BASE_ADDR_REG:
244 case PAT_BASE_ADDR_REG:
245 case MASK_BASE_ADDR_REG:
246 assert(ctx->cmd_buf_nr < G2D_MAX_GEM_CMD_NR);
248 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
249 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
253 assert(ctx->cmd_nr < G2D_MAX_CMD_NR);
255 ctx->cmd[ctx->cmd_nr].offset = cmd;
256 ctx->cmd[ctx->cmd_nr].data = value;
263 * g2d_add_base_addr - helper function to set dst/src base address register.
265 * @ctx: a pointer to g2d_context structure.
266 * @img: a pointer to the dst/src g2d_image structure.
267 * @reg: the register that should be set.
269 static void g2d_add_base_addr(struct g2d_context *ctx, struct g2d_image *img,
270 enum g2d_base_addr_reg reg)
272 const unsigned long cmd = (reg == g2d_dst) ?
273 DST_BASE_ADDR_REG : SRC_BASE_ADDR_REG;
275 if (img->buf_type == G2D_IMGBUF_USERPTR)
276 g2d_add_cmd(ctx, cmd | G2D_BUF_USERPTR,
277 (unsigned long)&img->user_ptr[0]);
279 g2d_add_cmd(ctx, cmd, img->bo[0]);
283 * g2d_set_direction - setup direction register (useful for overlapping blits).
285 * @ctx: a pointer to g2d_context structure.
286 * @dir: a pointer to the g2d_direction_val structure.
288 static void g2d_set_direction(struct g2d_context *ctx,
289 const union g2d_direction_val *dir)
291 g2d_add_cmd(ctx, SRC_MASK_DIRECT_REG, dir->val[0]);
292 g2d_add_cmd(ctx, DST_PAT_DIRECT_REG, dir->val[1]);
296 * g2d_flush - submit all commands and values in user side command buffer
297 * to command queue aware of fimg2d dma.
299 * @ctx: a pointer to g2d_context structure.
301 * This function should be called after all commands and values to user
302 * side command buffer are set. It submits that buffer to the kernel side driver.
304 static int g2d_flush(struct g2d_context *ctx)
307 struct drm_exynos_g2d_set_cmdlist cmdlist = {0};
309 if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
312 if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
313 fprintf(stderr, MSG_PREFIX "command list overflow.\n");
317 cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
318 cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
319 cmdlist.cmd_nr = ctx->cmd_nr;
320 cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
322 if (ctx->event_userdata) {
323 cmdlist.event_type = G2D_EVENT_NONSTOP;
324 cmdlist.user_data = (uint64_t)(uintptr_t)(ctx->event_userdata);
325 ctx->event_userdata = NULL;
327 cmdlist.event_type = G2D_EVENT_NOT;
328 cmdlist.user_data = 0;
334 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
336 fprintf(stderr, MSG_PREFIX "failed to set cmdlist.\n");
346 * g2d_init - create a new g2d context and get hardware version.
348 * fd: a file descriptor to an opened drm device.
350 struct g2d_context *g2d_init(int fd)
352 struct drm_exynos_g2d_get_ver ver;
353 struct g2d_context *ctx;
356 ctx = calloc(1, sizeof(*ctx));
358 fprintf(stderr, MSG_PREFIX "failed to allocate context.\n");
364 ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
366 fprintf(stderr, MSG_PREFIX "failed to get version.\n");
371 ctx->major = ver.major;
372 ctx->minor = ver.minor;
374 printf(MSG_PREFIX "G2D version (%d.%d).\n", ctx->major, ctx->minor);
378 void g2d_fini(struct g2d_context *ctx)
384 * g2d_config_event - setup userdata configuration for a g2d event.
385 * The next invocation of a g2d call (e.g. g2d_solid_fill) is
386 * then going to flag the command buffer as 'nonstop'.
387 * Completion of the command buffer execution can then be
388 * determined by using drmHandleEvent on the DRM fd.
389 * The userdata is 'consumed' in the process.
391 * @ctx: a pointer to g2d_context structure.
392 * @userdata: a pointer to the user data
394 void g2d_config_event(struct g2d_context *ctx, void *userdata)
396 ctx->event_userdata = userdata;
400 * g2d_exec - start the dma to process all commands summited by g2d_flush().
402 * @ctx: a pointer to g2d_context structure.
404 int g2d_exec(struct g2d_context *ctx)
406 struct drm_exynos_g2d_exec exec;
409 if (ctx->cmdlist_nr == 0)
414 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
416 fprintf(stderr, MSG_PREFIX "failed to execute.\n");
426 * g2d_solid_fill - fill given buffer with given color data.
428 * @ctx: a pointer to g2d_context structure.
429 * @img: a pointer to g2d_image structure including image and buffer
431 * @x: x start position to buffer filled with given color data.
432 * @y: y start position to buffer filled with given color data.
433 * @w: width value to buffer filled with given color data.
434 * @h: height value to buffer filled with given color data.
437 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
438 unsigned int x, unsigned int y, unsigned int w,
441 union g2d_bitblt_cmd_val bitblt;
442 union g2d_point_val pt;
444 if (g2d_check_space(ctx, 7, 1))
447 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
448 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
449 g2d_add_base_addr(ctx, img, g2d_dst);
450 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
452 if (x + w > img->width)
454 if (y + h > img->height)
459 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
463 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
465 g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
468 bitblt.data.fast_solid_color_fill_en = 1;
469 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
471 return g2d_flush(ctx);
475 * g2d_copy - copy contents in source buffer to destination buffer.
477 * @ctx: a pointer to g2d_context structure.
478 * @src: a pointer to g2d_image structure including image and buffer
479 * information to source.
480 * @dst: a pointer to g2d_image structure including image and buffer
481 * information to destination.
482 * @src_x: x start position to source buffer.
483 * @src_y: y start position to source buffer.
484 * @dst_x: x start position to destination buffer.
485 * @dst_y: y start position to destination buffer.
486 * @w: width value to source and destination buffers.
487 * @h: height value to source and destination buffers.
490 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
491 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
492 unsigned int dst_x, unsigned dst_y, unsigned int w,
495 union g2d_rop4_val rop4;
496 union g2d_point_val pt;
497 unsigned int src_w, src_h, dst_w, dst_h;
501 if (src_x + src->width > w)
502 src_w = src->width - src_x;
503 if (src_y + src->height > h)
504 src_h = src->height - src_y;
508 if (dst_x + dst->width > w)
509 dst_w = dst->width - dst_x;
510 if (dst_y + dst->height > h)
511 dst_h = dst->height - dst_y;
513 w = MIN(src_w, dst_w);
514 h = MIN(src_h, dst_h);
516 if (w <= 0 || h <= 0) {
517 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
521 if (g2d_check_space(ctx, 11, 2))
524 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
525 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
526 g2d_add_base_addr(ctx, dst, g2d_dst);
527 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
529 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
530 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
531 g2d_add_base_addr(ctx, src, g2d_src);
532 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
536 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
537 pt.data.x = src_x + w;
538 pt.data.y = src_y + h;
539 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
543 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
544 pt.data.x = dst_x + w;
545 pt.data.y = dst_y + h;
546 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
549 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
550 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
552 return g2d_flush(ctx);
556 * g2d_move - copy content inside single buffer.
557 * Similar to libc's memmove() this copies a rectangular
558 * region of the provided buffer to another location, while
559 * properly handling the situation where source and
560 * destination rectangle overlap.
562 * @ctx: a pointer to g2d_context structure.
563 * @img: a pointer to g2d_image structure providing
564 * buffer information.
565 * @src_x: x position of source rectangle.
566 * @src_y: y position of source rectangle.
567 * @dst_x: x position of destination rectangle.
568 * @dst_y: y position of destination rectangle.
569 * @w: width of rectangle to move.
570 * @h: height of rectangle to move.
573 g2d_move(struct g2d_context *ctx, struct g2d_image *img,
574 unsigned int src_x, unsigned int src_y,
575 unsigned int dst_x, unsigned dst_y, unsigned int w,
578 union g2d_rop4_val rop4;
579 union g2d_point_val pt;
580 union g2d_direction_val dir;
581 unsigned int src_w, src_h, dst_w, dst_h;
585 if (src_x + img->width > w)
586 src_w = img->width - src_x;
587 if (src_y + img->height > h)
588 src_h = img->height - src_y;
592 if (dst_x + img->width > w)
593 dst_w = img->width - dst_x;
594 if (dst_y + img->height > h)
595 dst_h = img->height - dst_y;
597 w = MIN(src_w, dst_w);
598 h = MIN(src_h, dst_h);
600 if (w == 0 || h == 0) {
601 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
605 if (g2d_check_space(ctx, 13, 2))
608 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
609 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
611 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
612 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, img->color_mode);
614 g2d_add_base_addr(ctx, img, g2d_dst);
615 g2d_add_base_addr(ctx, img, g2d_src);
617 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
618 g2d_add_cmd(ctx, SRC_STRIDE_REG, img->stride);
620 dir.val[0] = dir.val[1] = 0;
623 dir.data.src_x_direction = dir.data.dst_x_direction = 1;
625 dir.data.src_y_direction = dir.data.dst_y_direction = 1;
627 g2d_set_direction(ctx, &dir);
631 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
632 pt.data.x = src_x + w;
633 pt.data.y = src_y + h;
634 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
638 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
639 pt.data.x = dst_x + w;
640 pt.data.y = dst_y + h;
641 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
644 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
645 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
647 return g2d_flush(ctx);
651 * g2d_copy_with_scale - copy contents in source buffer to destination buffer
652 * scaling up or down properly.
654 * @ctx: a pointer to g2d_context structure.
655 * @src: a pointer to g2d_image structure including image and buffer
656 * information to source.
657 * @dst: a pointer to g2d_image structure including image and buffer
658 * information to destination.
659 * @src_x: x start position to source buffer.
660 * @src_y: y start position to source buffer.
661 * @src_w: width value to source buffer.
662 * @src_h: height value to source buffer.
663 * @dst_x: x start position to destination buffer.
664 * @dst_y: y start position to destination buffer.
665 * @dst_w: width value to destination buffer.
666 * @dst_h: height value to destination buffer.
667 * @negative: indicate that it uses color negative to source and
668 * destination buffers.
671 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
672 struct g2d_image *dst, unsigned int src_x,
673 unsigned int src_y, unsigned int src_w,
674 unsigned int src_h, unsigned int dst_x,
675 unsigned int dst_y, unsigned int dst_w,
676 unsigned int dst_h, unsigned int negative)
678 union g2d_rop4_val rop4;
679 union g2d_point_val pt;
680 unsigned int scale, repeat_pad;
681 unsigned int scale_x, scale_y;
683 /* Sanitize this parameter to facilitate space computation below. */
687 if (src_w == dst_w && src_h == dst_h)
691 scale_x = g2d_get_scaling(src_w, dst_w);
692 scale_y = g2d_get_scaling(src_h, dst_h);
695 repeat_pad = src->repeat_mode == G2D_REPEAT_MODE_PAD ? 1 : 0;
697 if (src_x + src_w > src->width)
698 src_w = src->width - src_x;
699 if (src_y + src_h > src->height)
700 src_h = src->height - src_y;
702 if (dst_x + dst_w > dst->width)
703 dst_w = dst->width - dst_x;
704 if (dst_y + dst_h > dst->height)
705 dst_h = dst->height - dst_y;
707 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
708 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
712 if (g2d_check_space(ctx, 12 + scale * 3 + negative + repeat_pad, 2))
715 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
716 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
717 g2d_add_base_addr(ctx, dst, g2d_dst);
718 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
720 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
721 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
723 g2d_add_cmd(ctx, SRC_REPEAT_MODE_REG, src->repeat_mode);
725 g2d_add_cmd(ctx, SRC_PAD_VALUE_REG, dst->color);
727 g2d_add_base_addr(ctx, src, g2d_src);
728 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
731 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
734 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
735 rop4.data.unmasked_rop3 ^= G2D_ROP3_DST;
738 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
741 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
742 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
743 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
748 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
749 pt.data.x = src_x + src_w;
750 pt.data.y = src_y + src_h;
751 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
755 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
756 pt.data.x = dst_x + dst_w;
757 pt.data.y = dst_y + dst_h;
758 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
760 return g2d_flush(ctx);
764 * g2d_blend - blend image data in source and destination buffers.
766 * @ctx: a pointer to g2d_context structure.
767 * @src: a pointer to g2d_image structure including image and buffer
768 * information to source.
769 * @dst: a pointer to g2d_image structure including image and buffer
770 * information to destination.
771 * @src_x: x start position to source buffer.
772 * @src_y: y start position to source buffer.
773 * @dst_x: x start position to destination buffer.
774 * @dst_y: y start position to destination buffer.
775 * @w: width value to source and destination buffer.
776 * @h: height value to source and destination buffer.
777 * @op: blend operation type.
780 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
781 struct g2d_image *dst, unsigned int src_x,
782 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
783 unsigned int w, unsigned int h, enum e_g2d_op op)
785 union g2d_point_val pt;
786 union g2d_bitblt_cmd_val bitblt;
787 union g2d_blend_func_val blend;
788 unsigned int gem_space;
789 unsigned int src_w, src_h, dst_w, dst_h;
793 if (src_x + w > src->width)
794 src_w = src->width - src_x;
795 if (src_y + h > src->height)
796 src_h = src->height - src_y;
800 if (dst_x + w > dst->width)
801 dst_w = dst->width - dst_x;
802 if (dst_y + h > dst->height)
803 dst_h = dst->height - dst_y;
805 w = MIN(src_w, dst_w);
806 h = MIN(src_h, dst_h);
808 if (w <= 0 || h <= 0) {
809 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
813 if (!g2d_validate_select_mode(src->select_mode)) {
814 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
818 if (!g2d_validate_blending_op(op)) {
819 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
823 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
825 if (g2d_check_space(ctx, 12, gem_space))
831 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
832 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
834 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
836 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
837 g2d_add_base_addr(ctx, dst, g2d_dst);
838 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
840 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
841 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
843 switch (src->select_mode) {
844 case G2D_SELECT_MODE_NORMAL:
845 g2d_add_base_addr(ctx, src, g2d_src);
846 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
848 case G2D_SELECT_MODE_FGCOLOR:
849 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
851 case G2D_SELECT_MODE_BGCOLOR:
852 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
856 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
857 blend.val = g2d_get_blend_op(op);
858 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
859 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
863 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
864 pt.data.x = src_x + w;
865 pt.data.y = src_y + h;
866 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
870 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
871 pt.data.x = dst_x + w;
872 pt.data.y = dst_y + h;
873 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
875 return g2d_flush(ctx);
879 * g2d_scale_and_blend - apply scaling to source buffer and then blend to destination buffer
881 * @ctx: a pointer to g2d_context structure.
882 * @src: a pointer to g2d_image structure including image and buffer
883 * information to source.
884 * @dst: a pointer to g2d_image structure including image and buffer
885 * information to destination.
886 * @src_x: x start position to source buffer.
887 * @src_y: y start position to source buffer.
888 * @src_w: width value to source buffer.
889 * @src_h: height value to source buffer.
890 * @dst_x: x start position to destination buffer.
891 * @dst_y: y start position to destination buffer.
892 * @dst_w: width value to destination buffer.
893 * @dst_h: height value to destination buffer.
894 * @op: blend operation type.
897 g2d_scale_and_blend(struct g2d_context *ctx, struct g2d_image *src,
898 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
899 unsigned int src_w, unsigned int src_h, unsigned int dst_x,
900 unsigned int dst_y, unsigned int dst_w, unsigned int dst_h,
903 union g2d_point_val pt;
904 union g2d_bitblt_cmd_val bitblt;
905 union g2d_blend_func_val blend;
906 unsigned int scale, gem_space;
907 unsigned int scale_x, scale_y;
909 if (src_w == dst_w && src_h == dst_h)
913 scale_x = g2d_get_scaling(src_w, dst_w);
914 scale_y = g2d_get_scaling(src_h, dst_h);
917 if (src_x + src_w > src->width)
918 src_w = src->width - src_x;
919 if (src_y + src_h > src->height)
920 src_h = src->height - src_y;
922 if (dst_x + dst_w > dst->width)
923 dst_w = dst->width - dst_x;
924 if (dst_y + dst_h > dst->height)
925 dst_h = dst->height - dst_y;
927 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
928 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
932 if (!g2d_validate_select_mode(src->select_mode)) {
933 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
937 if (!g2d_validate_blending_op(op)) {
938 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
942 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
944 if (g2d_check_space(ctx, 12 + scale * 3, gem_space))
950 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
951 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
953 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
955 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
956 g2d_add_base_addr(ctx, dst, g2d_dst);
957 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
959 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
960 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
962 switch (src->select_mode) {
963 case G2D_SELECT_MODE_NORMAL:
964 g2d_add_base_addr(ctx, src, g2d_src);
965 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
967 case G2D_SELECT_MODE_FGCOLOR:
968 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
970 case G2D_SELECT_MODE_BGCOLOR:
971 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
976 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
977 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
978 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
981 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
982 blend.val = g2d_get_blend_op(op);
983 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
984 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
988 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
989 pt.data.x = src_x + src_w;
990 pt.data.y = src_y + src_h;
991 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
995 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
996 pt.data.x = dst_x + dst_w;
997 pt.data.y = dst_y + dst_h;
998 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
1000 return g2d_flush(ctx);