exynos/fimg2d: add g2d_config_event
[platform/upstream/libdrm.git] / exynos / exynos_fimg2d.c
1 /*
2  * Copyright (C) 2013 Samsung Electronics Co.Ltd
3  * Authors:
4  *      Inki Dae <inki.dae@samsung.com>
5  *
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.
10  *
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <assert.h>
22
23 #include <sys/mman.h>
24 #include <linux/stddef.h>
25
26 #include <xf86drm.h>
27
28 #include "libdrm_macros.h"
29 #include "exynos_drm.h"
30 #include "fimg2d_reg.h"
31 #include "exynos_fimg2d.h"
32
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;
42
43 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
44
45 #define MSG_PREFIX "exynos/fimg2d: "
46
47 #define G2D_MAX_CMD_NR          64
48 #define G2D_MAX_GEM_CMD_NR      64
49 #define G2D_MAX_CMD_LIST_NR     64
50
51 struct g2d_context {
52         int                             fd;
53         unsigned int                    major;
54         unsigned int                    minor;
55         struct drm_exynos_g2d_cmd       cmd[G2D_MAX_CMD_NR];
56         struct drm_exynos_g2d_cmd       cmd_buf[G2D_MAX_GEM_CMD_NR];
57         unsigned int                    cmd_nr;
58         unsigned int                    cmd_buf_nr;
59         unsigned int                    cmdlist_nr;
60         void                            *event_userdata;
61 };
62
63 enum g2d_base_addr_reg {
64         g2d_dst = 0,
65         g2d_src
66 };
67
68 static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
69 {
70         /*
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.
75          */
76
77         return ((src << 16) / dst);
78 }
79
80 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
81 {
82         union g2d_blend_func_val val;
83
84         val.val = 0;
85
86         /*
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.
90          */
91         switch (op) {
92         case G2D_OP_CLEAR:
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,
96                                 0, 0, 0);
97                 break;
98         case G2D_OP_SRC:
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,
102                                 0, 0, 0);
103                 break;
104         case G2D_OP_DST:
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,
108                                 0, 0, 0);
109                 break;
110         case G2D_OP_OVER:
111                 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
112                                 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
113                 break;
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);
117                 break;
118         }
119
120         return val.val;
121 }
122
123 /*
124  * g2d_check_space - check if command buffers have enough space left.
125  *
126  * @ctx: a pointer to g2d_context structure.
127  * @num_cmds: number of (regular) commands.
128  * @num_gem_cmds: number of GEM commands.
129  */
130 static unsigned int g2d_check_space(const struct g2d_context *ctx,
131         unsigned int num_cmds, unsigned int num_gem_cmds)
132 {
133         if (ctx->cmd_nr + num_cmds >= G2D_MAX_CMD_NR ||
134             ctx->cmd_buf_nr + num_gem_cmds >= G2D_MAX_GEM_CMD_NR)
135                 return 1;
136         else
137                 return 0;
138 }
139
140 /*
141  * g2d_validate_select_mode - validate select mode.
142  *
143  * @mode: the mode to validate
144  *
145  * Returns zero for an invalid mode and one otherwise.
146  */
147 static int g2d_validate_select_mode(
148         enum e_g2d_select_mode mode)
149 {
150         switch (mode) {
151         case G2D_SELECT_MODE_NORMAL:
152         case G2D_SELECT_MODE_FGCOLOR:
153         case G2D_SELECT_MODE_BGCOLOR:
154                 return 1;
155         }
156
157         return 0;
158 }
159
160 /*
161  * g2d_validate_blending_op - validate blending operation.
162  *
163  * @operation: the operation to validate
164  *
165  * Returns zero for an invalid mode and one otherwise.
166  */
167 static int g2d_validate_blending_op(
168         enum e_g2d_op operation)
169 {
170         switch (operation) {
171         case G2D_OP_CLEAR:
172         case G2D_OP_SRC:
173         case G2D_OP_DST:
174         case G2D_OP_OVER:
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:
182                 return 1;
183         }
184
185         return 0;
186 }
187
188 /*
189  * g2d_add_cmd - set given command and value to user side command buffer.
190  *
191  * @ctx: a pointer to g2d_context structure.
192  * @cmd: command data.
193  * @value: value data.
194  *
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.
197  */
198 static void g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
199                         unsigned long value)
200 {
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);
209
210                 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
211                 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
212                 ctx->cmd_buf_nr++;
213                 break;
214         default:
215                 assert(ctx->cmd_nr < G2D_MAX_CMD_NR);
216
217                 ctx->cmd[ctx->cmd_nr].offset = cmd;
218                 ctx->cmd[ctx->cmd_nr].data = value;
219                 ctx->cmd_nr++;
220                 break;
221         }
222 }
223
224 /*
225  * g2d_add_base_addr - helper function to set dst/src base address register.
226  *
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.
230  */
231 static void g2d_add_base_addr(struct g2d_context *ctx, struct g2d_image *img,
232                         enum g2d_base_addr_reg reg)
233 {
234         const unsigned long cmd = (reg == g2d_dst) ?
235                 DST_BASE_ADDR_REG : SRC_BASE_ADDR_REG;
236
237         if (img->buf_type == G2D_IMGBUF_USERPTR)
238                 g2d_add_cmd(ctx, cmd | G2D_BUF_USERPTR,
239                                 (unsigned long)&img->user_ptr[0]);
240         else
241                 g2d_add_cmd(ctx, cmd, img->bo[0]);
242 }
243
244 /*
245  * g2d_reset - reset fimg2d hardware.
246  *
247  * @ctx: a pointer to g2d_context structure.
248  *
249  */
250 static void g2d_reset(struct g2d_context *ctx)
251 {
252         ctx->cmd_nr = 0;
253         ctx->cmd_buf_nr = 0;
254
255         g2d_add_cmd(ctx, SOFT_RESET_REG, 0x01);
256 }
257
258 /*
259  * g2d_flush - submit all commands and values in user side command buffer
260  *              to command queue aware of fimg2d dma.
261  *
262  * @ctx: a pointer to g2d_context structure.
263  *
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.
266  */
267 static int g2d_flush(struct g2d_context *ctx)
268 {
269         int ret;
270         struct drm_exynos_g2d_set_cmdlist cmdlist = {0};
271
272         if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
273                 return 0;
274
275         if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
276                 fprintf(stderr, MSG_PREFIX "command list overflow.\n");
277                 return -EINVAL;
278         }
279
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;
284
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;
289         } else {
290                 cmdlist.event_type = G2D_EVENT_NOT;
291                 cmdlist.user_data = 0;
292         }
293
294         ctx->cmd_nr = 0;
295         ctx->cmd_buf_nr = 0;
296
297         ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
298         if (ret < 0) {
299                 fprintf(stderr, MSG_PREFIX "failed to set cmdlist.\n");
300                 return ret;
301         }
302
303         ctx->cmdlist_nr++;
304
305         return ret;
306 }
307
308 /**
309  * g2d_init - create a new g2d context and get hardware version.
310  *
311  * fd: a file descriptor to an opened drm device.
312  */
313 struct g2d_context *g2d_init(int fd)
314 {
315         struct drm_exynos_g2d_get_ver ver;
316         struct g2d_context *ctx;
317         int ret;
318
319         ctx = calloc(1, sizeof(*ctx));
320         if (!ctx) {
321                 fprintf(stderr, MSG_PREFIX "failed to allocate context.\n");
322                 return NULL;
323         }
324
325         ctx->fd = fd;
326
327         ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
328         if (ret < 0) {
329                 fprintf(stderr, MSG_PREFIX "failed to get version.\n");
330                 free(ctx);
331                 return NULL;
332         }
333
334         ctx->major = ver.major;
335         ctx->minor = ver.minor;
336
337         printf(MSG_PREFIX "G2D version (%d.%d).\n", ctx->major, ctx->minor);
338         return ctx;
339 }
340
341 void g2d_fini(struct g2d_context *ctx)
342 {
343         free(ctx);
344 }
345
346 /**
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.
353  *
354  * @ctx: a pointer to g2d_context structure.
355  * @userdata: a pointer to the user data
356  */
357 void g2d_config_event(struct g2d_context *ctx, void *userdata)
358 {
359         ctx->event_userdata = userdata;
360 }
361
362 /**
363  * g2d_exec - start the dma to process all commands summited by g2d_flush().
364  *
365  * @ctx: a pointer to g2d_context structure.
366  */
367 int g2d_exec(struct g2d_context *ctx)
368 {
369         struct drm_exynos_g2d_exec exec;
370         int ret;
371
372         if (ctx->cmdlist_nr == 0)
373                 return -EINVAL;
374
375         exec.async = 0;
376
377         ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
378         if (ret < 0) {
379                 fprintf(stderr, MSG_PREFIX "failed to execute.\n");
380                 return ret;
381         }
382
383         ctx->cmdlist_nr = 0;
384
385         return ret;
386 }
387
388 /**
389  * g2d_solid_fill - fill given buffer with given color data.
390  *
391  * @ctx: a pointer to g2d_context structure.
392  * @img: a pointer to g2d_image structure including image and buffer
393  *      information.
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.
398  */
399 int
400 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
401                         unsigned int x, unsigned int y, unsigned int w,
402                         unsigned int h)
403 {
404         union g2d_bitblt_cmd_val bitblt;
405         union g2d_point_val pt;
406
407         if (g2d_check_space(ctx, 7, 1))
408                 return -ENOSPC;
409
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);
414
415         if (x + w > img->width)
416                 w = img->width - x;
417         if (y + h > img->height)
418                 h = img->height - y;
419
420         pt.data.x = x;
421         pt.data.y = y;
422         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
423
424         pt.data.x = x + w;
425         pt.data.y = y + h;
426         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
427
428         g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
429
430         bitblt.val = 0;
431         bitblt.data.fast_solid_color_fill_en = 1;
432         g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
433
434         return g2d_flush(ctx);
435 }
436
437 /**
438  * g2d_copy - copy contents in source buffer to destination buffer.
439  *
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.
451  */
452 int
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,
456                 unsigned int h)
457 {
458         union g2d_rop4_val rop4;
459         union g2d_point_val pt;
460         unsigned int src_w, src_h, dst_w, dst_h;
461
462         src_w = w;
463         src_h = 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;
468
469         dst_w = w;
470         dst_h = w;
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;
475
476         w = MIN(src_w, dst_w);
477         h = MIN(src_h, dst_h);
478
479         if (w <= 0 || h <= 0) {
480                 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
481                 return -EINVAL;
482         }
483
484         if (g2d_check_space(ctx, 11, 2))
485                 return -ENOSPC;
486
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);
491
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);
496
497         pt.data.x = src_x;
498         pt.data.y = src_y;
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);
503
504         pt.data.x = dst_x;
505         pt.data.y = dst_y;
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);
510
511         rop4.val = 0;
512         rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
513         g2d_add_cmd(ctx, ROP4_REG, rop4.val);
514
515         return g2d_flush(ctx);
516 }
517
518 /**
519  * g2d_copy_with_scale - copy contents in source buffer to destination buffer
520  *      scaling up or down properly.
521  *
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.
537  */
538 int
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)
545 {
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;
550
551         /* Sanitize this parameter to facilitate space computation below. */
552         if (negative)
553                 negative = 1;
554
555         if (src_w == dst_w && src_h == dst_h)
556                 scale = 0;
557         else {
558                 scale = 1;
559                 scale_x = g2d_get_scaling(src_w, dst_w);
560                 scale_y = g2d_get_scaling(src_h, dst_h);
561         }
562
563         repeat_pad = src->repeat_mode == G2D_REPEAT_MODE_PAD ? 1 : 0;
564
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;
569
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;
574
575         if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
576                 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
577                 return -EINVAL;
578         }
579
580         if (g2d_check_space(ctx, 12 + scale * 3 + negative + repeat_pad, 2))
581                 return -ENOSPC;
582
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);
587
588         g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
589         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
590
591         g2d_add_cmd(ctx, SRC_REPEAT_MODE_REG, src->repeat_mode);
592         if (repeat_pad)
593                 g2d_add_cmd(ctx, SRC_PAD_VALUE_REG, dst->color);
594
595         g2d_add_base_addr(ctx, src, g2d_src);
596         g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
597
598         rop4.val = 0;
599         rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
600
601         if (negative) {
602                 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
603                 rop4.data.unmasked_rop3 ^= G2D_ROP3_DST;
604         }
605
606         g2d_add_cmd(ctx, ROP4_REG, rop4.val);
607
608         if (scale) {
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);
612         }
613
614         pt.data.x = src_x;
615         pt.data.y = src_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);
620
621         pt.data.x = dst_x;
622         pt.data.y = dst_y;
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);
627
628         return g2d_flush(ctx);
629 }
630
631 /**
632  * g2d_blend - blend image data in source and destination buffers.
633  *
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.
646  */
647 int
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)
652 {
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;
658
659         src_w = w;
660         src_h = 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;
665
666         dst_w = w;
667         dst_h = h;
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;
672
673         w = MIN(src_w, dst_w);
674         h = MIN(src_h, dst_h);
675
676         if (w <= 0 || h <= 0) {
677                 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
678                 return -EINVAL;
679         }
680
681         if (!g2d_validate_select_mode(src->select_mode)) {
682                 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
683                 return -EINVAL;
684         }
685
686         if (!g2d_validate_blending_op(op)) {
687                 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
688                 return -EINVAL;
689         }
690
691         gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
692
693         if (g2d_check_space(ctx, 12, gem_space))
694                 return -ENOSPC;
695
696         bitblt.val = 0;
697         blend.val = 0;
698
699         if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
700                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
701         else
702                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
703
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);
707
708         g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
709         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
710
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);
715                 break;
716         case G2D_SELECT_MODE_FGCOLOR:
717                 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
718                 break;
719         case G2D_SELECT_MODE_BGCOLOR:
720                 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
721                 break;
722         }
723
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);
728
729         pt.data.x = src_x;
730         pt.data.y = src_y;
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);
735
736         pt.data.x = dst_x;
737         pt.data.y = dst_y;
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);
742
743         return g2d_flush(ctx);
744 }
745
746 /**
747  * g2d_scale_and_blend - apply scaling to source buffer and then blend to destination buffer
748  *
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.
763  */
764 int
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,
769                 enum e_g2d_op op)
770 {
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;
776
777         if (src_w == dst_w && src_h == dst_h)
778                 scale = 0;
779         else {
780                 scale = 1;
781                 scale_x = g2d_get_scaling(src_w, dst_w);
782                 scale_y = g2d_get_scaling(src_h, dst_h);
783         }
784
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;
789
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;
794
795         if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
796                 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
797                 return -EINVAL;
798         }
799
800         if (!g2d_validate_select_mode(src->select_mode)) {
801                 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
802                 return -EINVAL;
803         }
804
805         if (!g2d_validate_blending_op(op)) {
806                 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
807                 return -EINVAL;
808         }
809
810         gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
811
812         if (g2d_check_space(ctx, 12 + scale * 3, gem_space))
813                 return -ENOSPC;
814
815         bitblt.val = 0;
816         blend.val = 0;
817
818         if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
819                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
820         else
821                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
822
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);
826
827         g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
828         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
829
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);
834                 break;
835         case G2D_SELECT_MODE_FGCOLOR:
836                 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
837                 break;
838         case G2D_SELECT_MODE_BGCOLOR:
839                 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
840                 break;
841         }
842
843         if (scale) {
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);
847         }
848
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);
853
854         pt.data.x = src_x;
855         pt.data.y = src_y;
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);
860
861         pt.data.x = dst_x;
862         pt.data.y = dst_y;
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);
867
868         return g2d_flush(ctx);
869 }