This source file includes following definitions.
- defaultSize
- intrinsicSizeChanged
- updateIntrinsicSize
- calculateIntrinsicSize
- imageChanged
- videoBox
- shouldDisplayVideo
- paintReplaced
- acceleratedRenderingInUse
- layout
- videoElement
- updateFromElement
- updatePlayer
- computeReplacedLogicalWidth
- computeReplacedLogicalHeight
- minimumReplacedHeight
- supportsAcceleratedRendering
- rendererPlaceholder
- offsetLeft
- offsetTop
- offsetWidth
- offsetHeight
- additionalCompositingReasons
#include "config.h"
#include "core/rendering/RenderVideo.h"
#include "HTMLNames.h"
#include "core/dom/Document.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLVideoElement.h"
#include "core/rendering/LayoutRectRecorder.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/RenderFullScreen.h"
#include "platform/graphics/media/MediaPlayer.h"
#include "public/platform/WebLayer.h"
namespace WebCore {
using namespace HTMLNames;
RenderVideo::RenderVideo(HTMLVideoElement* video)
: RenderMedia(video)
{
setIntrinsicSize(calculateIntrinsicSize());
}
RenderVideo::~RenderVideo()
{
}
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();
}
void RenderVideo::updateIntrinsicSize()
{
LayoutSize size = calculateIntrinsicSize();
size.scale(style()->effectiveZoom());
if (size.isEmpty() && node()->ownerDocument() && node()->ownerDocument()->isMediaDocument())
return;
if (size == intrinsicSize())
return;
setIntrinsicSize(size);
setPreferredLogicalWidthsDirty();
setNeedsLayout();
}
LayoutSize RenderVideo::calculateIntrinsicSize()
{
HTMLVideoElement* video = videoElement();
MediaPlayer* player = mediaElement()->player();
if (player && video->readyState() >= HTMLVideoElement::HAVE_METADATA) {
LayoutSize size = player->naturalSize();
if (!size.isEmpty())
return size;
}
if (video->shouldDisplayPosterImage() && !m_cachedImageSize.isEmpty() && !imageResource()->errorOccurred())
return m_cachedImageSize;
if (video->ownerDocument() && video->ownerDocument()->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
{
const LayoutSize* overriddenIntrinsicSize = 0;
if (videoElement()->shouldDisplayPosterImage())
overriddenIntrinsicSize = &m_cachedImageSize;
return pixelSnappedIntRect(replacedContentRect(overriddenIntrinsicSize));
}
bool RenderVideo::shouldDisplayVideo() const
{
return !videoElement()->shouldDisplayPosterImage();
}
void RenderVideo::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
MediaPlayer* mediaPlayer = mediaElement()->player();
bool displayingPoster = videoElement()->shouldDisplayPosterImage();
if (!displayingPoster && !mediaPlayer)
return;
LayoutRect rect = videoBox();
if (rect.isEmpty())
return;
rect.moveBy(paintOffset);
LayoutRect contentRect = contentBoxRect();
contentRect.moveBy(paintOffset);
GraphicsContext* context = paintInfo.context;
bool clip = !contentRect.contains(rect);
if (clip) {
context->save();
context->clip(contentRect);
}
if (displayingPoster)
paintIntoRect(context, rect);
else if ((document().view() && document().view()->paintBehavior() & PaintBehaviorFlattenCompositingLayers) || !acceleratedRenderingInUse())
mediaPlayer->paint(context, pixelSnappedIntRect(rect));
if (clip)
context->restore();
}
bool RenderVideo::acceleratedRenderingInUse()
{
blink::WebLayer* webLayer = mediaElement()->platformLayer();
return webLayer && !webLayer->isOrphan();
}
void RenderVideo::layout()
{
LayoutRectRecorder recorder(*this);
updatePlayer();
RenderMedia::layout();
}
HTMLVideoElement* RenderVideo::videoElement() const
{
return toHTMLVideoElement(node());
}
void RenderVideo::updateFromElement()
{
RenderMedia::updateFromElement();
updatePlayer();
}
void RenderVideo::updatePlayer()
{
updateIntrinsicSize();
MediaPlayer* mediaPlayer = mediaElement()->player();
if (!mediaPlayer)
return;
if (!videoElement()->isActive())
return;
contentChanged(VideoChanged);
}
LayoutUnit RenderVideo::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
{
return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
}
LayoutUnit RenderVideo::computeReplacedLogicalHeight() const
{
return RenderReplaced::computeReplacedLogicalHeight();
}
LayoutUnit RenderVideo::minimumReplacedHeight() const
{
return RenderReplaced::minimumReplacedHeight();
}
bool RenderVideo::supportsAcceleratedRendering() const
{
return !!mediaElement()->platformLayer();
}
static const RenderBlock* rendererPlaceholder(const RenderObject* renderer)
{
RenderObject* parent = renderer->parent();
if (!parent)
return 0;
RenderFullScreen* fullScreen = parent->isRenderFullScreen() ? toRenderFullScreen(parent) : 0;
if (!fullScreen)
return 0;
return fullScreen->placeholder();
}
LayoutUnit RenderVideo::offsetLeft() const
{
if (const RenderBlock* block = rendererPlaceholder(this))
return block->offsetLeft();
return RenderMedia::offsetLeft();
}
LayoutUnit RenderVideo::offsetTop() const
{
if (const RenderBlock* block = rendererPlaceholder(this))
return block->offsetTop();
return RenderMedia::offsetTop();
}
LayoutUnit RenderVideo::offsetWidth() const
{
if (const RenderBlock* block = rendererPlaceholder(this))
return block->offsetWidth();
return RenderMedia::offsetWidth();
}
LayoutUnit RenderVideo::offsetHeight() const
{
if (const RenderBlock* block = rendererPlaceholder(this))
return block->offsetHeight();
return RenderMedia::offsetHeight();
}
CompositingReasons RenderVideo::additionalCompositingReasons(CompositingTriggerFlags triggers) const
{
if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
HTMLMediaElement* media = toHTMLMediaElement(node());
if (media->isFullscreen())
return CompositingReasonVideo;
}
if ((triggers & VideoTrigger) && shouldDisplayVideo() && supportsAcceleratedRendering())
return CompositingReasonVideo;
return CompositingReasonNone;
}
}