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.
23 #include <linux/stddef.h>
27 #include "exynos_drm.h"
28 #include "fimg2d_reg.h"
31 #define SET_BF(val, sc, si, scsa, scda, dc, di, dcsa, dcda) \
32 val.data.src_coeff = sc; \
33 val.data.inv_src_color_coeff = si; \
34 val.data.src_coeff_src_a = scsa; \
35 val.data.src_coeff_dst_a = scda; \
36 val.data.dst_coeff = dc; \
37 val.data.inv_dst_color_coeff = di; \
38 val.data.dst_coeff_src_a = dcsa; \
39 val.data.dst_coeff_dst_a = dcda;
41 #define MIN(a, b) ((a) < (b) ? (a) : (b))
43 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
45 union g2d_blend_func_val val;
51 case G2D_OP_DISJOINT_CLEAR:
52 case G2D_OP_CONJOINT_CLEAR:
53 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
57 case G2D_OP_DISJOINT_SRC:
58 case G2D_OP_CONJOINT_SRC:
59 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
63 case G2D_OP_DISJOINT_DST:
64 case G2D_OP_CONJOINT_DST:
65 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
69 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
70 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
73 fprintf(stderr, "Not support operation(%d).\n", op);
74 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
83 * g2d_add_cmd - set given command and value to user side command buffer.
85 * @ctx: a pointer to g2d_context structure.
89 static int g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
92 switch (cmd & ~(G2D_BUF_USERPTR)) {
93 case SRC_BASE_ADDR_REG:
94 case SRC_PLANE2_BASE_ADDR_REG:
95 case DST_BASE_ADDR_REG:
96 case DST_PLANE2_BASE_ADDR_REG:
97 case PAT_BASE_ADDR_REG:
98 case MASK_BASE_ADDR_REG:
99 if (ctx->cmd_buf_nr >= G2D_MAX_GEM_CMD_NR) {
100 fprintf(stderr, "Overflow cmd_gem size.\n");
104 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
105 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
109 if (ctx->cmd_nr >= G2D_MAX_CMD_NR) {
110 fprintf(stderr, "Overflow cmd size.\n");
114 ctx->cmd[ctx->cmd_nr].offset = cmd;
115 ctx->cmd[ctx->cmd_nr].data = value;
124 * g2d_reset - reset fimg2d hardware.
126 * @ctx: a pointer to g2d_context structure.
129 static void g2d_reset(struct g2d_context *ctx)
134 g2d_add_cmd(ctx, SOFT_RESET_REG, 0x01);
138 * g2d_flush - summit all commands and values in user side command buffer
139 * to command queue aware of fimg2d dma.
141 * @ctx: a pointer to g2d_context structure.
143 * This function should be called after all commands and values to user
144 * side command buffer is set to summit that buffer to kernel side driver.
146 static int g2d_flush(struct g2d_context *ctx)
149 struct drm_exynos_g2d_set_cmdlist cmdlist;
151 if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
154 if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
155 fprintf(stderr, "Overflow cmdlist.\n");
159 memset(&cmdlist, 0, sizeof(struct drm_exynos_g2d_set_cmdlist));
161 cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
162 cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
163 cmdlist.cmd_nr = ctx->cmd_nr;
164 cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
165 cmdlist.event_type = G2D_EVENT_NOT;
166 cmdlist.user_data = 0;
171 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
173 fprintf(stderr, "failed to set cmdlist.\n");
183 * g2d_init - create a new g2d context and get hardware version.
185 * fd: a file descriptor to drm device driver opened.
187 struct g2d_context *g2d_init(int fd)
189 struct drm_exynos_g2d_get_ver ver;
190 struct g2d_context *ctx;
193 ctx = calloc(1, sizeof(*ctx));
195 fprintf(stderr, "failed to allocate context.\n");
201 ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
203 fprintf(stderr, "failed to get version.\n");
208 ctx->major = ver.major;
209 ctx->minor = ver.minor;
211 printf("g2d version(%d.%d).\n", ctx->major, ctx->minor);
215 void g2d_fini(struct g2d_context *ctx)
222 * g2d_exec - start the dma to process all commands summited by g2d_flush().
224 * @ctx: a pointer to g2d_context structure.
226 int g2d_exec(struct g2d_context *ctx)
228 struct drm_exynos_g2d_exec exec;
231 if (ctx->cmdlist_nr == 0)
236 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
238 fprintf(stderr, "failed to execute.\n");
248 * g2d_solid_fill - fill given buffer with given color data.
250 * @ctx: a pointer to g2d_context structure.
251 * @img: a pointer to g2d_image structure including image and buffer
253 * @x: x start position to buffer filled with given color data.
254 * @y: y start position to buffer filled with given color data.
255 * @w: width value to buffer filled with given color data.
256 * @h: height value to buffer filled with given color data.
258 int g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
259 unsigned int x, unsigned int y, unsigned int w,
262 union g2d_bitblt_cmd_val bitblt;
263 union g2d_point_val pt;
265 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
266 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
268 if (img->buf_type == G2D_IMGBUF_USERPTR)
269 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
270 (unsigned long)&img->user_ptr[0]);
272 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, img->bo[0]);
274 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
276 if (x + w > img->width)
278 if (y + h > img->height)
284 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
290 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
292 g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
295 bitblt.data.fast_solid_color_fill_en = 1;
296 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
304 * g2d_copy - copy contents in source buffer to destination buffer.
306 * @ctx: a pointer to g2d_context structure.
307 * @src: a pointer to g2d_image structure including image and buffer
308 * information to source.
309 * @dst: a pointer to g2d_image structure including image and buffer
310 * information to destination.
311 * @src_x: x start position to source buffer.
312 * @src_y: y start position to source buffer.
313 * @dst_x: x start position to destination buffer.
314 * @dst_y: y start position to destination buffer.
315 * @w: width value to source and destination buffers.
316 * @h: height value to source and destination buffers.
318 int g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
319 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
320 unsigned int dst_x, unsigned dst_y, unsigned int w,
323 union g2d_rop4_val rop4;
324 union g2d_point_val pt;
325 unsigned int src_w = 0, src_h = 0, dst_w = 0, dst_h = 0;
327 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
328 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
329 if (dst->buf_type == G2D_IMGBUF_USERPTR)
330 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
331 (unsigned long)&dst->user_ptr[0]);
333 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
335 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
337 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
338 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
339 if (src->buf_type == G2D_IMGBUF_USERPTR)
340 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
341 (unsigned long)&src->user_ptr[0]);
343 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
345 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
349 if (src_x + src->width > w)
350 src_w = src->width - src_x;
351 if (src_y + src->height > h)
352 src_h = src->height - src_y;
356 if (dst_x + dst->width > w)
357 dst_w = dst->width - dst_x;
358 if (dst_y + dst->height > h)
359 dst_h = dst->height - dst_y;
361 w = MIN(src_w, dst_w);
362 h = MIN(src_h, dst_h);
364 if (w <= 0 || h <= 0) {
365 fprintf(stderr, "invalid width or height.\n");
373 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
375 pt.data.x = src_x + w;
376 pt.data.y = src_y + h;
377 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
382 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
384 pt.data.x = dst_x + w;
385 pt.data.y = dst_x + h;
386 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
389 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
390 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
398 * g2d_copy_with_scale - copy contents in source buffer to destination buffer
399 * scaling up or down properly.
401 * @ctx: a pointer to g2d_context structure.
402 * @src: a pointer to g2d_image structure including image and buffer
403 * information to source.
404 * @dst: a pointer to g2d_image structure including image and buffer
405 * information to destination.
406 * @src_x: x start position to source buffer.
407 * @src_y: y start position to source buffer.
408 * @src_w: width value to source buffer.
409 * @src_h: height value to source buffer.
410 * @dst_x: x start position to destination buffer.
411 * @dst_y: y start position to destination buffer.
412 * @dst_w: width value to destination buffer.
413 * @dst_h: height value to destination buffer.
414 * @negative: indicate that it uses color negative to source and
415 * destination buffers.
417 int g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
418 struct g2d_image *dst, unsigned int src_x,
419 unsigned int src_y, unsigned int src_w,
420 unsigned int src_h, unsigned int dst_x,
421 unsigned int dst_y, unsigned int dst_w,
422 unsigned int dst_h, unsigned int negative)
424 union g2d_rop4_val rop4;
425 union g2d_point_val pt;
427 double scale_x = 0.0f, scale_y = 0.0f;
429 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
430 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
431 if (dst->buf_type == G2D_IMGBUF_USERPTR)
432 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
433 (unsigned long)&dst->user_ptr[0]);
435 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
437 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
439 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
440 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
441 if (src->buf_type == G2D_IMGBUF_USERPTR)
442 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
443 (unsigned long)&src->user_ptr[0]);
445 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
447 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
449 if (src_w == dst_w && src_h == dst_h)
453 scale_x = (double)src_w / (double)dst_w;
454 scale_y = (double)src_w / (double)dst_h;
457 if (src_x + src_w > src->width)
458 src_w = src->width - src_x;
459 if (src_y + src_h > src->height)
460 src_h = src->height - src_y;
462 if (dst_x + dst_w > dst->width)
463 dst_w = dst->width - dst_x;
464 if (dst_y + dst_h > dst->height)
465 dst_h = dst->height - dst_y;
467 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
468 fprintf(stderr, "invalid width or height.\n");
474 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
476 rop4.data.unmasked_rop3 = G2D_ROP3_SRC^G2D_ROP3_DST;
477 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
480 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
481 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
485 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
486 g2d_add_cmd(ctx, SRC_XSCALE_REG, G2D_DOUBLE_TO_FIXED(scale_x));
487 g2d_add_cmd(ctx, SRC_YSCALE_REG, G2D_DOUBLE_TO_FIXED(scale_y));
493 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
495 pt.data.x = src_x + src_w;
496 pt.data.y = src_y + src_h;
497 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
502 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
504 pt.data.x = dst_x + dst_w;
505 pt.data.y = dst_y + dst_h;
506 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
514 * g2d_blend - blend image data in source and destion buffers
516 * @ctx: a pointer to g2d_context structure.
517 * @src: a pointer to g2d_image structure including image and buffer
518 * information to source.
519 * @dst: a pointer to g2d_image structure including image and buffer
520 * information to destination.
521 * @src_x: x start position to source buffer.
522 * @src_y: y start position to source buffer.
523 * @dst_x: x start position to destination buffer.
524 * @dst_y: y start position to destination buffer.
525 * @w: width value to source and destination buffer.
526 * @h: height value to source and destination buffer.
527 * @op: blend operation type.
529 int g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
530 struct g2d_image *dst, unsigned int src_x,
531 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
532 unsigned int w, unsigned int h, enum e_g2d_op op)
534 union g2d_point_val pt;
535 union g2d_bitblt_cmd_val bitblt;
536 union g2d_blend_func_val blend;
537 unsigned int src_w = 0, src_h = 0, dst_w = 0, dst_h = 0;
542 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
543 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
545 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
547 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
548 if (dst->buf_type == G2D_IMGBUF_USERPTR)
549 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
550 (unsigned long)&dst->user_ptr[0]);
552 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
554 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
556 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
557 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
559 switch (src->select_mode) {
560 case G2D_SELECT_MODE_NORMAL:
561 if (src->buf_type == G2D_IMGBUF_USERPTR)
562 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
563 (unsigned long)&src->user_ptr[0]);
565 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
567 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
569 case G2D_SELECT_MODE_FGCOLOR:
570 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
572 case G2D_SELECT_MODE_BGCOLOR:
573 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
576 fprintf(stderr , "failed to set src.\n");
582 if (src_x + w > src->width)
583 src_w = src->width - src_x;
584 if (src_y + h > src->height)
585 src_h = src->height - src_y;
589 if (dst_x + w > dst->width)
590 dst_w = dst->width - dst_x;
591 if (dst_y + h > dst->height)
592 dst_h = dst->height - dst_y;
594 w = MIN(src_w, dst_w);
595 h = MIN(src_h, dst_h);
597 if (w <= 0 || h <= 0) {
598 fprintf(stderr, "invalid width or height.\n");
603 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
604 blend.val = g2d_get_blend_op(op);
605 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
606 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
611 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
613 pt.data.x = src_x + w;
614 pt.data.y = src_y + h;
615 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
620 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
622 pt.data.x = dst_x + w;
623 pt.data.y = dst_y + h;
624 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);