tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
/******************************************************************************
 
 @File         KEGL/PVRShellAPI.cpp
 
 @Title        KEGL/PVRShellAPI
 
 @Version      
 
 @Copyright    Copyright (c) Imagination Technologies Limited.
 
 @Platform     Independent
 
 @Description  Makes programming for 3D APIs easier by wrapping surface
               initialization, Texture allocation and other functions for use by a demo.
 
******************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
 
#include "PVRShell.h"
#include "PVRShellAPI.h"
#include "PVRShellOS.h"
#include "PVRShellImpl.h"
 
// No Doxygen for CPP files, due to documentation duplication
/// @cond NO_DOXYGEN
 
#ifndef EGL_CONTEXT_LOST_IMG
/*! Extended error code EGL_CONTEXT_LOST_IMG generated when power management event has occurred. */
#define EGL_CONTEXT_LOST_IMG        0x300E
#endif
 
#ifndef EGL_CONTEXT_PRIORITY_LEVEL_IMG
/*! An extensions added to the list of attributes for the context to give it a priority hint */
#define EGL_CONTEXT_PRIORITY_LEVEL_IMG        0x3100
/*! Request the context is created with high priority */
#define EGL_CONTEXT_PRIORITY_HIGH_IMG        0x3101
/*! Request the context is created with medium priority */
#define EGL_CONTEXT_PRIORITY_MEDIUM_IMG        0x3102
/*! Request the context is created with low priority */
#define EGL_CONTEXT_PRIORITY_LOW_IMG        0x3103
#endif
 
/*****************************************************************************
   Declarations
*****************************************************************************/
static bool PVRShellIsExtensionSupported(EGLDisplay dpy, const char *extension);
 
#if defined GL_ES_VERSION_2_0 && !defined EGL_VERSION_1_3
#error OpenGL ES 2 requires egl.h version 1.3 or higher
#endif
 
/****************************************************************************
** Class: PVRShellInitAPI
****************************************************************************/
 
/*****************************************************************************
* Function Name  : ActivatePreferences
* Description    : Activates the user set preferences (like v-sync)
*****************************************************************************/
void PVRShellInit::ApiActivatePreferences()
{
 
#ifdef EGL_VERSION_1_1
   eglSwapInterval(m_EGLDisplay, m_pShell->m_pShellData->nSwapInterval);
#endif
}
 
/*****************************************************************************
* Function Name  : ApiInitAPI
* Returns        : true for success
* Description    : Initialise the 3D API
*****************************************************************************/
bool PVRShellInit::ApiInitAPI()
{
   int                    bDone;
 
   m_NDT = (EGLNativeDisplayType)OsGetNativeDisplayType();
   m_NPT = (EGLNativePixmapType) OsGetNativePixmapType();
   m_NWT = (EGLNativeWindowType) OsGetNativeWindowType();
 
   m_EGLContext = 0;
 
   do
   {
       bDone = true;
 
       m_EGLDisplay = eglGetDisplay(m_NDT);
 
       if(m_EGLDisplay == EGL_NO_DISPLAY)
       {
#if defined(BUILD_OGLES2) || defined(BUILD_OGLES3)
           m_EGLDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY);
#else
           m_EGLDisplay = eglGetDisplay((NativeDisplayType)EGL_DEFAULT_DISPLAY);
#endif
       }
 
       if(!eglInitialize(m_EGLDisplay, &m_MajorVersion, &m_MinorVersion))
       {
           m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to initialise EGL\n");
           m_pShell->PVRShellOutputDebug("PVRShell: EGL Error (%s)\n", StringFrom_eglGetError());
           return false;
       }
 
       m_pShell->PVRShellOutputDebug("PVRShell: EGL %d.%d initialized\n", m_MajorVersion, m_MinorVersion);
 
       // Check Extension availability after EGL initialization
       if (m_MajorVersion > 1 || (m_MajorVersion == 1 && m_MinorVersion >= 1))
       {
           m_bPowerManagementSupported = true;
       }
       else
       {
           m_bPowerManagementSupported = PVRShellIsExtensionSupported(m_EGLDisplay,"EGL_IMG_power_management");
       }
 
       do
       {
#if defined(BUILD_OGL)
           if(!eglBindAPI(EGL_OPENGL_API))
           {
               m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to bind OpenGL API\n");
               return false;
           }
#else
#if defined EGL_VERSION_1_3 && defined GL_ES_VERSION_2_0
           if(!eglBindAPI(EGL_OPENGL_ES_API))
           {
               m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to bind OpenGL ES API\n");
               return false;
           }
#endif
#endif
           // Find an EGL config
           m_EGLConfig = SelectEGLConfiguration(m_pShell->m_pShellData);
           eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_CONFIG_ID, &m_iConfig);
 
           // Destroy the context if we already created one
           if (m_EGLContext)
           {
               eglDestroyContext(m_EGLDisplay, m_EGLContext);
           }
 
           // Attempt to create a context
           EGLint ai32ContextAttribs[48];
           int    i = 0;
 
#if defined(BUILD_OGLES3)
           ai32ContextAttribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
           ai32ContextAttribs[i++] = 3;
#else
#if defined(EGL_VERSION_1_3) && defined(GL_ES_VERSION_2_0)
           ai32ContextAttribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
           ai32ContextAttribs[i++] = 2;
#endif
#endif
 
#if defined(BUILD_OGL)
           //Attempt to create an OpenGL 3.2 context.
           if (PVRShellIsExtensionSupported(m_EGLDisplay, "EGL_KHR_create_context"))
           {
               ai32ContextAttribs[i++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
               ai32ContextAttribs[i++] = 3;
               ai32ContextAttribs[i++] = EGL_CONTEXT_MINOR_VERSION_KHR;
               ai32ContextAttribs[i++] = 2;
               ai32ContextAttribs[i++] = EGL_CONTEXT_FLAGS_KHR;
               ai32ContextAttribs[i++] = EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;
               ai32ContextAttribs[i++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
               ai32ContextAttribs[i++] = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
           }
#endif
 
#if defined(BUILD_OGLES) || defined(BUILD_OGLES2) || defined(BUILD_OGLES3)
           if(PVRShellIsExtensionSupported(m_EGLDisplay,"EGL_IMG_context_priority"))
           {
               ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_LEVEL_IMG;
               switch(m_pShell->PVRShellGet(prefPriority))
               {
                   case 0: ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_LOW_IMG; break;
                   case 1: ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_MEDIUM_IMG; break;
                   default:ai32ContextAttribs[i++] = EGL_CONTEXT_PRIORITY_HIGH_IMG; break;
               }
           }
#endif
           ai32ContextAttribs[i] = EGL_NONE;
 
           if (m_EGLContext == EGL_NO_CONTEXT)
           {
               m_EGLContext = eglCreateContext(m_EGLDisplay, m_EGLConfig, NULL, ai32ContextAttribs);
           }
 
           if(m_EGLContext == EGL_NO_CONTEXT)
           {
               if(m_iRequestedConfig > 0)
               {
                   // We failed to create a context
                   m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to create a context\n");
                   return false;
               }
               else if(m_pShell->m_pShellData->bNeedPbuffer)
               {
                   // Disable P-buffer and try again
                   m_pShell->m_pShellData->bNeedPbuffer = false;
               }
               else if(m_pShell->m_pShellData->bNeedStencilBuffer)
               {
                    // Disable Stencil Buffer and try again
                   m_pShell->m_pShellData->bNeedStencilBuffer = false;
               }
               else if(m_pShell->m_pShellData->nAASamples > 0)
               {
                   // Still failing, reduce the AA samples and try again
                   --m_pShell->m_pShellData->nAASamples;
               }
               else
               {
                   m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to create a context\n");
                   return false;
               }
           }
       } while(m_EGLContext == EGL_NO_CONTEXT);
 
#if defined(__QNXNTO__)
       int format = SCREEN_FORMAT_RGBX8888;
       if(screen_set_window_property_iv((_screen_window*) m_NWT, SCREEN_PROPERTY_FORMAT, &format))
       {
           m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to set window property SCREEN_PROPERTY_FORMAT\n");
           return false;
       }
 
#if defined(BUILD_OGLES2)
       int usage = SCREEN_USAGE_OPENGL_ES2;
#else
#if defined(BUILD_OGLES)
       int usage = SCREEN_USAGE_OPENGL_ES1;
#endif
#endif
       if(screen_set_window_property_iv((_screen_window*) m_NWT, SCREEN_PROPERTY_USAGE, &usage))
       {
           m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to set window property SCREEN_PROPERTY_USAGE\n");
           return false;
       }
 
       if(screen_create_window_buffers((_screen_window*) m_NWT, 2))
       {
           m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Failed to create window buffers\n");
           return false;
       }
#endif
       EGLint        attrib_list[16];
       int    i = 0;
#if defined(EGL_VERSION_1_2)
       if(m_pShell->m_pShellData->bNeedAlphaFormatPre) // The default is EGL_ALPHA_FORMAT_NONPRE
       {
           attrib_list[i++] = EGL_ALPHA_FORMAT;
           attrib_list[i++] = EGL_ALPHA_FORMAT_PRE;
       }
#endif
       // Terminate the attribute list with EGL_NONE
       attrib_list[i] = EGL_NONE;
 
       if(m_pShell->m_pShellData->bNeedPixmap)
       {
           m_pShell->PVRShellOutputDebug("InitAPI() Using pixmaps, about to create egl surface\n");
           m_EGLWindow = eglCreatePixmapSurface(m_EGLDisplay, m_EGLConfig, m_NPT, attrib_list);
       }
       else
       {
#if defined(ANDROID)
           EGLint visualID;
           eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_NATIVE_VISUAL_ID, &visualID);
 
           // Change the format of our window to match our config
            ANativeWindow_setBuffersGeometry(m_NWT, 0, 0, visualID);
#endif
           m_EGLWindow = eglCreateWindowSurface(m_EGLDisplay, m_EGLConfig, m_NWT, attrib_list);
 
            // If we have failed to create a surface then try using Null
           if(m_EGLWindow == EGL_NO_SURFACE)
           {
               m_EGLWindow = eglCreateWindowSurface(m_EGLDisplay, m_EGLConfig, NULL, attrib_list);
           }
       }
 
       if (m_EGLWindow == EGL_NO_SURFACE)
       {
           m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to create surface\n");
           return false;
       }
 
       if (!eglMakeCurrent(m_EGLDisplay, m_EGLWindow, m_EGLWindow, m_EGLContext))
       {
#ifdef EGL_VERSION_1_3
           if((eglGetError() == EGL_CONTEXT_LOST))
#else
           if((eglGetError() == EGL_CONTEXT_LOST_IMG) && m_bPowerManagementSupported)
#endif
           {
               bDone = false;
           }
           else
           {
               m_pShell->PVRShellSet(prefExitMessage, "PVRShell: Unable to make context current\n");
               return false;
           }
       }
   } while(!bDone);
 
   /*
        Get correct screen width and height and
       save them into
       m_pShell->m_pShellData->nShellDimX and
       m_pShell->m_pShellData->nShellDimY
   */
   eglQuerySurface(m_EGLDisplay, m_EGLWindow,
           EGL_WIDTH,  (EGLint*)&m_pShell->m_pShellData->nShellDimX
       );
   eglQuerySurface(m_EGLDisplay, m_EGLWindow,
           EGL_HEIGHT, (EGLint*)&m_pShell->m_pShellData->nShellDimY
       );
 
#if defined(ANDROID)
   glViewport(0, 0, m_pShell->m_pShellData->nShellDimX, m_pShell->m_pShellData->nShellDimY);
#endif
   /*
       Done - activate requested features
   */
 
#if defined(BUILD_OGLES) || defined(BUILD_OGLES2)
   //Get the discardframebufferEXT function.
   {
       //Get the gl extension string
       const char* strExtensions = (const char*)glGetString(GL_EXTENSIONS);
 
       //Get the length of the string we're searching for
       const size_t strLength = strlen("GL_EXT_discard_framebuffer");
 
       //Get the string position
       const char* position = strstr(strExtensions,"GL_EXT_discard_framebuffer");
 
       //Loop through until we find the actual extension, avoiding substrings.
       while (position!=NULL && position[strLength]!='\0' && position[strLength]!=' ')
       {
           position = strstr(position+strLength,"GL_EXT_discard_framebuffer");
       }
 
       //Initialise the extension if it's found.
       if (position != NULL)
       {
           glDiscardFramebufferEXT = (PFNGLDISCARDFRAMEBUFFEREXTPROC)eglGetProcAddress("glDiscardFramebufferEXT");
       }
       else
       {
           glDiscardFramebufferEXT = NULL;
       }
   }
#endif
   ApiActivatePreferences();
   return true;
}
 
/*!***********************************************************************
@Function        OutputAPIInfo
@description    When prefOutputInfo is set to true this function outputs
               various pieces of API dependent information via
               PVRShellOutputDebug.
*************************************************************************/
void PVRShellInit::OutputAPIInfo()
{
   // Output API dependent information
   if(m_pShell->PVRShellGet(prefOutputInfo))
   {
       EGLint i32Values[5];
 
       m_pShell->PVRShellOutputDebug("\n");
 
       m_pShell->PVRShellOutputDebug("GL:\n");
       m_pShell->PVRShellOutputDebug("  Vendor:   %s\n", (char*) glGetString(GL_VENDOR));
       m_pShell->PVRShellOutputDebug("  Renderer: %s\n", (char*) glGetString(GL_RENDERER));
       m_pShell->PVRShellOutputDebug("  Version:  %s\n", (char*) glGetString(GL_VERSION));
       m_pShell->PVRShellOutputDebug("  Extensions:  ");
 
#if defined(BUILD_OGL)
       //Get the glGetString process.
       typedef const GLubyte* (KHRONOS_APIENTRY * PFNGLGETSTRINGIPROC)(GLenum name, GLuint index);
       PFNGLGETSTRINGIPROC glGetStringi = (PFNGLGETSTRINGIPROC)eglGetProcAddress("glGetStringi");
 
       //If we've successfully got the new way to query the string, then go ahead and use this.
       if (glGetStringi)
       {
#ifndef GL_NUM_EXTENSIONS
#define GL_NUM_EXTENSIONS                 0x821D
#endif
           GLint numExtensions;
           glGetIntegerv(GL_NUM_EXTENSIONS,&numExtensions);
 
           for (GLint i=0; i<numExtensions; ++i)
           {
               m_pShell->PVRShellOutputDebug((const char*)glGetStringi(GL_EXTENSIONS,i));
               m_pShell->PVRShellOutputDebug(" ");
           }
       }
#else
       m_pShell->PVRShellOutputDebug("%s\n", (char*) glGetString(GL_EXTENSIONS));
#endif
 
       m_pShell->PVRShellOutputDebug("\n");
 
       m_pShell->PVRShellOutputDebug("\n");
       m_pShell->PVRShellOutputDebug("EGL:\n");
       m_pShell->PVRShellOutputDebug("  Vendor:   %s\n" , (char*) eglQueryString(m_EGLDisplay, EGL_VENDOR));
       m_pShell->PVRShellOutputDebug("  Version:  %s\n" , (char*) eglQueryString(m_EGLDisplay, EGL_VERSION));
       m_pShell->PVRShellOutputDebug("  Extensions:  %s\n" , (char*) eglQueryString(m_EGLDisplay, EGL_EXTENSIONS));
 
       if(eglQueryContext(m_EGLDisplay, m_EGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &i32Values[0]))
       {
           switch(i32Values[0])
           {
               case EGL_CONTEXT_PRIORITY_HIGH_IMG:   m_pShell->PVRShellOutputDebug("  Context priority: High\n");  break;
               case EGL_CONTEXT_PRIORITY_MEDIUM_IMG: m_pShell->PVRShellOutputDebug("  Context priority: Medium\n");break;
               case EGL_CONTEXT_PRIORITY_LOW_IMG:    m_pShell->PVRShellOutputDebug("  Context priority: Low\n");   break;
               default: m_pShell->PVRShellOutputDebug("  Context priority: Unrecognised.\n"); break;
           }
       }
       else
       {
           eglGetError(); // Clear error
           m_pShell->PVRShellOutputDebug("  Context priority: Unsupported\n");
       }
 
#ifdef EGL_VERSION_1_2
       m_pShell->PVRShellOutputDebug("  Client APIs:  %s\n" , (char*) eglQueryString(m_EGLDisplay, EGL_CLIENT_APIS));
#endif
 
       m_pShell->PVRShellOutputDebug("\n");
       m_pShell->PVRShellOutputDebug("Window Width:  %i\n" , m_pShell->PVRShellGet(prefWidth));
       m_pShell->PVRShellOutputDebug("Window Height: %i\n" , m_pShell->PVRShellGet(prefHeight));
       m_pShell->PVRShellOutputDebug("Is Rotated: %s\n", m_pShell->PVRShellGet(prefIsRotated) ? "Yes" : "No");
       m_pShell->PVRShellOutputDebug("\n");
 
       // EGLSurface details
       m_pShell->PVRShellOutputDebug("EGL Surface:\n");
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_CONFIG_ID , &i32Values[0]);
       m_pShell->PVRShellOutputDebug("  Config ID:      %i\n", i32Values[0]);
 
       // Colour buffer
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_BUFFER_SIZE , &i32Values[0]);
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_RED_SIZE    , &i32Values[1]);
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_GREEN_SIZE  , &i32Values[2]);
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_BLUE_SIZE   , &i32Values[3]);
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_ALPHA_SIZE  , &i32Values[4]);
       m_pShell->PVRShellOutputDebug("  Colour Buffer:  %i bits (R%i G%i B%i A%i)\n", i32Values[0],i32Values[1],i32Values[2],i32Values[3],i32Values[4]);
 
       // Depth buffer
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_DEPTH_SIZE , &i32Values[0]);
       m_pShell->PVRShellOutputDebug("  Depth Buffer:   %i bits\n", i32Values[0]);
 
       // Stencil Buffer
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_STENCIL_SIZE , &i32Values[0]);
       m_pShell->PVRShellOutputDebug("  Stencil Buffer: %i bits\n", i32Values[0]);
 
       // EGL surface bits support
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_SURFACE_TYPE , &i32Values[0]);
       m_pShell->PVRShellOutputDebug("  Surface type:   %s%s%s\n",    i32Values[0] & EGL_WINDOW_BIT  ? "WINDOW " : "",
                                                                       i32Values[1] & EGL_PBUFFER_BIT ? "PBUFFER " : "",
                                                                       i32Values[2] & EGL_PIXMAP_BIT  ? "PIXMAP " : "");
       // EGL renderable type
#ifdef EGL_VERSION_1_2
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_RENDERABLE_TYPE , &i32Values[0]);
       m_pShell->PVRShellOutputDebug("  Renderable type: %s%s%s%s\n", i32Values[0] & EGL_OPENVG_BIT ? "OPENVG " : "",
                                                           i32Values[0] & EGL_OPENGL_ES_BIT ? "OPENGL_ES " : "",
#ifdef EGL_OPENGL_BIT
                                                           i32Values[0] & EGL_OPENGL_BIT ? "OPENGL " :
#endif
                                                           "",
                                                           i32Values[0] & EGL_OPENGL_ES2_BIT ? "OPENGL_ES2 " : "");
#endif
 
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_SAMPLE_BUFFERS , &i32Values[0]);
       eglGetConfigAttrib(m_EGLDisplay, m_EGLConfig, EGL_SAMPLES , &i32Values[1]);
       m_pShell->PVRShellOutputDebug("  Sample buffer No.: %i\n", i32Values[0]);
       m_pShell->PVRShellOutputDebug("  Samples per pixel: %i\n", i32Values[1]);
   }
}
 
/*!***********************************************************************
 @Function        ApiReleaseAPI
 @description    Releases all resources allocated by the API.
*************************************************************************/
void PVRShellInit::ApiReleaseAPI()
{
   eglSwapBuffers(m_EGLDisplay, m_EGLWindow);
   eglMakeCurrent(m_EGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
     eglDestroyContext(m_EGLDisplay, m_EGLContext);
   eglDestroySurface(m_EGLDisplay, m_EGLWindow);
       eglTerminate(m_EGLDisplay);
}
 
/*******************************************************************************
 * Function Name  : SelectEGLConfiguration
 * Inputs          : pData
 * Returns        : EGLConfig
 * Description    : Find the config to use for EGL initialisation
 *******************************************************************************/
EGLConfig PVRShellInitAPI::SelectEGLConfiguration(const PVRShellData * const pData)
{
    EGLint        num_config;
    EGLint        conflist[32];
   EGLConfig    conf = (EGLConfig) 0;
    int            i = 0;
 
   // Specific config ID requested?
   if (m_iRequestedConfig > 0)
   {
       conflist[i++] = EGL_CONFIG_ID;
       conflist[i++] = m_iRequestedConfig;
       conflist[i++] = EGL_NONE;
       if(!eglChooseConfig(m_EGLDisplay, conflist, &conf, 1, &num_config) || num_config != 1)
       {
           return 0;
       }
       return conf;
   }
 
   // Select default configuration
#if defined(ANDROID)
   if(pData->nColorBPP == 32)
   {
       conflist[i++] = EGL_RED_SIZE;
       conflist[i++] = 8;
       conflist[i++] = EGL_GREEN_SIZE;
       conflist[i++] = 8;
       conflist[i++] = EGL_BLUE_SIZE;
       conflist[i++] = 8;
       conflist[i++] = EGL_ALPHA_SIZE;
       conflist[i++] = 8;
   }
   else
   {
       conflist[i++] = EGL_RED_SIZE;
       conflist[i++] = 5;
       conflist[i++] = EGL_GREEN_SIZE;
       conflist[i++] = 6;
       conflist[i++] = EGL_BLUE_SIZE;
       conflist[i++] = 5;
       conflist[i++] = EGL_ALPHA_SIZE;
       conflist[i++] = 0;
   }
#else
   conflist[i++] = EGL_BUFFER_SIZE;
   conflist[i++] = pData->nColorBPP;
#endif
 
   if(pData->bNeedZbuffer || pData->nDepthBPP > 0)
   {
       conflist[i++] = EGL_DEPTH_SIZE;
       conflist[i++] = (pData->nDepthBPP > 0) ? pData->nDepthBPP : 16;
   }
 
   if(pData->bNeedStencilBuffer)
   {
       conflist[i++] = EGL_STENCIL_SIZE;
       conflist[i++] = 8;
   }
 
   conflist[i++] = EGL_SURFACE_TYPE;
   conflist[i] = EGL_WINDOW_BIT;
 
   if(pData->bNeedPbuffer)
   {
       conflist[i] |= EGL_PBUFFER_BIT;
   }
 
   if(pData->bNeedPixmap)
   {
       conflist[i] |= EGL_PIXMAP_BIT;
   }
 
   ++i;
 
#if defined(BUILD_OGL)
   conflist[i++] = EGL_RENDERABLE_TYPE;
   conflist[i++] = EGL_OPENGL_BIT;
#elif defined(EGL_VERSION_1_3) && defined(GL_ES_VERSION_2_0)
   conflist[i++] = EGL_RENDERABLE_TYPE;
   conflist[i++] = EGL_OPENGL_ES2_BIT;
#endif
 
   // Append number of number of samples depending on AA samples value set
   if(pData->nAASamples > 0)
   {
       conflist[i++] = EGL_SAMPLE_BUFFERS;
       conflist[i++] = 1;
       conflist[i++] = EGL_SAMPLES;
       conflist[i++] = pData->nAASamples;
   }
   else
   {
       conflist[i++] = EGL_SAMPLE_BUFFERS;
       conflist[i++] = 0;
   }
 
#if defined(EWS) || defined(__QNXNTO__)
   if(m_NWT != NULL)
   {
       EGLint r,g,b,a, value;
       EGLint i32Total_num_configs, j;
       EGLConfig    *pConfigs;
 
       // Some platforms require an egl config to have the same pixel format as the native window because
       // pixel format conversion is prohibited.
#if defined(EWS)
       int format = EWS_PIXEL_FORMAT_RGB_565;
       r = 5; g = 6; b = 5; a = 0;
#else
       r = g = b = a = 8;
#endif
 
       conflist[i++] = EGL_RED_SIZE;
       conflist[i++] = r;
 
       conflist[i++] = EGL_GREEN_SIZE;
       conflist[i++] = g;
 
       conflist[i++] = EGL_BLUE_SIZE;
       conflist[i++] = b;
 
       conflist[i++] = EGL_ALPHA_SIZE;
       conflist[i++] = a;
 
       // Terminate the list with EGL_NONE
       conflist[i++] = EGL_NONE;
 
       // Find out how many configs there are in total that match our criteria
       if(!eglChooseConfig(m_EGLDisplay, conflist, NULL, 0, &i32Total_num_configs) || i32Total_num_configs == 0)
           return 0;
 
       // Allocate an array large enough to store all the possible configs that may be returned
       pConfigs = new EGLConfig[i32Total_num_configs];
 
       if(!pConfigs)
           return 0;
 
       // Get all the configs that match our criteria
       if(!eglChooseConfig(m_EGLDisplay, conflist, pConfigs, i32Total_num_configs, &num_config))
       {
           delete[] pConfigs;
           return 0;
       }
 
       // Go through the returned configs and try and find a suitable match
       for(j = 0; j < num_config; ++j)
       {
#if defined(__QNXNTO__)
           if((eglGetConfigAttrib(m_EGLDisplay, pConfigs[j], EGL_RED_SIZE,   &value) && value == r)
           && (eglGetConfigAttrib(m_EGLDisplay, pConfigs[j], EGL_GREEN_SIZE, &value) && value == g)
           && (eglGetConfigAttrib(m_EGLDisplay, pConfigs[j], EGL_BLUE_SIZE,  &value) && value == b)
           && (eglGetConfigAttrib(m_EGLDisplay, pConfigs[j], EGL_ALPHA_SIZE, &value) && value == a))
           {
               conf = pConfigs[j];
               break;
           }
#else
#if defined (EWS)
           eglGetConfigAttrib(m_EGLDisplay, pConfigs[j], EGL_NATIVE_VISUAL_ID, &value);
           if (value == format)
           {
               conf = pConfigs[j];
               break;
           }
#endif
#endif
       }
 
       // Tidy up
       delete[] pConfigs;
   }
   else
#endif
   {
       // Terminate the list with EGL_NONE
       conflist[i++] = EGL_NONE;
 
       // Return null config if config is not found
       if(!eglChooseConfig(m_EGLDisplay, conflist, &conf, 1, &num_config) || num_config != 1)
       {
           return 0;
       }
   }
 
   // Return config index
   return conf;
}
 
/*******************************************************************************
 * Function Name  : StringFrom_eglGetError
 * Returns        : A string
 * Description    : Returns a string representation of an egl error
 *******************************************************************************/
const char *PVRShellInitAPI::StringFrom_eglGetError() const
{
   EGLint nErr = eglGetError();
 
   switch(nErr)
   {
       case EGL_SUCCESS:
           return "EGL_SUCCESS";
       case EGL_BAD_DISPLAY:
           return "EGL_BAD_DISPLAY";
       case EGL_NOT_INITIALIZED:
           return "EGL_NOT_INITIALIZED";
       case EGL_BAD_ACCESS:
           return "EGL_BAD_ACCESS";
       case EGL_BAD_ALLOC:
           return "EGL_BAD_ALLOC";
       case EGL_BAD_ATTRIBUTE:
           return "EGL_BAD_ATTRIBUTE";
       case EGL_BAD_CONFIG:
           return "EGL_BAD_CONFIG";
       case EGL_BAD_CONTEXT:
           return "EGL_BAD_CONTEXT";
       case EGL_BAD_CURRENT_SURFACE:
           return "EGL_BAD_CURRENT_SURFACE";
       case EGL_BAD_MATCH:
           return "EGL_BAD_MATCH";
       case EGL_BAD_NATIVE_PIXMAP:
           return "EGL_BAD_NATIVE_PIXMAP";
       case EGL_BAD_NATIVE_WINDOW:
           return "EGL_BAD_NATIVE_WINDOW";
       case EGL_BAD_PARAMETER:
           return "EGL_BAD_PARAMETER";
       case EGL_BAD_SURFACE:
           return "EGL_BAD_SURFACE";
       default:
           return "unknown";
   }
}
 
/*!***********************************************************************
@Function        ApiScreenCaptureBuffer
@Input            Width Width of the region to capture
@Input            Height Height of the region to capture
@Input            pBuf A buffer to put the screen capture into
@description    API-specific function to store the current content of the
               FrameBuffer into the memory allocated by the user.
*************************************************************************/
bool PVRShellInit::ApiScreenCaptureBuffer(int Width,int Height,unsigned char *pBuf)
{
   unsigned char    *pLines2;
   int                i, j;
   bool            bRet = true;
 
   /* Allocate memory for line */
   pLines2 = (unsigned char *)calloc(4 * Width * Height, sizeof(unsigned char));
   if (!pLines2) return false;
 
   while (glGetError());
   /* Read line from frame buffer */
   glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pLines2);
 
   if(glGetError())
   {
       bRet = false;
   }
   else
   {
       /* Convert RGB to BGR in line */
       for (j = 0, i = 0; j < 4 * Width * Height; j += 4, i += 3)
       {
           pBuf[i] = pLines2[j+2];
           pBuf[i+1] = pLines2[j+1];
           pBuf[i+2] = pLines2[j];
       }
   }
 
   free(pLines2);
   return bRet;
}
 
/*!***********************************************************************
 @Function        ApiRenderComplete
 @description    Perform API operations required after a frame has finished (e.g., flipping).
*************************************************************************/
void PVRShellInit::ApiRenderComplete()
{
 
#if defined(BUILD_OGLES) || defined(BUILD_OGLES2) || defined(BUILD_OGLES3)
   //Discard the framebuffer if set.
#if !defined(BUILD_OGLES3)
   if (glDiscardFramebufferEXT)
#endif
   {
       const GLint numAttachments=3;
       GLenum attachments[numAttachments];
       GLint currentAttachment=0;
       if (m_pShell->PVRShellGet(prefDiscardColor))
       {
           attachments[currentAttachment] = GL_COLOR_EXT;
           currentAttachment++;
       }
       if (m_pShell->PVRShellGet(prefDiscardDepth))
       {
           attachments[currentAttachment] = GL_DEPTH_EXT;
           currentAttachment++;
       }
       if (m_pShell->PVRShellGet(prefDiscardStencil))
       {
           attachments[currentAttachment] = GL_STENCIL_EXT;
           currentAttachment++;
       }
       //Assuming some attachments have been chosen, discard/invalidate them.
       if (currentAttachment!=0)
       {
#if defined(BUILD_OGLES)
           glDiscardFramebufferEXT(GL_FRAMEBUFFER_OES, currentAttachment, attachments);
#elif defined(BUILD_OGLES2)
           glDiscardFramebufferEXT(GL_FRAMEBUFFER, currentAttachment, attachments);
#elif defined(BUILD_OGLES3)
           glInvalidateFramebuffer(GL_FRAMEBUFFER, currentAttachment, attachments);
#endif
       }
   }
#endif
 
   bool bRes;
 
   if(m_pShell->m_pShellData->bNeedPixmap)
   {
       /*
           "Clients rendering to single buffered surfaces (e.g. pixmap surfaces)
           should call eglWaitGL before accessing the native pixmap from the client."
       */
       eglWaitGL();
 
       // Pixmap support: Copy the rendered pixmap to the display
       if(m_pShell->m_pShellData->bNeedPixmapDisableCopy)
       {
           bRes = true;
       }
       else
       {
           bRes = OsPixmapCopy();
       }
   }
   else
   {
       if(m_pShell->m_pShellData->bNoShellSwapBuffer)
           return;
 
       bRes = (eglSwapBuffers (m_EGLDisplay, m_EGLWindow) == EGL_TRUE);
   }
 
   if(!bRes)
   {
       // check for context loss
#ifdef EGL_VERSION_1_3
       if(eglGetError() == EGL_CONTEXT_LOST)
#else
       if((eglGetError() == EGL_CONTEXT_LOST_IMG) && m_bPowerManagementSupported)
#endif
       {
           m_pShell->ReleaseView();
 
           OsDoReleaseAPI();
           if(ApiInitAPI())
           {
               m_pShell->InitView();
           }
       }
       else
       {
           if(m_pShell->m_pShellData->bNeedPixmap)
               m_pShell->PVRShellOutputDebug("failed to copy pixmap\n");
           else
               m_pShell->PVRShellOutputDebug("eglSwapBuffers failed\n");
       }
   }
}
 
/*!***********************************************************************
 @Function        ApiSet
 @Input            prefName    Name of value to set
 @Modified        i32Value    Value to set it to
 @description    Set parameters which are specific to the API.
*************************************************************************/
bool PVRShellInit::ApiSet(const prefNameIntEnum prefName, const int i32Value)
{
   switch(prefName)
   {
#ifdef EGL_VERSION_1_1
   case prefSwapInterval:
       m_pShell->m_pShellData->nSwapInterval = i32Value;
       return true;
#endif
#if defined(BUILD_OGLES) || defined(BUILD_OGLES2) || defined(BUILD_OGLES3)
   case prefPriority:
       m_pShell->m_pShellData->nPriority = i32Value;
       return true;
#endif
   case prefRequestedConfig:
       m_iRequestedConfig = (EGLint) i32Value;
       return true;
 
   default:
       return false;
   }
}
 
/*!***********************************************************************
 @Function        ApiGet
 @Input            prefName    Name of value to get
 @Modified        pn A pointer set to the value asked for
 @description    Get parameters which are specific to the API.
*************************************************************************/
bool PVRShellInit::ApiGet(const prefNameIntEnum prefName, int *pn)
{
   switch(prefName)
   {
       case prefEGLMajorVersion:
           *pn = (int) m_MajorVersion;
           return true;
 
       case prefEGLMinorVersion:
           *pn = (int) m_MinorVersion;
           return true;
 
       case prefRequestedConfig:
           *pn = (int) m_iRequestedConfig;
           return true;
 
       case prefConfig:
           *pn = (int) m_iConfig;
           return true;
 
       default:
           return false;
   }
}
 
/*!***********************************************************************
 @Function        ApiGet
 @Input            prefName    Name of value to get
 @Modified        pp A pointer set to the value asked for
 @description    Get parameters which are specific to the API.
*************************************************************************/
bool PVRShellInit::ApiGet(const prefNamePtrEnum prefName, void **pp)
{
       switch(prefName)
       {
       case prefEGLDisplay:
           *pp = (void*)m_EGLDisplay;
           return true;
       case prefEGLSurface:
           *pp = (void*)m_EGLWindow;
           return true;
       default:
           return false;
       }
}
 
/****************************************************************************
** Local code
****************************************************************************/
 
// The recommended technique for querying OpenGL extensions;
// adapted from http://opengl.org/resources/features/OGLextensions/
static bool PVRShellIsExtensionSupported(EGLDisplay dpy, const char *extension)
{
   // The recommended technique for querying EGL extensions matches OpenGLES;
   // from http://opengl.org/resources/features/OGLextensions/
    const char *extensions = NULL;
    const char *start;
    char *terminator;
 
    /* Extension names should not have spaces. */
    char* where = (char *) strchr(extension, ' ');
    if (where || *extension == '\0')
        return 0;
 
    extensions = eglQueryString(dpy, EGL_EXTENSIONS);
   if(!extensions)
       return false;
 
    /* It takes a bit of care to be fool-proof about parsing the
    OpenGL extensions string. Don't be fooled by sub-strings, etc. */
    start = extensions;
    for (;;) {
        where = (char *) strstr((const char *) start, extension);
        if (!where)
            break;
        terminator = where + strlen(extension);
        if (where == start || *(where - 1) == ' ')
            if (*terminator == ' ' || *terminator == '\0')
                return true;
        start = terminator;
    }
    return false;
}
 
/// @endcond
 
/*****************************************************************************
 End of file (PVRShellAPI.cpp)
*****************************************************************************/