Tizen 2.1 base
[sdk/emulator/qemu.git] / tizen / src / hw / gloffscreen_test.c
1 /*
2  *  Offscreen OpenGL abstraction layer
3  *
4  *  Copyright (c) 2010 Intel Corporation
5  *  Written by: 
6  *    Gordon Williams <gordon.williams@collabora.co.uk>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26
27 #include "gloffscreen.h"
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #ifdef _WIN32
34 #include <windows.h>
35 #include <GL/gl.h>
36 #include <GL/glext.h>
37 #elif __APPLE__
38 #include <OpenGL/gl.h>
39 #include <sys/time.h>
40 #else
41 #include <GL/gl.h>
42 #include <sys/time.h>
43 #endif
44
45 #ifdef MANGLE_OPENGL_SYMBOLS
46 #include "gl_mangled.h"
47 #endif
48
49 // ---------------------------------------------------
50 //  Copied from glx.h as we need them in windows too
51 /*
52  * Tokens for glXChooseVisual and glXGetConfig:
53  */
54 #define GLX_USE_GL      1
55 #define GLX_BUFFER_SIZE     2
56 #define GLX_LEVEL       3
57 #define GLX_RGBA        4
58 #define GLX_DOUBLEBUFFER    5
59 #define GLX_STEREO      6
60 #define GLX_AUX_BUFFERS     7
61 #define GLX_RED_SIZE        8
62 #define GLX_GREEN_SIZE      9
63 #define GLX_BLUE_SIZE       10
64 #define GLX_ALPHA_SIZE      11
65 #define GLX_DEPTH_SIZE      12
66 #define GLX_STENCIL_SIZE    13
67 #define GLX_ACCUM_RED_SIZE  14
68 #define GLX_ACCUM_GREEN_SIZE    15
69 #define GLX_ACCUM_BLUE_SIZE 16
70 #define GLX_ACCUM_ALPHA_SIZE    17
71 // ---------------------------------------------------
72
73 #define TX (32)
74 #define TY (32)
75
76 #define CHECK_SUCCESS   0
77 #define CHECK_FAIL              1
78 int gl_acceleration_capability_check (void) {
79     //int test_failure = 0;
80     GloContext *context;
81     GloSurface *surface;
82     unsigned char *datain;
83     unsigned char *datain_flip;
84     unsigned char *dataout;
85     unsigned char *p;
86     int x,y;
87     const int bufferAttributes[] = {
88             GLX_RED_SIZE,      8,
89             GLX_GREEN_SIZE,    8,
90             GLX_BLUE_SIZE,     8,
91             GLX_ALPHA_SIZE,    8,
92             GLX_DEPTH_SIZE,    0,
93             GLX_STENCIL_SIZE,  0,
94             0,
95         };
96
97 #ifdef MANGLE_OPENGL_SYMBOLS
98     if (mgl_load_symbols("libGL.so.1")) {
99                 return CHECK_FAIL;
100     }
101 #endif
102
103     int bufferFlags = glo_flags_get_from_glx(bufferAttributes, 0);
104     int bpp = glo_flags_get_bytes_per_pixel(bufferFlags);
105     int glFormat, glType;
106 /*
107     if (glo_sanity_test () != 0) {
108         // test failed.
109         return 1;
110     }
111 */ 
112         datain = (unsigned char *)malloc(4*TX*TY);
113         datain_flip = (unsigned char *)malloc(4*TX*TY);
114
115     memset(datain, 0, TX*TY*4);
116     memset(datain_flip, 0, TX*TY*4);
117
118     p = datain;
119     for (y=0;y<TY;y++) {
120       for (x=0;x<TX;x++) {
121         p[0] = x;
122         p[1] = y;
123         //if (y&1) { p[0]=0; p[1]=0; }
124         if (bpp>2) p[2] = 0;
125         if (bpp>3) p[3] = 0xFF;
126         p+=bpp;
127       }
128       memcpy(&datain_flip[((TY-1)-y)*bpp*TX], &datain[y*bpp*TX], bpp*TX);
129     }
130
131     if (glo_init() != 0) {
132         printf ("Host does not have GL hardware acceleration!(glo_init() failed)\n");
133                 free (datain);
134                 free (datain_flip);
135                 return CHECK_FAIL;
136     }
137
138     // new surface
139     context = glo_context_create(bufferFlags, 0);
140         if (context == NULL) {
141         printf ("Host does not have GL hardware acceleration!(context_create() failed)\n");
142                 free (datain);
143                 free (datain_flip);
144                 return CHECK_FAIL;
145     }
146
147     surface = glo_surface_create(TX, TY, context);
148         if (surface == NULL) {
149         printf ("Host does not have GL hardware acceleration!(surface_create() failed)\n");
150                 free (datain);
151                 free (datain_flip);
152                 return CHECK_FAIL;
153     }
154
155     glo_surface_makecurrent(surface);
156     printf("GL VENDOR %s\n", glGetString(GL_VENDOR));
157     printf("GL RENDERER %s\n", glGetString(GL_RENDERER));
158     printf("GL VERSION %s\n", glGetString(GL_VERSION));
159     //printf("GLSL VERSION %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
160
161     if (strstr((const char*)glGetString(GL_RENDERER), "Software")) {
162         printf ("Host does not have GL hardware acceleration!(No host gl driver)\n");
163                 free (datain);
164                 free (datain_flip);
165                 return CHECK_FAIL;
166     }
167
168     // fill with stuff (in correctly ordered way)
169     glClear(GL_COLOR_BUFFER_BIT);
170     glMatrixMode(GL_PROJECTION);
171     glLoadIdentity();
172     glOrtho(0,TX, 0,TY, 0, 1);
173     glMatrixMode(GL_MODELVIEW);
174     glLoadIdentity();
175     glRasterPos2f(0,0);
176     glo_flags_get_readpixel_type(bufferFlags, &glFormat, &glType);
177     printf("glFormat: 0x%08X glType: 0x%08X\n", glFormat, glType);
178     glDrawPixels(TX,TY,glFormat, glType, datain_flip);
179     glFlush();
180         
181         dataout = (unsigned char *)malloc(4*TX*TY);
182     memset(dataout, 0, bpp*TX*TY);
183
184     glo_surface_getcontents(surface, TX*bpp, bpp*8, dataout);
185
186     // destroy surface
187     glo_surface_destroy(surface);
188     glo_context_destroy(context);
189     // compare
190     if (memcmp(datain, dataout, bpp*TX*TY)!=0) {
191         printf ("Host does not have GL hardware acceleration!(datain != dataout)\n");
192                 free (datain);
193                 free (datain_flip);
194                 free (dataout);
195                 return CHECK_FAIL;
196     }
197
198     //glo_kill();
199     //printf ("Testing %s\n", (test_failure ? "FAILED" : "PASSED"));
200     free (datain);
201     free (datain_flip);
202     free (dataout);
203
204         return CHECK_SUCCESS;
205 }