2 * Copyright (C) 2013 Canonical Ltd
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * Authored by: Thomas Voss <thomas.voss@canonical.com>
17 * Ricardo Salveti de Araujo <ricardo.salveti@canonical.com>
25 #include <GLES2/gl2.h>
26 #include <GLES2/gl2ext.h>
28 #include <hybris/surface_flinger/surface_flinger_compatibility_layer.h>
30 struct SfSurface
* sf_surface_create(struct SfClient
* client
, SfSurfaceCreationParameters
* params
);
31 EGLSurface
sf_surface_get_egl_surface(struct SfSurface
*);
32 void sf_surface_make_current(struct SfSurface
* surface
);
34 void sf_surface_move_to(struct SfSurface
* surface
, int x
, int y
);
35 void sf_surface_set_layer(struct SfSurface
* surface
, int layer
);
36 void sf_surface_set_alpha(struct SfSurface
* surface
, float alpha
);
38 double rotation_increment
= 0.01;
40 /* animation state variables */
41 GLfloat rotation_angle
= 0.0f
;
43 static GLuint gProgram
;
44 static GLuint gvPositionHandle
, gvColorHandle
;
45 static GLuint rotation_uniform
;
46 static GLint num_vertex
= 3;
47 static 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
};
51 static 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
};
55 const GLfloat
* vertex_data
;
56 const GLfloat
* color_data
;
57 static const char gVertexShader
[] =
58 "attribute vec4 vPosition;\n"
59 "attribute vec4 vColor;\n"
60 "uniform float angle;\n"
61 "varying vec4 colorinfo;\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"
70 static const char gFragmentShader
[] = "precision mediump float;\n"
71 "varying vec4 colorinfo;\n"
73 " gl_FragColor = colorinfo;\n"
77 GLuint
loadShader(GLenum shaderType
, const char* pSource
)
79 GLuint shader
= glCreateShader(shaderType
);
81 glShaderSource(shader
, 1, &pSource
, NULL
);
82 glCompileShader(shader
);
84 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &compiled
);
88 glGetShaderiv(shader
, GL_INFO_LOG_LENGTH
, &infoLen
);
90 char* buf
= (char*) malloc(infoLen
);
92 glGetShaderInfoLog(shader
, infoLen
, NULL
, buf
);
93 fprintf(stderr
, "Could not compile shader %d:\n%s\n",
97 glDeleteShader(shader
);
102 printf("Error, during shader creation: %i\n", glGetError());
108 GLuint
createProgram(const char* pVertexSource
, const char* pFragmentSource
)
110 GLuint vertexShader
= loadShader(GL_VERTEX_SHADER
, pVertexSource
);
112 printf("vertex shader not compiled\n");
116 GLuint pixelShader
= loadShader(GL_FRAGMENT_SHADER
, pFragmentSource
);
118 printf("frag shader not compiled\n");
122 GLuint program
= glCreateProgram();
124 glAttachShader(program
, vertexShader
);
125 glAttachShader(program
, pixelShader
);
126 glLinkProgram(program
);
127 GLint linkStatus
= GL_FALSE
;
128 glGetProgramiv(program
, GL_LINK_STATUS
, &linkStatus
);
130 if (linkStatus
!= GL_TRUE
) {
132 glGetProgramiv(program
, GL_INFO_LOG_LENGTH
, &bufLength
);
134 char* buf
= (char*) malloc(bufLength
);
136 glGetProgramInfoLog(program
, bufLength
, NULL
, buf
);
137 fprintf(stderr
, "Could not link program:\n%s\n", buf
);
141 glDeleteProgram(program
);
151 vertex_data
= triangle
;
152 color_data
= color_triangle
;
154 gProgram
= createProgram(gVertexShader
, gFragmentShader
);
156 printf("error making program\n");
160 gvPositionHandle
= glGetAttribLocation(gProgram
, "vPosition");
161 gvColorHandle
= glGetAttribLocation(gProgram
, "vColor");
163 rotation_uniform
= glGetUniformLocation(gProgram
, "angle");
168 void hw_render(EGLDisplay displ
, EGLSurface surface
)
170 glUseProgram(gProgram
);
172 glClear( GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT
);
174 glUniform1fv(rotation_uniform
,1, &rotation_angle
);
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
);
181 glDrawArrays(GL_TRIANGLE_STRIP
, 0, num_vertex
);
182 glDisableVertexAttribArray(gvPositionHandle
);
183 glDisableVertexAttribArray(gvColorHandle
);
185 eglSwapBuffers(displ
, surface
);
190 rotation_angle
+= rotation_increment
;
194 int main(int argc
, char** argv
)
196 struct SfClient
* sf_client
= sf_client_create();
199 printf("Problem creating client ... aborting now.");
203 int x
= getenv("X") ? atoi(getenv("X")) : 200;
204 int y
= getenv("Y") ? atoi(getenv("Y")) : 200;
206 int w
= getenv("W") ? atoi(getenv("W")) : 500;
207 int h
= getenv("H") ? atoi(getenv("H")) : 500;
209 float opacity
= getenv("O") ? atof(getenv("O")) : 0.5;
210 rotation_increment
= getenv("RI") ? atof(getenv("RI")) : 0.01;
212 SfSurfaceCreationParameters params
= {
217 -1, // PIXEL_FORMAT_RGBA_8888,
224 struct SfSurface
* sf_surface
= sf_surface_create(sf_client
, ¶ms
);
227 printf("Problem creating surface ... aborting now.");
231 sf_surface_make_current(sf_surface
);
233 EGLDisplay disp
= sf_client_get_egl_display(sf_client
);
234 EGLSurface surface
= sf_surface_get_egl_surface(sf_surface
);
238 printf("Turning off screen\n");
243 printf("Turning on screen\n");
247 hw_render(disp
, surface
);