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 {
68 static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
71 * The G2D hw scaling factor is a normalized inverse of the scaling factor.
72 * For example: When source width is 100 and destination width is 200
73 * (scaling of 2x), then the hw factor is NC * 100 / 200.
74 * The normalization factor (NC) is 2^16 = 0x10000.
77 return ((src << 16) / dst);
80 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
82 union g2d_blend_func_val val;
87 * The switch statement is missing the default branch since
88 * we assume that the caller checks the blending operation
89 * via g2d_validate_blending_op() first.
93 case G2D_OP_DISJOINT_CLEAR:
94 case G2D_OP_CONJOINT_CLEAR:
95 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
99 case G2D_OP_DISJOINT_SRC:
100 case G2D_OP_CONJOINT_SRC:
101 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
105 case G2D_OP_DISJOINT_DST:
106 case G2D_OP_CONJOINT_DST:
107 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
111 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
112 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
114 case G2D_OP_INTERPOLATE:
115 SET_BF(val, G2D_COEFF_MODE_SRC_ALPHA, 0, 0, 0,
116 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
124 * g2d_check_space - check if command buffers have enough space left.
126 * @ctx: a pointer to g2d_context structure.
127 * @num_cmds: number of (regular) commands.
128 * @num_gem_cmds: number of GEM commands.
130 static unsigned int g2d_check_space(const struct g2d_context *ctx,
131 unsigned int num_cmds, unsigned int num_gem_cmds)
133 if (ctx->cmd_nr + num_cmds >= G2D_MAX_CMD_NR ||
134 ctx->cmd_buf_nr + num_gem_cmds >= G2D_MAX_GEM_CMD_NR)
141 * g2d_validate_select_mode - validate select mode.
143 * @mode: the mode to validate
145 * Returns zero for an invalid mode and one otherwise.
147 static int g2d_validate_select_mode(
148 enum e_g2d_select_mode mode)
151 case G2D_SELECT_MODE_NORMAL:
152 case G2D_SELECT_MODE_FGCOLOR:
153 case G2D_SELECT_MODE_BGCOLOR:
161 * g2d_validate_blending_op - validate blending operation.
163 * @operation: the operation to validate
165 * Returns zero for an invalid mode and one otherwise.
167 static int g2d_validate_blending_op(
168 enum e_g2d_op operation)
175 case G2D_OP_INTERPOLATE:
176 case G2D_OP_DISJOINT_CLEAR:
177 case G2D_OP_DISJOINT_SRC:
178 case G2D_OP_DISJOINT_DST:
179 case G2D_OP_CONJOINT_CLEAR:
180 case G2D_OP_CONJOINT_SRC:
181 case G2D_OP_CONJOINT_DST:
189 * g2d_add_cmd - set given command and value to user side command buffer.
191 * @ctx: a pointer to g2d_context structure.
192 * @cmd: command data.
193 * @value: value data.
195 * The caller has to make sure that the commands buffers have enough space
196 * left to hold the command. Use g2d_check_space() to ensure this.
198 static void g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
201 switch (cmd & ~(G2D_BUF_USERPTR)) {
202 case SRC_BASE_ADDR_REG:
203 case SRC_PLANE2_BASE_ADDR_REG:
204 case DST_BASE_ADDR_REG:
205 case DST_PLANE2_BASE_ADDR_REG:
206 case PAT_BASE_ADDR_REG:
207 case MASK_BASE_ADDR_REG:
208 assert(ctx->cmd_buf_nr < G2D_MAX_GEM_CMD_NR);
210 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
211 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
215 assert(ctx->cmd_nr < G2D_MAX_CMD_NR);
217 ctx->cmd[ctx->cmd_nr].offset = cmd;
218 ctx->cmd[ctx->cmd_nr].data = value;
225 * g2d_add_base_addr - helper function to set dst/src base address register.
227 * @ctx: a pointer to g2d_context structure.
228 * @img: a pointer to the dst/src g2d_image structure.
229 * @reg: the register that should be set.
231 static void g2d_add_base_addr(struct g2d_context *ctx, struct g2d_image *img,
232 enum g2d_base_addr_reg reg)
234 const unsigned long cmd = (reg == g2d_dst) ?
235 DST_BASE_ADDR_REG : SRC_BASE_ADDR_REG;
237 if (img->buf_type == G2D_IMGBUF_USERPTR)
238 g2d_add_cmd(ctx, cmd | G2D_BUF_USERPTR,
239 (unsigned long)&img->user_ptr[0]);
241 g2d_add_cmd(ctx, cmd, img->bo[0]);
245 * g2d_reset - reset fimg2d hardware.
247 * @ctx: a pointer to g2d_context structure.
250 static void g2d_reset(struct g2d_context *ctx)
255 g2d_add_cmd(ctx, SOFT_RESET_REG, 0x01);
259 * g2d_flush - submit all commands and values in user side command buffer
260 * to command queue aware of fimg2d dma.
262 * @ctx: a pointer to g2d_context structure.
264 * This function should be called after all commands and values to user
265 * side command buffer are set. It submits that buffer to the kernel side driver.
267 static int g2d_flush(struct g2d_context *ctx)
270 struct drm_exynos_g2d_set_cmdlist cmdlist = {0};
272 if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
275 if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
276 fprintf(stderr, MSG_PREFIX "command list overflow.\n");
280 cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
281 cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
282 cmdlist.cmd_nr = ctx->cmd_nr;
283 cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
285 if (ctx->event_userdata) {
286 cmdlist.event_type = G2D_EVENT_NONSTOP;
287 cmdlist.user_data = (uint64_t)(uintptr_t)(ctx->event_userdata);
288 ctx->event_userdata = NULL;
290 cmdlist.event_type = G2D_EVENT_NOT;
291 cmdlist.user_data = 0;
297 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
299 fprintf(stderr, MSG_PREFIX "failed to set cmdlist.\n");
309 * g2d_init - create a new g2d context and get hardware version.
311 * fd: a file descriptor to an opened drm device.
313 struct g2d_context *g2d_init(int fd)
315 struct drm_exynos_g2d_get_ver ver;
316 struct g2d_context *ctx;
319 ctx = calloc(1, sizeof(*ctx));
321 fprintf(stderr, MSG_PREFIX "failed to allocate context.\n");
327 ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
329 fprintf(stderr, MSG_PREFIX "failed to get version.\n");
334 ctx->major = ver.major;
335 ctx->minor = ver.minor;
337 printf(MSG_PREFIX "G2D version (%d.%d).\n", ctx->major, ctx->minor);
341 void g2d_fini(struct g2d_context *ctx)
347 * g2d_config_event - setup userdata configuration for a g2d event.
348 * The next invocation of a g2d call (e.g. g2d_solid_fill) is
349 * then going to flag the command buffer as 'nonstop'.
350 * Completion of the command buffer execution can then be
351 * determined by using drmHandleEvent on the DRM fd.
352 * The userdata is 'consumed' in the process.
354 * @ctx: a pointer to g2d_context structure.
355 * @userdata: a pointer to the user data
357 void g2d_config_event(struct g2d_context *ctx, void *userdata)
359 ctx->event_userdata = userdata;
363 * g2d_exec - start the dma to process all commands summited by g2d_flush().
365 * @ctx: a pointer to g2d_context structure.
367 int g2d_exec(struct g2d_context *ctx)
369 struct drm_exynos_g2d_exec exec;
372 if (ctx->cmdlist_nr == 0)
377 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
379 fprintf(stderr, MSG_PREFIX "failed to execute.\n");
389 * g2d_solid_fill - fill given buffer with given color data.
391 * @ctx: a pointer to g2d_context structure.
392 * @img: a pointer to g2d_image structure including image and buffer
394 * @x: x start position to buffer filled with given color data.
395 * @y: y start position to buffer filled with given color data.
396 * @w: width value to buffer filled with given color data.
397 * @h: height value to buffer filled with given color data.
400 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
401 unsigned int x, unsigned int y, unsigned int w,
404 union g2d_bitblt_cmd_val bitblt;
405 union g2d_point_val pt;
407 if (g2d_check_space(ctx, 7, 1))
410 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
411 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
412 g2d_add_base_addr(ctx, img, g2d_dst);
413 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
415 if (x + w > img->width)
417 if (y + h > img->height)
422 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
426 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
428 g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
431 bitblt.data.fast_solid_color_fill_en = 1;
432 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
434 return g2d_flush(ctx);
438 * g2d_copy - copy contents in source buffer to destination buffer.
440 * @ctx: a pointer to g2d_context structure.
441 * @src: a pointer to g2d_image structure including image and buffer
442 * information to source.
443 * @dst: a pointer to g2d_image structure including image and buffer
444 * information to destination.
445 * @src_x: x start position to source buffer.
446 * @src_y: y start position to source buffer.
447 * @dst_x: x start position to destination buffer.
448 * @dst_y: y start position to destination buffer.
449 * @w: width value to source and destination buffers.
450 * @h: height value to source and destination buffers.
453 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
454 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
455 unsigned int dst_x, unsigned dst_y, unsigned int w,
458 union g2d_rop4_val rop4;
459 union g2d_point_val pt;
460 unsigned int src_w, src_h, dst_w, dst_h;
464 if (src_x + src->width > w)
465 src_w = src->width - src_x;
466 if (src_y + src->height > h)
467 src_h = src->height - src_y;
471 if (dst_x + dst->width > w)
472 dst_w = dst->width - dst_x;
473 if (dst_y + dst->height > h)
474 dst_h = dst->height - dst_y;
476 w = MIN(src_w, dst_w);
477 h = MIN(src_h, dst_h);
479 if (w <= 0 || h <= 0) {
480 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
484 if (g2d_check_space(ctx, 11, 2))
487 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
488 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
489 g2d_add_base_addr(ctx, dst, g2d_dst);
490 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
492 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
493 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
494 g2d_add_base_addr(ctx, src, g2d_src);
495 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
499 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
500 pt.data.x = src_x + w;
501 pt.data.y = src_y + h;
502 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
506 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
507 pt.data.x = dst_x + w;
508 pt.data.y = dst_y + h;
509 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
512 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
513 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
515 return g2d_flush(ctx);
519 * g2d_copy_with_scale - copy contents in source buffer to destination buffer
520 * scaling up or down properly.
522 * @ctx: a pointer to g2d_context structure.
523 * @src: a pointer to g2d_image structure including image and buffer
524 * information to source.
525 * @dst: a pointer to g2d_image structure including image and buffer
526 * information to destination.
527 * @src_x: x start position to source buffer.
528 * @src_y: y start position to source buffer.
529 * @src_w: width value to source buffer.
530 * @src_h: height value to source buffer.
531 * @dst_x: x start position to destination buffer.
532 * @dst_y: y start position to destination buffer.
533 * @dst_w: width value to destination buffer.
534 * @dst_h: height value to destination buffer.
535 * @negative: indicate that it uses color negative to source and
536 * destination buffers.
539 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
540 struct g2d_image *dst, unsigned int src_x,
541 unsigned int src_y, unsigned int src_w,
542 unsigned int src_h, unsigned int dst_x,
543 unsigned int dst_y, unsigned int dst_w,
544 unsigned int dst_h, unsigned int negative)
546 union g2d_rop4_val rop4;
547 union g2d_point_val pt;
548 unsigned int scale, repeat_pad;
549 unsigned int scale_x, scale_y;
551 /* Sanitize this parameter to facilitate space computation below. */
555 if (src_w == dst_w && src_h == dst_h)
559 scale_x = g2d_get_scaling(src_w, dst_w);
560 scale_y = g2d_get_scaling(src_h, dst_h);
563 repeat_pad = src->repeat_mode == G2D_REPEAT_MODE_PAD ? 1 : 0;
565 if (src_x + src_w > src->width)
566 src_w = src->width - src_x;
567 if (src_y + src_h > src->height)
568 src_h = src->height - src_y;
570 if (dst_x + dst_w > dst->width)
571 dst_w = dst->width - dst_x;
572 if (dst_y + dst_h > dst->height)
573 dst_h = dst->height - dst_y;
575 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
576 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
580 if (g2d_check_space(ctx, 12 + scale * 3 + negative + repeat_pad, 2))
583 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
584 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
585 g2d_add_base_addr(ctx, dst, g2d_dst);
586 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
588 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
589 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
591 g2d_add_cmd(ctx, SRC_REPEAT_MODE_REG, src->repeat_mode);
593 g2d_add_cmd(ctx, SRC_PAD_VALUE_REG, dst->color);
595 g2d_add_base_addr(ctx, src, g2d_src);
596 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
599 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
602 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
603 rop4.data.unmasked_rop3 ^= G2D_ROP3_DST;
606 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
609 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
610 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
611 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
616 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
617 pt.data.x = src_x + src_w;
618 pt.data.y = src_y + src_h;
619 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
623 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
624 pt.data.x = dst_x + dst_w;
625 pt.data.y = dst_y + dst_h;
626 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
628 return g2d_flush(ctx);
632 * g2d_blend - blend image data in source and destination buffers.
634 * @ctx: a pointer to g2d_context structure.
635 * @src: a pointer to g2d_image structure including image and buffer
636 * information to source.
637 * @dst: a pointer to g2d_image structure including image and buffer
638 * information to destination.
639 * @src_x: x start position to source buffer.
640 * @src_y: y start position to source buffer.
641 * @dst_x: x start position to destination buffer.
642 * @dst_y: y start position to destination buffer.
643 * @w: width value to source and destination buffer.
644 * @h: height value to source and destination buffer.
645 * @op: blend operation type.
648 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
649 struct g2d_image *dst, unsigned int src_x,
650 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
651 unsigned int w, unsigned int h, enum e_g2d_op op)
653 union g2d_point_val pt;
654 union g2d_bitblt_cmd_val bitblt;
655 union g2d_blend_func_val blend;
656 unsigned int gem_space;
657 unsigned int src_w, src_h, dst_w, dst_h;
661 if (src_x + w > src->width)
662 src_w = src->width - src_x;
663 if (src_y + h > src->height)
664 src_h = src->height - src_y;
668 if (dst_x + w > dst->width)
669 dst_w = dst->width - dst_x;
670 if (dst_y + h > dst->height)
671 dst_h = dst->height - dst_y;
673 w = MIN(src_w, dst_w);
674 h = MIN(src_h, dst_h);
676 if (w <= 0 || h <= 0) {
677 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
681 if (!g2d_validate_select_mode(src->select_mode)) {
682 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
686 if (!g2d_validate_blending_op(op)) {
687 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
691 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
693 if (g2d_check_space(ctx, 12, gem_space))
699 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
700 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
702 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
704 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
705 g2d_add_base_addr(ctx, dst, g2d_dst);
706 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
708 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
709 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
711 switch (src->select_mode) {
712 case G2D_SELECT_MODE_NORMAL:
713 g2d_add_base_addr(ctx, src, g2d_src);
714 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
716 case G2D_SELECT_MODE_FGCOLOR:
717 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
719 case G2D_SELECT_MODE_BGCOLOR:
720 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
724 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
725 blend.val = g2d_get_blend_op(op);
726 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
727 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
731 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
732 pt.data.x = src_x + w;
733 pt.data.y = src_y + h;
734 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
738 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
739 pt.data.x = dst_x + w;
740 pt.data.y = dst_y + h;
741 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
743 return g2d_flush(ctx);
747 * g2d_scale_and_blend - apply scaling to source buffer and then blend to destination buffer
749 * @ctx: a pointer to g2d_context structure.
750 * @src: a pointer to g2d_image structure including image and buffer
751 * information to source.
752 * @dst: a pointer to g2d_image structure including image and buffer
753 * information to destination.
754 * @src_x: x start position to source buffer.
755 * @src_y: y start position to source buffer.
756 * @src_w: width value to source buffer.
757 * @src_h: height value to source buffer.
758 * @dst_x: x start position to destination buffer.
759 * @dst_y: y start position to destination buffer.
760 * @dst_w: width value to destination buffer.
761 * @dst_h: height value to destination buffer.
762 * @op: blend operation type.
765 g2d_scale_and_blend(struct g2d_context *ctx, struct g2d_image *src,
766 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
767 unsigned int src_w, unsigned int src_h, unsigned int dst_x,
768 unsigned int dst_y, unsigned int dst_w, unsigned int dst_h,
771 union g2d_point_val pt;
772 union g2d_bitblt_cmd_val bitblt;
773 union g2d_blend_func_val blend;
774 unsigned int scale, gem_space;
775 unsigned int scale_x, scale_y;
777 if (src_w == dst_w && src_h == dst_h)
781 scale_x = g2d_get_scaling(src_w, dst_w);
782 scale_y = g2d_get_scaling(src_h, dst_h);
785 if (src_x + src_w > src->width)
786 src_w = src->width - src_x;
787 if (src_y + src_h > src->height)
788 src_h = src->height - src_y;
790 if (dst_x + dst_w > dst->width)
791 dst_w = dst->width - dst_x;
792 if (dst_y + dst_h > dst->height)
793 dst_h = dst->height - dst_y;
795 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
796 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
800 if (!g2d_validate_select_mode(src->select_mode)) {
801 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
805 if (!g2d_validate_blending_op(op)) {
806 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
810 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
812 if (g2d_check_space(ctx, 12 + scale * 3, gem_space))
818 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
819 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
821 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
823 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
824 g2d_add_base_addr(ctx, dst, g2d_dst);
825 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
827 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
828 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
830 switch (src->select_mode) {
831 case G2D_SELECT_MODE_NORMAL:
832 g2d_add_base_addr(ctx, src, g2d_src);
833 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
835 case G2D_SELECT_MODE_FGCOLOR:
836 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
838 case G2D_SELECT_MODE_BGCOLOR:
839 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
844 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
845 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
846 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
849 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
850 blend.val = g2d_get_blend_op(op);
851 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
852 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
856 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
857 pt.data.x = src_x + src_w;
858 pt.data.y = src_y + src_h;
859 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
863 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
864 pt.data.x = dst_x + dst_w;
865 pt.data.y = dst_y + dst_h;
866 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
868 return g2d_flush(ctx);