tizen 2.4 release
[framework/uifw/e17-mod-tizen-comp.git] / src / hwcomp / hwcomp_util.c
1 #include "e_mod_comp_shared_types.h"
2 #include "e_mod_comp.h"
3 #include "e_mod_comp_atoms.h"
4 #include "e_mod_comp_debug.h"
5
6 #include <X11/extensions/hwc.h>
7
8 #include "hwcomp_debug.h"
9 #include "hwcomp_util.h"
10
11 #ifdef USE_HWC
12
13 #define E_HWCOMP_COMP_COUNTDOWN  30
14 #define E_HWCOMP_SET_COUNTDOWN   1
15
16 static Eina_Bool _hwc_available = EINA_FALSE;
17 static int _hwc_max_layer;
18 static int _hwc_major, _hwc_minor;
19 static Ecore_X_Display *_display = NULL;
20 static int _hwc_op_code;
21
22 EAPI void
23 hwcomp_util_hwcomp_enable(Ecore_X_Window win)
24 {
25    int hwc_event, hwc_error;
26    _hwc_major = 2;
27    _hwc_minor = 0;
28
29    if (!_display) _display = ecore_x_display_get();
30
31    if (XQueryExtension(_display, HWC_NAME, &_hwc_op_code, &hwc_event, &hwc_error))
32      {
33         HWCQueryVersion(_display, &_hwc_major, &_hwc_minor);
34         if (!HWCOpen(_display, 0, &_hwc_max_layer)) /* TODO: Get active CRTC */
35           {
36              _hwc_available = EINA_FALSE;
37              return;
38           }
39         HWCSelectInput(_display, win, HWCAllEvents);
40         _hwc_available = EINA_TRUE;
41      }
42    else
43       _hwc_available = EINA_FALSE;
44 }
45
46 EAPI Ecore_X_Display*
47 hwcomp_util_display_get(void)
48 {
49    return _display;
50 }
51
52 EAPI Eina_Bool
53 hwcomp_util_hwcomp_query(void)
54 {
55    return _hwc_available;
56 }
57
58 EAPI int
59 hwcomp_util_max_layer_get(void)
60 {
61    return _hwc_max_layer;
62 }
63
64 EAPI void
65 hwcomp_util_max_layer_set(int maxLayer)
66 {
67    _hwc_max_layer = maxLayer;
68 }
69
70 EAPI int
71 hwcomp_util_get_event_data(Ecore_X_Event_Generic *e)
72 {
73    if (!e) return -1;
74
75    if (e->extension == _hwc_op_code && e->evtype == HWCConfigureNotify)
76      {
77         HWCConfigureNotifyCookie *data = (HWCConfigureNotifyCookie *)e->data;
78         return data->maxLayer;
79      }
80    return -1;
81 }
82
83 EAPI void
84 hwcomp_util_turn_composite(E_Comp_HWComp *hwcomp, Eina_Bool on)
85 {
86
87    E_Comp_Canvas *canvas = hwcomp->canvas;
88    Eina_Bool manual_render_set = !on;
89
90    if (ecore_evas_manual_render_get(canvas->ee) != manual_render_set)
91       ecore_evas_manual_render_set(canvas->ee, manual_render_set);
92 }
93
94 EAPI Eina_Bool
95 hwcomp_util_check_fullcomp_mode(E_Comp_Win *cw)
96 {
97    Eina_Bool ret = EINA_FALSE;
98
99    /* Mini app */
100    if (STATE_INSET_CHECK(cw) && cw->bd->lock_user_location)
101       ret = EINA_TRUE;
102
103    /* Resizing of window */
104    if (cw->needpix)
105       ret= EINA_TRUE;
106
107    /* Moving of window */
108    if ((STATE_INSET_CHECK(cw) || CLASS_ICONIC_CHECK(cw)) &&
109        cw->hwc.geo_changed)
110      {
111         cw->hwc.geo_changed--;
112         ret= EINA_TRUE;
113      }
114
115    /* split */
116    if (cw->bd->client.e.state.ly.curr_ly)
117       ret= EINA_TRUE;
118
119    return ret;
120 }
121
122 EAPI const char*
123 hwcomp_util_border_name_get(E_Border *bd)
124 {
125    if (!bd) return NULL;
126
127    if (bd->client.icccm.name && strcmp(bd->client.icccm.name, "NORMAL_WINDOW"))
128       return bd->client.icccm.name;
129    else if (bd->client.netwm.name)
130       return bd->client.netwm.name;
131    else if (bd->client.icccm.class)
132       return bd->client.icccm.class;
133    else
134       return NULL;
135 }
136
137 EAPI void
138 hwcomp_util_dec_set_countdown(E_Comp_HWComp_Drawable *d)
139 {
140    if (d->set_countdown <= 0) return;
141
142    d->set_countdown--;
143 }
144
145 EAPI void
146 hwcomp_util_reset_set_countdown(E_Comp_HWComp_Drawable *d)
147 {
148    d->set_countdown = E_HWCOMP_SET_COUNTDOWN;
149 }
150
151 EAPI void
152 hwcomp_util_dec_comp_countdown(E_Comp_HWComp_Drawable *d)
153 {
154    if (d->comp_countdown <= 0) return;
155
156    d->comp_countdown--;
157 }
158
159 EAPI void
160 hwcomp_util_reset_comp_countdown(E_Comp_HWComp_Drawable *d)
161 {
162    d->comp_countdown = E_HWCOMP_COMP_COUNTDOWN;
163 }
164
165 EAPI Eina_Bool
166 hwcomp_util_update_check_region(E_Comp_HWComp_Update *hwc_update, E_Comp_Win *cw, int w, int h, int max_w, int max_h)
167 {
168    Eina_Bool ret = EINA_FALSE;
169    int area = 0;
170    int max = max_w*max_h/2;
171
172    _hwcomp_dbg_elbf(ELBT_COMP, 0, 0, "%15.15s| d:%p (%dx%d)", "HWC:WIN REGION",
173                     cw->bd->client.win, w, h);
174
175    area = w * h;
176    if (area > max)
177       ret = EINA_TRUE;
178
179 #if OPTIMIZED_HWC_MOBILE
180    return EINA_TRUE;
181 #else
182    return ret;
183 #endif
184
185 }
186
187 EAPI void
188 hwcomp_util_update_countdown(E_Comp_HWComp_Update *hwc_update, E_Comp_Win *cw)
189 {
190    E_Comp_HWComp_Drawable *hwc_drawable = NULL;
191    int i = 0;
192    int num_drawable = 0;
193
194    if (hwc_update->update_mode != E_HWCOMP_USE_HYBRIDCOMP_MODE) return;
195
196    num_drawable = hwc_update->num_drawable;
197
198    for (i = 0; i < num_drawable; i++)
199      {
200         hwc_drawable = hwc_update->hwc_drawable[i];
201
202         if (hwc_drawable && hwc_drawable->cw == cw)
203           {
204              if (hwc_drawable->set_drawable)
205                {
206                   hwcomp_util_reset_comp_countdown(hwc_drawable);
207
208                   if (i != 0 && hwc_drawable->first_update)
209                     {
210                        hwc_drawable->set_drawable = EINA_FALSE;
211                        hwc_drawable->first_update = 0;
212                     }
213                }
214              else
215                {
216                   if (hwc_drawable->region_update)
217                     {
218                        hwcomp_util_dec_set_countdown(hwc_drawable);
219                        hwc_drawable->region_update = EINA_FALSE;
220                     }
221                }
222              _hwcomp_dbg_elbf(ELBT_COMP, 0, 0, "%15.15s| client.win: %p set:%d set_cnt:%d comp_cnt:%d fu(%d)",
223                               "HWC:CW Update", cw->bd->client.win, hwc_drawable->set_drawable, hwc_drawable->set_countdown, hwc_drawable->comp_countdown, hwc_drawable->first_update);
224           }
225      }
226 }
227
228 EAPI Eina_Bool
229 hwcomp_util_update_check_resized(E_Comp_HWComp_Update *hwc_update)
230 {
231    int i;
232
233    for (i = 0; i < hwc_update->num_drawable; i++)
234      {
235         if (hwc_update->hwc_drawable[i]->resized)
236            return EINA_TRUE;
237      }
238
239    return EINA_FALSE;
240 }
241
242 EAPI void
243 hwcomp_util_update_unset_resized(E_Comp_HWComp_Update *hwc_update, E_Comp_Win *cw)
244 {
245    int i;
246    for (i = 0; i < hwc_update->num_drawable; i++)
247      {
248         if (cw == hwc_update->hwc_drawable[i]->cw)
249           {
250              if (hwc_update->hwc_drawable[i]->resized)
251                {
252                   hwc_update->hwc_drawable[i]->resized = EINA_FALSE;
253                   break;
254                }
255           }
256      }
257 }
258
259 EAPI void
260 hwcomp_util_update_set_resized(E_Comp_HWComp_Update *hwc_update, E_Comp_Win *cw)
261 {
262    int i;
263
264    for (i = 0; i < hwc_update->num_drawable; i++)
265      {
266         if (cw == hwc_update->hwc_drawable[i]->cw)
267           {
268              hwc_update->hwc_drawable[i]->resized = EINA_TRUE;
269              break;
270           }
271      }
272 }
273
274 EAPI Eina_Bool
275 hwcomp_util_update_check_comp(E_Comp_HWComp_Update *hwc_update)
276 {
277    return hwc_update->comp_update;
278 }
279
280 EAPI void
281 hwcomp_util_update_set_comp(E_Comp_HWComp_Update *hwc_update, Eina_Bool comp_update)
282 {
283    if (hwc_update->comp_update == comp_update) return;
284
285    hwc_update->comp_update = comp_update;
286 }
287
288
289 EAPI Eina_Bool
290 hwcomp_util_update_compare_drawables(E_Comp_HWComp_Update *cur_hwc_update, E_Comp_HWComp_Update *new_hwc_update)
291 {
292    E_Comp_HWComp_Drawable *cur_d = NULL;
293    E_Comp_HWComp_Drawable *new_d = NULL;
294    int i;
295
296    if (cur_hwc_update->num_drawable != new_hwc_update->num_drawable)
297       return EINA_FALSE;
298
299    for (i = 0; i < cur_hwc_update->num_drawable; i++)
300      {
301         cur_d = cur_hwc_update->hwc_drawable[i];
302         new_d = new_hwc_update->hwc_drawable[i];
303
304         if (cur_d->d != new_d->d)
305            return EINA_FALSE;
306
307 #if OPTIMIZED_HWC_MOBILE
308         if (cur_hwc_update->ime_present && new_hwc_update->ime_present)
309           {
310              _hwcomp_dbg_elbf(ELBT_COMP, 0, 0, "%15.15s|ime_old(%d,%d,%d,%d)->ime_new(%d,%d,%d,%d)", "HWC:IME",
311                               cur_hwc_update->ime_rect.x, cur_hwc_update->ime_rect.y, cur_hwc_update->ime_rect.w, cur_hwc_update->ime_rect.h,
312                               new_hwc_update->ime_rect.x, new_hwc_update->ime_rect.y, new_hwc_update->ime_rect.w, new_hwc_update->ime_rect.h);
313
314              if (cur_hwc_update->ime_rect.x != new_hwc_update->ime_rect.x)
315                 return EINA_FALSE;
316
317              if (cur_hwc_update->ime_rect.y != new_hwc_update->ime_rect.y)
318                 return EINA_FALSE;
319
320              if (cur_hwc_update->ime_rect.w != new_hwc_update->ime_rect.w)
321                 return EINA_FALSE;
322
323              if (cur_hwc_update->ime_rect.h != new_hwc_update->ime_rect.h)
324                 return EINA_FALSE;
325           }
326 #endif
327      }
328    return EINA_TRUE;
329 }
330
331 EAPI void
332 hwcomp_util_update_migrate_drawables(E_Comp_HWComp_Update *cur_hwc_update, E_Comp_HWComp_Update *new_hwc_update)
333 {
334    E_Comp_HWComp_Drawable *cur_d = NULL;
335    E_Comp_HWComp_Drawable *new_d = NULL;
336    int i, j;
337
338    for (i = 0; i < new_hwc_update->num_drawable; i++)
339      {
340         new_d = new_hwc_update->hwc_drawable[i];
341         for (j = 0; j < cur_hwc_update->num_drawable; j++)
342           {
343              cur_d = cur_hwc_update->hwc_drawable[i];
344              if (cur_d->d == new_d->d)
345                {
346                   new_d->comp_countdown = cur_d->comp_countdown;
347                   new_d->set_countdown = cur_d->set_countdown;
348                   new_d->set_drawable = cur_d->set_drawable;
349                   new_d->first_update = cur_d->first_update;
350                   new_d->update_count = cur_d->update_count;
351
352                   break;
353                }
354           }
355      }
356 }
357
358 EAPI void
359 hwcomp_util_update_destroy_drawable(E_Comp_HWComp_Update *hwc_update)
360 {
361    int i;
362    E_Comp_Win *hide_cw = NULL;
363    E_Comp_HWComp_Drawable *hwc_drawable = NULL;
364
365    for (i = 1; i < hwc_update->num_drawable; i++)
366      {
367         hwc_drawable = hwc_update->hwc_drawable[i];
368         hide_cw = hwc_drawable->cw;
369         if (hwc_update->update_mode == E_HWCOMP_USE_HYBRIDCOMP_MODE &&
370             hide_cw->hwc.set_drawable == EINA_FALSE)
371            e_mod_comp_win_hwcomp_mask_objs_hide(hide_cw);
372      }
373
374    if (hwc_update->hwc_drawable)
375      {
376         for (i = 0; i < hwc_update->num_overlays; i++)
377           {
378              if (!hwc_update->hwc_drawable[i])
379                {
380                   E_FREE(hwc_update->hwc_drawable[i]);
381                   hwc_update->hwc_drawable[i] = NULL;
382                }
383           }
384         E_FREE(hwc_update->hwc_drawable);
385         hwc_update->hwc_drawable = NULL;
386      }
387 }
388
389 EAPI Eina_Bool
390 hwcomp_util_update_create_drawable(E_Comp_HWComp_Update *hwc_update)
391 {
392    int i;
393    int num = hwc_update->num_overlays;
394
395    hwc_update->hwc_drawable = E_NEW(E_Comp_HWComp_Drawable *, num);
396    E_CHECK_RETURN(hwc_update->hwc_drawable, 0);
397    memset(hwc_update->hwc_drawable, 0x0, num * sizeof(E_Comp_HWComp_Drawable *));
398
399    for (i = 0; i < num; i++)
400      {
401         hwc_update->hwc_drawable[i] = E_NEW(E_Comp_HWComp_Drawable, 1);
402         if (!hwc_update->hwc_drawable[i]) goto fail;
403      }
404
405    return EINA_TRUE;
406 fail:
407    hwcomp_util_update_destroy_drawable(hwc_update);
408
409    return EINA_FALSE;
410 }
411
412 EAPI void
413 hwcomp_util_update_clear_drawable(E_Comp_HWComp_Update *hwc_update)
414 {
415    int i;
416
417    for (i = 0; i < hwc_update->num_overlays; i++)
418      {
419         memset(hwc_update->hwc_drawable[i], 0x0, sizeof(E_Comp_HWComp_Drawable));
420      }
421 }
422
423 EAPI void
424 hwcomp_util_update_destroy(E_Comp_HWComp_Update *hwc_update)
425 {
426    if (!hwc_update) return;
427
428    hwcomp_util_update_destroy_drawable(hwc_update);
429
430    E_FREE(hwc_update);
431    hwc_update = NULL;
432 }
433
434 EAPI E_Comp_HWComp_Update *
435 hwcomp_util_update_create(E_Comp_HWComp *hwcomp, int num)
436 {
437    E_Comp_HWComp_Update *hwc_update = NULL;
438    Eina_Bool ret = EINA_FALSE;
439
440    hwc_update = E_NEW(E_Comp_HWComp_Update, 1);
441    E_CHECK_RETURN(hwc_update, 0);
442
443    hwc_update->update_mode = E_HWCOMP_USE_INVALID;
444    hwc_update->num_overlays = num;
445    ret = hwcomp_util_update_create_drawable(hwc_update);
446    if (ret == EINA_FALSE)
447      {
448         hwcomp_util_update_destroy(hwc_update);
449         return NULL;
450      }
451    hwc_update->num_drawable = 0;
452    hwc_update->comp_update = EINA_FALSE;
453
454    hwc_update->hwcomp = hwcomp;
455
456    return hwc_update;
457 }
458
459 EAPI void
460 hwcomp_util_update_clear(E_Comp_HWComp_Update *hwc_update)
461 {
462    int i;
463    E_Comp_Win *hide_cw = NULL;
464    E_Comp_HWComp_Drawable *hwc_drawable = NULL;
465
466    for (i = 1; i < hwc_update->num_drawable; i++)
467      {
468         hwc_drawable = hwc_update->hwc_drawable[i];
469         hide_cw = hwc_drawable->cw;
470         e_mod_comp_win_hwcomp_mask_objs_hide(hide_cw);
471      }
472
473    hwc_update->update_mode = E_HWCOMP_USE_INVALID;
474    hwc_update->num_drawable = 0;
475    hwc_update->comp_update = EINA_FALSE;
476    hwcomp_util_update_clear_drawable(hwc_update);
477 }
478
479 EAPI void
480 hwcomp_util_update_reset_maskobj(E_Comp_HWComp_Update *hwc_update)
481 {
482    E_CHECK(hwc_update);
483    int i;
484    E_Comp_Win *hide_cw = NULL;
485    E_Comp_HWComp_Drawable *hwc_drawable = NULL;
486
487    for (i = 1; i < hwc_update->num_drawable; i++)
488      {
489         hwc_drawable = hwc_update->hwc_drawable[i];
490         hide_cw = hwc_drawable->cw;
491         if (hide_cw)
492            e_mod_comp_win_hwcomp_mask_objs_hide(hide_cw);
493      }
494 }
495
496 EAPI void
497 hwcomp_util_set_hybrid_composite(E_Comp_HWComp_Update *hwc_update)
498 {
499    E_CHECK(hwc_update);
500
501    E_Comp_HWComp_Drawable *hwc_drawable = NULL;
502    int i;
503
504    for (i = 1; i < hwc_update->num_drawable; i++)
505      {
506         hwc_drawable = hwc_update->hwc_drawable[i];
507         hwc_drawable->set_drawable = EINA_TRUE;
508         hwc_drawable->first_update = 1;
509         hwcomp_util_reset_comp_countdown(hwc_drawable);
510         hwcomp_util_reset_set_countdown(hwc_drawable);
511      }
512 }
513 #endif /* End of USE_HWC */
514
515
516
517