Initialize Tizen 2.3
[framework/graphics/cairo.git] / perf / cairo-perf.c
1 /*
2  * Copyright (c) 2007 Netlabs
3  * Copyright (c) 2006 Mozilla Corporation
4  * Copyright (c) 2006 Red Hat, Inc.
5  * Copyright (c) 2011 Andrea Canciani
6  *
7  * Permission to use, copy, modify, distribute, and sell this software
8  * and its documentation for any purpose is hereby granted without
9  * fee, provided that the above copyright notice appear in all copies
10  * and that both that copyright notice and this permission notice
11  * appear in supporting documentation, and that the name of
12  * the authors not be used in advertising or publicity pertaining to
13  * distribution of the software without specific, written prior
14  * permission. The authors make no representations about the
15  * suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
19  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
21  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
23  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
24  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25  *
26  * Authors: Peter Weilbacher <mozilla@weilbacher.org>
27  *          Vladimir Vukicevic <vladimir@pobox.com> (win32/linux code)
28  *          Carl Worth <cworth@cworth.org> (win32/linux code)
29  *          Andrea Canciani <ranma42@gmail.com>
30  */
31
32 #include "cairo-perf.h"
33 #include "../src/cairo-time-private.h"
34
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #if defined(__OS2__)
40 #define INCL_BASE
41 #include <os2.h>
42 #elif defined(_WIN32)
43 #define WIN32_LEAN_AND_MEAN
44 #include <windows.h>
45 #elif defined(_POSIX_PRIORITY_SCHEDULING)
46 #include <sched.h>
47 #endif
48
49 /* XXX: add thread-aware for gl backend */
50 #if CAIRO_HAS_GL_SURFACE || CAIRO_HAS_GLESV2_SURFACE
51 #include <cairo-gl.h>
52 #endif
53 void cairo_perf_set_thread_aware (cairo_t *cr, cairo_bool_t thread_aware)
54 {
55 #if CAIRO_HAS_GL_SURFACE || CAIRO_HAS_GLESV2_SURFACE
56     cairo_device_t *device = cairo_surface_get_device (cairo_get_target (cr));
57     if (cairo_device_get_type (device) == CAIRO_DEVICE_TYPE_GL)
58         cairo_gl_device_set_thread_aware (device, thread_aware);
59 #endif
60 }
61
62
63 /* timers */
64 static cairo_time_t timer;
65 static cairo_perf_timer_synchronize_t cairo_perf_timer_synchronize = NULL;
66 static void *cairo_perf_timer_synchronize_closure = NULL;
67
68 void
69 cairo_perf_timer_set_synchronize (cairo_perf_timer_synchronize_t  synchronize,
70                                   void                           *closure)
71 {
72     cairo_perf_timer_synchronize = synchronize;
73     cairo_perf_timer_synchronize_closure = closure;
74 }
75
76 void
77 cairo_perf_timer_start (void)
78 {
79     timer = _cairo_time_get ();
80 }
81
82 void
83 cairo_perf_timer_stop (void)
84 {
85     if (cairo_perf_timer_synchronize)
86         cairo_perf_timer_synchronize (cairo_perf_timer_synchronize_closure);
87
88     timer = _cairo_time_get_delta (timer);
89 }
90
91 cairo_time_t
92 cairo_perf_timer_elapsed (void)
93 {
94     return timer;
95 }
96
97 void
98 cairo_perf_yield (void)
99 {
100     /* try to deactivate this thread until the scheduler calls it again */
101
102 #if defined(__OS2__)
103     DosSleep (0);
104 #elif defined(_WIN32)
105     SleepEx(0, TRUE);
106 #elif defined(_POSIX_PRIORITY_SCHEDULING)
107     sched_yield ();
108 #else
109     sleep (0);
110 #endif
111 }