source: mainline/uspace/lib/memgfx/test/xlategc.c

Last change on this file was 8ce56a6, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Translate window rendering in console/SSR with translation GC

In full-screen mode (such as text/console) and server-side rendering
we need other mechanism than memory GC to translate the coordinates
while rendering. We use a translation GC (xlategc).

Note the extensively elaborate testing of this very simple GC seems
quite overboard. At least the very elaborate test_gc_t could be hoisted
into a library and used wherever a test GC is required.

  • Property mode set to 100644
File size: 23.6 KB
Line 
1/*
2 * Copyright (c) 2021 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#include <errno.h>
30#include <gfx/bitmap.h>
31#include <gfx/color.h>
32#include <gfx/coord.h>
33#include <gfx/context.h>
34#include <gfx/cursor.h>
35#include <gfx/render.h>
36#include <mem.h>
37#include <memgfx/xlategc.h>
38#include <pcut/pcut.h>
39
40PCUT_INIT;
41
42PCUT_TEST_SUITE(xlategc);
43
44static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
45static errno_t testgc_set_color(void *, gfx_color_t *);
46static errno_t testgc_fill_rect(void *, gfx_rect_t *);
47static errno_t testgc_update(void *);
48static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
49 gfx_bitmap_alloc_t *, void **);
50static errno_t testgc_bitmap_destroy(void *);
51static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
52static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
53static errno_t testgc_cursor_get_pos(void *, gfx_coord2_t *);
54static errno_t testgc_cursor_set_pos(void *, gfx_coord2_t *);
55static errno_t testgc_cursor_set_visible(void *, bool);
56
57static gfx_context_ops_t testgc_ops = {
58 .set_clip_rect = testgc_set_clip_rect,
59 .set_color = testgc_set_color,
60 .fill_rect = testgc_fill_rect,
61 .update = testgc_update,
62 .bitmap_create = testgc_bitmap_create,
63 .bitmap_destroy = testgc_bitmap_destroy,
64 .bitmap_render = testgc_bitmap_render,
65 .bitmap_get_alloc = testgc_bitmap_get_alloc,
66 .cursor_get_pos = testgc_cursor_get_pos,
67 .cursor_set_pos = testgc_cursor_set_pos,
68 .cursor_set_visible = testgc_cursor_set_visible
69};
70
71typedef struct {
72 /** Return code to return */
73 errno_t rc;
74 /** True if set_clip_rect was called */
75 bool set_clip_rect_called;
76 /** Argument to set_clip_rect */
77 gfx_rect_t set_clip_rect_rect;
78 /** True if set_color was called */
79 bool set_color_called;
80 /** Argument to set_color (red) */
81 uint16_t set_color_r;
82 /** Argument to set_color (green) */
83 uint16_t set_color_g;
84 /** Argument to set_color (blue) */
85 uint16_t set_color_b;
86 /** True if fill_rect was called */
87 bool fill_rect_called;
88 /** Argument to fill_rect */
89 gfx_rect_t fill_rect_rect;
90 /** True if bitmap_create was called */
91 bool bitmap_create_called;
92 /** Bitmap parameters passed to bitmap_create */
93 gfx_bitmap_params_t bitmap_create_params;
94 /** Allocation info passed to bitmap_create */
95 gfx_bitmap_alloc_t bitmap_create_alloc;
96 /** Cookie to store in the created bitmap */
97 uint32_t bitmap_create_cookie;
98 /** True if bitmap_destroy was called */
99 bool bitmap_destroy_called;
100 /** Cookie retrieved from bitmap by bitmap_destroy */
101 uint32_t bitmap_destroy_cookie;
102 /** True if bitmap_render was called */
103 bool bitmap_render_called;
104 /** Source rectangle passed to bitmap_render */
105 gfx_rect_t bitmap_render_srect;
106 /** Offset passed to bitmap_render */
107 gfx_coord2_t bitmap_render_off;
108 /** Cookie retrieved from bitmap by bitmap_render */
109 uint32_t bitmap_render_cookie;
110 /** True if bitmap_get_alloc was called */
111 bool bitmap_get_alloc_called;
112 /** Cookie retrieved from bitmap by bitmap_get_alloc */
113 uint32_t bitmap_get_alloc_cookie;
114 /** Allocation info to return from bitmap_get_alloc */
115 gfx_bitmap_alloc_t bitmap_get_alloc_alloc;
116 /** True if update was called */
117 bool update_called;
118 /** True if cursor_get_pos was called */
119 bool cursor_get_pos_called;
120 /** Position to return from cursor_get_pos */
121 gfx_coord2_t cursor_get_pos_pos;
122 /** True if cursor_set_pos was called */
123 bool cursor_set_pos_called;
124 /** Position passed to cursor_set_pos */
125 gfx_coord2_t cursor_set_pos_pos;
126 /** True if cursor_set_visible was called */
127 bool cursor_set_visible_called;
128 /** Value passed to cursor_set_visible */
129 bool cursor_set_visible_vis;
130} test_gc_t;
131
132typedef struct {
133 test_gc_t *gc;
134 /** Cookie passed around for verification */
135 uint32_t cookie;
136} test_gc_bitmap_t;
137
138/** Test creating and deleting a translation GC */
139PCUT_TEST(create_delete)
140{
141 test_gc_t test_gc;
142 gfx_context_t *tgc;
143 xlate_gc_t *xlategc;
144 gfx_coord2_t off;
145 errno_t rc;
146
147 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
148 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149
150 off.x = 10;
151 off.y = 20;
152 rc = xlate_gc_create(&off, tgc, &xlategc);
153 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
154
155 xlate_gc_delete(xlategc);
156 gfx_context_delete(tgc);
157}
158
159/** Test setting a clipping rectangle in translation GC */
160PCUT_TEST(set_clip_rect)
161{
162 test_gc_t test_gc;
163 gfx_context_t *tgc;
164 xlate_gc_t *xlategc;
165 gfx_context_t *xgc;
166 gfx_rect_t rect;
167 gfx_coord2_t off;
168 errno_t rc;
169
170 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
171 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
172
173 off.x = 10;
174 off.y = 20;
175 rc = xlate_gc_create(&off, tgc, &xlategc);
176 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
177
178 xgc = xlate_gc_get_ctx(xlategc);
179
180 memset(&test_gc, 0, sizeof(test_gc));
181
182 rect.p0.x = 1;
183 rect.p0.y = 2;
184 rect.p1.x = 3;
185 rect.p1.y = 4;
186
187 test_gc.rc = EOK;
188 rc = gfx_set_clip_rect(xgc, &rect);
189 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
190
191 PCUT_ASSERT_TRUE(test_gc.set_clip_rect_called);
192 PCUT_ASSERT_INT_EQUALS(11, test_gc.set_clip_rect_rect.p0.x);
193 PCUT_ASSERT_INT_EQUALS(22, test_gc.set_clip_rect_rect.p0.y);
194 PCUT_ASSERT_INT_EQUALS(13, test_gc.set_clip_rect_rect.p1.x);
195 PCUT_ASSERT_INT_EQUALS(24, test_gc.set_clip_rect_rect.p1.y);
196
197 test_gc.rc = EIO;
198 rc = gfx_set_clip_rect(xgc, &rect);
199 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
200
201 xlate_gc_delete(xlategc);
202 gfx_context_delete(tgc);
203}
204
205/** Test setting color in translation GC */
206PCUT_TEST(set_color)
207{
208 test_gc_t test_gc;
209 gfx_context_t *tgc;
210 xlate_gc_t *xlategc;
211 gfx_context_t *xgc;
212 gfx_color_t *color;
213 gfx_coord2_t off;
214 errno_t rc;
215
216 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
217 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
218
219 off.x = 10;
220 off.y = 20;
221 rc = xlate_gc_create(&off, tgc, &xlategc);
222 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
223
224 xgc = xlate_gc_get_ctx(xlategc);
225
226 memset(&test_gc, 0, sizeof(test_gc));
227
228 rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
229 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
230
231 test_gc.rc = EOK;
232 rc = gfx_set_color(xgc, color);
233 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
234
235 PCUT_ASSERT_TRUE(test_gc.set_color_called);
236 PCUT_ASSERT_INT_EQUALS(1, test_gc.set_color_r);
237 PCUT_ASSERT_INT_EQUALS(2, test_gc.set_color_g);
238 PCUT_ASSERT_INT_EQUALS(3, test_gc.set_color_b);
239
240 test_gc.rc = EIO;
241 rc = gfx_set_color(xgc, color);
242 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
243
244 xlate_gc_delete(xlategc);
245 gfx_context_delete(tgc);
246}
247
248/** Test filling a rectangle in translation GC */
249PCUT_TEST(fill_rect)
250{
251 test_gc_t test_gc;
252 gfx_context_t *tgc;
253 xlate_gc_t *xlategc;
254 gfx_context_t *xgc;
255 gfx_rect_t rect;
256 gfx_coord2_t off;
257 errno_t rc;
258
259 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
260 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
261
262 off.x = 10;
263 off.y = 20;
264 rc = xlate_gc_create(&off, tgc, &xlategc);
265 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
266
267 xgc = xlate_gc_get_ctx(xlategc);
268
269 memset(&test_gc, 0, sizeof(test_gc));
270
271 rect.p0.x = 1;
272 rect.p0.y = 2;
273 rect.p1.x = 3;
274 rect.p1.y = 4;
275
276 test_gc.rc = EOK;
277 rc = gfx_fill_rect(xgc, &rect);
278 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
279
280 PCUT_ASSERT_TRUE(test_gc.fill_rect_called);
281 PCUT_ASSERT_INT_EQUALS(11, test_gc.fill_rect_rect.p0.x);
282 PCUT_ASSERT_INT_EQUALS(22, test_gc.fill_rect_rect.p0.y);
283 PCUT_ASSERT_INT_EQUALS(13, test_gc.fill_rect_rect.p1.x);
284 PCUT_ASSERT_INT_EQUALS(24, test_gc.fill_rect_rect.p1.y);
285
286 test_gc.rc = EIO;
287 rc = gfx_fill_rect(xgc, &rect);
288 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
289
290 xlate_gc_delete(xlategc);
291 gfx_context_delete(tgc);
292}
293
294/** Test updating a translation GC */
295PCUT_TEST(update)
296{
297 test_gc_t test_gc;
298 gfx_context_t *tgc;
299 xlate_gc_t *xlategc;
300 gfx_context_t *xgc;
301 gfx_rect_t rect;
302 gfx_coord2_t off;
303 errno_t rc;
304
305 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
306 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
307
308 off.x = 10;
309 off.y = 20;
310 rc = xlate_gc_create(&off, tgc, &xlategc);
311 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
312
313 xgc = xlate_gc_get_ctx(xlategc);
314
315 memset(&test_gc, 0, sizeof(test_gc));
316
317 rect.p0.x = 1;
318 rect.p0.y = 2;
319 rect.p1.x = 3;
320 rect.p1.y = 4;
321
322 test_gc.rc = EOK;
323 rc = gfx_set_clip_rect(xgc, &rect);
324 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
325
326 PCUT_ASSERT_TRUE(test_gc.set_clip_rect_called);
327 PCUT_ASSERT_INT_EQUALS(11, test_gc.set_clip_rect_rect.p0.x);
328 PCUT_ASSERT_INT_EQUALS(22, test_gc.set_clip_rect_rect.p0.y);
329 PCUT_ASSERT_INT_EQUALS(13, test_gc.set_clip_rect_rect.p1.x);
330 PCUT_ASSERT_INT_EQUALS(24, test_gc.set_clip_rect_rect.p1.y);
331
332 test_gc.rc = EIO;
333 rc = gfx_set_clip_rect(xgc, &rect);
334 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
335
336 xlate_gc_delete(xlategc);
337 gfx_context_delete(tgc);
338}
339
340/** Test creating bitmap in translating GC */
341PCUT_TEST(bitmap_create)
342{
343 test_gc_t test_gc;
344 gfx_context_t *tgc;
345 xlate_gc_t *xlategc;
346 gfx_context_t *xgc;
347 gfx_bitmap_params_t params;
348 gfx_bitmap_alloc_t alloc;
349 gfx_coord2_t off;
350 gfx_bitmap_t *bitmap;
351 errno_t rc;
352
353 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
354 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
355
356 off.x = 10;
357 off.y = 20;
358 rc = xlate_gc_create(&off, tgc, &xlategc);
359 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
360
361 xgc = xlate_gc_get_ctx(xlategc);
362
363 memset(&test_gc, 0, sizeof(test_gc));
364 gfx_bitmap_params_init(&params);
365 params.rect.p0.x = 1;
366 params.rect.p0.y = 2;
367 params.rect.p1.x = 3;
368 params.rect.p1.y = 4;
369 params.flags = bmpf_direct_output;
370 params.key_color = 0x112233;
371
372 test_gc.rc = EOK;
373 rc = gfx_bitmap_create(xgc, &params, &alloc, &bitmap);
374 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
375
376 PCUT_ASSERT_TRUE(test_gc.bitmap_create_called);
377 PCUT_ASSERT_INT_EQUALS(1, test_gc.bitmap_create_params.rect.p0.x);
378 PCUT_ASSERT_INT_EQUALS(2, test_gc.bitmap_create_params.rect.p0.y);
379 PCUT_ASSERT_INT_EQUALS(3, test_gc.bitmap_create_params.rect.p1.x);
380 PCUT_ASSERT_INT_EQUALS(4, test_gc.bitmap_create_params.rect.p1.y);
381
382 test_gc.rc = EOK;
383 gfx_bitmap_destroy(bitmap);
384
385 test_gc.rc = EIO;
386 rc = gfx_bitmap_create(xgc, &params, &alloc, &bitmap);
387 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
388
389 xlate_gc_delete(xlategc);
390 gfx_context_delete(tgc);
391}
392
393/** Test destroying bitmap in translating GC */
394PCUT_TEST(bitmap_destroy)
395{
396 test_gc_t test_gc;
397 gfx_context_t *tgc;
398 xlate_gc_t *xlategc;
399 gfx_context_t *xgc;
400 gfx_bitmap_params_t params;
401 gfx_bitmap_alloc_t alloc;
402 gfx_coord2_t off;
403 gfx_bitmap_t *bitmap;
404 errno_t rc;
405
406 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
407 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
408
409 off.x = 10;
410 off.y = 20;
411 rc = xlate_gc_create(&off, tgc, &xlategc);
412 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
413
414 xgc = xlate_gc_get_ctx(xlategc);
415
416 memset(&test_gc, 0, sizeof(test_gc));
417 gfx_bitmap_params_init(&params);
418 params.rect.p0.x = 1;
419 params.rect.p0.y = 2;
420 params.rect.p1.x = 3;
421 params.rect.p1.y = 4;
422 params.flags = bmpf_direct_output;
423 params.key_color = 0x112233;
424
425 test_gc.rc = EOK;
426 test_gc.bitmap_create_cookie = 0x12345678;
427 rc = gfx_bitmap_create(xgc, &params, &alloc, &bitmap);
428 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
429 PCUT_ASSERT_INT_EQUALS(1, test_gc.bitmap_create_params.rect.p0.x);
430 PCUT_ASSERT_INT_EQUALS(2, test_gc.bitmap_create_params.rect.p0.y);
431 PCUT_ASSERT_INT_EQUALS(3, test_gc.bitmap_create_params.rect.p1.x);
432 PCUT_ASSERT_INT_EQUALS(4, test_gc.bitmap_create_params.rect.p1.y);
433 PCUT_ASSERT_INT_EQUALS(bmpf_direct_output,
434 test_gc.bitmap_create_params.flags);
435 PCUT_ASSERT_INT_EQUALS(0x112233,
436 test_gc.bitmap_create_params.key_color);
437
438 test_gc.rc = EOK;
439 rc = gfx_bitmap_destroy(bitmap);
440 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
441 PCUT_ASSERT_TRUE(test_gc.bitmap_destroy_called);
442 PCUT_ASSERT_INT_EQUALS(0x12345678, test_gc.bitmap_destroy_cookie);
443
444 test_gc.rc = EOK;
445 rc = gfx_bitmap_create(xgc, &params, &alloc, &bitmap);
446 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
447
448 test_gc.rc = EIO;
449 rc = gfx_bitmap_destroy(bitmap);
450 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
451
452 xlate_gc_delete(xlategc);
453 gfx_context_delete(tgc);
454}
455
456/** Test rendering bitmap in translation GC */
457PCUT_TEST(bitmap_render)
458{
459 test_gc_t test_gc;
460 gfx_context_t *tgc;
461 xlate_gc_t *xlategc;
462 gfx_context_t *xgc;
463 gfx_bitmap_params_t params;
464 gfx_bitmap_alloc_t alloc;
465 gfx_rect_t srect;
466 gfx_coord2_t off;
467 gfx_bitmap_t *bitmap;
468 errno_t rc;
469
470 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
471 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
472
473 off.x = 10;
474 off.y = 20;
475 rc = xlate_gc_create(&off, tgc, &xlategc);
476 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
477
478 xgc = xlate_gc_get_ctx(xlategc);
479
480 memset(&test_gc, 0, sizeof(test_gc));
481 gfx_bitmap_params_init(&params);
482 params.rect.p0.x = 1;
483 params.rect.p0.y = 2;
484 params.rect.p1.x = 3;
485 params.rect.p1.y = 4;
486
487 test_gc.rc = EOK;
488 test_gc.bitmap_create_cookie = 0x12345678;
489 rc = gfx_bitmap_create(xgc, &params, &alloc, &bitmap);
490 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
491
492 srect.p0.x = 5;
493 srect.p0.y = 6;
494 srect.p1.x = 7;
495 srect.p1.y = 8;
496
497 off.x = 100;
498 off.y = 200;
499
500 test_gc.rc = EOK;
501 rc = gfx_bitmap_render(bitmap, &srect, &off);
502 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
503 PCUT_ASSERT_TRUE(test_gc.bitmap_render_called);
504 PCUT_ASSERT_INT_EQUALS(0x12345678, test_gc.bitmap_render_cookie);
505 PCUT_ASSERT_INT_EQUALS(5, test_gc.bitmap_render_srect.p0.x);
506 PCUT_ASSERT_INT_EQUALS(6, test_gc.bitmap_render_srect.p0.y);
507 PCUT_ASSERT_INT_EQUALS(7, test_gc.bitmap_render_srect.p1.x);
508 PCUT_ASSERT_INT_EQUALS(8, test_gc.bitmap_render_srect.p1.y);
509 PCUT_ASSERT_INT_EQUALS(110, test_gc.bitmap_render_off.x);
510 PCUT_ASSERT_INT_EQUALS(220, test_gc.bitmap_render_off.y);
511
512 test_gc.rc = EIO;
513 rc = gfx_bitmap_render(bitmap, &srect, &off);
514 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
515
516 test_gc.rc = EOK;
517 rc = gfx_bitmap_destroy(bitmap);
518 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
519
520 xlate_gc_delete(xlategc);
521 gfx_context_delete(tgc);
522}
523
524/** Test getting bitmap allocation info in translation GC */
525PCUT_TEST(bitmap_get_alloc)
526{
527 test_gc_t test_gc;
528 gfx_context_t *tgc;
529 xlate_gc_t *xlategc;
530 gfx_context_t *xgc;
531 gfx_bitmap_params_t params;
532 gfx_bitmap_alloc_t alloc;
533 gfx_bitmap_alloc_t galloc;
534 gfx_coord2_t off;
535 gfx_bitmap_t *bitmap;
536 errno_t rc;
537
538 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
539 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
540
541 off.x = 10;
542 off.y = 20;
543 rc = xlate_gc_create(&off, tgc, &xlategc);
544 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
545
546 xgc = xlate_gc_get_ctx(xlategc);
547
548 memset(&test_gc, 0, sizeof(test_gc));
549 gfx_bitmap_params_init(&params);
550 params.rect.p0.x = 1;
551 params.rect.p0.y = 2;
552 params.rect.p1.x = 3;
553 params.rect.p1.y = 4;
554
555 test_gc.rc = EOK;
556 test_gc.bitmap_create_cookie = 0x12345678;
557 rc = gfx_bitmap_create(xgc, &params, &alloc, &bitmap);
558 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
559
560 test_gc.bitmap_get_alloc_alloc.pitch = 42;
561 test_gc.bitmap_get_alloc_alloc.off0 = 43;
562 test_gc.bitmap_get_alloc_alloc.pixels = malloc(1);
563 PCUT_ASSERT_NOT_NULL(test_gc.bitmap_get_alloc_alloc.pixels);
564
565 test_gc.rc = EOK;
566 rc = gfx_bitmap_get_alloc(bitmap, &galloc);
567 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
568 PCUT_ASSERT_TRUE(test_gc.bitmap_get_alloc_called);
569 PCUT_ASSERT_INT_EQUALS(42, galloc.pitch);
570 PCUT_ASSERT_INT_EQUALS(43, galloc.off0);
571 PCUT_ASSERT_EQUALS(test_gc.bitmap_get_alloc_alloc.pixels, galloc.pixels);
572
573 test_gc.rc = EIO;
574 rc = gfx_bitmap_get_alloc(bitmap, &galloc);
575 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
576
577 test_gc.rc = EOK;
578 rc = gfx_bitmap_destroy(bitmap);
579 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
580
581 free(test_gc.bitmap_get_alloc_alloc.pixels);
582 xlate_gc_delete(xlategc);
583 gfx_context_delete(tgc);
584}
585
586/** Test getting cursor position in translation GC */
587PCUT_TEST(cursor_get_pos)
588{
589 test_gc_t test_gc;
590 gfx_context_t *tgc;
591 xlate_gc_t *xlategc;
592 gfx_context_t *xgc;
593 gfx_coord2_t off;
594 gfx_coord2_t gpos;
595 errno_t rc;
596
597 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
598 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
599
600 off.x = 10;
601 off.y = 20;
602 rc = xlate_gc_create(&off, tgc, &xlategc);
603 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
604
605 xgc = xlate_gc_get_ctx(xlategc);
606
607 memset(&test_gc, 0, sizeof(test_gc));
608
609 test_gc.cursor_get_pos_pos.x = 13;
610 test_gc.cursor_get_pos_pos.y = 24;
611 test_gc.rc = EOK;
612 rc = gfx_cursor_get_pos(xgc, &gpos);
613 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
614 PCUT_ASSERT_TRUE(test_gc.cursor_get_pos_called);
615
616 /*
617 * Note that translation GC performs inverse translation when getting
618 * cursor coordinates.
619 */
620 PCUT_ASSERT_INT_EQUALS(3, gpos.x);
621 PCUT_ASSERT_INT_EQUALS(4, gpos.y);
622
623 xlate_gc_delete(xlategc);
624 gfx_context_delete(tgc);
625}
626
627/** Test setting cursor position in translation GC */
628PCUT_TEST(cursor_set_pos)
629{
630 test_gc_t test_gc;
631 gfx_context_t *tgc;
632 xlate_gc_t *xlategc;
633 gfx_context_t *xgc;
634 gfx_coord2_t off;
635 gfx_coord2_t pos;
636 errno_t rc;
637
638 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
639 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
640
641 off.x = 10;
642 off.y = 20;
643 rc = xlate_gc_create(&off, tgc, &xlategc);
644 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
645
646 xgc = xlate_gc_get_ctx(xlategc);
647
648 memset(&test_gc, 0, sizeof(test_gc));
649
650 pos.x = 3;
651 pos.y = 4;
652
653 test_gc.rc = EOK;
654 rc = gfx_cursor_set_pos(xgc, &pos);
655 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
656 PCUT_ASSERT_TRUE(test_gc.cursor_set_pos_called);
657 PCUT_ASSERT_INT_EQUALS(13, test_gc.cursor_set_pos_pos.x);
658 PCUT_ASSERT_INT_EQUALS(24, test_gc.cursor_set_pos_pos.y);
659
660 xlate_gc_delete(xlategc);
661 gfx_context_delete(tgc);
662}
663
664/** Test setting cursor visibility in translation GC */
665PCUT_TEST(cursor_set_visible)
666{
667 test_gc_t test_gc;
668 gfx_context_t *tgc;
669 xlate_gc_t *xlategc;
670 gfx_context_t *xgc;
671 gfx_coord2_t off;
672 errno_t rc;
673
674 rc = gfx_context_new(&testgc_ops, &test_gc, &tgc);
675 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
676
677 off.x = 10;
678 off.y = 20;
679 rc = xlate_gc_create(&off, tgc, &xlategc);
680 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
681
682 xgc = xlate_gc_get_ctx(xlategc);
683
684 memset(&test_gc, 0, sizeof(test_gc));
685
686 test_gc.rc = EOK;
687 rc = gfx_cursor_set_visible(xgc, true);
688 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
689 PCUT_ASSERT_TRUE(test_gc.cursor_set_visible_called);
690 PCUT_ASSERT_TRUE(test_gc.cursor_set_visible_vis);
691
692 memset(&test_gc, 0, sizeof(test_gc));
693
694 test_gc.rc = EOK;
695 rc = gfx_cursor_set_visible(xgc, false);
696 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
697 PCUT_ASSERT_TRUE(test_gc.cursor_set_visible_called);
698 PCUT_ASSERT_FALSE(test_gc.cursor_set_visible_vis);
699
700 memset(&test_gc, 0, sizeof(test_gc));
701
702 test_gc.rc = EIO;
703 rc = gfx_cursor_set_visible(xgc, false);
704 PCUT_ASSERT_ERRNO_VAL(EIO, rc);
705 PCUT_ASSERT_TRUE(test_gc.cursor_set_visible_called);
706 PCUT_ASSERT_FALSE(test_gc.cursor_set_visible_vis);
707
708 xlate_gc_delete(xlategc);
709 gfx_context_delete(tgc);
710}
711
712/** Set clipping rectangle in test GC.
713 *
714 * @param arg Argument (test_gc_t *)
715 * @param rect Rectangle
716 * @return EOK on success or an error code
717 */
718static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
719{
720 test_gc_t *test_gc = (test_gc_t *)arg;
721
722 test_gc->set_clip_rect_called = true;
723 test_gc->set_clip_rect_rect = *rect;
724
725 return test_gc->rc;
726}
727
728/** Set drawing color in test GC.
729 *
730 * @param arg Argument (test_gc_t *)
731 * @param color Color
732 * @return EOK on success or an error code
733 */
734static errno_t testgc_set_color(void *arg, gfx_color_t *color)
735{
736 test_gc_t *test_gc = (test_gc_t *)arg;
737
738 test_gc->set_color_called = true;
739 gfx_color_get_rgb_i16(color, &test_gc->set_color_r,
740 &test_gc->set_color_g, &test_gc->set_color_b);
741
742 return test_gc->rc;
743}
744
745/** Fill rectangle in test GC.
746 *
747 * @param arg Argument (test_gc_t *)
748 * @param rect Rectangle
749 * @return EOK on success or an error code
750 */
751static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
752{
753 test_gc_t *test_gc = (test_gc_t *)arg;
754
755 test_gc->fill_rect_called = true;
756 test_gc->fill_rect_rect = *rect;
757
758 return test_gc->rc;
759}
760
761/** Update test GC.
762 *
763 * @param arg Argument (test_gc_t *)
764 * @return EOK on success or an error code
765 */
766static errno_t testgc_update(void *arg)
767{
768 test_gc_t *test_gc = (test_gc_t *)arg;
769
770 test_gc->update_called = true;
771 return test_gc->rc;
772}
773
774/** Create bitmap in test GC.
775 *
776 * @param arg Argument (test_gc_t *)
777 * @param params Bitmap parameters
778 * @param alloc Bitmap allocation info or @c NULL
779 * @param rbitmap Place to store pointer to new bitmap
780 * @return EOK on success or an error code
781 */
782static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
783 gfx_bitmap_alloc_t *alloc, void **rbitmap)
784{
785 test_gc_t *test_gc = (test_gc_t *)arg;
786 test_gc_bitmap_t *tbitmap;
787
788 test_gc->bitmap_create_called = true;
789 if (params != NULL)
790 test_gc->bitmap_create_params = *params;
791 if (alloc != NULL)
792 test_gc->bitmap_create_alloc = *alloc;
793
794 if (test_gc->rc != EOK)
795 return test_gc->rc;
796
797 tbitmap = calloc(1, sizeof(test_gc_bitmap_t));
798 if (tbitmap == NULL)
799 return ENOMEM;
800
801 tbitmap->gc = test_gc;
802 tbitmap->cookie = test_gc->bitmap_create_cookie;
803 *rbitmap = (void *)tbitmap;
804 return EOK;
805}
806
807/** Destroy bitmap in test GC.
808 *
809 * @param bitmap Bitmap
810 * @return EOK on success or an error code
811 */
812static errno_t testgc_bitmap_destroy(void *bitmap)
813{
814 test_gc_bitmap_t *tbitmap = (test_gc_bitmap_t *)bitmap;
815 test_gc_t *test_gc = tbitmap->gc;
816
817 test_gc->bitmap_destroy_called = true;
818 test_gc->bitmap_destroy_cookie = tbitmap->cookie;
819 return test_gc->rc;
820}
821
822/** Render bitmap in test GC.
823 *
824 * @param arg Bitmap
825 * @param srect Source rectangle (or @c NULL)
826 * @param off Offset (or @c NULL)
827 * @return EOK on success or an error code
828 */
829static errno_t testgc_bitmap_render(void *bitmap, gfx_rect_t *srect,
830 gfx_coord2_t *off)
831{
832 test_gc_bitmap_t *tbitmap = (test_gc_bitmap_t *)bitmap;
833 test_gc_t *test_gc = tbitmap->gc;
834
835 test_gc->bitmap_render_called = true;
836 test_gc->bitmap_render_cookie = tbitmap->cookie;
837 if (srect != NULL)
838 test_gc->bitmap_render_srect = *srect;
839 if (off != NULL)
840 test_gc->bitmap_render_off = *off;
841 return test_gc->rc;
842}
843
844/** Get bitmap allocation info in test GC.
845 *
846 * @param bitmap Bitmap
847 * @param alloc Place to store allocation info
848 * @return EOK on success or an error code
849 */
850static errno_t testgc_bitmap_get_alloc(void *bitmap, gfx_bitmap_alloc_t *alloc)
851{
852 test_gc_bitmap_t *tbitmap = (test_gc_bitmap_t *)bitmap;
853 test_gc_t *test_gc = tbitmap->gc;
854
855 test_gc->bitmap_get_alloc_called = true;
856 test_gc->bitmap_get_alloc_cookie = tbitmap->cookie;
857
858 if (test_gc->rc != EOK)
859 return test_gc->rc;
860
861 *alloc = test_gc->bitmap_get_alloc_alloc;
862 return EOK;
863}
864
865/** Get cursor position in test GC.
866 *
867 * @param arg Argument (test_gc_t *)
868 * @param pos Place to store cursor position
869 * @return EOK on success or an error code
870 */
871static errno_t testgc_cursor_get_pos(void *arg, gfx_coord2_t *pos)
872{
873 test_gc_t *test_gc = (test_gc_t *)arg;
874
875 test_gc->cursor_get_pos_called = true;
876
877 if (test_gc->rc != EOK)
878 return test_gc->rc;
879
880 *pos = test_gc->cursor_get_pos_pos;
881 return EOK;
882}
883
884/** Set cursor position in test GC.
885 *
886 * @param arg Argument (test_gc_t *)
887 * @param pos Cursor position
888 * @return EOK on success or an error code
889 */
890static errno_t testgc_cursor_set_pos(void *arg, gfx_coord2_t *pos)
891{
892 test_gc_t *test_gc = (test_gc_t *)arg;
893
894 test_gc->cursor_set_pos_called = true;
895 test_gc->cursor_set_pos_pos = *pos;
896
897 return test_gc->rc;
898}
899
900/** Set cursor visibility in test GC.
901 *
902 * @param arg Argument (test_gc_t *)
903 * @param visible @c true iff cursor should be visible
904 * @return EOK on success or an error code
905 */
906static errno_t testgc_cursor_set_visible(void *arg, bool visible)
907{
908 test_gc_t *test_gc = (test_gc_t *)arg;
909
910 test_gc->cursor_set_visible_called = true;
911 test_gc->cursor_set_visible_vis = visible;
912
913 return test_gc->rc;
914}
915
916PCUT_EXPORT(xlategc);
Note: See TracBrowser for help on using the repository browser.