2 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/rendering/RenderVideo.h"
30 #include "HTMLNames.h"
31 #include "core/dom/Document.h"
32 #include "core/frame/FrameView.h"
33 #include "core/frame/LocalFrame.h"
34 #include "core/html/HTMLVideoElement.h"
35 #include "core/rendering/LayoutRectRecorder.h"
36 #include "core/rendering/PaintInfo.h"
37 #include "core/rendering/RenderFullScreen.h"
38 #include "platform/graphics/media/MediaPlayer.h"
39 #include "public/platform/WebLayer.h"
43 using namespace HTMLNames;
45 RenderVideo::RenderVideo(HTMLVideoElement* video)
48 setIntrinsicSize(calculateIntrinsicSize());
51 RenderVideo::~RenderVideo()
55 IntSize RenderVideo::defaultSize()
57 // These values are specified in the spec.
58 static const int cDefaultWidth = 300;
59 static const int cDefaultHeight = 150;
61 return IntSize(cDefaultWidth, cDefaultHeight);
64 void RenderVideo::intrinsicSizeChanged()
66 if (videoElement()->shouldDisplayPosterImage())
67 RenderMedia::intrinsicSizeChanged();
68 updateIntrinsicSize();
71 void RenderVideo::updateIntrinsicSize()
73 LayoutSize size = calculateIntrinsicSize();
74 size.scale(style()->effectiveZoom());
76 // Never set the element size to zero when in a media document.
77 if (size.isEmpty() && node()->ownerDocument() && node()->ownerDocument()->isMediaDocument())
80 if (size == intrinsicSize())
83 setIntrinsicSize(size);
84 setPreferredLogicalWidthsDirty();
88 LayoutSize RenderVideo::calculateIntrinsicSize()
90 HTMLVideoElement* video = videoElement();
92 // Spec text from 4.8.6
94 // The intrinsic width of a video element's playback area is the intrinsic width
95 // of the video resource, if that is available; otherwise it is the intrinsic
96 // width of the poster frame, if that is available; otherwise it is 300 CSS pixels.
98 // The intrinsic height of a video element's playback area is the intrinsic height
99 // of the video resource, if that is available; otherwise it is the intrinsic
100 // height of the poster frame, if that is available; otherwise it is 150 CSS pixels.
101 MediaPlayer* player = mediaElement()->player();
102 if (player && video->readyState() >= HTMLVideoElement::HAVE_METADATA) {
103 LayoutSize size = player->naturalSize();
108 if (video->shouldDisplayPosterImage() && !m_cachedImageSize.isEmpty() && !imageResource()->errorOccurred())
109 return m_cachedImageSize;
111 // <video> in standalone media documents should not use the default 300x150
112 // size since they also have audio-only files. By setting the intrinsic
113 // size to 300x1 the video will resize itself in these cases, and audio will
114 // have the correct height (it needs to be > 0 for controls to render properly).
115 if (video->ownerDocument() && video->ownerDocument()->isMediaDocument())
116 return LayoutSize(defaultSize().width(), 1);
118 return defaultSize();
121 void RenderVideo::imageChanged(WrappedImagePtr newImage, const IntRect* rect)
123 RenderMedia::imageChanged(newImage, rect);
125 // Cache the image intrinsic size so we can continue to use it to draw the image correctly
126 // even if we know the video intrinsic size but aren't able to draw video frames yet
127 // (we don't want to scale the poster to the video size without keeping aspect ratio).
128 if (videoElement()->shouldDisplayPosterImage())
129 m_cachedImageSize = intrinsicSize();
131 // The intrinsic size is now that of the image, but in case we already had the
132 // intrinsic size of the video we call this here to restore the video size.
133 updateIntrinsicSize();
136 IntRect RenderVideo::videoBox() const
138 const LayoutSize* overriddenIntrinsicSize = 0;
139 if (videoElement()->shouldDisplayPosterImage())
140 overriddenIntrinsicSize = &m_cachedImageSize;
142 return pixelSnappedIntRect(replacedContentRect(overriddenIntrinsicSize));
145 bool RenderVideo::shouldDisplayVideo() const
147 return !videoElement()->shouldDisplayPosterImage();
150 void RenderVideo::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
152 MediaPlayer* mediaPlayer = mediaElement()->player();
153 bool displayingPoster = videoElement()->shouldDisplayPosterImage();
154 if (!displayingPoster && !mediaPlayer)
157 LayoutRect rect = videoBox();
160 rect.moveBy(paintOffset);
162 LayoutRect contentRect = contentBoxRect();
163 contentRect.moveBy(paintOffset);
164 GraphicsContext* context = paintInfo.context;
165 bool clip = !contentRect.contains(rect);
168 context->clip(contentRect);
171 if (displayingPoster)
172 paintIntoRect(context, rect);
173 else if ((document().view() && document().view()->paintBehavior() & PaintBehaviorFlattenCompositingLayers) || !acceleratedRenderingInUse())
174 mediaPlayer->paint(context, pixelSnappedIntRect(rect));
180 bool RenderVideo::acceleratedRenderingInUse()
182 blink::WebLayer* webLayer = mediaElement()->platformLayer();
183 return webLayer && !webLayer->isOrphan();
186 void RenderVideo::layout()
188 LayoutRectRecorder recorder(*this);
190 RenderMedia::layout();
193 HTMLVideoElement* RenderVideo::videoElement() const
195 return toHTMLVideoElement(node());
198 void RenderVideo::updateFromElement()
200 RenderMedia::updateFromElement();
204 void RenderVideo::updatePlayer()
206 updateIntrinsicSize();
208 MediaPlayer* mediaPlayer = mediaElement()->player();
212 if (!videoElement()->isActive())
215 contentChanged(VideoChanged);
218 LayoutUnit RenderVideo::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
220 return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
223 LayoutUnit RenderVideo::computeReplacedLogicalHeight() const
225 return RenderReplaced::computeReplacedLogicalHeight();
228 LayoutUnit RenderVideo::minimumReplacedHeight() const
230 return RenderReplaced::minimumReplacedHeight();
233 bool RenderVideo::supportsAcceleratedRendering() const
235 return !!mediaElement()->platformLayer();
238 static const RenderBlock* rendererPlaceholder(const RenderObject* renderer)
240 RenderObject* parent = renderer->parent();
244 RenderFullScreen* fullScreen = parent->isRenderFullScreen() ? toRenderFullScreen(parent) : 0;
248 return fullScreen->placeholder();
251 LayoutUnit RenderVideo::offsetLeft() const
253 if (const RenderBlock* block = rendererPlaceholder(this))
254 return block->offsetLeft();
255 return RenderMedia::offsetLeft();
258 LayoutUnit RenderVideo::offsetTop() const
260 if (const RenderBlock* block = rendererPlaceholder(this))
261 return block->offsetTop();
262 return RenderMedia::offsetTop();
265 LayoutUnit RenderVideo::offsetWidth() const
267 if (const RenderBlock* block = rendererPlaceholder(this))
268 return block->offsetWidth();
269 return RenderMedia::offsetWidth();
272 LayoutUnit RenderVideo::offsetHeight() const
274 if (const RenderBlock* block = rendererPlaceholder(this))
275 return block->offsetHeight();
276 return RenderMedia::offsetHeight();
279 CompositingReasons RenderVideo::additionalCompositingReasons(CompositingTriggerFlags triggers) const
281 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
282 HTMLMediaElement* media = toHTMLMediaElement(node());
283 if (media->isFullscreen())
284 return CompositingReasonVideo;
287 if ((triggers & VideoTrigger) && shouldDisplayVideo() && supportsAcceleratedRendering())
288 return CompositingReasonVideo;
290 return CompositingReasonNone;
293 } // namespace WebCore