exynos/fimg2d: add g2d_check_space()
[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
22 #include <sys/mman.h>
23 #include <linux/stddef.h>
24
25 #include <xf86drm.h>
26
27 #include "libdrm_macros.h"
28 #include "exynos_drm.h"
29 #include "fimg2d_reg.h"
30 #include "exynos_fimg2d.h"
31
32 #define         SET_BF(val, sc, si, scsa, scda, dc, di, dcsa, dcda) \
33                         val.data.src_coeff = sc;                \
34                         val.data.inv_src_color_coeff = si;      \
35                         val.data.src_coeff_src_a = scsa;        \
36                         val.data.src_coeff_dst_a = scda;        \
37                         val.data.dst_coeff = dc;                \
38                         val.data.inv_dst_color_coeff = di;      \
39                         val.data.dst_coeff_src_a = dcsa;        \
40                         val.data.dst_coeff_dst_a = dcda;
41
42 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
43
44 enum g2d_base_addr_reg {
45         g2d_dst = 0,
46         g2d_src
47 };
48
49 static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
50 {
51         /*
52          * The G2D hw scaling factor is a normalized inverse of the scaling factor.
53          * For example: When source width is 100 and destination width is 200
54          * (scaling of 2x), then the hw factor is NC * 100 / 200.
55          * The normalization factor (NC) is 2^16 = 0x10000.
56          */
57
58         return ((src << 16) / dst);
59 }
60
61 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
62 {
63         union g2d_blend_func_val val;
64
65         val.val = 0;
66
67         switch (op) {
68         case G2D_OP_CLEAR:
69         case G2D_OP_DISJOINT_CLEAR:
70         case G2D_OP_CONJOINT_CLEAR:
71                 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
72                                 0, 0, 0);
73                 break;
74         case G2D_OP_SRC:
75         case G2D_OP_DISJOINT_SRC:
76         case G2D_OP_CONJOINT_SRC:
77                 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
78                                 0, 0, 0);
79                 break;
80         case G2D_OP_DST:
81         case G2D_OP_DISJOINT_DST:
82         case G2D_OP_CONJOINT_DST:
83                 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
84                                 0, 0, 0);
85                 break;
86         case G2D_OP_OVER:
87                 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
88                                 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
89                 break;
90         case G2D_OP_INTERPOLATE:
91                 SET_BF(val, G2D_COEFF_MODE_SRC_ALPHA, 0, 0, 0,
92                                 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
93                 break;
94         default:
95                 fprintf(stderr, "Not support operation(%d).\n", op);
96                 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
97                                 0, 0, 0);
98                 break;
99         }
100
101         return val.val;
102 }
103
104 /*
105  * g2d_check_space - check if command buffers have enough space left.
106  *
107  * @ctx: a pointer to g2d_context structure.
108  * @num_cmds: number of (regular) commands.
109  * @num_gem_cmds: number of GEM commands.
110  */
111 static unsigned int g2d_check_space(const struct g2d_context *ctx,
112         unsigned int num_cmds, unsigned int num_gem_cmds)
113 {
114         if (ctx->cmd_nr + num_cmds >= G2D_MAX_CMD_NR ||
115             ctx->cmd_buf_nr + num_gem_cmds >= G2D_MAX_GEM_CMD_NR)
116                 return 1;
117         else
118                 return 0;
119 }
120
121 /*
122  * g2d_add_cmd - set given command and value to user side command buffer.
123  *
124  * @ctx: a pointer to g2d_context structure.
125  * @cmd: command data.
126  * @value: value data.
127  */
128 static int g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
129                         unsigned long value)
130 {
131         switch (cmd & ~(G2D_BUF_USERPTR)) {
132         case SRC_BASE_ADDR_REG:
133         case SRC_PLANE2_BASE_ADDR_REG:
134         case DST_BASE_ADDR_REG:
135         case DST_PLANE2_BASE_ADDR_REG:
136         case PAT_BASE_ADDR_REG:
137         case MASK_BASE_ADDR_REG:
138                 if (ctx->cmd_buf_nr >= G2D_MAX_GEM_CMD_NR) {
139                         fprintf(stderr, "Overflow cmd_gem size.\n");
140                         return -EINVAL;
141                 }
142
143                 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
144                 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
145                 ctx->cmd_buf_nr++;
146                 break;
147         default:
148                 if (ctx->cmd_nr >= G2D_MAX_CMD_NR) {
149                         fprintf(stderr, "Overflow cmd size.\n");
150                         return -EINVAL;
151                 }
152
153                 ctx->cmd[ctx->cmd_nr].offset = cmd;
154                 ctx->cmd[ctx->cmd_nr].data = value;
155                 ctx->cmd_nr++;
156                 break;
157         }
158
159         return 0;
160 }
161
162 /*
163  * g2d_add_base_addr - helper function to set dst/src base address register.
164  *
165  * @ctx: a pointer to g2d_context structure.
166  * @img: a pointer to the dst/src g2d_image structure.
167  * @reg: the register that should be set.
168  */
169 static void g2d_add_base_addr(struct g2d_context *ctx, struct g2d_image *img,
170                         enum g2d_base_addr_reg reg)
171 {
172         const unsigned long cmd = (reg == g2d_dst) ?
173                 DST_BASE_ADDR_REG : SRC_BASE_ADDR_REG;
174
175         if (img->buf_type == G2D_IMGBUF_USERPTR)
176                 g2d_add_cmd(ctx, cmd | G2D_BUF_USERPTR,
177                                 (unsigned long)&img->user_ptr[0]);
178         else
179                 g2d_add_cmd(ctx, cmd, img->bo[0]);
180 }
181
182 /*
183  * g2d_reset - reset fimg2d hardware.
184  *
185  * @ctx: a pointer to g2d_context structure.
186  *
187  */
188 static void g2d_reset(struct g2d_context *ctx)
189 {
190         ctx->cmd_nr = 0;
191         ctx->cmd_buf_nr = 0;
192
193         g2d_add_cmd(ctx, SOFT_RESET_REG, 0x01);
194 }
195
196 /*
197  * g2d_flush - submit all commands and values in user side command buffer
198  *              to command queue aware of fimg2d dma.
199  *
200  * @ctx: a pointer to g2d_context structure.
201  *
202  * This function should be called after all commands and values to user
203  * side command buffer are set. It submits that buffer to the kernel side driver.
204  */
205 static int g2d_flush(struct g2d_context *ctx)
206 {
207         int ret;
208         struct drm_exynos_g2d_set_cmdlist cmdlist = {0};
209
210         if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
211                 return 0;
212
213         if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
214                 fprintf(stderr, "Overflow cmdlist.\n");
215                 return -EINVAL;
216         }
217
218         cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
219         cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
220         cmdlist.cmd_nr = ctx->cmd_nr;
221         cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
222         cmdlist.event_type = G2D_EVENT_NOT;
223         cmdlist.user_data = 0;
224
225         ctx->cmd_nr = 0;
226         ctx->cmd_buf_nr = 0;
227
228         ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
229         if (ret < 0) {
230                 fprintf(stderr, "failed to set cmdlist.\n");
231                 return ret;
232         }
233
234         ctx->cmdlist_nr++;
235
236         return ret;
237 }
238
239 /**
240  * g2d_init - create a new g2d context and get hardware version.
241  *
242  * fd: a file descriptor to an opened drm device.
243  */
244 struct g2d_context *g2d_init(int fd)
245 {
246         struct drm_exynos_g2d_get_ver ver;
247         struct g2d_context *ctx;
248         int ret;
249
250         ctx = calloc(1, sizeof(*ctx));
251         if (!ctx) {
252                 fprintf(stderr, "failed to allocate context.\n");
253                 return NULL;
254         }
255
256         ctx->fd = fd;
257
258         ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
259         if (ret < 0) {
260                 fprintf(stderr, "failed to get version.\n");
261                 free(ctx);
262                 return NULL;
263         }
264
265         ctx->major = ver.major;
266         ctx->minor = ver.minor;
267
268         printf("g2d version(%d.%d).\n", ctx->major, ctx->minor);
269         return ctx;
270 }
271
272 void g2d_fini(struct g2d_context *ctx)
273 {
274         free(ctx);
275 }
276
277 /**
278  * g2d_exec - start the dma to process all commands summited by g2d_flush().
279  *
280  * @ctx: a pointer to g2d_context structure.
281  */
282 int g2d_exec(struct g2d_context *ctx)
283 {
284         struct drm_exynos_g2d_exec exec;
285         int ret;
286
287         if (ctx->cmdlist_nr == 0)
288                 return -EINVAL;
289
290         exec.async = 0;
291
292         ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
293         if (ret < 0) {
294                 fprintf(stderr, "failed to execute.\n");
295                 return ret;
296         }
297
298         ctx->cmdlist_nr = 0;
299
300         return ret;
301 }
302
303 /**
304  * g2d_solid_fill - fill given buffer with given color data.
305  *
306  * @ctx: a pointer to g2d_context structure.
307  * @img: a pointer to g2d_image structure including image and buffer
308  *      information.
309  * @x: x start position to buffer filled with given color data.
310  * @y: y start position to buffer filled with given color data.
311  * @w: width value to buffer filled with given color data.
312  * @h: height value to buffer filled with given color data.
313  */
314 int
315 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
316                         unsigned int x, unsigned int y, unsigned int w,
317                         unsigned int h)
318 {
319         union g2d_bitblt_cmd_val bitblt;
320         union g2d_point_val pt;
321
322         if (g2d_check_space(ctx, 7, 1))
323                 return -ENOSPC;
324
325         g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
326         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
327         g2d_add_base_addr(ctx, img, g2d_dst);
328         g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
329
330         if (x + w > img->width)
331                 w = img->width - x;
332         if (y + h > img->height)
333                 h = img->height - y;
334
335         pt.val = 0;
336         pt.data.x = x;
337         pt.data.y = y;
338         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
339
340         pt.val = 0;
341         pt.data.x = x + w;
342         pt.data.y = y + h;
343
344         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
345
346         g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
347
348         bitblt.val = 0;
349         bitblt.data.fast_solid_color_fill_en = 1;
350         g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
351
352         return g2d_flush(ctx);
353 }
354
355 /**
356  * g2d_copy - copy contents in source buffer to destination buffer.
357  *
358  * @ctx: a pointer to g2d_context structure.
359  * @src: a pointer to g2d_image structure including image and buffer
360  *      information to source.
361  * @dst: a pointer to g2d_image structure including image and buffer
362  *      information to destination.
363  * @src_x: x start position to source buffer.
364  * @src_y: y start position to source buffer.
365  * @dst_x: x start position to destination buffer.
366  * @dst_y: y start position to destination buffer.
367  * @w: width value to source and destination buffers.
368  * @h: height value to source and destination buffers.
369  */
370 int
371 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
372                 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
373                 unsigned int dst_x, unsigned dst_y, unsigned int w,
374                 unsigned int h)
375 {
376         union g2d_rop4_val rop4;
377         union g2d_point_val pt;
378         unsigned int src_w, src_h, dst_w, dst_h;
379
380         src_w = w;
381         src_h = h;
382         if (src_x + src->width > w)
383                 src_w = src->width - src_x;
384         if (src_y + src->height > h)
385                 src_h = src->height - src_y;
386
387         dst_w = w;
388         dst_h = w;
389         if (dst_x + dst->width > w)
390                 dst_w = dst->width - dst_x;
391         if (dst_y + dst->height > h)
392                 dst_h = dst->height - dst_y;
393
394         w = MIN(src_w, dst_w);
395         h = MIN(src_h, dst_h);
396
397         if (w <= 0 || h <= 0) {
398                 fprintf(stderr, "invalid width or height.\n");
399                 return -EINVAL;
400         }
401
402         if (g2d_check_space(ctx, 11, 2))
403                 return -ENOSPC;
404
405         g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
406         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
407         g2d_add_base_addr(ctx, dst, g2d_dst);
408         g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
409
410         g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
411         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
412         g2d_add_base_addr(ctx, src, g2d_src);
413         g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
414
415         pt.val = 0;
416         pt.data.x = src_x;
417         pt.data.y = src_y;
418         g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
419         pt.val = 0;
420         pt.data.x = src_x + w;
421         pt.data.y = src_y + h;
422         g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
423
424         pt.val = 0;
425         pt.data.x = dst_x;
426         pt.data.y = dst_y;
427         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
428         pt.val = 0;
429         pt.data.x = dst_x + w;
430         pt.data.y = dst_y + h;
431         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
432
433         rop4.val = 0;
434         rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
435         g2d_add_cmd(ctx, ROP4_REG, rop4.val);
436
437         return g2d_flush(ctx);
438 }
439
440 /**
441  * g2d_copy_with_scale - copy contents in source buffer to destination buffer
442  *      scaling up or down properly.
443  *
444  * @ctx: a pointer to g2d_context structure.
445  * @src: a pointer to g2d_image structure including image and buffer
446  *      information to source.
447  * @dst: a pointer to g2d_image structure including image and buffer
448  *      information to destination.
449  * @src_x: x start position to source buffer.
450  * @src_y: y start position to source buffer.
451  * @src_w: width value to source buffer.
452  * @src_h: height value to source buffer.
453  * @dst_x: x start position to destination buffer.
454  * @dst_y: y start position to destination buffer.
455  * @dst_w: width value to destination buffer.
456  * @dst_h: height value to destination buffer.
457  * @negative: indicate that it uses color negative to source and
458  *      destination buffers.
459  */
460 int
461 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
462                                 struct g2d_image *dst, unsigned int src_x,
463                                 unsigned int src_y, unsigned int src_w,
464                                 unsigned int src_h, unsigned int dst_x,
465                                 unsigned int dst_y, unsigned int dst_w,
466                                 unsigned int dst_h, unsigned int negative)
467 {
468         union g2d_rop4_val rop4;
469         union g2d_point_val pt;
470         unsigned int scale, repeat_pad;
471         unsigned int scale_x, scale_y;
472
473         /* Sanitize this parameter to facilitate space computation below. */
474         if (negative)
475                 negative = 1;
476
477         if (src_w == dst_w && src_h == dst_h)
478                 scale = 0;
479         else {
480                 scale = 1;
481                 scale_x = g2d_get_scaling(src_w, dst_w);
482                 scale_y = g2d_get_scaling(src_h, dst_h);
483         }
484
485         repeat_pad = src->repeat_mode == G2D_REPEAT_MODE_PAD ? 1 : 0;
486
487         if (src_x + src_w > src->width)
488                 src_w = src->width - src_x;
489         if (src_y + src_h > src->height)
490                 src_h = src->height - src_y;
491
492         if (dst_x + dst_w > dst->width)
493                 dst_w = dst->width - dst_x;
494         if (dst_y + dst_h > dst->height)
495                 dst_h = dst->height - dst_y;
496
497         if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
498                 fprintf(stderr, "invalid width or height.\n");
499                 return -EINVAL;
500         }
501
502         if (g2d_check_space(ctx, 12 + scale * 3 + negative + repeat_pad, 2))
503                 return -ENOSPC;
504
505         g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
506         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
507         g2d_add_base_addr(ctx, dst, g2d_dst);
508         g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
509
510         g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
511         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
512
513         g2d_add_cmd(ctx, SRC_REPEAT_MODE_REG, src->repeat_mode);
514         if (repeat_pad)
515                 g2d_add_cmd(ctx, SRC_PAD_VALUE_REG, dst->color);
516
517         g2d_add_base_addr(ctx, src, g2d_src);
518         g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
519
520         rop4.val = 0;
521         rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
522
523         if (negative) {
524                 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
525                 rop4.data.unmasked_rop3 ^= G2D_ROP3_DST;
526         }
527
528         g2d_add_cmd(ctx, ROP4_REG, rop4.val);
529
530         if (scale) {
531                 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
532                 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
533                 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
534         }
535
536         pt.val = 0;
537         pt.data.x = src_x;
538         pt.data.y = src_y;
539         g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
540         pt.val = 0;
541         pt.data.x = src_x + src_w;
542         pt.data.y = src_y + src_h;
543         g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
544
545         pt.val = 0;
546         pt.data.x = dst_x;
547         pt.data.y = dst_y;
548         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
549         pt.val = 0;
550         pt.data.x = dst_x + dst_w;
551         pt.data.y = dst_y + dst_h;
552         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
553
554         return g2d_flush(ctx);
555 }
556
557 /**
558  * g2d_blend - blend image data in source and destination buffers.
559  *
560  * @ctx: a pointer to g2d_context structure.
561  * @src: a pointer to g2d_image structure including image and buffer
562  *      information to source.
563  * @dst: a pointer to g2d_image structure including image and buffer
564  *      information to destination.
565  * @src_x: x start position to source buffer.
566  * @src_y: y start position to source buffer.
567  * @dst_x: x start position to destination buffer.
568  * @dst_y: y start position to destination buffer.
569  * @w: width value to source and destination buffer.
570  * @h: height value to source and destination buffer.
571  * @op: blend operation type.
572  */
573 int
574 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
575                 struct g2d_image *dst, unsigned int src_x,
576                 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
577                 unsigned int w, unsigned int h, enum e_g2d_op op)
578 {
579         union g2d_point_val pt;
580         union g2d_bitblt_cmd_val bitblt;
581         union g2d_blend_func_val blend;
582         unsigned int src_w = 0, src_h = 0, dst_w = 0, dst_h = 0;
583
584         bitblt.val = 0;
585         blend.val = 0;
586
587         if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
588                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
589         else
590                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
591
592         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
593         g2d_add_base_addr(ctx, dst, g2d_dst);
594         g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
595
596         g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
597         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
598
599         switch (src->select_mode) {
600         case G2D_SELECT_MODE_NORMAL:
601                 g2d_add_base_addr(ctx, src, g2d_src);
602                 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
603                 break;
604         case G2D_SELECT_MODE_FGCOLOR:
605                 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
606                 break;
607         case G2D_SELECT_MODE_BGCOLOR:
608                 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
609                 break;
610         default:
611                 fprintf(stderr , "failed to set src.\n");
612                 return -EINVAL;
613         }
614
615         src_w = w;
616         src_h = h;
617         if (src_x + w > src->width)
618                 src_w = src->width - src_x;
619         if (src_y + h > src->height)
620                 src_h = src->height - src_y;
621
622         dst_w = w;
623         dst_h = h;
624         if (dst_x + w > dst->width)
625                 dst_w = dst->width - dst_x;
626         if (dst_y + h > dst->height)
627                 dst_h = dst->height - dst_y;
628
629         w = MIN(src_w, dst_w);
630         h = MIN(src_h, dst_h);
631
632         if (w <= 0 || h <= 0) {
633                 fprintf(stderr, "invalid width or height.\n");
634                 g2d_reset(ctx);
635                 return -EINVAL;
636         }
637
638         bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
639         blend.val = g2d_get_blend_op(op);
640         g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
641         g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
642
643         pt.val = 0;
644         pt.data.x = src_x;
645         pt.data.y = src_y;
646         g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
647         pt.val = 0;
648         pt.data.x = src_x + w;
649         pt.data.y = src_y + h;
650         g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
651
652         pt.val = 0;
653         pt.data.x = dst_x;
654         pt.data.y = dst_y;
655         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
656         pt.val = 0;
657         pt.data.x = dst_x + w;
658         pt.data.y = dst_y + h;
659         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
660
661         return g2d_flush(ctx);
662 }
663
664 /**
665  * g2d_scale_and_blend - apply scaling to source buffer and then blend to destination buffer
666  *
667  * @ctx: a pointer to g2d_context structure.
668  * @src: a pointer to g2d_image structure including image and buffer
669  *      information to source.
670  * @dst: a pointer to g2d_image structure including image and buffer
671  *      information to destination.
672  * @src_x: x start position to source buffer.
673  * @src_y: y start position to source buffer.
674  * @src_w: width value to source buffer.
675  * @src_h: height value to source buffer.
676  * @dst_x: x start position to destination buffer.
677  * @dst_y: y start position to destination buffer.
678  * @dst_w: width value to destination buffer.
679  * @dst_h: height value to destination buffer.
680  * @op: blend operation type.
681  */
682 int
683 g2d_scale_and_blend(struct g2d_context *ctx, struct g2d_image *src,
684                 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
685                 unsigned int src_w, unsigned int src_h, unsigned int dst_x,
686                 unsigned int dst_y, unsigned int dst_w, unsigned int dst_h,
687                 enum e_g2d_op op)
688 {
689         union g2d_point_val pt;
690         union g2d_bitblt_cmd_val bitblt;
691         union g2d_blend_func_val blend;
692         unsigned int scale;
693         unsigned int scale_x, scale_y;
694
695         bitblt.val = 0;
696         blend.val = 0;
697
698         if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
699                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
700         else
701                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
702
703         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
704         g2d_add_base_addr(ctx, dst, g2d_dst);
705         g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
706
707         g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
708         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
709
710         switch (src->select_mode) {
711         case G2D_SELECT_MODE_NORMAL:
712                 g2d_add_base_addr(ctx, src, g2d_src);
713                 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
714                 break;
715         case G2D_SELECT_MODE_FGCOLOR:
716                 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
717                 break;
718         case G2D_SELECT_MODE_BGCOLOR:
719                 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
720                 break;
721         default:
722                 fprintf(stderr , "failed to set src.\n");
723                 return -EINVAL;
724         }
725
726         if (src_w == dst_w && src_h == dst_h)
727                 scale = 0;
728         else {
729                 scale = 1;
730                 scale_x = g2d_get_scaling(src_w, dst_w);
731                 scale_y = g2d_get_scaling(src_h, dst_h);
732         }
733
734         if (src_x + src_w > src->width)
735                 src_w = src->width - src_x;
736         if (src_y + src_h > src->height)
737                 src_h = src->height - src_y;
738
739         if (dst_x + dst_w > dst->width)
740                 dst_w = dst->width - dst_x;
741         if (dst_y + dst_h > dst->height)
742                 dst_h = dst->height - dst_y;
743
744         if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
745                 fprintf(stderr, "invalid width or height.\n");
746                 g2d_reset(ctx);
747                 return -EINVAL;
748         }
749
750         if (scale) {
751                 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
752                 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
753                 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
754         }
755
756         bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
757         blend.val = g2d_get_blend_op(op);
758         g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
759         g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
760
761         pt.val = 0;
762         pt.data.x = src_x;
763         pt.data.y = src_y;
764         g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
765         pt.val = 0;
766         pt.data.x = src_x + src_w;
767         pt.data.y = src_y + src_h;
768         g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
769
770         pt.val = 0;
771         pt.data.x = dst_x;
772         pt.data.y = dst_y;
773         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
774         pt.val = 0;
775         pt.data.x = dst_x + dst_w;
776         pt.data.y = dst_y + dst_h;
777         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
778
779         return g2d_flush(ctx);
780 }