| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "RenderBufferOpenGLES.h" |
| 10 |
|
| 11 |
#include "cores/RetroPlayer/rendering/RenderContext.h" |
| 12 |
|
| 13 |
using namespace KODI; |
| 14 |
using namespace RETRO; |
| 15 |
|
| 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) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
CRenderBufferOpenGLES::~CRenderBufferOpenGLES() |
| 30 |
{ |
| 31 |
DeleteTexture(); |
| 32 |
} |
| 33 |
|
| 34 |
void CRenderBufferOpenGLES::CreateTexture() |
| 35 |
{ |
| 36 |
glGenTextures(1, &m_textureId); |
| 37 |
|
| 38 |
glBindTexture(m_textureTarget, m_textureId); |
| 39 |
|
| 40 |
glTexImage2D(m_textureTarget, 0, m_internalformat, m_width, m_height, 0, m_pixelformat, |
| 41 |
m_pixeltype, NULL); |
| 42 |
|
| 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); |
| 47 |
|
| 48 |
glBindTexture(m_textureTarget, 0); |
| 49 |
} |
| 50 |
|
| 51 |
bool CRenderBufferOpenGLES::UploadTexture() |
| 52 |
{ |
| 53 |
if (!glIsTexture(m_textureId)) |
| 54 |
CreateTexture(); |
| 55 |
|
| 56 |
glBindTexture(m_textureTarget, m_textureId); |
| 57 |
|
| 58 |
const int stride = GetFrameSize() / m_height; |
| 59 |
|
| 60 |
glPixelStorei(GL_UNPACK_ALIGNMENT, m_bpp); |
| 61 |
|
| 62 |
if (m_bpp == 4 && m_pixelformat == GL_RGBA) |
| 63 |
{ |
| 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) |
| 68 |
{ |
| 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); |
| 72 |
} |
| 73 |
} |
| 74 |
else if (m_context.IsExtSupported("GL_EXT_unpack_subimage")) |
| 75 |
{ |
| 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 |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 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); |
| 88 |
} |
| 89 |
|
| 90 |
glBindTexture(m_textureTarget, 0); |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
void CRenderBufferOpenGLES::DeleteTexture() |
| 96 |
{ |
| 97 |
if (glIsTexture(m_textureId)) |
| 98 |
glDeleteTextures(1, &m_textureId); |
| 99 |
|
| 100 |
m_textureId = 0; |
| 101 |
} |