#include "config.h"
#if ENABLE(VIDEO)
#include "RenderVideo.h"
#include "Document.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "HTMLVideoElement.h"
#include "MediaPlayer.h"
#include "RenderView.h"
#if USE(ACCELERATED_COMPOSITING)
#include "RenderLayer.h"
#include "RenderLayerBacking.h"
#endif
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderVideo::RenderVideo(HTMLVideoElement* video)
: RenderMedia(video)
{
if (video->player() && video->readyState() >= HTMLVideoElement::HAVE_METADATA)
setIntrinsicSize(video->player()->naturalSize());
else {
if (video->ownerDocument() && video->ownerDocument()->isMediaDocument())
setIntrinsicSize(IntSize(defaultSize().width(), 1));
else
setIntrinsicSize(defaultSize());
}
}
RenderVideo::~RenderVideo()
{
if (MediaPlayer* p = player()) {
p->setVisible(false);
p->setFrameView(0);
}
}
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();
videoSizeChanged();
}
void RenderVideo::videoSizeChanged()
{
if (!player())
return;
IntSize size = player()->naturalSize();
if (style())
size.scale(style()->effectiveZoom());
if (size.isEmpty()) {
if (node()->ownerDocument() && node()->ownerDocument()->isMediaDocument())
return;
}
if (size != intrinsicSize()) {
setIntrinsicSize(size);
setPrefWidthsDirty(true);
setNeedsLayout(true);
}
}
void RenderVideo::imageChanged(WrappedImagePtr newImage, const IntRect* rect)
{
RenderMedia::imageChanged(newImage, rect);
if (videoElement()->shouldDisplayPosterImage())
m_cachedImageSize = intrinsicSize();
}
IntRect RenderVideo::videoBox() const
{
if (m_cachedImageSize.isEmpty() && videoElement()->shouldDisplayPosterImage())
return IntRect();
IntSize elementSize;
if (videoElement()->shouldDisplayPosterImage())
elementSize = m_cachedImageSize;
else
elementSize = intrinsicSize();
IntRect contentRect = contentBoxRect();
if (elementSize.isEmpty() || contentRect.isEmpty())
return IntRect();
IntRect renderBox = contentRect;
int ratio = renderBox.width() * elementSize.height() - renderBox.height() * elementSize.width();
if (ratio > 0) {
int newWidth = renderBox.height() * elementSize.width() / elementSize.height();
if (renderBox.width() - newWidth > 2)
renderBox.setWidth(newWidth);
renderBox.move((contentRect.width() - renderBox.width()) / 2, 0);
} else if (ratio < 0) {
int newHeight = renderBox.width() * elementSize.height() / elementSize.width();
if (renderBox.height() - newHeight > 2)
renderBox.setHeight(newHeight);
renderBox.move(0, (contentRect.height() - renderBox.height()) / 2);
}
return renderBox;
}
void RenderVideo::paintReplaced(PaintInfo& paintInfo, int tx, int ty)
{
MediaPlayer* mediaPlayer = player();
bool displayingPoster = videoElement()->shouldDisplayPosterImage();
if (displayingPoster && document()->printing() && !view()->printImages())
return;
if (!displayingPoster) {
if (!mediaPlayer)
return;
updatePlayer();
}
IntRect rect = videoBox();
if (rect.isEmpty())
return;
rect.move(tx, ty);
if (displayingPoster)
paintIntoRect(paintInfo.context, rect);
else
mediaPlayer->paint(paintInfo.context, rect);
}
void RenderVideo::layout()
{
RenderMedia::layout();
updatePlayer();
}
HTMLVideoElement* RenderVideo::videoElement() const
{
ASSERT(node()->hasTagName(videoTag));
return static_cast<HTMLVideoElement*>(node());
}
void RenderVideo::updateFromElement()
{
RenderMedia::updateFromElement();
updatePlayer();
}
void RenderVideo::updatePlayer()
{
MediaPlayer* mediaPlayer = player();
if (!mediaPlayer)
return;
if (!videoElement()->inActiveDocument()) {
mediaPlayer->setVisible(false);
return;
}
#if USE(ACCELERATED_COMPOSITING)
layer()->rendererContentChanged();
#endif
IntRect videoBounds = videoBox();
mediaPlayer->setFrameView(document()->view());
mediaPlayer->setSize(IntSize(videoBounds.width(), videoBounds.height()));
mediaPlayer->setVisible(true);
}
int RenderVideo::calcReplacedWidth(bool includeMaxWidth) const
{
return RenderReplaced::calcReplacedWidth(includeMaxWidth);
}
int RenderVideo::calcReplacedHeight() const
{
return RenderReplaced::calcReplacedHeight();
}
int RenderVideo::minimumReplacedHeight() const
{
return RenderReplaced::minimumReplacedHeight();
}
#if USE(ACCELERATED_COMPOSITING)
bool RenderVideo::supportsAcceleratedRendering() const
{
MediaPlayer* p = player();
if (p)
return p->supportsAcceleratedRendering();
return false;
}
void RenderVideo::acceleratedRenderingStateChanged()
{
MediaPlayer* p = player();
if (p)
p->acceleratedRenderingStateChanged();
}
#endif // USE(ACCELERATED_COMPOSITING)
}
#endif