2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source distribution.
22 #include "../../SDL_internal.h"
24 #if SDL_VIDEO_OPENGL_EGL
26 #include "SDL_tizenvideo.h"
27 #include "SDL_tizenopengles.h"
28 #include "SDL_tizenwindow.h"
29 #include "SDL_tizenevents_c.h"
31 #include "SDL_ecore_ipc.h"
35 #include "SDL_opengl.h"
36 #endif /* SDL_VIDEO_OPENGL */
38 #if SDL_VIDEO_OPENGL_ES
39 #include "SDL_opengles.h"
40 #endif /* SDL_VIDEO_OPENGL_ES */
42 /* GL and GLES2 headers conflict on Linux 32 bits */
43 #if SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL
44 #include "SDL_opengles2.h"
45 #endif /* SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL */
48 GLfloat vVertices[18]={
57 GLfloat vCoord[12] = {
66 /* EGL implementation of SDL OpenGL ES support */
67 typedef struct GLES2_Context
69 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
70 #include "../../render/opengles2/SDL_gles2funcs.h"
77 Tizen_GLES_LoadLibrary(_THIS, const char *path)
81 ret = SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType)ecore_wl_display_get());
85 static int LoadContext(GLES2_Context * data)
87 #define SDL_PROC(ret,func,params) \
89 data->func = SDL_GL_GetProcAddress(#func); \
90 if ( ! data->func ) { \
91 return SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
95 #include "../../render/opengles2/SDL_gles2funcs.h"
101 static GLuint LoadShader(GLES2_Context* Mainctx, const char *shaderSrc, GLenum type)
106 shader = Mainctx->glCreateShader(type);
107 SDL_Log("shader == %d", shader);
110 Mainctx->glShaderSource(shader, 1, &shaderSrc, NULL);
111 Mainctx->glCompileShader(shader);
112 Mainctx->glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
116 Mainctx->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
119 char* infoLog = (char*)(malloc(sizeof(char) * infoLen));
120 Mainctx->glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
121 SDL_Log("Error compiling shader: %s", infoLog );
124 Mainctx->glDeleteShader(shader);
131 SharedFileInfo fileInfo;
133 unsigned int textureID;
134 unsigned int indicator_vbo[4], indicator_ibo[4];
135 unsigned short indicator_index;
137 extern void ModelMatrixLoadIdentity(ModelMatrix* matrix)
139 matrix->m[0][0] = 1.0f;
140 matrix->m[0][1] = 0.0f;
141 matrix->m[0][2] = 0.0f;
142 matrix->m[0][3] = 0.0f;
144 matrix->m[1][0] = 0.0f;
145 matrix->m[1][1] = 1.0f;
146 matrix->m[1][2] = 0.0f;
147 matrix->m[1][3] = 0.0f;
149 matrix->m[2][0] = 0.0f;
150 matrix->m[2][1] = 0.0f;
151 matrix->m[2][2] = 1.0f;
152 matrix->m[2][3] = 0.0f;
154 matrix->m[3][0] = 0.0f;
155 matrix->m[3][1] = 0.0f;
156 matrix->m[3][2] = 0.0f;
157 matrix->m[3][3] = 1.0f;
160 static void ModelMatrixTranslate(ModelMatrix* matrix, GLfloat x, GLfloat y, GLfloat z)
162 matrix->m[3][0] += (matrix->m[0][0] * x + matrix->m[1][0] * y + matrix->m[2][0] * z);
163 matrix->m[3][1] += (matrix->m[0][1] * x + matrix->m[1][1] * y + matrix->m[2][1] * z);
164 matrix->m[3][2] += (matrix->m[0][2] * x + matrix->m[1][2] * y + matrix->m[2][2] * z);
165 matrix->m[3][3] += (matrix->m[0][3] * x + matrix->m[1][3] * y + matrix->m[2][3] * z);
168 void Tizen_glTexImage2d()
171 GLES2_Context Mainctx;
172 LoadContext(&Mainctx);
174 Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
175 Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,
176 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
180 static int Indicator_GLES_Init(SDL_Window* window)
182 SDL_WindowData *wind = window->driverdata;
183 ecore_wl_indicator_visible_type_set(wind->window, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
184 ecore_wl_window_indicator_opacity_set(wind->window, ECORE_WL_INDICATOR_OPAQUE);
185 ecore_wl_window_indicator_state_set(wind->window, ECORE_WL_INDICATOR_STATE_ON);
187 Ecore_Ipc_Server* IpcServer = serverConnection("elm_indicator", &fileInfo);
190 SDL_Log("Fail to connect elm_indicator!\n");
194 GLchar vShaderStr[] =
195 "attribute vec4 vVertices;\n"
196 "attribute vec2 vCoord;\n"
197 "uniform mat4 modelMatrix;\n"
198 "varying vec2 Coord;\n"
201 " gl_Position = modelMatrix * vVertices;\n"
205 GLchar fShaderStr[] =
206 "precision mediump float;\n"
207 "varying vec2 Coord;\n"
208 "uniform sampler2D s_texture;\n"
211 " gl_FragColor = texture2D(s_texture,Coord);\n"
214 ecore_main_loop_iterate();
217 GLuint fragmentShader;
221 GLES2_Context Mainctx;
222 LoadContext(&Mainctx);
224 vertexShader = LoadShader(&Mainctx, vShaderStr, GL_VERTEX_SHADER);
225 fragmentShader = LoadShader(&Mainctx, fShaderStr, GL_FRAGMENT_SHADER);
226 SDL_Log("The vertex shader is %d", vertexShader);
227 SDL_Log("The fragment shader is %d", fragmentShader);
229 programObject = Mainctx.glCreateProgram();
230 if(programObject == 0)
233 Mainctx.glAttachShader(programObject, vertexShader);
234 Mainctx.glAttachShader(programObject, fragmentShader);
236 Mainctx.glLinkProgram(programObject);
237 Mainctx.glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
242 Mainctx.glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
245 char* infoLog = (char*)(malloc(sizeof(char) * infoLen));
246 Mainctx.glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
247 SDL_Log("Error linking program: %s", infoLog);
250 Mainctx.glDeleteProgram(programObject);
255 Mainctx.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
256 Mainctx.glEnable(GL_DEPTH_TEST);
258 /* Generate buffer object names */
259 Mainctx.glGenBuffers(4, indicator_vbo);
260 /* Bind a named buffer object */
261 Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_vbo[0]);
262 /* Ceates and initializes a buffer object's data store */
263 Mainctx.glBufferData(GL_ARRAY_BUFFER, 18 * 4, vVertices, GL_STATIC_DRAW);
265 /* Generate buffer object names */
266 Mainctx.glGenBuffers(4, indicator_ibo);
267 /* Bind a named buffer object */
268 Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_ibo[0]);
269 /* Ceates and initializes a buffer object's data store */
270 Mainctx.glBufferData(GL_ARRAY_BUFFER, 12 * 4, vCoord, GL_STATIC_DRAW);
272 Mainctx.glBindAttribLocation(programObject, 0, "vVertices");
273 Mainctx.glBindAttribLocation(programObject, 1, "vCoord");
275 ModelMatrixLoadIdentity(&mMatrix);
278 Mainctx.glGenTextures(1, &textureID);
279 Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
281 Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
282 Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
283 Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
286 Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
288 window->indicator_show = SDL_TRUE;
289 window->last_indicator_showtime = SDL_GetTicks();
294 Tizen_GLES_CreateContext(_THIS, SDL_Window *window)
296 SDL_GLContext context;
298 context = SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
300 if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS))
302 if(!Indicator_GLES_Init(window))
304 SDL_Log("Indicator GLES init error!");
312 Tizen_GLES_SwapWindow(_THIS, SDL_Window *window)
314 SDL_WindowData* wdata = (SDL_WindowData*)window->driverdata;
315 if (wdata->received_rotation == 1) {
316 ecore_wl_window_rotation_change_done_send(wdata->window);
317 wdata->received_rotation = 0;
320 if(window->last_indicator_showtime + 3000 < SDL_GetTicks())
322 switch(window->indicator_type)
325 ModelMatrixTranslate(&mMatrix, 0.0f, 0.02f, 0.0f);
328 ModelMatrixTranslate(&mMatrix, -0.02f, 0.0f, 0.0f);
331 ModelMatrixTranslate(&mMatrix, 0.0f, -0.02f, 0.0f);
334 ModelMatrixTranslate(&mMatrix, 0.02f, 0.0f, 0.0f);
339 Uint32 time = window->last_indicator_showtime;
340 Uint32 getTick = SDL_GetTicks();
341 if(time + 3000> getTick && time + 4000 < getTick)
343 window->indicator_show = SDL_FALSE;
346 if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS) && window->indicator_show)
348 GLES2_Context Mainctx;
349 LoadContext(&Mainctx);
350 Mainctx.glUseProgram(programObject);
352 Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_vbo[window->indicator_type]);
353 Mainctx.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
355 Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_ibo[window->indicator_type]);
356 Mainctx.glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
358 Mainctx.glUniformMatrix4fv(Mainctx.glGetUniformLocation(programObject, "modelMatrix"), 1, GL_FALSE, mMatrix.m);
360 Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
361 Mainctx.glEnableVertexAttribArray(0);
362 Mainctx.glEnableVertexAttribArray(1);
363 Mainctx.glDrawArrays(GL_TRIANGLES, 0, 6);
366 SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
371 Tizen_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
375 if (window && context) {
376 ret = SDL_EGL_MakeCurrent(_this, ((SDL_WindowData *) window->driverdata)->egl_surface, context);
378 ret = SDL_EGL_MakeCurrent(_this, NULL, NULL);
385 Tizen_GLES_DeleteContext(_THIS, SDL_GLContext context)
387 SDL_EGL_DeleteContext(_this, context);
391 SDL_IndicatorProcessEvent(SDL_Event * event, SDL_Window *window)
393 GLES2_Context Mainctx;
394 LoadContext(&Mainctx);
396 if(event->type == SDL_ROTATEEVENT)
398 window->indicator_type = ((int)event->user.data1) / 90;
399 if( window->indicator_type == 0)
401 SDL_Log("===rotate 0 degree!\n");
404 vVertices[3] = -1.0f;
405 vVertices[4] = 0.92f;
407 vVertices[7] = 0.92f;
409 vVertices[10] = 1.0f;
410 vVertices[12] = -1.0f;
411 vVertices[13] = 1.0f;
412 vVertices[15] = -1.0f;
413 vVertices[16] = 0.92f;
429 else if(window->indicator_type == 1)
431 SDL_Log("===rotate 90 degree!\n");
432 vVertices[0] = -0.86f;
434 vVertices[3] = -0.86f;
435 vVertices[4] = -1.0f;
436 vVertices[6] = -1.0f;
438 vVertices[9] = -1.0f;
439 vVertices[10] = -1.0f;
440 vVertices[12] = -0.86f;
441 vVertices[13] = -1.0f;
442 vVertices[15] = -1.0f;
443 vVertices[16] = 1.0f;
458 else if(window->indicator_type == 2)
460 SDL_Log("===rotate 180 degree!\n");
461 vVertices[0] = -1.0f;
462 vVertices[1] = -1.0f;
463 vVertices[3] = -1.0f;
464 vVertices[4] = -0.92f;
466 vVertices[7] = -1.0f;
468 vVertices[10] = -0.92f;
469 vVertices[12] = -1.0f;
470 vVertices[13] = -0.92f;
471 vVertices[15] = 1.0f;
472 vVertices[16] = -1.0f;
487 else if(window->indicator_type == 3)
489 SDL_Log("===rotate 270 degree!\n");
492 vVertices[3] = 0.86f;
495 vVertices[7] = -1.0f;
496 vVertices[9] = 0.86f;
497 vVertices[10] = -1.0f;
498 vVertices[12] = 0.86f;
499 vVertices[13] = 1.0f;
500 vVertices[15] = 1.0f;
501 vVertices[16] = -1.0f;
517 Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_vbo[window->indicator_type]);
518 Mainctx.glBufferData(GL_ARRAY_BUFFER, 18 * 4, vVertices, GL_STATIC_DRAW);
519 Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_ibo[window->indicator_type]);
520 Mainctx.glBufferData(GL_ARRAY_BUFFER, 12 * 4, vCoord, GL_STATIC_DRAW);
522 Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
524 Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
526 ModelMatrixLoadIdentity(&mMatrix);
527 window->indicator_show = SDL_TRUE;
528 window->last_indicator_showtime = SDL_GetTicks();
532 #endif /* SDL_VIDEO_DRIVER_TIZEN && SDL_VIDEO_OPENGL_EGL */
534 /* vi: set ts=4 sw=4 expandtab: */