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"
20 #include "system_gl.h"
22 void CScreenshotSurfaceGL::Register()
24 CScreenShot::Register(CScreenshotSurfaceGL::CreateSurface
);
27 std::unique_ptr
<IScreenshotSurface
> CScreenshotSurfaceGL::CreateSurface()
29 return std::unique_ptr
<CScreenshotSurfaceGL
>(new CScreenshotSurfaceGL());
32 bool CScreenshotSurfaceGL::Capture()
34 CWinSystemBase
* winsystem
= CServiceBroker::GetWinSystem();
38 CGUIComponent
* gui
= CServiceBroker::GetGUI();
42 std::unique_lock
<CCriticalSection
> lock(winsystem
->GetGfxContext());
43 gui
->GetWindowManager().Render();
45 glReadBuffer(GL_BACK
);
47 // get current viewport
49 glGetIntegerv(GL_VIEWPORT
, viewport
);
51 m_width
= viewport
[2] - viewport
[0];
52 m_height
= viewport
[3] - viewport
[1];
53 m_stride
= m_width
* 4;
54 std::vector
<uint8_t> surface(m_stride
* m_height
);
56 // read pixels from the backbuffer
57 glReadPixels(viewport
[0], viewport
[1], viewport
[2], viewport
[3], GL_BGRA
, GL_UNSIGNED_BYTE
, static_cast<GLvoid
*>(surface
.data()));
59 // make a new buffer and copy the read image to it with the Y axis inverted
60 m_buffer
= new unsigned char[m_stride
* m_height
];
61 for (int y
= 0; y
< m_height
; y
++)
62 memcpy(m_buffer
+ y
* m_stride
, surface
.data() + (m_height
- y
- 1) * m_stride
, m_stride
);
64 return m_buffer
!= nullptr;