source: mainline/uspace/srv/hid/display/window.c@ 2012fe0

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

Repaint display when finished moving a window

  • Property mode set to 100644
File size: 12.3 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 display
30 * @{
31 */
32/**
33 * @file GFX window backend
34 *
35 * This implements a graphics context over display server window.
36 */
37
38#include <gfx/bitmap.h>
39#include <gfx/color.h>
40#include <gfx/coord.h>
41#include <gfx/context.h>
42#include <gfx/render.h>
43#include <io/log.h>
44#include <io/pixelmap.h>
45#include <stdlib.h>
46#include "client.h"
47#include "display.h"
48#include "window.h"
49
50static errno_t ds_window_set_color(void *, gfx_color_t *);
51static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
52static errno_t ds_window_bitmap_create(void *, gfx_bitmap_params_t *,
53 gfx_bitmap_alloc_t *, void **);
54static errno_t ds_window_bitmap_destroy(void *);
55static errno_t ds_window_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
56static errno_t ds_window_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
57
58gfx_context_ops_t ds_window_ops = {
59 .set_color = ds_window_set_color,
60 .fill_rect = ds_window_fill_rect,
61 .bitmap_create = ds_window_bitmap_create,
62 .bitmap_destroy = ds_window_bitmap_destroy,
63 .bitmap_render = ds_window_bitmap_render,
64 .bitmap_get_alloc = ds_window_bitmap_get_alloc
65};
66
67/** Set color on window GC.
68 *
69 * Set drawing color on window GC.
70 *
71 * @param arg Console GC
72 * @param color Color
73 *
74 * @return EOK on success or an error code
75 */
76static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
77{
78 ds_window_t *wnd = (ds_window_t *) arg;
79 uint16_t r, g, b;
80
81 log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color gc=%p",
82 ds_display_get_gc(wnd->display));
83
84 gfx_color_get_rgb_i16(color, &r, &g, &b);
85 wnd->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
86
87 return gfx_set_color(ds_display_get_gc(wnd->display), color);
88}
89
90/** Fill rectangle on window GC.
91 *
92 * @param arg Window GC
93 * @param rect Rectangle
94 *
95 * @return EOK on success or an error code
96 */
97static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
98{
99 ds_window_t *wnd = (ds_window_t *) arg;
100 gfx_rect_t crect;
101 gfx_rect_t drect;
102 gfx_coord_t x, y;
103
104 log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
105
106 gfx_rect_clip(rect, &wnd->rect, &crect);
107 gfx_rect_translate(&wnd->dpos, &crect, &drect);
108
109 /* Render a copy to the backbuffer */
110 for (y = crect.p0.y; y < crect.p1.y; y++) {
111 for (x = crect.p0.x; x < crect.p1.x; x++) {
112 pixelmap_put_pixel(&wnd->pixelmap, x - wnd->rect.p0.x,
113 y - wnd->rect.p0.y, wnd->color);
114 }
115 }
116
117 return gfx_fill_rect(ds_display_get_gc(wnd->display), &drect);
118}
119
120/** Create bitmap in window GC.
121 *
122 * @param arg Window GC
123 * @param params Bitmap params
124 * @param alloc Bitmap allocation info or @c NULL
125 * @param rbm Place to store pointer to new bitmap
126 * @return EOK on success or an error code
127 */
128errno_t ds_window_bitmap_create(void *arg, gfx_bitmap_params_t *params,
129 gfx_bitmap_alloc_t *alloc, void **rbm)
130{
131 ds_window_t *wnd = (ds_window_t *) arg;
132 ds_window_bitmap_t *cbm = NULL;
133 errno_t rc;
134
135 cbm = calloc(1, sizeof(ds_window_bitmap_t));
136 if (cbm == NULL)
137 return ENOMEM;
138
139 rc = gfx_bitmap_create(ds_display_get_gc(wnd->display), params, alloc,
140 &cbm->bitmap);
141 if (rc != EOK)
142 goto error;
143
144 cbm->wnd = wnd;
145 cbm->rect = params->rect;
146 *rbm = (void *)cbm;
147 return EOK;
148error:
149 if (cbm != NULL)
150 free(cbm);
151 return rc;
152}
153
154/** Destroy bitmap in window GC.
155 *
156 * @param bm Bitmap
157 * @return EOK on success or an error code
158 */
159static errno_t ds_window_bitmap_destroy(void *bm)
160{
161 ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
162
163 gfx_bitmap_destroy(cbm->bitmap);
164 free(cbm);
165 return EOK;
166}
167
168/** Render bitmap in window GC.
169 *
170 * @param bm Bitmap
171 * @param srect0 Source rectangle or @c NULL
172 * @param offs0 Offset or @c NULL
173 * @return EOK on success or an error code
174 */
175static errno_t ds_window_bitmap_render(void *bm, gfx_rect_t *srect0,
176 gfx_coord2_t *offs0)
177{
178 ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
179 gfx_coord2_t doffs;
180 gfx_coord2_t offs;
181 gfx_rect_t srect;
182 gfx_rect_t swrect;
183 gfx_rect_t crect;
184 gfx_coord_t x, y;
185 pixelmap_t pixelmap;
186 gfx_bitmap_alloc_t alloc;
187 pixel_t pixel;
188 errno_t rc;
189
190 if (srect0 != NULL) {
191 /* Clip source rectangle to bitmap rectangle */
192 gfx_rect_clip(srect0, &cbm->rect, &srect);
193 } else {
194 /* Source is entire bitmap rectangle */
195 srect = cbm->rect;
196 }
197
198 if (offs0 != NULL) {
199 offs = *offs0;
200 } else {
201 offs.x = 0;
202 offs.y = 0;
203 }
204
205 /* Transform window rectangle back to bitmap coordinate system */
206 gfx_rect_rtranslate(&offs, &cbm->wnd->rect, &swrect);
207
208 /* Clip so that transformed rectangle will be inside the window */
209 gfx_rect_clip(&srect, &swrect, &crect);
210
211 /* Offset for rendering on screen = window pos + offs */
212 gfx_coord2_add(&cbm->wnd->dpos, &offs, &doffs);
213
214 rc = gfx_bitmap_get_alloc(cbm->bitmap, &alloc);
215 if (rc != EOK)
216 return rc;
217
218 pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
219 pixelmap.height = cbm->rect.p1.y - cbm->rect.p0.y;
220 pixelmap.data = alloc.pixels;
221
222 /* Render a copy to the backbuffer */
223 for (y = crect.p0.y; y < crect.p1.y; y++) {
224 for (x = crect.p0.x; x < crect.p1.x; x++) {
225 pixel = pixelmap_get_pixel(&pixelmap,
226 x - cbm->rect.p0.x, y - cbm->rect.p0.y);
227 pixelmap_put_pixel(&cbm->wnd->pixelmap,
228 x + offs.x - cbm->rect.p0.x + cbm->wnd->rect.p0.x,
229 y + offs.y - cbm->rect.p0.y + cbm->wnd->rect.p0.y,
230 pixel);
231 }
232 }
233
234 return gfx_bitmap_render(cbm->bitmap, &crect, &doffs);
235}
236
237/** Get allocation info for bitmap in window GC.
238 *
239 * @param bm Bitmap
240 * @param alloc Place to store allocation info
241 * @return EOK on success or an error code
242 */
243static errno_t ds_window_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
244{
245 ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
246
247 return gfx_bitmap_get_alloc(cbm->bitmap, alloc);
248}
249
250/** Create window.
251 *
252 * Create graphics context for rendering into a window.
253 *
254 * @param client Client owning the window
255 * @param params Window parameters
256 * @param rgc Place to store pointer to new GC.
257 *
258 * @return EOK on success or an error code
259 */
260errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
261 ds_window_t **rgc)
262{
263 ds_window_t *wnd = NULL;
264 gfx_context_t *gc = NULL;
265 gfx_context_t *dgc;
266 gfx_rect_t rect;
267 gfx_coord2_t dims;
268 gfx_bitmap_params_t bparams;
269 gfx_bitmap_alloc_t alloc;
270 errno_t rc;
271
272 wnd = calloc(1, sizeof(ds_window_t));
273 if (wnd == NULL) {
274 rc = ENOMEM;
275 goto error;
276 }
277
278 rc = gfx_context_new(&ds_window_ops, wnd, &gc);
279 if (rc != EOK)
280 goto error;
281
282 ds_client_add_window(client, wnd);
283 ds_display_add_window(client->display, wnd);
284
285 gfx_rect_points_sort(&params->rect, &rect);
286 gfx_coord2_subtract(&rect.p1, &rect.p0, &dims);
287
288 bparams.rect = params->rect;
289
290 dgc = ds_display_get_gc(wnd->display); // XXX
291 if (dgc != NULL) {
292 rc = gfx_bitmap_create(dgc, &bparams, NULL, &wnd->bitmap);
293 if (rc != EOK)
294 goto error;
295
296 rc = gfx_bitmap_get_alloc(wnd->bitmap, &alloc);
297 if (rc != EOK)
298 goto error;
299
300 wnd->pixelmap.width = dims.x;
301 wnd->pixelmap.height = dims.y;
302 wnd->pixelmap.data = alloc.pixels;
303 }
304
305 if (wnd->pixelmap.data == NULL) {
306 rc = ENOMEM;
307 goto error;
308 }
309
310 wnd->rect = params->rect;
311 wnd->gc = gc;
312 *rgc = wnd;
313 return EOK;
314error:
315 if (wnd != NULL) {
316 if (wnd->bitmap != NULL)
317 gfx_bitmap_destroy(wnd->bitmap);
318 free(wnd);
319 }
320
321 gfx_context_delete(gc);
322 return rc;
323}
324
325/** Delete window GC.
326 *
327 * @param wnd Window GC
328 */
329void ds_window_destroy(ds_window_t *wnd)
330{
331 ds_client_remove_window(wnd);
332 ds_display_remove_window(wnd);
333 (void) gfx_context_delete(wnd->gc);
334 if (wnd->bitmap != NULL)
335 gfx_bitmap_destroy(wnd->bitmap);
336
337 free(wnd);
338}
339
340/** Get generic graphic context from window.
341 *
342 * @param wnd Window
343 * @return Graphic context
344 */
345gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
346{
347 return wnd->gc;
348}
349
350/** Paint a window using its backing bitmap.
351 *
352 * @param wnd Window to paint
353 * @param rect Display rectangle to paint to
354 * @return EOK on success or an error code
355 */
356errno_t ds_window_paint(ds_window_t *wnd, gfx_rect_t *rect)
357{
358 gfx_rect_t srect;
359 gfx_rect_t *brect;
360 gfx_rect_t crect;
361
362 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint");
363
364 if (rect != NULL) {
365 gfx_rect_rtranslate(&wnd->dpos, rect, &srect);
366
367 /* Determine if we have anything to do */
368 gfx_rect_clip(&srect, rect, &crect);
369 if (gfx_rect_is_empty(&crect))
370 return EOK;
371
372 brect = &srect;
373 } else {
374 brect = NULL;
375 }
376
377 return gfx_bitmap_render(wnd->bitmap, brect, &wnd->dpos);
378}
379
380/** Start moving a window by mouse drag.
381 *
382 * @param wnd Window
383 * @param event Button press event
384 */
385static void ds_window_start_move(ds_window_t *wnd, pos_event_t *event)
386{
387 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
388 (int) event->hpos, (int) event->vpos);
389
390 assert(wnd->state == dsw_idle);
391
392 wnd->orig_pos.x = event->hpos;
393 wnd->orig_pos.y = event->vpos;
394 wnd->state = dsw_moving;
395}
396
397/** Finish moving a window by mouse drag.
398 *
399 * @param wnd Window
400 * @param event Button release event
401 */
402static void ds_window_finish_move(ds_window_t *wnd, pos_event_t *event)
403{
404 gfx_coord2_t pos;
405 gfx_coord2_t dmove;
406 gfx_coord2_t nwpos;
407
408 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
409 (int) event->hpos, (int) event->vpos);
410
411 assert(wnd->state == dsw_moving);
412 pos.x = event->hpos;
413 pos.y = event->vpos;
414 gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
415
416 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
417 wnd->dpos = nwpos;
418 wnd->state = dsw_idle;
419
420 (void) ds_display_paint(wnd->display, NULL);
421}
422
423/** Update window position when moving by mouse drag.
424 *
425 * @param wnd Window
426 * @param event Position update event
427 */
428static void ds_window_update_move(ds_window_t *wnd, pos_event_t *event)
429{
430 gfx_coord2_t pos;
431 gfx_coord2_t dmove;
432 gfx_coord2_t nwpos;
433 gfx_rect_t drect;
434 gfx_color_t *color;
435 gfx_context_t *gc;
436 errno_t rc;
437
438 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
439 (int) event->hpos, (int) event->vpos);
440
441 gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
442
443 gc = ds_display_get_gc(wnd->display); // XXX
444 if (gc != NULL) {
445 gfx_set_color(gc, wnd->display->bg_color);
446 gfx_fill_rect(gc, &drect);
447 }
448
449 assert(wnd->state == dsw_moving);
450 pos.x = event->hpos;
451 pos.y = event->vpos;
452 gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
453
454 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
455 gfx_rect_translate(&nwpos, &wnd->rect, &drect);
456
457 wnd->orig_pos = pos;
458 wnd->dpos = nwpos;
459
460 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
461 if (rc != EOK)
462 return;
463
464 gc = ds_display_get_gc(wnd->display); // XXX
465 if (gc != NULL) {
466 gfx_set_color(gc, color);
467 gfx_fill_rect(gc, &drect);
468 }
469
470 gfx_color_delete(color);
471}
472
473/** Post position event to window.
474 *
475 * @param wnd Window
476 * @param event Position event
477 */
478errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
479{
480 log_msg(LOG_DEFAULT, LVL_DEBUG,
481 "ds_window_post_pos_event type=%d pos=%d,%d\n", event->type,
482 (int) event->hpos, (int) event->vpos);
483
484 if (event->type == POS_PRESS) {
485 if (wnd->state == dsw_idle)
486 ds_window_start_move(wnd, event);
487 }
488
489 if (event->type == POS_RELEASE) {
490 if (wnd->state == dsw_moving)
491 ds_window_finish_move(wnd, event);
492 }
493
494 if (event->type == POS_UPDATE) {
495 if (wnd->state == dsw_moving)
496 ds_window_update_move(wnd, event);
497 }
498
499 return EOK;
500}
501
502/** @}
503 */
Note: See TracBrowser for help on using the repository browser.