forked from xbmc/xbmc
-
Notifications
You must be signed in to change notification settings - Fork 24
/
RBP.cpp
515 lines (432 loc) · 13.2 KB
/
RBP.cpp
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
505
506
507
508
509
510
511
512
513
514
515
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* 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, 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "RBP.h"
#if defined(TARGET_RASPBERRY_PI)
#include <assert.h>
#include "settings/Settings.h"
#include "settings/AdvancedSettings.h"
#include "utils/log.h"
#include "cores/omxplayer/OMXImage.h"
#include "guilib/GraphicContext.h"
#include "settings/DisplaySettings.h"
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include "rpi_user_vcsm.h"
#define MAJOR_NUM 100
#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *)
#define DEVICE_FILE_NAME "/dev/vcio"
typedef struct gpu_mem_ptr_s {
void *arm; // Pointer to memory mapped on ARM side
int vc_handle; // Videocore handle of relocatable memory
int vcsm_handle; // Handle for use by VCSM
int vc; // Address for use in GPU code
int numbytes; // Size of memory block
int suballoc;
} GPU_MEM_PTR_T;
static int mbox_open();
static void mbox_close(int file_desc);
static void gpu_free_internal(GPU_MEM_PTR_T *p, int mb);
static int gpu_malloc_uncached_internal(int numbytes, GPU_MEM_PTR_T *p, int mb);
CRBP::CRBP()
{
m_initialized = false;
m_omx_initialized = false;
m_DllBcmHost = new DllBcmHost();
m_OMX = new COMXCore();
m_display = DISPMANX_NO_HANDLE;
m_p = NULL;
m_x = 0;
m_y = 0;
m_enabled = 0;
m_mb = mbox_open();
vcsm_init();
m_vsync_count = 0;
}
CRBP::~CRBP()
{
Deinitialize();
delete m_OMX;
delete m_DllBcmHost;
}
void CRBP::InitializeSettings()
{
if (m_initialized && g_advancedSettings.m_cacheMemBufferSize == ~0U)
g_advancedSettings.m_cacheMemBufferSize = m_arm_mem < 256 ? 1024 * 1024 * 2 : 1024 * 1024 * 20;
}
bool CRBP::Initialize()
{
CSingleLock lock (m_critSection);
if (m_initialized)
return true;
m_initialized = m_DllBcmHost->Load();
if(!m_initialized)
return false;
m_DllBcmHost->bcm_host_init();
m_omx_initialized = m_OMX->Initialize();
if(!m_omx_initialized)
return false;
char response[80] = "";
m_arm_mem = 0;
m_gpu_mem = 0;
m_codec_mpg2_enabled = false;
m_codec_wvc1_enabled = false;
if (vc_gencmd(response, sizeof response, "get_mem arm") == 0)
vc_gencmd_number_property(response, "arm", &m_arm_mem);
if (vc_gencmd(response, sizeof response, "get_mem gpu") == 0)
vc_gencmd_number_property(response, "gpu", &m_gpu_mem);
if (vc_gencmd(response, sizeof response, "codec_enabled MPG2") == 0)
m_codec_mpg2_enabled = strcmp("MPG2=enabled", response) == 0;
if (vc_gencmd(response, sizeof response, "codec_enabled WVC1") == 0)
m_codec_wvc1_enabled = strcmp("WVC1=enabled", response) == 0;
if (m_gpu_mem < 128)
setenv("V3D_DOUBLE_BUFFER", "1", 1);
m_gui_resolution_limit = CSettings::GetInstance().GetInt("videoscreen.limitgui");
if (!m_gui_resolution_limit)
m_gui_resolution_limit = m_gpu_mem < 128 ? 720:1080;
InitializeSettings();
// in case xbcm was restarted when suspended
ResumeVideoOutput();
g_OMXImage.Initialize();
m_omx_image_init = true;
return true;
}
void CRBP::LogFirmwareVerison()
{
char response[1024];
m_DllBcmHost->vc_gencmd(response, sizeof response, "version");
response[sizeof(response) - 1] = '\0';
CLog::Log(LOGNOTICE, "Raspberry PI firmware version: %s", response);
CLog::Log(LOGNOTICE, "ARM mem: %dMB GPU mem: %dMB MPG2:%d WVC1:%d", m_arm_mem, m_gpu_mem, m_codec_mpg2_enabled, m_codec_wvc1_enabled);
CLog::Log(LOGNOTICE, "cacheMemBufferSize: %dMB", g_advancedSettings.m_cacheMemBufferSize >> 20);
m_DllBcmHost->vc_gencmd(response, sizeof response, "get_config int");
response[sizeof(response) - 1] = '\0';
CLog::Log(LOGNOTICE, "Config:\n%s", response);
m_DllBcmHost->vc_gencmd(response, sizeof response, "get_config str");
response[sizeof(response) - 1] = '\0';
CLog::Log(LOGNOTICE, "Config:\n%s", response);
}
static void vsync_callback_static(DISPMANX_UPDATE_HANDLE_T u, void *arg)
{
CRBP *rbp = reinterpret_cast<CRBP*>(arg);
rbp->VSyncCallback();
}
void CRBP::VSyncCallback()
{
CSingleLock lock(m_vsync_lock);
m_vsync_count++;
m_vsync_cond.notifyAll();
}
unsigned int CRBP::WaitVsync(unsigned int target)
{
CSingleLock lock(m_vsync_lock);
if (target == ~0U)
target = m_vsync_count+1;
if (m_display == DISPMANX_NO_HANDLE)
{
CLog::Log(LOGDEBUG, "CRBP::%s skipping while display closed", __func__);
return m_vsync_count;
}
while (m_vsync_count < target)
if (!m_vsync_cond.wait(lock, 100))
break;
return m_vsync_count;
}
DISPMANX_DISPLAY_HANDLE_T CRBP::OpenDisplay(uint32_t device)
{
if (m_display == DISPMANX_NO_HANDLE)
{
m_display = vc_dispmanx_display_open( 0 /*screen*/ );
int s = vc_dispmanx_vsync_callback(m_display, vsync_callback_static, (void *)this);
assert(s == 0);
init_cursor();
}
return m_display;
}
void CRBP::CloseDisplay(DISPMANX_DISPLAY_HANDLE_T display)
{
assert(display == m_display);
int s = vc_dispmanx_vsync_callback(m_display, NULL, NULL);
assert(s == 0);
uninit_cursor();
vc_dispmanx_display_close(m_display);
m_display = DISPMANX_NO_HANDLE;
}
void CRBP::GetDisplaySize(int &width, int &height)
{
DISPMANX_MODEINFO_T info;
if (vc_dispmanx_display_get_info(m_display, &info) == 0)
{
width = info.width;
height = info.height;
}
else
{
width = 0;
height = 0;
}
}
unsigned char *CRBP::CaptureDisplay(int width, int height, int *pstride, bool swap_red_blue, bool video_only)
{
DISPMANX_RESOURCE_HANDLE_T resource;
VC_RECT_T rect;
unsigned char *image = NULL;
uint32_t vc_image_ptr;
int stride;
uint32_t flags = 0;
if (video_only)
flags |= DISPMANX_SNAPSHOT_NO_RGB|DISPMANX_SNAPSHOT_FILL;
if (swap_red_blue)
flags |= DISPMANX_SNAPSHOT_SWAP_RED_BLUE;
if (!pstride)
flags |= DISPMANX_SNAPSHOT_PACK;
stride = ((width + 15) & ~15) * 4;
image = new unsigned char [height * stride];
if (image)
{
resource = vc_dispmanx_resource_create( VC_IMAGE_RGBA32, width, height, &vc_image_ptr );
assert(m_display != DISPMANX_NO_HANDLE);
vc_dispmanx_snapshot(m_display, resource, (DISPMANX_TRANSFORM_T)flags);
vc_dispmanx_rect_set(&rect, 0, 0, width, height);
vc_dispmanx_resource_read_data(resource, &rect, image, stride);
vc_dispmanx_resource_delete( resource );
}
if (pstride)
*pstride = stride;
return image;
}
void CRBP::Deinitialize()
{
if (m_omx_image_init)
g_OMXImage.Deinitialize();
if(m_omx_initialized)
m_OMX->Deinitialize();
if (m_display)
CloseDisplay(m_display);
m_DllBcmHost->bcm_host_deinit();
if(m_initialized)
m_DllBcmHost->Unload();
m_omx_image_init = false;
m_initialized = false;
m_omx_initialized = false;
uninit_cursor();
if (m_mb && m_p)
gpu_free_internal(m_p, m_mb);
delete m_p;
m_p = NULL;
if (m_mb)
mbox_close(m_mb);
m_mb = 0;
vcsm_exit();
}
void CRBP::SuspendVideoOutput()
{
CLog::Log(LOGDEBUG, "Raspberry PI suspending video output\n");
char response[80];
m_DllBcmHost->vc_gencmd(response, sizeof response, "display_power 0");
}
void CRBP::ResumeVideoOutput()
{
char response[80];
m_DllBcmHost->vc_gencmd(response, sizeof response, "display_power 1");
CLog::Log(LOGDEBUG, "Raspberry PI resuming video output\n");
}
static int mbox_property(int file_desc, void *buf)
{
int ret_val = ioctl(file_desc, IOCTL_MBOX_PROPERTY, buf);
if (ret_val < 0) {
printf("ioctl_set_msg failed:%d\n", ret_val);
}
return ret_val;
}
static int mbox_open()
{
int file_desc;
// open a char device file used for communicating with kernel mbox driver
file_desc = open(DEVICE_FILE_NAME, 0);
if (file_desc < 0) {
printf("Can't open device file: %s (%d)\n", DEVICE_FILE_NAME, file_desc);
printf("Try creating a device file with: sudo mknod %s c %d 0\n", DEVICE_FILE_NAME, MAJOR_NUM);
}
return file_desc;
}
static void mbox_close(int file_desc)
{
close(file_desc);
}
static unsigned mem_lock(int file_desc, unsigned handle)
{
int i=0;
unsigned p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request
p[i++] = 0x3000d; // (the tag id)
p[i++] = 4; // (size of the buffer)
p[i++] = 4; // (size of the data)
p[i++] = handle;
p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size
mbox_property(file_desc, p);
return p[5];
}
unsigned mem_unlock(int file_desc, unsigned handle)
{
int i=0;
unsigned p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request
p[i++] = 0x3000e; // (the tag id)
p[i++] = 4; // (size of the buffer)
p[i++] = 4; // (size of the data)
p[i++] = handle;
p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size
mbox_property(file_desc, p);
return p[5];
}
unsigned int mailbox_set_cursor_info(int file_desc, int width, int height, int format, uint32_t buffer, int hotspotx, int hotspoty)
{
int i=0;
unsigned int p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request
p[i++] = 0x00008010; // set cursor state
p[i++] = 24; // buffer size
p[i++] = 24; // data size
p[i++] = width;
p[i++] = height;
p[i++] = format;
p[i++] = buffer; // ptr to VC memory buffer. Doesn't work in 64bit....
p[i++] = hotspotx;
p[i++] = hotspoty;
p[i++] = 0x00000000; // end tag
p[0] = i*sizeof(*p); // actual size
mbox_property(file_desc, p);
return p[5];
}
unsigned int mailbox_set_cursor_position(int file_desc, int enabled, int x, int y)
{
int i=0;
unsigned p[32];
p[i++] = 0; // size
p[i++] = 0x00000000; // process request
p[i++] = 0x00008011; // set cursor state
p[i++] = 12; // buffer size
p[i++] = 12; // data size
p[i++] = enabled;
p[i++] = x;
p[i++] = y;
p[i++] = 0x00000000; // end tag
p[0] = i*sizeof *p; // actual size
mbox_property(file_desc, p);
return p[5];
}
static int gpu_malloc_uncached_internal(int numbytes, GPU_MEM_PTR_T *p, int mb)
{
//printf("%s %d\n", __func__, numbytes);
p->numbytes = numbytes;
p->suballoc = 0;
p->vcsm_handle = vcsm_malloc_cache(numbytes, VCSM_CACHE_TYPE_NONE, (char *)"Mouse pointer");
assert(p->vcsm_handle);
p->vc_handle = vcsm_vc_hdl_from_hdl(p->vcsm_handle);
assert(p->vc_handle);
p->arm = vcsm_lock(p->vcsm_handle);
assert(p->arm);
p->vc = mem_lock(mb, p->vc_handle);
assert(p->vc);
return 0;
}
static void gpu_free_internal(GPU_MEM_PTR_T *p, int mb)
{
mem_unlock(mb,p->vc_handle);
vcsm_unlock_ptr(p->arm);
vcsm_free(p->vcsm_handle);
}
#define T 0
#define W 0xffffffff
#define B 0xff000000
const static uint32_t default_cursor_pixels[] =
{
B,B,B,B,B,B,B,B,B,T,T,T,T,T,T,T,
B,W,W,W,W,W,W,B,T,T,T,T,T,T,T,T,
B,W,W,W,W,W,B,T,T,T,T,T,T,T,T,T,
B,W,W,W,W,B,T,T,T,T,T,T,T,T,T,T,
B,W,W,W,W,W,B,T,T,T,T,T,T,T,T,T,
B,W,W,B,W,W,W,B,T,T,T,T,T,T,T,T,
B,W,B,T,B,W,W,W,B,T,T,T,T,T,T,T,
B,B,T,T,T,B,W,W,W,B,T,T,T,T,T,T,
B,T,T,T,T,T,B,W,W,W,B,T,T,T,T,T,
T,T,T,T,T,T,T,B,W,W,W,B,T,T,T,T,
T,T,T,T,T,T,T,T,B,W,W,W,B,T,T,T,
T,T,T,T,T,T,T,T,T,B,W,W,W,B,T,T,
T,T,T,T,T,T,T,T,T,T,B,W,W,W,B,T,
T,T,T,T,T,T,T,T,T,T,T,B,W,W,W,B,
T,T,T,T,T,T,T,T,T,T,T,T,B,W,B,T,
T,T,T,T,T,T,T,T,T,T,T,T,T,B,T,T
};
#undef T
#undef W
#undef B
void CRBP::init_cursor()
{
//printf("%s\n", __func__);
if (!m_mb)
return;
if (!m_p)
{
m_p = new GPU_MEM_PTR_T;
if (m_p)
gpu_malloc_uncached_internal(64 * 64 * 4, m_p, m_mb);
}
if (m_mb && m_p && m_p->arm && m_p->vc)
set_cursor(default_cursor_pixels, 16, 16, 0, 0);
}
void CRBP::set_cursor(const void *pixels, int width, int height, int hotspot_x, int hotspot_y)
{
if (!m_mb || !m_p || !m_p->arm || !m_p->vc || !pixels || width * height > 64 * 64)
return;
//printf("%s %dx%d %p\n", __func__, width, height, pixels);
memcpy(m_p->arm, pixels, width * height * 4);
unsigned int s = mailbox_set_cursor_info(m_mb, width, height, 0, m_p->vc, hotspot_x, hotspot_y);
assert(s == 0);
}
void CRBP::update_cursor(int x, int y, bool enabled)
{
if (!m_mb || !m_p || !m_p->arm || !m_p->vc)
return;
RESOLUTION res = g_graphicsContext.GetVideoResolution();
CRect gui(0, 0, CDisplaySettings::GetInstance().GetResolutionInfo(res).iWidth, CDisplaySettings::GetInstance().GetResolutionInfo(res).iHeight);
CRect display(0, 0, CDisplaySettings::GetInstance().GetResolutionInfo(res).iScreenWidth, CDisplaySettings::GetInstance().GetResolutionInfo(res).iScreenHeight);
int x2 = x * display.Width() / gui.Width();
int y2 = y * display.Height() / gui.Height();
//printf("%s %d,%d (%d)\n", __func__, x, y, enabled);
if (m_x != x2 || m_y != y2 || m_enabled != enabled)
mailbox_set_cursor_position(m_mb, enabled, x2, y2);
m_x = x2;
m_y = y2;
m_enabled = enabled;
}
void CRBP::uninit_cursor()
{
if (!m_mb || !m_p || !m_p->arm || !m_p->vc)
return;
//printf("%s\n", __func__);
mailbox_set_cursor_position(m_mb, 0, 0, 0);
}
#endif