Merge pull request #26126 from stephan49/fix-pipewire-unlock-error
[xbmc.git] / xbmc / cores / RetroPlayer / buffers / RenderBufferOpenGLES.cpp
blob8ed366ed7089663b15332d98158a135ae1a612a4
1 /*
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.
7 */
9 #include "RenderBufferOpenGLES.h"
11 #include "cores/RetroPlayer/rendering/RenderContext.h"
13 using namespace KODI;
14 using namespace RETRO;
16 CRenderBufferOpenGLES::CRenderBufferOpenGLES(CRenderContext& context,
17 GLuint pixeltype,
18 GLuint internalformat,
19 GLuint pixelformat,
20 GLuint bpp)
21 : m_context(context),
22 m_pixeltype(pixeltype),
23 m_internalformat(internalformat),
24 m_pixelformat(pixelformat),
25 m_bpp(bpp)
29 CRenderBufferOpenGLES::~CRenderBufferOpenGLES()
31 DeleteTexture();
34 void CRenderBufferOpenGLES::CreateTexture()
36 glGenTextures(1, &m_textureId);
38 glBindTexture(m_textureTarget, m_textureId);
40 glTexImage2D(m_textureTarget, 0, m_internalformat, m_width, m_height, 0, m_pixelformat,
41 m_pixeltype, NULL);
43 glTexParameteri(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
44 glTexParameteri(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
45 glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
46 glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
48 glBindTexture(m_textureTarget, 0);
51 bool CRenderBufferOpenGLES::UploadTexture()
53 if (!glIsTexture(m_textureId))
54 CreateTexture();
56 glBindTexture(m_textureTarget, m_textureId);
58 const int stride = GetFrameSize() / m_height;
60 glPixelStorei(GL_UNPACK_ALIGNMENT, m_bpp);
62 if (m_bpp == 4 && m_pixelformat == GL_RGBA)
64 // XOR Swap RGBA -> BGRA
65 // GLES 2.0 doesn't support strided textures (unless GL_UNPACK_ROW_LENGTH_EXT is supported)
66 uint8_t* pixels = const_cast<uint8_t*>(m_data.data());
67 for (unsigned int y = 0; y < m_height; ++y, pixels += stride)
69 for (int x = 0; x < stride; x += 4)
70 std::swap(pixels[x], pixels[x + 2]);
71 glTexSubImage2D(m_textureTarget, 0, 0, y, m_width, 1, m_pixelformat, m_pixeltype, pixels);
74 else if (m_context.IsExtSupported("GL_EXT_unpack_subimage"))
76 #ifdef GL_UNPACK_ROW_LENGTH_EXT
77 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / m_bpp);
78 glTexSubImage2D(m_textureTarget, 0, 0, 0, m_width, m_height, m_pixelformat, m_pixeltype,
79 m_data.data());
80 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
81 #endif
83 else
85 uint8_t* pixels = const_cast<uint8_t*>(m_data.data());
86 for (unsigned int y = 0; y < m_height; ++y, pixels += stride)
87 glTexSubImage2D(m_textureTarget, 0, 0, y, m_width, 1, m_pixelformat, m_pixeltype, pixels);
90 glBindTexture(m_textureTarget, 0);
92 return true;
95 void CRenderBufferOpenGLES::DeleteTexture()
97 if (glIsTexture(m_textureId))
98 glDeleteTextures(1, &m_textureId);
100 m_textureId = 0;