Imported Upstream version 0.1.0+git20131207+e452e83
[deb_libhybris.git] / hybris / tests / test_sf.c
CommitLineData
d42e7319
JB
1/*
2 * Copyright (C) 2013 Canonical Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Authored by: Thomas Voss <thomas.voss@canonical.com>
17 * Ricardo Salveti de Araujo <ricardo.salveti@canonical.com>
18 */
19
20#include <unistd.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <limits.h>
24
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <hybris/surface_flinger/surface_flinger_compatibility_layer.h>
29
30struct SfSurface* sf_surface_create(struct SfClient* client, SfSurfaceCreationParameters* params);
31EGLSurface sf_surface_get_egl_surface(struct SfSurface*);
32void sf_surface_make_current(struct SfSurface* surface);
33
34void sf_surface_move_to(struct SfSurface* surface, int x, int y);
35void sf_surface_set_layer(struct SfSurface* surface, int layer);
36void sf_surface_set_alpha(struct SfSurface* surface, float alpha);
37
38double rotation_increment = 0.01;
39
40/* animation state variables */
41GLfloat rotation_angle = 0.0f;
42
43static GLuint gProgram;
44static GLuint gvPositionHandle, gvColorHandle;
45static GLuint rotation_uniform;
46static GLint num_vertex = 3;
47static const GLfloat triangle[] = {
48 -0.125f, -0.125f, 0.0f, 0.5f,
49 0.0f, 0.125f, 0.0f, 0.5f,
50 0.125f, -0.125f, 0.0f, 0.5f };
51static const GLfloat color_triangle[] = {
52 0.0f, 0.0f, 1.0f, 1.0f,
53 0.0f, 1.0f, 0.0f, 1.0f,
54 1.0f, 0.0f, 0.0f, 0.0f };
55const GLfloat * vertex_data;
56const GLfloat * color_data;
57static const char gVertexShader[] =
58 "attribute vec4 vPosition;\n"
59 "attribute vec4 vColor;\n"
60 "uniform float angle;\n"
61 "varying vec4 colorinfo;\n"
62 "void main() {\n"
63 " mat3 rot_z = mat3( vec3( cos(angle), sin(angle), 0.0),\n"
64 " vec3(-sin(angle), cos(angle), 0.0),\n"
65 " vec3( 0.0, 0.0, 1.0));\n"
66 " gl_Position = vec4(rot_z * vPosition.xyz, 1.0);\n"
67 " colorinfo = vColor;\n"
68 "}\n";
69
70static const char gFragmentShader[] = "precision mediump float;\n"
71 "varying vec4 colorinfo;\n"
72 "void main() {\n"
73 " gl_FragColor = colorinfo;\n"
74 "}\n";
75
76/* util functions */
77GLuint loadShader(GLenum shaderType, const char* pSource)
78{
79 GLuint shader = glCreateShader(shaderType);
80 if (shader) {
81 glShaderSource(shader, 1, &pSource, NULL);
82 glCompileShader(shader);
83 GLint compiled = 0;
84 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
85
86 if (!compiled) {
87 GLint infoLen = 0;
88 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
89 if (infoLen) {
90 char* buf = (char*) malloc(infoLen);
91 if (buf) {
92 glGetShaderInfoLog(shader, infoLen, NULL, buf);
93 fprintf(stderr, "Could not compile shader %d:\n%s\n",
94 shaderType, buf);
95 free(buf);
96 }
97 glDeleteShader(shader);
98 shader = 0;
99 }
100 }
101 } else {
102 printf("Error, during shader creation: %i\n", glGetError());
103 }
104
105 return shader;
106}
107
108GLuint createProgram(const char* pVertexSource, const char* pFragmentSource)
109{
110 GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
111 if (!vertexShader) {
112 printf("vertex shader not compiled\n");
113 return 0;
114 }
115
116 GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
117 if (!pixelShader) {
118 printf("frag shader not compiled\n");
119 return 0;
120 }
121
122 GLuint program = glCreateProgram();
123 if (program) {
124 glAttachShader(program, vertexShader);
125 glAttachShader(program, pixelShader);
126 glLinkProgram(program);
127 GLint linkStatus = GL_FALSE;
128 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
129
130 if (linkStatus != GL_TRUE) {
131 GLint bufLength = 0;
132 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
133 if (bufLength) {
134 char* buf = (char*) malloc(bufLength);
135 if (buf) {
136 glGetProgramInfoLog(program, bufLength, NULL, buf);
137 fprintf(stderr, "Could not link program:\n%s\n", buf);
138 free(buf);
139 }
140 }
141 glDeleteProgram(program);
142 program = 0;
143 }
144 }
145
146 return program;
147}
148
149int setupGraphics()
150{
151 vertex_data = triangle;
152 color_data = color_triangle;
153
154 gProgram = createProgram(gVertexShader, gFragmentShader);
155 if (!gProgram) {
156 printf("error making program\n");
157 return 0;
158 }
159
160 gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
161 gvColorHandle = glGetAttribLocation(gProgram, "vColor");
162
163 rotation_uniform = glGetUniformLocation(gProgram, "angle");
164
165 return 1;
166}
167
168void hw_render(EGLDisplay displ, EGLSurface surface)
169{
170 glUseProgram(gProgram);
171
172 glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
173
174 glUniform1fv(rotation_uniform,1, &rotation_angle);
175
176 glVertexAttribPointer(gvColorHandle, num_vertex, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*4, color_data);
177 glVertexAttribPointer(gvPositionHandle, num_vertex, GL_FLOAT, GL_FALSE, 0, vertex_data);
178 glEnableVertexAttribArray(gvPositionHandle);
179 glEnableVertexAttribArray(gvColorHandle);
180
181 glDrawArrays(GL_TRIANGLE_STRIP, 0, num_vertex);
182 glDisableVertexAttribArray(gvPositionHandle);
183 glDisableVertexAttribArray(gvColorHandle);
184
185 eglSwapBuffers(displ, surface);
186}
187
188void hw_step()
189{
190 rotation_angle += rotation_increment;
191 return;
192}
193
194int main(int argc, char** argv)
195{
196 struct SfClient* sf_client = sf_client_create();
197
198 if (!sf_client) {
199 printf("Problem creating client ... aborting now.");
200 return 1;
201 }
202
203 int x = getenv("X") ? atoi(getenv("X")) : 200;
204 int y = getenv("Y") ? atoi(getenv("Y")) : 200;
205
206 int w = getenv("W") ? atoi(getenv("W")) : 500;
207 int h = getenv("H") ? atoi(getenv("H")) : 500;
208
209 float opacity = getenv("O") ? atof(getenv("O")) : 0.5;
210 rotation_increment = getenv("RI") ? atof(getenv("RI")) : 0.01;
211
212 SfSurfaceCreationParameters params = {
213 x,
214 y,
215 w,
216 h,
217 -1, // PIXEL_FORMAT_RGBA_8888,
218 INT_MAX,
219 opacity,
220 1,
221 "A test surface"
222 };
223
224 struct SfSurface* sf_surface = sf_surface_create(sf_client, &params);
225
226 if (!sf_surface) {
227 printf("Problem creating surface ... aborting now.");
228 return 1;
229 }
230
231 sf_surface_make_current(sf_surface);
232
233 EGLDisplay disp = sf_client_get_egl_display(sf_client);
234 EGLSurface surface = sf_surface_get_egl_surface(sf_surface);
235
236 setupGraphics();
237
238 printf("Turning off screen\n");
239 sf_blank(0);
240
241 sleep(1);
242
243 printf("Turning on screen\n");
244 sf_unblank(0);
245
246 for (;;) {
247 hw_render(disp, surface);
248 hw_step();
249 }
250}