Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / linux / RBP.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "RBP.h"
22 #if defined(TARGET_RASPBERRY_PI)
23
24 #include "utils/log.h"
25
26 #include "cores/omxplayer/OMXImage.h"
27
28 CRBP::CRBP()
29 {
30   m_initialized     = false;
31   m_omx_initialized = false;
32   m_DllBcmHost      = new DllBcmHost();
33   m_OMX             = new COMXCore();
34 }
35
36 CRBP::~CRBP()
37 {
38   Deinitialize();
39   delete m_OMX;
40   delete m_DllBcmHost;
41 }
42
43 bool CRBP::Initialize()
44 {
45   CSingleLock lock (m_critSection);
46   if (m_initialized)
47     return true;
48
49   m_initialized = m_DllBcmHost->Load();
50   if(!m_initialized)
51     return false;
52
53   m_DllBcmHost->bcm_host_init();
54
55   m_omx_initialized = m_OMX->Initialize();
56   if(!m_omx_initialized)
57     return false;
58
59   char response[80] = "";
60   m_arm_mem = 0;
61   m_gpu_mem = 0;
62   m_codec_mpg2_enabled = false;
63   m_codec_wvc1_enabled = false;
64
65   if (vc_gencmd(response, sizeof response, "get_mem arm") == 0)
66     vc_gencmd_number_property(response, "arm", &m_arm_mem);
67   if (vc_gencmd(response, sizeof response, "get_mem gpu") == 0)
68     vc_gencmd_number_property(response, "gpu", &m_gpu_mem);
69
70   if (vc_gencmd(response, sizeof response, "codec_enabled MPG2") == 0)
71     m_codec_mpg2_enabled = strcmp("MPG2=enabled", response) == 0;
72   if (vc_gencmd(response, sizeof response, "codec_enabled WVC1") == 0)
73     m_codec_wvc1_enabled = strcmp("WVC1=enabled", response) == 0;
74
75   g_OMXImage.Initialize();
76   m_omx_image_init = true;
77   return true;
78 }
79
80 void CRBP::LogFirmwareVerison()
81 {
82   char  response[160];
83   m_DllBcmHost->vc_gencmd(response, sizeof response, "version");
84   response[sizeof(response) - 1] = '\0';
85   CLog::Log(LOGNOTICE, "Raspberry PI firmware version: %s", response);
86   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);
87 }
88
89 void CRBP::GetDisplaySize(int &width, int &height)
90 {
91   DISPMANX_DISPLAY_HANDLE_T display;
92   DISPMANX_MODEINFO_T info;
93
94   display = vc_dispmanx_display_open( 0 /*screen*/ );
95   if (vc_dispmanx_display_get_info(display, &info) == 0)
96   {
97     width = info.width;
98     height = info.height;
99   }
100   else
101   {
102     width = 0;
103     height = 0;
104   }
105   vc_dispmanx_display_close(display );
106 }
107
108 unsigned char *CRBP::CaptureDisplay(int width, int height, int *pstride, bool swap_red_blue, bool video_only)
109 {
110   DISPMANX_DISPLAY_HANDLE_T display;
111   DISPMANX_RESOURCE_HANDLE_T resource;
112   VC_RECT_T rect;
113   unsigned char *image = NULL;
114   uint32_t vc_image_ptr;
115   int stride;
116   uint32_t flags = 0;
117
118   if (video_only)
119     flags |= DISPMANX_SNAPSHOT_NO_RGB|DISPMANX_SNAPSHOT_FILL;
120   if (swap_red_blue)
121     flags |= DISPMANX_SNAPSHOT_SWAP_RED_BLUE;
122   if (!pstride)
123     flags |= DISPMANX_SNAPSHOT_PACK;
124
125   display = vc_dispmanx_display_open( 0 /*screen*/ );
126   stride = ((width + 15) & ~15) * 4;
127   image = new unsigned char [height * stride];
128
129   if (image)
130   {
131     resource = vc_dispmanx_resource_create( VC_IMAGE_RGBA32, width, height, &vc_image_ptr );
132
133     vc_dispmanx_snapshot(display, resource, (DISPMANX_TRANSFORM_T)flags);
134
135     vc_dispmanx_rect_set(&rect, 0, 0, width, height);
136     vc_dispmanx_resource_read_data(resource, &rect, image, stride);
137     vc_dispmanx_resource_delete( resource );
138     vc_dispmanx_display_close(display );
139   }
140   if (pstride)
141     *pstride = stride;
142   return image;
143 }
144
145 void CRBP::Deinitialize()
146 {
147   if (m_omx_image_init)
148     g_OMXImage.Deinitialize();
149
150   if(m_omx_initialized)
151     m_OMX->Deinitialize();
152
153   m_DllBcmHost->bcm_host_deinit();
154
155   if(m_initialized)
156     m_DllBcmHost->Unload();
157
158   m_omx_image_init  = false;
159   m_initialized     = false;
160   m_omx_initialized = false;
161 }
162 #endif