Add indicator
[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
138 void Tizen_glTexImage2d()
139 {
140
141     GLES2_Context Mainctx;
142     LoadContext(&Mainctx);
143
144     Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
145     Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,
146                       0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
147 }
148
149 static int Indicator_GLES_Init(SDL_Window* window)
150 {
151     SDL_WindowData *wind = window->driverdata;
152     ecore_wl_indicator_visible_type_set(wind->window, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
153     ecore_wl_window_indicator_opacity_set(wind->window, ECORE_WL_INDICATOR_OPAQUE);
154     ecore_wl_window_indicator_state_set(wind->window, ECORE_WL_INDICATOR_STATE_ON);
155
156     Ecore_Ipc_Server* IpcServer = serverConnection("elm_indicator", &fileInfo);
157     if(!IpcServer)
158     {
159         SDL_Log("Fail to connect elm_indicator!\n");
160         return 0;
161     }
162
163     GLchar vShaderStr[] =
164         "attribute vec4 vVertices;\n"
165         "attribute vec2 vCoord;\n"
166         "varying vec2 Coord;\n"
167         "void main()\n"
168         "{\n"
169         "    gl_Position = vVertices;\n"
170         "    Coord = vCoord;\n"
171         "}\n";
172
173     GLchar fShaderStr[] =
174         "precision mediump float;\n"
175         "varying vec2 Coord;\n"
176         "uniform sampler2D s_texture;\n"
177         "void main()\n"
178         "{\n"
179         " gl_FragColor = texture2D(s_texture,Coord);\n"
180         "}\n";
181
182     ecore_main_loop_iterate();
183
184     GLuint vertexShader;
185     GLuint fragmentShader;
186
187     GLint linked;
188
189     GLES2_Context Mainctx;
190     LoadContext(&Mainctx);
191
192     vertexShader = LoadShader(&Mainctx, vShaderStr, GL_VERTEX_SHADER);
193     fragmentShader = LoadShader(&Mainctx, fShaderStr, GL_FRAGMENT_SHADER);
194     SDL_Log("The vertex shader is %d", vertexShader);
195     SDL_Log("The fragment shader is %d", fragmentShader);
196
197     programObject = Mainctx.glCreateProgram();
198     if(programObject == 0)
199         return 0;
200
201     Mainctx.glAttachShader(programObject, vertexShader);
202     Mainctx.glAttachShader(programObject, fragmentShader);
203
204     Mainctx.glBindAttribLocation(programObject, 0, "vVertices");
205     Mainctx.glBindAttribLocation(programObject, 1, "vCoord");
206
207     Mainctx.glLinkProgram(programObject);
208     Mainctx.glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
209
210     if(!linked)
211     {
212         GLint infoLen = 0;
213         Mainctx.glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
214         if(infoLen > 1)
215         {
216             char* infoLog = (char*)(malloc(sizeof(char) * infoLen));
217             Mainctx.glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
218             SDL_Log("Error linking program: %s", infoLog);
219             free(infoLog);
220         }
221         Mainctx.glDeleteProgram(programObject);
222         return 0;
223     }
224
225
226     Mainctx.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
227     Mainctx.glEnable(GL_DEPTH_TEST);
228
229     /* Generate buffer object names */
230     Mainctx.glGenBuffers(4, indicator_vbo);
231     /* Bind a named buffer object */
232     Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_vbo[0]);
233     /* Ceates and initializes a buffer object's data store */
234     Mainctx.glBufferData(GL_ARRAY_BUFFER, 18 * 4, vVertices, GL_STATIC_DRAW);
235
236     /* Generate buffer object names */
237     Mainctx.glGenBuffers(4, indicator_ibo);
238     /* Bind a named buffer object */
239     Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_ibo[0]);
240     /* Ceates and initializes a buffer object's data store */
241     Mainctx.glBufferData(GL_ARRAY_BUFFER, 12 * 4, vCoord, GL_STATIC_DRAW);
242
243
244
245     Mainctx.glGenTextures(1, &textureID);
246     Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
247
248     Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
249     Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
250     Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
251     Mainctx.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
252
253     Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
254
255     window->indicator_show = SDL_TRUE;
256     window->last_indicator_showtime = SDL_GetTicks();
257     return 1;
258 }
259
260 SDL_GLContext
261 Tizen_GLES_CreateContext(_THIS, SDL_Window *window)
262 {
263     SDL_GLContext context;
264
265     context = SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
266
267     if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS))
268     {
269         if(!Indicator_GLES_Init(window))
270         {
271             SDL_Log("Indicator GLES init error!");
272         }
273     }
274
275     return context;
276 }
277
278 void
279 Tizen_GLES_SwapWindow(_THIS, SDL_Window *window)
280 {
281     SDL_WindowData* wdata = (SDL_WindowData*)window->driverdata;
282     if (wdata->received_rotation == 1) {
283         ecore_wl_window_rotation_change_done_send(wdata->window);
284         wdata->received_rotation = 0;
285     }
286
287     if(window->last_indicator_showtime + 3000 < SDL_GetTicks())
288     {
289         window->indicator_show = SDL_FALSE;
290     }
291
292     if(!(window->flags & SDL_WINDOW_FULLSCREEN) && !(window->flags & SDL_WINDOW_BORDERLESS) && window->indicator_show)
293     {
294         GLES2_Context Mainctx;
295         LoadContext(&Mainctx);
296         Mainctx.glUseProgram(programObject);
297
298         Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_vbo[window->indicator_type]);
299         Mainctx.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
300
301          Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_ibo[window->indicator_type]);
302          Mainctx.glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
303
304         Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
305         Mainctx.glEnableVertexAttribArray(0);
306         Mainctx.glEnableVertexAttribArray(1);
307         Mainctx.glDrawArrays(GL_TRIANGLES, 0, 6);
308     }
309
310     SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
311 }
312
313
314 int
315 Tizen_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
316 {
317     int ret;
318
319     if (window && context) {
320         ret = SDL_EGL_MakeCurrent(_this, ((SDL_WindowData *) window->driverdata)->egl_surface, context);
321     } else {
322         ret = SDL_EGL_MakeCurrent(_this, NULL, NULL);
323     }
324
325     return ret;
326 }
327
328 void
329 Tizen_GLES_DeleteContext(_THIS, SDL_GLContext context)
330 {
331     SDL_EGL_DeleteContext(_this, context);
332 }
333
334 void
335 SDL_IndicatorProcessEvent(SDL_Event * event, SDL_Window *window)
336 {
337     GLES2_Context Mainctx;
338     LoadContext(&Mainctx);
339
340     if(event->type == SDL_ROTATEEVENT)
341     {
342         window->indicator_type = ((int)event->user.data1) / 90;
343         if( window->indicator_type == 0)
344         {
345             SDL_Log("===rotate 0 degree!\n");
346             vVertices[0] = 1.0f;
347             vVertices[1] = 1.0f;
348             vVertices[3] = -1.0f;
349             vVertices[4] = 0.92f;
350             vVertices[6] = 1.0f;
351             vVertices[7] = 0.92f;
352             vVertices[9] = 1.0f;
353             vVertices[10] = 1.0f;
354             vVertices[12] =  -1.0f;
355             vVertices[13] =  1.0f;
356             vVertices[15] = -1.0f;
357             vVertices[16] = 0.92f;
358
359             vCoord[0] = 1.0f;
360             vCoord[1] = 0.0f;
361             vCoord[2] = 0.0f;
362             vCoord[3] = 1.0f;
363             vCoord[4] = 1.0f;
364             vCoord[5] = 1.0f;
365             vCoord[6] = 1.0f;
366             vCoord[7] = 0.0f;
367             vCoord[8] = 0.0f;
368             vCoord[9] = 0.0f;
369             vCoord[10] = 0.0f;
370             vCoord[11] = 1.0f;
371
372         }
373         else if(window->indicator_type == 1)
374         {
375             SDL_Log("===rotate 90 degree!\n");
376             vVertices[0] = -0.86f;
377             vVertices[1] = 1.0f;
378             vVertices[3] = -0.86f;
379             vVertices[4] = -1.0f;
380             vVertices[6] = -1.0f;
381             vVertices[7] = 1.0f;
382             vVertices[9] = -1.0f;
383             vVertices[10] = -1.0f;
384             vVertices[12] =  -0.86f;
385             vVertices[13] = -1.0f;
386             vVertices[15] = -1.0f;
387             vVertices[16] = 1.0f;
388
389             vCoord[0] = 1.0f;
390             vCoord[1] = 1.0f;
391             vCoord[2] = 0.0f;
392             vCoord[3] = 1.0f;
393             vCoord[4] = 1.0f;
394             vCoord[5] = 0.0f;
395             vCoord[6] = 0.0f;
396             vCoord[7] = 0.0f;
397             vCoord[8] = 0.0f;
398             vCoord[9] = 1.0f;
399             vCoord[10] = 1.0f;
400             vCoord[11] = 0.0f;
401         }
402         else if(window->indicator_type == 2)
403         {
404             SDL_Log("===rotate 180 degree!\n");
405             vVertices[0] = -1.0f;
406             vVertices[1] = -1.0f;
407             vVertices[3] = -1.0f;
408             vVertices[4] = -0.92f;
409             vVertices[6] = 1.0f;
410             vVertices[7] = -1.0f;
411             vVertices[9] = 1.0f;
412             vVertices[10] = -0.92f;
413             vVertices[12] =  -1.0f;
414             vVertices[13] = -0.92f;
415             vVertices[15] = 1.0f;
416             vVertices[16] = -1.0f;
417
418             vCoord[0] = 1.0f;
419             vCoord[1] = 0.0f;
420             vCoord[2] = 1.0f;
421             vCoord[3] = 1.0f;
422             vCoord[4] = 0.0f;
423             vCoord[5] = 0.0f;
424             vCoord[6] = 0.0f;
425             vCoord[7] = 1.0f;
426             vCoord[8] = 1.0f;
427             vCoord[9] = 1.0f;
428             vCoord[10] = 0.0f;
429             vCoord[11] = 0.0f;
430         }
431         else if(window->indicator_type == 3)
432         {
433             SDL_Log("===rotate 270 degree!\n");
434             vVertices[0] = 1.0f;
435             vVertices[1] = 1.0f;
436             vVertices[3] = 0.86f;
437             vVertices[4] = 1.0f;
438             vVertices[6] = 1.0f;
439             vVertices[7] = -1.0f;
440             vVertices[9] = 0.86f;
441             vVertices[10] = -1.0f;
442             vVertices[12] =  0.86f;
443             vVertices[13] = 1.0f;
444             vVertices[15] = 1.0f;
445             vVertices[16] = -1.0f;
446
447             vCoord[0] = 0.0f;
448             vCoord[1] = 0.0f;
449             vCoord[2] = 0.0f;
450             vCoord[3] = 1.0f;
451             vCoord[4] = 1.0f;
452             vCoord[5] = 0.0f;
453             vCoord[6] = 1.0f;
454             vCoord[7] = 1.0f;
455             vCoord[8] = 0.0f;
456             vCoord[9] = 1.0f;
457             vCoord[10] = 1.0f;
458             vCoord[11] = 0.0f;
459         }
460
461         Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_vbo[window->indicator_type]);
462         Mainctx.glBufferData(GL_ARRAY_BUFFER, 18 * 4, vVertices, GL_STATIC_DRAW);
463         Mainctx.glBindBuffer(GL_ARRAY_BUFFER, indicator_ibo[window->indicator_type]);
464         Mainctx.glBufferData(GL_ARRAY_BUFFER, 12 * 4, vCoord, GL_STATIC_DRAW);
465
466         Mainctx.glBindTexture(GL_TEXTURE_2D, textureID);
467
468         Mainctx.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fileInfo.ImageWidth, fileInfo.ImageHeight,0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)fileInfo.sharedFile->address);
469         window->indicator_show = SDL_TRUE;
470         window->last_indicator_showtime = SDL_GetTicks();
471     }
472 }
473
474 #endif /* SDL_VIDEO_DRIVER_TIZEN && SDL_VIDEO_OPENGL_EGL */
475
476 /* vi: set ts=4 sw=4 expandtab: */