2 * Copyright (C) 2013 Samsung Electronics Co.Ltd
4 * Inki Dae <inki.dae@samsung.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
33 #include <linux/stddef.h>
37 #include "libdrm_macros.h"
38 #include "exynos_drm.h"
39 #include "fimg2d_reg.h"
40 #include "exynos_fimg2d.h"
42 #define SET_BF(val, sc, si, scsa, scda, dc, di, dcsa, dcda) \
43 val.data.src_coeff = sc; \
44 val.data.inv_src_color_coeff = si; \
45 val.data.src_coeff_src_a = scsa; \
46 val.data.src_coeff_dst_a = scda; \
47 val.data.dst_coeff = dc; \
48 val.data.inv_dst_color_coeff = di; \
49 val.data.dst_coeff_src_a = dcsa; \
50 val.data.dst_coeff_dst_a = dcda;
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 #define MSG_PREFIX "exynos/fimg2d: "
56 #define G2D_MAX_CMD_NR 64
57 #define G2D_MAX_GEM_CMD_NR 64
58 #define G2D_MAX_CMD_LIST_NR 64
64 struct drm_exynos_g2d_cmd cmd[G2D_MAX_CMD_NR];
65 struct drm_exynos_g2d_cmd cmd_buf[G2D_MAX_GEM_CMD_NR];
67 unsigned int cmd_buf_nr;
68 unsigned int cmdlist_nr;
72 enum g2d_base_addr_reg {
78 G2D_DIR_MODE_POSITIVE = 0,
79 G2D_DIR_MODE_NEGATIVE = 1
82 union g2d_direction_val {
85 /* SRC_MSK_DIRECT_REG [0:1] (source) */
86 enum e_g2d_dir_mode src_x_direction:1;
87 enum e_g2d_dir_mode src_y_direction:1;
89 /* SRC_MSK_DIRECT_REG [2:3] */
90 unsigned int reversed1:2;
92 /* SRC_MSK_DIRECT_REG [4:5] (mask) */
93 enum e_g2d_dir_mode mask_x_direction:1;
94 enum e_g2d_dir_mode mask_y_direction:1;
96 /* SRC_MSK_DIRECT_REG [6:31] */
97 unsigned int padding1:26;
99 /* DST_PAT_DIRECT_REG [0:1] (destination) */
100 enum e_g2d_dir_mode dst_x_direction:1;
101 enum e_g2d_dir_mode dst_y_direction:1;
103 /* DST_PAT_DIRECT_REG [2:3] */
104 unsigned int reversed2:2;
106 /* DST_PAT_DIRECT_REG [4:5] (pattern) */
107 enum e_g2d_dir_mode pat_x_direction:1;
108 enum e_g2d_dir_mode pat_y_direction:1;
110 /* DST_PAT_DIRECT_REG [6:31] */
111 unsigned int padding2:26;
115 static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
118 * The G2D hw scaling factor is a normalized inverse of the scaling factor.
119 * For example: When source width is 100 and destination width is 200
120 * (scaling of 2x), then the hw factor is NC * 100 / 200.
121 * The normalization factor (NC) is 2^16 = 0x10000.
124 return ((src << 16) / dst);
127 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
129 union g2d_blend_func_val val;
134 * The switch statement is missing the default branch since
135 * we assume that the caller checks the blending operation
136 * via g2d_validate_blending_op() first.
140 case G2D_OP_DISJOINT_CLEAR:
141 case G2D_OP_CONJOINT_CLEAR:
142 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
146 case G2D_OP_DISJOINT_SRC:
147 case G2D_OP_CONJOINT_SRC:
148 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
152 case G2D_OP_DISJOINT_DST:
153 case G2D_OP_CONJOINT_DST:
154 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
158 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
159 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
161 case G2D_OP_INTERPOLATE:
162 SET_BF(val, G2D_COEFF_MODE_SRC_ALPHA, 0, 0, 0,
163 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
171 * g2d_check_space - check if command buffers have enough space left.
173 * @ctx: a pointer to g2d_context structure.
174 * @num_cmds: number of (regular) commands.
175 * @num_gem_cmds: number of GEM commands.
177 static unsigned int g2d_check_space(const struct g2d_context *ctx,
178 unsigned int num_cmds, unsigned int num_gem_cmds)
180 if (ctx->cmd_nr + num_cmds >= G2D_MAX_CMD_NR ||
181 ctx->cmd_buf_nr + num_gem_cmds >= G2D_MAX_GEM_CMD_NR)
188 * g2d_validate_select_mode - validate select mode.
190 * @mode: the mode to validate
192 * Returns zero for an invalid mode and one otherwise.
194 static int g2d_validate_select_mode(
195 enum e_g2d_select_mode mode)
198 case G2D_SELECT_MODE_NORMAL:
199 case G2D_SELECT_MODE_FGCOLOR:
200 case G2D_SELECT_MODE_BGCOLOR:
208 * g2d_validate_blending_op - validate blending operation.
210 * @operation: the operation to validate
212 * Returns zero for an invalid mode and one otherwise.
214 static int g2d_validate_blending_op(
215 enum e_g2d_op operation)
222 case G2D_OP_INTERPOLATE:
223 case G2D_OP_DISJOINT_CLEAR:
224 case G2D_OP_DISJOINT_SRC:
225 case G2D_OP_DISJOINT_DST:
226 case G2D_OP_CONJOINT_CLEAR:
227 case G2D_OP_CONJOINT_SRC:
228 case G2D_OP_CONJOINT_DST:
236 * g2d_add_cmd - set given command and value to user side command buffer.
238 * @ctx: a pointer to g2d_context structure.
239 * @cmd: command data.
240 * @value: value data.
242 * The caller has to make sure that the commands buffers have enough space
243 * left to hold the command. Use g2d_check_space() to ensure this.
245 static void g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
248 switch (cmd & ~(G2D_BUF_USERPTR)) {
249 case SRC_BASE_ADDR_REG:
250 case SRC_PLANE2_BASE_ADDR_REG:
251 case DST_BASE_ADDR_REG:
252 case DST_PLANE2_BASE_ADDR_REG:
253 case PAT_BASE_ADDR_REG:
254 case MASK_BASE_ADDR_REG:
255 assert(ctx->cmd_buf_nr < G2D_MAX_GEM_CMD_NR);
257 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
258 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
262 assert(ctx->cmd_nr < G2D_MAX_CMD_NR);
264 ctx->cmd[ctx->cmd_nr].offset = cmd;
265 ctx->cmd[ctx->cmd_nr].data = value;
272 * g2d_add_base_addr - helper function to set dst/src base address register.
274 * @ctx: a pointer to g2d_context structure.
275 * @img: a pointer to the dst/src g2d_image structure.
276 * @reg: the register that should be set.
278 static void g2d_add_base_addr(struct g2d_context *ctx, struct g2d_image *img,
279 enum g2d_base_addr_reg reg)
281 const unsigned long cmd = (reg == g2d_dst) ?
282 DST_BASE_ADDR_REG : SRC_BASE_ADDR_REG;
284 if (img->buf_type == G2D_IMGBUF_USERPTR)
285 g2d_add_cmd(ctx, cmd | G2D_BUF_USERPTR,
286 (unsigned long)&img->user_ptr[0]);
288 g2d_add_cmd(ctx, cmd, img->bo[0]);
292 * g2d_set_direction - setup direction register (useful for overlapping blits).
294 * @ctx: a pointer to g2d_context structure.
295 * @dir: a pointer to the g2d_direction_val structure.
297 static void g2d_set_direction(struct g2d_context *ctx,
298 const union g2d_direction_val *dir)
300 g2d_add_cmd(ctx, SRC_MASK_DIRECT_REG, dir->val[0]);
301 g2d_add_cmd(ctx, DST_PAT_DIRECT_REG, dir->val[1]);
305 * g2d_flush - submit all commands and values in user side command buffer
306 * to command queue aware of fimg2d dma.
308 * @ctx: a pointer to g2d_context structure.
310 * This function should be called after all commands and values to user
311 * side command buffer are set. It submits that buffer to the kernel side driver.
313 static int g2d_flush(struct g2d_context *ctx)
316 struct drm_exynos_g2d_set_cmdlist cmdlist = {0};
318 if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
321 if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
322 fprintf(stderr, MSG_PREFIX "command list overflow.\n");
326 cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
327 cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
328 cmdlist.cmd_nr = ctx->cmd_nr;
329 cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
331 if (ctx->event_userdata) {
332 cmdlist.event_type = G2D_EVENT_NONSTOP;
333 cmdlist.user_data = (uint64_t)(uintptr_t)(ctx->event_userdata);
334 ctx->event_userdata = NULL;
336 cmdlist.event_type = G2D_EVENT_NOT;
337 cmdlist.user_data = 0;
343 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
345 fprintf(stderr, MSG_PREFIX "failed to set cmdlist.\n");
355 * g2d_init - create a new g2d context and get hardware version.
357 * fd: a file descriptor to an opened drm device.
359 struct g2d_context *g2d_init(int fd)
361 struct drm_exynos_g2d_get_ver ver;
362 struct g2d_context *ctx;
365 ctx = calloc(1, sizeof(*ctx));
367 fprintf(stderr, MSG_PREFIX "failed to allocate context.\n");
373 ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
375 fprintf(stderr, MSG_PREFIX "failed to get version.\n");
380 ctx->major = ver.major;
381 ctx->minor = ver.minor;
383 printf(MSG_PREFIX "G2D version (%d.%d).\n", ctx->major, ctx->minor);
387 void g2d_fini(struct g2d_context *ctx)
393 * g2d_config_event - setup userdata configuration for a g2d event.
394 * The next invocation of a g2d call (e.g. g2d_solid_fill) is
395 * then going to flag the command buffer as 'nonstop'.
396 * Completion of the command buffer execution can then be
397 * determined by using drmHandleEvent on the DRM fd.
398 * The userdata is 'consumed' in the process.
400 * @ctx: a pointer to g2d_context structure.
401 * @userdata: a pointer to the user data
403 void g2d_config_event(struct g2d_context *ctx, void *userdata)
405 ctx->event_userdata = userdata;
409 * g2d_exec - start the dma to process all commands summited by g2d_flush().
411 * @ctx: a pointer to g2d_context structure.
413 int g2d_exec(struct g2d_context *ctx)
415 struct drm_exynos_g2d_exec exec;
418 if (ctx->cmdlist_nr == 0)
423 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
425 fprintf(stderr, MSG_PREFIX "failed to execute.\n");
435 * g2d_solid_fill - fill given buffer with given color data.
437 * @ctx: a pointer to g2d_context structure.
438 * @img: a pointer to g2d_image structure including image and buffer
440 * @x: x start position to buffer filled with given color data.
441 * @y: y start position to buffer filled with given color data.
442 * @w: width value to buffer filled with given color data.
443 * @h: height value to buffer filled with given color data.
446 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
447 unsigned int x, unsigned int y, unsigned int w,
450 union g2d_bitblt_cmd_val bitblt;
451 union g2d_point_val pt;
453 if (g2d_check_space(ctx, 7, 1))
456 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
457 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
458 g2d_add_base_addr(ctx, img, g2d_dst);
459 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
461 if (x + w > img->width)
463 if (y + h > img->height)
468 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
472 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
474 g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
477 bitblt.data.fast_solid_color_fill_en = 1;
478 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
480 return g2d_flush(ctx);
484 * g2d_copy - copy contents in source buffer to destination buffer.
486 * @ctx: a pointer to g2d_context structure.
487 * @src: a pointer to g2d_image structure including image and buffer
488 * information to source.
489 * @dst: a pointer to g2d_image structure including image and buffer
490 * information to destination.
491 * @src_x: x start position to source buffer.
492 * @src_y: y start position to source buffer.
493 * @dst_x: x start position to destination buffer.
494 * @dst_y: y start position to destination buffer.
495 * @w: width value to source and destination buffers.
496 * @h: height value to source and destination buffers.
499 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
500 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
501 unsigned int dst_x, unsigned dst_y, unsigned int w,
504 union g2d_rop4_val rop4;
505 union g2d_point_val pt;
506 unsigned int src_w, src_h, dst_w, dst_h;
510 if (src_x + src->width > w)
511 src_w = src->width - src_x;
512 if (src_y + src->height > h)
513 src_h = src->height - src_y;
517 if (dst_x + dst->width > w)
518 dst_w = dst->width - dst_x;
519 if (dst_y + dst->height > h)
520 dst_h = dst->height - dst_y;
522 w = MIN(src_w, dst_w);
523 h = MIN(src_h, dst_h);
525 if (w <= 0 || h <= 0) {
526 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
530 if (g2d_check_space(ctx, 11, 2))
533 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
534 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
535 g2d_add_base_addr(ctx, dst, g2d_dst);
536 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
538 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
539 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
540 g2d_add_base_addr(ctx, src, g2d_src);
541 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
545 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
546 pt.data.x = src_x + w;
547 pt.data.y = src_y + h;
548 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
552 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
553 pt.data.x = dst_x + w;
554 pt.data.y = dst_y + h;
555 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
558 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
559 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
561 return g2d_flush(ctx);
565 * g2d_move - copy content inside single buffer.
566 * Similar to libc's memmove() this copies a rectangular
567 * region of the provided buffer to another location, while
568 * properly handling the situation where source and
569 * destination rectangle overlap.
571 * @ctx: a pointer to g2d_context structure.
572 * @img: a pointer to g2d_image structure providing
573 * buffer information.
574 * @src_x: x position of source rectangle.
575 * @src_y: y position of source rectangle.
576 * @dst_x: x position of destination rectangle.
577 * @dst_y: y position of destination rectangle.
578 * @w: width of rectangle to move.
579 * @h: height of rectangle to move.
582 g2d_move(struct g2d_context *ctx, struct g2d_image *img,
583 unsigned int src_x, unsigned int src_y,
584 unsigned int dst_x, unsigned dst_y, unsigned int w,
587 union g2d_rop4_val rop4;
588 union g2d_point_val pt;
589 union g2d_direction_val dir;
590 unsigned int src_w, src_h, dst_w, dst_h;
594 if (src_x + img->width > w)
595 src_w = img->width - src_x;
596 if (src_y + img->height > h)
597 src_h = img->height - src_y;
601 if (dst_x + img->width > w)
602 dst_w = img->width - dst_x;
603 if (dst_y + img->height > h)
604 dst_h = img->height - dst_y;
606 w = MIN(src_w, dst_w);
607 h = MIN(src_h, dst_h);
609 if (w == 0 || h == 0) {
610 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
614 if (g2d_check_space(ctx, 13, 2))
617 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
618 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
620 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
621 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, img->color_mode);
623 g2d_add_base_addr(ctx, img, g2d_dst);
624 g2d_add_base_addr(ctx, img, g2d_src);
626 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
627 g2d_add_cmd(ctx, SRC_STRIDE_REG, img->stride);
629 dir.val[0] = dir.val[1] = 0;
632 dir.data.src_x_direction = dir.data.dst_x_direction = 1;
634 dir.data.src_y_direction = dir.data.dst_y_direction = 1;
636 g2d_set_direction(ctx, &dir);
640 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
641 pt.data.x = src_x + w;
642 pt.data.y = src_y + h;
643 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
647 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
648 pt.data.x = dst_x + w;
649 pt.data.y = dst_y + h;
650 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
653 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
654 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
656 return g2d_flush(ctx);
660 * g2d_copy_with_scale - copy contents in source buffer to destination buffer
661 * scaling up or down properly.
663 * @ctx: a pointer to g2d_context structure.
664 * @src: a pointer to g2d_image structure including image and buffer
665 * information to source.
666 * @dst: a pointer to g2d_image structure including image and buffer
667 * information to destination.
668 * @src_x: x start position to source buffer.
669 * @src_y: y start position to source buffer.
670 * @src_w: width value to source buffer.
671 * @src_h: height value to source buffer.
672 * @dst_x: x start position to destination buffer.
673 * @dst_y: y start position to destination buffer.
674 * @dst_w: width value to destination buffer.
675 * @dst_h: height value to destination buffer.
676 * @negative: indicate that it uses color negative to source and
677 * destination buffers.
680 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
681 struct g2d_image *dst, unsigned int src_x,
682 unsigned int src_y, unsigned int src_w,
683 unsigned int src_h, unsigned int dst_x,
684 unsigned int dst_y, unsigned int dst_w,
685 unsigned int dst_h, unsigned int negative)
687 union g2d_rop4_val rop4;
688 union g2d_point_val pt;
689 unsigned int scale, repeat_pad;
690 unsigned int scale_x, scale_y;
692 /* Sanitize this parameter to facilitate space computation below. */
696 if (src_w == dst_w && src_h == dst_h)
700 scale_x = g2d_get_scaling(src_w, dst_w);
701 scale_y = g2d_get_scaling(src_h, dst_h);
704 repeat_pad = src->repeat_mode == G2D_REPEAT_MODE_PAD ? 1 : 0;
706 if (src_x + src_w > src->width)
707 src_w = src->width - src_x;
708 if (src_y + src_h > src->height)
709 src_h = src->height - src_y;
711 if (dst_x + dst_w > dst->width)
712 dst_w = dst->width - dst_x;
713 if (dst_y + dst_h > dst->height)
714 dst_h = dst->height - dst_y;
716 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
717 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
721 if (g2d_check_space(ctx, 12 + scale * 3 + negative + repeat_pad, 2))
724 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
725 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
726 g2d_add_base_addr(ctx, dst, g2d_dst);
727 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
729 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
730 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
732 g2d_add_cmd(ctx, SRC_REPEAT_MODE_REG, src->repeat_mode);
734 g2d_add_cmd(ctx, SRC_PAD_VALUE_REG, dst->color);
736 g2d_add_base_addr(ctx, src, g2d_src);
737 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
740 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
743 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
744 rop4.data.unmasked_rop3 ^= G2D_ROP3_DST;
747 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
750 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
751 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
752 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
757 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
758 pt.data.x = src_x + src_w;
759 pt.data.y = src_y + src_h;
760 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
764 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
765 pt.data.x = dst_x + dst_w;
766 pt.data.y = dst_y + dst_h;
767 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
769 return g2d_flush(ctx);
773 * g2d_blend - blend image data in source and destination buffers.
775 * @ctx: a pointer to g2d_context structure.
776 * @src: a pointer to g2d_image structure including image and buffer
777 * information to source.
778 * @dst: a pointer to g2d_image structure including image and buffer
779 * information to destination.
780 * @src_x: x start position to source buffer.
781 * @src_y: y start position to source buffer.
782 * @dst_x: x start position to destination buffer.
783 * @dst_y: y start position to destination buffer.
784 * @w: width value to source and destination buffer.
785 * @h: height value to source and destination buffer.
786 * @op: blend operation type.
789 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
790 struct g2d_image *dst, unsigned int src_x,
791 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
792 unsigned int w, unsigned int h, enum e_g2d_op op)
794 union g2d_point_val pt;
795 union g2d_bitblt_cmd_val bitblt;
796 union g2d_blend_func_val blend;
797 unsigned int gem_space;
798 unsigned int src_w, src_h, dst_w, dst_h;
802 if (src_x + w > src->width)
803 src_w = src->width - src_x;
804 if (src_y + h > src->height)
805 src_h = src->height - src_y;
809 if (dst_x + w > dst->width)
810 dst_w = dst->width - dst_x;
811 if (dst_y + h > dst->height)
812 dst_h = dst->height - dst_y;
814 w = MIN(src_w, dst_w);
815 h = MIN(src_h, dst_h);
817 if (w <= 0 || h <= 0) {
818 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
822 if (!g2d_validate_select_mode(src->select_mode)) {
823 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
827 if (!g2d_validate_blending_op(op)) {
828 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
832 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
834 if (g2d_check_space(ctx, 12, gem_space))
840 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
841 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
843 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
845 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
846 g2d_add_base_addr(ctx, dst, g2d_dst);
847 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
849 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
850 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
852 switch (src->select_mode) {
853 case G2D_SELECT_MODE_NORMAL:
854 g2d_add_base_addr(ctx, src, g2d_src);
855 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
857 case G2D_SELECT_MODE_FGCOLOR:
858 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
860 case G2D_SELECT_MODE_BGCOLOR:
861 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
865 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
866 blend.val = g2d_get_blend_op(op);
867 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
868 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
872 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
873 pt.data.x = src_x + w;
874 pt.data.y = src_y + h;
875 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
879 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
880 pt.data.x = dst_x + w;
881 pt.data.y = dst_y + h;
882 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
884 return g2d_flush(ctx);
888 * g2d_scale_and_blend - apply scaling to source buffer and then blend to destination buffer
890 * @ctx: a pointer to g2d_context structure.
891 * @src: a pointer to g2d_image structure including image and buffer
892 * information to source.
893 * @dst: a pointer to g2d_image structure including image and buffer
894 * information to destination.
895 * @src_x: x start position to source buffer.
896 * @src_y: y start position to source buffer.
897 * @src_w: width value to source buffer.
898 * @src_h: height value to source buffer.
899 * @dst_x: x start position to destination buffer.
900 * @dst_y: y start position to destination buffer.
901 * @dst_w: width value to destination buffer.
902 * @dst_h: height value to destination buffer.
903 * @op: blend operation type.
906 g2d_scale_and_blend(struct g2d_context *ctx, struct g2d_image *src,
907 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
908 unsigned int src_w, unsigned int src_h, unsigned int dst_x,
909 unsigned int dst_y, unsigned int dst_w, unsigned int dst_h,
912 union g2d_point_val pt;
913 union g2d_bitblt_cmd_val bitblt;
914 union g2d_blend_func_val blend;
915 unsigned int scale, gem_space;
916 unsigned int scale_x, scale_y;
918 if (src_w == dst_w && src_h == dst_h)
922 scale_x = g2d_get_scaling(src_w, dst_w);
923 scale_y = g2d_get_scaling(src_h, dst_h);
926 if (src_x + src_w > src->width)
927 src_w = src->width - src_x;
928 if (src_y + src_h > src->height)
929 src_h = src->height - src_y;
931 if (dst_x + dst_w > dst->width)
932 dst_w = dst->width - dst_x;
933 if (dst_y + dst_h > dst->height)
934 dst_h = dst->height - dst_y;
936 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
937 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
941 if (!g2d_validate_select_mode(src->select_mode)) {
942 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
946 if (!g2d_validate_blending_op(op)) {
947 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
951 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
953 if (g2d_check_space(ctx, 12 + scale * 3, gem_space))
959 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
960 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
962 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
964 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
965 g2d_add_base_addr(ctx, dst, g2d_dst);
966 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
968 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
969 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
971 switch (src->select_mode) {
972 case G2D_SELECT_MODE_NORMAL:
973 g2d_add_base_addr(ctx, src, g2d_src);
974 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
976 case G2D_SELECT_MODE_FGCOLOR:
977 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
979 case G2D_SELECT_MODE_BGCOLOR:
980 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
985 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
986 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
987 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
990 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
991 blend.val = g2d_get_blend_op(op);
992 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
993 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
997 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
998 pt.data.x = src_x + src_w;
999 pt.data.y = src_y + src_h;
1000 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
1004 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
1005 pt.data.x = dst_x + dst_w;
1006 pt.data.y = dst_y + dst_h;
1007 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
1009 return g2d_flush(ctx);