source: mainline/uspace/lib/draw/drawctx.c@ 6d5e378

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6d5e378 was 6d5e378, checked in by Martin Decky <martin@…>, 13 years ago

cherrypick GUI implementation (originally by Petr Koupy), with several major changes

  • for character-oriented devices a new output server and output protocol was created based on the original fb server
  • DDF visualizer drivers are pixel-oriented only
  • console and compositor can coexist in the same build
  • terminal widget is self-sufficient, no strange console nesting is needed
  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[6d5e378]1/*
2 * Copyright (c) 2012 Petr Koupy
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 draw
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <assert.h>
37#include <adt/list.h>
38#include <malloc.h>
39
40#include "drawctx.h"
41
42void drawctx_init(drawctx_t *context, surface_t *surface)
43{
44 assert(surface);
45 list_initialize(&context->list);
46 context->surface = surface;
47 context->compose = compose_src;
48 context->mask = NULL;
49 context->source = NULL;
50 context->shall_clip = false;
51 context->clip_x = 0;
52 context->clip_y = 0;
53 surface_get_resolution(surface, &context->clip_width, &context->clip_height);
54}
55
56void drawctx_save(drawctx_t *context)
57{
58 drawctx_t *to_save = (drawctx_t *) malloc(sizeof(drawctx_t));
59 if (to_save) {
60 link_initialize(&to_save->link);
61 to_save->compose = context->compose;
62 to_save->mask = context->mask;
63 to_save->source = context->source;
64 to_save->shall_clip = context->shall_clip;
65 to_save->clip_x = context->clip_x;
66 to_save->clip_y = context->clip_y;
67 to_save->clip_width = context->clip_width;
68 to_save->clip_height = context->clip_height;
69 list_append(&to_save->link, &context->list);
70 }
71}
72
73void drawctx_restore(drawctx_t *context)
74{
75 drawctx_t *to_load = (drawctx_t *) list_last(&context->list);
76 if (to_load) {
77 list_remove(&to_load->link);
78 context->compose = to_load->compose;
79 context->mask = to_load->mask;
80 context->source = to_load->source;
81 context->shall_clip = to_load->shall_clip;
82 context->clip_x = to_load->clip_x;
83 context->clip_y = to_load->clip_y;
84 context->clip_width = to_load->clip_width;
85 context->clip_height = to_load->clip_height;
86 free(to_load);
87 }
88}
89
90void drawctx_set_compose(drawctx_t *context, compose_t compose)
91{
92 context->compose = compose;
93}
94
95void drawctx_set_clip(drawctx_t *context,
96 sysarg_t x, sysarg_t y, sysarg_t width, sysarg_t height)
97{
98 surface_get_resolution(context->surface,
99 &context->clip_width, &context->clip_height);
100 context->shall_clip = (x > 0) || (y > 0) ||
101 (width < context->clip_width) || (height < context->clip_height);
102
103 context->clip_x = x;
104 context->clip_y = y;
105 context->clip_width = width;
106 context->clip_height = height;
107}
108
109void drawctx_set_mask(drawctx_t *context, surface_t *mask)
110{
111 context->mask = mask;
112}
113
114void drawctx_set_source(drawctx_t *context, source_t *source)
115{
116 context->source = source;
117}
118
119void drawctx_set_font(drawctx_t *context, font_t *font)
120{
121 context->font = font;
122}
123
124void drawctx_transfer(drawctx_t *context,
125 sysarg_t x, sysarg_t y, sysarg_t width, sysarg_t height)
126{
127 if (!context->source) {
128 return;
129 }
130
131 bool clipped = false;
132 bool masked = false;
133
134 for (sysarg_t _y = y; _y < y + height; ++_y) {
135 for (sysarg_t _x = x; _x < x + width; ++_x) {
136 if (context->shall_clip) {
137 clipped = _x < context->clip_x && _x >= context->clip_width
138 && _y < context->clip_y && _y >= context->clip_height;
139 }
140
141 if (context->mask) {
142 pixel_t p = surface_get_pixel(context->mask, _x, _y);
143 masked = p > 0 ? false : true;
144 }
145
146 if (!clipped && !masked) {
147 pixel_t p_src = source_determine_pixel(context->source, _x, _y);
148 pixel_t p_dst = surface_get_pixel(context->surface, _x, _y);
149 pixel_t p_res = context->compose(p_src, p_dst);
150 surface_put_pixel(context->surface, _x, _y, p_res);
151 }
152 }
153 }
154}
155
156void drawctx_stroke(drawctx_t *context, path_t *path)
157{
158 if (!context->source || !path) {
159 return;
160 }
161
162 /* Note:
163 * Antialiasing could be achieved by up-scaling path coordinates and
164 * rendering into temporary higher-resolution surface. Then, the temporary
165 * surface would be set as a source and its damaged region would be
166 * transferred to the original surface. */
167
168 list_foreach(*((list_t *) path), link) {
169 path_step_t *step = (path_step_t *) link;
170 switch (step->type) {
171 case PATH_STEP_MOVETO:
172 // TODO
173 break;
174 case PATH_STEP_LINETO:
175 // TODO
176 break;
177 default:
178 break;
179 }
180 }
181}
182
183void drawctx_fill(drawctx_t *context, path_t *path)
184{
185 if (!context->source || !path) {
186 return;
187 }
188
189 // TODO
190}
191
192void drawctx_print(drawctx_t *context, const char *text, sysarg_t x, sysarg_t y)
193{
194 if (context->font && context->source) {
195 font_draw_text(context->font, context, context->source, text, x, y);
196 }
197}
198
199/** @}
200 */
Note: See TracBrowser for help on using the repository browser.