1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla code.
18 * The Initial Developer of the Original Code is the Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
23 * Chris Double <chris.double@double.co.nz>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 /* rendering object for the HTML <video> element */
41 #include "nsHTMLParts.h"
43 #include "nsIServiceManager.h"
44 #include "nsGkAtoms.h"
46 #include "nsVideoFrame.h"
47 #include "nsHTMLVideoElement.h"
48 #include "nsIDOMHTMLVideoElement.h"
49 #include "nsDisplayList.h"
50 #include "nsIRenderingContext.h"
51 #include "gfxContext.h"
52 #include "gfxImageSurface.h"
53 #include "nsPresContext.h"
54 #include "nsTransform2D.h"
55 #include "nsContentCreatorFunctions.h"
56 #include "nsBoxLayoutState.h"
57 #include "nsBoxFrame.h"
60 NS_NewHTMLVideoFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
62 return new (aPresShell
) nsVideoFrame(aContext
);
65 nsVideoFrame::nsVideoFrame(nsStyleContext
* aContext
) :
66 nsContainerFrame(aContext
)
70 nsVideoFrame::~nsVideoFrame()
74 NS_INTERFACE_MAP_BEGIN(nsVideoFrame
)
75 NS_INTERFACE_MAP_ENTRY(nsIAnonymousContentCreator
)
77 NS_INTERFACE_MAP_ENTRY(nsIFrameDebug
)
79 NS_INTERFACE_MAP_END_INHERITING(nsContainerFrame
)
81 NS_IMETHODIMP_(nsrefcnt
)
82 nsVideoFrame::AddRef(void)
87 NS_IMETHODIMP_(nsrefcnt
)
88 nsVideoFrame::Release(void)
94 nsVideoFrame::CreateAnonymousContent(nsTArray
<nsIContent
*>& aElements
)
96 // Set up "videocontrols" XUL element which will be XBL-bound to the
98 nsPresContext
* presContext
= PresContext();
99 nsNodeInfoManager
*nodeInfoManager
=
100 presContext
->Document()->NodeInfoManager();
101 nsCOMPtr
<nsINodeInfo
> nodeInfo
;
102 nsresult rv
= nodeInfoManager
->GetNodeInfo(nsGkAtoms::videocontrols
, nsnull
,
103 kNameSpaceID_XUL
, getter_AddRefs(nodeInfo
));
104 NS_ENSURE_SUCCESS(rv
, rv
);
106 rv
= NS_NewElement(getter_AddRefs(mVideoControls
), kNameSpaceID_XUL
, nodeInfo
, PR_FALSE
);
107 NS_ENSURE_SUCCESS(rv
, rv
);
108 if (!aElements
.AppendElement(mVideoControls
))
109 return NS_ERROR_OUT_OF_MEMORY
;
114 nsVideoFrame::Destroy()
116 nsContentUtils::DestroyAnonymousContent(&mVideoControls
);
117 nsContainerFrame::Destroy();
121 nsVideoFrame::IsLeaf() const
127 nsVideoFrame::PaintVideo(nsIRenderingContext
& aRenderingContext
,
128 const nsRect
& aDirtyRect
, nsPoint aPt
)
130 gfxContext
* ctx
= static_cast<gfxContext
*>(aRenderingContext
.GetNativeGraphicData(nsIRenderingContext::NATIVE_THEBES_CONTEXT
));
131 // TODO: handle the situation where the frame size is not the same as the
132 // video size, by drawing to the largest rectangle that fits in the frame
133 // whose aspect ratio equals the video's aspect ratio
134 nsRect area
= GetContentRect() - GetPosition() + aPt
;
135 nsPresContext
* presContext
= PresContext();
136 gfxRect r
= gfxRect(presContext
->AppUnitsToGfxUnits(area
.x
),
137 presContext
->AppUnitsToGfxUnits(area
.y
),
138 presContext
->AppUnitsToGfxUnits(area
.width
),
139 presContext
->AppUnitsToGfxUnits(area
.height
));
141 nsHTMLVideoElement
* element
= static_cast<nsHTMLVideoElement
*>(GetContent());
142 element
->Paint(ctx
, r
);
146 nsVideoFrame::Reflow(nsPresContext
* aPresContext
,
147 nsHTMLReflowMetrics
& aMetrics
,
148 const nsHTMLReflowState
& aReflowState
,
149 nsReflowStatus
& aStatus
)
151 DO_GLOBAL_REFLOW_COUNT("nsVideoFrame");
152 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aMetrics
, aStatus
);
153 NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS
,
154 ("enter nsVideoFrame::Reflow: availSize=%d,%d",
155 aReflowState
.availableWidth
, aReflowState
.availableHeight
));
157 NS_PRECONDITION(mState
& NS_FRAME_IN_REFLOW
, "frame is not in reflow");
159 aStatus
= NS_FRAME_COMPLETE
;
161 aMetrics
.width
= aReflowState
.ComputedWidth();
162 aMetrics
.height
= aReflowState
.ComputedHeight();
164 // stash this away so we can compute our inner area later
165 mBorderPadding
= aReflowState
.mComputedBorderPadding
;
167 aMetrics
.width
+= mBorderPadding
.left
+ mBorderPadding
.right
;
168 aMetrics
.height
+= mBorderPadding
.top
+ mBorderPadding
.bottom
;
170 nsIFrame
* child
= mFrames
.FirstChild();
172 NS_ASSERTION(child
->GetContent() == mVideoControls
,
173 "What is this child doing here?");
174 nsBoxLayoutState
boxState(PresContext(), aReflowState
.rendContext
);
175 nsBoxFrame::LayoutChildAt(boxState
, child
,
176 nsRect(mBorderPadding
.left
, mBorderPadding
.right
,
177 aReflowState
.ComputedWidth(), aReflowState
.ComputedHeight()));
180 aMetrics
.mOverflowArea
.SetRect(0, 0, aMetrics
.width
, aMetrics
.height
);
182 FinishAndStoreOverflow(&aMetrics
);
184 if (mRect
.width
!= aMetrics
.width
|| mRect
.height
!= aMetrics
.height
) {
185 Invalidate(nsRect(0, 0, mRect
.width
, mRect
.height
), PR_FALSE
);
188 NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS
,
189 ("exit nsVideoFrame::Reflow: size=%d,%d",
190 aMetrics
.width
, aMetrics
.height
));
191 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aMetrics
);
196 static void PaintVideo(nsIFrame
* aFrame
, nsIRenderingContext
* aCtx
,
197 const nsRect
& aDirtyRect
, nsPoint aPt
)
200 double start
= double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
203 static_cast<nsVideoFrame
*>(aFrame
)->PaintVideo(*aCtx
, aDirtyRect
, aPt
);
205 double end
= double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
206 printf("PaintVideo: %f\n", (float)end
- (float)start
);
212 nsVideoFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
213 const nsRect
& aDirtyRect
,
214 const nsDisplayListSet
& aLists
)
216 if (!IsVisibleForPainting(aBuilder
))
219 DO_GLOBAL_REFLOW_COUNT_DSP("nsVideoFrame");
221 nsresult rv
= DisplayBorderBackgroundOutline(aBuilder
, aLists
);
222 NS_ENSURE_SUCCESS(rv
, rv
);
224 rv
= aLists
.Content()->AppendNewToTop(new (aBuilder
) nsDisplayGeneric(this, ::PaintVideo
, "Video"));
225 NS_ENSURE_SUCCESS(rv
, rv
);
227 if (mFrames
.FirstChild()) {
228 rv
= mFrames
.FirstChild()->BuildDisplayListForStackingContext(aBuilder
, aDirtyRect
, aLists
.Content());
234 nsVideoFrame::GetType() const
236 return nsGkAtoms::HTMLVideoFrame
;
241 nsVideoFrame::GetAccessible(nsIAccessible
** aAccessible
)
243 return NS_ERROR_FAILURE
;
249 nsVideoFrame::GetFrameName(nsAString
& aResult
) const
251 return MakeFrameName(NS_LITERAL_STRING("HTMLVideo"), aResult
);
255 nsSize
nsVideoFrame::ComputeSize(nsIRenderingContext
*aRenderingContext
,
257 nscoord aAvailableWidth
,
263 nsSize size
= GetVideoSize();
265 IntrinsicSize intrinsicSize
;
266 intrinsicSize
.width
.SetCoordValue(size
.width
);
267 intrinsicSize
.height
.SetCoordValue(size
.height
);
269 nsSize
& intrinsicRatio
= size
; // won't actually be used
271 return nsLayoutUtils::ComputeSizeWithIntrinsicDimensions(aRenderingContext
,
281 nscoord
nsVideoFrame::GetMinWidth(nsIRenderingContext
*aRenderingContext
)
283 // XXX The caller doesn't account for constraints of the height,
284 // min-height, and max-height properties.
285 nscoord result
= GetVideoSize().width
;
286 DISPLAY_MIN_WIDTH(this, result
);
290 nscoord
nsVideoFrame::GetPrefWidth(nsIRenderingContext
*aRenderingContext
)
292 // XXX The caller doesn't account for constraints of the height,
293 // min-height, and max-height properties.
294 nscoord result
= GetVideoSize().width
;
295 DISPLAY_PREF_WIDTH(this, result
);
299 nsSize
nsVideoFrame::GetIntrinsicRatio()
301 return GetVideoSize();
304 nsSize
nsVideoFrame::GetVideoSize()
306 // Defaulting size to 300x150 if no size given.
307 nsIntSize
size(300,150);
309 nsHTMLVideoElement
* element
= static_cast<nsHTMLVideoElement
*>(GetContent());
313 size
= element
->GetVideoSize(size
);
314 if (element
->HasAttr(kNameSpaceID_None
, nsGkAtoms::width
)) {
316 rv
= element
->GetWidth(&width
);
317 if (NS_SUCCEEDED(rv
)) {
321 if (element
->HasAttr(kNameSpaceID_None
, nsGkAtoms::height
)) {
323 rv
= element
->GetHeight(&height
);
324 if (NS_SUCCEEDED(rv
)) {
325 size
.height
= height
;
330 return nsSize(nsPresContext::CSSPixelsToAppUnits(size
.width
),
331 nsPresContext::CSSPixelsToAppUnits(size
.height
));
334 NS_IMETHODIMP
nsVideoFrame::Freeze()
336 nsHTMLVideoElement
* element
= static_cast<nsHTMLVideoElement
*>(GetContent());
341 NS_IMETHODIMP
nsVideoFrame::Thaw()
343 nsHTMLVideoElement
* element
= static_cast<nsHTMLVideoElement
*>(GetContent());