calls_video_incoming.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "calls/calls_video_incoming.h"
  8. #include "ui/gl/gl_surface.h"
  9. #include "ui/gl/gl_shader.h"
  10. #include "ui/gl/gl_image.h"
  11. #include "ui/gl/gl_primitives.h"
  12. #include "ui/painter.h"
  13. #include "media/view/media_view_pip.h"
  14. #include "webrtc/webrtc_video_track.h"
  15. #include "styles/style_calls.h"
  16. #include <QOpenGLShader>
  17. #include <QOpenGLBuffer>
  18. namespace Calls {
  19. namespace {
  20. constexpr auto kBottomShadowAlphaMax = 74;
  21. using namespace Ui::GL;
  22. [[nodiscard]] ShaderPart FragmentBottomShadow() {
  23. return {
  24. .header = R"(
  25. uniform vec3 shadow; // fullHeight, shadowTop, maxOpacity
  26. )",
  27. .body = R"(
  28. float shadowCoord = shadow.y - gl_FragCoord.y;
  29. float shadowValue = clamp(shadowCoord / shadow.x, 0., 1.);
  30. float shadowShown = shadowValue * shadow.z;
  31. result = vec4(min(result.rgb, vec3(1.)) * (1. - shadowShown), result.a);
  32. )",
  33. };
  34. }
  35. } // namespace
  36. class Panel::Incoming::RendererGL final : public Ui::GL::Renderer {
  37. public:
  38. explicit RendererGL(not_null<Incoming*> owner);
  39. void init(
  40. not_null<QOpenGLWidget*> widget,
  41. QOpenGLFunctions &f) override;
  42. void deinit(
  43. not_null<QOpenGLWidget*> widget,
  44. QOpenGLFunctions *f) override;
  45. void paint(
  46. not_null<QOpenGLWidget*> widget,
  47. QOpenGLFunctions &f) override;
  48. private:
  49. void uploadTexture(
  50. QOpenGLFunctions &f,
  51. GLint internalformat,
  52. GLint format,
  53. QSize size,
  54. QSize hasSize,
  55. int stride,
  56. const void *data) const;
  57. void validateShadowImage();
  58. const not_null<Incoming*> _owner;
  59. QSize _viewport;
  60. float _factor = 1.;
  61. int _ifactor = 1;
  62. QVector2D _uniformViewport;
  63. std::optional<QOpenGLBuffer> _contentBuffer;
  64. std::optional<QOpenGLShaderProgram> _argb32Program;
  65. QOpenGLShader *_texturedVertexShader = nullptr;
  66. std::optional<QOpenGLShaderProgram> _yuv420Program;
  67. std::optional<QOpenGLShaderProgram> _imageProgram;
  68. Ui::GL::Textures<4> _textures;
  69. QSize _rgbaSize;
  70. QSize _lumaSize;
  71. QSize _chromaSize;
  72. int _trackFrameIndex = 0;
  73. Ui::GL::Image _controlsShadowImage;
  74. QRect _controlsShadowLeft;
  75. QRect _controlsShadowRight;
  76. rpl::lifetime _lifetime;
  77. };
  78. class Panel::Incoming::RendererSW final : public Ui::GL::Renderer {
  79. public:
  80. explicit RendererSW(not_null<Incoming*> owner);
  81. void paintFallback(
  82. Painter &&p,
  83. const QRegion &clip,
  84. Ui::GL::Backend backend) override;
  85. private:
  86. void initBottomShadow();
  87. void fillTopShadow(QPainter &p);
  88. void fillBottomShadow(QPainter &p);
  89. const not_null<Incoming*> _owner;
  90. QImage _bottomShadow;
  91. };
  92. Panel::Incoming::RendererGL::RendererGL(not_null<Incoming*> owner)
  93. : _owner(owner) {
  94. style::PaletteChanged(
  95. ) | rpl::start_with_next([=] {
  96. _controlsShadowImage.invalidate();
  97. }, _lifetime);
  98. }
  99. void Panel::Incoming::RendererGL::init(
  100. not_null<QOpenGLWidget*> widget,
  101. QOpenGLFunctions &f) {
  102. constexpr auto kQuads = 2;
  103. constexpr auto kQuadVertices = kQuads * 4;
  104. constexpr auto kQuadValues = kQuadVertices * 4;
  105. _contentBuffer.emplace();
  106. _contentBuffer->setUsagePattern(QOpenGLBuffer::DynamicDraw);
  107. _contentBuffer->create();
  108. _contentBuffer->bind();
  109. _contentBuffer->allocate(kQuadValues * sizeof(GLfloat));
  110. _textures.ensureCreated(f);
  111. _imageProgram.emplace();
  112. _texturedVertexShader = LinkProgram(
  113. &*_imageProgram,
  114. VertexShader({
  115. VertexViewportTransform(),
  116. VertexPassTextureCoord(),
  117. }),
  118. FragmentShader({
  119. FragmentSampleARGB32Texture(),
  120. })).vertex;
  121. _argb32Program.emplace();
  122. LinkProgram(
  123. &*_argb32Program,
  124. _texturedVertexShader,
  125. FragmentShader({
  126. FragmentSampleARGB32Texture(),
  127. FragmentBottomShadow(),
  128. }));
  129. _yuv420Program.emplace();
  130. LinkProgram(
  131. &*_yuv420Program,
  132. _texturedVertexShader,
  133. FragmentShader({
  134. FragmentSampleYUV420Texture(),
  135. FragmentBottomShadow(),
  136. }));
  137. }
  138. void Panel::Incoming::RendererGL::deinit(
  139. not_null<QOpenGLWidget*> widget,
  140. QOpenGLFunctions *f) {
  141. _textures.destroy(f);
  142. _imageProgram = std::nullopt;
  143. _texturedVertexShader = nullptr;
  144. _argb32Program = std::nullopt;
  145. _yuv420Program = std::nullopt;
  146. _contentBuffer = std::nullopt;
  147. }
  148. void Panel::Incoming::RendererGL::paint(
  149. not_null<QOpenGLWidget*> widget,
  150. QOpenGLFunctions &f) {
  151. const auto markGuard = gsl::finally([&] {
  152. _owner->_track->markFrameShown();
  153. });
  154. const auto data = _owner->_track->frameWithInfo(false);
  155. if (data.format == Webrtc::FrameFormat::None) {
  156. return;
  157. }
  158. const auto factor = widget->devicePixelRatioF();
  159. if (_factor != factor) {
  160. _factor = factor;
  161. _ifactor = int(std::ceil(_factor));
  162. _controlsShadowImage.invalidate();
  163. }
  164. _viewport = widget->size();
  165. _uniformViewport = QVector2D(
  166. _viewport.width() * _factor,
  167. _viewport.height() * _factor);
  168. const auto rgbaFrame = (data.format == Webrtc::FrameFormat::ARGB32);
  169. const auto upload = (_trackFrameIndex != data.index);
  170. _trackFrameIndex = data.index;
  171. auto &program = rgbaFrame ? _argb32Program : _yuv420Program;
  172. program->bind();
  173. if (rgbaFrame) {
  174. Assert(!data.original.isNull());
  175. f.glActiveTexture(GL_TEXTURE0);
  176. _textures.bind(f, 0);
  177. if (upload) {
  178. uploadTexture(
  179. f,
  180. Ui::GL::kFormatRGBA,
  181. Ui::GL::kFormatRGBA,
  182. data.original.size(),
  183. _rgbaSize,
  184. data.original.bytesPerLine() / 4,
  185. data.original.constBits());
  186. _rgbaSize = data.original.size();
  187. }
  188. program->setUniformValue("s_texture", GLint(0));
  189. } else {
  190. Assert(data.format == Webrtc::FrameFormat::YUV420);
  191. Assert(!data.yuv420->size.isEmpty());
  192. const auto yuv = data.yuv420;
  193. f.glActiveTexture(GL_TEXTURE0);
  194. _textures.bind(f, 1);
  195. if (upload) {
  196. f.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  197. uploadTexture(
  198. f,
  199. GL_ALPHA,
  200. GL_ALPHA,
  201. yuv->size,
  202. _lumaSize,
  203. yuv->y.stride,
  204. yuv->y.data);
  205. _lumaSize = yuv->size;
  206. }
  207. f.glActiveTexture(GL_TEXTURE1);
  208. _textures.bind(f, 2);
  209. if (upload) {
  210. uploadTexture(
  211. f,
  212. GL_ALPHA,
  213. GL_ALPHA,
  214. yuv->chromaSize,
  215. _chromaSize,
  216. yuv->u.stride,
  217. yuv->u.data);
  218. }
  219. f.glActiveTexture(GL_TEXTURE2);
  220. _textures.bind(f, 3);
  221. if (upload) {
  222. uploadTexture(
  223. f,
  224. GL_ALPHA,
  225. GL_ALPHA,
  226. yuv->chromaSize,
  227. _chromaSize,
  228. yuv->v.stride,
  229. yuv->v.data);
  230. _chromaSize = yuv->chromaSize;
  231. f.glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  232. }
  233. program->setUniformValue("y_texture", GLint(0));
  234. program->setUniformValue("u_texture", GLint(1));
  235. program->setUniformValue("v_texture", GLint(2));
  236. }
  237. const auto rect = TransformRect(
  238. widget->rect(),
  239. _viewport,
  240. _factor);
  241. std::array<std::array<GLfloat, 2>, 4> texcoords = { {
  242. { { 0.f, 1.f } },
  243. { { 1.f, 1.f } },
  244. { { 1.f, 0.f } },
  245. { { 0.f, 0.f } },
  246. } };
  247. if (const auto shift = (data.rotation / 90); shift != 0) {
  248. std::rotate(
  249. begin(texcoords),
  250. begin(texcoords) + shift,
  251. end(texcoords));
  252. }
  253. const auto width = widget->parentWidget()->width();
  254. const auto left = (_owner->_topControlsAlignment == style::al_left);
  255. validateShadowImage();
  256. const auto position = left
  257. ? QPoint()
  258. : QPoint(width - st::callTitleShadowRight.width(), 0);
  259. const auto translated = position - widget->pos();
  260. const auto shadowArea = QRect(translated, st::callTitleShadowLeft.size());
  261. const auto shadow = _controlsShadowImage.texturedRect(
  262. shadowArea,
  263. (left ? _controlsShadowLeft : _controlsShadowRight),
  264. widget->rect());
  265. const auto shadowRect = TransformRect(
  266. shadow.geometry,
  267. _viewport,
  268. _factor);
  269. const GLfloat coords[] = {
  270. rect.left(), rect.top(),
  271. texcoords[0][0], texcoords[0][1],
  272. rect.right(), rect.top(),
  273. texcoords[1][0], texcoords[1][1],
  274. rect.right(), rect.bottom(),
  275. texcoords[2][0], texcoords[2][1],
  276. rect.left(), rect.bottom(),
  277. texcoords[3][0], texcoords[3][1],
  278. shadowRect.left(), shadowRect.top(),
  279. shadow.texture.left(), shadow.texture.bottom(),
  280. shadowRect.right(), shadowRect.top(),
  281. shadow.texture.right(), shadow.texture.bottom(),
  282. shadowRect.right(), shadowRect.bottom(),
  283. shadow.texture.right(), shadow.texture.top(),
  284. shadowRect.left(), shadowRect.bottom(),
  285. shadow.texture.left(), shadow.texture.top(),
  286. };
  287. _contentBuffer->bind();
  288. _contentBuffer->write(0, coords, sizeof(coords));
  289. const auto bottomShadowArea = QRect(
  290. 0,
  291. widget->parentWidget()->height() - st::callBottomShadowSize,
  292. widget->parentWidget()->width(),
  293. st::callBottomShadowSize);
  294. const auto bottomShadowFill = bottomShadowArea.intersected(
  295. widget->geometry()).translated(-widget->pos());
  296. const auto shadowHeight = bottomShadowFill.height();
  297. const auto shadowAlpha = (shadowHeight * kBottomShadowAlphaMax)
  298. / (st::callBottomShadowSize * 255.);
  299. program->setUniformValue("viewport", _uniformViewport);
  300. program->setUniformValue("shadow", QVector3D(
  301. shadowHeight * _factor,
  302. TransformRect(bottomShadowFill, _viewport, _factor).bottom(),
  303. shadowAlpha));
  304. FillTexturedRectangle(f, &*program);
  305. #ifndef Q_OS_MAC
  306. if (!shadowRect.empty()) {
  307. f.glEnable(GL_BLEND);
  308. f.glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  309. const auto guard = gsl::finally([&] {
  310. f.glDisable(GL_BLEND);
  311. });
  312. _imageProgram->bind();
  313. _imageProgram->setUniformValue("viewport", _uniformViewport);
  314. _imageProgram->setUniformValue("s_texture", GLint(0));
  315. f.glActiveTexture(GL_TEXTURE0);
  316. _controlsShadowImage.bind(f);
  317. FillTexturedRectangle(f, &*_imageProgram, 4);
  318. }
  319. #endif // Q_OS_MAC
  320. }
  321. void Panel::Incoming::RendererGL::validateShadowImage() {
  322. if (_controlsShadowImage) {
  323. return;
  324. }
  325. const auto size = st::callTitleShadowLeft.size();
  326. const auto full = QSize(size.width(), 2 * size.height()) * _ifactor;
  327. auto image = QImage(full, QImage::Format_ARGB32_Premultiplied);
  328. image.setDevicePixelRatio(_ifactor);
  329. image.fill(Qt::transparent);
  330. {
  331. auto p = QPainter(&image);
  332. st::callTitleShadowLeft.paint(p, 0, 0, size.width());
  333. _controlsShadowLeft = QRect(0, 0, full.width(), full.height() / 2);
  334. st::callTitleShadowRight.paint(p, 0, size.height(), size.width());
  335. _controlsShadowRight = QRect(
  336. 0,
  337. full.height() / 2,
  338. full.width(),
  339. full.height() / 2);
  340. }
  341. _controlsShadowImage.setImage(std::move(image));
  342. }
  343. void Panel::Incoming::RendererGL::uploadTexture(
  344. QOpenGLFunctions &f,
  345. GLint internalformat,
  346. GLint format,
  347. QSize size,
  348. QSize hasSize,
  349. int stride,
  350. const void *data) const {
  351. f.glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
  352. if (hasSize != size) {
  353. f.glTexImage2D(
  354. GL_TEXTURE_2D,
  355. 0,
  356. internalformat,
  357. size.width(),
  358. size.height(),
  359. 0,
  360. format,
  361. GL_UNSIGNED_BYTE,
  362. data);
  363. } else {
  364. f.glTexSubImage2D(
  365. GL_TEXTURE_2D,
  366. 0,
  367. 0,
  368. 0,
  369. size.width(),
  370. size.height(),
  371. format,
  372. GL_UNSIGNED_BYTE,
  373. data);
  374. }
  375. f.glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  376. }
  377. Panel::Incoming::RendererSW::RendererSW(not_null<Incoming*> owner)
  378. : _owner(owner) {
  379. initBottomShadow();
  380. }
  381. void Panel::Incoming::RendererSW::paintFallback(
  382. Painter &&p,
  383. const QRegion &clip,
  384. Ui::GL::Backend backend) {
  385. const auto markGuard = gsl::finally([&] {
  386. _owner->_track->markFrameShown();
  387. });
  388. const auto data = _owner->_track->frameWithInfo(true);
  389. const auto &image = data.original;
  390. const auto rotation = data.rotation;
  391. if (image.isNull()) {
  392. p.fillRect(clip.boundingRect(), Qt::black);
  393. } else {
  394. const auto rect = _owner->widget()->rect();
  395. using namespace Media::View;
  396. auto hq = PainterHighQualityEnabler(p);
  397. if (UsePainterRotation(rotation)) {
  398. if (rotation) {
  399. p.save();
  400. p.rotate(rotation);
  401. }
  402. p.drawImage(RotatedRect(rect, rotation), image);
  403. if (rotation) {
  404. p.restore();
  405. }
  406. } else if (rotation) {
  407. p.drawImage(rect, RotateFrameImage(image, rotation));
  408. } else {
  409. p.drawImage(rect, image);
  410. }
  411. fillBottomShadow(p);
  412. fillTopShadow(p);
  413. }
  414. }
  415. void Panel::Incoming::RendererSW::initBottomShadow() {
  416. auto image = QImage(
  417. QSize(1, st::callBottomShadowSize) * style::DevicePixelRatio(),
  418. QImage::Format_ARGB32_Premultiplied);
  419. const auto colorFrom = uint32(0);
  420. const auto colorTill = uint32(kBottomShadowAlphaMax);
  421. const auto rows = image.height();
  422. const auto step = (uint64(colorTill - colorFrom) << 32) / rows;
  423. auto accumulated = uint64();
  424. auto bytes = image.bits();
  425. for (auto y = 0; y != rows; ++y) {
  426. accumulated += step;
  427. const auto color = (colorFrom + uint32(accumulated >> 32)) << 24;
  428. for (auto x = 0; x != image.width(); ++x) {
  429. *(reinterpret_cast<uint32*>(bytes) + x) = color;
  430. }
  431. bytes += image.bytesPerLine();
  432. }
  433. _bottomShadow = std::move(image);
  434. }
  435. void Panel::Incoming::RendererSW::fillTopShadow(QPainter &p) {
  436. #ifndef Q_OS_MAC
  437. const auto widget = _owner->widget();
  438. const auto width = widget->parentWidget()->width();
  439. const auto left = (_owner->_topControlsAlignment == style::al_left);
  440. const auto &icon = left
  441. ? st::callTitleShadowLeft
  442. : st::callTitleShadowRight;
  443. const auto position = left
  444. ? QPoint()
  445. : QPoint(width - icon.width(), 0);
  446. const auto shadowArea = QRect(position, icon.size());
  447. const auto fill = shadowArea.intersected(
  448. widget->geometry()).translated(-widget->pos());
  449. if (fill.isEmpty()) {
  450. return;
  451. }
  452. p.save();
  453. p.setClipRect(fill);
  454. icon.paint(p, position - widget->pos(), width);
  455. p.restore();
  456. #endif // Q_OS_MAC
  457. }
  458. void Panel::Incoming::RendererSW::fillBottomShadow(QPainter &p) {
  459. const auto widget = _owner->widget();
  460. const auto shadowArea = QRect(
  461. 0,
  462. widget->parentWidget()->height() - st::callBottomShadowSize,
  463. widget->parentWidget()->width(),
  464. st::callBottomShadowSize);
  465. const auto fill = shadowArea.intersected(
  466. widget->geometry()).translated(-widget->pos());
  467. if (fill.isEmpty()) {
  468. return;
  469. }
  470. const auto factor = style::DevicePixelRatio();
  471. p.drawImage(
  472. fill,
  473. _bottomShadow,
  474. QRect(
  475. 0,
  476. (factor
  477. * (fill.y() - shadowArea.translated(-widget->pos()).y())),
  478. factor,
  479. factor * fill.height()));
  480. }
  481. Panel::Incoming::Incoming(
  482. not_null<QWidget*> parent,
  483. not_null<Webrtc::VideoTrack*> track,
  484. Ui::GL::Backend backend)
  485. : _surface(Ui::GL::CreateSurface(parent, chooseRenderer(backend)))
  486. , _track(track) {
  487. widget()->setAttribute(Qt::WA_OpaquePaintEvent);
  488. widget()->setAttribute(Qt::WA_TransparentForMouseEvents);
  489. }
  490. not_null<QWidget*> Panel::Incoming::widget() const {
  491. return _surface->rpWidget();
  492. }
  493. not_null<Ui::RpWidgetWrap*> Panel::Incoming::rp() const {
  494. return _surface.get();
  495. }
  496. void Panel::Incoming::setControlsAlignment(style::align align) {
  497. if (_topControlsAlignment != align) {
  498. _topControlsAlignment = align;
  499. widget()->update();
  500. }
  501. }
  502. Ui::GL::ChosenRenderer Panel::Incoming::chooseRenderer(
  503. Ui::GL::Backend backend) {
  504. _opengl = (backend == Ui::GL::Backend::OpenGL);
  505. return {
  506. .renderer = (_opengl
  507. ? std::unique_ptr<Ui::GL::Renderer>(
  508. std::make_unique<RendererGL>(this))
  509. : std::make_unique<RendererSW>(this)),
  510. .backend = backend,
  511. };
  512. }
  513. } // namespace Calls