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-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
|
|
|
}));
|
|
|
|
|
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-06-02 16:36:24 +00:00
|
|
|
_background.init(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::deinit(
|
|
|
|
not_null<QOpenGLWidget*> widget,
|
|
|
|
QOpenGLFunctions &f) {
|
|
|
|
_background.deinit(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;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::resize(
|
|
|
|
not_null<QOpenGLWidget*> widget,
|
|
|
|
QOpenGLFunctions &f,
|
|
|
|
int w,
|
|
|
|
int h) {
|
2021-06-04 15:14:42 +00:00
|
|
|
const auto factor = widget->devicePixelRatio();
|
|
|
|
if (_factor != factor) {
|
|
|
|
_factor = factor;
|
|
|
|
_controlsImage.invalidate();
|
|
|
|
}
|
|
|
|
_viewport = QSize{ w, h };
|
|
|
|
_uniformViewport = QVector2D(
|
|
|
|
_viewport.width() * _factor,
|
|
|
|
_viewport.height() * _factor);
|
2021-06-02 16:36:24 +00:00
|
|
|
setDefaultViewport(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::setDefaultViewport(QOpenGLFunctions &f) {
|
2021-06-04 15:14:42 +00:00
|
|
|
f.glViewport(0, 0, _uniformViewport.x(), _uniformViewport.y());
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paint(
|
|
|
|
not_null<QOpenGLWidget*> widget,
|
|
|
|
QOpenGLFunctions &f) {
|
|
|
|
if (handleHideWorkaround(f)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_f = &f;
|
|
|
|
_owner->paint(this);
|
2021-06-03 07:11:35 +00:00
|
|
|
_f = nullptr;
|
2021-06-02 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OverlayWidget::RendererGL::handleHideWorkaround(QOpenGLFunctions &f) {
|
|
|
|
if (!Platform::IsWindows() || !_owner->_hideWorkaround) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// This is needed on Windows,
|
|
|
|
// because on reopen it blinks with the last shown content.
|
|
|
|
f.glClearColor(0., 0., 0., 0.);
|
|
|
|
f.glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverlayWidget::RendererGL::paintBackground() {
|
|
|
|
const auto &bg = _owner->_fullScreenVideo
|
|
|
|
? st::mediaviewVideoBg
|
|
|
|
: st::mediaviewBg;
|
|
|
|
auto fill = QRegion(QRect(QPoint(), _viewport));
|
2021-06-03 07:11:35 +00:00
|
|
|
toggleBlending(false);
|
2021-06-02 16:36:24 +00:00
|
|
|
_background.fill(
|
|
|
|
*_f,
|
|
|
|
fill,
|
|
|
|
_viewport,
|
|
|
|
_factor,
|
|
|
|
bg);
|
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;
|
|
|
|
}
|
|
|
|
if (data.format == Streaming::FrameFormat::ARGB32) {
|
|
|
|
Assert(!data.original.isNull());
|
2021-06-04 15:42:02 +00:00
|
|
|
paintTransformedStaticContent(data.original, geometry, false);
|
2021-06-03 12:57:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Assert(data.format == Streaming::FrameFormat::YUV420);
|
|
|
|
Assert(!data.yuv420->size.isEmpty());
|
|
|
|
const auto yuv = data.yuv420;
|
2021-06-08 09:25:07 +00:00
|
|
|
_yuv420Program->bind();
|
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(
|
|
|
|
GL_RED,
|
|
|
|
GL_RED,
|
|
|
|
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(
|
|
|
|
GL_RED,
|
|
|
|
GL_RED,
|
|
|
|
yuv->chromaSize,
|
|
|
|
_chromaSize,
|
|
|
|
yuv->u.stride,
|
|
|
|
yuv->u.data);
|
|
|
|
}
|
|
|
|
_f->glActiveTexture(GL_TEXTURE2);
|
2021-06-04 09:50:41 +00:00
|
|
|
_textures.bind(*_f, 3);
|
2021-06-03 12:57:48 +00:00
|
|
|
if (upload) {
|
|
|
|
uploadTexture(
|
|
|
|
GL_RED,
|
|
|
|
GL_RED,
|
|
|
|
yuv->chromaSize,
|
|
|
|
_chromaSize,
|
|
|
|
yuv->v.stride,
|
|
|
|
yuv->v.data);
|
|
|
|
_chromaSize = yuv->chromaSize;
|
|
|
|
_f->glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
|
|
|
_yuv420Program->setUniformValue("y_texture", GLint(0));
|
|
|
|
_yuv420Program->setUniformValue("u_texture", GLint(1));
|
|
|
|
_yuv420Program->setUniformValue("v_texture", GLint(2));
|
|
|
|
|
2021-06-04 15:42:02 +00:00
|
|
|
paintTransformedContent(&*_yuv420Program, 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-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(
|
|
|
|
GL_RGBA,
|
|
|
|
GL_RGBA,
|
|
|
|
QSize(2, 2),
|
|
|
|
_rgbaSize,
|
|
|
|
stride,
|
|
|
|
data);
|
|
|
|
} else {
|
|
|
|
const auto stride = image.bytesPerLine() / 4;
|
|
|
|
const auto data = image.constBits();
|
|
|
|
uploadTexture(
|
|
|
|
GL_RGBA,
|
|
|
|
GL_RGBA,
|
|
|
|
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-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;
|
|
|
|
const auto rsin = std::sinf(geometry.rotation * M_PI / 180.);
|
|
|
|
const auto rcos = std::cosf(geometry.rotation * M_PI / 180.);
|
|
|
|
const auto rotated = [&](float x, float y) -> std::array<float, 2> {
|
|
|
|
x -= centerx;
|
|
|
|
y -= centery;
|
|
|
|
return {
|
|
|
|
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
|
|
|
|
|
|
|
toggleBlending(false);
|
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;
|
|
|
|
const auto bgAlpha = int(std::round(bg.alpha() * outerOpacity));
|
|
|
|
const auto offset = kControlsOffset + (meta.index * kControlValues) / 4;
|
|
|
|
const auto fgOffset = offset + 2;
|
|
|
|
const auto bgRect = TransformRect(outer, _viewport, _factor);
|
|
|
|
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;
|
|
|
|
for (const auto meta : metas) {
|
|
|
|
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;
|
|
|
|
for (const auto meta : metas) {
|
|
|
|
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));
|
|
|
|
|
|
|
|
image.setImage(std::move(raster));
|
|
|
|
image.bind(*_f, size);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
_f->glActiveTexture(GL_TEXTURE0);
|
|
|
|
image.bind(*_f, size);
|
|
|
|
|
|
|
|
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
|