source: mainline/uspace/drv/fb/kfb/port.c@ 5a68791

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5a68791 was 6d527cff, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Remove headers which are no longer used

  • Property mode set to 100644
File size: 14.0 KB
Line 
1/*
2 * Copyright (c) 2019 Jiri Svoboda
3 * Copyright (c) 2006 Jakub Vana
4 * Copyright (c) 2006 Ondrej Palkovsky
5 * Copyright (c) 2008 Martin Decky
6 * Copyright (c) 2011 Petr Koupy
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * - The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/** @addtogroup kfb
34 * @{
35 */
36/**
37 * @file
38 */
39
40#include <abi/fb/visuals.h>
41#include <adt/list.h>
42#include <align.h>
43#include <as.h>
44#include <ddev_srv.h>
45#include <ddev/info.h>
46#include <ddi.h>
47#include <ddf/log.h>
48#include <errno.h>
49#include <gfx/bitmap.h>
50#include <gfx/color.h>
51#include <gfx/coord.h>
52#include <io/pixelmap.h>
53#include <ipcgfx/server.h>
54#include <mem.h>
55#include <pixconv.h>
56#include <stddef.h>
57#include <stdint.h>
58#include <stdlib.h>
59#include <sysinfo.h>
60
61#include "kfb.h"
62#include "port.h"
63
64#define FB_POS(fb, x, y) ((y) * (fb)->scanline + (x) * (fb)->pixel_bytes)
65
66typedef struct {
67 ddf_fun_t *fun;
68
69 sysarg_t paddr;
70 gfx_rect_t rect;
71 size_t offset;
72 size_t scanline;
73 visual_t visual;
74
75 pixel2visual_t pixel2visual;
76 visual2pixel_t visual2pixel;
77 visual_mask_t visual_mask;
78 size_t pixel_bytes;
79
80 size_t size;
81 uint8_t *addr;
82
83 /** Current drawing color */
84 pixel_t color;
85} kfb_t;
86
87typedef struct {
88 kfb_t *kfb;
89 gfx_bitmap_alloc_t alloc;
90 gfx_rect_t rect;
91 gfx_bitmap_flags_t flags;
92 pixel_t key_color;
93 bool myalloc;
94} kfb_bitmap_t;
95
96static errno_t kfb_ddev_get_gc(void *, sysarg_t *, sysarg_t *);
97static errno_t kfb_ddev_get_info(void *, ddev_info_t *);
98
99static errno_t kfb_gc_set_color(void *, gfx_color_t *);
100static errno_t kfb_gc_fill_rect(void *, gfx_rect_t *);
101static errno_t kfb_gc_bitmap_create(void *, gfx_bitmap_params_t *,
102 gfx_bitmap_alloc_t *, void **);
103static errno_t kfb_gc_bitmap_destroy(void *);
104static errno_t kfb_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
105static errno_t kfb_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
106
107static ddev_ops_t kfb_ddev_ops = {
108 .get_gc = kfb_ddev_get_gc,
109 .get_info = kfb_ddev_get_info
110};
111
112static gfx_context_ops_t kfb_gc_ops = {
113 .set_color = kfb_gc_set_color,
114 .fill_rect = kfb_gc_fill_rect,
115 .bitmap_create = kfb_gc_bitmap_create,
116 .bitmap_destroy = kfb_gc_bitmap_destroy,
117 .bitmap_render = kfb_gc_bitmap_render,
118 .bitmap_get_alloc = kfb_gc_bitmap_get_alloc
119};
120
121static errno_t kfb_ddev_get_gc(void *arg, sysarg_t *arg2, sysarg_t *arg3)
122{
123 kfb_t *kfb = (kfb_t *) arg;
124
125 *arg2 = ddf_fun_get_handle(kfb->fun);
126 *arg3 = 42;
127 return EOK;
128}
129
130static errno_t kfb_ddev_get_info(void *arg, ddev_info_t *info)
131{
132 kfb_t *kfb = (kfb_t *) arg;
133
134 ddev_info_init(info);
135 info->rect = kfb->rect;
136 return EOK;
137}
138
139/** Set color on KFB.
140 *
141 * Set drawing color on KFB GC.
142 *
143 * @param arg KFB
144 * @param color Color
145 *
146 * @return EOK on success or an error code
147 */
148static errno_t kfb_gc_set_color(void *arg, gfx_color_t *color)
149{
150 kfb_t *kfb = (kfb_t *) arg;
151 uint16_t r, g, b;
152
153 gfx_color_get_rgb_i16(color, &r, &g, &b);
154 kfb->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
155 return EOK;
156}
157
158/** Fill rectangle on KFB.
159 *
160 * @param arg KFB
161 * @param rect Rectangle
162 *
163 * @return EOK on success or an error code
164 */
165static errno_t kfb_gc_fill_rect(void *arg, gfx_rect_t *rect)
166{
167 kfb_t *kfb = (kfb_t *) arg;
168 gfx_rect_t crect;
169 gfx_coord_t x, y;
170
171 /* Make sure we have a sorted, clipped rectangle */
172 gfx_rect_clip(rect, &kfb->rect, &crect);
173
174 for (y = crect.p0.y; y < crect.p1.y; y++) {
175 for (x = crect.p0.x; x < crect.p1.x; x++) {
176 kfb->pixel2visual(kfb->addr + FB_POS(kfb, x, y),
177 kfb->color);
178 }
179 }
180
181 return EOK;
182}
183
184/** Create bitmap in KFB GC.
185 *
186 * @param arg KFB
187 * @param params Bitmap params
188 * @param alloc Bitmap allocation info or @c NULL
189 * @param rbm Place to store pointer to new bitmap
190 * @return EOK on success or an error code
191 */
192errno_t kfb_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
193 gfx_bitmap_alloc_t *alloc, void **rbm)
194{
195 kfb_t *kfb = (kfb_t *) arg;
196 kfb_bitmap_t *kfbbm = NULL;
197 gfx_coord2_t dim;
198 errno_t rc;
199
200 /* Check that we support all required flags */
201 if ((params->flags & ~(bmpf_color_key | bmpf_colorize)) != 0)
202 return ENOTSUP;
203
204 kfbbm = calloc(1, sizeof(kfb_bitmap_t));
205 if (kfbbm == NULL)
206 return ENOMEM;
207
208 gfx_coord2_subtract(&params->rect.p1, &params->rect.p0, &dim);
209 kfbbm->rect = params->rect;
210 kfbbm->flags = params->flags;
211 kfbbm->key_color = params->key_color;
212
213 if (alloc == NULL) {
214 kfbbm->alloc.pitch = dim.x * sizeof(uint32_t);
215 kfbbm->alloc.off0 = 0;
216 kfbbm->alloc.pixels = malloc(kfbbm->alloc.pitch * dim.y);
217 kfbbm->myalloc = true;
218
219 if (kfbbm->alloc.pixels == NULL) {
220 rc = ENOMEM;
221 goto error;
222 }
223 } else {
224 kfbbm->alloc = *alloc;
225 }
226
227 kfbbm->kfb = kfb;
228 *rbm = (void *)kfbbm;
229 return EOK;
230error:
231 if (rbm != NULL)
232 free(kfbbm);
233 return rc;
234}
235
236/** Destroy bitmap in KFB GC.
237 *
238 * @param bm Bitmap
239 * @return EOK on success or an error code
240 */
241static errno_t kfb_gc_bitmap_destroy(void *bm)
242{
243 kfb_bitmap_t *kfbbm = (kfb_bitmap_t *)bm;
244 if (kfbbm->myalloc)
245 free(kfbbm->alloc.pixels);
246 free(kfbbm);
247 return EOK;
248}
249
250/** Render bitmap in KFB GC.
251 *
252 * @param bm Bitmap
253 * @param srect0 Source rectangle or @c NULL
254 * @param offs0 Offset or @c NULL
255 * @return EOK on success or an error code
256 */
257static errno_t kfb_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
258 gfx_coord2_t *offs0)
259{
260 kfb_bitmap_t *kfbbm = (kfb_bitmap_t *)bm;
261 kfb_t *kfb = kfbbm->kfb;
262 gfx_rect_t srect;
263 gfx_rect_t drect;
264 gfx_rect_t skfbrect;
265 gfx_rect_t crect;
266 gfx_coord2_t offs;
267 gfx_coord2_t bmdim;
268 gfx_coord2_t dim;
269 gfx_coord2_t sp;
270 gfx_coord2_t dp;
271 gfx_coord2_t pos;
272 pixelmap_t pbm;
273 pixel_t color;
274
275 /* Clip source rectangle to bitmap bounds */
276
277 if (srect0 != NULL)
278 gfx_rect_clip(srect0, &kfbbm->rect, &srect);
279 else
280 srect = kfbbm->rect;
281
282 if (offs0 != NULL) {
283 offs = *offs0;
284 } else {
285 offs.x = 0;
286 offs.y = 0;
287 }
288
289 /* Destination rectangle */
290 gfx_rect_translate(&offs, &srect, &drect);
291 gfx_coord2_subtract(&drect.p1, &drect.p0, &dim);
292 gfx_coord2_subtract(&kfbbm->rect.p1, &kfbbm->rect.p0, &bmdim);
293
294 pbm.width = bmdim.x;
295 pbm.height = bmdim.y;
296 pbm.data = kfbbm->alloc.pixels;
297
298 /* Transform KFB bounding rectangle back to bitmap coordinate system */
299 gfx_rect_rtranslate(&offs, &kfb->rect, &skfbrect);
300
301 /*
302 * Make sure we have a sorted source rectangle, clipped so that
303 * destination lies within KFB bounding rectangle
304 */
305 gfx_rect_clip(&srect, &skfbrect, &crect);
306
307 if ((kfbbm->flags & bmpf_color_key) != 0) {
308 /* Simple copy */
309 for (pos.y = crect.p0.y; pos.y < crect.p1.y; pos.y++) {
310 for (pos.x = crect.p0.x; pos.x < crect.p1.x; pos.x++) {
311 gfx_coord2_subtract(&pos, &kfbbm->rect.p0, &sp);
312 gfx_coord2_add(&pos, &offs, &dp);
313
314 color = pixelmap_get_pixel(&pbm, sp.x, sp.y);
315 if (color != kfbbm->key_color) {
316 kfb->pixel2visual(kfb->addr +
317 FB_POS(kfb, dp.x, dp.y), color);
318 }
319 }
320 }
321 } else if ((kfbbm->flags & bmpf_colorize) != 0) {
322 /* Color key */
323 for (pos.y = crect.p0.y; pos.y < crect.p1.y; pos.y++) {
324 for (pos.x = crect.p0.x; pos.x < crect.p1.x; pos.x++) {
325 gfx_coord2_subtract(&pos, &kfbbm->rect.p0, &sp);
326 gfx_coord2_add(&pos, &offs, &dp);
327
328 color = pixelmap_get_pixel(&pbm, sp.x, sp.y);
329 kfb->pixel2visual(kfb->addr +
330 FB_POS(kfb, dp.x, dp.y), color);
331 }
332 }
333 } else {
334 /* Color key & colorize */
335 for (pos.y = crect.p0.y; pos.y < crect.p1.y; pos.y++) {
336 for (pos.x = crect.p0.x; pos.x < crect.p1.x; pos.x++) {
337 gfx_coord2_subtract(&pos, &kfbbm->rect.p0, &sp);
338 gfx_coord2_add(&pos, &offs, &dp);
339
340 color = pixelmap_get_pixel(&pbm, sp.x, sp.y);
341 kfb->pixel2visual(kfb->addr +
342 FB_POS(kfb, dp.x, dp.y), color);
343 }
344 }
345 }
346
347 return EOK;
348}
349
350/** Get allocation info for bitmap in KFB GC.
351 *
352 * @param bm Bitmap
353 * @param alloc Place to store allocation info
354 * @return EOK on success or an error code
355 */
356static errno_t kfb_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
357{
358 kfb_bitmap_t *kfbbm = (kfb_bitmap_t *)bm;
359 *alloc = kfbbm->alloc;
360 return EOK;
361}
362
363#include <stdio.h>
364static void kfb_client_conn(ipc_call_t *icall, void *arg)
365{
366 kfb_t *kfb;
367 ddev_srv_t srv;
368 sysarg_t gc_id;
369 gfx_context_t *gc;
370 errno_t rc;
371
372 kfb = (kfb_t *) ddf_fun_data_get((ddf_fun_t *) arg);
373
374 printf("kfb_client_conn arg2=%lu arg3=%lu arg4=%lu\n",
375 (unsigned long) ipc_get_arg2(icall),
376 (unsigned long) ipc_get_arg3(icall),
377 (unsigned long) ipc_get_arg4(icall));
378
379 gc_id = ipc_get_arg3(icall);
380
381 if (gc_id == 0) {
382 /* Set up protocol structure */
383 ddev_srv_initialize(&srv);
384 srv.ops = &kfb_ddev_ops;
385 srv.arg = kfb;
386
387 /* Handle connection */
388 ddev_conn(icall, &srv);
389 } else {
390 assert(gc_id == 42);
391
392 if (kfb->addr != AS_AREA_ANY) {
393 /* This means there already is a GC connection */
394 async_answer_0(icall, EBUSY);
395 return;
396 }
397
398 rc = physmem_map(kfb->paddr + kfb->offset,
399 ALIGN_UP(kfb->size, PAGE_SIZE) >> PAGE_WIDTH,
400 AS_AREA_READ | AS_AREA_WRITE, (void *) &kfb->addr);
401 if (rc != EOK)
402 goto error;
403
404 rc = gfx_context_new(&kfb_gc_ops, kfb, &gc);
405 if (rc != EOK)
406 goto error;
407
408 /* GC connection */
409 gc_conn(icall, gc);
410
411 rc = physmem_unmap(kfb->addr);
412 if (rc == EOK)
413 kfb->addr = AS_AREA_ANY;
414 }
415
416 return;
417error:
418 if (kfb->addr != AS_AREA_ANY) {
419 if (physmem_unmap(kfb->addr) == EOK)
420 kfb->addr = AS_AREA_ANY;
421 }
422
423 async_answer_0(icall, rc);
424}
425
426errno_t port_init(ddf_dev_t *dev)
427{
428 ddf_fun_t *fun = NULL;
429 kfb_t *kfb = NULL;
430 errno_t rc;
431
432 fun = ddf_fun_create(dev, fun_exposed, "kfb");
433 if (fun == NULL) {
434 rc = ENOMEM;
435 goto error;
436 }
437
438 ddf_fun_set_conn_handler(fun, &kfb_client_conn);
439
440 kfb = ddf_fun_data_alloc(fun, sizeof(kfb_t));
441 if (kfb == NULL) {
442 rc = ENOMEM;
443 goto error;
444 }
445
446 sysarg_t present;
447 rc = sysinfo_get_value("fb", &present);
448 if (rc != EOK)
449 present = false;
450
451 if (!present) {
452 ddf_fun_destroy(fun);
453 rc = ENOENT;
454 goto error;
455 }
456
457 sysarg_t kind;
458 rc = sysinfo_get_value("fb.kind", &kind);
459 if (rc != EOK)
460 kind = (sysarg_t) -1;
461
462 if (kind != 1) {
463 rc = EINVAL;
464 goto error;
465 }
466
467 sysarg_t paddr;
468 rc = sysinfo_get_value("fb.address.physical", &paddr);
469 if (rc != EOK)
470 goto error;
471
472 sysarg_t offset;
473 rc = sysinfo_get_value("fb.offset", &offset);
474 if (rc != EOK)
475 offset = 0;
476
477 sysarg_t width;
478 rc = sysinfo_get_value("fb.width", &width);
479 if (rc != EOK)
480 goto error;
481
482 sysarg_t height;
483 rc = sysinfo_get_value("fb.height", &height);
484 if (rc != EOK)
485 goto error;
486
487 sysarg_t scanline;
488 rc = sysinfo_get_value("fb.scanline", &scanline);
489 if (rc != EOK)
490 goto error;
491
492 sysarg_t visual;
493 rc = sysinfo_get_value("fb.visual", &visual);
494 if (rc != EOK)
495 goto error;
496
497 kfb->fun = fun;
498
499 kfb->rect.p0.x = 0;
500 kfb->rect.p0.y = 0;
501 kfb->rect.p1.x = width;
502 kfb->rect.p1.y = height;
503
504 kfb->paddr = paddr;
505 kfb->offset = offset;
506 kfb->scanline = scanline;
507 kfb->visual = visual;
508
509 switch (visual) {
510 case VISUAL_INDIRECT_8:
511 kfb->pixel2visual = pixel2bgr_323;
512 kfb->visual2pixel = bgr_323_2pixel;
513 kfb->visual_mask = visual_mask_323;
514 kfb->pixel_bytes = 1;
515 break;
516 case VISUAL_RGB_5_5_5_LE:
517 kfb->pixel2visual = pixel2rgb_555_le;
518 kfb->visual2pixel = rgb_555_le_2pixel;
519 kfb->visual_mask = visual_mask_555;
520 kfb->pixel_bytes = 2;
521 break;
522 case VISUAL_RGB_5_5_5_BE:
523 kfb->pixel2visual = pixel2rgb_555_be;
524 kfb->visual2pixel = rgb_555_be_2pixel;
525 kfb->visual_mask = visual_mask_555;
526 kfb->pixel_bytes = 2;
527 break;
528 case VISUAL_RGB_5_6_5_LE:
529 kfb->pixel2visual = pixel2rgb_565_le;
530 kfb->visual2pixel = rgb_565_le_2pixel;
531 kfb->visual_mask = visual_mask_565;
532 kfb->pixel_bytes = 2;
533 break;
534 case VISUAL_RGB_5_6_5_BE:
535 kfb->pixel2visual = pixel2rgb_565_be;
536 kfb->visual2pixel = rgb_565_be_2pixel;
537 kfb->visual_mask = visual_mask_565;
538 kfb->pixel_bytes = 2;
539 break;
540 case VISUAL_RGB_8_8_8:
541 kfb->pixel2visual = pixel2rgb_888;
542 kfb->visual2pixel = rgb_888_2pixel;
543 kfb->visual_mask = visual_mask_888;
544 kfb->pixel_bytes = 3;
545 break;
546 case VISUAL_BGR_8_8_8:
547 kfb->pixel2visual = pixel2bgr_888;
548 kfb->visual2pixel = bgr_888_2pixel;
549 kfb->visual_mask = visual_mask_888;
550 kfb->pixel_bytes = 3;
551 break;
552 case VISUAL_RGB_8_8_8_0:
553 kfb->pixel2visual = pixel2rgb_8880;
554 kfb->visual2pixel = rgb_8880_2pixel;
555 kfb->visual_mask = visual_mask_8880;
556 kfb->pixel_bytes = 4;
557 break;
558 case VISUAL_RGB_0_8_8_8:
559 kfb->pixel2visual = pixel2rgb_0888;
560 kfb->visual2pixel = rgb_0888_2pixel;
561 kfb->visual_mask = visual_mask_0888;
562 kfb->pixel_bytes = 4;
563 break;
564 case VISUAL_BGR_0_8_8_8:
565 kfb->pixel2visual = pixel2bgr_0888;
566 kfb->visual2pixel = bgr_0888_2pixel;
567 kfb->visual_mask = visual_mask_0888;
568 kfb->pixel_bytes = 4;
569 break;
570 case VISUAL_BGR_8_8_8_0:
571 kfb->pixel2visual = pixel2bgr_8880;
572 kfb->visual2pixel = bgr_8880_2pixel;
573 kfb->visual_mask = visual_mask_8880;
574 kfb->pixel_bytes = 4;
575 break;
576 default:
577 return EINVAL;
578 }
579
580 kfb->size = scanline * height;
581 kfb->addr = AS_AREA_ANY;
582
583 rc = ddf_fun_bind(fun);
584 if (rc != EOK)
585 goto error;
586
587 rc = ddf_fun_add_to_category(fun, "display-device");
588 if (rc != EOK) {
589 ddf_fun_unbind(fun);
590 goto error;
591 }
592
593 return EOK;
594error:
595 if (fun != NULL)
596 ddf_fun_destroy(fun);
597 return rc;
598}
599
600/** @}
601 */
Note: See TracBrowser for help on using the repository browser.