OSDN Git Service

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