2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "ScreenshotSurfaceGL.h"
11 #include "ServiceBroker.h"
12 #include "guilib/GUIComponent.h"
13 #include "guilib/GUIWindowManager.h"
14 #include "utils/Screenshot.h"
15 #include "windowing/GraphicContext.h"
21 #include "system_gl.h"
23 void CScreenshotSurfaceGL::Register()
25 CScreenShot::Register(CScreenshotSurfaceGL::CreateSurface
);
28 std::unique_ptr
<IScreenshotSurface
> CScreenshotSurfaceGL::CreateSurface()
30 return std::make_unique
<CScreenshotSurfaceGL
>();
33 bool CScreenshotSurfaceGL::Capture()
35 CWinSystemBase
* winsystem
= CServiceBroker::GetWinSystem();
39 CGUIComponent
* gui
= CServiceBroker::GetGUI();
43 std::unique_lock
<CCriticalSection
> lock(winsystem
->GetGfxContext());
44 gui
->GetWindowManager().Render();
46 glReadBuffer(GL_BACK
);
48 // get current viewport
50 glGetIntegerv(GL_VIEWPORT
, viewport
);
52 m_width
= viewport
[2] - viewport
[0];
53 m_height
= viewport
[3] - viewport
[1];
54 m_stride
= m_width
* 4;
55 std::vector
<uint8_t> surface(m_stride
* m_height
);
57 // read pixels from the backbuffer
58 glReadPixels(viewport
[0], viewport
[1], viewport
[2], viewport
[3], GL_BGRA
, GL_UNSIGNED_BYTE
, static_cast<GLvoid
*>(surface
.data()));
60 // make a new buffer and copy the read image to it with the Y axis inverted
61 m_buffer
= new unsigned char[m_stride
* m_height
];
62 for (int y
= 0; y
< m_height
; y
++)
63 memcpy(m_buffer
+ y
* m_stride
, surface
.data() + (m_height
- y
- 1) * m_stride
, m_stride
);
65 return m_buffer
!= nullptr;