summaryrefslogtreecommitdiff
path: root/tizen/src/maru_sdl.c
blob: d6a4ba2a5b72d82c2cf36f4aa84ad044744bd174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * SDL_WINDOWID hack
 *
 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * GiWoong Kim <giwoong.kim@samsung.com>
 * SeokYeon Hwang <syeon.hwang@samsung.com>
 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */


#include <pthread.h>
#include "console.h"
#include "maru_sdl.h"
#include "emul_state.h"
#include "sdl_rotate.h"
#include "maru_finger.h"
#include "hw/maru_pm.h"
#include "debug_ch.h"
#include "SDL_opengl.h"

MULTI_DEBUG_CHANNEL(tizen, maru_sdl);


DisplaySurface* qemu_display_surface = NULL;

SDL_Surface *surface_screen;
SDL_Surface *surface_qemu;
GLuint texture;

static double current_scale_factor = 1.0;
static double current_screen_degree = 0.0;
static int current_screen_width;
static int current_screen_height;

static int sdl_initialized = 0;
static int sdl_alteration = 0;
static int sdl_opengl = 0; //0 : just SDL surface, 1 : using SDL with OpenGL


#define SDL_THREAD

static pthread_mutex_t sdl_mutex = PTHREAD_MUTEX_INITIALIZER;
#ifdef SDL_THREAD
static pthread_cond_t sdl_cond = PTHREAD_COND_INITIALIZER;
static int sdl_thread_initialized = 0;
#endif

#define SDL_FLAGS (SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_NOFRAME)
#define SDL_BPP 32


void qemu_ds_sdl_update(DisplayState *ds, int x, int y, int w, int h)
{
    /* call sdl update */
#ifdef SDL_THREAD
    pthread_mutex_lock(&sdl_mutex);

    pthread_cond_signal(&sdl_cond);

    pthread_mutex_unlock(&sdl_mutex);
#else
    qemu_update();
#endif
}

void qemu_ds_sdl_resize(DisplayState *ds)
{
    TRACE("%d, %d\n", ds_get_width(ds), ds_get_height(ds));

#ifdef SDL_THREAD
    pthread_mutex_lock(&sdl_mutex);
#endif

    /* create surface_qemu */
    surface_qemu = SDL_CreateRGBSurfaceFrom(ds_get_data(ds),
            ds_get_width(ds),
            ds_get_height(ds),
            ds_get_bits_per_pixel(ds),
            ds_get_linesize(ds),
            ds->surface->pf.rmask,
            ds->surface->pf.gmask,
            ds->surface->pf.bmask,
            ds->surface->pf.amask);

#ifdef SDL_THREAD
    pthread_mutex_unlock(&sdl_mutex);
#endif

    if (surface_qemu == NULL) {
        ERR("Unable to set the RGBSurface: %s\n", SDL_GetError());
        return;
    }

}

static int maru_sdl_poll_event(SDL_Event *ev)
{
    int ret = 0;

    if (sdl_initialized == 1) {
        //pthread_mutex_lock(&sdl_mutex);
        ret = SDL_PollEvent(ev);
        //pthread_mutex_unlock(&sdl_mutex);
    }

    return ret;
}
void qemu_ds_sdl_refresh(DisplayState *ds)
{
    SDL_Event ev1, *ev = &ev1;

    // surface may be NULL in init func.
    qemu_display_surface = ds->surface;

    while (maru_sdl_poll_event(ev)) {
        switch (ev->type) {
            case SDL_VIDEORESIZE:
            {
                pthread_mutex_lock(&sdl_mutex);

                maruskin_sdl_init(0, get_emul_lcd_width(), get_emul_lcd_height(), true);

                pthread_mutex_unlock(&sdl_mutex);
                vga_hw_invalidate();
                break;
            }

            default:
                break;
        }
    }

    vga_hw_update();

#ifdef TARGET_ARM
#ifdef SDL_THREAD
    pthread_mutex_lock(&sdl_mutex);
#endif

    /*
    * It is necessary only for exynos4210 FIMD in connection with
    * some WM (xfwm4, for example)
    */

    SDL_UpdateRect(surface_screen, 0, 0, 0, 0);

#ifdef SDL_THREAD
    pthread_mutex_unlock(&sdl_mutex);
#endif
#endif
}





extern int capability_check_gl;
static void _sdl_init(void)
{
    int w, h, temp;

    INFO("Set up a video mode with the specified width, height and bits-per-pixel\n");

    //get current setting information and calculate screen size
    current_scale_factor = get_emul_win_scale();
    w = current_screen_width = get_emul_lcd_width() * current_scale_factor;
    h = current_screen_height = get_emul_lcd_height() * current_scale_factor;

    short rotaton_type = get_emul_rotation();
    if (rotaton_type == ROTATION_PORTRAIT) {
        current_screen_degree = 0.0;
    } else if (rotaton_type == ROTATION_LANDSCAPE) {
        current_screen_degree = 90.0;
        temp = w;
        w = h;
        h = temp;
    } else if (rotaton_type == ROTATION_REVERSE_PORTRAIT) {
        current_screen_degree = 180.0;
    } else if (rotaton_type == ROTATION_REVERSE_LANDSCAPE) {
        current_screen_degree = 270.0;
        temp = w;
        w = h;
        h = temp;
    }

    /*if (capability_check_gl != 0) {
        ERR("GL check returned non-zero\n");
        surface_screen = NULL;
    } else {
        surface_screen = SDL_SetVideoMode(w, h, get_emul_sdl_bpp(), SDL_OPENGL);
    }

    if (surface_screen == NULL) {
        sdl_opengl = 0;
        INFO("No OpenGL support on this system!??\n");
        ERR("%s\n", SDL_GetError());*/

        surface_screen = SDL_SetVideoMode(w, h, get_emul_sdl_bpp(), SDL_FLAGS);
        if (surface_screen == NULL) {
            ERR("Could not open SDL display (%dx%dx%d): %s\n", w, h, get_emul_sdl_bpp(), SDL_GetError());
            return;
        }
    /*} else {
        sdl_opengl = 1;
        INFO("OpenGL is supported on this system.\n");
    }*/

    if (sdl_opengl == 1) {
        /* Set the OpenGL state */
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, w, h, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glViewport(0, 0, w, h);
        glLoadIdentity();

        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        //glGenerateMipmapEXT(GL_TEXTURE_2D); // GL_MIPMAP_LINEAR
    }

    /* remove multi-touch finger points */
    get_emul_multi_touch_state()->multitouch_enable = 0;
    clear_finger_slot();
}

static int point_degree = 0;
static void draw_outline_circle(int cx, int cy, int r, int num_segments)
{
    int ii;
    float theta = 2 * 3.1415926 / (num_segments);
    float c = cosf(theta);//precalculate the sine and cosine
    float s = sinf(theta);
    float t;

    float x = r;//we start at angle = 0
    float y = 0;

    glEnable(GL_LINE_STIPPLE);
    glLoadIdentity();
    glColor3f(0.9, 0.9, 0.9);
    glLineStipple(1, 0xcccc);
    glLineWidth(3.f);

    glTranslatef(cx, cy, 0);
    glRotated(point_degree++ % 360, 0, 0, 1);

    glBegin(GL_LINE_LOOP);
    for(ii = 0; ii < num_segments; ii++) {
        glVertex2f(x, y); //output vertex

        //apply the rotation matrix
        t = x;
        x = c * x - s * y;
        y = s * t + c * y;
    }
    glEnd();

    glDisable(GL_LINE_STIPPLE);
}

static void draw_fill_circle(int cx, int cy, int r)
{
    glEnable(GL_POINT_SMOOTH);
    glLoadIdentity();
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor4f(0.1, 0.1, 0.1, 0.6);
    glPointSize(r - 2);

    glBegin(GL_POINTS);
        glVertex2f(cx, cy);
    glEnd();

    glDisable(GL_POINT_SMOOTH);
    glDisable(GL_BLEND);
}

static void qemu_update(void)
{
    if (sdl_alteration == 1) {
        sdl_alteration = 0;
        _sdl_init();
    }

    if (surface_qemu != NULL) {
        int i;

        if (sdl_opengl == 1)
        { //gl surface
            glEnable(GL_TEXTURE_2D);
            glLoadIdentity();
            glColor3f(1.0, 1.0, 1.0);
            glTexImage2D(GL_TEXTURE_2D,
                0, 3, surface_qemu->w, surface_qemu->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface_qemu->pixels);

            glClear(GL_COLOR_BUFFER_BIT);

            /* rotation */
            if (current_screen_degree == 270.0) { //reverse landscape
                glRotated(90, 0, 0, 1);
                glTranslatef(0, -current_screen_height, 0);
            } else if (current_screen_degree == 180.0) { //reverse portrait
                glRotated(180, 0, 0, 1);
                glTranslatef(-current_screen_width, -current_screen_height, 0);
            } else if (current_screen_degree == 90.0) { //landscape
                glRotated(270, 0, 0, 1);
                glTranslatef(-current_screen_width, 0, 0);
            }

            glBegin(GL_QUADS);
                glTexCoord2i(0, 0);
                glVertex3f(0, 0, 0);
                glTexCoord2i(1, 0);
                glVertex3f(current_screen_width, 0, 0);
                glTexCoord2i(1, 1);
                glVertex3f(current_screen_width, current_screen_height, 0);
                glTexCoord2i(0, 1);
                glVertex3f(0, current_screen_height, 0);
            glEnd();

            glDisable(GL_TEXTURE_2D);

            /* draw multi-touch finger points */
            MultiTouchState *mts = get_emul_multi_touch_state();
            if (mts->multitouch_enable != 0) {
                FingerPoint *finger = NULL;
                int finger_point_size_half = mts->finger_point_size / 2;

                for (i = 0; i < mts->finger_cnt; i++) {
                    finger = get_finger_point_from_slot(i);
                    if (finger != NULL && finger->id != 0) {
                        draw_outline_circle(finger->origin_x, finger->origin_y, finger_point_size_half, 10); //TODO: optimize
                        draw_fill_circle(finger->origin_x, finger->origin_y, mts->finger_point_size);
                    }
                }
            } //end  of draw multi-touch

            glFlush();

            SDL_GL_SwapBuffers();
        }
        else
        { //sdl surface
            SDL_Surface *processing_screen = NULL;

            if (current_scale_factor != 1.0 || current_screen_degree != 0.0) {
                // workaround
                // set color key 'magenta'
                surface_qemu->format->colorkey = 0xFF00FF;

                //image processing
                processing_screen = rotozoomSurface(surface_qemu, current_screen_degree, current_scale_factor, 1);
                SDL_BlitSurface(processing_screen, NULL, surface_screen, NULL);
            } else {
                SDL_BlitSurface(surface_qemu, NULL, surface_screen, NULL);
            }

            SDL_FreeSurface(processing_screen);

            /* draw multi-touch finger points */
            MultiTouchState *mts = get_emul_multi_touch_state();
            if (mts->multitouch_enable != 0 && mts->finger_point_surface != NULL) {
                FingerPoint *finger = NULL;
                int finger_point_size_half = mts->finger_point_size / 2;
                SDL_Rect rect;

                for (i = 0; i < mts->finger_cnt; i++) {
                    finger = get_finger_point_from_slot(i);
                    if (finger != NULL && finger->id != 0) {
                        rect.x = finger->origin_x - finger_point_size_half;
                        rect.y = finger->origin_y - finger_point_size_half;
                        rect.w = rect.h = mts->finger_point_size;

                        SDL_BlitSurface((SDL_Surface *)mts->finger_point_surface, NULL, surface_screen, &rect);
                    }
                }
            } //end  of draw multi-touch
        }
    }

    SDL_UpdateRect(surface_screen, 0, 0, 0, 0);
}


#ifdef SDL_THREAD
static void* run_qemu_update(void* arg)
{
    while(1) {
        pthread_mutex_lock(&sdl_mutex);

        pthread_cond_wait(&sdl_cond, &sdl_mutex);

        qemu_update();

        pthread_mutex_unlock(&sdl_mutex);
    }

    return NULL;
}
#endif

void maruskin_sdl_init(uint64 swt_handle, int lcd_size_width, int lcd_size_height, bool is_resize)
{
    gchar SDL_windowhack[32];
    SDL_SysWMinfo info;
    long window_id = swt_handle;

    INFO("maru sdl initialization = %d\n", is_resize);

    if (is_resize == FALSE) { //once
        sprintf(SDL_windowhack, "%ld", window_id);
        g_setenv("SDL_WINDOWID", SDL_windowhack, 1);
        INFO("register SDL environment variable. (SDL_WINDOWID = %s)\n", SDL_windowhack);

        if (SDL_Init(SDL_INIT_VIDEO) < 0 ) {
            ERR("unable to init SDL: %s\n", SDL_GetError());
        }

        set_emul_lcd_size(lcd_size_width, lcd_size_height);
        set_emul_sdl_bpp(SDL_BPP);
    }

    if (sdl_initialized == 0) {
        sdl_initialized = 1;

        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        init_multi_touch_state();

#ifndef _WIN32
        SDL_VERSION(&info.version);
        SDL_GetWMInfo(&info);
#endif
    }

    sdl_alteration = 1;

#ifdef SDL_THREAD
    if (sdl_thread_initialized == 0) {
        sdl_thread_initialized = 1;
        pthread_t thread_id;
        INFO("sdl update thread create\n");
        if (pthread_create(&thread_id, NULL, run_qemu_update, NULL) != 0) {
            ERR("pthread_create fail\n");
            return;
        }
    }
#endif
}

void maruskin_sdl_quit(void)
{
    /* remove multi-touch finger points */
    get_emul_multi_touch_state()->multitouch_enable = 0;
    clear_finger_slot();
    cleanup_multi_touch_state();

    if (sdl_opengl == 1) {
        glDeleteTextures(1, &texture);
    }

    SDL_Quit();
}


void maruskin_sdl_resize(void)
{
    SDL_Event ev;

    /* this fails if SDL is not initialized */
    memset(&ev, 0, sizeof(ev));
    ev.resize.type = SDL_VIDEORESIZE;

    //This function is thread safe, and can be called from other threads safely.
    SDL_PushEvent(&ev);
}

DisplaySurface* maruskin_sdl_get_display(void) {
    return qemu_display_surface;
}