1 /*
2 * Copyright (C) 2013 Samsung Electronics Co.Ltd
3 * Authors:
4 * Inki Dae <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <assert.h>
31
32 #include <sys/mman.h>
33
34 #include <xf86drm.h>
35
36 #include "libdrm_macros.h"
37 #include "exynos_drm.h"
38 #include "fimg2d_reg.h"
39 #include "exynos_fimg2d.h"
40
41 #define SET_BF(val, sc, si, scsa, scda, dc, di, dcsa, dcda) \
42 val.data.src_coeff = sc; \
43 val.data.inv_src_color_coeff = si; \
44 val.data.src_coeff_src_a = scsa; \
45 val.data.src_coeff_dst_a = scda; \
46 val.data.dst_coeff = dc; \
47 val.data.inv_dst_color_coeff = di; \
48 val.data.dst_coeff_src_a = dcsa; \
49 val.data.dst_coeff_dst_a = dcda;
50
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52
53 #define MSG_PREFIX "exynos/fimg2d: "
54
55 #define G2D_MAX_CMD_NR 64
56 #define G2D_MAX_GEM_CMD_NR 64
57 #define G2D_MAX_CMD_LIST_NR 64
58
59 struct g2d_context {
60 int fd;
61 unsigned int major;
62 unsigned int minor;
63 struct drm_exynos_g2d_cmd cmd[G2D_MAX_CMD_NR];
64 struct drm_exynos_g2d_cmd cmd_buf[G2D_MAX_GEM_CMD_NR];
65 unsigned int cmd_nr;
66 unsigned int cmd_buf_nr;
67 unsigned int cmdlist_nr;
68 void *event_userdata;
69 };
70
71 enum g2d_base_addr_reg {
72 g2d_dst = 0,
73 g2d_src
74 };
75
76 enum e_g2d_dir_mode {
77 G2D_DIR_MODE_POSITIVE = 0,
78 G2D_DIR_MODE_NEGATIVE = 1
79 };
80
81 union g2d_direction_val {
82 unsigned int val[2];
83 struct {
84 /* SRC_MSK_DIRECT_REG [0:1] (source) */
85 enum e_g2d_dir_mode src_x_direction:1;
86 enum e_g2d_dir_mode src_y_direction:1;
87
88 /* SRC_MSK_DIRECT_REG [2:3] */
89 unsigned int reversed1:2;
90
91 /* SRC_MSK_DIRECT_REG [4:5] (mask) */
92 enum e_g2d_dir_mode mask_x_direction:1;
93 enum e_g2d_dir_mode mask_y_direction:1;
94
95 /* SRC_MSK_DIRECT_REG [6:31] */
96 unsigned int padding1:26;
97
98 /* DST_PAT_DIRECT_REG [0:1] (destination) */
99 enum e_g2d_dir_mode dst_x_direction:1;
100 enum e_g2d_dir_mode dst_y_direction:1;
101
102 /* DST_PAT_DIRECT_REG [2:3] */
103 unsigned int reversed2:2;
104
105 /* DST_PAT_DIRECT_REG [4:5] (pattern) */
106 enum e_g2d_dir_mode pat_x_direction:1;
107 enum e_g2d_dir_mode pat_y_direction:1;
108
109 /* DST_PAT_DIRECT_REG [6:31] */
110 unsigned int padding2:26;
111 } data;
112 };
113
g2d_get_scaling(unsigned int src,unsigned int dst)114 static unsigned int g2d_get_scaling(unsigned int src, unsigned int dst)
115 {
116 /*
117 * The G2D hw scaling factor is a normalized inverse of the scaling factor.
118 * For example: When source width is 100 and destination width is 200
119 * (scaling of 2x), then the hw factor is NC * 100 / 200.
120 * The normalization factor (NC) is 2^16 = 0x10000.
121 */
122
123 return ((src << 16) / dst);
124 }
125
g2d_get_blend_op(enum e_g2d_op op)126 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
127 {
128 union g2d_blend_func_val val;
129
130 val.val = 0;
131
132 /*
133 * The switch statement is missing the default branch since
134 * we assume that the caller checks the blending operation
135 * via g2d_validate_blending_op() first.
136 */
137 switch (op) {
138 case G2D_OP_CLEAR:
139 case G2D_OP_DISJOINT_CLEAR:
140 case G2D_OP_CONJOINT_CLEAR:
141 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
142 0, 0, 0);
143 break;
144 case G2D_OP_SRC:
145 case G2D_OP_DISJOINT_SRC:
146 case G2D_OP_CONJOINT_SRC:
147 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
148 0, 0, 0);
149 break;
150 case G2D_OP_DST:
151 case G2D_OP_DISJOINT_DST:
152 case G2D_OP_CONJOINT_DST:
153 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
154 0, 0, 0);
155 break;
156 case G2D_OP_OVER:
157 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
158 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
159 break;
160 case G2D_OP_INTERPOLATE:
161 SET_BF(val, G2D_COEFF_MODE_SRC_ALPHA, 0, 0, 0,
162 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
163 break;
164 }
165
166 return val.val;
167 }
168
169 /*
170 * g2d_check_space - check if command buffers have enough space left.
171 *
172 * @ctx: a pointer to g2d_context structure.
173 * @num_cmds: number of (regular) commands.
174 * @num_gem_cmds: number of GEM commands.
175 */
g2d_check_space(const struct g2d_context * ctx,unsigned int num_cmds,unsigned int num_gem_cmds)176 static unsigned int g2d_check_space(const struct g2d_context *ctx,
177 unsigned int num_cmds, unsigned int num_gem_cmds)
178 {
179 if (ctx->cmd_nr + num_cmds >= G2D_MAX_CMD_NR ||
180 ctx->cmd_buf_nr + num_gem_cmds >= G2D_MAX_GEM_CMD_NR)
181 return 1;
182 else
183 return 0;
184 }
185
186 /*
187 * g2d_validate_select_mode - validate select mode.
188 *
189 * @mode: the mode to validate
190 *
191 * Returns zero for an invalid mode and one otherwise.
192 */
g2d_validate_select_mode(enum e_g2d_select_mode mode)193 static int g2d_validate_select_mode(
194 enum e_g2d_select_mode mode)
195 {
196 switch (mode) {
197 case G2D_SELECT_MODE_NORMAL:
198 case G2D_SELECT_MODE_FGCOLOR:
199 case G2D_SELECT_MODE_BGCOLOR:
200 return 1;
201 }
202
203 return 0;
204 }
205
206 /*
207 * g2d_validate_blending_op - validate blending operation.
208 *
209 * @operation: the operation to validate
210 *
211 * Returns zero for an invalid mode and one otherwise.
212 */
g2d_validate_blending_op(enum e_g2d_op operation)213 static int g2d_validate_blending_op(
214 enum e_g2d_op operation)
215 {
216 switch (operation) {
217 case G2D_OP_CLEAR:
218 case G2D_OP_SRC:
219 case G2D_OP_DST:
220 case G2D_OP_OVER:
221 case G2D_OP_INTERPOLATE:
222 case G2D_OP_DISJOINT_CLEAR:
223 case G2D_OP_DISJOINT_SRC:
224 case G2D_OP_DISJOINT_DST:
225 case G2D_OP_CONJOINT_CLEAR:
226 case G2D_OP_CONJOINT_SRC:
227 case G2D_OP_CONJOINT_DST:
228 return 1;
229 }
230
231 return 0;
232 }
233
234 /*
235 * g2d_add_cmd - set given command and value to user side command buffer.
236 *
237 * @ctx: a pointer to g2d_context structure.
238 * @cmd: command data.
239 * @value: value data.
240 *
241 * The caller has to make sure that the commands buffers have enough space
242 * left to hold the command. Use g2d_check_space() to ensure this.
243 */
g2d_add_cmd(struct g2d_context * ctx,unsigned long cmd,unsigned long value)244 static void g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
245 unsigned long value)
246 {
247 switch (cmd & ~(G2D_BUF_USERPTR)) {
248 case SRC_BASE_ADDR_REG:
249 case SRC_PLANE2_BASE_ADDR_REG:
250 case DST_BASE_ADDR_REG:
251 case DST_PLANE2_BASE_ADDR_REG:
252 case PAT_BASE_ADDR_REG:
253 case MASK_BASE_ADDR_REG:
254 assert(ctx->cmd_buf_nr < G2D_MAX_GEM_CMD_NR);
255
256 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
257 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
258 ctx->cmd_buf_nr++;
259 break;
260 default:
261 assert(ctx->cmd_nr < G2D_MAX_CMD_NR);
262
263 ctx->cmd[ctx->cmd_nr].offset = cmd;
264 ctx->cmd[ctx->cmd_nr].data = value;
265 ctx->cmd_nr++;
266 break;
267 }
268 }
269
270 /*
271 * g2d_add_base_addr - helper function to set dst/src base address register.
272 *
273 * @ctx: a pointer to g2d_context structure.
274 * @img: a pointer to the dst/src g2d_image structure.
275 * @reg: the register that should be set.
276 */
g2d_add_base_addr(struct g2d_context * ctx,struct g2d_image * img,enum g2d_base_addr_reg reg)277 static void g2d_add_base_addr(struct g2d_context *ctx, struct g2d_image *img,
278 enum g2d_base_addr_reg reg)
279 {
280 const unsigned long cmd = (reg == g2d_dst) ?
281 DST_BASE_ADDR_REG : SRC_BASE_ADDR_REG;
282
283 if (img->buf_type == G2D_IMGBUF_USERPTR)
284 g2d_add_cmd(ctx, cmd | G2D_BUF_USERPTR,
285 (unsigned long)&img->user_ptr[0]);
286 else
287 g2d_add_cmd(ctx, cmd, img->bo[0]);
288 }
289
290 /*
291 * g2d_set_direction - setup direction register (useful for overlapping blits).
292 *
293 * @ctx: a pointer to g2d_context structure.
294 * @dir: a pointer to the g2d_direction_val structure.
295 */
g2d_set_direction(struct g2d_context * ctx,const union g2d_direction_val * dir)296 static void g2d_set_direction(struct g2d_context *ctx,
297 const union g2d_direction_val *dir)
298 {
299 g2d_add_cmd(ctx, SRC_MASK_DIRECT_REG, dir->val[0]);
300 g2d_add_cmd(ctx, DST_PAT_DIRECT_REG, dir->val[1]);
301 }
302
303 /*
304 * g2d_flush - submit all commands and values in user side command buffer
305 * to command queue aware of fimg2d dma.
306 *
307 * @ctx: a pointer to g2d_context structure.
308 *
309 * This function should be called after all commands and values to user
310 * side command buffer are set. It submits that buffer to the kernel side driver.
311 */
g2d_flush(struct g2d_context * ctx)312 static int g2d_flush(struct g2d_context *ctx)
313 {
314 int ret;
315 struct drm_exynos_g2d_set_cmdlist cmdlist = {0};
316
317 if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
318 return 0;
319
320 if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
321 fprintf(stderr, MSG_PREFIX "command list overflow.\n");
322 return -EINVAL;
323 }
324
325 cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
326 cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
327 cmdlist.cmd_nr = ctx->cmd_nr;
328 cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
329
330 if (ctx->event_userdata) {
331 cmdlist.event_type = G2D_EVENT_NONSTOP;
332 cmdlist.user_data = (uint64_t)(uintptr_t)(ctx->event_userdata);
333 ctx->event_userdata = NULL;
334 } else {
335 cmdlist.event_type = G2D_EVENT_NOT;
336 cmdlist.user_data = 0;
337 }
338
339 ctx->cmd_nr = 0;
340 ctx->cmd_buf_nr = 0;
341
342 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
343 if (ret < 0) {
344 fprintf(stderr, MSG_PREFIX "failed to set cmdlist.\n");
345 return ret;
346 }
347
348 ctx->cmdlist_nr++;
349
350 return ret;
351 }
352
353 /**
354 * g2d_init - create a new g2d context and get hardware version.
355 *
356 * fd: a file descriptor to an opened drm device.
357 */
g2d_init(int fd)358 drm_public struct g2d_context *g2d_init(int fd)
359 {
360 struct drm_exynos_g2d_get_ver ver;
361 struct g2d_context *ctx;
362 int ret;
363
364 ctx = calloc(1, sizeof(*ctx));
365 if (!ctx) {
366 fprintf(stderr, MSG_PREFIX "failed to allocate context.\n");
367 return NULL;
368 }
369
370 ctx->fd = fd;
371
372 ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
373 if (ret < 0) {
374 fprintf(stderr, MSG_PREFIX "failed to get version.\n");
375 free(ctx);
376 return NULL;
377 }
378
379 ctx->major = ver.major;
380 ctx->minor = ver.minor;
381
382 printf(MSG_PREFIX "G2D version (%d.%d).\n", ctx->major, ctx->minor);
383 return ctx;
384 }
385
g2d_fini(struct g2d_context * ctx)386 drm_public void g2d_fini(struct g2d_context *ctx)
387 {
388 free(ctx);
389 }
390
391 /**
392 * g2d_config_event - setup userdata configuration for a g2d event.
393 * The next invocation of a g2d call (e.g. g2d_solid_fill) is
394 * then going to flag the command buffer as 'nonstop'.
395 * Completion of the command buffer execution can then be
396 * determined by using drmHandleEvent on the DRM fd.
397 * The userdata is 'consumed' in the process.
398 *
399 * @ctx: a pointer to g2d_context structure.
400 * @userdata: a pointer to the user data
401 */
g2d_config_event(struct g2d_context * ctx,void * userdata)402 drm_public void g2d_config_event(struct g2d_context *ctx, void *userdata)
403 {
404 ctx->event_userdata = userdata;
405 }
406
407 /**
408 * g2d_exec - start the dma to process all commands summited by g2d_flush().
409 *
410 * @ctx: a pointer to g2d_context structure.
411 */
g2d_exec(struct g2d_context * ctx)412 drm_public int g2d_exec(struct g2d_context *ctx)
413 {
414 struct drm_exynos_g2d_exec exec;
415 int ret;
416
417 if (ctx->cmdlist_nr == 0)
418 return -EINVAL;
419
420 exec.async = 0;
421
422 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
423 if (ret < 0) {
424 fprintf(stderr, MSG_PREFIX "failed to execute.\n");
425 return ret;
426 }
427
428 ctx->cmdlist_nr = 0;
429
430 return ret;
431 }
432
433 /**
434 * g2d_solid_fill - fill given buffer with given color data.
435 *
436 * @ctx: a pointer to g2d_context structure.
437 * @img: a pointer to g2d_image structure including image and buffer
438 * information.
439 * @x: x start position to buffer filled with given color data.
440 * @y: y start position to buffer filled with given color data.
441 * @w: width value to buffer filled with given color data.
442 * @h: height value to buffer filled with given color data.
443 */
444 drm_public int
g2d_solid_fill(struct g2d_context * ctx,struct g2d_image * img,unsigned int x,unsigned int y,unsigned int w,unsigned int h)445 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
446 unsigned int x, unsigned int y, unsigned int w,
447 unsigned int h)
448 {
449 union g2d_bitblt_cmd_val bitblt;
450 union g2d_point_val pt;
451
452 if (g2d_check_space(ctx, 7, 1))
453 return -ENOSPC;
454
455 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
456 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
457 g2d_add_base_addr(ctx, img, g2d_dst);
458 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
459
460 if (x + w > img->width)
461 w = img->width - x;
462 if (y + h > img->height)
463 h = img->height - y;
464
465 pt.data.x = x;
466 pt.data.y = y;
467 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
468
469 pt.data.x = x + w;
470 pt.data.y = y + h;
471 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
472
473 g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
474
475 bitblt.val = 0;
476 bitblt.data.fast_solid_color_fill_en = 1;
477 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
478
479 return g2d_flush(ctx);
480 }
481
482 /**
483 * g2d_copy - copy contents in source buffer to destination buffer.
484 *
485 * @ctx: a pointer to g2d_context structure.
486 * @src: a pointer to g2d_image structure including image and buffer
487 * information to source.
488 * @dst: a pointer to g2d_image structure including image and buffer
489 * information to destination.
490 * @src_x: x start position to source buffer.
491 * @src_y: y start position to source buffer.
492 * @dst_x: x start position to destination buffer.
493 * @dst_y: y start position to destination buffer.
494 * @w: width value to source and destination buffers.
495 * @h: height value to source and destination buffers.
496 */
497 drm_public int
g2d_copy(struct g2d_context * ctx,struct g2d_image * src,struct g2d_image * dst,unsigned int src_x,unsigned int src_y,unsigned int dst_x,unsigned dst_y,unsigned int w,unsigned int h)498 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
499 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
500 unsigned int dst_x, unsigned dst_y, unsigned int w,
501 unsigned int h)
502 {
503 union g2d_rop4_val rop4;
504 union g2d_point_val pt;
505 unsigned int src_w, src_h, dst_w, dst_h;
506
507 src_w = w;
508 src_h = h;
509 if (src_x + src->width > w)
510 src_w = src->width - src_x;
511 if (src_y + src->height > h)
512 src_h = src->height - src_y;
513
514 dst_w = w;
515 dst_h = w;
516 if (dst_x + dst->width > w)
517 dst_w = dst->width - dst_x;
518 if (dst_y + dst->height > h)
519 dst_h = dst->height - dst_y;
520
521 w = MIN(src_w, dst_w);
522 h = MIN(src_h, dst_h);
523
524 if (w <= 0 || h <= 0) {
525 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
526 return -EINVAL;
527 }
528
529 if (g2d_check_space(ctx, 11, 2))
530 return -ENOSPC;
531
532 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
533 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
534 g2d_add_base_addr(ctx, dst, g2d_dst);
535 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
536
537 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
538 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
539 g2d_add_base_addr(ctx, src, g2d_src);
540 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
541
542 pt.data.x = src_x;
543 pt.data.y = src_y;
544 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
545 pt.data.x = src_x + w;
546 pt.data.y = src_y + h;
547 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
548
549 pt.data.x = dst_x;
550 pt.data.y = dst_y;
551 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
552 pt.data.x = dst_x + w;
553 pt.data.y = dst_y + h;
554 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
555
556 rop4.val = 0;
557 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
558 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
559
560 return g2d_flush(ctx);
561 }
562
563 /**
564 * g2d_move - copy content inside single buffer.
565 * Similar to libc's memmove() this copies a rectangular
566 * region of the provided buffer to another location, while
567 * properly handling the situation where source and
568 * destination rectangle overlap.
569 *
570 * @ctx: a pointer to g2d_context structure.
571 * @img: a pointer to g2d_image structure providing
572 * buffer information.
573 * @src_x: x position of source rectangle.
574 * @src_y: y position of source rectangle.
575 * @dst_x: x position of destination rectangle.
576 * @dst_y: y position of destination rectangle.
577 * @w: width of rectangle to move.
578 * @h: height of rectangle to move.
579 */
580 drm_public int
g2d_move(struct g2d_context * ctx,struct g2d_image * img,unsigned int src_x,unsigned int src_y,unsigned int dst_x,unsigned dst_y,unsigned int w,unsigned int h)581 g2d_move(struct g2d_context *ctx, struct g2d_image *img,
582 unsigned int src_x, unsigned int src_y,
583 unsigned int dst_x, unsigned dst_y, unsigned int w,
584 unsigned int h)
585 {
586 union g2d_rop4_val rop4;
587 union g2d_point_val pt;
588 union g2d_direction_val dir;
589 unsigned int src_w, src_h, dst_w, dst_h;
590
591 src_w = w;
592 src_h = h;
593 if (src_x + img->width > w)
594 src_w = img->width - src_x;
595 if (src_y + img->height > h)
596 src_h = img->height - src_y;
597
598 dst_w = w;
599 dst_h = w;
600 if (dst_x + img->width > w)
601 dst_w = img->width - dst_x;
602 if (dst_y + img->height > h)
603 dst_h = img->height - dst_y;
604
605 w = MIN(src_w, dst_w);
606 h = MIN(src_h, dst_h);
607
608 if (w == 0 || h == 0) {
609 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
610 return -EINVAL;
611 }
612
613 if (g2d_check_space(ctx, 13, 2))
614 return -ENOSPC;
615
616 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
617 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
618
619 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
620 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, img->color_mode);
621
622 g2d_add_base_addr(ctx, img, g2d_dst);
623 g2d_add_base_addr(ctx, img, g2d_src);
624
625 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
626 g2d_add_cmd(ctx, SRC_STRIDE_REG, img->stride);
627
628 dir.val[0] = dir.val[1] = 0;
629
630 if (dst_x >= src_x)
631 dir.data.src_x_direction = dir.data.dst_x_direction = 1;
632 if (dst_y >= src_y)
633 dir.data.src_y_direction = dir.data.dst_y_direction = 1;
634
635 g2d_set_direction(ctx, &dir);
636
637 pt.data.x = src_x;
638 pt.data.y = src_y;
639 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
640 pt.data.x = src_x + w;
641 pt.data.y = src_y + h;
642 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
643
644 pt.data.x = dst_x;
645 pt.data.y = dst_y;
646 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
647 pt.data.x = dst_x + w;
648 pt.data.y = dst_y + h;
649 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
650
651 rop4.val = 0;
652 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
653 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
654
655 return g2d_flush(ctx);
656 }
657
658 /**
659 * g2d_copy_with_scale - copy contents in source buffer to destination buffer
660 * scaling up or down properly.
661 *
662 * @ctx: a pointer to g2d_context structure.
663 * @src: a pointer to g2d_image structure including image and buffer
664 * information to source.
665 * @dst: a pointer to g2d_image structure including image and buffer
666 * information to destination.
667 * @src_x: x start position to source buffer.
668 * @src_y: y start position to source buffer.
669 * @src_w: width value to source buffer.
670 * @src_h: height value to source buffer.
671 * @dst_x: x start position to destination buffer.
672 * @dst_y: y start position to destination buffer.
673 * @dst_w: width value to destination buffer.
674 * @dst_h: height value to destination buffer.
675 * @negative: indicate that it uses color negative to source and
676 * destination buffers.
677 */
678 drm_public int
g2d_copy_with_scale(struct g2d_context * ctx,struct g2d_image * src,struct g2d_image * dst,unsigned int src_x,unsigned int src_y,unsigned int src_w,unsigned int src_h,unsigned int dst_x,unsigned int dst_y,unsigned int dst_w,unsigned int dst_h,unsigned int negative)679 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
680 struct g2d_image *dst, unsigned int src_x,
681 unsigned int src_y, unsigned int src_w,
682 unsigned int src_h, unsigned int dst_x,
683 unsigned int dst_y, unsigned int dst_w,
684 unsigned int dst_h, unsigned int negative)
685 {
686 union g2d_rop4_val rop4;
687 union g2d_point_val pt;
688 unsigned int scale, repeat_pad;
689 unsigned int scale_x, scale_y;
690
691 /* Sanitize this parameter to facilitate space computation below. */
692 if (negative)
693 negative = 1;
694
695 if (src_w == dst_w && src_h == dst_h)
696 scale = 0;
697 else {
698 scale = 1;
699 scale_x = g2d_get_scaling(src_w, dst_w);
700 scale_y = g2d_get_scaling(src_h, dst_h);
701 }
702
703 repeat_pad = src->repeat_mode == G2D_REPEAT_MODE_PAD ? 1 : 0;
704
705 if (src_x + src_w > src->width)
706 src_w = src->width - src_x;
707 if (src_y + src_h > src->height)
708 src_h = src->height - src_y;
709
710 if (dst_x + dst_w > dst->width)
711 dst_w = dst->width - dst_x;
712 if (dst_y + dst_h > dst->height)
713 dst_h = dst->height - dst_y;
714
715 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
716 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
717 return -EINVAL;
718 }
719
720 if (g2d_check_space(ctx, 12 + scale * 3 + negative + repeat_pad, 2))
721 return -ENOSPC;
722
723 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
724 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
725 g2d_add_base_addr(ctx, dst, g2d_dst);
726 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
727
728 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
729 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
730
731 g2d_add_cmd(ctx, SRC_REPEAT_MODE_REG, src->repeat_mode);
732 if (repeat_pad)
733 g2d_add_cmd(ctx, SRC_PAD_VALUE_REG, dst->color);
734
735 g2d_add_base_addr(ctx, src, g2d_src);
736 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
737
738 rop4.val = 0;
739 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
740
741 if (negative) {
742 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
743 rop4.data.unmasked_rop3 ^= G2D_ROP3_DST;
744 }
745
746 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
747
748 if (scale) {
749 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
750 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
751 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
752 }
753
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.data.x = src_x + src_w;
758 pt.data.y = src_y + src_h;
759 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
760
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.data.x = dst_x + dst_w;
765 pt.data.y = dst_y + dst_h;
766 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
767
768 return g2d_flush(ctx);
769 }
770
771 /**
772 * g2d_blend - blend image data in source and destination buffers.
773 *
774 * @ctx: a pointer to g2d_context structure.
775 * @src: a pointer to g2d_image structure including image and buffer
776 * information to source.
777 * @dst: a pointer to g2d_image structure including image and buffer
778 * information to destination.
779 * @src_x: x start position to source buffer.
780 * @src_y: y start position to source buffer.
781 * @dst_x: x start position to destination buffer.
782 * @dst_y: y start position to destination buffer.
783 * @w: width value to source and destination buffer.
784 * @h: height value to source and destination buffer.
785 * @op: blend operation type.
786 */
787 drm_public int
g2d_blend(struct g2d_context * ctx,struct g2d_image * src,struct g2d_image * dst,unsigned int src_x,unsigned int src_y,unsigned int dst_x,unsigned int dst_y,unsigned int w,unsigned int h,enum e_g2d_op op)788 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
789 struct g2d_image *dst, unsigned int src_x,
790 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
791 unsigned int w, unsigned int h, enum e_g2d_op op)
792 {
793 union g2d_point_val pt;
794 union g2d_bitblt_cmd_val bitblt;
795 union g2d_blend_func_val blend;
796 unsigned int gem_space;
797 unsigned int src_w, src_h, dst_w, dst_h;
798
799 src_w = w;
800 src_h = h;
801 if (src_x + w > src->width)
802 src_w = src->width - src_x;
803 if (src_y + h > src->height)
804 src_h = src->height - src_y;
805
806 dst_w = w;
807 dst_h = h;
808 if (dst_x + w > dst->width)
809 dst_w = dst->width - dst_x;
810 if (dst_y + h > dst->height)
811 dst_h = dst->height - dst_y;
812
813 w = MIN(src_w, dst_w);
814 h = MIN(src_h, dst_h);
815
816 if (w <= 0 || h <= 0) {
817 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
818 return -EINVAL;
819 }
820
821 if (!g2d_validate_select_mode(src->select_mode)) {
822 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
823 return -EINVAL;
824 }
825
826 if (!g2d_validate_blending_op(op)) {
827 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
828 return -EINVAL;
829 }
830
831 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
832
833 if (g2d_check_space(ctx, 12, gem_space))
834 return -ENOSPC;
835
836 bitblt.val = 0;
837 blend.val = 0;
838
839 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
840 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
841 else
842 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
843
844 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
845 g2d_add_base_addr(ctx, dst, g2d_dst);
846 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
847
848 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
849 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
850
851 switch (src->select_mode) {
852 case G2D_SELECT_MODE_NORMAL:
853 g2d_add_base_addr(ctx, src, g2d_src);
854 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
855 break;
856 case G2D_SELECT_MODE_FGCOLOR:
857 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
858 break;
859 case G2D_SELECT_MODE_BGCOLOR:
860 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
861 break;
862 }
863
864 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
865 blend.val = g2d_get_blend_op(op);
866 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
867 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
868
869 pt.data.x = src_x;
870 pt.data.y = src_y;
871 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
872 pt.data.x = src_x + w;
873 pt.data.y = src_y + h;
874 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
875
876 pt.data.x = dst_x;
877 pt.data.y = dst_y;
878 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
879 pt.data.x = dst_x + w;
880 pt.data.y = dst_y + h;
881 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
882
883 return g2d_flush(ctx);
884 }
885
886 /**
887 * g2d_scale_and_blend - apply scaling to source buffer and then blend to destination buffer
888 *
889 * @ctx: a pointer to g2d_context structure.
890 * @src: a pointer to g2d_image structure including image and buffer
891 * information to source.
892 * @dst: a pointer to g2d_image structure including image and buffer
893 * information to destination.
894 * @src_x: x start position to source buffer.
895 * @src_y: y start position to source buffer.
896 * @src_w: width value to source buffer.
897 * @src_h: height value to source buffer.
898 * @dst_x: x start position to destination buffer.
899 * @dst_y: y start position to destination buffer.
900 * @dst_w: width value to destination buffer.
901 * @dst_h: height value to destination buffer.
902 * @op: blend operation type.
903 */
904 drm_public int
g2d_scale_and_blend(struct g2d_context * ctx,struct g2d_image * src,struct g2d_image * dst,unsigned int src_x,unsigned int src_y,unsigned int src_w,unsigned int src_h,unsigned int dst_x,unsigned int dst_y,unsigned int dst_w,unsigned int dst_h,enum e_g2d_op op)905 g2d_scale_and_blend(struct g2d_context *ctx, struct g2d_image *src,
906 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
907 unsigned int src_w, unsigned int src_h, unsigned int dst_x,
908 unsigned int dst_y, unsigned int dst_w, unsigned int dst_h,
909 enum e_g2d_op op)
910 {
911 union g2d_point_val pt;
912 union g2d_bitblt_cmd_val bitblt;
913 union g2d_blend_func_val blend;
914 unsigned int scale, gem_space;
915 unsigned int scale_x, scale_y;
916
917 if (src_w == dst_w && src_h == dst_h)
918 scale = 0;
919 else {
920 scale = 1;
921 scale_x = g2d_get_scaling(src_w, dst_w);
922 scale_y = g2d_get_scaling(src_h, dst_h);
923 }
924
925 if (src_x + src_w > src->width)
926 src_w = src->width - src_x;
927 if (src_y + src_h > src->height)
928 src_h = src->height - src_y;
929
930 if (dst_x + dst_w > dst->width)
931 dst_w = dst->width - dst_x;
932 if (dst_y + dst_h > dst->height)
933 dst_h = dst->height - dst_y;
934
935 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
936 fprintf(stderr, MSG_PREFIX "invalid width or height.\n");
937 return -EINVAL;
938 }
939
940 if (!g2d_validate_select_mode(src->select_mode)) {
941 fprintf(stderr , MSG_PREFIX "invalid select mode for source.\n");
942 return -EINVAL;
943 }
944
945 if (!g2d_validate_blending_op(op)) {
946 fprintf(stderr , MSG_PREFIX "unsupported blending operation.\n");
947 return -EINVAL;
948 }
949
950 gem_space = src->select_mode == G2D_SELECT_MODE_NORMAL ? 2 : 1;
951
952 if (g2d_check_space(ctx, 12 + scale * 3, gem_space))
953 return -ENOSPC;
954
955 bitblt.val = 0;
956 blend.val = 0;
957
958 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
959 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
960 else
961 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
962
963 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
964 g2d_add_base_addr(ctx, dst, g2d_dst);
965 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
966
967 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
968 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
969
970 switch (src->select_mode) {
971 case G2D_SELECT_MODE_NORMAL:
972 g2d_add_base_addr(ctx, src, g2d_src);
973 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
974 break;
975 case G2D_SELECT_MODE_FGCOLOR:
976 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
977 break;
978 case G2D_SELECT_MODE_BGCOLOR:
979 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
980 break;
981 }
982
983 if (scale) {
984 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
985 g2d_add_cmd(ctx, SRC_XSCALE_REG, scale_x);
986 g2d_add_cmd(ctx, SRC_YSCALE_REG, scale_y);
987 }
988
989 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
990 blend.val = g2d_get_blend_op(op);
991 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
992 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
993
994 pt.data.x = src_x;
995 pt.data.y = src_y;
996 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
997 pt.data.x = src_x + src_w;
998 pt.data.y = src_y + src_h;
999 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
1000
1001 pt.data.x = dst_x;
1002 pt.data.y = dst_y;
1003 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
1004 pt.data.x = dst_x + dst_w;
1005 pt.data.y = dst_y + dst_h;
1006 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
1007
1008 return g2d_flush(ctx);
1009 }
1010