/
Drawer2.c
executable file
·1919 lines (1552 loc) · 63.8 KB
/
Drawer2.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Copyright © 2016, 2018 Boubacar DIENE //
// //
// This file is part of mmstreamer project. //
// //
// mmstreamer is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 2 of the License, or //
// (at your option) any later version. //
// //
// mmstreamer 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 //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with mmstreamer. If not, see <http://www.gnu.org/licenses/> //
// or write to the Free Software Foundation, Inc., 51 Franklin Street, //
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. //
// //
//////////////////////////////////////////////////////////////////////////////////////////////////
/*!
* \file Drawer2.c
* \brief Graphics elements drawer based on SDLv2
* \author Boubacar DIENE
*/
/* -------------------------------------------------------------------------------------------- */
/* ////////////////////////////////////////// HEADERS ///////////////////////////////////////// */
/* -------------------------------------------------------------------------------------------- */
#include <fcntl.h>
#include <sys/types.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_events.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h"
#include "utils/Log.h"
#include "graphics/Drawer.h"
/* -------------------------------------------------------------------------------------------- */
/* ////////////////////////////////////////// MACROS ////////////////////////////////////////// */
/* -------------------------------------------------------------------------------------------- */
#undef TAG
#define TAG "Drawer"
#define SCREENSHOT_ADJUST_VIDEO_X 60
#define SCREENSHOT_ADJUST_VIDEO_Y 25
/* -------------------------------------------------------------------------------------------- */
/* ////////////////////////////////////////// TYPES /////////////////////////////////////////// */
/* -------------------------------------------------------------------------------------------- */
struct drawer_video_s {
struct gfx_video_s params;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *surface;
SDL_Texture *texture;
SDL_Texture *initialBgTexture;
SDL_RWops *rwops;
uint32_t windowID;
uint32_t pixelFormat;
};
struct drawer_image_s {
SDL_Surface *surface;
SDL_Texture *texture;
};
struct drawer_text_s {
SDL_Surface *surface;
SDL_Texture *texture;
SDL_Color color;
TTF_Font *font;
};
struct drawer_screen_s {
struct gfx_screen_s params;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *surface;
SDL_Texture *texture;
SDL_Texture *initialBgTexture;
uint32_t windowID;
volatile uint8_t update;
};
struct drawer_private_data_s {
struct drawer_screen_s screen;
struct drawer_video_s video;
struct drawer_image_s image;
struct drawer_text_s text;
SDL_Rect rect;
SDL_Event event;
SDL_mutex *lock;
};
/* -------------------------------------------------------------------------------------------- */
/* PUBLIC FUNCTIONS PROTOTYPES */
/* -------------------------------------------------------------------------------------------- */
static enum drawer_error_e initScreen_f(struct drawer_s *obj, struct gfx_screen_s *screenParams);
static enum drawer_error_e uninitScreen_f(struct drawer_s *obj);
static enum drawer_error_e drawVideo_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct buffer_s *buffer);
static enum drawer_error_e drawImage_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_image_s *image, enum gfx_target_e target);
static enum drawer_error_e drawText_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_text_s *text, enum gfx_target_e target);
static enum drawer_error_e setBgColor_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_color_s *color, enum gfx_target_e target);
static enum drawer_error_e restoreBgColor_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_color_s *fallbackColor,
enum gfx_target_e target);
static enum drawer_error_e startDrawingInBg_f(struct drawer_s *obj);
static enum drawer_error_e stopDrawingInBg_f(struct drawer_s *obj);
static enum drawer_error_e saveBuffer_f(struct drawer_s *obj, struct buffer_s *buffer,
struct gfx_image_s *inOut);
static enum drawer_error_e saveTarget_f(struct drawer_s *obj, struct gfx_image_s *inOut,
struct gfx_rect_s *srcRect, enum gfx_target_e target);
static enum drawer_error_e getEvent_f(struct drawer_s *obj, struct gfx_event_s *gfxEvent);
static enum drawer_error_e stopAwaitingEvent_f(struct drawer_s *obj);
/* -------------------------------------------------------------------------------------------- */
/* /////////////////////////////// PRIVATE FUNCTIONS PROTOTYPES /////////////////////////////// */
/* -------------------------------------------------------------------------------------------- */
static int sdlEventFilter_f(void *userdata, SDL_Event* event);
static enum drawer_error_e convertSdlEvent_f(struct drawer_s *obj, SDL_Event *event,
struct gfx_event_s *out);
static enum drawer_error_e initSdlComponents_f(struct drawer_s *obj, int32_t imgFlags);
static enum drawer_error_e uninitSdlComponents_f(struct drawer_s *obj);
static enum drawer_error_e initWindowAndRenderer_f(struct drawer_s *obj, enum gfx_target_e target,
struct gfx_rect_s *pos, uint8_t showWindow);
static enum drawer_error_e uninitWindowAndRenderer_f(struct drawer_s *obj,
enum gfx_target_e target);
static enum drawer_error_e initVideoContext_f(struct drawer_s *obj, struct gfx_rect_s *rect,
uint8_t showWindow);
static enum drawer_error_e uninitVideoContext_f(struct drawer_s *obj);
static enum drawer_error_e setInitialBackground_f(struct drawer_s *obj, enum gfx_target_e target);
static enum drawer_error_e setWindowIcon_f(struct drawer_s *obj, SDL_Window *window,
struct gfx_image_s *icon);
static enum drawer_error_e waitForWindowEvent_f(struct drawer_s *obj, enum gfx_target_e target,
SDL_WindowEventID eventID, SDL_Event *inOut);
static enum drawer_error_e setTextureTransparency_f(struct drawer_s *obj, SDL_Texture *texture,
uint8_t alpha);
static enum drawer_error_e renderTexture_f(struct drawer_s *obj, enum gfx_target_e target,
SDL_Texture *texture, SDL_Rect *srcRect,
SDL_Rect *dstRect);
static enum drawer_error_e createSurfaceFromTarget_f(struct drawer_s *obj, enum gfx_target_e target,
SDL_Surface **surface, SDL_Rect *rect);
static enum drawer_error_e writeSurfaceToFile_f(struct drawer_s *obj, SDL_Surface *surface,
struct gfx_image_s *inOut);
static enum drawer_error_e adjustDrawingRect_f(struct drawer_s *obj, enum gfx_target_e target,
SDL_Rect *inOut);
/* -------------------------------------------------------------------------------------------- */
/* /////////////////////////////////////// INITIALIZER //////////////////////////////////////// */
/* -------------------------------------------------------------------------------------------- */
/*!
*
*/
enum drawer_error_e Drawer_Init(struct drawer_s **obj)
{
ASSERT(obj && (*obj = calloc(1, sizeof(struct drawer_s))));
struct drawer_private_data_s *pData;
ASSERT((pData = calloc(1, sizeof(struct drawer_private_data_s))));
(*obj)->initScreen = initScreen_f;
(*obj)->uninitScreen = uninitScreen_f;
(*obj)->drawVideo = drawVideo_f;
(*obj)->drawImage = drawImage_f;
(*obj)->drawText = drawText_f;
(*obj)->setBgColor = setBgColor_f;
(*obj)->restoreBgColor = restoreBgColor_f;
(*obj)->startDrawingInBg = startDrawingInBg_f;
(*obj)->stopDrawingInBg = stopDrawingInBg_f;
(*obj)->saveBuffer = saveBuffer_f;
(*obj)->saveTarget = saveTarget_f;
(*obj)->getEvent = getEvent_f;
(*obj)->stopAwaitingEvent = stopAwaitingEvent_f;
ASSERT((pData->lock = SDL_CreateMutex()));
(*obj)->pData = (void*)pData;
return DRAWER_ERROR_NONE;
}
/*!
*
*/
enum drawer_error_e Drawer_UnInit(struct drawer_s **obj)
{
ASSERT(obj && *obj && (*obj)->pData);
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)((*obj)->pData);
SDL_DestroyMutex(pData->lock);
free(pData);
free(*obj);
*obj = NULL;
return DRAWER_ERROR_NONE;
}
/* -------------------------------------------------------------------------------------------- */
/* ////////////////////////////// PUBLIC FUNCTIONS IMPLEMENTATION ///////////////////////////// */
/* -------------------------------------------------------------------------------------------- */
/*!
*
*/
static enum drawer_error_e initScreen_f(struct drawer_s *obj, struct gfx_screen_s *screenParams)
{
ASSERT(obj && obj->pData && screenParams);
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)(obj->pData);
enum drawer_error_e ret = DRAWER_ERROR_INIT;
pData->screen.params = *screenParams;
pData->video.params = screenParams->video;
ret = initSdlComponents_f(obj, IMG_INIT_JPG|IMG_INIT_PNG);
if (ret != DRAWER_ERROR_NONE) {
Loge("Failed to init SDL components");
goto initSdlComponentsExit;
}
ret = initWindowAndRenderer_f(obj, GFX_TARGET_SCREEN, &pData->screen.params.rect, 1);
if (ret != DRAWER_ERROR_NONE) {
Loge("Failed to init screen window and renderer");
goto initScreenWindowAndRendererExit;
}
ret = setInitialBackground_f(obj, GFX_TARGET_SCREEN);
if (ret != DRAWER_ERROR_NONE) {
Loge("Failed to set screen's background");
goto initScreenWindowAndRendererExit;
}
ret = initVideoContext_f(obj, &pData->video.params.rect, 1);
if (ret != DRAWER_ERROR_NONE) {
Loge("Failed to init video context (window, renderer, texture, ...)");
goto initVideoContextExit;
}
ret = setInitialBackground_f(obj, GFX_TARGET_VIDEO);
if (ret != DRAWER_ERROR_NONE) {
Loge("Failed to set video area's background");
goto initVideoContextExit;
}
// Filter out some events before they are posted to the SDL's internal
// event queue
SDL_SetEventFilter(sdlEventFilter_f, obj);
return DRAWER_ERROR_NONE;
initVideoContextExit:
(void)uninitVideoContext_f(obj);
initScreenWindowAndRendererExit:
(void)uninitWindowAndRenderer_f(obj, GFX_TARGET_SCREEN);
initSdlComponentsExit:
(void)uninitSdlComponents_f(obj);
return ret;
}
/*!
*
*/
static enum drawer_error_e uninitScreen_f(struct drawer_s *obj)
{
ASSERT(obj && obj->pData);
(void)uninitVideoContext_f(obj);
(void)uninitWindowAndRenderer_f(obj, GFX_TARGET_SCREEN);
(void)uninitSdlComponents_f(obj);
return DRAWER_ERROR_NONE;
}
/*!
*
*/
static enum drawer_error_e drawVideo_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct buffer_s *buffer)
{
ASSERT(obj && obj->pData);
if (!rect || !buffer || !buffer->data) {
//Loge("No valid video buffer received yet");
return DRAWER_ERROR_PARAMS;
}
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)(obj->pData);
enum drawer_error_e ret = DRAWER_ERROR_DRAW;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
SDL_Rect newRect = {rect->x, rect->y, (int32_t)rect->w, (int32_t)rect->h};
(void)adjustDrawingRect_f(obj, GFX_TARGET_VIDEO, &newRect);
// MJPEG format
if (pData->video.pixelFormat == SDL_PIXELFORMAT_ARGB8888) {
pData->video.rwops = SDL_RWFromMem(buffer->data, (int32_t)buffer->length);
pData->video.surface = IMG_Load_RW(pData->video.rwops, 0);
pData->video.texture = SDL_CreateTextureFromSurface(pData->video.renderer,
pData->video.surface);
SDL_FreeSurface(pData->video.surface);
SDL_RWclose(pData->video.rwops);
if (!pData->video.texture) {
// This can occur if
// - video.pixelformat does not match video streaming format
// (E.g. Graphics.xml#pixelformat="0" && Videos.xml#configChoice="2"
// i.e ARGB8888 vs YVYU)
// - Buffer is received from "Clients" module which sometimes could provide
// incomplete data (To be fixed!)
//Loge("Failed to create video texture - %s", SDL_GetError());
goto unlockMutexExit;
}
ret = renderTexture_f(obj, GFX_TARGET_VIDEO, pData->video.texture, NULL, &newRect);
SDL_DestroyTexture(pData->video.texture);
goto unlockMutexExit;
}
// YUV format
//
// Lots of people use SDL_UpdateTexture() to update texture with received data but this
// method has a big drawback :
// "This is a fairly slow function, intended for use with static textures that do
// not change often" --> Cf. SDL official documentation
//
// Another reason to not use it is that the pitch (i.e the number of bytes per line) is
// one of its expected argument. Here are the two common ways of calculating pitch :
// - pitch = buffer->length / newRect.h
// - pitch = SDL_BYTESPERPIXEL(pData->video.pixelFormat) * newRect.w
//
// Using these formula could lead to serious issues
// E.g. Segmentation fault when for example trying to SDL_UpdatTexture() from a 640 x 480
// video buffer to a 320 x 240 streaming texture
//
// For YUV format, it's indeed a bit risky to calculate pitch in such ways due to internal
// operations made by SDL when creating a streaming texture :
//
// if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
// size_t size;
// data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
// size = texture->h * data->pitch;
// if (data->yuv) {
// /* Need to add size for the U and V planes */
// size += (2 * (texture->h * data->pitch) / 4);
// }
// if (data->nv12) {
// /* Need to add size for the U/V plane */
// size += ((texture->h * data->pitch) / 2);
// }
// data->pixel_data = SDL_calloc(1, size);
// if (!data->pixel_data) {
// SDL_free(data);
// return SDL_OutOfMemory();
// }
// }
//
// As you can see, size is calculated differently for YUV and NV12 formats
// => A more reliable solution must be used as for example SDL_LockTexture() which returns
// pitch value and pixels in write-only mode so received data could be simply copied using
// memcpy() function without taking care of pitch.
void *pixels = NULL;
int pitch = 0;
// --> Lock texture to retrieve pixels and pitch
if (SDL_LockTexture(pData->video.texture, NULL, &pixels, &pitch) != 0) {
Loge("SDL_LockTexture() failed - %s", SDL_GetError());
goto unlockMutexExit;
}
// --> Copy buffer to texture's pixels
memcpy(pixels, buffer->data, buffer->length);
// --> Unlock texture to apply changes
SDL_UnlockTexture(pData->video.texture);
// --> Now, render texture to screen
ret = renderTexture_f(obj, GFX_TARGET_VIDEO, pData->video.texture, NULL, &newRect);
unlockMutexExit:
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e drawImage_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_image_s *image, enum gfx_target_e target)
{
ASSERT(obj && obj->pData);
if (!rect || !image) {
Loge("Bad arguments");
return DRAWER_ERROR_PARAMS;
}
if (access(image->path, F_OK) != 0) {
Loge("File \"%s\" not found", image->path);
return DRAWER_ERROR_DRAW;
}
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)(obj->pData);
enum drawer_error_e ret = DRAWER_ERROR_DRAW;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
// Get renderer
SDL_Renderer *renderer = (target == GFX_TARGET_VIDEO ? pData->video.renderer
: pData->screen.renderer);
if (!renderer) {
Loge("Renderer not initialized yet for target %d", target);
goto exit;
}
// Create image texture
int32_t width = -1;
int32_t height = -1;
if (image->hiddenColor) {
pData->image.surface = IMG_Load(image->path);
if (!pData->image.surface) {
Loge("Failed to load image \"%s\"", image->path);
goto exit;
}
SDL_SetColorKey(pData->image.surface,
SDL_TRUE,
SDL_MapRGBA(pData->image.surface->format,
image->hiddenColor->red,
image->hiddenColor->green,
image->hiddenColor->blue,
image->hiddenColor->alpha
)
);
width = pData->image.surface->w;
height = pData->image.surface->h;
pData->image.texture = SDL_CreateTextureFromSurface(renderer, pData->image.surface);
SDL_FreeSurface(pData->image.surface); // Surface not needed anymore
}
else {
pData->image.texture = IMG_LoadTexture(renderer, image->path);
}
// Set dstRect
if (!pData->image.texture) {
Loge("Failed to load image \"%s\"", image->path);
goto exit;
}
if ((width < 0) || (height < 0)) {
SDL_QueryTexture(pData->image.texture, NULL, NULL, &width, &height);
}
Logd("Real size of image \"%s\": %d x %d", image->path, width, height);
if ((width < (int32_t)rect->w) && (height < (int32_t)rect->h)) {
Logd("Centering image \"%s\" inside element", image->path);
pData->rect.x = rect->x + (((int32_t)rect->w - width) / 2);
pData->rect.y = rect->y + (((int32_t)rect->h - height) / 2);
pData->rect.w = width;
pData->rect.h = height;
}
else {
pData->rect.x = rect->x;
pData->rect.y = rect->y;
pData->rect.w = (int32_t)rect->w;
pData->rect.h = (int32_t)rect->h;
}
(void)adjustDrawingRect_f(obj, target, &pData->rect);
// Render to screen
ret = renderTexture_f(obj, target, pData->image.texture, NULL, &pData->rect);
// Destroy texture
SDL_DestroyTexture(pData->image.texture);
exit:
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e drawText_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_text_s *text, enum gfx_target_e target)
{
ASSERT(obj && obj->pData);
if (!rect || !text) {
Loge("Bad arguments");
return DRAWER_ERROR_PARAMS;
}
if (access(text->ttfFont, F_OK) != 0) {
Loge("Font file \"%s\" not found", text->ttfFont);
return DRAWER_ERROR_PARAMS;
}
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)(obj->pData);
enum drawer_error_e ret = DRAWER_ERROR_DRAW;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
// Get renderer
SDL_Renderer *renderer = (target == GFX_TARGET_VIDEO ? pData->video.renderer
: pData->screen.renderer);
if (!renderer) {
Loge("Renderer not initialized yet for target %d", target);
goto exit;
}
// Create texture from text
pData->text.font = TTF_OpenFont(text->ttfFont, text->ttfFontSize);
if (!pData->text.font) {
Loge("Failed to open font file \"%s\" - %s", text->ttfFont, TTF_GetError());
goto exit;
}
pData->text.color.r = text->color.red;
pData->text.color.g = text->color.green;
pData->text.color.b = text->color.blue;
pData->text.color.a = text->color.alpha;
pData->text.surface = TTF_RenderText_Solid(pData->text.font, text->str, pData->text.color);
if (!pData->text.surface) {
Loge("Failed to render text - %s", TTF_GetError());
goto exit;
}
int32_t width = pData->text.surface->w;
int32_t height = pData->text.surface->h;
pData->text.texture = SDL_CreateTextureFromSurface(renderer, pData->text.surface);
SDL_FreeSurface(pData->text.surface); // Surface not needed anymore
if (!pData->text.texture) {
Loge("Failed to create texture from text \"%s\"", text->str);
goto exit;
}
Logd("Real size of text \"%s\": %u x %u", text->str, width, height);
// Set transparency
(void)setTextureTransparency_f(obj, pData->text.texture, pData->text.color.a);
// Set dstRect
if ((width < (int32_t)rect->w) && (height < (int32_t)rect->h)) {
Logd("Centering text \"%s\" inside element", text->str);
pData->rect.x = rect->x + (((int32_t)rect->w - width) / 2);
pData->rect.y = rect->y + (((int32_t)rect->h - height) / 2);
pData->rect.w = width;
pData->rect.h = height;
}
else {
pData->rect.x = rect->x;
pData->rect.y = rect->y;
pData->rect.w = (int32_t)rect->w;
pData->rect.h = (int32_t)rect->h;
}
(void)adjustDrawingRect_f(obj, target, &pData->rect);
// Render to screen
ret = renderTexture_f(obj, target, pData->text.texture, NULL, &pData->rect);
// Destroy texture
SDL_DestroyTexture(pData->text.texture);
exit:
if (pData->text.font) {
TTF_CloseFont(pData->text.font);
}
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e setBgColor_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_color_s *color, enum gfx_target_e target)
{
ASSERT(obj && obj->pData);
if (!color) {
Loge("Bad arguments");
return DRAWER_ERROR_PARAMS;
}
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)obj->pData;
enum drawer_error_e ret = DRAWER_ERROR_DRAW;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
// Get renderer
SDL_Renderer *renderer = (target == GFX_TARGET_VIDEO ? pData->video.renderer
: pData->screen.renderer);
if (!renderer) {
Loge("Renderer not initialized yet for target %d", target);
goto exit;
}
// Set color
if (rect) {
pData->rect.x = rect->x;
pData->rect.y = rect->y;
pData->rect.w = (int32_t)rect->w;
pData->rect.h = (int32_t)rect->h;
}
(void)adjustDrawingRect_f(obj, target, &pData->rect);
if (SDL_SetRenderDrawColor(renderer, color->red, color->green,
color->blue, color->alpha) < 0) {
Loge("SDL_SetRenderDrawColor() failed - %s", SDL_GetError());
goto exit;
}
if (SDL_RenderFillRect(renderer, rect ? &pData->rect : NULL) < 0) {
Loge("SDL_RenderFillRect() failed - %s", SDL_GetError());
goto exit;
}
// In fullscreen, unlike SDL 2.0.5, SDL 2.0.8 does not refresh screen if
// RenderPresent() is called outside the main thread i.e thread that set
// the video mode
pData->screen.update = pData->screen.params.isFullScreen;
SDL_RenderPresent(renderer);
ret = DRAWER_ERROR_NONE;
exit:
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e restoreBgColor_f(struct drawer_s *obj, struct gfx_rect_s *rect,
struct gfx_color_s *fallbackColor,
enum gfx_target_e target)
{
ASSERT(obj && obj->pData);
if (!rect || !fallbackColor) {
Loge("Invalid arguments");
return DRAWER_ERROR_PARAMS;
}
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)obj->pData;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
enum drawer_error_e ret = DRAWER_ERROR_DRAW;
SDL_Renderer *renderer = NULL;
SDL_Texture *initialBgTexture = NULL;
SDL_Rect srcRect = {rect->x, rect->y, (int32_t)rect->w, (int32_t)rect->h};
SDL_Rect dstRect = {rect->x, rect->y, (int32_t)rect->w, (int32_t)rect->h};
// Prepare parameters
switch (target) {
case GFX_TARGET_SCREEN:
renderer = pData->screen.renderer;
initialBgTexture = pData->screen.initialBgTexture;
break;
case GFX_TARGET_VIDEO:
renderer = pData->video.renderer;
initialBgTexture = pData->video.initialBgTexture;
break;
default:
;
}
// Only positions on screen and video area have to be adjusted
// initialBgTexture is just a texture in which initial content is saved
// there's no adjustDrwaingRect to call when extracting rect from it
(void)adjustDrawingRect_f(obj, target, &dstRect);
// Restore background color/image on given rect
if (initialBgTexture) {
Logd("Restore from saved texture");
ret = renderTexture_f(obj, target, initialBgTexture, &srcRect, &dstRect);
}
if (ret != DRAWER_ERROR_NONE) {
Logd("Restore from provided fallbackColor");
if (SDL_SetRenderDrawColor(renderer, fallbackColor->red, fallbackColor->green,
fallbackColor->blue, fallbackColor->alpha) < 0) {
Loge("SDL_SetRenderDrawColor() failed - %s", SDL_GetError());
goto exit;
}
if (SDL_RenderFillRect(renderer, &dstRect) < 0) {
Loge("SDL_RenderFillRect() failed - %s", SDL_GetError());
goto exit;
}
// In fullscreen, unlike SDL 2.0.5, SDL 2.0.8 does not refresh screen if
// RenderPresent() is called outside the main thread i.e thread that set
// the video mode
pData->screen.update = pData->screen.params.isFullScreen;
SDL_RenderPresent(renderer);
ret = DRAWER_ERROR_NONE;
}
exit:
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e startDrawingInBg_f(struct drawer_s *obj)
{
ASSERT(obj && obj->pData);
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)obj->pData;
enum drawer_error_e ret = DRAWER_ERROR_DRAW;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
// Create screen surface with the right pixel format
SDL_Rect screenRect = {0, 0, (int32_t)pData->screen.params.rect.w,
(int32_t)pData->screen.params.rect.h};
ret = createSurfaceFromTarget_f(obj, GFX_TARGET_SCREEN, &pData->screen.surface, &screenRect);
if (ret != DRAWER_ERROR_NONE) {
goto unlockExit;
}
// Create screen texture to render to and update it with screen surface
uint32_t windowFormat = SDL_GetWindowPixelFormat(pData->screen.window);
pData->screen.texture = SDL_CreateTexture(pData->screen.renderer, windowFormat,
SDL_TEXTUREACCESS_TARGET,
screenRect.w, screenRect.h);
if (!pData->screen.texture) {
Loge("SDL_CreateTextureFromSurface() failed - %s", SDL_GetError());
goto freeSurfaceExit;
}
SDL_SetTextureBlendMode(pData->screen.texture, SDL_BLENDMODE_BLEND);
// Unfortunately, SDL_LockTexture() can't be used here because it expects a streaming
// texture
if (SDL_UpdateTexture(pData->screen.texture, NULL, pData->screen.surface->pixels,
pData->screen.surface->pitch) < 0) {
Loge("SDL_UpdateTexture() failed - %s", SDL_GetError());
SDL_DestroyTexture(pData->screen.texture);
goto freeSurfaceExit;
}
// Ask SDL to start rendering to screen texture
if (SDL_SetRenderTarget(pData->screen.renderer, pData->screen.texture) != 0) {
Loge("SDL_SetRenderTarget() failed - %s", SDL_GetError());
SDL_DestroyTexture(pData->screen.texture);
goto freeSurfaceExit;
}
ret = DRAWER_ERROR_NONE;
freeSurfaceExit:
SDL_FreeSurface(pData->screen.surface);
unlockExit:
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e stopDrawingInBg_f(struct drawer_s *obj)
{
ASSERT(obj && obj->pData);
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)obj->pData;
enum drawer_error_e ret = DRAWER_ERROR_DRAW;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
if (!pData->screen.texture) {
Loge("Screen texture not initialized");
goto unlockExit;
}
// Ask SDL to stop rendering to screen texture
if (SDL_SetRenderTarget(pData->screen.renderer, NULL) != 0) {
Loge("SDL_SetRenderTarget() failed - %s", SDL_GetError());
}
// Refresh screen
ret = renderTexture_f(obj, GFX_TARGET_SCREEN, pData->screen.texture, NULL, NULL);
// Destroy screen texture
SDL_DestroyTexture(pData->screen.texture);
unlockExit:
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e saveBuffer_f(struct drawer_s *obj, struct buffer_s *buffer,
struct gfx_image_s *inOut)
{
ASSERT(obj && obj->pData && buffer && inOut);
if (!buffer->data) {
Loge("No data provided");
return DRAWER_ERROR_PARAMS;
}
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)(obj->pData);
enum drawer_error_e ret = DRAWER_ERROR_NONE;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
if (pData->video.pixelFormat == SDL_PIXELFORMAT_ARGB8888) { // MJPEG format
pData->video.rwops = SDL_RWFromMem(buffer->data, (int32_t)buffer->length);
pData->video.surface = IMG_Load_RW(pData->video.rwops, 0);
Logd("Save video buffer to \"%s\"", inOut->path);
ret = writeSurfaceToFile_f(obj, pData->video.surface, inOut);
SDL_FreeSurface(pData->video.surface);
SDL_RWclose(pData->video.rwops);
}
else {
// TODO: Implementation for non-MJPEG format
Logw("Non-MJPEG format not supported yet");
}
SDL_UnlockMutex(pData->lock);
return ret;
}
/*!
*
*/
static enum drawer_error_e saveTarget_f(struct drawer_s *obj, struct gfx_image_s *inOut,
struct gfx_rect_s *srcRect, enum gfx_target_e target)
{
ASSERT(obj && obj->pData && inOut);
struct drawer_private_data_s *pData = (struct drawer_private_data_s*)(obj->pData);
enum drawer_error_e ret = DRAWER_ERROR_NONE;
if (SDL_LockMutex(pData->lock) != 0) {
Loge("Failed to lock mutex");
return DRAWER_ERROR_LOCK;
}
Logd("Create surface from video content");
// {0,0} to use the whole content when not in fullscreen
SDL_Rect videoRect = {0, 0, (int32_t)pData->video.params.rect.w,
(int32_t)pData->video.params.rect.h};
if (srcRect) {
videoRect.x = srcRect->x;
videoRect.y = srcRect->y;
videoRect.w = (int32_t)srcRect->w;
videoRect.h = (int32_t)srcRect->h;
}
(void)adjustDrawingRect_f(obj, target, &videoRect);
ret = createSurfaceFromTarget_f(obj, GFX_TARGET_VIDEO,
&pData->video.surface, &videoRect);
if (ret != DRAWER_ERROR_NONE) {
goto unlockExit;
}
if (target == GFX_TARGET_SCREEN) {
Logd("Create surface from screen content");
SDL_Rect screenRect = {pData->screen.params.rect.x,
pData->screen.params.rect.y,
(int32_t)pData->screen.params.rect.w,
(int32_t)pData->screen.params.rect.h};
ret = createSurfaceFromTarget_f(obj, GFX_TARGET_SCREEN,
&pData->screen.surface, &screenRect);
if (ret != DRAWER_ERROR_NONE) {
goto freeVideoSurfaceExit;
}
// In fullscreen mode, screen contains all gfx elements so no need to append
// video content
if (!pData->screen.params.isFullScreen) {
Logd("Add video content to screen content");
videoRect.x = pData->video.params.rect.x - SCREENSHOT_ADJUST_VIDEO_X;
videoRect.y = pData->video.params.rect.y - SCREENSHOT_ADJUST_VIDEO_Y;
SDL_BlitSurface(pData->video.surface, NULL, pData->screen.surface, &videoRect);
}
Logd("Save screen target to \"%s\"", inOut->path);
ret = writeSurfaceToFile_f(obj, pData->screen.surface, inOut);
SDL_FreeSurface(pData->screen.surface);
}
else {
Logd("Save video target to \"%s\"", inOut->path);
ret = writeSurfaceToFile_f(obj, pData->video.surface, inOut);
}
freeVideoSurfaceExit: