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
|
/*
* 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 "Screenshot.h"
#include "system.h"
#include <vector>
#include "Util.h"
#include "Application.h"
#include "windowing/WindowingFactory.h"
#include "pictures/Picture.h"
#ifdef TARGET_RASPBERRY_PI
#include "xbmc/linux/RBP.h"
#endif
#ifdef HAS_VIDEO_PLAYBACK
#include "cores/VideoRenderers/RenderManager.h"
#endif
#include "filesystem/File.h"
#include "guilib/GraphicContext.h"
#include "utils/JobManager.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
#include "settings/SettingPath.h"
#include "settings/Settings.h"
#include "settings/windows/GUIControlSettings.h"
using namespace std;
using namespace XFILE;
CScreenshotSurface::CScreenshotSurface()
{
m_width = 0;
m_height = 0;
m_stride = 0;
m_buffer = NULL;
}
CScreenshotSurface::~CScreenshotSurface()
{
delete m_buffer;
}
bool CScreenshotSurface::capture()
{
#if defined(TARGET_RASPBERRY_PI)
g_RBP.GetDisplaySize(m_width, m_height);
m_buffer = g_RBP.CaptureDisplay(m_width, m_height, &m_stride, true, false);
if (!m_buffer)
return false;
#elif defined(HAS_DX)
LPDIRECT3DSURFACE9 lpSurface = NULL, lpBackbuffer = NULL;
g_graphicsContext.Lock();
if (g_application.m_pPlayer->IsPlayingVideo())
{
#ifdef HAS_VIDEO_PLAYBACK
g_renderManager.SetupScreenshot();
#endif
}
g_application.RenderNoPresent();
if (FAILED(g_Windowing.Get3DDevice()->CreateOffscreenPlainSurface(g_Windowing.GetWidth(), g_Windowing.GetHeight(), D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &lpSurface, NULL)))
return false;
if (FAILED(g_Windowing.Get3DDevice()->GetRenderTarget(0, &lpBackbuffer)))
return false;
// now take screenshot
if (SUCCEEDED(g_Windowing.Get3DDevice()->GetRenderTargetData(lpBackbuffer, lpSurface)))
{
D3DLOCKED_RECT lr;
D3DSURFACE_DESC desc;
lpSurface->GetDesc(&desc);
if (SUCCEEDED(lpSurface->LockRect(&lr, NULL, D3DLOCK_READONLY)))
{
m_width = desc.Width;
m_height = desc.Height;
m_stride = lr.Pitch;
m_buffer = new unsigned char[m_height * m_stride];
memcpy(m_buffer, lr.pBits, m_height * m_stride);
lpSurface->UnlockRect();
}
else
{
CLog::Log(LOGERROR, "%s LockRect failed", __FUNCTION__);
}
}
else
{
CLog::Log(LOGERROR, "%s GetBackBuffer failed", __FUNCTION__);
}
lpSurface->Release();
lpBackbuffer->Release();
g_graphicsContext.Unlock();
#elif defined(HAS_GL) || defined(HAS_GLES)
g_graphicsContext.BeginPaint();
if (g_application.m_pPlayer->IsPlayingVideo())
{
#ifdef HAS_VIDEO_PLAYBACK
g_renderManager.SetupScreenshot();
#endif
}
g_application.RenderNoPresent();
#ifndef HAS_GLES
glReadBuffer(GL_BACK);
#endif
//get current viewport
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
m_width = viewport[2] - viewport[0];
m_height = viewport[3] - viewport[1];
m_stride = m_width * 4;
unsigned char* surface = new unsigned char[m_stride * m_height];
//read pixels from the backbuffer
#if HAS_GLES == 2
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)surface);
#else
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)surface);
#endif
g_graphicsContext.EndPaint();
//make a new buffer and copy the read image to it with the Y axis inverted
m_buffer = new unsigned char[m_stride * m_height];
for (int y = 0; y < m_height; y++)
{
#ifdef HAS_GLES
// we need to save in BGRA order so XOR Swap RGBA -> BGRA
unsigned char* swap_pixels = surface + (m_height - y - 1) * m_stride;
for (int x = 0; x < m_width; x++, swap_pixels+=4)
{
std::swap(swap_pixels[0], swap_pixels[2]);
}
#endif
memcpy(m_buffer + y * m_stride, surface + (m_height - y - 1) *m_stride, m_stride);
}
delete [] surface;
#else
//nothing to take a screenshot from
return false;
#endif
return true;
}
void CScreenShot::TakeScreenshot(const std::string &filename, bool sync)
{
CScreenshotSurface surface;
if (!surface.capture())
{
CLog::Log(LOGERROR, "Screenshot %s failed", filename.c_str());
return;
}
CLog::Log(LOGDEBUG, "Saving screenshot %s", filename.c_str());
//set alpha byte to 0xFF
for (int y = 0; y < surface.m_height; y++)
{
unsigned char* alphaptr = surface.m_buffer - 1 + y * surface.m_stride;
for (int x = 0; x < surface.m_width; x++)
*(alphaptr += 4) = 0xFF;
}
//if sync is true, the png file needs to be completely written when this function returns
if (sync)
{
if (!CPicture::CreateThumbnailFromSurface(surface.m_buffer, surface.m_width, surface.m_height, surface.m_stride, filename))
CLog::Log(LOGERROR, "Unable to write screenshot %s", filename.c_str());
delete [] surface.m_buffer;
surface.m_buffer = NULL;
}
else
{
//make sure the file exists to avoid concurrency issues
FILE* fp = fopen(filename.c_str(), "w");
if (fp)
fclose(fp);
else
CLog::Log(LOGERROR, "Unable to create file %s", filename.c_str());
//write .png file asynchronous with CThumbnailWriter, prevents stalling of the render thread
//buffer is deleted from CThumbnailWriter
CThumbnailWriter* thumbnailwriter = new CThumbnailWriter(surface.m_buffer, surface.m_width, surface.m_height, surface.m_stride, filename);
CJobManager::GetInstance().AddJob(thumbnailwriter, NULL);
surface.m_buffer = NULL;
}
}
void CScreenShot::TakeScreenshot()
{
static bool savingScreenshots = false;
static vector<std::string> screenShots;
bool promptUser = false;
std::string strDir;
// check to see if we have a screenshot folder yet
CSettingPath *screenshotSetting = (CSettingPath*)CSettings::Get().GetSetting("debug.screenshotpath");
if (screenshotSetting != NULL)
{
strDir = screenshotSetting->GetValue();
if (strDir.empty())
{
if (CGUIControlButtonSetting::GetPath(screenshotSetting))
strDir = screenshotSetting->GetValue();
}
}
if (strDir.empty())
{
strDir = "special://temp/";
if (!savingScreenshots)
{
promptUser = true;
savingScreenshots = true;
screenShots.clear();
}
}
URIUtils::RemoveSlashAtEnd(strDir);
if (!strDir.empty())
{
std::string file = CUtil::GetNextFilename(URIUtils::AddFileToFolder(strDir, "screenshot%03d.png"), 999);
if (!file.empty())
{
TakeScreenshot(file, false);
if (savingScreenshots)
screenShots.push_back(file);
if (promptUser)
{ // grab the real directory
std::string newDir;
if (screenshotSetting != NULL)
{
newDir = screenshotSetting->GetValue();
if (newDir.empty())
{
if (CGUIControlButtonSetting::GetPath(screenshotSetting))
newDir = screenshotSetting->GetValue();
}
}
if (!newDir.empty())
{
for (unsigned int i = 0; i < screenShots.size(); i++)
{
std::string file = CUtil::GetNextFilename(URIUtils::AddFileToFolder(newDir, "screenshot%03d.png"), 999);
CFile::Copy(screenShots[i], file);
}
screenShots.clear();
}
savingScreenshots = false;
}
}
else
{
CLog::Log(LOGWARNING, "Too many screen shots or invalid folder");
}
}
}
|