2021-06-02 16:36:24 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "media/view/media_view_overlay_opengl.h"
|
|
|
|
|
2021-06-03 07:11:35 +00:00
|
|
|
#include "ui/gl/gl_shader.h"
|
2021-06-03 12:57:48 +00:00
|
|
|
#include "media/streaming/media_streaming_common.h"
|
2021-06-02 16:36:24 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2021-08-23 15:29:20 +00:00
|
|
|
#include "core/crash_reports.h"
|
2021-06-04 15:14:42 +00:00
|
|
|
#include "styles/style_media_view.h"
|
2021-06-02 16:36:24 +00:00
|
|
|
|
|
|
|
namespace Media::View {
|
2021-06-03 07:11:35 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using namespace Ui::GL;
|
|
|
|
|
|
|
|
constexpr auto kRadialLoadingOffset = 4;
|
|
|
|
constexpr auto kThemePreviewOffset = kRadialLoadingOffset + 4;
|
|
|
|
constexpr auto kDocumentBubbleOffset = kThemePreviewOffset + 4;
|
|
|
|
constexpr auto kSaveMsgOffset = kDocumentBubbleOffset + 4;
|
|
|
|
constexpr auto kFooterOffset = kSaveMsgOffset + 4;
|
|
|
|
constexpr auto kCaptionOffset = kFooterOffset + 4;
|
|
|
|
constexpr auto kGroupThumbsOffset = kCaptionOffset + 4;
|
2021-06-04 15:14:42 +00:00
|
|
|
constexpr auto kControlsOffset = kGroupThumbsOffset + 4;
|
|
|
|
constexpr auto kControlValues = 2 * 4 + 4 * 4;
|
2021-06-03 07:11:35 +00:00
|
|
|
|
2021-06-03 12:57:48 +00:00
|
|
|
[[nodiscard]] ShaderPart FragmentPlaceOnTransparentBackground() {
|
|
|
|
return {
|
|
|
|
.header = R"(
|
|
|
|
uniform vec4 transparentBg;
|
|
|
|
uniform vec4 transparentFg;
|
|
|
|
uniform float transparentSize;
|
|
|
|
)",
|
|
|
|
.body = R"(
|
|
|
|
vec2 checkboardLadder = floor(gl_FragCoord.xy / transparentSize);
|
|
|
|
float checkboard = mod(checkboardLadder.x + checkboardLadder.y, 2.0);
|
|
|
|
vec4 checkboardColor = checkboard * transparentBg
|
|
|
|
+ (1. - checkboard) * transparentFg;
|
|
|
|
result += checkboardColor * (1. - result.a);
|
|
|
|
)",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:11:35 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
OverlayWidget::RendererGL::RendererGL(not_null<OverlayWidget*> owner)
|
|
|
|
: _owner(owner) {
|
|
|
|
style::PaletteChanged(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_radialImage.invalidate();
|
|
|
|
_documentBubbleImage.invalidate();
|
|
|
|
_themePreviewImage.invalidate();
|
|
|
|
_saveMsgImage.invalidate();
|
|
|
|
_footerImage.invalidate();
|
|
|
|
_captionImage.invalidate();
|
2021-06-04 15:14:42 +00:00
|
|
|
invalidateControls();
|
2021-06-03 07:11:35 +00:00
|
|
|
}, _lifetime);
|
|
|
|
}
|
2021-06-02 16:36:24 +00:00
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::init(
|
|
|
|
not_null<QOpenGLWidget*> widget,
|
|
|
|
QOpenGLFunctions &f) {
|
2021-06-04 15:14:42 +00:00
|
|
|
constexpr auto kQuads = 8;
|
|
|
|
constexpr auto kQuadVertices = kQuads * 4;
|
|
|
|
constexpr auto kQuadValues = kQuadVertices * 4;
|
|
|
|
constexpr auto kControlsValues = kControlsCount * kControlValues;
|
|
|
|
constexpr auto kValues = kQuadValues + kControlsValues;
|
|
|
|
|
2021-06-03 07:11:35 +00:00
|
|
|
_contentBuffer.emplace();
|
|
|
|
_contentBuffer->setUsagePattern(QOpenGLBuffer::DynamicDraw);
|
|
|
|
_contentBuffer->create();
|
|
|
|
_contentBuffer->bind();
|
|
|
|
_contentBuffer->allocate(kValues * sizeof(GLfloat));
|
|
|
|
|
|
|
|
_textures.ensureCreated(f);
|
|
|
|
|
|
|
|
_imageProgram.emplace();
|
2021-06-03 12:57:48 +00:00
|
|
|
_texturedVertexShader = LinkProgram(
|
2021-06-03 07:11:35 +00:00
|
|
|
&*_imageProgram,
|
|
|
|
VertexShader({
|
|
|
|
VertexViewportTransform(),
|
|
|
|
VertexPassTextureCoord(),
|
|
|
|
}),
|
|
|
|
FragmentShader({
|
|
|
|
FragmentSampleARGB32Texture(),
|
2021-06-03 12:57:48 +00:00
|
|
|
})).vertex;
|
|
|
|
|
|
|
|
_withTransparencyProgram.emplace();
|
|
|
|
LinkProgram(
|
|
|
|
&*_withTransparencyProgram,
|
|
|
|
_texturedVertexShader,
|
|
|
|
FragmentShader({
|
|
|
|
FragmentSampleARGB32Texture(),
|
|
|
|
FragmentPlaceOnTransparentBackground(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
_yuv420Program.emplace();
|
|
|
|
LinkProgram(
|
|
|
|
&*_yuv420Program,
|
|
|
|
_texturedVertexShader,
|
|
|
|
FragmentShader({
|
|
|
|
FragmentSampleYUV420Texture(),
|
2021-06-03 07:11:35 +00:00
|
|
|
}));
|
|
|
|
|
2022-03-22 15:50:23 +00:00
|
|
|
_nv12Program.emplace();
|
|
|
|
LinkProgram(
|
|
|
|
&*_nv12Program,
|
|
|
|
_texturedVertexShader,
|
|
|
|
FragmentShader({
|
|
|
|
FragmentSampleNV12Texture(),
|
|
|
|
}));
|
|
|
|
|
2021-06-04 15:14:42 +00:00
|
|
|
_fillProgram.emplace();
|
|
|
|
LinkProgram(
|
|
|
|
&*_fillProgram,
|
|
|
|
VertexShader({ VertexViewportTransform() }),
|
|
|
|
FragmentShader({ FragmentStaticColor() }));
|
|
|
|
|
|
|
|
_controlsProgram.emplace();
|
|
|
|
LinkProgram(
|
|
|
|
&*_controlsProgram,
|
|
|
|
_texturedVertexShader,
|
|
|
|
FragmentShader({
|
|
|
|
FragmentSampleARGB32Texture(),
|
|
|
|
FragmentGlobalOpacity(),
|
|
|
|
}));
|
2021-08-23 15:29:20 +00:00
|
|
|
|
|
|
|
const auto renderer = reinterpret_cast<const char*>(
|
|
|
|
f.glGetString(GL_RENDERER));
|
|
|
|
CrashReports::SetAnnotation(
|
|
|
|
"OpenGL Renderer",
|
|
|
|
renderer ? renderer : "[nullptr]");
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::deinit(
|
|
|
|
not_null<QOpenGLWidget*> widget,
|
2021-09-03 18:46:12 +00:00
|
|
|
QOpenGLFunctions *f) {
|
2021-06-03 07:11:35 +00:00
|
|
|
_textures.destroy(f);
|
|
|
|
_imageProgram = std::nullopt;
|
2021-06-03 12:57:48 +00:00
|
|
|
_texturedVertexShader = nullptr;
|
|
|
|
_withTransparencyProgram = std::nullopt;
|
|
|
|
_yuv420Program = std::nullopt;
|
2022-03-22 15:50:23 +00:00
|
|
|
_nv12Program = std::nullopt;
|
2021-06-04 15:14:42 +00:00
|
|
|
_fillProgram = std::nullopt;
|
|
|
|
_controlsProgram = std::nullopt;
|
2021-06-03 07:11:35 +00:00
|
|
|
_contentBuffer = std::nullopt;
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 19:37:09 +00:00
|
|
|
void OverlayWidget::RendererGL::paint(
|
2021-06-02 16:36:24 +00:00
|
|
|
not_null<QOpenGLWidget*> widget,
|
2021-06-10 19:37:09 +00:00
|
|
|
QOpenGLFunctions &f) {
|
|
|
|
if (handleHideWorkaround(f)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-04 15:14:42 +00:00
|
|
|
const auto factor = widget->devicePixelRatio();
|
|
|
|
if (_factor != factor) {
|
|
|
|
_factor = factor;
|
|
|
|
_controlsImage.invalidate();
|
|
|
|
}
|
2021-06-11 15:51:43 +00:00
|
|
|
_blendingEnabled = false;
|
2021-06-10 19:37:09 +00:00
|
|
|
_viewport = widget->size();
|
2021-06-04 15:14:42 +00:00
|
|
|
_uniformViewport = QVector2D(
|
|
|
|
_viewport.width() * _factor,
|
|
|
|
_viewport.height() * _factor);
|
2021-06-02 16:36:24 +00:00
|
|
|
_f = &f;
|
|
|
|
_owner->paint(this);
|
2021-06-03 07:11:35 +00:00
|
|
|
_f = nullptr;
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 19:37:09 +00:00
|
|
|
std::optional<QColor> OverlayWidget::RendererGL::clearColor() {
|
|
|
|
if (Platform::IsWindows() && _owner->_hideWorkaround) {
|
|
|
|
return QColor(0, 0, 0, 0);
|
|
|
|
} else if (_owner->_fullScreenVideo) {
|
|
|
|
return st::mediaviewVideoBg->c;
|
|
|
|
} else {
|
|
|
|
return st::mediaviewBg->c;
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
2021-06-10 19:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OverlayWidget::RendererGL::handleHideWorkaround(QOpenGLFunctions &f) {
|
2021-06-02 16:36:24 +00:00
|
|
|
// This is needed on Windows,
|
|
|
|
// because on reopen it blinks with the last shown content.
|
2021-06-10 19:37:09 +00:00
|
|
|
return Platform::IsWindows() && _owner->_hideWorkaround;
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintBackground() {
|
2021-06-04 15:14:42 +00:00
|
|
|
_contentBuffer->bind();
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintTransformedVideoFrame(
|
2021-06-04 15:42:02 +00:00
|
|
|
ContentGeometry geometry) {
|
2021-06-03 12:57:48 +00:00
|
|
|
const auto data = _owner->videoFrameWithInfo();
|
|
|
|
if (data.format == Streaming::FrameFormat::None) {
|
|
|
|
return;
|
2022-01-21 15:01:33 +00:00
|
|
|
} else if (data.format == Streaming::FrameFormat::ARGB32) {
|
2022-02-10 16:10:42 +00:00
|
|
|
Assert(!data.image.isNull());
|
2022-01-21 15:01:33 +00:00
|
|
|
paintTransformedStaticContent(
|
2022-02-10 16:10:42 +00:00
|
|
|
data.image,
|
2022-01-21 15:01:33 +00:00
|
|
|
geometry,
|
|
|
|
data.alpha,
|
|
|
|
data.alpha);
|
2021-06-03 12:57:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-22 15:50:23 +00:00
|
|
|
Assert(!data.yuv->size.isEmpty());
|
|
|
|
const auto program = (data.format == Streaming::FrameFormat::NV12)
|
|
|
|
? &*_nv12Program
|
|
|
|
: &*_yuv420Program;
|
|
|
|
program->bind();
|
|
|
|
const auto nv12 = (data.format == Streaming::FrameFormat::NV12);
|
|
|
|
const auto yuv = data.yuv;
|
2022-03-30 13:07:35 +00:00
|
|
|
const auto nv12changed = (_chromaNV12 != nv12);
|
2021-06-03 12:57:48 +00:00
|
|
|
|
|
|
|
const auto upload = (_trackFrameIndex != data.index)
|
|
|
|
|| (_streamedIndex != _owner->streamedIndex());
|
|
|
|
_trackFrameIndex = data.index;
|
|
|
|
_streamedIndex = _owner->streamedIndex();
|
|
|
|
|
|
|
|
_f->glActiveTexture(GL_TEXTURE0);
|
2021-06-04 09:50:41 +00:00
|
|
|
_textures.bind(*_f, 1);
|
2021-06-03 12:57:48 +00:00
|
|
|
if (upload) {
|
|
|
|
_f->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
|
|
uploadTexture(
|
2021-07-22 10:53:13 +00:00
|
|
|
GL_ALPHA,
|
|
|
|
GL_ALPHA,
|
2021-06-03 12:57:48 +00:00
|
|
|
yuv->size,
|
|
|
|
_lumaSize,
|
|
|
|
yuv->y.stride,
|
|
|
|
yuv->y.data);
|
|
|
|
_lumaSize = yuv->size;
|
|
|
|
}
|
|
|
|
_f->glActiveTexture(GL_TEXTURE1);
|
2021-06-04 09:50:41 +00:00
|
|
|
_textures.bind(*_f, 2);
|
2021-06-03 12:57:48 +00:00
|
|
|
if (upload) {
|
|
|
|
uploadTexture(
|
2022-03-22 15:50:23 +00:00
|
|
|
nv12 ? GL_RG : GL_ALPHA,
|
|
|
|
nv12 ? GL_RG : GL_ALPHA,
|
2021-06-03 12:57:48 +00:00
|
|
|
yuv->chromaSize,
|
2022-03-30 13:07:35 +00:00
|
|
|
nv12changed ? QSize() : _chromaSize,
|
2022-03-22 15:50:23 +00:00
|
|
|
yuv->u.stride / (nv12 ? 2 : 1),
|
2021-06-03 12:57:48 +00:00
|
|
|
yuv->u.data);
|
2022-03-28 13:39:25 +00:00
|
|
|
if (nv12) {
|
|
|
|
_chromaSize = yuv->chromaSize;
|
|
|
|
_f->glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
2022-03-30 13:07:35 +00:00
|
|
|
_chromaNV12 = nv12;
|
2021-06-03 12:57:48 +00:00
|
|
|
}
|
2022-03-22 15:50:23 +00:00
|
|
|
if (!nv12) {
|
|
|
|
_f->glActiveTexture(GL_TEXTURE2);
|
|
|
|
_textures.bind(*_f, 3);
|
|
|
|
if (upload) {
|
|
|
|
uploadTexture(
|
|
|
|
GL_ALPHA,
|
|
|
|
GL_ALPHA,
|
|
|
|
yuv->chromaSize,
|
|
|
|
_chromaSize,
|
|
|
|
yuv->v.stride,
|
|
|
|
yuv->v.data);
|
|
|
|
_chromaSize = yuv->chromaSize;
|
|
|
|
_f->glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
program->setUniformValue("y_texture", GLint(0));
|
|
|
|
if (nv12) {
|
|
|
|
program->setUniformValue("uv_texture", GLint(1));
|
|
|
|
} else {
|
|
|
|
program->setUniformValue("u_texture", GLint(1));
|
|
|
|
program->setUniformValue("v_texture", GLint(2));
|
2021-06-03 12:57:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 11:28:39 +00:00
|
|
|
toggleBlending(false);
|
2022-03-22 15:50:23 +00:00
|
|
|
paintTransformedContent(program, geometry);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintTransformedStaticContent(
|
|
|
|
const QImage &image,
|
2021-06-04 15:42:02 +00:00
|
|
|
ContentGeometry geometry,
|
2021-06-14 11:28:39 +00:00
|
|
|
bool semiTransparent,
|
2021-06-02 16:36:24 +00:00
|
|
|
bool fillTransparentBackground) {
|
2021-06-07 06:25:44 +00:00
|
|
|
Expects(image.isNull()
|
|
|
|
|| image.format() == QImage::Format_RGB32
|
|
|
|
|| image.format() == QImage::Format_ARGB32_Premultiplied);
|
|
|
|
|
|
|
|
if (geometry.rect.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-03 12:57:48 +00:00
|
|
|
auto &program = fillTransparentBackground
|
|
|
|
? _withTransparencyProgram
|
|
|
|
: _imageProgram;
|
2021-06-08 09:25:07 +00:00
|
|
|
program->bind();
|
2021-06-03 12:57:48 +00:00
|
|
|
if (fillTransparentBackground) {
|
|
|
|
program->setUniformValue(
|
|
|
|
"transparentBg",
|
2021-06-07 15:04:50 +00:00
|
|
|
st::mediaviewTransparentBg->c);
|
2021-06-03 12:57:48 +00:00
|
|
|
program->setUniformValue(
|
|
|
|
"transparentFg",
|
2021-06-07 15:04:50 +00:00
|
|
|
st::mediaviewTransparentFg->c);
|
2021-06-03 12:57:48 +00:00
|
|
|
program->setUniformValue(
|
|
|
|
"transparentSize",
|
|
|
|
st::transparentPlaceholderSize * _factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
_f->glActiveTexture(GL_TEXTURE0);
|
|
|
|
_textures.bind(*_f, 0);
|
2021-06-07 06:25:44 +00:00
|
|
|
const auto cacheKey = image.isNull() ? qint64(-1) : image.cacheKey();
|
2021-06-03 12:57:48 +00:00
|
|
|
const auto upload = (_cacheKey != cacheKey);
|
|
|
|
if (upload) {
|
2021-06-04 15:14:42 +00:00
|
|
|
_cacheKey = cacheKey;
|
2021-06-07 06:25:44 +00:00
|
|
|
if (image.isNull()) {
|
|
|
|
// Upload transparent 2x2 texture.
|
|
|
|
const auto stride = 2;
|
|
|
|
const uint32_t data[4] = { 0 };
|
|
|
|
uploadTexture(
|
2021-07-14 17:04:08 +00:00
|
|
|
Ui::GL::kFormatRGBA,
|
|
|
|
Ui::GL::kFormatRGBA,
|
2021-06-07 06:25:44 +00:00
|
|
|
QSize(2, 2),
|
|
|
|
_rgbaSize,
|
|
|
|
stride,
|
|
|
|
data);
|
|
|
|
} else {
|
|
|
|
const auto stride = image.bytesPerLine() / 4;
|
|
|
|
const auto data = image.constBits();
|
|
|
|
uploadTexture(
|
2021-07-14 17:04:08 +00:00
|
|
|
Ui::GL::kFormatRGBA,
|
|
|
|
Ui::GL::kFormatRGBA,
|
2021-06-07 06:25:44 +00:00
|
|
|
image.size(),
|
|
|
|
_rgbaSize,
|
|
|
|
stride,
|
|
|
|
data);
|
|
|
|
_rgbaSize = image.size();
|
|
|
|
}
|
2021-06-03 12:57:48 +00:00
|
|
|
}
|
2021-06-06 06:18:25 +00:00
|
|
|
program->setUniformValue("s_texture", GLint(0));
|
2021-06-03 12:57:48 +00:00
|
|
|
|
2021-06-14 11:28:39 +00:00
|
|
|
toggleBlending(semiTransparent && !fillTransparentBackground);
|
2021-06-04 15:42:02 +00:00
|
|
|
paintTransformedContent(&*program, geometry);
|
2021-06-03 12:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintTransformedContent(
|
|
|
|
not_null<QOpenGLShaderProgram*> program,
|
2021-06-04 15:42:02 +00:00
|
|
|
ContentGeometry geometry) {
|
|
|
|
const auto rect = transformRect(geometry.rect);
|
|
|
|
const auto centerx = rect.x() + rect.width() / 2;
|
|
|
|
const auto centery = rect.y() + rect.height() / 2;
|
2021-06-08 21:06:01 +00:00
|
|
|
const auto rsin = float(std::sin(geometry.rotation * M_PI / 180.));
|
|
|
|
const auto rcos = float(std::cos(geometry.rotation * M_PI / 180.));
|
2021-06-04 15:42:02 +00:00
|
|
|
const auto rotated = [&](float x, float y) -> std::array<float, 2> {
|
|
|
|
x -= centerx;
|
|
|
|
y -= centery;
|
2021-06-08 21:06:01 +00:00
|
|
|
return std::array<float, 2>{
|
2021-06-04 15:42:02 +00:00
|
|
|
centerx + (x * rcos + y * rsin),
|
|
|
|
centery + (y * rcos - x * rsin)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const auto topleft = rotated(rect.left(), rect.top());
|
|
|
|
const auto topright = rotated(rect.right(), rect.top());
|
|
|
|
const auto bottomright = rotated(rect.right(), rect.bottom());
|
|
|
|
const auto bottomleft = rotated(rect.left(), rect.bottom());
|
2021-06-03 07:11:35 +00:00
|
|
|
const GLfloat coords[] = {
|
2021-06-04 15:42:02 +00:00
|
|
|
topleft[0], topleft[1],
|
2021-06-06 06:18:25 +00:00
|
|
|
0.f, 1.f,
|
2021-06-03 07:11:35 +00:00
|
|
|
|
2021-06-04 15:42:02 +00:00
|
|
|
topright[0], topright[1],
|
2021-06-06 06:18:25 +00:00
|
|
|
1.f, 1.f,
|
2021-06-03 07:11:35 +00:00
|
|
|
|
2021-06-04 15:42:02 +00:00
|
|
|
bottomright[0], bottomright[1],
|
2021-06-06 06:18:25 +00:00
|
|
|
1.f, 0.f,
|
2021-06-03 07:11:35 +00:00
|
|
|
|
2021-06-04 15:42:02 +00:00
|
|
|
bottomleft[0], bottomleft[1],
|
2021-06-06 06:18:25 +00:00
|
|
|
0.f, 0.f,
|
2021-06-03 07:11:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_contentBuffer->write(0, coords, sizeof(coords));
|
|
|
|
|
2021-06-04 15:14:42 +00:00
|
|
|
program->setUniformValue("viewport", _uniformViewport);
|
2021-06-03 07:11:35 +00:00
|
|
|
|
2021-06-03 12:57:48 +00:00
|
|
|
FillTexturedRectangle(*_f, &*program);
|
2021-06-03 07:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::uploadTexture(
|
|
|
|
GLint internalformat,
|
|
|
|
GLint format,
|
|
|
|
QSize size,
|
|
|
|
QSize hasSize,
|
|
|
|
int stride,
|
|
|
|
const void *data) const {
|
|
|
|
_f->glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
|
|
|
|
if (hasSize != size) {
|
|
|
|
_f->glTexImage2D(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
internalformat,
|
|
|
|
size.width(),
|
|
|
|
size.height(),
|
|
|
|
0,
|
|
|
|
format,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
data);
|
|
|
|
} else {
|
|
|
|
_f->glTexSubImage2D(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
size.width(),
|
|
|
|
size.height(),
|
|
|
|
format,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
_f->glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintRadialLoading(
|
|
|
|
QRect inner,
|
|
|
|
bool radial,
|
|
|
|
float64 radialOpacity) {
|
2021-06-03 07:11:35 +00:00
|
|
|
paintUsingRaster(_radialImage, inner, [&](Painter &&p) {
|
2021-06-02 16:36:24 +00:00
|
|
|
const auto newInner = QRect(QPoint(), inner.size());
|
|
|
|
_owner->paintRadialLoadingContent(p, newInner, radial, radialOpacity);
|
2021-06-03 07:11:35 +00:00
|
|
|
}, kRadialLoadingOffset, true);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintThemePreview(QRect outer) {
|
2021-06-03 07:11:35 +00:00
|
|
|
paintUsingRaster(_themePreviewImage, outer, [&](Painter &&p) {
|
2021-06-02 16:36:24 +00:00
|
|
|
const auto newOuter = QRect(QPoint(), outer.size());
|
|
|
|
_owner->paintThemePreviewContent(p, newOuter, newOuter);
|
2021-06-03 07:11:35 +00:00
|
|
|
}, kThemePreviewOffset);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintDocumentBubble(
|
|
|
|
QRect outer,
|
|
|
|
QRect icon) {
|
2021-06-03 07:11:35 +00:00
|
|
|
paintUsingRaster(_documentBubbleImage, outer, [&](Painter &&p) {
|
2021-06-02 16:36:24 +00:00
|
|
|
const auto newOuter = QRect(QPoint(), outer.size());
|
|
|
|
const auto newIcon = icon.translated(-outer.topLeft());
|
|
|
|
_owner->paintDocumentBubbleContent(p, newOuter, newIcon, newOuter);
|
2021-06-03 07:11:35 +00:00
|
|
|
}, kDocumentBubbleOffset);
|
2021-06-02 16:36:24 +00:00
|
|
|
_owner->paintRadialLoading(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintSaveMsg(QRect outer) {
|
2021-06-03 07:11:35 +00:00
|
|
|
paintUsingRaster(_saveMsgImage, outer, [&](Painter &&p) {
|
2021-06-02 16:36:24 +00:00
|
|
|
const auto newOuter = QRect(QPoint(), outer.size());
|
|
|
|
_owner->paintSaveMsgContent(p, newOuter, newOuter);
|
2021-06-03 07:11:35 +00:00
|
|
|
}, kSaveMsgOffset, true);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:14:42 +00:00
|
|
|
void OverlayWidget::RendererGL::paintControlsStart() {
|
|
|
|
validateControls();
|
|
|
|
_f->glActiveTexture(GL_TEXTURE0);
|
|
|
|
_controlsImage.bind(*_f);
|
|
|
|
toggleBlending(true);
|
|
|
|
}
|
|
|
|
|
2021-06-02 16:36:24 +00:00
|
|
|
void OverlayWidget::RendererGL::paintControl(
|
|
|
|
OverState control,
|
|
|
|
QRect outer,
|
|
|
|
float64 outerOpacity,
|
|
|
|
QRect inner,
|
|
|
|
float64 innerOpacity,
|
|
|
|
const style::icon &icon) {
|
2021-06-04 15:14:42 +00:00
|
|
|
const auto meta = ControlMeta(control);
|
|
|
|
Assert(meta.icon == &icon);
|
|
|
|
|
|
|
|
const auto &bg = st::mediaviewControlBg->c;
|
2021-09-27 08:13:57 +00:00
|
|
|
const auto bgAlpha = int(base::SafeRound(bg.alpha() * outerOpacity));
|
2021-06-04 15:14:42 +00:00
|
|
|
const auto offset = kControlsOffset + (meta.index * kControlValues) / 4;
|
|
|
|
const auto fgOffset = offset + 2;
|
2021-06-10 13:16:17 +00:00
|
|
|
const auto bgRect = transformRect(outer);
|
2021-06-04 15:14:42 +00:00
|
|
|
const auto iconRect = _controlsImage.texturedRect(
|
|
|
|
inner,
|
|
|
|
_controlsTextures[meta.index]);
|
|
|
|
const auto iconGeometry = transformRect(iconRect.geometry);
|
|
|
|
const GLfloat coords[] = {
|
|
|
|
bgRect.left(), bgRect.top(),
|
|
|
|
bgRect.right(), bgRect.top(),
|
|
|
|
bgRect.right(), bgRect.bottom(),
|
|
|
|
bgRect.left(), bgRect.bottom(),
|
|
|
|
|
|
|
|
iconGeometry.left(), iconGeometry.top(),
|
|
|
|
iconRect.texture.left(), iconRect.texture.bottom(),
|
|
|
|
|
|
|
|
iconGeometry.right(), iconGeometry.top(),
|
|
|
|
iconRect.texture.right(), iconRect.texture.bottom(),
|
|
|
|
|
|
|
|
iconGeometry.right(), iconGeometry.bottom(),
|
|
|
|
iconRect.texture.right(), iconRect.texture.top(),
|
|
|
|
|
|
|
|
iconGeometry.left(), iconGeometry.bottom(),
|
|
|
|
iconRect.texture.left(), iconRect.texture.top(),
|
|
|
|
};
|
|
|
|
if (!outer.isEmpty() && bgAlpha > 0) {
|
|
|
|
_contentBuffer->write(
|
|
|
|
offset * 4 * sizeof(GLfloat),
|
|
|
|
coords,
|
|
|
|
sizeof(coords));
|
2021-06-08 09:25:07 +00:00
|
|
|
_fillProgram->bind();
|
2021-06-04 15:14:42 +00:00
|
|
|
_fillProgram->setUniformValue("viewport", _uniformViewport);
|
|
|
|
FillRectangle(
|
|
|
|
*_f,
|
|
|
|
&*_fillProgram,
|
|
|
|
offset,
|
|
|
|
QColor(bg.red(), bg.green(), bg.blue(), bgAlpha));
|
|
|
|
} else {
|
|
|
|
_contentBuffer->write(
|
|
|
|
fgOffset * 4 * sizeof(GLfloat),
|
|
|
|
coords + (fgOffset - offset) * 4,
|
|
|
|
sizeof(coords) - (fgOffset - offset) * 4 * sizeof(GLfloat));
|
|
|
|
}
|
2021-06-08 09:25:07 +00:00
|
|
|
_controlsProgram->bind();
|
2021-06-04 15:14:42 +00:00
|
|
|
_controlsProgram->setUniformValue("g_opacity", GLfloat(innerOpacity));
|
|
|
|
_controlsProgram->setUniformValue("viewport", _uniformViewport);
|
|
|
|
FillTexturedRectangle(*_f, &*_controlsProgram, fgOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto OverlayWidget::RendererGL::ControlMeta(OverState control)
|
|
|
|
-> Control {
|
|
|
|
switch (control) {
|
|
|
|
case OverLeftNav: return { 0, &st::mediaviewLeft };
|
|
|
|
case OverRightNav: return { 1, &st::mediaviewRight };
|
|
|
|
case OverClose: return { 2, &st::mediaviewClose };
|
|
|
|
case OverSave: return { 3, &st::mediaviewSave };
|
|
|
|
case OverRotate: return { 4, &st::mediaviewRotate };
|
|
|
|
case OverMore: return { 5, &st::mediaviewMore };
|
|
|
|
}
|
|
|
|
Unexpected("Control value in OverlayWidget::RendererGL::ControlIndex.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::validateControls() {
|
|
|
|
if (!_controlsImage.image().isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto metas = {
|
|
|
|
ControlMeta(OverLeftNav),
|
|
|
|
ControlMeta(OverRightNav),
|
|
|
|
ControlMeta(OverClose),
|
|
|
|
ControlMeta(OverSave),
|
|
|
|
ControlMeta(OverRotate),
|
|
|
|
ControlMeta(OverMore),
|
|
|
|
};
|
|
|
|
auto maxWidth = 0;
|
|
|
|
auto fullHeight = 0;
|
2021-07-11 20:16:28 +00:00
|
|
|
for (const auto &meta : metas) {
|
2021-06-04 15:14:42 +00:00
|
|
|
maxWidth = std::max(meta.icon->width(), maxWidth);
|
|
|
|
fullHeight += meta.icon->height();
|
|
|
|
}
|
|
|
|
auto image = QImage(
|
|
|
|
QSize(maxWidth, fullHeight) * _factor,
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
image.fill(Qt::transparent);
|
|
|
|
image.setDevicePixelRatio(_factor);
|
|
|
|
{
|
|
|
|
auto p = QPainter(&image);
|
|
|
|
auto index = 0;
|
|
|
|
auto height = 0;
|
2021-07-11 20:16:28 +00:00
|
|
|
for (const auto &meta : metas) {
|
2021-06-04 15:14:42 +00:00
|
|
|
meta.icon->paint(p, 0, height, maxWidth);
|
|
|
|
_controlsTextures[index++] = QRect(
|
|
|
|
QPoint(0, height) * _factor,
|
|
|
|
meta.icon->size() * _factor);
|
|
|
|
height += meta.icon->height();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_controlsImage.setImage(std::move(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::invalidateControls() {
|
|
|
|
_controlsImage.invalidate();
|
|
|
|
ranges::fill(_controlsTextures, QRect());
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintFooter(QRect outer, float64 opacity) {
|
2021-06-03 07:11:35 +00:00
|
|
|
paintUsingRaster(_footerImage, outer, [&](Painter &&p) {
|
2021-06-02 16:36:24 +00:00
|
|
|
const auto newOuter = QRect(QPoint(), outer.size());
|
|
|
|
_owner->paintFooterContent(p, newOuter, newOuter, opacity);
|
2021-06-03 07:11:35 +00:00
|
|
|
}, kFooterOffset, true);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintCaption(QRect outer, float64 opacity) {
|
2021-06-03 07:11:35 +00:00
|
|
|
paintUsingRaster(_captionImage, outer, [&](Painter &&p) {
|
2021-06-02 16:36:24 +00:00
|
|
|
const auto newOuter = QRect(QPoint(), outer.size());
|
|
|
|
_owner->paintCaptionContent(p, newOuter, newOuter, opacity);
|
2021-06-03 07:11:35 +00:00
|
|
|
}, kCaptionOffset, true);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintGroupThumbs(
|
|
|
|
QRect outer,
|
|
|
|
float64 opacity) {
|
2021-06-03 07:11:35 +00:00
|
|
|
paintUsingRaster(_groupThumbsImage, outer, [&](Painter &&p) {
|
2021-06-02 16:36:24 +00:00
|
|
|
const auto newOuter = QRect(QPoint(), outer.size());
|
|
|
|
_owner->paintGroupThumbsContent(p, newOuter, newOuter, opacity);
|
2021-06-03 07:11:35 +00:00
|
|
|
}, kGroupThumbsOffset, true);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 12:57:48 +00:00
|
|
|
void OverlayWidget::RendererGL::invalidate() {
|
|
|
|
_trackFrameIndex = -1;
|
|
|
|
_streamedIndex = -1;
|
|
|
|
const auto images = {
|
|
|
|
&_radialImage,
|
|
|
|
&_documentBubbleImage,
|
|
|
|
&_themePreviewImage,
|
|
|
|
&_saveMsgImage,
|
|
|
|
&_footerImage,
|
|
|
|
&_captionImage,
|
|
|
|
&_groupThumbsImage,
|
2021-06-04 15:14:42 +00:00
|
|
|
&_controlsImage,
|
2021-06-03 12:57:48 +00:00
|
|
|
};
|
|
|
|
for (const auto image : images) {
|
|
|
|
image->setImage(QImage());
|
|
|
|
}
|
2021-06-04 15:14:42 +00:00
|
|
|
invalidateControls();
|
2021-06-03 12:57:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 07:11:35 +00:00
|
|
|
void OverlayWidget::RendererGL::paintUsingRaster(
|
|
|
|
Ui::GL::Image &image,
|
|
|
|
QRect rect,
|
2021-06-02 16:36:24 +00:00
|
|
|
Fn<void(Painter&&)> method,
|
2021-06-03 07:11:35 +00:00
|
|
|
int bufferOffset,
|
|
|
|
bool transparent) {
|
|
|
|
auto raster = image.takeImage();
|
|
|
|
const auto size = rect.size() * _factor;
|
|
|
|
if (raster.width() < size.width() || raster.height() < size.height()) {
|
|
|
|
raster = QImage(size, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
raster.setDevicePixelRatio(_factor);
|
|
|
|
if (!transparent
|
|
|
|
&& (raster.width() > size.width()
|
|
|
|
|| raster.height() > size.height())) {
|
|
|
|
raster.fill(Qt::transparent);
|
|
|
|
}
|
|
|
|
} else if (raster.devicePixelRatio() != _factor) {
|
|
|
|
raster.setDevicePixelRatio(_factor);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
2021-06-03 07:11:35 +00:00
|
|
|
|
|
|
|
if (transparent) {
|
|
|
|
raster.fill(Qt::transparent);
|
|
|
|
}
|
|
|
|
method(Painter(&raster));
|
|
|
|
|
2021-06-15 12:49:53 +00:00
|
|
|
_f->glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
|
|
|
image.setImage(std::move(raster), size);
|
|
|
|
image.bind(*_f);
|
2021-06-03 07:11:35 +00:00
|
|
|
|
|
|
|
const auto textured = image.texturedRect(rect, QRect(QPoint(), size));
|
|
|
|
const auto geometry = transformRect(textured.geometry);
|
|
|
|
const GLfloat coords[] = {
|
|
|
|
geometry.left(), geometry.top(),
|
|
|
|
textured.texture.left(), textured.texture.bottom(),
|
|
|
|
|
|
|
|
geometry.right(), geometry.top(),
|
|
|
|
textured.texture.right(), textured.texture.bottom(),
|
|
|
|
|
|
|
|
geometry.right(), geometry.bottom(),
|
|
|
|
textured.texture.right(), textured.texture.top(),
|
|
|
|
|
|
|
|
geometry.left(), geometry.bottom(),
|
|
|
|
textured.texture.left(), textured.texture.top(),
|
|
|
|
};
|
|
|
|
_contentBuffer->write(
|
|
|
|
bufferOffset * 4 * sizeof(GLfloat),
|
|
|
|
coords,
|
|
|
|
sizeof(coords));
|
|
|
|
|
2021-06-08 09:25:07 +00:00
|
|
|
_imageProgram->bind();
|
2021-06-04 15:14:42 +00:00
|
|
|
_imageProgram->setUniformValue("viewport", _uniformViewport);
|
2021-06-03 07:11:35 +00:00
|
|
|
_imageProgram->setUniformValue("s_texture", GLint(0));
|
|
|
|
|
|
|
|
toggleBlending(transparent);
|
|
|
|
FillTexturedRectangle(*_f, &*_imageProgram, bufferOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::toggleBlending(bool enabled) {
|
|
|
|
if (_blendingEnabled == enabled) {
|
|
|
|
return;
|
|
|
|
} else if (enabled) {
|
|
|
|
_f->glEnable(GL_BLEND);
|
|
|
|
_f->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
} else {
|
|
|
|
_f->glDisable(GL_BLEND);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
2021-06-03 07:11:35 +00:00
|
|
|
_blendingEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect OverlayWidget::RendererGL::transformRect(const Rect &raster) const {
|
|
|
|
return TransformRect(raster, _viewport, _factor);
|
|
|
|
}
|
|
|
|
|
2021-06-04 15:42:02 +00:00
|
|
|
Rect OverlayWidget::RendererGL::transformRect(const QRectF &raster) const {
|
|
|
|
return TransformRect(raster, _viewport, _factor);
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:11:35 +00:00
|
|
|
Rect OverlayWidget::RendererGL::transformRect(const QRect &raster) const {
|
|
|
|
return TransformRect(Rect(raster), _viewport, _factor);
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Media::View
|