forked from rdkcmf/westeros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
westeros-render-embedded.cpp
1853 lines (1628 loc) · 56.9 KB
/
westeros-render-embedded.cpp
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 <dlfcn.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"
#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>
//#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 DEFAULT_SURFACE_WIDTH (0)
#define DEFAULT_SURFACE_HEIGHT (0)
// assume premultiplied
static const char *fShaderText =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"uniform sampler2D s_texture;\n"
"uniform float u_alpha;\n"
"varying vec2 v_uv;\n"
"void main()\n"
"{\n"
" gl_FragColor = texture2D(s_texture, v_uv) * u_alpha;\n"
"}\n";
static const char *vShaderText =
"uniform vec2 u_resolution;\n"
"uniform mat4 amymatrix;\n"
"attribute vec2 pos;\n"
"attribute vec2 uv;\n"
"varying vec2 v_uv;\n"
"void main()\n"
"{\n"
// map from "pixel coordinates"
" vec4 p = amymatrix * vec4(pos, 0, 1);\n"
" vec4 zeroToOne = p / vec4(u_resolution, u_resolution.x, 1);\n"
" vec4 zeroToTwo = zeroToOne * vec4(2.0, 2.0, 1, 1);\n"
" vec4 clipSpace = zeroToTwo - vec4(1.0, 1.0, 0, 0);\n"
" clipSpace.w = 1.0+clipSpace.z;\n"
" gl_Position = clipSpace * vec4(1, -1, 1, 1);\n"
" v_uv = uv;\n"
"}\n";
static const char *fShaderTextYUV =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"uniform sampler2D s_texturey;\n"
"uniform sampler2D s_textureuv;\n"
"const vec3 cc_r = vec3(1.0, -0.8604, 1.59580);\n"
"const vec4 cc_g = vec4(1.0, 0.539815, -0.39173, -0.81290);\n"
"const vec3 cc_b = vec3(1.0, -1.071, 2.01700);\n"
"uniform float u_alpha;\n"
"varying vec2 v_texy;\n"
"varying vec2 v_texuv;\n"
"void main()\n"
"{\n"
" vec4 y_vec = texture2D(s_texturey, v_texy);\n"
" vec4 c_vec = texture2D(s_textureuv, v_texuv);\n"
" vec4 temp_vec = vec4(y_vec.a, 1.0, c_vec.b, c_vec.a);\n"
" gl_FragColor = vec4( dot(cc_r,temp_vec.xyw), dot(cc_g,temp_vec), dot(cc_b,temp_vec.xyz), u_alpha );\n"
"}\n";
static const char *vShaderTextYUV =
"uniform vec2 u_resolution;\n"
"uniform mat4 amymatrix;\n"
"attribute vec2 pos;\n"
"attribute vec2 texcoordy;\n"
"attribute vec2 texcoorduv;\n"
"varying vec2 v_texy;\n"
"varying vec2 v_texuv;\n"
"void main()\n"
"{\n"
// map from "pixel coordinates"
" vec4 p = amymatrix * vec4(pos, 0, 1);\n"
" vec4 zeroToOne = p / vec4(u_resolution, u_resolution.x, 1);\n"
" vec4 zeroToTwo = zeroToOne * vec4(2.0, 2.0, 1, 1);\n"
" vec4 clipSpace = zeroToTwo - vec4(1.0, 1.0, 0, 0);\n"
" clipSpace.w = 1.0+clipSpace.z;\n"
" gl_Position = clipSpace * vec4(1, -1, 1, 1);\n"
" v_texy = texcoordy;\n"
" v_texuv = texcoorduv;\n"
"}\n";
static GLuint createShaderProgram(const char* vShaderTxt, const char* fShaderTxt)
{
GLuint fragShader, vertShader, program = 0;
GLint stat;
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, (const char **) &fShaderTxt, NULL);
glCompileShader(fragShader);
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &stat);
if (!stat)
{
WST_TRACE("Error: fragment shader did not compile: %d\nSource:\n%s", glGetError(), fShaderTxt);
GLint maxLength = 0;
glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength includes the NULL character
std::vector<char> errorLog(maxLength);
glGetShaderInfoLog(fragShader, maxLength, &maxLength, &errorLog[0]);
WST_TRACE("%s", &errorLog[0]);
glDeleteShader(fragShader);
return GL_NONE;
}
vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, (const char **) &vShaderTxt, NULL);
glCompileShader(vertShader);
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &stat);
if (!stat)
{
WST_TRACE("vertex shader did not compile: %d\nSource:\n%s", glGetError(), vShaderTxt);
glDeleteShader(fragShader);
glDeleteShader(vertShader);
return GL_NONE;
}
program = glCreateProgram();
glAttachShader(program, fragShader);
glAttachShader(program, vertShader);
return program;
}
void linkShaderProgram(GLuint program)
{
GLint stat;
glLinkProgram(program); /* needed to put attribs into effect */
glGetProgramiv(program, GL_LINK_STATUS, &stat);
if (!stat)
{
char log[1000];
GLsizei len;
glGetProgramInfoLog(program, 1000, &len, log);
WST_TRACE("failed to link:%s", log);
assert(false);
}
}
class shaderProgram
{
public:
virtual void init(const char* v, const char* f)
{
mProgram = createShaderProgram(v, f);
prelink();
linkShaderProgram(mProgram);
postlink();
}
int getUniformLocation(const char* name)
{
int l = glGetUniformLocation(mProgram, name);
if (l == -1)
{
WST_TRACE("Shader does not define uniform %s.\n", name);
}
return l;
}
void use()
{
glUseProgram(mProgram);
}
protected:
// Override to do uniform lookups
virtual void prelink() {}
virtual void postlink() {}
GLuint mProgram;
};
class textureShaderProgram: public shaderProgram
{
protected:
virtual void prelink()
{
mPosLoc = 0;
mUVLoc = 1;
glBindAttribLocation(mProgram, mPosLoc, "pos");
glBindAttribLocation(mProgram, mUVLoc, "uv");
}
virtual void postlink()
{
mResolutionLoc = getUniformLocation("u_resolution");
mMatrixLoc = getUniformLocation("amymatrix");
mAlphaLoc = getUniformLocation("u_alpha");
mTextureLoc = getUniformLocation("s_texture");
}
public:
void draw(int resW, int resH, float* matrix, float alpha,
GLuint textureId, int count,
const float* pos, const float* uv )
{
use();
glUniform2f(mResolutionLoc, resW, resH);
glUniformMatrix4fv(mMatrixLoc, 1, GL_FALSE, matrix);
glUniform1f(mAlphaLoc, alpha);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureId);
glUniform1i(mTextureLoc, 1);
glVertexAttribPointer(mPosLoc, 2, GL_FLOAT, GL_FALSE, 0, pos);
glVertexAttribPointer(mUVLoc, 2, GL_FLOAT, GL_FALSE, 0, uv);
glEnableVertexAttribArray(mPosLoc);
glEnableVertexAttribArray(mUVLoc);
glDrawArrays(GL_TRIANGLE_STRIP, 0, count);
glDisableVertexAttribArray(mPosLoc);
glDisableVertexAttribArray(mUVLoc);
}
private:
GLint mResolutionLoc;
GLint mMatrixLoc;
GLint mPosLoc;
GLint mUVLoc;
GLint mAlphaLoc;
GLint mTextureLoc;
};
class textureShaderYUVProgram: public shaderProgram
{
protected:
virtual void prelink()
{
mPosLoc = 0;
mTexYLoc = 1;
mTexUVLoc = 2;
glBindAttribLocation(mProgram, mPosLoc, "pos");
glBindAttribLocation(mProgram, mTexYLoc, "texcoordy");
glBindAttribLocation(mProgram, mTexUVLoc, "texcoorduv");
}
virtual void postlink()
{
mResolutionLoc = getUniformLocation("u_resolution");
mMatrixLoc = getUniformLocation("amymatrix");
mAlphaLoc = getUniformLocation("u_alpha");
mTextureYLoc = getUniformLocation("s_texturey");
mTextureUVLoc = getUniformLocation("s_textureuv");
}
public:
void draw(int resW, int resH, float* matrix, float alpha,
GLuint textureYId, GLuint textureUVId, int count,
const float* pos, const float* uv )
{
use();
glUniform2f(mResolutionLoc, resW, resH);
glUniformMatrix4fv(mMatrixLoc, 1, GL_FALSE, matrix);
glUniform1f(mAlphaLoc, alpha);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureYId);
glUniform1i(mTextureYLoc, 1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, textureUVId);
glUniform1i(mTextureUVLoc, 2);
glVertexAttribPointer(mPosLoc, 2, GL_FLOAT, GL_FALSE, 0, pos);
glVertexAttribPointer(mTexYLoc, 2, GL_FLOAT, GL_FALSE, 0, uv);
glVertexAttribPointer(mTexUVLoc, 2, GL_FLOAT, GL_FALSE, 0, uv);
glEnableVertexAttribArray(mPosLoc);
glEnableVertexAttribArray(mTexYLoc);
glEnableVertexAttribArray(mTexUVLoc);
glDrawArrays(GL_TRIANGLE_STRIP, 0, count);
glDisableVertexAttribArray(mPosLoc);
glDisableVertexAttribArray(mTexYLoc);
glDisableVertexAttribArray(mTexUVLoc);
}
private:
GLint mResolutionLoc;
GLint mMatrixLoc;
GLint mPosLoc;
GLint mTexYLoc;
GLint mTexUVLoc;
GLint mAlphaLoc;
GLint mTextureYLoc;
GLint mTextureUVLoc;
};
#define MAX_TEXTURES (2)
struct _WstRenderSurface
{
int textureCount;
GLuint textureId[MAX_TEXTURES];
int bufferWidth;
int bufferHeight;
#if defined (WESTEROS_PLATFORM_EMBEDDED) || defined (WESTEROS_HAVE_WAYLAND_EGL)
void *nativePixmap;
EGLImageKHR eglImage[MAX_TEXTURES];
#endif
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;
bool dirty;
bool invertedY;
WstRenderSurface *surfaceFast;
};
typedef struct _WstRendererEMB
{
WstRenderer *renderer;
int outputWidth;
int outputHeight;
#if defined (WESTEROS_PLATFORM_EMBEDDED)
WstGLCtx *glCtx;
#endif
void *nativeWindow;
EGLDisplay eglDisplay;
EGLContext eglContext;
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
textureShaderProgram *textureShader;
textureShaderYUVProgram *textureShaderYUV;
std::vector<WstRenderSurface*> surfaces;
bool fastPathActive;
WstRenderer *rendererFast;
} WstRendererEMB;
static bool wstRendererEMBSetupEGL( WstRendererEMB *renderer );
static WstRendererEMB* wstRendererEMBCreate( WstRenderer *renderer );
static void wstRendererEMBDestroy( WstRendererEMB *renderer );
static WstRenderSurface *wstRendererEMBCreateSurface(WstRendererEMB *renderer);
static void wstRendererEMBDestroySurface( WstRendererEMB *renderer, WstRenderSurface *surface );
static void wstRendererEMBFlushSurface( WstRendererEMB *renderer, WstRenderSurface *surface );
static void wstRendererEMBCommitShm( WstRendererEMB *renderer, WstRenderSurface *surface, struct wl_resource *resource );
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
static void wstRendererEMBCommitWaylandEGL( WstRendererEMB *renderer, WstRenderSurface *surface,
struct wl_resource *resource, EGLint format );
#endif
#ifdef ENABLE_SBPROTOCOL
static void wstRendererEMBCommitSB( WstRendererEMB *renderer, WstRenderSurface *surface, struct wl_resource *resource );
#endif
#if defined (WESTEROS_PLATFORM_RPI)
static void wstRendererEMBCommitDispmanx( WstRendererEMB *renderer, WstRenderSurface *surface,
DISPMANX_RESOURCE_HANDLE_T dispResource,
EGLint format, int bufferWidth, int bufferHeight );
#endif
static void wstRendererEMBRenderSurface( WstRendererEMB *renderer, WstRenderSurface *surface );
static void wstRendererInitFastPath( WstRendererEMB *renderer );
static bool wstRendererActivateFastPath( WstRendererEMB *renderer );
static void wstRendererDeactivateFastPath( WstRendererEMB *renderer );
#define RED_SIZE (8)
#define GREEN_SIZE (8)
#define BLUE_SIZE (8)
#define ALPHA_SIZE (8)
#define DEPTH_SIZE (0)
static WstRendererEMB* wstRendererEMBCreate( WstRenderer *renderer )
{
WstRendererEMB *rendererEMB= 0;
rendererEMB= (WstRendererEMB*)calloc(1, sizeof(WstRendererEMB) );
if ( rendererEMB )
{
rendererEMB->outputWidth= renderer->outputWidth;
rendererEMB->outputHeight= renderer->outputHeight;
rendererEMB->renderer= renderer;
#if defined (WESTEROS_PLATFORM_EMBEDDED)
rendererEMB->glCtx= WstGLInit();
if ( !rendererEMB->glCtx )
{
free( rendererEMB );
rendererEMB= 0;
goto exit;
}
#endif
rendererEMB->eglCreateImageKHR= (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
WST_TRACE( "eglCreateImageKHR %p\n", rendererEMB->eglCreateImageKHR);
rendererEMB->eglDestroyImageKHR= (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
WST_TRACE( "eglDestroyImageKHR %p\n", rendererEMB->eglDestroyImageKHR);
#if defined (WESTEROS_PLATFORM_EMBEDDED) || defined (WESTEROS_HAVE_WAYLAND_EGL)
rendererEMB->glEGLImageTargetTexture2DOES= (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
WST_TRACE( "glEGLImageTargetTexture2DOES %p\n", rendererEMB->glEGLImageTargetTexture2DOES);
#endif
rendererEMB->eglDisplay= eglGetDisplay(EGL_DEFAULT_DISPLAY);
#if defined (WESTEROS_HAVE_WAYLAND_EGL)
const char *extensions= eglQueryString( rendererEMB->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" );
rendererEMB->eglBindWaylandDisplayWL= (PFNEGLBINDWAYLANDDISPLAYWL)eglGetProcAddress("eglBindWaylandDisplayWL");
printf( "eglBindWaylandDisplayWL %p\n", rendererEMB->eglBindWaylandDisplayWL );
rendererEMB->eglUnbindWaylandDisplayWL= (PFNEGLUNBINDWAYLANDDISPLAYWL)eglGetProcAddress("eglUnbindWaylandDisplayWL");
printf( "eglUnbindWaylandDisplayWL %p\n", rendererEMB->eglUnbindWaylandDisplayWL );
rendererEMB->eglQueryWaylandBufferWL= (PFNEGLQUERYWAYLANDBUFFERWL)eglGetProcAddress("eglQueryWaylandBufferWL");
printf( "eglQueryWaylandBufferWL %p\n", rendererEMB->eglQueryWaylandBufferWL );
if ( rendererEMB->eglBindWaylandDisplayWL &&
rendererEMB->eglUnbindWaylandDisplayWL &&
rendererEMB->eglQueryWaylandBufferWL )
{
printf("calling eglBindWaylandDisplayWL with eglDisplay %p and wayland display %p\n", rendererEMB->eglDisplay, renderer->display );
EGLBoolean rc= rendererEMB->eglBindWaylandDisplayWL( rendererEMB->eglDisplay, renderer->display );
if ( rc )
{
rendererEMB->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", rendererEMB->haveWaylandEGL );
#endif
}
exit:
return rendererEMB;
}
static void wstRendererEMBDestroy( WstRendererEMB *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->textureShader )
{
delete renderer->textureShader;
renderer->textureShader= 0;
}
if ( renderer->textureShaderYUV )
{
delete renderer->textureShaderYUV;
renderer->textureShaderYUV= 0;
}
#if defined (WESTEROS_PLATFORM_EMBEDDED)
if ( renderer->glCtx )
{
WstGLTerm( renderer->glCtx );
renderer->glCtx= 0;
}
#endif
if ( renderer->rendererFast )
{
renderer->rendererFast->renderTerm( renderer->rendererFast );
renderer->rendererFast= 0;
}
free( renderer );
}
}
static WstRenderSurface *wstRendererEMBCreateSurface(WstRendererEMB *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;
surface->dirty= true;
if ( renderer->fastPathActive )
{
surface->surfaceFast= renderer->rendererFast->surfaceCreate( renderer->rendererFast );
if ( !surface->surfaceFast )
{
wstRendererDeactivateFastPath( renderer );
}
}
}
return surface;
}
static void wstRendererEMBDestroySurface( WstRendererEMB *renderer, WstRenderSurface *surface )
{
if ( surface )
{
wstRendererEMBFlushSurface( renderer, surface );
if ( renderer->rendererFast )
{
if ( surface->surfaceFast )
{
renderer->rendererFast->surfaceDestroy( renderer->rendererFast, surface->surfaceFast );
surface->surfaceFast= 0;
}
}
free( surface );
}
}
static void wstRendererEMBFlushSurface( WstRendererEMB *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 defined (WESTEROS_PLATFORM_EMBEDDED) || defined (WESTEROS_HAVE_WAYLAND_EGL)
if ( surface->eglImage[i] )
{
renderer->eglDestroyImageKHR( renderer->eglDisplay,
surface->eglImage[i] );
surface->eglImage[i]= 0;
}
#endif
}
#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 wstRendererEMBCommitShm( WstRendererEMB *renderer, 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 ( renderer->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 ( renderer->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 ( renderer->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 ( renderer->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;
}
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 wstRendererEMBCommitWaylandEGL( WstRendererEMB *renderer, WstRenderSurface *surface,
struct wl_resource *resource, EGLint format )
{
EGLImageKHR eglImage= 0;
EGLint value;
EGLint attrList[3];
int bufferWidth= 0, bufferHeight= 0;
if (EGL_TRUE == renderer->eglQueryWaylandBufferWL( renderer->eglDisplay,
resource,
EGL_WIDTH,
&value ) )
{
bufferWidth= value;
}
if (EGL_TRUE == renderer->eglQueryWaylandBufferWL( renderer->eglDisplay,
resource,
EGL_HEIGHT,
&value ) )
{
bufferHeight= value;
}
#if defined (WESTEROS_PLATFORM_RPI)
/*
* The Userland wayland-egl implementation used on RPI isn't complete in that it does not
* support the use of eglCreateImageKHR using the wl_buffer resource and target EGL_WAYLAND_BUFFER_WL.
* For that reason we need to supply a different method for handling buffers received via
* wayland-egl on RPI
*/
{
DISPMANX_RESOURCE_HANDLE_T dispResource= vc_dispmanx_get_handle_from_wl_buffer(resource);
if ( dispResource != DISPMANX_NO_HANDLE )
{
wstRendererEMBCommitDispmanx( renderer, surface, dispResource, format, bufferWidth, bufferHeight );
}
}
#else
if ( (surface->bufferWidth != bufferWidth) || (surface->bufferHeight != bufferHeight) )
{
surface->bufferWidth= bufferWidth;
surface->bufferHeight= bufferHeight;
}
for( int i= 0; i < MAX_TEXTURES; ++i )
{
if ( surface->eglImage[i] )
{
renderer->eglDestroyImageKHR( renderer->eglDisplay,
surface->eglImage[i] );
surface->eglImage[i]= 0;
}
}
switch ( format )
{
case EGL_TEXTURE_RGB:
case EGL_TEXTURE_RGBA:
eglImage= renderer->eglCreateImageKHR( renderer->eglDisplay,
EGL_NO_CONTEXT,
EGL_WAYLAND_BUFFER_WL,
resource,
NULL // EGLInt attrList[]
);
if ( eglImage )
{
/*
* We have a new eglImage. Mark the surface as having no texture to
* trigger texture creation during the next scene render
*/
surface->eglImage[0]= eglImage;
if ( surface->textureId[0] != GL_NONE )
{
glDeleteTextures( 1, &surface->textureId[0] );
}
surface->textureId[0]= GL_NONE;
surface->textureCount= 1;
}
break;
case EGL_TEXTURE_Y_U_V_WL:
printf("wstRendererEMBCommitWaylandEGL: EGL_TEXTURE_Y_U_V_WL not supported\n" );
break;
case EGL_TEXTURE_Y_UV_WL:
attrList[0]= EGL_WAYLAND_PLANE_WL;
attrList[2]= EGL_NONE;
for( int i= 0; i < 2; ++i )
{
attrList[1]= i;
eglImage= renderer->eglCreateImageKHR( renderer->eglDisplay,
EGL_NO_CONTEXT,
EGL_WAYLAND_BUFFER_WL,
resource,
attrList
);
if ( eglImage )
{
surface->eglImage[i]= eglImage;
if ( surface->textureId[i] != GL_NONE )