source: mainline/uspace/lib/gfx/src/backend/canvas.c@ 00e8290

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

Canvas backend for libgfx

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2019 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libgfx
30 * @{
31 */
32/**
33 * @file GFX canvas backend
34 *
35 * This implements a graphics context over a libgui canvas.
36 * This is just for experimentation purposes and its kind of backwards.
37 */
38
39#include <gfx/backend/canvas.h>
40#include <gfx/context.h>
41#include <gfx/render.h>
42#include <io/pixel.h>
43#include <stdlib.h>
44#include "../../private/backend/canvas.h"
45#include "../../private/color.h"
46
47static errno_t canvas_gc_set_color(void *, gfx_color_t *);
48static errno_t canvas_gc_fill_rect(void *, gfx_rect_t *);
49
50gfx_context_ops_t canvas_gc_ops = {
51 .set_color = canvas_gc_set_color,
52 .fill_rect = canvas_gc_fill_rect
53};
54
55/** Set color on canvas GC.
56 *
57 * Set drawing color on canvas GC.
58 *
59 * @param arg Canvas GC
60 * @param color Color
61 *
62 * @return EOK on success or an error code
63 */
64static errno_t canvas_gc_set_color(void *arg, gfx_color_t *color)
65{
66 canvas_gc_t *cgc = (canvas_gc_t *) arg;
67
68 cgc->color = PIXEL(0, color->r >> 8, color->g >> 8, color->b >> 8);
69 return EOK;
70}
71
72/** Fill rectangle on canvas GC.
73 *
74 * @param arg Canvas GC
75 * @param rect Rectangle
76 *
77 * @return EOK on success or an error code
78 */
79static errno_t canvas_gc_fill_rect(void *arg, gfx_rect_t *rect)
80{
81 canvas_gc_t *cgc = (canvas_gc_t *) arg;
82 int x, y;
83
84 // XXX We should handle p0.x > p1.x and p0.y > p1.y
85
86 for (y = rect->p0.y; y < rect->p1.y; y++) {
87 for (x = rect->p0.x; x < rect->p1.x; x++) {
88 surface_put_pixel(cgc->surface, x, y, cgc->color);
89 }
90 }
91
92 update_canvas(cgc->canvas, cgc->surface);
93
94 return EOK;
95}
96
97/** Create canvas GC.
98 *
99 * Create graphics context for rendering into a canvas.
100 *
101 * @param con Canvas object
102 * @param fout File to which characters are written (canvas)
103 * @param rgc Place to store pointer to new GC.
104 *
105 * @return EOK on success or an error code
106 */
107errno_t canvas_gc_create(canvas_t *canvas, surface_t *surface,
108 canvas_gc_t **rgc)
109{
110 canvas_gc_t *cgc = NULL;
111 gfx_context_t *gc = NULL;
112 errno_t rc;
113
114 cgc = calloc(1, sizeof(canvas_gc_t));
115 if (cgc == NULL) {
116 rc = ENOMEM;
117 goto error;
118 }
119
120 rc = gfx_context_new(&canvas_gc_ops, cgc, &gc);
121 if (rc != EOK)
122 goto error;
123
124 cgc->gc = gc;
125 cgc->canvas = canvas;
126 cgc->surface = surface;
127 *rgc = cgc;
128 return EOK;
129error:
130 if (cgc != NULL)
131 free(cgc);
132 gfx_context_delete(gc);
133 return rc;
134}
135
136/** Delete canvas GC.
137 *
138 * @param cgc Canvas GC
139 */
140errno_t canvas_gc_delete(canvas_gc_t *cgc)
141{
142 errno_t rc;
143
144 rc = gfx_context_delete(cgc->gc);
145 if (rc != EOK)
146 return rc;
147
148 free(cgc);
149 return EOK;
150}
151
152/** Get generic graphic context from canvas GC.
153 *
154 * @param cgc Canvas GC
155 * @return Graphic context
156 */
157gfx_context_t *canvas_gc_get_ctx(canvas_gc_t *cgc)
158{
159 return cgc->gc;
160}
161
162/** @}
163 */
Note: See TracBrowser for help on using the repository browser.