forked from lc-soft/LCUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
683 lines (630 loc) · 16.5 KB
/
display.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/* ***************************************************************************
* display.c -- graphical display control
*
* Copyright (C) 2012-2017 by Liu Chao <[email protected]>
*
* This file is part of the LCUI project, and may only be used, modified, and
* distributed under the terms of the GPLv2.
*
* (GPLv2 is abbreviation of GNU General Public License Version 2)
*
* By continuing to use, modify, or distribute this file you indicate that you
* have read the license and understand and accept it fully.
*
* The LCUI project is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for more details.
*
* You should have received a copy of the GPLv2 along with this file. It is
* usually in the LICENSE.TXT file, If not, see <http://www.gnu.org/licenses/>.
* ****************************************************************************/
/* ****************************************************************************
* display.c -- 图形显示控制
*
* 版权所有 (C) 2012-2017 归属于 刘超 <[email protected]>
*
* 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。
*
* (GPLv2 是 GNU通用公共许可证第二版 的英文缩写)
*
* 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。
*
* LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特
* 定用途的隐含担保,详情请参照GPLv2许可协议。
*
* 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果
* 没有,请查看:<http://www.gnu.org/licenses/>.
* ****************************************************************************/
//#define DEBUG
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#include <LCUI/input.h>
#include <LCUI/timer.h>
#include <LCUI/cursor.h>
#include <LCUI/thread.h>
#include <LCUI/display.h>
#include <LCUI/platform.h>
#include LCUI_DISPLAY_H
#define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 600
/** surface 记录 */
typedef struct SurfaceRecordRec_ {
LCUI_BOOL rendered; /**< 是否已渲染了新内容 */
LinkedList rects; /**< 需重绘的区域列表 */
LCUI_Surface surface; /**< surface */
LCUI_Widget widget; /**< surface 所映射的 widget */
} SurfaceRecordRec, *SurfaceRecord;
/** 图形显示功能的上下文数据 */
static struct DisplayContext {
int mode; /**< 显示模式 */
size_t width, height; /**< 当前缓存的屏幕尺寸 */
LCUI_BOOL show_rect_border; /**< 是否为重绘的区域显示边框 */
LCUI_BOOL is_working; /**< 标志,指示当前模块是否处于工作状态 */
LCUI_Thread thread; /**< 线程,负责画面更新工作 */
LinkedList surfaces; /**< surface 列表 */
LinkedList rects; /**< 无效区域列表 */
LCUI_DisplayDriver driver;
} display;
#define LCUIDisplay_CleanSurfaces() \
LinkedList_Clear( &display.surfaces, OnDestroySurfaceRecord )
static void OnDestroySurfaceRecord( void *data )
{
SurfaceRecord record = data;
Surface_Close( record->surface );
free( record );
}
static void DrawBorder( LCUI_PaintContext paint )
{
LCUI_Pos pos;
LCUI_Color color;
int end_x = paint->rect.width - 1;
int end_y = paint->rect.height - 1;
pos.x = pos.y = 0;
color = RGB( 255, 0, 0 );
Graph_DrawHorizLine( &paint->canvas, color, 1, pos, end_x );
Graph_DrawVertiLine( &paint->canvas, color, 1, pos, end_y );
pos.x = paint->rect.width - 1;
Graph_DrawVertiLine( &paint->canvas, color, 1, pos, end_y );
pos.x = 0;
pos.y = paint->rect.height - 1;
Graph_DrawHorizLine( &paint->canvas, color, 1, pos, end_x );
}
void LCUIDisplay_Update( void )
{
LCUI_Surface surface;
LinkedListNode *node;
SurfaceRecord record = NULL;
if( !display.is_working ) {
return;
}
LCUICursor_Update();
LCUIWidget_Update();
/* 遍历当前的 surface 记录列表 */
for( LinkedList_Each( node, &display.surfaces ) ) {
record = node->data;
surface = record->surface;
if( !record->widget || !surface ||
!Surface_IsReady( surface ) ) {
continue;
}
Surface_Update( surface );
/* 收集无效区域记录 */
Widget_ProcInvalidArea( record->widget, &record->rects );
}
if( display.mode == LCDM_SEAMLESS || !record ) {
return;
}
LinkedList_Concat( &record->rects, &display.rects );
}
void LCUIDisplay_Render( void )
{
LinkedListNode *sn, *rn;
LCUI_PaintContext paint;
if( !display.is_working ) {
return;
}
/* 遍历当前的 surface 记录列表 */
for( LinkedList_Each( sn, &display.surfaces ) ) {
SurfaceRecord record = sn->data;
LCUI_Surface surface = record->surface;
if( !record->widget || !surface ||
!Surface_IsReady( surface ) ) {
continue;
}
record->rendered = FALSE;
/* 在 surface 上逐个重绘无效区域 */
for( LinkedList_Each( rn, &record->rects ) ) {
paint = Surface_BeginPaint( surface, rn->data );
if( !paint ) {
continue;
}
DEBUG_MSG( "[%s]: render rect: (%d,%d,%d,%d)\n",
record->widget->type,
paint->rect.x, paint->rect.y,
paint->rect.width, paint->rect.height );
Widget_Render( record->widget, paint );
if( display.show_rect_border ) {
DrawBorder( paint );
}
Surface_EndPaint( surface, paint );
record->rendered = TRUE;
}
RectList_Clear( &record->rects );
}
}
void LCUIDisplay_Present( void )
{
LinkedListNode *sn;
if( !display.is_working ) {
return;
}
for( LinkedList_Each( sn, &display.surfaces ) ) {
SurfaceRecord record = sn->data;
LCUI_Surface surface = record->surface;
if( !surface || !Surface_IsReady( surface ) ) {
continue;
}
if( record->rendered ) {
Surface_Present( surface );
}
}
}
void LCUIDisplay_InvalidateArea( LCUI_Rect *rect )
{
LCUI_Rect screen;
if( !display.is_working ) {
return;
}
if( !rect ) {
screen.x = 0;
screen.y = 0;
screen.width = LCUIDisplay_GetWidth();
screen.height = LCUIDisplay_GetHeight();
rect = &screen;
}
RectList_Add( &display.rects, rect );
}
static LCUI_Widget LCUIDisplay_GetBindWidget( LCUI_Surface surface )
{
LinkedListNode *node;
for( LinkedList_Each( node, &display.surfaces ) ) {
SurfaceRecord record = node->data;
if( record && record->surface == surface ) {
return record->widget;
}
}
return NULL;
}
static LCUI_Surface LCUIDisplay_GetBindSurface( LCUI_Widget widget )
{
LinkedListNode *node;
for( LinkedList_Each( node, &display.surfaces ) ) {
SurfaceRecord record = node->data;
if( record && record->widget == widget ) {
return record->surface;
}
}
return NULL;
}
LCUI_Surface LCUIDisplay_GetSurfaceOwner( LCUI_Widget w )
{
if( LCUIDisplay_GetMode() == LCDM_SEAMLESS ) {
while( w->parent ) {
w = w->parent;
}
} else {
w = LCUIWidget_GetRoot();
}
return LCUIDisplay_GetBindSurface( w );
}
LCUI_Surface LCUIDisplay_GetSurfaceByHandle( void *handle )
{
LinkedListNode *node;
for( LinkedList_Each( node, &display.surfaces ) ) {
SurfaceRecord record = node->data;
if( Surface_GetHandle( record->surface ) == handle ) {
return record->surface;
}
}
return NULL;
}
/** 将 widget 与 sruface 进行绑定 */
static void LCUIDisplay_BindSurface( LCUI_Widget widget )
{
LCUI_RectF *rect;
SurfaceRecord record;
int width, height;
if( LCUIDisplay_GetBindSurface( widget ) ) {
return;
}
rect = &widget->box.graph;
record = NEW( SurfaceRecordRec, 1 );
record->surface = Surface_New();
record->widget = widget;
record->rendered = FALSE;
LinkedList_Init( &record->rects );
Surface_SetCaptionW( record->surface, widget->title );
if( widget->style->sheet[key_top].is_valid &&
widget->style->sheet[key_left].is_valid ) {
Surface_Move( record->surface, roundi( rect->x ),
roundi( rect->y ) );
}
width = roundi( rect->width );
height = roundi( rect->height );
Surface_Resize( record->surface, width, height );
if( widget->computed_style.visible ) {
Surface_Show( record->surface );
} else {
Surface_Hide( record->surface );
}
Widget_InvalidateArea( widget, NULL, SV_GRAPH_BOX );
LinkedList_Append( &display.surfaces, record );
}
/** 解除 widget 与 sruface 的绑定 */
static void LCUIDisplay_UnbindSurface( LCUI_Widget widget )
{
LinkedListNode *node;
for( LinkedList_Each( node, &display.surfaces ) ) {
SurfaceRecord record = node->data;
if( record && record->widget == widget ) {
Surface_Close( record->surface );
LinkedList_DeleteNode( &display.surfaces, node );
break;
}
}
}
static int LCUIDisplay_Windowed( void )
{
LCUI_Widget root = LCUIWidget_GetRoot();
switch( display.mode ) {
case LCDM_WINDOWED:
return 0;
case LCDM_FULLSCREEN:
LCUIDisplay_GetBindSurface( root );
break;
case LCDM_SEAMLESS:
default:
LCUIDisplay_CleanSurfaces();
LCUIDisplay_BindSurface( root );
break;
}
LCUIDisplay_SetSize( display.width, display.height );
display.mode = LCDM_WINDOWED;
return 0;
}
static int LCUIDisplay_FullScreen( void )
{
LCUI_Widget root = LCUIWidget_GetRoot();
switch( display.mode ) {
case LCDM_SEAMLESS:
LCUIDisplay_CleanSurfaces();
LCUIDisplay_BindSurface(root );
case LCDM_WINDOWED:
default: break;
case LCDM_FULLSCREEN:
return 0;
}
display.mode = LCDM_FULLSCREEN;
display.width = LCUIDisplay_GetWidth();
display.height = LCUIDisplay_GetHeight();
LCUIDisplay_SetSize( display.width, display.height );
return 0;
}
static int LCUIDisplay_Seamless( void )
{
LinkedListNode *node;
LCUI_Widget root = LCUIWidget_GetRoot();
DEBUG_MSG("display.mode: %d\n", display.mode);
switch( display.mode ) {
case LCDM_SEAMLESS:
return 0;
case LCDM_FULLSCREEN:
case LCDM_WINDOWED:
default:
LCUIDisplay_CleanSurfaces();
break;
}
for( LinkedList_Each( node, &root->children ) ) {
LCUIDisplay_BindSurface( node->data );
}
display.mode = LCDM_SEAMLESS;
return 0;
}
/* 设置呈现模式 */
int LCUIDisplay_SetMode( int mode )
{
int ret;
DEBUG_MSG("mode: %d\n", mode);
switch( mode ) {
case LCDM_WINDOWED:
ret = LCUIDisplay_Windowed();
break;
case LCDM_SEAMLESS:
ret = LCUIDisplay_Seamless();
break;
case LCDM_FULLSCREEN:
default:
ret = LCUIDisplay_FullScreen();
break;
}
return ret;
}
/* 获取呈现模式 */
int LCUIDisplay_GetMode(void)
{
return display.mode;
}
void LCUIDisplay_ShowRectBorder(void)
{
display.show_rect_border = TRUE;
}
void LCUIDisplay_HideRectBorder( void )
{
display.show_rect_border = FALSE;
}
/** 设置显示区域的尺寸,仅在窗口化、全屏模式下有效 */
void LCUIDisplay_SetSize( int width, int height )
{
LCUI_Widget root;
LCUI_Surface surface;
if( display.mode == LCDM_SEAMLESS ) {
return;
}
root = LCUIWidget_GetRoot();
surface = LCUIDisplay_GetBindSurface( root );
Surface_Resize( surface, width, height );
Widget_Resize( root, (float)width, (float)height );
}
int LCUIDisplay_GetWidth( void )
{
if( !display.is_working ) {
return 0;
}
if( display.mode == LCDM_WINDOWED ||
display.mode == LCDM_FULLSCREEN ) {
return roundi( LCUIWidget_GetRoot()->width );
}
return display.driver->getWidth();
}
int LCUIDisplay_GetHeight( void )
{
if( !display.is_working ) {
return 0;
}
if( display.mode == LCDM_WINDOWED ||
display.mode == LCDM_FULLSCREEN ) {
return roundi( LCUIWidget_GetRoot()->height );
}
return display.driver->getHeight();
}
void Surface_Close( LCUI_Surface surface )
{
if( display.is_working ) {
display.driver->close( surface );
}
}
void Surface_Destroy( LCUI_Surface surface )
{
if( display.is_working ) {
display.driver->destroy( surface );
}
}
LCUI_Surface Surface_New( void )
{
if( display.is_working ) {
return display.driver->create();
}
return NULL;
}
LCUI_BOOL Surface_IsReady( LCUI_Surface surface )
{
if( display.is_working ) {
return display.driver->isReady( surface );
}
return TRUE;
}
void Surface_Move( LCUI_Surface surface, int x, int y )
{
if( display.is_working ) {
display.driver->move( surface, x, y );
}
}
void Surface_Resize( LCUI_Surface surface, int w, int h )
{
if( display.is_working ) {
LCUI_Rect rect;
display.driver->resize( surface, w, h );
rect.x = rect.y = 0;
rect.width = w;
rect.height = h;
LCUIDisplay_InvalidateArea( &rect );
}
}
void Surface_SetCaptionW( LCUI_Surface surface, const wchar_t *str )
{
if( display.is_working ) {
display.driver->setCaptionW( surface, str );
}
}
void Surface_Show( LCUI_Surface surface )
{
if( display.is_working ) {
display.driver->show( surface );
}
}
void Surface_Hide( LCUI_Surface surface )
{
if( display.is_working ) {
display.driver->hide( surface );
}
}
void *Surface_GetHandle( LCUI_Surface surface )
{
if( display.is_working ) {
return display.driver->getHandle( surface );
}
return NULL;
}
void Surface_SetRenderMode( LCUI_Surface surface, int mode )
{
if( display.is_working ) {
display.driver->setRenderMode( surface, mode );
}
}
void Surface_Update( LCUI_Surface surface )
{
if( display.is_working ) {
display.driver->update( surface );
}
}
LCUI_PaintContext Surface_BeginPaint( LCUI_Surface surface, LCUI_Rect *rect )
{
if( display.is_working ) {
return display.driver->beginPaint( surface, rect );
}
return NULL;
}
void Surface_EndPaint( LCUI_Surface surface, LCUI_PaintContext paint_ctx )
{
if( display.is_working ) {
display.driver->endPaint( surface, paint_ctx );
}
}
void Surface_Present( LCUI_Surface surface )
{
if( display.is_working ) {
display.driver->present( surface );
}
}
/** 响应顶级部件的各种事件 */
static void OnSurfaceEvent( LCUI_Widget w, LCUI_WidgetEvent e, void *arg )
{
int e_type;
LCUI_Widget root;
LCUI_RectF *rect;
LCUI_Surface surface;
e_type = *((int*)arg);
root = LCUIWidget_GetRoot();
surface = LCUIDisplay_GetBindSurface( e->target );
if( display.mode == LCDM_SEAMLESS ) {
if( !surface && e_type != WET_ADD ) {
return;
}
} else if ( e->target == root ) {
if( !surface && e_type != WET_ADD ) {
return;
}
} else {
return;
}
rect = &e->target->box.graph;
switch( e_type ) {
case WET_ADD:
LCUIDisplay_BindSurface( e->target );
break;
case WET_REMOVE:
case WET_DESTROY:
LCUIDisplay_UnbindSurface( e->target );
break;
case WET_SHOW:
Surface_Show( surface );
break;
case WET_HIDE:
Surface_Hide( surface );
break;
case WET_RESIZE:
{
LCUI_Rect area;
area.x = area.y = 0;
area.width = roundi( rect->width );
area.height = roundi( rect->height );
LCUIDisplay_InvalidateArea( &area );
break;
}
case WET_TITLE:
DEBUG_MSG("%S\n", e->target->title );
Surface_SetCaptionW( surface, e->target->title );
break;
default: break;
}
}
/** 在 surface 主动产生无效区域并需要绘制的时候 */
static void OnPaint( LCUI_Event e, void *arg )
{
LinkedListNode *node;
LCUI_DisplayEvent dpy_ev = arg;
for( LinkedList_Each( node, &display.surfaces ) ) {
SurfaceRecord record = node->data;
if( record && record->surface != dpy_ev->surface ) {
continue;
}
Widget_InvalidateArea( record->widget,
&dpy_ev->paint.rect, SV_GRAPH_BOX );
}
}
static void OnResize( LCUI_Event e, void *arg )
{
LCUI_Widget widget;
LCUI_DisplayEvent dpy_ev = arg;
float width = (float)( dpy_ev->resize.width );
float height = (float)( dpy_ev->resize.height );
LOG( "[display] resize: (%.2f,%.2f)\n", width, height );
widget = LCUIDisplay_GetBindWidget( dpy_ev->surface );
if( widget ) {
Widget_Resize( widget, width, height );
}
}
int LCUIDisplay_BindEvent( int event_id,
LCUI_EventFunc func, void *arg,
void *data, void( *destroy_data )(void*) )
{
if( display.is_working ) {
return display.driver->bindEvent( event_id, func,
data, destroy_data );
}
return -1;
}
int LCUI_InitDisplay( LCUI_DisplayDriver driver )
{
LCUI_Widget root;
if( display.is_working ) {
return -1;
}
LOG( "[display] init ...\n" );
display.mode = 0;
root = LCUIWidget_GetRoot();
LinkedList_Init( &display.rects );
LinkedList_Init( &display.surfaces );
if( !driver ) {
driver = LCUI_CreateDisplayDriver();
if( !driver ) {
LOG( "[display] init failed\n" );
return -2;
}
}
display.driver = driver;
display.is_working = TRUE;
display.width = DEFAULT_WIDTH;
display.height = DEFAULT_HEIGHT;
display.driver->bindEvent( DET_RESIZE, OnResize, NULL, NULL );
display.driver->bindEvent( DET_PAINT, OnPaint, NULL, NULL );
Widget_BindEvent( root, "surface", OnSurfaceEvent, NULL, NULL );
LCUIDisplay_SetMode( LCDM_DEFAULT );
LCUIDisplay_Update();
LOG( "[display] init ok, driver name: %s\n", display.driver->name );
return 0;
}
/** 停用图形输出模块 */
int LCUI_ExitDisplay( void )
{
if( !display.is_working ) {
return -1;
}
display.is_working = FALSE;
RectList_Clear( &display.rects );
LCUIDisplay_CleanSurfaces();
return 0;
}