xref: /oneTBB/examples/common/gui/macvideo.cpp (revision d86ed7fb)
1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include "video.hpp"
18 #include <cassert>
19 #include <stdio.h>
20 #include <iostream>
21 #include <pthread.h>
22 
23 unsigned int *g_pImg = 0;
24 int g_sizex = 0, g_sizey = 0;
25 static video *g_video = 0;
26 static int g_fps = 0;
27 char *window_title = NULL;
28 #define WINDOW_TITLE_SIZE 256
29 int cocoa_update = 0;
30 
31 #include <sched.h>
32 #include <sys/time.h>
33 struct timeval g_time;
34 
35 video::video()
36 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
37         : depth(24),
38           red_shift(0),
39           green_shift(8),
40           blue_shift(16),
41           red_mask(0xff),
42           green_mask(0xff00),
43           blue_mask(0xff0000)
44 #else
45         : depth(24),
46           red_shift(16),
47           green_shift(8),
48           blue_shift(0),
49           red_mask(0xff0000),
50           green_mask(0xff00),
51           blue_mask(0xff)
52 #endif
53 {
54     assert(g_video == 0);
55     g_video = this;
56     title = "Video";
57     cocoa_update = 1;
58     updating = true;
59     calc_fps = false;
60 }
61 
62 bool video::init_window(int x, int y) {
63     g_sizex = x;
64     g_sizey = y;
65     g_pImg = new unsigned int[x * y];
66     if (window_title == NULL)
67         window_title = (char *)malloc(WINDOW_TITLE_SIZE);
68     strncpy(window_title, title, WINDOW_TITLE_SIZE - 1);
69     running = true;
70     return true;
71 }
72 
73 bool video::init_console() {
74     running = true;
75     return true;
76 }
77 
78 void video::terminate() {
79     if (calc_fps) {
80         double fps = g_fps;
81         struct timezone tz;
82         struct timeval end_time;
83         gettimeofday(&end_time, &tz);
84         fps /= (end_time.tv_sec + 1.0 * end_time.tv_usec / 1000000.0) -
85                (g_time.tv_sec + 1.0 * g_time.tv_usec / 1000000.0);
86         printf("%s: %.1f fps\n", title, fps);
87     }
88     g_video = 0;
89     running = false;
90     if (g_pImg) {
91         delete[] g_pImg;
92         g_pImg = 0;
93     }
94 }
95 
96 video::~video() {
97     if (g_video)
98         terminate();
99 }
100 
101 //! Count and display FPS count in titlebar
102 bool video::next_frame() {
103     if (calc_fps) {
104         if (!g_fps) {
105             struct timezone tz;
106             gettimeofday(&g_time, &tz);
107         }
108         g_fps++;
109     }
110     struct timezone tz;
111     struct timeval now_time;
112     gettimeofday(&now_time, &tz);
113     double sec = ((now_time.tv_sec + 1.0 * now_time.tv_usec / 1000000.0) -
114                   (g_time.tv_sec + 1.0 * g_time.tv_usec / 1000000.0));
115     if (sec > 1) {
116         if (calc_fps) {
117             memcpy(&g_time, &now_time, sizeof(g_time));
118             int fps;
119             fps = g_fps / sec;
120             cocoa_update = (int)updating;
121             snprintf(window_title,
122                      WINDOW_TITLE_SIZE,
123                      "%s%s: %d fps",
124                      title,
125                      updating ? "" : " (no updating)",
126                      int(fps));
127             g_fps = 0;
128         }
129     }
130     return running;
131 }
132 
133 void *thread_func(void *) {
134     g_video->on_process();
135     exit(EXIT_SUCCESS);
136 }
137 
138 extern "C" void on_mouse_func(int x, int y, int k) {
139     g_video->on_mouse(x, y, k);
140     return;
141 }
142 
143 extern "C" void on_key_func(int x) {
144     g_video->on_key(x);
145     return;
146 }
147 
148 extern "C" int cocoa_main(int argc, char *argv[]);
149 //! Do standard loop
150 void video::main_loop() {
151     pthread_t handle;
152     pthread_attr_t attr;
153     pthread_attr_init(&attr);
154     pthread_create(&handle, &attr, &thread_func, (void *)NULL);
155     pthread_detach(handle);
156     cocoa_main(0, NULL);
157 }
158 
159 //! Change window title
160 void video::show_title() {
161     if (title)
162         strncpy(window_title, title, WINDOW_TITLE_SIZE);
163     return;
164 }
165 
166 ///////////////////////////////////////////// public methods of video class ///////////////////////
167 
168 drawing_area::drawing_area(int x, int y, int sizex, int sizey)
169         : base_index(y * g_sizex + x),
170           max_index(g_sizex * g_sizey),
171           index_stride(g_sizex),
172           pixel_depth(24),
173           ptr32(g_pImg),
174           start_x(x),
175           start_y(y),
176           size_x(sizex),
177           size_y(sizey) {
178     assert(x < g_sizex);
179     assert(y < g_sizey);
180     assert(x + sizex <= g_sizex);
181     assert(y + sizey <= g_sizey);
182 
183     index = base_index; // current index
184 }
185 
186 void drawing_area::update() {
187     //nothing to do, updating via timer in cocoa part.
188 }
189