summaryrefslogtreecommitdiffstats
path: root/xbmc/rendering/gl/ScreenshotSurfaceGL.cpp
blob: 022611bece3d67d2bcda09133ea4325140d52953 (plain)
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
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "ScreenshotSurfaceGL.h"

#include "ServiceBroker.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "utils/Screenshot.h"
#include "windowing/GraphicContext.h"

#include <mutex>
#include <vector>

#include "system_gl.h"

void CScreenshotSurfaceGL::Register()
{
  CScreenShot::Register(CScreenshotSurfaceGL::CreateSurface);
}

std::unique_ptr<IScreenshotSurface> CScreenshotSurfaceGL::CreateSurface()
{
  return std::unique_ptr<CScreenshotSurfaceGL>(new CScreenshotSurfaceGL());
}

bool CScreenshotSurfaceGL::Capture()
{
  CWinSystemBase* winsystem = CServiceBroker::GetWinSystem();
  if (!winsystem)
    return false;

  CGUIComponent* gui = CServiceBroker::GetGUI();
  if (!gui)
    return false;

  std::unique_lock<CCriticalSection> lock(winsystem->GetGfxContext());
  gui->GetWindowManager().Render();

  glReadBuffer(GL_BACK);

  // 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;
  std::vector<uint8_t> surface(m_stride * m_height);

  // read pixels from the backbuffer
  glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_BGRA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(surface.data()));

  // 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++)
    memcpy(m_buffer + y * m_stride, surface.data() + (m_height - y - 1) * m_stride, m_stride);

  return m_buffer != nullptr;
}