#include "config.h"
#if ENABLE(VIDEO)
#include "RenderVideo.h"
#include "Document.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "HTMLVideoElement.h"
#include "MediaPlayer.h"
#include "Page.h"
#include "PaintInfo.h"
#include "RenderView.h"
#include <wtf/StackStats.h>
#if ENABLE(FULLSCREEN_API)
#include "RenderFullScreen.h"
#endif
namespace WebCore {
using namespace HTMLNames;
RenderVideo::RenderVideo(HTMLVideoElement& element, RenderStyle&& style)
: RenderMedia(element, WTFMove(style))
{
setIntrinsicSize(calculateIntrinsicSize());
}
RenderVideo::~RenderVideo()
{
}
void RenderVideo::willBeDestroyed()
{
if (MediaPlayer* player = videoElement().player())
player->setVisible(false);
RenderMedia::willBeDestroyed();
}
void RenderVideo::visibleInViewportStateChanged()
{
videoElement().isVisibleInViewportChanged();
}
IntSize RenderVideo::defaultSize()
{
static const int cDefaultWidth = 300;
static const int cDefaultHeight = 150;
return IntSize(cDefaultWidth, cDefaultHeight);
}
void RenderVideo::intrinsicSizeChanged()
{
if (videoElement().shouldDisplayPosterImage())
RenderMedia::intrinsicSizeChanged();
updateIntrinsicSize();
}
bool RenderVideo::updateIntrinsicSize()
{
LayoutSize size = calculateIntrinsicSize();
size.scale(style().effectiveZoom());
if (size.isEmpty() && document().isMediaDocument())
return false;
if (size == intrinsicSize())
return false;
setIntrinsicSize(size);
setPreferredLogicalWidthsDirty(true);
setNeedsLayout();
return true;
}
LayoutSize RenderVideo::calculateIntrinsicSize()
{
MediaPlayer* player = videoElement().player();
if (player && videoElement().readyState() >= HTMLVideoElement::HAVE_METADATA) {
LayoutSize size(player->naturalSize());
if (!size.isEmpty())
return size;
}
if (videoElement().shouldDisplayPosterImage() && !m_cachedImageSize.isEmpty() && !imageResource().errorOccurred())
return m_cachedImageSize;
if (videoElement().document().isMediaDocument())
return LayoutSize(defaultSize().width(), 1);
return defaultSize();
}
void RenderVideo::imageChanged(WrappedImagePtr newImage, const IntRect* rect)
{
RenderMedia::imageChanged(newImage, rect);
if (videoElement().shouldDisplayPosterImage())
m_cachedImageSize = intrinsicSize();
updateIntrinsicSize();
}
IntRect RenderVideo::videoBox() const
{
LayoutSize intrinsicSize = this->intrinsicSize();
if (videoElement().shouldDisplayPosterImage())
intrinsicSize = m_cachedImageSize;
return snappedIntRect(replacedContentRect(intrinsicSize));
}
bool RenderVideo::shouldDisplayVideo() const
{
return !videoElement().shouldDisplayPosterImage();
}
void RenderVideo::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
MediaPlayer* mediaPlayer = videoElement().player();
bool displayingPoster = videoElement().shouldDisplayPosterImage();
if (!displayingPoster && !mediaPlayer) {
if (paintInfo.phase == PaintPhaseForeground)
page().addRelevantUnpaintedObject(this, visualOverflowRect());
return;
}
LayoutRect rect = videoBox();
if (rect.isEmpty()) {
if (paintInfo.phase == PaintPhaseForeground)
page().addRelevantUnpaintedObject(this, visualOverflowRect());
return;
}
rect.moveBy(paintOffset);
if (paintInfo.phase == PaintPhaseForeground)
page().addRelevantRepaintedObject(this, rect);
LayoutRect contentRect = contentBoxRect();
contentRect.moveBy(paintOffset);
GraphicsContext& context = paintInfo.context();
bool clip = !contentRect.contains(rect);
GraphicsContextStateSaver stateSaver(context, clip);
if (clip)
context.clip(contentRect);
if (displayingPoster)
paintIntoRect(paintInfo, rect);
else if (!videoElement().isFullscreen() || !mediaPlayer->supportsAcceleratedRendering()) {
if (paintInfo.paintBehavior & PaintBehaviorFlattenCompositingLayers)
mediaPlayer->paintCurrentFrameInContext(context, rect);
else
mediaPlayer->paint(context, rect);
}
}
void RenderVideo::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
updateIntrinsicSize();
RenderMedia::layout();
updatePlayer();
}
HTMLVideoElement& RenderVideo::videoElement() const
{
return downcast<HTMLVideoElement>(RenderMedia::mediaElement());
}
void RenderVideo::updateFromElement()
{
RenderMedia::updateFromElement();
updatePlayer();
}
void RenderVideo::updatePlayer()
{
if (renderTreeBeingDestroyed())
return;
bool intrinsicSizeChanged;
intrinsicSizeChanged = updateIntrinsicSize();
ASSERT_UNUSED(intrinsicSizeChanged, !intrinsicSizeChanged || !view().frameView().isInRenderTreeLayout());
MediaPlayer* mediaPlayer = videoElement().player();
if (!mediaPlayer)
return;
if (!videoElement().inActiveDocument()) {
mediaPlayer->setVisible(false);
return;
}
contentChanged(VideoChanged);
IntRect videoBounds = videoBox();
mediaPlayer->setSize(IntSize(videoBounds.width(), videoBounds.height()));
mediaPlayer->setVisible(!videoElement().elementIsHidden());
mediaPlayer->setShouldMaintainAspectRatio(style().objectFit() != ObjectFitFill);
}
LayoutUnit RenderVideo::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
{
return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
}
LayoutUnit RenderVideo::minimumReplacedHeight() const
{
return RenderReplaced::minimumReplacedHeight();
}
bool RenderVideo::supportsAcceleratedRendering() const
{
if (MediaPlayer* player = videoElement().player())
return player->supportsAcceleratedRendering();
return false;
}
void RenderVideo::acceleratedRenderingStateChanged()
{
if (MediaPlayer* player = videoElement().player())
player->acceleratedRenderingStateChanged();
}
bool RenderVideo::requiresImmediateCompositing() const
{
MediaPlayer* player = videoElement().player();
return player && player->requiresImmediateCompositing();
}
#if ENABLE(FULLSCREEN_API)
static const RenderBlock* placeholder(const RenderVideo& renderer)
{
auto* parent = renderer.parent();
return is<RenderFullScreen>(parent) ? downcast<RenderFullScreen>(*parent).placeholder() : nullptr;
}
LayoutUnit RenderVideo::offsetLeft() const
{
if (auto* block = placeholder(*this))
return block->offsetLeft();
return RenderMedia::offsetLeft();
}
LayoutUnit RenderVideo::offsetTop() const
{
if (auto* block = placeholder(*this))
return block->offsetTop();
return RenderMedia::offsetTop();
}
LayoutUnit RenderVideo::offsetWidth() const
{
if (auto* block = placeholder(*this))
return block->offsetWidth();
return RenderMedia::offsetWidth();
}
LayoutUnit RenderVideo::offsetHeight() const
{
if (auto* block = placeholder(*this))
return block->offsetHeight();
return RenderMedia::offsetHeight();
}
#endif
bool RenderVideo::foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const
{
if (videoElement().shouldDisplayPosterImage())
return RenderImage::foregroundIsKnownToBeOpaqueInRect(localRect, maxDepthToTest);
if (!videoBox().contains(enclosingIntRect(localRect)))
return false;
if (MediaPlayer* player = videoElement().player())
return player->hasAvailableVideoFrame();
return false;
}
}
#endif