Merge "add indicator sliding effect" into tizen
[platform/upstream/SDL.git] / src / video / tizen / SDL_tizenopengles.c
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4   Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
5
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.
9
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:
13
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.
21 */
22 #include "../../SDL_internal.h"
23
24 #if SDL_VIDEO_OPENGL_EGL
25
26 #include "SDL_tizenvideo.h"
27 #include "SDL_tizenopengles.h"
28 #include "SDL_tizenwindow.h"
29 #include "SDL_tizenevents_c.h"
30
31 #include "SDL_ecore_ipc.h"
32
33
34 #if SDL_VIDEO_OPENGL
35 #include "SDL_opengl.h"
36 #endif /* SDL_VIDEO_OPENGL */
37
38 #if SDL_VIDEO_OPENGL_ES
39 #include "SDL_opengles.h"
40 #endif /* SDL_VIDEO_OPENGL_ES */
41
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 */
46
47
48 GLfloat vVertices[18]={
49      1.0f, 1.0f,  0.0f,
50     -1.0f, 0.92f, 0.0f,
51      1.0f, 0.92f, 0.0f,
52      1.0f, 1.0f,  0.0f,
53     -1.0f, 1.0f,  0.0f,
54     -1.0f, 0.92f, 0.0f
55     };
56
57 GLfloat vCoord[12] = {
58     1.0f, 0.0f,
59     0.0f, 1.0f,
60     1.0f, 1.0f,
61     1.0f, 0.0f,
62     0.0f, 0.0f,
63     0.0f, 1.0f};
64
65
66 /* EGL implementation of SDL OpenGL ES support */
67 typedef struct GLES2_Context
68 {
69     #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
70     #include "../../render/opengles2/SDL_gles2funcs.h"
71     #undef SDL_PROC
72 } GLES2_Context;
73
74 GLuint programObject;
75
76 int
77 Tizen_GLES_LoadLibrary(_THIS, const char *path)
78 {
79     int ret;
80
81     ret = SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType)ecore_wl_display_get());
82     return ret;
83 }
84
85 static int LoadContext(GLES2_Context * data)
86 {
87     #define SDL_PROC(ret,func,params) \
88     do { \
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()); \
92         } \
93     } while ( 0 );
94
95     #include "../../render/opengles2/SDL_gles2funcs.h"
96     #undef SDL_PROC
97
98     return 0;
99 }
100
101 static GLuint LoadShader(GLES2_Context* Mainctx, const char *shaderSrc, GLenum type)
102 {
103     GLuint shader;
104     GLint compiled;
105
106     shader = Mainctx->glCreateShader(type);
107     SDL_Log("shader == %d", shader);
108     if(shader == 0)
109         return 0;
110     Mainctx->glShaderSource(shader, 1, &shaderSrc, NULL);
111     Mainctx->glCompileShader(shader);
112     Mainctx->glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
113     if(!compiled)
114     {
115         GLint infoLen = 0;
116         Mainctx->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
117         if(infoLen > 1)
118         {
119             char* infoLog = (char*)(malloc(sizeof(char) * infoLen));
120             Mainctx->glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
121             SDL_Log("Error compiling shader: %s", infoLog );
122             free(infoLog);
123         }
124         Mainctx->glDeleteShader(shader);
125         return 0;
126     }
127     return shader;
128
129 }
130
131 SharedFileInfo fileInfo;
132
133 unsigned int textureID;
134 unsigned int indicator_vbo[4], indicator_ibo[4];
135 unsigned short indicator_index;
136
137 extern void ModelMatrixLoadIdentity(ModelMatrix* matrix)
138 {
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;
143
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;
148
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;
153
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;
158 }
159
160 static void ModelMatrixTranslate(ModelMatrix* matrix, GLfloat x, GLfloat y, GLfloat z)
161 {
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);
166 }
167
168 void Tizen_glTexImage2d()
169 {
170
171     GLES2_Context Mainctx;
172     LoadContext(&Mainctx);
173
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);
177 }
178
179     ModelMatrix mMatrix;
180 static int Indicator_GLES_Init(SDL_Window* window)
181 {
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);
186
187     Ecore_Ipc_Server* IpcServer = serverConnection("elm_indicator", &fileInfo);
188     if(!IpcServer)
189     {
190         SDL_Log("Fail to connect elm_indicator!\n");
191         return 0;
192     }
193
194     GLchar vShaderStr[] =
195         "attribute vec4 vVertices;\n"
196         "attribute vec2 vCoord;\n"
197         "uniform mat4 modelMatrix;\n"
198         "varying vec2 Coord;\n"
199         "void main()\n"
200         "{\n"
201         "    gl_Position = modelMatrix * vVertices;\n"
202         "    Coord = vCoord;\n"
203         "}\n";
204
205     GLchar fShaderStr[] =
206         "precision mediump float;\n"
207         "varying vec2 Coord;\n"
208         "uniform sampler2D s_texture;\n"
209         "void main()\n"
210         "{\n"
211         " gl_FragColor = texture2D(s_texture,Coord);\n"
212         "}\n";
213
214     ecore_main_loop_iterate();
215
216     GLuint vertexShader;
217     GLuint fragmentShader;
218
219     GLint linked;
220
221     GLES2_Context Mainctx;
222     LoadContext(&Mainctx);
223
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);
228
229     programObject = Mainctx.glCreateProgram();
230     if(programObject == 0)
231         return 0;
232
233     Mainctx.glAttachShader(programObject, vertexShader);
234     Mainctx.glAttachShader(programObject, fragmentShader);
235
236     Mainctx.glLinkProgram(programObject);
237     Mainctx.glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
238
239     if(!linked)
240     {
241         GLint infoLen = 0;
242         Mainctx.glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
243         if(infoLen > 1)
244         {
245             char* infoLog = (char*)(malloc(sizeof(char) * infoLen));
246             Mainctx.glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
247             SDL_Log("Error linking program: %s", infoLog);
248             free(infoLog);
249         }
250         Mainctx.glDeleteProgram(programObject);
251         return 0;
252     }
253
254
255     Mainctx.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
256     Mainctx.glEnable(GL_DEPTH_TEST);
257
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);
264
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);
271
272     Mainctx.glBindAttribLocation(programObject, 0, "vVertices");
273     Mainctx.glBindAttribLocation(programObject, 1, "vCoord");
274
275     ModelMatrixLoadIdentity(&mMatrix);
276
277
278     Mainctx.glGenTextures(1, &textureID);
279     Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
280
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);
285
286     Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
287
288     window->indicator_show = SDL_TRUE;
289     window->last_indicator_showtime = SDL_GetTicks();
290     return 1;
291 }
292
293 SDL_GLContext
294 Tizen_GLES_CreateContext(_THIS, SDL_Window *window)
295 {
296     SDL_GLContext context;
297
298     context = SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
299
300     if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS))
301     {
302         if(!Indicator_GLES_Init(window))
303         {
304             SDL_Log("Indicator GLES init error!");
305         }
306     }
307
308     return context;
309 }
310
311 void
312 Tizen_GLES_SwapWindow(_THIS, SDL_Window *window)
313 {
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;
318     }
319
320     if(window->last_indicator_showtime + 3000 < SDL_GetTicks())
321     {
322         switch(window->indicator_type)
323         {
324         case 0://0
325             ModelMatrixTranslate(&mMatrix, 0.0f, 0.02f, 0.0f);
326         break;
327         case 1://90
328             ModelMatrixTranslate(&mMatrix, -0.02f, 0.0f, 0.0f);
329         break;
330         case 2://180
331             ModelMatrixTranslate(&mMatrix, 0.0f, -0.02f, 0.0f);
332         break;
333         case 3://270
334             ModelMatrixTranslate(&mMatrix, 0.02f, 0.0f, 0.0f);
335         break;
336         }
337     }
338
339     Uint32 time = window->last_indicator_showtime;
340     Uint32 getTick = SDL_GetTicks();
341     if(time + 3000> getTick && time + 4000 < getTick)
342     {
343         window->indicator_show = SDL_FALSE;
344     }
345
346     if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS) && window->indicator_show)
347     {
348         GLES2_Context Mainctx;
349         LoadContext(&Mainctx);
350         Mainctx.glUseProgram(programObject);
351
352         Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_vbo[window->indicator_type]);
353         Mainctx.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
354
355          Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_ibo[window->indicator_type]);
356          Mainctx.glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
357
358         Mainctx.glUniformMatrix4fv(Mainctx.glGetUniformLocation(programObject,  "modelMatrix"), 1, GL_FALSE, mMatrix.m);
359
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);
364     }
365
366     SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
367 }
368
369
370 int
371 Tizen_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
372 {
373     int ret;
374
375     if (window && context) {
376         ret = SDL_EGL_MakeCurrent(_this, ((SDL_WindowData *) window->driverdata)->egl_surface, context);
377     } else {
378         ret = SDL_EGL_MakeCurrent(_this, NULL, NULL);
379     }
380
381     return ret;
382 }
383
384 void
385 Tizen_GLES_DeleteContext(_THIS, SDL_GLContext context)
386 {
387     SDL_EGL_DeleteContext(_this, context);
388 }
389
390 void
391 SDL_IndicatorProcessEvent(SDL_Event * event, SDL_Window *window)
392 {
393     GLES2_Context Mainctx;
394     LoadContext(&Mainctx);
395
396     if(event->type == SDL_ROTATEEVENT)
397     {
398         window->indicator_type = ((int)event->user.data1) / 90;
399         if( window->indicator_type == 0)
400         {
401             SDL_Log("===rotate 0 degree!\n");
402             vVertices[0] = 1.0f;
403             vVertices[1] = 1.0f;
404             vVertices[3] = -1.0f;
405             vVertices[4] = 0.92f;
406             vVertices[6] = 1.0f;
407             vVertices[7] = 0.92f;
408             vVertices[9] = 1.0f;
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;
414
415             vCoord[0] = 1.0f;
416             vCoord[1] = 0.0f;
417             vCoord[2] = 0.0f;
418             vCoord[3] = 1.0f;
419             vCoord[4] = 1.0f;
420             vCoord[5] = 1.0f;
421             vCoord[6] = 1.0f;
422             vCoord[7] = 0.0f;
423             vCoord[8] = 0.0f;
424             vCoord[9] = 0.0f;
425             vCoord[10] = 0.0f;
426             vCoord[11] = 1.0f;
427
428         }
429         else if(window->indicator_type == 1)
430         {
431             SDL_Log("===rotate 90 degree!\n");
432             vVertices[0] = -0.86f;
433             vVertices[1] = 1.0f;
434             vVertices[3] = -0.86f;
435             vVertices[4] = -1.0f;
436             vVertices[6] = -1.0f;
437             vVertices[7] = 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;
444
445             vCoord[0] = 1.0f;
446             vCoord[1] = 1.0f;
447             vCoord[2] = 0.0f;
448             vCoord[3] = 1.0f;
449             vCoord[4] = 1.0f;
450             vCoord[5] = 0.0f;
451             vCoord[6] = 0.0f;
452             vCoord[7] = 0.0f;
453             vCoord[8] = 0.0f;
454             vCoord[9] = 1.0f;
455             vCoord[10] = 1.0f;
456             vCoord[11] = 0.0f;
457         }
458         else if(window->indicator_type == 2)
459         {
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;
465             vVertices[6] = 1.0f;
466             vVertices[7] = -1.0f;
467             vVertices[9] = 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;
473
474             vCoord[0] = 1.0f;
475             vCoord[1] = 0.0f;
476             vCoord[2] = 1.0f;
477             vCoord[3] = 1.0f;
478             vCoord[4] = 0.0f;
479             vCoord[5] = 0.0f;
480             vCoord[6] = 0.0f;
481             vCoord[7] = 1.0f;
482             vCoord[8] = 1.0f;
483             vCoord[9] = 1.0f;
484             vCoord[10] = 0.0f;
485             vCoord[11] = 0.0f;
486         }
487         else if(window->indicator_type == 3)
488         {
489             SDL_Log("===rotate 270 degree!\n");
490             vVertices[0] = 1.0f;
491             vVertices[1] = 1.0f;
492             vVertices[3] = 0.86f;
493             vVertices[4] = 1.0f;
494             vVertices[6] = 1.0f;
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;
502
503             vCoord[0] = 0.0f;
504             vCoord[1] = 0.0f;
505             vCoord[2] = 0.0f;
506             vCoord[3] = 1.0f;
507             vCoord[4] = 1.0f;
508             vCoord[5] = 0.0f;
509             vCoord[6] = 1.0f;
510             vCoord[7] = 1.0f;
511             vCoord[8] = 0.0f;
512             vCoord[9] = 1.0f;
513             vCoord[10] = 1.0f;
514             vCoord[11] = 0.0f;
515         }
516
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);
521
522         Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
523
524         Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
525
526         ModelMatrixLoadIdentity(&mMatrix);
527         window->indicator_show = SDL_TRUE;
528         window->last_indicator_showtime = SDL_GetTicks();
529     }
530 }
531
532 #endif /* SDL_VIDEO_DRIVER_TIZEN && SDL_VIDEO_OPENGL_EGL */
533
534 /* vi: set ts=4 sw=4 expandtab: */