From e1cf508eca6f941aba20e0dc2e3dd982a838eacf Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 24 May 2021 16:01:21 +0300 Subject: [PATCH] Revert "[Perf] Use canvas for faster blur (#1100)" This reverts commit 27cc33b5541e6f9fe27cf74220e8629a5966d339. --- src/components/common/GifButton.scss | 8 +-- src/components/common/GifButton.tsx | 29 +++++----- .../left/settings/WallpaperTile.scss | 2 +- .../left/settings/WallpaperTile.tsx | 13 +++-- src/components/middle/message/Photo.tsx | 20 ++++--- src/components/middle/message/RoundVideo.tsx | 14 ++--- src/components/middle/message/Video.tsx | 30 ++++------- .../message/hooks/useBlurredMediaThumb.ts | 14 +++++ .../message/hooks/useBlurredMediaThumbRef.ts | 13 ----- src/hooks/useCanvasBlur.ts | 53 ------------------- src/styles/_common.scss | 6 +++ src/util/environment.ts | 1 - 12 files changed, 74 insertions(+), 129 deletions(-) create mode 100644 src/components/middle/message/hooks/useBlurredMediaThumb.ts delete mode 100644 src/components/middle/message/hooks/useBlurredMediaThumbRef.ts delete mode 100644 src/hooks/useCanvasBlur.ts diff --git a/src/components/common/GifButton.scss b/src/components/common/GifButton.scss index aefe8875..d0575d49 100644 --- a/src/components/common/GifButton.scss +++ b/src/components/common/GifButton.scss @@ -20,16 +20,12 @@ grid-column-end: span 2; } - .thumbnail { + .preview { background-size: cover !important; background: transparent no-repeat center; } - .thumbnail ~ video { - position: absolute; - } - - .thumbnail, video { + .preview, video { width: 100%; height: 100%; object-fit: cover; diff --git a/src/components/common/GifButton.tsx b/src/components/common/GifButton.tsx index 29360053..a86c58b9 100644 --- a/src/components/common/GifButton.tsx +++ b/src/components/common/GifButton.tsx @@ -8,9 +8,9 @@ import buildClassName from '../../util/buildClassName'; import { ObserveFn, useIsIntersecting } from '../../hooks/useIntersectionObserver'; import useMedia from '../../hooks/useMedia'; import useTransitionForMedia from '../../hooks/useTransitionForMedia'; +import useBlur from '../../hooks/useBlur'; import useVideoCleanup from '../../hooks/useVideoCleanup'; import useBuffering from '../../hooks/useBuffering'; -import useCanvasBlur from '../../hooks/useCanvasBlur'; import Spinner from '../ui/Spinner'; @@ -31,15 +31,15 @@ const GifButton: FC = ({ // eslint-disable-next-line no-null/no-null const videoRef = useRef(null); - const hasThumbnail = gif.thumbnail && !!gif.thumbnail.dataUri; const localMediaHash = `gif${gif.id}`; const isIntersecting = useIsIntersecting(ref, observeIntersection); const loadAndPlay = isIntersecting && !isDisabled; const previewBlobUrl = useMedia(`${localMediaHash}?size=m`, !loadAndPlay, ApiMediaFormat.BlobUrl); - const thumbRef = useCanvasBlur(gif.thumbnail && gif.thumbnail.dataUri, Boolean(previewBlobUrl)); + const thumbDataUri = useBlur(gif.thumbnail && gif.thumbnail.dataUri, Boolean(previewBlobUrl)); + const previewData = previewBlobUrl || thumbDataUri; const videoData = useMedia(localMediaHash, !loadAndPlay, ApiMediaFormat.BlobUrl); const shouldRenderVideo = Boolean(loadAndPlay && videoData); - const { transitionClassNames } = useTransitionForMedia(hasThumbnail || previewBlobUrl || videoData, 'slow'); + const { transitionClassNames } = useTransitionForMedia(previewData || videoData, 'slow'); const { isBuffered, bufferingHandlers } = useBuffering(true); const shouldRenderSpinner = loadAndPlay && !isBuffered; @@ -66,20 +66,14 @@ const GifButton: FC = ({ className={className} onClick={handleClick} > - {hasThumbnail && ( - )} - {!hasThumbnail && previewBlobUrl && ( - - )} - {(shouldRenderVideo || previewBlobUrl) && ( + {shouldRenderVideo && ( )} {shouldRenderSpinner && ( - + )} ); diff --git a/src/components/left/settings/WallpaperTile.scss b/src/components/left/settings/WallpaperTile.scss index 0cd8314c..1115e407 100644 --- a/src/components/left/settings/WallpaperTile.scss +++ b/src/components/left/settings/WallpaperTile.scss @@ -18,7 +18,7 @@ transform: scale(1); transition: transform .15s ease; - img, canvas { + img { position: absolute; left: 0; top: 0; diff --git a/src/components/left/settings/WallpaperTile.tsx b/src/components/left/settings/WallpaperTile.tsx index d595cbf9..d517a98d 100644 --- a/src/components/left/settings/WallpaperTile.tsx +++ b/src/components/left/settings/WallpaperTile.tsx @@ -13,7 +13,7 @@ import useMedia from '../../../hooks/useMedia'; import useMediaWithDownloadProgress from '../../../hooks/useMediaWithDownloadProgress'; import useShowTransition from '../../../hooks/useShowTransition'; import usePrevious from '../../../hooks/usePrevious'; -import useCanvasBlur from '../../../hooks/useCanvasBlur'; +import useBlur from '../../../hooks/useBlur'; import ProgressSpinner from '../../ui/ProgressSpinner'; @@ -25,6 +25,8 @@ type OwnProps = { onClick: (slug: string) => void; }; +const ANIMATION_DURATION = 300; + const WallpaperTile: FC = ({ wallpaper, isSelected, @@ -35,10 +37,10 @@ const WallpaperTile: FC = ({ const localMediaHash = `wallpaper${document.id!}`; const localBlobUrl = document.previewBlobUrl; const previewBlobUrl = useMedia(`${localMediaHash}?size=m`); - const thumbRef = useCanvasBlur( + const thumbDataUri = useBlur( document.thumbnail && document.thumbnail.dataUri, Boolean(previewBlobUrl), - true, + ANIMATION_DURATION, ); const { shouldRenderThumb, shouldRenderFullMedia, transitionClassNames, @@ -86,9 +88,10 @@ const WallpaperTile: FC = ({
{shouldRenderThumb && ( - )} {shouldRenderFullMedia && ( diff --git a/src/components/middle/message/Photo.tsx b/src/components/middle/message/Photo.tsx index 872b5fef..e3fcfc62 100644 --- a/src/components/middle/message/Photo.tsx +++ b/src/components/middle/message/Photo.tsx @@ -16,8 +16,8 @@ import { ObserveFn, useIsIntersecting } from '../../../hooks/useIntersectionObse import useMediaWithDownloadProgress from '../../../hooks/useMediaWithDownloadProgress'; import useTransitionForMedia from '../../../hooks/useTransitionForMedia'; import useShowTransition from '../../../hooks/useShowTransition'; -import useBlurredMediaThumbRef from './hooks/useBlurredMediaThumbRef'; import usePrevious from '../../../hooks/usePrevious'; +import useBlurredMediaThumb from './hooks/useBlurredMediaThumb'; import buildClassName from '../../../util/buildClassName'; import getCustomAppendixBg from './helpers/getCustomAppendixBg'; import { calculateMediaDimensions } from './helpers/mediaDimensions'; @@ -69,7 +69,7 @@ const Photo: FC = ({ mediaData, downloadProgress, } = useMediaWithDownloadProgress(getMessageMediaHash(message, size), !shouldDownload); const fullMediaData = localBlobUrl || mediaData; - const thumbRef = useBlurredMediaThumbRef(message, fullMediaData); + const thumbDataUri = useBlurredMediaThumb(message, fullMediaData); const { isUploading, isTransferring, transferProgress, @@ -122,6 +122,11 @@ const Photo: FC = ({ width === height && 'square-image', ); + const thumbClassName = buildClassName( + 'thumbnail', + !thumbDataUri && 'empty', + ); + const style = dimensions ? `width: ${width}px; height: ${height}px; left: ${dimensions.x}px; top: ${dimensions.y}px;` : ''; @@ -136,11 +141,12 @@ const Photo: FC = ({ onClick={isUploading ? undefined : handleClick} > {shouldRenderThumb && ( - )} {shouldRenderFullMedia && ( diff --git a/src/components/middle/message/RoundVideo.tsx b/src/components/middle/message/RoundVideo.tsx index cca135d8..2a585f34 100644 --- a/src/components/middle/message/RoundVideo.tsx +++ b/src/components/middle/message/RoundVideo.tsx @@ -20,8 +20,8 @@ import useBuffering from '../../../hooks/useBuffering'; import buildClassName from '../../../util/buildClassName'; import useHeavyAnimationCheckForVideo from '../../../hooks/useHeavyAnimationCheckForVideo'; import useVideoCleanup from '../../../hooks/useVideoCleanup'; +import useBlurredMediaThumb from './hooks/useBlurredMediaThumb'; import usePauseOnInactive from './hooks/usePauseOnInactive'; -import useBlurredMediaThumbRef from './hooks/useBlurredMediaThumbRef'; import safePlay from '../../../util/safePlay'; import ProgressSpinner from '../../ui/ProgressSpinner'; @@ -74,7 +74,7 @@ const RoundVideo: FC = ({ getMessageMediaFormat(message, 'inline'), lastSyncTime, ); - const thumbRef = useBlurredMediaThumbRef(message, mediaData); + const thumbDataUri = useBlurredMediaThumb(message, mediaData); const { isBuffered, bufferingHandlers } = useBuffering(); const isTransferring = isDownloadAllowed && !isBuffered; @@ -183,11 +183,12 @@ const RoundVideo: FC = ({ > {shouldRenderThumb && (
-
)} @@ -203,6 +204,7 @@ const RoundVideo: FC = ({ muted={!isActivated} loop={!isActivated} playsInline + poster={thumbDataUri} onEnded={isActivated ? stopPlaying : undefined} // eslint-disable-next-line react/jsx-props-no-spreading {...bufferingHandlers} diff --git a/src/components/middle/message/Video.tsx b/src/components/middle/message/Video.tsx index 3237b3ab..e57d3701 100644 --- a/src/components/middle/message/Video.tsx +++ b/src/components/middle/message/Video.tsx @@ -24,9 +24,9 @@ import useTransitionForMedia from '../../../hooks/useTransitionForMedia'; import usePrevious from '../../../hooks/usePrevious'; import useBuffering from '../../../hooks/useBuffering'; import useHeavyAnimationCheckForVideo from '../../../hooks/useHeavyAnimationCheckForVideo'; +import useBlurredMediaThumb from './hooks/useBlurredMediaThumb'; import useVideoCleanup from '../../../hooks/useVideoCleanup'; import usePauseOnInactive from './hooks/usePauseOnInactive'; -import useBlurredMediaThumbRef from './hooks/useBlurredMediaThumbRef'; import ProgressSpinner from '../../ui/ProgressSpinner'; @@ -76,7 +76,7 @@ const Video: FC = ({ getMessageMediaFormat(message, 'pictogram'), lastSyncTime, ); - const thumbRef = useBlurredMediaThumbRef(message); + const thumbDataUri = useBlurredMediaThumb(message, previewBlobUrl); const { mediaData, downloadProgress } = useMediaWithDownloadProgress( getMessageMediaHash(message, 'inline'), !shouldDownload, @@ -84,6 +84,7 @@ const Video: FC = ({ lastSyncTime, ); + const previewMediaData = previewBlobUrl || thumbDataUri; const fullMediaData = localBlobUrl || mediaData; const isInline = Boolean(canPlayInline && isIntersecting && fullMediaData); @@ -131,10 +132,9 @@ const Video: FC = ({ }, [isUploading, canPlayInline, fullMediaData, isPlayAllowed, onClick, onCancelUpload, message]); const className = buildClassName('media-inner dark', !isUploading && 'interactive'); + const thumbClassName = buildClassName('thumbnail', !previewMediaData && 'empty'); const videoClassName = buildClassName('full-media', transitionClassNames); - const videoStyle = previewBlobUrl - ? `background-image: url(${previewBlobUrl}); background-size: cover` - : 'background: transparent'; + const videoStyle = previewMediaData ? `background-image: url(${previewMediaData}); background-size: cover` : ''; const style = dimensions ? `width: ${width}px; height: ${height}px; left: ${dimensions.x}px; top: ${dimensions.y}px;` @@ -154,25 +154,15 @@ const Video: FC = ({ style={style} onClick={isUploading ? undefined : handleClick} > - {((!isInline || shouldRenderThumb) && !previewBlobUrl) - && ( - - )} - {previewBlobUrl && fullMediaData && ( + {(shouldRenderThumb || !isInline) && ( )} - {shouldRenderInlineVideo && (