1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4 
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #pragma once
9 
10 #include "media/view/media_view_pip_renderer.h"
11 #include "ui/gl/gl_image.h"
12 #include "ui/gl/gl_primitives.h"
13 
14 #include <QOpenGLBuffer>
15 
16 namespace Media::View {
17 
18 class Pip::RendererGL final : public Pip::Renderer {
19 public:
20 	explicit RendererGL(not_null<Pip*> owner);
21 
22 	void init(
23 		not_null<QOpenGLWidget*> widget,
24 		QOpenGLFunctions &f) override;
25 
26 	void deinit(
27 		not_null<QOpenGLWidget*> widget,
28 		QOpenGLFunctions *f) override;
29 
30 	void paint(
31 		not_null<QOpenGLWidget*> widget,
32 		QOpenGLFunctions &f) override;
33 
34 	std::optional<QColor> clearColor() override;
35 
36 private:
37 	struct Control {
38 		int index = -1;
39 		not_null<const style::icon*> icon;
40 		not_null<const style::icon*> iconOver;
41 	};
42 	void createShadowTexture();
43 
44 	void paintTransformedVideoFrame(ContentGeometry geometry) override;
45 	void paintTransformedStaticContent(
46 		const QImage &image,
47 		ContentGeometry geometry) override;
48 	void paintTransformedContent(
49 		not_null<QOpenGLShaderProgram*> program,
50 		ContentGeometry geometry);
51 	void paintRadialLoading(
52 		QRect inner,
53 		float64 controlsShown) override;
54 	void paintButtonsStart() override;
55 	void paintButton(
56 		const Button &button,
57 		int outerWidth,
58 		float64 shown,
59 		float64 over,
60 		const style::icon &icon,
61 		const style::icon &iconOver) override;
62 	void paintPlayback(QRect outer, float64 shown) override;
63 	void paintVolumeController(QRect outer, float64 shown) override;
64 
65 	void paintUsingRaster(
66 		Ui::GL::Image &image,
67 		QRect rect,
68 		Fn<void(Painter&&)> method,
69 		int bufferOffset,
70 		bool transparent = false);
71 
72 	void validateControls();
73 	void invalidateControls();
74 	void toggleBlending(bool enabled);
75 
76 	[[nodiscard]] QRect RoundingRect(ContentGeometry geometry);
77 
78 	[[nodiscard]] Ui::GL::Rect transformRect(const QRect &raster) const;
79 	[[nodiscard]] Ui::GL::Rect transformRect(const QRectF &raster) const;
80 	[[nodiscard]] Ui::GL::Rect transformRect(
81 		const Ui::GL::Rect &raster) const;
82 
83 	void uploadTexture(
84 		GLint internalformat,
85 		GLint format,
86 		QSize size,
87 		QSize hasSize,
88 		int stride,
89 		const void *data) const;
90 
91 	const not_null<Pip*> _owner;
92 
93 	QOpenGLFunctions *_f = nullptr;
94 	QSize _viewport;
95 	float _factor = 1.;
96 	QVector2D _uniformViewport;
97 
98 	std::optional<QOpenGLBuffer> _contentBuffer;
99 	std::optional<QOpenGLShaderProgram> _imageProgram;
100 	std::optional<QOpenGLShaderProgram> _controlsProgram;
101 	QOpenGLShader *_texturedVertexShader = nullptr;
102 	std::optional<QOpenGLShaderProgram> _argb32Program;
103 	std::optional<QOpenGLShaderProgram> _yuv420Program;
104 	Ui::GL::Textures<4> _textures;
105 	QSize _rgbaSize;
106 	QSize _lumaSize;
107 	QSize _chromaSize;
108 	quint64 _cacheKey = 0;
109 	int _trackFrameIndex = 0;
110 
111 	Ui::GL::Image _radialImage;
112 	Ui::GL::Image _controlsImage;
113 	Ui::GL::Image _playbackImage;
114 	Ui::GL::Image _volumeControllerImage;
115 	Ui::GL::Image _shadowImage;
116 
117 	static constexpr auto kControlsCount = 7;
118 	[[nodiscard]] static Control ControlMeta(
119 		OverState control,
120 		int index = 0);
121 	std::array<QRect, kControlsCount * 2> _controlsTextures;
122 
123 	bool _blendingEnabled = false;
124 
125 	rpl::lifetime _lifetime;
126 
127 };
128 
129 } // namespace Media::View
130