1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* rendering object for the HTML <canvas> element */
9 #include "nsHTMLCanvasFrame.h"
11 #include "nsGkAtoms.h"
12 #include "mozilla/Assertions.h"
13 #include "mozilla/PresShell.h"
14 #include "mozilla/dom/HTMLCanvasElement.h"
15 #include "mozilla/layers/ImageDataSerializer.h"
16 #include "mozilla/layers/WebRenderBridgeChild.h"
17 #include "mozilla/layers/WebRenderCanvasRenderer.h"
18 #include "mozilla/layers/RenderRootStateManager.h"
19 #include "mozilla/webgpu/CanvasContext.h"
20 #include "nsDisplayList.h"
21 #include "nsLayoutUtils.h"
22 #include "nsStyleUtil.h"
23 #include "ActiveLayerTracker.h"
27 using namespace mozilla
;
28 using namespace mozilla::dom
;
29 using namespace mozilla::layers
;
30 using namespace mozilla::gfx
;
32 /* Helper for our nsIFrame::GetIntrinsicSize() impl. Takes the result of
33 * "GetCanvasSize()" as a parameter, which may help avoid redundant
34 * indirect calls to GetCanvasSize().
36 * @param aCanvasSizeInPx The canvas's size in CSS pixels, as returned
38 * @return The canvas's intrinsic size, as an IntrinsicSize object.
40 static IntrinsicSize
IntrinsicSizeFromCanvasSize(
41 const nsIntSize
& aCanvasSizeInPx
) {
43 nsPresContext::CSSPixelsToAppUnits(aCanvasSizeInPx
.width
),
44 nsPresContext::CSSPixelsToAppUnits(aCanvasSizeInPx
.height
));
47 /* Helper for our nsIFrame::GetIntrinsicRatio() impl. Takes the result of
48 * "GetCanvasSize()" as a parameter, which may help avoid redundant
49 * indirect calls to GetCanvasSize().
51 * @return The canvas's intrinsic ratio.
53 static AspectRatio
IntrinsicRatioFromCanvasSize(
54 const nsIntSize
& aCanvasSizeInPx
) {
55 return AspectRatio::FromSize(aCanvasSizeInPx
.width
, aCanvasSizeInPx
.height
);
58 class nsDisplayCanvas final
: public nsPaintedDisplayItem
{
60 nsDisplayCanvas(nsDisplayListBuilder
* aBuilder
, nsIFrame
* aFrame
)
61 : nsPaintedDisplayItem(aBuilder
, aFrame
) {
62 MOZ_COUNT_CTOR(nsDisplayCanvas
);
64 MOZ_COUNTED_DTOR_OVERRIDE(nsDisplayCanvas
)
66 NS_DISPLAY_DECL_NAME("nsDisplayCanvas", TYPE_CANVAS
)
68 virtual nsRegion
GetOpaqueRegion(nsDisplayListBuilder
* aBuilder
,
69 bool* aSnap
) const override
{
71 nsHTMLCanvasFrame
* f
= static_cast<nsHTMLCanvasFrame
*>(Frame());
72 HTMLCanvasElement
* canvas
= HTMLCanvasElement::FromNode(f
->GetContent());
74 if (canvas
->GetIsOpaque()) {
75 // OK, the entire region painted by the canvas is opaque. But what is
76 // that region? It's the canvas's "dest rect" (controlled by the
77 // object-fit/object-position CSS properties), clipped to the container's
78 // content box (which is what GetBounds() returns). So, we grab those
79 // rects and intersect them.
80 nsRect constraintRect
= GetBounds(aBuilder
, aSnap
);
82 // Need intrinsic size & ratio, for ComputeObjectDestRect:
83 nsIntSize canvasSize
= f
->GetCanvasSize();
84 IntrinsicSize intrinsicSize
= IntrinsicSizeFromCanvasSize(canvasSize
);
85 AspectRatio intrinsicRatio
= IntrinsicRatioFromCanvasSize(canvasSize
);
87 const nsRect destRect
= nsLayoutUtils::ComputeObjectDestRect(
88 constraintRect
, intrinsicSize
, intrinsicRatio
, f
->StylePosition());
89 return nsRegion(destRect
.Intersect(constraintRect
));
94 virtual nsRect
GetBounds(nsDisplayListBuilder
* aBuilder
,
95 bool* aSnap
) const override
{
97 return Frame()->GetContentRectRelativeToSelf() + ToReferenceFrame();
100 virtual bool CreateWebRenderCommands(
101 mozilla::wr::DisplayListBuilder
& aBuilder
,
102 wr::IpcResourceUpdateQueue
& aResources
, const StackingContextHelper
& aSc
,
103 mozilla::layers::RenderRootStateManager
* aManager
,
104 nsDisplayListBuilder
* aDisplayListBuilder
) override
{
105 HTMLCanvasElement
* element
=
106 static_cast<HTMLCanvasElement
*>(mFrame
->GetContent());
107 element
->HandlePrintCallback(mFrame
->PresContext());
109 if (element
->IsOffscreen()) {
110 // If we are offscreen, then we either display via an ImageContainer
111 // which is updated asynchronously, likely from a worker thread, or a
112 // CompositableHandle managed inside the compositor process. There is
113 // nothing to paint until the owner attaches it.
115 element
->FlushOffscreenCanvas();
117 nsHTMLCanvasFrame
* canvasFrame
= static_cast<nsHTMLCanvasFrame
*>(mFrame
);
118 nsIntSize canvasSizeInPx
= canvasFrame
->GetCanvasSize();
119 IntrinsicSize intrinsicSize
= IntrinsicSizeFromCanvasSize(canvasSizeInPx
);
120 AspectRatio intrinsicRatio
= IntrinsicRatioFromCanvasSize(canvasSizeInPx
);
121 nsRect area
= mFrame
->GetContentRectRelativeToSelf() + ToReferenceFrame();
122 nsRect dest
= nsLayoutUtils::ComputeObjectDestRect(
123 area
, intrinsicSize
, intrinsicRatio
, mFrame
->StylePosition());
124 LayoutDeviceRect bounds
= LayoutDeviceRect::FromAppUnits(
125 dest
, mFrame
->PresContext()->AppUnitsPerDevPixel());
127 RefPtr
<ImageContainer
> container
= element
->GetImageContainer();
129 MOZ_ASSERT(container
->IsAsync());
130 aManager
->CommandBuilder().PushImage(this, container
, aBuilder
,
131 aResources
, aSc
, bounds
, bounds
);
138 switch (element
->GetCurrentContextType()) {
139 case CanvasContextType::Canvas2D
:
140 case CanvasContextType::WebGL1
:
141 case CanvasContextType::WebGL2
:
142 case CanvasContextType::WebGPU
: {
144 RefPtr
<WebRenderCanvasData
> canvasData
=
145 aManager
->CommandBuilder()
146 .CreateOrRecycleWebRenderUserData
<WebRenderCanvasData
>(
148 nsHTMLCanvasFrame
* canvasFrame
=
149 static_cast<nsHTMLCanvasFrame
*>(mFrame
);
150 if (!canvasFrame
->UpdateWebRenderCanvasData(aDisplayListBuilder
,
154 WebRenderCanvasRendererAsync
* data
= canvasData
->GetCanvasRenderer();
156 data
->UpdateCompositableClient();
158 // Push IFrame for async image pipeline.
159 // XXX Remove this once partial display list update is supported.
161 nsIntSize canvasSizeInPx
= data
->GetSize();
162 IntrinsicSize intrinsicSize
=
163 IntrinsicSizeFromCanvasSize(canvasSizeInPx
);
164 AspectRatio intrinsicRatio
=
165 IntrinsicRatioFromCanvasSize(canvasSizeInPx
);
168 mFrame
->GetContentRectRelativeToSelf() + ToReferenceFrame();
169 nsRect dest
= nsLayoutUtils::ComputeObjectDestRect(
170 area
, intrinsicSize
, intrinsicRatio
, mFrame
->StylePosition());
172 LayoutDeviceRect bounds
= LayoutDeviceRect::FromAppUnits(
173 dest
, mFrame
->PresContext()->AppUnitsPerDevPixel());
175 // We don't push a stacking context for this async image pipeline here.
176 // Instead, we do it inside the iframe that hosts the image. As a
177 // result, a bunch of the calculations normally done as part of that
178 // stacking context need to be done manually and pushed over to the
179 // parent side, where it will be done when we build the display list for
180 // the iframe. That happens in WebRenderCompositableHolder.s2);
181 aBuilder
.PushIFrame(bounds
, !BackfaceIsHidden(),
182 data
->GetPipelineId().ref(),
183 /*ignoreMissingPipelines*/ false);
185 LayoutDeviceRect
scBounds(LayoutDevicePoint(0, 0), bounds
.Size());
186 auto filter
= wr::ToImageRendering(mFrame
->UsedImageRendering());
187 auto mixBlendMode
= wr::MixBlendMode::Normal
;
188 aManager
->WrBridge()->AddWebRenderParentCommand(
189 OpUpdateAsyncImagePipeline(data
->GetPipelineId().value(), scBounds
,
190 wr::WrRotation::Degree0
, filter
,
194 case CanvasContextType::ImageBitmap
: {
195 nsHTMLCanvasFrame
* canvasFrame
=
196 static_cast<nsHTMLCanvasFrame
*>(mFrame
);
197 nsIntSize canvasSizeInPx
= canvasFrame
->GetCanvasSize();
198 if (canvasSizeInPx
.width
<= 0 || canvasSizeInPx
.height
<= 0) {
202 RefPtr
<WebRenderCanvasData
> canvasData
=
203 aManager
->CommandBuilder()
204 .CreateOrRecycleWebRenderUserData
<WebRenderCanvasData
>(
206 if (!canvasFrame
->UpdateWebRenderCanvasData(aDisplayListBuilder
,
208 canvasData
->ClearImageContainer();
212 IntrinsicSize intrinsicSize
=
213 IntrinsicSizeFromCanvasSize(canvasSizeInPx
);
214 AspectRatio intrinsicRatio
=
215 IntrinsicRatioFromCanvasSize(canvasSizeInPx
);
218 mFrame
->GetContentRectRelativeToSelf() + ToReferenceFrame();
219 nsRect dest
= nsLayoutUtils::ComputeObjectDestRect(
220 area
, intrinsicSize
, intrinsicRatio
, mFrame
->StylePosition());
222 LayoutDeviceRect bounds
= LayoutDeviceRect::FromAppUnits(
223 dest
, mFrame
->PresContext()->AppUnitsPerDevPixel());
225 aManager
->CommandBuilder().PushImage(
226 this, canvasData
->GetImageContainer(), aBuilder
, aResources
, aSc
,
230 case CanvasContextType::NoContext
:
233 MOZ_ASSERT_UNREACHABLE("unknown canvas context type");
238 // FirstContentfulPaint is supposed to ignore "white" canvases. We use
239 // MaybeModified (if GetContext() was called on the canvas) as a standin for
241 virtual bool IsContentful() const override
{
242 nsHTMLCanvasFrame
* f
= static_cast<nsHTMLCanvasFrame
*>(Frame());
243 HTMLCanvasElement
* canvas
= HTMLCanvasElement::FromNode(f
->GetContent());
244 return canvas
->MaybeModified();
247 virtual void Paint(nsDisplayListBuilder
* aBuilder
,
248 gfxContext
* aCtx
) override
{
249 nsHTMLCanvasFrame
* f
= static_cast<nsHTMLCanvasFrame
*>(Frame());
250 HTMLCanvasElement
* canvas
= HTMLCanvasElement::FromNode(f
->GetContent());
252 nsRect area
= f
->GetContentRectRelativeToSelf() + ToReferenceFrame();
253 nsIntSize canvasSizeInPx
= f
->GetCanvasSize();
255 nsPresContext
* presContext
= f
->PresContext();
256 canvas
->HandlePrintCallback(presContext
);
258 if (canvasSizeInPx
.width
<= 0 || canvasSizeInPx
.height
<= 0 ||
263 IntrinsicSize intrinsicSize
= IntrinsicSizeFromCanvasSize(canvasSizeInPx
);
264 AspectRatio intrinsicRatio
= IntrinsicRatioFromCanvasSize(canvasSizeInPx
);
266 nsRect dest
= nsLayoutUtils::ComputeObjectDestRect(
267 area
, intrinsicSize
, intrinsicRatio
, f
->StylePosition());
269 gfxContextMatrixAutoSaveRestore
saveMatrix(aCtx
);
271 if (RefPtr
<layers::Image
> image
= canvas
->GetAsImage()) {
272 gfxRect destGFXRect
= presContext
->AppUnitsToGfxUnits(dest
);
274 // Transform the canvas into the right place
275 gfxPoint p
= destGFXRect
.TopLeft();
276 Matrix transform
= Matrix::Translation(p
.x
, p
.y
);
277 transform
.PreScale(destGFXRect
.Width() / canvasSizeInPx
.width
,
278 destGFXRect
.Height() / canvasSizeInPx
.height
);
281 gfxUtils::SnapTransformTranslation(aCtx
->CurrentMatrix(), nullptr));
283 RefPtr
<gfx::SourceSurface
> surface
= image
->GetAsSourceSurface();
284 if (!surface
|| !surface
->IsValid()) {
288 transform
= gfxUtils::SnapTransform(
289 transform
, gfxRect(0, 0, canvasSizeInPx
.width
, canvasSizeInPx
.height
),
291 aCtx
->Multiply(transform
);
293 aCtx
->GetDrawTarget()->FillRect(
294 Rect(0, 0, canvasSizeInPx
.width
, canvasSizeInPx
.height
),
295 SurfacePattern(surface
, ExtendMode::CLAMP
, Matrix(),
296 nsLayoutUtils::GetSamplingFilterForFrame(f
)));
300 if (canvas
->IsOffscreen()) {
304 RefPtr
<CanvasRenderer
> renderer
= new CanvasRenderer();
305 if (!canvas
->InitializeCanvasRenderer(aBuilder
, renderer
)) {
308 renderer
->FirePreTransactionCallback();
309 const auto snapshot
= renderer
->BorrowSnapshot();
313 const auto& surface
= snapshot
->mSurf
;
314 DrawTarget
& dt
= *aCtx
->GetDrawTarget();
316 NSRectToSnappedRect(dest
, presContext
->AppUnitsPerDevPixel(), dt
);
318 if (!renderer
->YIsDown()) {
319 // Calculate y-coord that is as far below the bottom of destGFXRect as
320 // the origin was above the top, then reflect about that.
321 float y
= destRect
.Y() + destRect
.YMost();
322 Matrix transform
= Matrix::Translation(0.0f
, y
).PreScale(1.0f
, -1.0f
);
323 aCtx
->Multiply(transform
);
326 const auto& srcRect
= surface
->GetRect();
329 Rect(float(srcRect
.X()), float(srcRect
.Y()), float(srcRect
.Width()),
330 float(srcRect
.Height())),
331 DrawSurfaceOptions(nsLayoutUtils::GetSamplingFilterForFrame(f
)));
333 renderer
->FireDidTransactionCallback();
334 renderer
->ResetDirty();
338 nsIFrame
* NS_NewHTMLCanvasFrame(PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
339 return new (aPresShell
)
340 nsHTMLCanvasFrame(aStyle
, aPresShell
->GetPresContext());
343 NS_QUERYFRAME_HEAD(nsHTMLCanvasFrame
)
344 NS_QUERYFRAME_ENTRY(nsHTMLCanvasFrame
)
345 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame
)
347 NS_IMPL_FRAMEARENA_HELPERS(nsHTMLCanvasFrame
)
349 void nsHTMLCanvasFrame::Destroy(DestroyContext
& aContext
) {
350 if (IsPrimaryFrame()) {
351 HTMLCanvasElement::FromNode(*mContent
)->ResetPrintCallback();
353 nsContainerFrame::Destroy(aContext
);
356 nsHTMLCanvasFrame::~nsHTMLCanvasFrame() = default;
358 nsIntSize
nsHTMLCanvasFrame::GetCanvasSize() const {
359 nsIntSize
size(0, 0);
360 HTMLCanvasElement
* canvas
= HTMLCanvasElement::FromNodeOrNull(GetContent());
362 size
= canvas
->GetSize();
363 MOZ_ASSERT(size
.width
>= 0 && size
.height
>= 0,
364 "we should've required <canvas> width/height attrs to be "
365 "unsigned (non-negative) values");
367 MOZ_ASSERT_UNREACHABLE("couldn't get canvas size");
374 nscoord
nsHTMLCanvasFrame::GetMinISize(gfxContext
* aRenderingContext
) {
375 // XXX The caller doesn't account for constraints of the height,
376 // min-height, and max-height properties.
378 if (Maybe
<nscoord
> containISize
= ContainIntrinsicISize()) {
379 result
= *containISize
;
381 bool vertical
= GetWritingMode().IsVertical();
382 result
= nsPresContext::CSSPixelsToAppUnits(
383 vertical
? GetCanvasSize().height
: GetCanvasSize().width
);
385 DISPLAY_MIN_INLINE_SIZE(this, result
);
390 nscoord
nsHTMLCanvasFrame::GetPrefISize(gfxContext
* aRenderingContext
) {
391 // XXX The caller doesn't account for constraints of the height,
392 // min-height, and max-height properties.
394 if (Maybe
<nscoord
> containISize
= ContainIntrinsicISize()) {
395 result
= *containISize
;
397 bool vertical
= GetWritingMode().IsVertical();
398 result
= nsPresContext::CSSPixelsToAppUnits(
399 vertical
? GetCanvasSize().height
: GetCanvasSize().width
);
401 DISPLAY_PREF_INLINE_SIZE(this, result
);
406 IntrinsicSize
nsHTMLCanvasFrame::GetIntrinsicSize() {
407 const auto containAxes
= GetContainSizeAxes();
408 IntrinsicSize size
= containAxes
.IsBoth()
409 ? IntrinsicSize(0, 0)
410 : IntrinsicSizeFromCanvasSize(GetCanvasSize());
411 return containAxes
.ContainIntrinsicSize(size
, *this);
415 AspectRatio
nsHTMLCanvasFrame::GetIntrinsicRatio() const {
416 if (GetContainSizeAxes().IsAny()) {
417 return AspectRatio();
420 return IntrinsicRatioFromCanvasSize(GetCanvasSize());
424 nsIFrame::SizeComputationResult
nsHTMLCanvasFrame::ComputeSize(
425 gfxContext
* aRenderingContext
, WritingMode aWM
, const LogicalSize
& aCBSize
,
426 nscoord aAvailableISize
, const LogicalSize
& aMargin
,
427 const LogicalSize
& aBorderPadding
, const StyleSizeOverrides
& aSizeOverrides
,
428 ComputeSizeFlags aFlags
) {
429 return {ComputeSizeWithIntrinsicDimensions(
430 aRenderingContext
, aWM
, GetIntrinsicSize(), GetAspectRatio(),
431 aCBSize
, aMargin
, aBorderPadding
, aSizeOverrides
, aFlags
),
432 AspectRatioUsage::None
};
435 void nsHTMLCanvasFrame::Reflow(nsPresContext
* aPresContext
,
436 ReflowOutput
& aMetrics
,
437 const ReflowInput
& aReflowInput
,
438 nsReflowStatus
& aStatus
) {
440 DO_GLOBAL_REFLOW_COUNT("nsHTMLCanvasFrame");
441 DISPLAY_REFLOW(aPresContext
, this, aReflowInput
, aMetrics
, aStatus
);
442 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
444 NS_FRAME_TRACE_CALLS
,
445 ("enter nsHTMLCanvasFrame::Reflow: availSize=%d,%d",
446 aReflowInput
.AvailableWidth(), aReflowInput
.AvailableHeight()));
448 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_IN_REFLOW
), "frame is not in reflow");
450 WritingMode wm
= aReflowInput
.GetWritingMode();
451 const LogicalSize finalSize
= aReflowInput
.ComputedSizeWithBorderPadding(wm
);
453 aMetrics
.SetSize(wm
, finalSize
);
454 aMetrics
.SetOverflowAreasToDesiredBounds();
455 FinishAndStoreOverflow(&aMetrics
);
457 // Reflow the single anon block child.
458 nsReflowStatus childStatus
;
459 nsIFrame
* childFrame
= mFrames
.FirstChild();
460 WritingMode childWM
= childFrame
->GetWritingMode();
461 LogicalSize availSize
= aReflowInput
.ComputedSize(childWM
);
462 availSize
.BSize(childWM
) = NS_UNCONSTRAINEDSIZE
;
463 NS_ASSERTION(!childFrame
->GetNextSibling(), "HTML canvas should have 1 kid");
464 ReflowOutput
childDesiredSize(aReflowInput
.GetWritingMode());
465 ReflowInput
childReflowInput(aPresContext
, aReflowInput
, childFrame
,
467 ReflowChild(childFrame
, aPresContext
, childDesiredSize
, childReflowInput
, 0,
468 0, ReflowChildFlags::Default
, childStatus
, nullptr);
469 FinishReflowChild(childFrame
, aPresContext
, childDesiredSize
,
470 &childReflowInput
, 0, 0, ReflowChildFlags::Default
);
472 NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS
,
473 ("exit nsHTMLCanvasFrame::Reflow: size=%d,%d",
474 aMetrics
.ISize(wm
), aMetrics
.BSize(wm
)));
477 bool nsHTMLCanvasFrame::UpdateWebRenderCanvasData(
478 nsDisplayListBuilder
* aBuilder
, WebRenderCanvasData
* aCanvasData
) {
479 HTMLCanvasElement
* element
= static_cast<HTMLCanvasElement
*>(GetContent());
480 return element
->UpdateWebRenderCanvasData(aBuilder
, aCanvasData
);
483 void nsHTMLCanvasFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
484 const nsDisplayListSet
& aLists
) {
485 if (!IsVisibleForPainting()) return;
487 DisplayBorderBackgroundOutline(aBuilder
, aLists
);
489 if (HidesContent()) {
490 DisplaySelectionOverlay(aBuilder
, aLists
.Content(),
491 nsISelectionDisplay::DISPLAY_IMAGES
);
496 nsStyleUtil::ObjectPropsMightCauseOverflow(StylePosition())
498 : DisplayListClipState::ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT
;
500 DisplayListClipState::AutoClipContainingBlockDescendantsToContentBox
clip(
501 aBuilder
, this, clipFlags
);
503 aLists
.Content()->AppendNewToTop
<nsDisplayCanvas
>(aBuilder
, this);
505 DisplaySelectionOverlay(aBuilder
, aLists
.Content(),
506 nsISelectionDisplay::DISPLAY_IMAGES
);
509 void nsHTMLCanvasFrame::AppendDirectlyOwnedAnonBoxes(
510 nsTArray
<OwnedAnonBox
>& aResult
) {
511 MOZ_ASSERT(mFrames
.FirstChild(), "Must have our canvas content anon box");
512 MOZ_ASSERT(!mFrames
.FirstChild()->GetNextSibling(),
513 "Must only have our canvas content anon box");
514 aResult
.AppendElement(OwnedAnonBox(mFrames
.FirstChild()));
517 void nsHTMLCanvasFrame::UnionChildOverflow(
518 mozilla::OverflowAreas
& aOverflowAreas
) {
519 // Our one child (the canvas content anon box) is unpainted and isn't relevant
520 // for child-overflow purposes. So we need to provide our own trivial impl to
521 // avoid receiving the child-considering impl that we would otherwise inherit.
525 a11y::AccType
nsHTMLCanvasFrame::AccessibleType() {
526 return a11y::eHTMLCanvasType
;
530 #ifdef DEBUG_FRAME_DUMP
531 nsresult
nsHTMLCanvasFrame::GetFrameName(nsAString
& aResult
) const {
532 return MakeFrameName(u
"HTMLCanvas"_ns
, aResult
);