Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderVideo.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27
28 #include "core/rendering/RenderVideo.h"
29
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"
40
41 namespace WebCore {
42
43 using namespace HTMLNames;
44
45 RenderVideo::RenderVideo(HTMLVideoElement* video)
46     : RenderMedia(video)
47 {
48     setIntrinsicSize(calculateIntrinsicSize());
49 }
50
51 RenderVideo::~RenderVideo()
52 {
53 }
54
55 IntSize RenderVideo::defaultSize()
56 {
57     // These values are specified in the spec.
58     static const int cDefaultWidth = 300;
59     static const int cDefaultHeight = 150;
60
61     return IntSize(cDefaultWidth, cDefaultHeight);
62 }
63
64 void RenderVideo::intrinsicSizeChanged()
65 {
66     if (videoElement()->shouldDisplayPosterImage())
67         RenderMedia::intrinsicSizeChanged();
68     updateIntrinsicSize();
69 }
70
71 void RenderVideo::updateIntrinsicSize()
72 {
73     LayoutSize size = calculateIntrinsicSize();
74     size.scale(style()->effectiveZoom());
75
76     // Never set the element size to zero when in a media document.
77     if (size.isEmpty() && node()->ownerDocument() && node()->ownerDocument()->isMediaDocument())
78         return;
79
80     if (size == intrinsicSize())
81         return;
82
83     setIntrinsicSize(size);
84     setPreferredLogicalWidthsDirty();
85     setNeedsLayout();
86 }
87
88 LayoutSize RenderVideo::calculateIntrinsicSize()
89 {
90     HTMLVideoElement* video = videoElement();
91
92     // Spec text from 4.8.6
93     //
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.
97     //
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();
104         if (!size.isEmpty())
105             return size;
106     }
107
108     if (video->shouldDisplayPosterImage() && !m_cachedImageSize.isEmpty() && !imageResource()->errorOccurred())
109         return m_cachedImageSize;
110
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);
117
118     return defaultSize();
119 }
120
121 void RenderVideo::imageChanged(WrappedImagePtr newImage, const IntRect* rect)
122 {
123     RenderMedia::imageChanged(newImage, rect);
124
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();
130
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();
134 }
135
136 IntRect RenderVideo::videoBox() const
137 {
138     const LayoutSize* overriddenIntrinsicSize = 0;
139     if (videoElement()->shouldDisplayPosterImage())
140         overriddenIntrinsicSize = &m_cachedImageSize;
141
142     return pixelSnappedIntRect(replacedContentRect(overriddenIntrinsicSize));
143 }
144
145 bool RenderVideo::shouldDisplayVideo() const
146 {
147     return !videoElement()->shouldDisplayPosterImage();
148 }
149
150 void RenderVideo::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
151 {
152     MediaPlayer* mediaPlayer = mediaElement()->player();
153     bool displayingPoster = videoElement()->shouldDisplayPosterImage();
154     if (!displayingPoster && !mediaPlayer)
155         return;
156
157     LayoutRect rect = videoBox();
158     if (rect.isEmpty())
159         return;
160     rect.moveBy(paintOffset);
161
162     LayoutRect contentRect = contentBoxRect();
163     contentRect.moveBy(paintOffset);
164     GraphicsContext* context = paintInfo.context;
165     bool clip = !contentRect.contains(rect);
166     if (clip) {
167         context->save();
168         context->clip(contentRect);
169     }
170
171     if (displayingPoster)
172         paintIntoRect(context, rect);
173     else if ((document().view() && document().view()->paintBehavior() & PaintBehaviorFlattenCompositingLayers) || !acceleratedRenderingInUse())
174         mediaPlayer->paint(context, pixelSnappedIntRect(rect));
175
176     if (clip)
177         context->restore();
178 }
179
180 bool RenderVideo::acceleratedRenderingInUse()
181 {
182     blink::WebLayer* webLayer = mediaElement()->platformLayer();
183     return webLayer && !webLayer->isOrphan();
184 }
185
186 void RenderVideo::layout()
187 {
188     LayoutRectRecorder recorder(*this);
189     updatePlayer();
190     RenderMedia::layout();
191 }
192
193 HTMLVideoElement* RenderVideo::videoElement() const
194 {
195     return toHTMLVideoElement(node());
196 }
197
198 void RenderVideo::updateFromElement()
199 {
200     RenderMedia::updateFromElement();
201     updatePlayer();
202 }
203
204 void RenderVideo::updatePlayer()
205 {
206     updateIntrinsicSize();
207
208     MediaPlayer* mediaPlayer = mediaElement()->player();
209     if (!mediaPlayer)
210         return;
211
212     if (!videoElement()->isActive())
213         return;
214
215     contentChanged(VideoChanged);
216 }
217
218 LayoutUnit RenderVideo::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
219 {
220     return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
221 }
222
223 LayoutUnit RenderVideo::computeReplacedLogicalHeight() const
224 {
225     return RenderReplaced::computeReplacedLogicalHeight();
226 }
227
228 LayoutUnit RenderVideo::minimumReplacedHeight() const
229 {
230     return RenderReplaced::minimumReplacedHeight();
231 }
232
233 bool RenderVideo::supportsAcceleratedRendering() const
234 {
235     return !!mediaElement()->platformLayer();
236 }
237
238 static const RenderBlock* rendererPlaceholder(const RenderObject* renderer)
239 {
240     RenderObject* parent = renderer->parent();
241     if (!parent)
242         return 0;
243
244     RenderFullScreen* fullScreen = parent->isRenderFullScreen() ? toRenderFullScreen(parent) : 0;
245     if (!fullScreen)
246         return 0;
247
248     return fullScreen->placeholder();
249 }
250
251 LayoutUnit RenderVideo::offsetLeft() const
252 {
253     if (const RenderBlock* block = rendererPlaceholder(this))
254         return block->offsetLeft();
255     return RenderMedia::offsetLeft();
256 }
257
258 LayoutUnit RenderVideo::offsetTop() const
259 {
260     if (const RenderBlock* block = rendererPlaceholder(this))
261         return block->offsetTop();
262     return RenderMedia::offsetTop();
263 }
264
265 LayoutUnit RenderVideo::offsetWidth() const
266 {
267     if (const RenderBlock* block = rendererPlaceholder(this))
268         return block->offsetWidth();
269     return RenderMedia::offsetWidth();
270 }
271
272 LayoutUnit RenderVideo::offsetHeight() const
273 {
274     if (const RenderBlock* block = rendererPlaceholder(this))
275         return block->offsetHeight();
276     return RenderMedia::offsetHeight();
277 }
278
279 CompositingReasons RenderVideo::additionalCompositingReasons(CompositingTriggerFlags triggers) const
280 {
281     if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
282         HTMLMediaElement* media = toHTMLMediaElement(node());
283         if (media->isFullscreen())
284             return CompositingReasonVideo;
285     }
286
287     if ((triggers & VideoTrigger) && shouldDisplayVideo() && supportsAcceleratedRendering())
288         return CompositingReasonVideo;
289
290     return CompositingReasonNone;
291 }
292
293 } // namespace WebCore