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