OSDN Git Service

exynos: replace G2D_DOUBLE_TO_FIXED macro with function
[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 static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
45 {
46         /*
47          * The G2D hw scaling factor is a normalized inverse of the scaling factor.
48          * For example: When source width is 100 and destination width is 200
49          * (scaling of 2x), then the hw factor is NC * 100 / 200.
50          * The normalization factor (NC) is 2^16 = 0x10000.
51          */
52
53         return ((src << 16) / dst);
54 }
55
56 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
57 {
58         union g2d_blend_func_val val;
59
60         val.val = 0;
61
62         switch (op) {
63         case G2D_OP_CLEAR:
64         case G2D_OP_DISJOINT_CLEAR:
65         case G2D_OP_CONJOINT_CLEAR:
66                 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
67                                 0, 0, 0);
68                 break;
69         case G2D_OP_SRC:
70         case G2D_OP_DISJOINT_SRC:
71         case G2D_OP_CONJOINT_SRC:
72                 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
73                                 0, 0, 0);
74                 break;
75         case G2D_OP_DST:
76         case G2D_OP_DISJOINT_DST:
77         case G2D_OP_CONJOINT_DST:
78                 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
79                                 0, 0, 0);
80                 break;
81         case G2D_OP_OVER:
82                 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
83                                 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
84                 break;
85         default:
86                 fprintf(stderr, "Not support operation(%d).\n", op);
87                 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
88                                 0, 0, 0);
89                 break;
90         }
91
92         return val.val;
93 }
94
95 /*
96  * g2d_add_cmd - set given command and value to user side command buffer.
97  *
98  * @ctx: a pointer to g2d_context structure.
99  * @cmd: command data.
100  * @value: value data.
101  */
102 static int g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
103                         unsigned long value)
104 {
105         switch (cmd & ~(G2D_BUF_USERPTR)) {
106         case SRC_BASE_ADDR_REG:
107         case SRC_PLANE2_BASE_ADDR_REG:
108         case DST_BASE_ADDR_REG:
109         case DST_PLANE2_BASE_ADDR_REG:
110         case PAT_BASE_ADDR_REG:
111         case MASK_BASE_ADDR_REG:
112                 if (ctx->cmd_buf_nr >= G2D_MAX_GEM_CMD_NR) {
113                         fprintf(stderr, "Overflow cmd_gem size.\n");
114                         return -EINVAL;
115                 }
116
117                 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
118                 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
119                 ctx->cmd_buf_nr++;
120                 break;
121         default:
122                 if (ctx->cmd_nr >= G2D_MAX_CMD_NR) {
123                         fprintf(stderr, "Overflow cmd size.\n");
124                         return -EINVAL;
125                 }
126
127                 ctx->cmd[ctx->cmd_nr].offset = cmd;
128                 ctx->cmd[ctx->cmd_nr].data = value;
129                 ctx->cmd_nr++;
130                 break;
131         }
132
133         return TRUE;
134 }
135
136 /*
137  * g2d_reset - reset fimg2d hardware.
138  *
139  * @ctx: a pointer to g2d_context structure.
140  *
141  */
142 static void g2d_reset(struct g2d_context *ctx)
143 {
144         ctx->cmd_nr = 0;
145         ctx->cmd_buf_nr = 0;
146
147         g2d_add_cmd(ctx, SOFT_RESET_REG, 0x01);
148 }
149
150 /*
151  * g2d_flush - summit all commands and values in user side command buffer
152  *              to command queue aware of fimg2d dma.
153  *
154  * @ctx: a pointer to g2d_context structure.
155  *
156  * This function should be called after all commands and values to user
157  * side command buffer is set to summit that buffer to kernel side driver.
158  */
159 static int g2d_flush(struct g2d_context *ctx)
160 {
161         int ret;
162         struct drm_exynos_g2d_set_cmdlist cmdlist;
163
164         if (ctx->cmd_nr  == 0 && ctx->cmd_buf_nr == 0)
165                 return FALSE;
166
167         if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
168                 fprintf(stderr, "Overflow cmdlist.\n");
169                 return -EINVAL;
170         }
171
172         memset(&cmdlist, 0, sizeof(struct drm_exynos_g2d_set_cmdlist));
173
174         cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
175         cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
176         cmdlist.cmd_nr = ctx->cmd_nr;
177         cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
178         cmdlist.event_type = G2D_EVENT_NOT;
179         cmdlist.user_data = 0;
180
181         ctx->cmd_nr = 0;
182         ctx->cmd_buf_nr = 0;
183
184         ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
185         if (ret < 0) {
186                 fprintf(stderr, "failed to set cmdlist.\n");
187                 return ret;
188         }
189
190         ctx->cmdlist_nr++;
191
192         return ret;
193 }
194
195 /**
196  * g2d_init - create a new g2d context and get hardware version.
197  *
198  * fd: a file descriptor to drm device driver opened.
199  */
200 drm_public struct g2d_context *g2d_init(int fd)
201 {
202         struct drm_exynos_g2d_get_ver ver;
203         struct g2d_context *ctx;
204         int ret;
205
206         ctx = calloc(1, sizeof(*ctx));
207         if (!ctx) {
208                 fprintf(stderr, "failed to allocate context.\n");
209                 return NULL;
210         }
211
212         ctx->fd = fd;
213
214         ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
215         if (ret < 0) {
216                 fprintf(stderr, "failed to get version.\n");
217                 free(ctx);
218                 return NULL;
219         }
220
221         ctx->major = ver.major;
222         ctx->minor = ver.minor;
223
224         printf("g2d version(%d.%d).\n", ctx->major, ctx->minor);
225         return ctx;
226 }
227
228 drm_public void g2d_fini(struct g2d_context *ctx)
229 {
230         if (ctx)
231                 free(ctx);
232 }
233
234 /**
235  * g2d_exec - start the dma to process all commands summited by g2d_flush().
236  *
237  * @ctx: a pointer to g2d_context structure.
238  */
239 drm_public int g2d_exec(struct g2d_context *ctx)
240 {
241         struct drm_exynos_g2d_exec exec;
242         int ret;
243
244         if (ctx->cmdlist_nr == 0)
245                 return -EINVAL;
246
247         exec.async = 0;
248
249         ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
250         if (ret < 0) {
251                 fprintf(stderr, "failed to execute.\n");
252                 return ret;
253         }
254
255         ctx->cmdlist_nr = 0;
256
257         return ret;
258 }
259
260 /**
261  * g2d_solid_fill - fill given buffer with given color data.
262  *
263  * @ctx: a pointer to g2d_context structure.
264  * @img: a pointer to g2d_image structure including image and buffer
265  *      information.
266  * @x: x start position to buffer filled with given color data.
267  * @y: y start position to buffer filled with given color data.
268  * @w: width value to buffer filled with given color data.
269  * @h: height value to buffer filled with given color data.
270  */
271 drm_public int
272 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
273                         unsigned int x, unsigned int y, unsigned int w,
274                         unsigned int h)
275 {
276         union g2d_bitblt_cmd_val bitblt;
277         union g2d_point_val pt;
278
279         g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
280         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
281
282         if (img->buf_type == G2D_IMGBUF_USERPTR)
283                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
284                                 (unsigned long)&img->user_ptr[0]);
285         else
286                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, img->bo[0]);
287
288         g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
289
290         if (x + w > img->width)
291                 w = img->width - x;
292         if (y + h > img->height)
293                 h = img->height - y;
294
295         pt.val = 0;
296         pt.data.x = x;
297         pt.data.y = y;
298         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
299
300         pt.val = 0;
301         pt.data.x = x + w;
302         pt.data.y = y + h;
303
304         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
305
306         g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
307
308         bitblt.val = 0;
309         bitblt.data.fast_solid_color_fill_en = 1;
310         g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
311
312         g2d_flush(ctx);
313
314         return 0;
315 }
316
317 /**
318  * g2d_copy - copy contents in source buffer to destination buffer.
319  *
320  * @ctx: a pointer to g2d_context structure.
321  * @src: a pointer to g2d_image structure including image and buffer
322  *      information to source.
323  * @dst: a pointer to g2d_image structure including image and buffer
324  *      information to destination.
325  * @src_x: x start position to source buffer.
326  * @src_y: y start position to source buffer.
327  * @dst_x: x start position to destination buffer.
328  * @dst_y: y start position to destination buffer.
329  * @w: width value to source and destination buffers.
330  * @h: height value to source and destination buffers.
331  */
332 drm_public int
333 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
334                 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
335                 unsigned int dst_x, unsigned dst_y, unsigned int w,
336                 unsigned int h)
337 {
338         union g2d_rop4_val rop4;
339         union g2d_point_val pt;
340         unsigned int src_w = 0, src_h = 0, dst_w = 0, dst_h = 0;
341
342         g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
343         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
344         if (dst->buf_type == G2D_IMGBUF_USERPTR)
345                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
346                                 (unsigned long)&dst->user_ptr[0]);
347         else
348                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
349
350         g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
351
352         g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
353         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
354         if (src->buf_type == G2D_IMGBUF_USERPTR)
355                 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
356                                 (unsigned long)&src->user_ptr[0]);
357         else
358                 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
359
360         g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
361
362         src_w = w;
363         src_h = h;
364         if (src_x + src->width > w)
365                 src_w = src->width - src_x;
366         if (src_y + src->height > h)
367                 src_h = src->height - src_y;
368
369         dst_w = w;
370         dst_h = w;
371         if (dst_x + dst->width > w)
372                 dst_w = dst->width - dst_x;
373         if (dst_y + dst->height > h)
374                 dst_h = dst->height - dst_y;
375
376         w = MIN(src_w, dst_w);
377         h = MIN(src_h, dst_h);
378
379         if (w <= 0 || h <= 0) {
380                 fprintf(stderr, "invalid width or height.\n");
381                 g2d_reset(ctx);
382                 return -EINVAL;
383         }
384
385         pt.val = 0;
386         pt.data.x = src_x;
387         pt.data.y = src_y;
388         g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
389         pt.val = 0;
390         pt.data.x = src_x + w;
391         pt.data.y = src_y + h;
392         g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
393
394         pt.val = 0;
395         pt.data.x = dst_x;
396         pt.data.y = dst_y;
397         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
398         pt.val = 0;
399         pt.data.x = dst_x + w;
400         pt.data.y = dst_y + h;
401         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
402
403         rop4.val = 0;
404         rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
405         g2d_add_cmd(ctx, ROP4_REG, rop4.val);
406
407         g2d_flush(ctx);
408
409         return 0;
410 }
411
412 /**
413  * g2d_copy_with_scale - copy contents in source buffer to destination buffer
414  *      scaling up or down properly.
415  *
416  * @ctx: a pointer to g2d_context structure.
417  * @src: a pointer to g2d_image structure including image and buffer
418  *      information to source.
419  * @dst: a pointer to g2d_image structure including image and buffer
420  *      information to destination.
421  * @src_x: x start position to source buffer.
422  * @src_y: y start position to source buffer.
423  * @src_w: width value to source buffer.
424  * @src_h: height value to source buffer.
425  * @dst_x: x start position to destination buffer.
426  * @dst_y: y start position to destination buffer.
427  * @dst_w: width value to destination buffer.
428  * @dst_h: height value to destination buffer.
429  * @negative: indicate that it uses color negative to source and
430  *      destination buffers.
431  */
432 drm_public int
433 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
434                                 struct g2d_image *dst, unsigned int src_x,
435                                 unsigned int src_y, unsigned int src_w,
436                                 unsigned int src_h, unsigned int dst_x,
437                                 unsigned int dst_y, unsigned int dst_w,
438                                 unsigned int dst_h, unsigned int negative)
439 {
440         union g2d_rop4_val rop4;
441         union g2d_point_val pt;
442         unsigned int scale;
443         unsigned int scale_x, scale_y;
444
445         g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
446         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
447         if (dst->buf_type == G2D_IMGBUF_USERPTR)
448                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
449                                 (unsigned long)&dst->user_ptr[0]);
450         else
451                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
452
453         g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
454
455         g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
456         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
457         if (src->buf_type == G2D_IMGBUF_USERPTR)
458                 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
459                                 (unsigned long)&src->user_ptr[0]);
460         else
461                 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
462
463         g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
464
465         if (src_w == dst_w && src_h == dst_h)
466                 scale = 0;
467         else {
468                 scale = 1;
469                 scale_x = g2d_get_scaling(src_w, dst_w);
470                 scale_y = g2d_get_scaling(src_h, dst_h);
471         }
472
473         if (src_x + src_w > src->width)
474                 src_w = src->width - src_x;
475         if (src_y + src_h > src->height)
476                 src_h = src->height - src_y;
477
478         if (dst_x + dst_w > dst->width)
479                 dst_w = dst->width - dst_x;
480         if (dst_y + dst_h > dst->height)
481                 dst_h = dst->height - dst_y;
482
483         if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
484                 fprintf(stderr, "invalid width or height.\n");
485                 g2d_reset(ctx);
486                 return -EINVAL;
487         }
488
489         if (negative) {
490                 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
491                 rop4.val = 0;
492                 rop4.data.unmasked_rop3 = G2D_ROP3_SRC^G2D_ROP3_DST;
493                 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
494         } else {
495                 rop4.val = 0;
496                 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
497                 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
498         }
499
500         if (scale) {
501                 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
502                 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
503                 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
504         }
505
506         pt.val = 0;
507         pt.data.x = src_x;
508         pt.data.y = src_y;
509         g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
510         pt.val = 0;
511         pt.data.x = src_x + src_w;
512         pt.data.y = src_y + src_h;
513         g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
514
515         pt.val = 0;
516         pt.data.x = dst_x;
517         pt.data.y = dst_y;
518         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
519         pt.val = 0;
520         pt.data.x = dst_x + dst_w;
521         pt.data.y = dst_y + dst_h;
522         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
523
524         g2d_flush(ctx);
525
526         return 0;
527 }
528
529 /**
530  * g2d_blend - blend image data in source and destion buffers
531  *
532  * @ctx: a pointer to g2d_context structure.
533  * @src: a pointer to g2d_image structure including image and buffer
534  *      information to source.
535  * @dst: a pointer to g2d_image structure including image and buffer
536  *      information to destination.
537  * @src_x: x start position to source buffer.
538  * @src_y: y start position to source buffer.
539  * @dst_x: x start position to destination buffer.
540  * @dst_y: y start position to destination buffer.
541  * @w: width value to source and destination buffer.
542  * @h: height value to source and destination buffer.
543  * @op: blend operation type.
544  */
545 drm_public int
546 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
547                 struct g2d_image *dst, unsigned int src_x,
548                 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
549                 unsigned int w, unsigned int h, enum e_g2d_op op)
550 {
551         union g2d_point_val pt;
552         union g2d_bitblt_cmd_val bitblt;
553         union g2d_blend_func_val blend;
554         unsigned int src_w = 0, src_h = 0, dst_w = 0, dst_h = 0;
555
556         bitblt.val = 0;
557         blend.val = 0;
558
559         if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
560                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
561         else
562                 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
563
564         g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
565         if (dst->buf_type == G2D_IMGBUF_USERPTR)
566                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
567                                 (unsigned long)&dst->user_ptr[0]);
568         else
569                 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
570
571         g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
572
573         g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
574         g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
575
576         switch (src->select_mode) {
577         case G2D_SELECT_MODE_NORMAL:
578                 if (src->buf_type == G2D_IMGBUF_USERPTR)
579                         g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
580                                         (unsigned long)&src->user_ptr[0]);
581                 else
582                         g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
583
584                 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
585                 break;
586         case G2D_SELECT_MODE_FGCOLOR:
587                 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
588                 break;
589         case G2D_SELECT_MODE_BGCOLOR:
590                 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
591                 break;
592         default:
593                 fprintf(stderr , "failed to set src.\n");
594                 return -EINVAL;
595         }
596
597         src_w = w;
598         src_h = h;
599         if (src_x + w > src->width)
600                 src_w = src->width - src_x;
601         if (src_y + h > src->height)
602                 src_h = src->height - src_y;
603
604         dst_w = w;
605         dst_h = h;
606         if (dst_x + w > dst->width)
607                 dst_w = dst->width - dst_x;
608         if (dst_y + h > dst->height)
609                 dst_h = dst->height - dst_y;
610
611         w = MIN(src_w, dst_w);
612         h = MIN(src_h, dst_h);
613
614         if (w <= 0 || h <= 0) {
615                 fprintf(stderr, "invalid width or height.\n");
616                 g2d_reset(ctx);
617                 return -EINVAL;
618         }
619
620         bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
621         blend.val = g2d_get_blend_op(op);
622         g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
623         g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
624
625         pt.val = 0;
626         pt.data.x = src_x;
627         pt.data.y = src_y;
628         g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
629         pt.val = 0;
630         pt.data.x = src_x + w;
631         pt.data.y = src_y + h;
632         g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
633
634         pt.val = 0;
635         pt.data.x = dst_x;
636         pt.data.y = dst_y;
637         g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
638         pt.val = 0;
639         pt.data.x = dst_x + w;
640         pt.data.y = dst_y + h;
641         g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
642
643         g2d_flush(ctx);
644
645         return 0;
646 }
647