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_reset - reset fimg2d hardware.
298 * @ctx: a pointer to g2d_context structure.
301 static void g2d_reset(struct g2d_context *ctx)
306 g2d_add_cmd(ctx, SOFT_RESET_REG, 0x01);
310 * g2d_flush - submit all commands and values in user side command buffer
311 * to command queue aware of fimg2d dma.
313 * @ctx: a pointer to g2d_context structure.
315 * This function should be called after all commands and values to user
316 * side command buffer are set. It submits that buffer to the kernel side driver.
318 static int g2d_flush(struct g2d_context *ctx)
321 struct drm_exynos_g2d_set_cmdlist cmdlist = {0};
323 if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
326 if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
327 fprintf(stderr, MSG_PREFIX "command list overflow.\n");
331 cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
332 cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
333 cmdlist.cmd_nr = ctx->cmd_nr;
334 cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
336 if (ctx->event_userdata) {
337 cmdlist.event_type = G2D_EVENT_NONSTOP;
338 cmdlist.user_data = (uint64_t)(uintptr_t)(ctx->event_userdata);
339 ctx->event_userdata = NULL;
341 cmdlist.event_type = G2D_EVENT_NOT;
342 cmdlist.user_data = 0;
348 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
350 fprintf(stderr, MSG_PREFIX "failed to set cmdlist.\n");
360 * g2d_init - create a new g2d context and get hardware version.
362 * fd: a file descriptor to an opened drm device.
364 struct g2d_context *g2d_init(int fd)
366 struct drm_exynos_g2d_get_ver ver;
367 struct g2d_context *ctx;
370 ctx = calloc(1, sizeof(*ctx));
372 fprintf(stderr, MSG_PREFIX "failed to allocate context.\n");
378 ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
380 fprintf(stderr, MSG_PREFIX "failed to get version.\n");
385 ctx->major = ver.major;
386 ctx->minor = ver.minor;
388 printf(MSG_PREFIX "G2D version (%d.%d).\n", ctx->major, ctx->minor);
392 void g2d_fini(struct g2d_context *ctx)
398 * g2d_config_event - setup userdata configuration for a g2d event.
399 * The next invocation of a g2d call (e.g. g2d_solid_fill) is
400 * then going to flag the command buffer as 'nonstop'.
401 * Completion of the command buffer execution can then be
402 * determined by using drmHandleEvent on the DRM fd.
403 * The userdata is 'consumed' in the process.
405 * @ctx: a pointer to g2d_context structure.
406 * @userdata: a pointer to the user data
408 void g2d_config_event(struct g2d_context *ctx, void *userdata)
410 ctx->event_userdata = userdata;
414 * g2d_exec - start the dma to process all commands summited by g2d_flush().
416 * @ctx: a pointer to g2d_context structure.
418 int g2d_exec(struct g2d_context *ctx)
420 struct drm_exynos_g2d_exec exec;
423 if (ctx->cmdlist_nr == 0)
428 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
430 fprintf(stderr, MSG_PREFIX "failed to execute.\n");
440 * g2d_solid_fill - fill given buffer with given color data.
442 * @ctx: a pointer to g2d_context structure.
443 * @img: a pointer to g2d_image structure including image and buffer
445 * @x: x start position to buffer filled with given color data.
446 * @y: y start position to buffer filled with given color data.
447 * @w: width value to buffer filled with given color data.
448 * @h: height value to buffer filled with given color data.
451 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
452 unsigned int x, unsigned int y, unsigned int w,
455 union g2d_bitblt_cmd_val bitblt;
456 union g2d_point_val pt;
458 if (g2d_check_space(ctx, 7, 1))
461 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
462 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
463 g2d_add_base_addr(ctx, img, g2d_dst);
464 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
466 if (x + w > img->width)
468 if (y + h > img->height)
473 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
477 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
479 g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
482 bitblt.data.fast_solid_color_fill_en = 1;
483 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
485 return g2d_flush(ctx);
489 * g2d_copy - copy contents in source buffer to destination buffer.
491 * @ctx: a pointer to g2d_context structure.
492 * @src: a pointer to g2d_image structure including image and buffer
493 * information to source.
494 * @dst: a pointer to g2d_image structure including image and buffer
495 * information to destination.
496 * @src_x: x start position to source buffer.
497 * @src_y: y start position to source buffer.
498 * @dst_x: x start position to destination buffer.
499 * @dst_y: y start position to destination buffer.
500 * @w: width value to source and destination buffers.
501 * @h: height value to source and destination buffers.
504 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
505 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
506 unsigned int dst_x, unsigned dst_y, unsigned int w,
509 union g2d_rop4_val rop4;
510 union g2d_point_val pt;
511 unsigned int src_w, src_h, dst_w, dst_h;
515 if (src_x + src->width > w)
516 src_w = src->width - src_x;
517 if (src_y + src->height > h)
518 src_h = src->height - src_y;
522 if (dst_x + dst->width > w)
523 dst_w = dst->width - dst_x;
524 if (dst_y + dst->height > h)
525 dst_h = dst->height - dst_y;
527 w = MIN(src_w, dst_w);
528 h = MIN(src_h, dst_h);
530 if (w <= 0 || h <= 0) {
531 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
535 if (g2d_check_space(ctx, 11, 2))
538 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
539 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
540 g2d_add_base_addr(ctx, dst, g2d_dst);
541 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
543 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
544 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
545 g2d_add_base_addr(ctx, src, g2d_src);
546 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
550 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
551 pt.data.x = src_x + w;
552 pt.data.y = src_y + h;
553 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
557 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
558 pt.data.x = dst_x + w;
559 pt.data.y = dst_y + h;
560 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
563 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
564 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
566 return g2d_flush(ctx);
570 * g2d_copy_with_scale - copy contents in source buffer to destination buffer
571 * scaling up or down properly.
573 * @ctx: a pointer to g2d_context structure.
574 * @src: a pointer to g2d_image structure including image and buffer
575 * information to source.
576 * @dst: a pointer to g2d_image structure including image and buffer
577 * information to destination.
578 * @src_x: x start position to source buffer.
579 * @src_y: y start position to source buffer.
580 * @src_w: width value to source buffer.
581 * @src_h: height value to source buffer.
582 * @dst_x: x start position to destination buffer.
583 * @dst_y: y start position to destination buffer.
584 * @dst_w: width value to destination buffer.
585 * @dst_h: height value to destination buffer.
586 * @negative: indicate that it uses color negative to source and
587 * destination buffers.
590 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
591 struct g2d_image *dst, unsigned int src_x,
592 unsigned int src_y, unsigned int src_w,
593 unsigned int src_h, unsigned int dst_x,
594 unsigned int dst_y, unsigned int dst_w,
595 unsigned int dst_h, unsigned int negative)
597 union g2d_rop4_val rop4;
598 union g2d_point_val pt;
599 unsigned int scale, repeat_pad;
600 unsigned int scale_x, scale_y;
602 /* Sanitize this parameter to facilitate space computation below. */
606 if (src_w == dst_w && src_h == dst_h)
610 scale_x = g2d_get_scaling(src_w, dst_w);
611 scale_y = g2d_get_scaling(src_h, dst_h);
614 repeat_pad = src->repeat_mode == G2D_REPEAT_MODE_PAD ? 1 : 0;
616 if (src_x + src_w > src->width)
617 src_w = src->width - src_x;
618 if (src_y + src_h > src->height)
619 src_h = src->height - src_y;
621 if (dst_x + dst_w > dst->width)
622 dst_w = dst->width - dst_x;
623 if (dst_y + dst_h > dst->height)
624 dst_h = dst->height - dst_y;
626 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
627 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
631 if (g2d_check_space(ctx, 12 + scale * 3 + negative + repeat_pad, 2))
634 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
635 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
636 g2d_add_base_addr(ctx, dst, g2d_dst);
637 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
639 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
640 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
642 g2d_add_cmd(ctx, SRC_REPEAT_MODE_REG, src->repeat_mode);
644 g2d_add_cmd(ctx, SRC_PAD_VALUE_REG, dst->color);
646 g2d_add_base_addr(ctx, src, g2d_src);
647 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
650 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
653 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
654 rop4.data.unmasked_rop3 ^= G2D_ROP3_DST;
657 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
660 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
661 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
662 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
667 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
668 pt.data.x = src_x + src_w;
669 pt.data.y = src_y + src_h;
670 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
674 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
675 pt.data.x = dst_x + dst_w;
676 pt.data.y = dst_y + dst_h;
677 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
679 return g2d_flush(ctx);
683 * g2d_blend - blend image data in source and destination buffers.
685 * @ctx: a pointer to g2d_context structure.
686 * @src: a pointer to g2d_image structure including image and buffer
687 * information to source.
688 * @dst: a pointer to g2d_image structure including image and buffer
689 * information to destination.
690 * @src_x: x start position to source buffer.
691 * @src_y: y start position to source buffer.
692 * @dst_x: x start position to destination buffer.
693 * @dst_y: y start position to destination buffer.
694 * @w: width value to source and destination buffer.
695 * @h: height value to source and destination buffer.
696 * @op: blend operation type.
699 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
700 struct g2d_image *dst, unsigned int src_x,
701 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
702 unsigned int w, unsigned int h, enum e_g2d_op op)
704 union g2d_point_val pt;
705 union g2d_bitblt_cmd_val bitblt;
706 union g2d_blend_func_val blend;
707 unsigned int gem_space;
708 unsigned int src_w, src_h, dst_w, dst_h;
712 if (src_x + w > src->width)
713 src_w = src->width - src_x;
714 if (src_y + h > src->height)
715 src_h = src->height - src_y;
719 if (dst_x + w > dst->width)
720 dst_w = dst->width - dst_x;
721 if (dst_y + h > dst->height)
722 dst_h = dst->height - dst_y;
724 w = MIN(src_w, dst_w);
725 h = MIN(src_h, dst_h);
727 if (w <= 0 || h <= 0) {
728 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
732 if (!g2d_validate_select_mode(src->select_mode)) {
733 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
737 if (!g2d_validate_blending_op(op)) {
738 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
742 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
744 if (g2d_check_space(ctx, 12, gem_space))
750 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
751 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
753 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
755 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
756 g2d_add_base_addr(ctx, dst, g2d_dst);
757 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
759 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
760 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
762 switch (src->select_mode) {
763 case G2D_SELECT_MODE_NORMAL:
764 g2d_add_base_addr(ctx, src, g2d_src);
765 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
767 case G2D_SELECT_MODE_FGCOLOR:
768 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
770 case G2D_SELECT_MODE_BGCOLOR:
771 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
775 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
776 blend.val = g2d_get_blend_op(op);
777 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
778 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
782 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
783 pt.data.x = src_x + w;
784 pt.data.y = src_y + h;
785 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
789 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
790 pt.data.x = dst_x + w;
791 pt.data.y = dst_y + h;
792 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
794 return g2d_flush(ctx);
798 * g2d_scale_and_blend - apply scaling to source buffer and then blend to destination buffer
800 * @ctx: a pointer to g2d_context structure.
801 * @src: a pointer to g2d_image structure including image and buffer
802 * information to source.
803 * @dst: a pointer to g2d_image structure including image and buffer
804 * information to destination.
805 * @src_x: x start position to source buffer.
806 * @src_y: y start position to source buffer.
807 * @src_w: width value to source buffer.
808 * @src_h: height value to source buffer.
809 * @dst_x: x start position to destination buffer.
810 * @dst_y: y start position to destination buffer.
811 * @dst_w: width value to destination buffer.
812 * @dst_h: height value to destination buffer.
813 * @op: blend operation type.
816 g2d_scale_and_blend(struct g2d_context *ctx, struct g2d_image *src,
817 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
818 unsigned int src_w, unsigned int src_h, unsigned int dst_x,
819 unsigned int dst_y, unsigned int dst_w, unsigned int dst_h,
822 union g2d_point_val pt;
823 union g2d_bitblt_cmd_val bitblt;
824 union g2d_blend_func_val blend;
825 unsigned int scale, gem_space;
826 unsigned int scale_x, scale_y;
828 if (src_w == dst_w && src_h == dst_h)
832 scale_x = g2d_get_scaling(src_w, dst_w);
833 scale_y = g2d_get_scaling(src_h, dst_h);
836 if (src_x + src_w > src->width)
837 src_w = src->width - src_x;
838 if (src_y + src_h > src->height)
839 src_h = src->height - src_y;
841 if (dst_x + dst_w > dst->width)
842 dst_w = dst->width - dst_x;
843 if (dst_y + dst_h > dst->height)
844 dst_h = dst->height - dst_y;
846 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
847 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
851 if (!g2d_validate_select_mode(src->select_mode)) {
852 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
856 if (!g2d_validate_blending_op(op)) {
857 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
861 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
863 if (g2d_check_space(ctx, 12 + scale * 3, gem_space))
869 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
870 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
872 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
874 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
875 g2d_add_base_addr(ctx, dst, g2d_dst);
876 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
878 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
879 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
881 switch (src->select_mode) {
882 case G2D_SELECT_MODE_NORMAL:
883 g2d_add_base_addr(ctx, src, g2d_src);
884 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
886 case G2D_SELECT_MODE_FGCOLOR:
887 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
889 case G2D_SELECT_MODE_BGCOLOR:
890 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
895 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
896 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
897 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
900 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
901 blend.val = g2d_get_blend_op(op);
902 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
903 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
907 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
908 pt.data.x = src_x + src_w;
909 pt.data.y = src_y + src_h;
910 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
914 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
915 pt.data.x = dst_x + dst_w;
916 pt.data.y = dst_y + dst_h;
917 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
919 return g2d_flush(ctx);