forked from rdkcmf/westeros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwesteros-render-gl.cpp
More file actions
3143 lines (2739 loc) · 102 KB
/
Copy pathwesteros-render-gl.cpp
File metadata and controls
3143 lines (2739 loc) · 102 KB
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
/*
* If not stated otherwise in this file or this component's Licenses.txt file the
* following copyright and licenses apply:
*
* Copyright 2016 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <assert.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#if defined (WESTEROS_PLATFORM_EMBEDDED) || defined (WESTEROS_HAVE_WAYLAND_EGL)
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#if defined (WESTEROS_PLATFORM_EMBEDDED)
#include "westeros-gl.h"
#include <gbm.h> //SIVA
#include <fcntl.h> //SIVA
#endif
#if defined (WESTEROS_PLATFORM_RPI)
#include <bcm_host.h>
#endif
#include "westeros-render.h"
#include "wayland-server.h"
#include "wayland-client.h"
#include "wayland-egl.h"
#ifdef ENABLE_SBPROTOCOL
#include "westeros-simplebuffer.h"
#endif
#include <vector>
//SIVA: DRM support
#define DRM
#ifdef DRM
extern "C" {
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_fourcc.h>
}
#endif
//#define WST_DEBUG
#ifdef WST_DEBUG
#define INT_TRACE(FORMAT,...) printf( FORMAT "\n", __VA_ARGS__)
#else
#define INT_TRACE(FORMAT,...)
#endif
#define WST_TRACE(...) INT_TRACE(__VA_ARGS__, "")
#define WST_UNUSED( n ) ((void)n)
#define ZNEAR (0.0f)
#define ZFAR (1000000.0f)
#define ZSTART (0.1f)
#define ZINC (0.1f)
#define MAX_LOG_SIZE (1024)
#define TCM11 (0)
#define TCM12 (1)
#define TCM21 (2)
#define TCM22 (3)
#define TCMDX (4)
#define TCMDY (5)
#define TCM_MAPX( x, y, t ) ((x)*(t)[TCM11]+(y)*(t)[TCM21]+(t)[TCMDX])
#define TCM_MAPY( x, y, t ) ((x)*(t)[TCM12]+(y)*(t)[TCM22]+(t)[TCMDY])
#define DEFAULT_SURFACE_WIDTH (0)
#define DEFAULT_SURFACE_HEIGHT (0)
typedef enum _WstShaderType
{
WstShaderType_texture,
WstShaderType_textureYUV,
WstShaderType_fill,
WstShaderType_alphaText,
} WstShaderType;
typedef struct _WstShaderClassBits
{
unsigned int type:4;
unsigned int opacity:1;
unsigned int unused:3;
int maskCount:24;
} WstShaderClassBits;
typedef union _WstShaderClass
{
WstShaderClassBits bits;
int id;
} WstShaderClass;
#define MAX_TEXTURES (2)
#define WST_MAX_MASK 16
typedef struct _WstShader
{
WstShaderClass shaderClass;
GLuint programId;
GLuint vertexShaderId;
GLuint fragmentShaderId;
GLuint mvpLocation;
GLuint vertexLocation;
GLuint textureLocation[MAX_TEXTURES];
GLuint fillColorLocation;
GLuint opacityLocation;
GLuint textureUnit0;
GLuint textureUnit1;
GLuint alphaTextColorLocation;
GLuint maskAttribLocation[WST_MAX_MASK];
GLuint maskUniformLocation[WST_MAX_MASK];
} WstShader;
typedef struct _WstRenderContext
{
float opacity;
float z;
const float *transform;
int cpuClip;
GLint clip[4];
} WstRenderContext;
struct _WstRenderSurface
{
void *nativePixmap;
int textureCount;
GLuint textureId[MAX_TEXTURES];
EGLImageKHR eglImage[MAX_TEXTURES];
int bufferWidth;
int bufferHeight;
unsigned char *mem;
bool memDirty;
int memWidth;
int memHeight;
GLint memFormatGL;
GLenum memType;
int x;
int y;
int width;
int height;
bool visible;
float opacity;
float zorder;
int onScreen;
int vertexLocation;
int textureLocation[MAX_TEXTURES];
float transform[6];
int clip[4];
int vertexCoordsDirty;
int texCoordsDirty;
bool invertedY;
GLfloat vertexCoords[4*2];
GLfloat textureCoords[4*2];
};
typedef struct _WstRendererGL
{
WstRenderer *renderer;
int outputWidth;
int outputHeight;
int noScreenClip;
GLfloat projection[16];
GLfloat modelview[16];
GLfloat mvp[16];
WstShader nullShader;
int currentShaderClass;
WstShader *currentShader;
int shaderCacheSize;
GLint maxTextureUnits;
GLuint *activeTextureId;
WstShader **shaderCache;
WstRenderContext renderCtx;
void *nativeWindow;
#if defined (WESTEROS_PLATFORM_EMBEDDED)
WstGLCtx *glCtx;
#endif
EGLDisplay eglDisplay;
EGLConfig eglConfig;
EGLContext eglContext;
EGLSurface eglSurface;
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
#if defined (WESTEROS_PLATFORM_EMBEDDED) || defined (WESTEROS_HAVE_WAYLAND_EGL)
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
#endif
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
bool haveWaylandEGL;
PFNEGLBINDWAYLANDDISPLAYWL eglBindWaylandDisplayWL;
PFNEGLUNBINDWAYLANDDISPLAYWL eglUnbindWaylandDisplayWL;
PFNEGLQUERYWAYLANDBUFFERWL eglQueryWaylandBufferWL;
#endif
std::vector<WstRenderSurface*> surfaces;
} WstRendererGL;
#ifdef DRM
extern "C"
{
struct drm_backend {
struct {
int id;
int fd;
char *filename;
} drm;
struct gbm_device *gbm;
uint32_t *crtcs;
int num_crtcs;
uint32_t crtc_allocator;
uint32_t connector_allocator;
struct wl_listener session_listener;
uint32_t format;
uint32_t min_width, max_width;
uint32_t min_height, max_height;
int no_addfb2;
struct wl_list sprite_list;
int sprites_are_broken;
int sprites_hidden;
int cursors_are_broken;
int use_pixman;
uint32_t prev_state;
int32_t cursor_width;
int32_t cursor_height;
};
struct drm_edid {
char eisa_id[13];
char monitor_name[13];
char pnp_id[5];
char serial_number[13];
};
struct drm_output {
uint32_t crtc_id;
int pipe;
uint32_t connector_id;
drmModeCrtcPtr original_crtc;
struct drm_edid edid;
drmModePropertyPtr dpms_prop;
uint32_t format;
int vblank_pending;
int page_flip_pending;
int destroy_pending;
struct gbm_surface *surface;
struct gbm_bo *cursor_bo[2];
int current_cursor;
struct drm_fb *current, *next;
struct backlight *backlight;
struct drm_fb *dumb[2];
// pixman_image_t *image[2];
int current_image;
// pixman_region32_t previous_damage;
struct vaapi_recorder *recorder;
struct wl_listener recorder_frame_listener;
};
struct drm_fb {
struct drm_output *output;
uint32_t fb_id, stride, handle, size;
int fd;
int is_client_buffer;
struct wl_resource *buffer;
/* Used by gbm fbs */
struct gbm_bo *bo;
/* Used by dumb fbs */
void *map;
};
static int create_output(struct drm_backend *b,
drmModeRes *resources,
drmModeConnector *connector,
int x, int y);
}
#endif
static WstRendererGL* wstRendererGLCreate( int width, int height );
static void wstRendererGLDestroy( WstRendererGL *renderer );
static WstRenderSurface *wstRenderGLCreateSurface(WstRendererGL *renderer);
static void wstRenderGLDestroySurface( WstRendererGL *renderer, WstRenderSurface *surface );
static void wstRendererGLFlushSurface( WstRendererGL *renderer, WstRenderSurface *surface );
static void wstRendererGLCommitShm( WstRendererGL *rendererGL, WstRenderSurface *surface, struct wl_resource *resource );
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
static void wstRendererGLCommitWaylandEGL( WstRendererGL *rendererGL, WstRenderSurface *surface,
struct wl_resource *resource, EGLint format );
#endif
#ifdef ENABLE_SBPROTOCOL
static void wstRendererGLCommitSB( WstRendererGL *rendererGL, WstRenderSurface *surface, struct wl_resource *resource );
#endif
#if defined (WESTEROS_PLATFORM_RPI)
static void wstRendererGLCommitDispmanx( WstRendererGL *rendererGL, WstRenderSurface *surface,
DISPMANX_RESOURCE_HANDLE_T dispResource,
EGLint format, int bufferWidth, int bufferHeight );
#endif
static void wstRenderGLPrepareSurface( WstRendererGL *renderer, WstRenderContext *rctx, WstRenderSurface *surf );
static void wstRenderGLRenderSurface( WstRendererGL *renderer, WstRenderContext *rctx, WstRenderSurface *surf );
static bool wstRenderGLSetupEGL( WstRendererGL *renderer );
static void wstMatrixIdentity(float matrix[16]);
static void wstMatrixMultMatrix(float matrix[16], float in1[16], float in2[16]);
static void wstMatrixTranslate(float matrix[16], float tx, float ty, float tz);
static void wstMatrixOrtho(float matrix[16], float left, float right, float bottom, float top, float near, float far);
static void wstSetCurrentShader( WstRendererGL *renderer, int shaderClassId );
static WstShader* wstGetShaderForClass( WstRendererGL *renderer, int shaderClassId );
static WstShader* wstCreateShaderForClass( WstShaderClass shaderClass );
static void wstDestroyShader( WstRendererGL *renderer, WstShader *shader );
static unsigned int wstCreateGLShader(const char *pShaderSource, bool isVertexShader );
#ifdef DRM
struct drm_backend *b;
struct drm_output *output;
static void
drm_fb_set_buffer(struct drm_fb *fb, struct wl_resource *buffer)
{
assert(fb->buffer == NULL);
fb->is_client_buffer = 1;
fb->buffer = buffer;
}
static void
drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
{
struct drm_fb *fb = (struct drm_fb*)data;
struct gbm_device *gbm = gbm_bo_get_device(bo);
if (fb->fb_id)
drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
fb->buffer = NULL;
free(data);
}
static struct drm_fb *
drm_fb_get_from_bo(struct gbm_bo *bo,
struct drm_backend *backend, uint32_t format)
{
struct drm_fb *fb = (struct drm_fb*)gbm_bo_get_user_data(bo);
uint32_t width, height;
uint32_t handles[4], pitches[4], offsets[4];
int ret;
if (fb)
return fb;
fb = (struct drm_fb*)calloc(1, sizeof *fb);
if (fb == NULL)
return NULL;
fb->bo = bo;
width = gbm_bo_get_width(bo);
height = gbm_bo_get_height(bo);
fb->stride = gbm_bo_get_stride(bo);
fb->handle = gbm_bo_get_handle(bo).u32;
fb->size = fb->stride * height;
fb->fd = backend->drm.fd;
if (backend->min_width > width || width > backend->max_width ||
backend->min_height > height ||
height > backend->max_height) {
printf("bo geometry out of bounds\n");
goto err_free;
}
ret = -1;
if (format && !backend->no_addfb2) {
handles[0] = fb->handle;
pitches[0] = fb->stride;
offsets[0] = 0;
ret = drmModeAddFB2(backend->drm.fd, width, height,
format, handles, pitches, offsets,
&fb->fb_id, 0);
if (ret) {
printf("addfb2 failed: \n");
backend->no_addfb2 = 1;
backend->sprites_are_broken = 1;
}
}
if (ret)
ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
fb->stride, fb->handle, &fb->fb_id);
if (ret) {
printf("failed to create kms fb: \n");
goto err_free;
}
gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
return fb;
err_free:
free(fb);
return NULL;
}
#endif
static WstRendererGL* wstRendererGLCreate( WstRenderer *renderer )
{
WstRendererGL *rendererGL= 0;
#ifdef DRM
int ret;
uint64_t cap;
b = (struct drm_backend*)calloc(1, sizeof(b));
if(!b)
printf("DRM backend is null\n");
b->format = GBM_FORMAT_XRGB8888;
#endif
rendererGL= (WstRendererGL*)calloc(1, sizeof(WstRendererGL) );
if ( rendererGL )
{
rendererGL->outputWidth= renderer->outputWidth;
rendererGL->outputHeight= renderer->outputHeight;
rendererGL->currentShaderClass= -1;
#if defined (WESTEROS_PLATFORM_EMBEDDED)
rendererGL->glCtx= WstGLInit();
#endif
rendererGL->renderer= renderer;
wstRenderGLSetupEGL( rendererGL );
#ifdef DRM
b->drm.filename = "/dev/dri/card0";
ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
if (ret == 0)
b->cursor_width = cap;
else
b->cursor_width = 64;
ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
if (ret == 0)
b->cursor_height = cap;
else
b->cursor_height = 64;
#endif
rendererGL->eglCreateImageKHR= (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
printf( "eglCreateImageKHR %p\n", rendererGL->eglCreateImageKHR);
rendererGL->eglDestroyImageKHR= (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
printf( "eglDestroyImageKHR %p\n", rendererGL->eglDestroyImageKHR);
#if defined (WESTEROS_PLATFORM_EMBEDDED) || defined (WESTEROS_HAVE_WAYLAND_EGL)
rendererGL->glEGLImageTargetTexture2DOES= (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
printf( "glEGLImageTargetTexture2DOES %p\n", rendererGL->glEGLImageTargetTexture2DOES);
#endif
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
const char *extensions= eglQueryString( rendererGL->eglDisplay, EGL_EXTENSIONS );
if ( extensions )
{
if ( !strstr( extensions, "EGL_WL_bind_wayland_display" ) )
{
printf("wayland-egl support expected, but not advertised by eglQueryString(eglDisplay,EGL_EXTENSIONS): not attempting to use\n" );
}
else
{
printf("wayland-egl support expected, and is advertised by eglQueryString(eglDisplay,EGL_EXTENSIONS): proceeding to use \n" );
rendererGL->eglBindWaylandDisplayWL= (PFNEGLBINDWAYLANDDISPLAYWL)eglGetProcAddress("eglBindWaylandDisplayWL");
printf( "eglBindWaylandDisplayWL %p\n", rendererGL->eglBindWaylandDisplayWL );
rendererGL->eglUnbindWaylandDisplayWL= (PFNEGLUNBINDWAYLANDDISPLAYWL)eglGetProcAddress("eglUnbindWaylandDisplayWL");
printf( "eglUnbindWaylandDisplayWL %p\n", rendererGL->eglUnbindWaylandDisplayWL );
rendererGL->eglQueryWaylandBufferWL= (PFNEGLQUERYWAYLANDBUFFERWL)eglGetProcAddress("eglQueryWaylandBufferWL");
printf( "eglQueryWaylandBufferWL %p\n", rendererGL->eglQueryWaylandBufferWL );
if ( rendererGL->eglBindWaylandDisplayWL &&
rendererGL->eglUnbindWaylandDisplayWL &&
rendererGL->eglQueryWaylandBufferWL )
{
printf("calling eglBindWaylandDisplayWL with eglDisplay %p and wayland display %p\n", rendererGL->eglDisplay, renderer->display );
EGLBoolean rc= rendererGL->eglBindWaylandDisplayWL( rendererGL->eglDisplay, renderer->display );
if ( rc )
{
rendererGL->haveWaylandEGL= true;
}
else
{
printf("eglBindWaylandDisplayWL failed: %x\n", eglGetError() );
}
}
else
{
printf("wayland-egl support expected, and advertised, but methods are missing: no wayland-egl\n" );
}
}
}
printf("have wayland-egl: %d\n", rendererGL->haveWaylandEGL );
#endif
// Get the number of texture units
glGetIntegerv( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &rendererGL->maxTextureUnits );
WST_TRACE( "WstRendererGL: WstRendererGLCreate: maxTextureUnits=%d\n", rendererGL->maxTextureUnits );
rendererGL->shaderCacheSize= 4 * // texture + textureYUV + fill + alphaText
2 * // opacity on/off
rendererGL->maxTextureUnits;
rendererGL->shaderCache= (WstShader**)calloc( 1, rendererGL->shaderCacheSize*sizeof(WstShader*) );
WST_TRACE( "WstRendererGL: WstRendererGLCreate: allocate cache for %d shader classes\n", rendererGL->shaderCacheSize );
// Initialize null shader
rendererGL->nullShader.shaderClass.id= -1;
rendererGL->nullShader.programId= GL_NONE;
rendererGL->nullShader.vertexShaderId= GL_NONE;
rendererGL->nullShader.fragmentShaderId= GL_NONE;
rendererGL->nullShader.mvpLocation= -1;
rendererGL->nullShader.vertexLocation= -1;
rendererGL->nullShader.textureLocation[0]= -1;
rendererGL->nullShader.textureLocation[1]= -1;
rendererGL->nullShader.fillColorLocation= -1;
rendererGL->nullShader.opacityLocation= -1;
for( int i= 0; i < WST_MAX_MASK; ++i )
{
rendererGL->nullShader.maskAttribLocation[i]= -1;
rendererGL->nullShader.maskUniformLocation[i]= -1;
}
rendererGL->activeTextureId= (GLuint*)malloc( rendererGL->maxTextureUnits*sizeof(GLuint) );
for( int i= 0; i < rendererGL->maxTextureUnits; ++i )
{
rendererGL->activeTextureId[i]= GL_NONE;
}
rendererGL->currentShader= &rendererGL->nullShader;
wstMatrixIdentity(rendererGL->modelview);
wstMatrixTranslate(rendererGL->modelview, -rendererGL->outputWidth, -rendererGL->outputHeight, 0);
wstMatrixIdentity(rendererGL->projection);
wstMatrixOrtho( rendererGL->projection,
0, //left
rendererGL->outputWidth, //right
rendererGL->outputHeight, //bottom
0, //top
ZNEAR, //near
ZFAR ); //far
wstMatrixMultMatrix(rendererGL->mvp, rendererGL->projection, rendererGL->modelview);
}
return rendererGL;
}
static void wstRendererGLDestroy( WstRendererGL *renderer )
{
if ( renderer )
{
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
if ( renderer->haveWaylandEGL )
{
renderer->eglUnbindWaylandDisplayWL( renderer->eglDisplay, renderer->renderer->display );
renderer->haveWaylandEGL= false;
}
#endif
if ( renderer->shaderCache )
{
WstShader *shader;
for( int i= 0; i < renderer->shaderCacheSize; ++i )
{
shader= renderer->shaderCache[i];
if ( shader )
{
wstDestroyShader( renderer, shader );
renderer->shaderCache[i]= 0;
}
}
free( renderer->shaderCache );
renderer->shaderCache= 0;
}
if (renderer->activeTextureId)
{
free(renderer->activeTextureId);
renderer->activeTextureId = 0;
}
if ( renderer->eglSurface )
{
eglDestroySurface( renderer->eglDisplay, renderer->eglSurface );
renderer->eglSurface= 0;
}
// If we are doing nested composition destroy the wayland egl window
// otherwise destroy the native egl window
if ( renderer->renderer->displayNested )
{
if ( renderer->nativeWindow )
{
wl_egl_window_destroy( (struct wl_egl_window *)renderer->nativeWindow );
renderer->nativeWindow= 0;
}
}
else
{
#if defined (WESTEROS_PLATFORM_EMBEDDED)
if ( renderer->nativeWindow )
{
WstGLDestroyNativeWindow( renderer->glCtx, renderer->nativeWindow );
renderer->nativeWindow= 0;
}
WstGLTerm( renderer->glCtx );
renderer->glCtx= 0;
#endif
}
free( renderer );
}
}
static WstRenderSurface *wstRenderGLCreateSurface(WstRendererGL *renderer)
{
WstRenderSurface *surface= 0;
WST_UNUSED(renderer);
surface= (WstRenderSurface*)calloc( 1, sizeof(WstRenderSurface) );
if ( surface )
{
surface->textureCount= 1;
surface->textureId[0]= GL_NONE;
surface->width= DEFAULT_SURFACE_WIDTH;
surface->height= DEFAULT_SURFACE_HEIGHT;
surface->x= 0;
surface->y= 0;
surface->visible= true;
surface->opacity= 1.0;
surface->zorder= 0.5;
wstMatrixIdentity(surface->transform);
surface->vertexCoordsDirty= 1;
surface->texCoordsDirty= 1;
}
return surface;
}
static void wstRenderGLDestroySurface( WstRendererGL *renderer, WstRenderSurface *surface )
{
WST_UNUSED(renderer);
if ( surface )
{
wstRendererGLFlushSurface( renderer, surface );
free( surface );
}
}
static void wstRendererGLFlushSurface( WstRendererGL *renderer, WstRenderSurface *surface )
{
if ( surface )
{
for( int i= 0; i < MAX_TEXTURES; ++i )
{
if ( surface->textureId[i] )
{
glDeleteTextures( 1, &surface->textureId[i] );
surface->textureId[i]= GL_NONE;
}
if ( surface->eglImage[i] )
{
renderer->eglDestroyImageKHR( renderer->eglDisplay,
surface->eglImage[i] );
surface->eglImage[i]= 0;
}
}
#if defined (WESTEROS_PLATFORM_EMBEDDED)
if ( surface->nativePixmap )
{
WstGLReleaseNativePixmap( renderer->glCtx, surface->nativePixmap );
surface->nativePixmap= 0;
}
#endif
if ( surface->mem )
{
free( surface->mem );
}
}
}
static void wstRendererGLCommitShm( WstRendererGL *rendererGL, WstRenderSurface *surface, struct wl_resource *resource )
{
struct wl_shm_buffer *shmBuffer;
int width, height, stride;
GLint formatGL;
GLenum type;
bool transformPixelsA= false;
bool transformPixelsB= false;
bool fillAlpha= false;
void *data;
shmBuffer= wl_shm_buffer_get( resource );
if ( shmBuffer )
{
width= wl_shm_buffer_get_width(shmBuffer);
height= wl_shm_buffer_get_height(shmBuffer);
stride= wl_shm_buffer_get_stride(shmBuffer);
// The SHM formats describe the structure of the color channels for a pixel as
// they would appear in a machine register not the byte order in memory. For
// example WL_SHM_FORMAT_ARGB8888 is a 32 bit pixel with alpha in the 8 most significant
// bits and blue in the 8 list significant bits. On a little endian machine the
// byte order in memory would be B, G, R, A.
switch( wl_shm_buffer_get_format(shmBuffer) )
{
case WL_SHM_FORMAT_ARGB8888:
#ifdef BIG_ENDIAN_CPU
formatGL= GL_RGBA;
transformPixelsA= true;
#else
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
if ( rendererGL->haveWaylandEGL )
{
formatGL= GL_BGRA_EXT;
}
else
{
formatGL= GL_RGBA;
transformPixelsB= true;
}
#elif defined (WESTEROS_PLATFORM_EMBEDDED)
formatGL= GL_BGRA_EXT;
#else
formatGL= GL_RGBA;
transformPixelsB= true;
#endif
#endif
type= GL_UNSIGNED_BYTE;
break;
case WL_SHM_FORMAT_XRGB8888:
#ifdef BIG_ENDIAN_CPU
formatGL= GL_RGBA;
transformPixelsA= true;
#else
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
if ( rendererGL->haveWaylandEGL )
{
formatGL= GL_BGRA_EXT;
}
else
{
formatGL= GL_RGBA;
transformPixelsB= true;
}
#elif defined (WESTEROS_PLATFORM_EMBEDDED)
formatGL= GL_BGRA_EXT;
#else
formatGL= GL_RGBA;
transformPixelsB= true;
#endif
#endif
type= GL_UNSIGNED_BYTE;
fillAlpha= true;
break;
case WL_SHM_FORMAT_BGRA8888:
#ifdef BIG_ENDIAN_CPU
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
if ( rendererGL->haveWaylandEGL )
{
formatGL= GL_BGRA_EXT;
}
else
{
formatGL= GL_RGBA;
transformPixelsB= true;
}
#elif defined (WESTEROS_PLATFORM_EMBEDDED)
formatGL= GL_BGRA_EXT;
#else
formatGL= GL_RGBA;
transformPixelsB= true;
#endif
#else
formatGL= GL_RGBA;
transformPixelsA= true;
#endif
type= GL_UNSIGNED_BYTE;
break;
case WL_SHM_FORMAT_BGRX8888:
#ifdef BIG_ENDIAN_CPU
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
if ( rendererGL->haveWaylandEGL )
{
formatGL= GL_BGRA_EXT;
}
else
{
formatGL= GL_RGBA;
transformPixelsB= true;
}
#elif defined (WESTEROS_PLATFORM_EMBEDDED)
formatGL= GL_BGRA_EXT;
#else
formatGL= GL_RGBA;
transformPixelsB= true;
#endif
#else
formatGL= GL_RGBA;
transformPixelsA= true;
#endif
type= GL_UNSIGNED_BYTE;
fillAlpha= true;
break;
case WL_SHM_FORMAT_RGB565:
formatGL= GL_RGB;
type= GL_UNSIGNED_SHORT_5_6_5;
break;
case WL_SHM_FORMAT_ARGB4444:
formatGL= GL_RGBA;
type= GL_UNSIGNED_SHORT_4_4_4_4;
break;
default:
formatGL= GL_NONE;
break;
}
if ( formatGL != GL_NONE )
{
wl_shm_buffer_begin_access(shmBuffer);
data= wl_shm_buffer_get_data(shmBuffer);
if ( surface->mem &&
(
(surface->memWidth != width) ||
(surface->memHeight != height) ||
(surface->memFormatGL != formatGL) ||
(surface->memType != type)
)
)
{
free( surface->mem );
surface->mem= 0;
surface->vertexCoordsDirty= 1;
}
if ( !surface->mem )
{
surface->mem= (unsigned char*)malloc( stride*height );
}
if ( surface->mem )
{
memcpy( surface->mem, data, stride*height );
if ( transformPixelsA )
{
// transform ARGB to RGBA
unsigned int pixel, alpha;
unsigned int *pixdata= (unsigned int*)surface->mem;
for( int y= 0; y < height; ++y )
{
for( int x= 0; x < width; ++x )
{
pixel= pixdata[y*width+x];
alpha= (fillAlpha ? 0xFF : (pixel>>24));
pixel= (pixel<<8)|alpha;
pixdata[y*width+x]= pixel;
}
}
}
else if ( transformPixelsB )
{
// transform BGRA to RGBA
unsigned char *pixdata= (unsigned char*)surface->mem;
for( int y= 0; y < height; ++y )
{
for( int x= 0; x < width; ++x )
{
if ( fillAlpha )
{
pixdata[y*width*4 + x*4 +3]= 0xFF;
}
unsigned char temp= pixdata[y*width*4 + x*4 +2];
pixdata[y*width*4 + x*4 +2]= pixdata[y*width*4 + x*4 +0];
pixdata[y*width*4 + x*4 +0]= temp;
}
}
}
else if ( fillAlpha )
{
if ( fillAlpha )
{
unsigned char *pixdata= (unsigned char*)surface->mem;
for( int y= 0; y < height; ++y )
{
for( int x= 0; x < width; ++x )
{
pixdata[y*width*4 + x*4 +3]= 0xFF;
}
}
}
}
surface->bufferWidth= width;
surface->bufferHeight= height;
surface->memWidth= width;
surface->memHeight= height;
surface->memFormatGL= formatGL;
surface->memType= type;
surface->memDirty= true;
}
wl_shm_buffer_end_access(shmBuffer);
}
}
}
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
static void wstRendererGLCommitWaylandEGL( WstRendererGL *rendererGL, WstRenderSurface *surface,
struct wl_resource *resource, EGLint format )
{
static int i = 1;
char s[50];
FILE *fbfd = NULL;
void *data = NULL;
EGLImageKHR eglImage= 0;
EGLint value;
EGLint attrList[3];
int bufferWidth= 0, bufferHeight= 0;
struct gbm_bo *bo;
int pixelformat = 0;
if (EGL_TRUE == rendererGL->eglQueryWaylandBufferWL( rendererGL->eglDisplay,
resource,
EGL_WIDTH,
&value ) )
{
bufferWidth= value;
}
if (EGL_TRUE == rendererGL->eglQueryWaylandBufferWL( rendererGL->eglDisplay,