OSDN Git Service

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