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/. */
6 
7 /* rendering object for the HTML <canvas> element */
8 
9 #include "nsHTMLCanvasFrame.h"
10 
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 "BasicLayers.h"
20 #include "mozilla/webgpu/CanvasContext.h"
21 #include "nsDisplayList.h"
22 #include "nsLayoutUtils.h"
23 #include "nsStyleUtil.h"
24 #include "ImageLayers.h"
25 #include "Layers.h"
26 #include "ActiveLayerTracker.h"
27 
28 #include <algorithm>
29 
30 using namespace mozilla;
31 using namespace mozilla::dom;
32 using namespace mozilla::layers;
33 using namespace mozilla::gfx;
34 
35 /* Helper for our nsIFrame::GetIntrinsicSize() impl. Takes the result of
36  * "GetCanvasSize()" as a parameter, which may help avoid redundant
37  * indirect calls to GetCanvasSize().
38  *
39  * @param aCanvasSizeInPx The canvas's size in CSS pixels, as returned
40  *                        by GetCanvasSize().
41  * @return The canvas's intrinsic size, as an IntrinsicSize object.
42  */
IntrinsicSizeFromCanvasSize(const nsIntSize & aCanvasSizeInPx)43 static IntrinsicSize IntrinsicSizeFromCanvasSize(
44     const nsIntSize& aCanvasSizeInPx) {
45   return IntrinsicSize(
46       nsPresContext::CSSPixelsToAppUnits(aCanvasSizeInPx.width),
47       nsPresContext::CSSPixelsToAppUnits(aCanvasSizeInPx.height));
48 }
49 
50 /* Helper for our nsIFrame::GetIntrinsicRatio() impl. Takes the result of
51  * "GetCanvasSize()" as a parameter, which may help avoid redundant
52  * indirect calls to GetCanvasSize().
53  *
54  * @return The canvas's intrinsic ratio.
55  */
IntrinsicRatioFromCanvasSize(const nsIntSize & aCanvasSizeInPx)56 static AspectRatio IntrinsicRatioFromCanvasSize(
57     const nsIntSize& aCanvasSizeInPx) {
58   return AspectRatio::FromSize(aCanvasSizeInPx.width, aCanvasSizeInPx.height);
59 }
60 
61 class nsDisplayCanvas final : public nsPaintedDisplayItem {
62  public:
nsDisplayCanvas(nsDisplayListBuilder * aBuilder,nsIFrame * aFrame)63   nsDisplayCanvas(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame)
64       : nsPaintedDisplayItem(aBuilder, aFrame) {
65     MOZ_COUNT_CTOR(nsDisplayCanvas);
66   }
67   MOZ_COUNTED_DTOR_OVERRIDE(nsDisplayCanvas)
68 
69   NS_DISPLAY_DECL_NAME("nsDisplayCanvas", TYPE_CANVAS)
70 
GetOpaqueRegion(nsDisplayListBuilder * aBuilder,bool * aSnap) const71   virtual nsRegion GetOpaqueRegion(nsDisplayListBuilder* aBuilder,
72                                    bool* aSnap) const override {
73     *aSnap = false;
74     nsHTMLCanvasFrame* f = static_cast<nsHTMLCanvasFrame*>(Frame());
75     HTMLCanvasElement* canvas = HTMLCanvasElement::FromNode(f->GetContent());
76     nsRegion result;
77     if (canvas->GetIsOpaque()) {
78       // OK, the entire region painted by the canvas is opaque. But what is
79       // that region? It's the canvas's "dest rect" (controlled by the
80       // object-fit/object-position CSS properties), clipped to the container's
81       // content box (which is what GetBounds() returns). So, we grab those
82       // rects and intersect them.
83       nsRect constraintRect = GetBounds(aBuilder, aSnap);
84 
85       // Need intrinsic size & ratio, for ComputeObjectDestRect:
86       nsIntSize canvasSize = f->GetCanvasSize();
87       IntrinsicSize intrinsicSize = IntrinsicSizeFromCanvasSize(canvasSize);
88       AspectRatio intrinsicRatio = IntrinsicRatioFromCanvasSize(canvasSize);
89 
90       const nsRect destRect = nsLayoutUtils::ComputeObjectDestRect(
91           constraintRect, intrinsicSize, intrinsicRatio, f->StylePosition());
92       return nsRegion(destRect.Intersect(constraintRect));
93     }
94     return result;
95   }
96 
GetBounds(nsDisplayListBuilder * aBuilder,bool * aSnap) const97   virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
98                            bool* aSnap) const override {
99     *aSnap = true;
100     nsHTMLCanvasFrame* f = static_cast<nsHTMLCanvasFrame*>(Frame());
101     return f->GetInnerArea() + ToReferenceFrame();
102   }
103 
BuildLayer(nsDisplayListBuilder * aBuilder,LayerManager * aManager,const ContainerLayerParameters & aContainerParameters)104   virtual already_AddRefed<Layer> BuildLayer(
105       nsDisplayListBuilder* aBuilder, LayerManager* aManager,
106       const ContainerLayerParameters& aContainerParameters) override {
107     return static_cast<nsHTMLCanvasFrame*>(mFrame)->BuildLayer(
108         aBuilder, aManager, this, aContainerParameters);
109   }
110 
CreateWebRenderCommands(mozilla::wr::DisplayListBuilder & aBuilder,wr::IpcResourceUpdateQueue & aResources,const StackingContextHelper & aSc,mozilla::layers::RenderRootStateManager * aManager,nsDisplayListBuilder * aDisplayListBuilder)111   virtual bool CreateWebRenderCommands(
112       mozilla::wr::DisplayListBuilder& aBuilder,
113       wr::IpcResourceUpdateQueue& aResources, const StackingContextHelper& aSc,
114       mozilla::layers::RenderRootStateManager* aManager,
115       nsDisplayListBuilder* aDisplayListBuilder) override {
116     HTMLCanvasElement* element =
117         static_cast<HTMLCanvasElement*>(mFrame->GetContent());
118     element->HandlePrintCallback(mFrame->PresContext());
119 
120     switch (element->GetCurrentContextType()) {
121       case CanvasContextType::Canvas2D:
122       case CanvasContextType::WebGL1:
123       case CanvasContextType::WebGL2: {
124         bool isRecycled;
125         RefPtr<WebRenderCanvasData> canvasData =
126             aManager->CommandBuilder()
127                 .CreateOrRecycleWebRenderUserData<WebRenderCanvasData>(
128                     this, &isRecycled);
129         nsHTMLCanvasFrame* canvasFrame =
130             static_cast<nsHTMLCanvasFrame*>(mFrame);
131         if (!canvasFrame->UpdateWebRenderCanvasData(aDisplayListBuilder,
132                                                     canvasData)) {
133           return true;
134         }
135         WebRenderCanvasRendererAsync* data = canvasData->GetCanvasRenderer();
136         MOZ_ASSERT(data);
137         data->UpdateCompositableClient();
138 
139         // Push IFrame for async image pipeline.
140         // XXX Remove this once partial display list update is supported.
141 
142         nsIntSize canvasSizeInPx = data->GetSize();
143         IntrinsicSize intrinsicSize =
144             IntrinsicSizeFromCanvasSize(canvasSizeInPx);
145         AspectRatio intrinsicRatio =
146             IntrinsicRatioFromCanvasSize(canvasSizeInPx);
147 
148         nsRect area =
149             mFrame->GetContentRectRelativeToSelf() + ToReferenceFrame();
150         nsRect dest = nsLayoutUtils::ComputeObjectDestRect(
151             area, intrinsicSize, intrinsicRatio, mFrame->StylePosition());
152 
153         LayoutDeviceRect bounds = LayoutDeviceRect::FromAppUnits(
154             dest, mFrame->PresContext()->AppUnitsPerDevPixel());
155 
156         // We don't push a stacking context for this async image pipeline here.
157         // Instead, we do it inside the iframe that hosts the image. As a
158         // result, a bunch of the calculations normally done as part of that
159         // stacking context need to be done manually and pushed over to the
160         // parent side, where it will be done when we build the display list for
161         // the iframe. That happens in WebRenderCompositableHolder.
162 
163         wr::LayoutRect r = wr::ToLayoutRect(bounds);
164         aBuilder.PushIFrame(r, !BackfaceIsHidden(), data->GetPipelineId().ref(),
165                             /*ignoreMissingPipelines*/ false);
166 
167         LayoutDeviceRect scBounds(LayoutDevicePoint(0, 0), bounds.Size());
168         wr::ImageRendering filter = wr::ToImageRendering(
169             nsLayoutUtils::GetSamplingFilterForFrame(mFrame));
170         wr::MixBlendMode mixBlendMode = wr::MixBlendMode::Normal;
171         aManager->WrBridge()->AddWebRenderParentCommand(
172             OpUpdateAsyncImagePipeline(data->GetPipelineId().value(), scBounds,
173                                        VideoInfo::Rotation::kDegree_0, filter,
174                                        mixBlendMode));
175         break;
176       }
177       case CanvasContextType::WebGPU: {
178         nsHTMLCanvasFrame* canvasFrame =
179             static_cast<nsHTMLCanvasFrame*>(mFrame);
180         HTMLCanvasElement* canvasElement =
181             static_cast<HTMLCanvasElement*>(canvasFrame->GetContent());
182         webgpu::CanvasContext* canvasContext =
183             canvasElement->GetWebGPUContext();
184 
185         if (!canvasContext) {
186           return true;
187         }
188 
189         bool isRecycled;
190         RefPtr<WebRenderLocalCanvasData> canvasData =
191             aManager->CommandBuilder()
192                 .CreateOrRecycleWebRenderUserData<WebRenderLocalCanvasData>(
193                     this, &isRecycled);
194         if (!canvasContext->UpdateWebRenderLocalCanvasData(canvasData)) {
195           return true;
196         }
197 
198         nsIntSize canvasSizeInPx = canvasFrame->GetCanvasSize();
199         IntrinsicSize intrinsicSize =
200             IntrinsicSizeFromCanvasSize(canvasSizeInPx);
201         AspectRatio intrinsicRatio =
202             IntrinsicRatioFromCanvasSize(canvasSizeInPx);
203         nsRect area =
204             mFrame->GetContentRectRelativeToSelf() + ToReferenceFrame();
205         nsRect dest = nsLayoutUtils::ComputeObjectDestRect(
206             area, intrinsicSize, intrinsicRatio, mFrame->StylePosition());
207         LayoutDeviceRect bounds = LayoutDeviceRect::FromAppUnits(
208             dest, mFrame->PresContext()->AppUnitsPerDevPixel());
209 
210         const RGBDescriptor rgbDesc(canvasSizeInPx, canvasData->mFormat, false);
211         const auto targetStride = ImageDataSerializer::GetRGBStride(rgbDesc);
212         const bool preferCompositorSurface = true;
213         const wr::ImageDescriptor imageDesc(
214             canvasSizeInPx, targetStride, canvasData->mFormat,
215             wr::OpacityType::HasAlphaChannel, preferCompositorSurface);
216 
217         wr::ImageKey imageKey;
218         auto imageKeyMaybe = canvasContext->GetImageKey();
219         // Check that the key exists, and its namespace matches the active
220         // bridge. It will mismatch if there was a GPU reset.
221         if (imageKeyMaybe &&
222             aManager->WrBridge()->GetNamespace() == imageKeyMaybe->mNamespace) {
223           imageKey = imageKeyMaybe.value();
224         } else {
225           imageKey = canvasContext->CreateImageKey(aManager);
226           aResources.AddPrivateExternalImage(canvasContext->mExternalImageId,
227                                              imageKey, imageDesc);
228         }
229 
230         mozilla::wr::ImageRendering rendering = wr::ToImageRendering(
231             nsLayoutUtils::GetSamplingFilterForFrame(mFrame));
232         aBuilder.PushImage(wr::ToLayoutRect(bounds), wr::ToLayoutRect(bounds),
233                            !BackfaceIsHidden(), rendering, imageKey);
234 
235         canvasData->mDescriptor = imageDesc;
236         canvasData->mImageKey = imageKey;
237         canvasData->RequestFrameReadback();
238         break;
239       }
240       case CanvasContextType::ImageBitmap: {
241         nsHTMLCanvasFrame* canvasFrame =
242             static_cast<nsHTMLCanvasFrame*>(mFrame);
243         nsIntSize canvasSizeInPx = canvasFrame->GetCanvasSize();
244         if (canvasSizeInPx.width <= 0 || canvasSizeInPx.height <= 0) {
245           return true;
246         }
247         bool isRecycled;
248         RefPtr<WebRenderCanvasData> canvasData =
249             aManager->CommandBuilder()
250                 .CreateOrRecycleWebRenderUserData<WebRenderCanvasData>(
251                     this, &isRecycled);
252         if (!canvasFrame->UpdateWebRenderCanvasData(aDisplayListBuilder,
253                                                     canvasData)) {
254           canvasData->ClearImageContainer();
255           return true;
256         }
257 
258         IntrinsicSize intrinsicSize =
259             IntrinsicSizeFromCanvasSize(canvasSizeInPx);
260         AspectRatio intrinsicRatio =
261             IntrinsicRatioFromCanvasSize(canvasSizeInPx);
262 
263         nsRect area =
264             mFrame->GetContentRectRelativeToSelf() + ToReferenceFrame();
265         nsRect dest = nsLayoutUtils::ComputeObjectDestRect(
266             area, intrinsicSize, intrinsicRatio, mFrame->StylePosition());
267 
268         LayoutDeviceRect bounds = LayoutDeviceRect::FromAppUnits(
269             dest, mFrame->PresContext()->AppUnitsPerDevPixel());
270 
271         aManager->CommandBuilder().PushImage(
272             this, canvasData->GetImageContainer(), aBuilder, aResources, aSc,
273             bounds, bounds);
274         break;
275       }
276       case CanvasContextType::NoContext:
277         break;
278       default:
279         MOZ_ASSERT_UNREACHABLE("unknown canvas context type");
280     }
281     return true;
282   }
283 
GetLayerState(nsDisplayListBuilder * aBuilder,LayerManager * aManager,const ContainerLayerParameters & aParameters)284   virtual LayerState GetLayerState(
285       nsDisplayListBuilder* aBuilder, LayerManager* aManager,
286       const ContainerLayerParameters& aParameters) override {
287     if (HTMLCanvasElement::FromNode(mFrame->GetContent())
288             ->ShouldForceInactiveLayer(aManager))
289       return LayerState::LAYER_INACTIVE;
290 
291     // If compositing is cheap, just do that
292     if (aManager->IsCompositingCheap() ||
293         ActiveLayerTracker::IsContentActive(mFrame))
294       return mozilla::LayerState::LAYER_ACTIVE;
295 
296     return LayerState::LAYER_INACTIVE;
297   }
298 
299   // FirstContentfulPaint is supposed to ignore "white" canvases.  We use
300   // MaybeModified (if GetContext() was called on the canvas) as a standin for
301   // "white"
IsContentful() const302   virtual bool IsContentful() const override {
303     nsHTMLCanvasFrame* f = static_cast<nsHTMLCanvasFrame*>(Frame());
304     HTMLCanvasElement* canvas = HTMLCanvasElement::FromNode(f->GetContent());
305     return canvas->MaybeModified();
306   }
307 
Paint(nsDisplayListBuilder * aBuilder,gfxContext * aCtx)308   virtual void Paint(nsDisplayListBuilder* aBuilder,
309                      gfxContext* aCtx) override {
310     // This currently uses BasicLayerManager to re-use the code for extracting
311     // the current CanvasRenderer/Image and generating DrawTarget rendering
312     // commands for it.
313     // Ideally we'll factor out that code and use it directly soon.
314     RefPtr<BasicLayerManager> layerManager =
315         new BasicLayerManager(BasicLayerManager::BLM_OFFSCREEN);
316 
317     layerManager->BeginTransactionWithTarget(aCtx);
318     RefPtr<Layer> layer =
319         BuildLayer(aBuilder, layerManager, ContainerLayerParameters());
320     if (!layer) {
321       layerManager->AbortTransaction();
322       return;
323     }
324 
325     layerManager->SetRoot(layer);
326     layerManager->EndEmptyTransaction();
327   }
328 };
329 
NS_NewHTMLCanvasFrame(PresShell * aPresShell,ComputedStyle * aStyle)330 nsIFrame* NS_NewHTMLCanvasFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
331   return new (aPresShell)
332       nsHTMLCanvasFrame(aStyle, aPresShell->GetPresContext());
333 }
334 
335 NS_QUERYFRAME_HEAD(nsHTMLCanvasFrame)
NS_QUERYFRAME_ENTRY(nsHTMLCanvasFrame)336   NS_QUERYFRAME_ENTRY(nsHTMLCanvasFrame)
337 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
338 
339 NS_IMPL_FRAMEARENA_HELPERS(nsHTMLCanvasFrame)
340 
341 void nsHTMLCanvasFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
342                              nsIFrame* aPrevInFlow) {
343   nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
344 
345   // We can fill in the canvas before the canvas frame is created, in
346   // which case we never get around to marking the content as active. Therefore,
347   // we mark it active here when we create the frame.
348   ActiveLayerTracker::NotifyContentChange(this);
349 }
350 
DestroyFrom(nsIFrame * aDestroyRoot,PostDestroyData & aPostDestroyData)351 void nsHTMLCanvasFrame::DestroyFrom(nsIFrame* aDestroyRoot,
352                                     PostDestroyData& aPostDestroyData) {
353   if (IsPrimaryFrame()) {
354     HTMLCanvasElement::FromNode(*mContent)->ResetPrintCallback();
355   }
356   nsContainerFrame::DestroyFrom(aDestroyRoot, aPostDestroyData);
357 }
358 
359 nsHTMLCanvasFrame::~nsHTMLCanvasFrame() = default;
360 
GetCanvasSize() const361 nsIntSize nsHTMLCanvasFrame::GetCanvasSize() const {
362   nsIntSize size(0, 0);
363   HTMLCanvasElement* canvas = HTMLCanvasElement::FromNodeOrNull(GetContent());
364   if (canvas) {
365     size = canvas->GetSize();
366     MOZ_ASSERT(size.width >= 0 && size.height >= 0,
367                "we should've required <canvas> width/height attrs to be "
368                "unsigned (non-negative) values");
369   } else {
370     MOZ_ASSERT_UNREACHABLE("couldn't get canvas size");
371   }
372 
373   return size;
374 }
375 
376 /* virtual */
GetMinISize(gfxContext * aRenderingContext)377 nscoord nsHTMLCanvasFrame::GetMinISize(gfxContext* aRenderingContext) {
378   // XXX The caller doesn't account for constraints of the height,
379   // min-height, and max-height properties.
380   bool vertical = GetWritingMode().IsVertical();
381   nscoord result;
382   if (StyleDisplay()->IsContainSize()) {
383     result = 0;
384   } else {
385     result = nsPresContext::CSSPixelsToAppUnits(
386         vertical ? GetCanvasSize().height : GetCanvasSize().width);
387   }
388   DISPLAY_MIN_INLINE_SIZE(this, result);
389   return result;
390 }
391 
392 /* virtual */
GetPrefISize(gfxContext * aRenderingContext)393 nscoord nsHTMLCanvasFrame::GetPrefISize(gfxContext* aRenderingContext) {
394   // XXX The caller doesn't account for constraints of the height,
395   // min-height, and max-height properties.
396   bool vertical = GetWritingMode().IsVertical();
397   nscoord result;
398   if (StyleDisplay()->IsContainSize()) {
399     result = 0;
400   } else {
401     result = nsPresContext::CSSPixelsToAppUnits(
402         vertical ? GetCanvasSize().height : GetCanvasSize().width);
403   }
404   DISPLAY_PREF_INLINE_SIZE(this, result);
405   return result;
406 }
407 
408 /* virtual */
GetIntrinsicSize()409 IntrinsicSize nsHTMLCanvasFrame::GetIntrinsicSize() {
410   if (StyleDisplay()->IsContainSize()) {
411     return IntrinsicSize(0, 0);
412   }
413   return IntrinsicSizeFromCanvasSize(GetCanvasSize());
414 }
415 
416 /* virtual */
GetIntrinsicRatio() const417 AspectRatio nsHTMLCanvasFrame::GetIntrinsicRatio() const {
418   if (StyleDisplay()->IsContainSize()) {
419     return AspectRatio();
420   }
421 
422   return IntrinsicRatioFromCanvasSize(GetCanvasSize());
423 }
424 
425 /* virtual */
ComputeSize(gfxContext * aRenderingContext,WritingMode aWM,const LogicalSize & aCBSize,nscoord aAvailableISize,const LogicalSize & aMargin,const LogicalSize & aBorderPadding,const StyleSizeOverrides & aSizeOverrides,ComputeSizeFlags aFlags)426 nsIFrame::SizeComputationResult nsHTMLCanvasFrame::ComputeSize(
427     gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
428     nscoord aAvailableISize, const LogicalSize& aMargin,
429     const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
430     ComputeSizeFlags aFlags) {
431   return {ComputeSizeWithIntrinsicDimensions(
432               aRenderingContext, aWM, GetIntrinsicSize(), GetAspectRatio(),
433               aCBSize, aMargin, aBorderPadding, aSizeOverrides, aFlags),
434           AspectRatioUsage::None};
435 }
436 
Reflow(nsPresContext * aPresContext,ReflowOutput & aMetrics,const ReflowInput & aReflowInput,nsReflowStatus & aStatus)437 void nsHTMLCanvasFrame::Reflow(nsPresContext* aPresContext,
438                                ReflowOutput& aMetrics,
439                                const ReflowInput& aReflowInput,
440                                nsReflowStatus& aStatus) {
441   MarkInReflow();
442   DO_GLOBAL_REFLOW_COUNT("nsHTMLCanvasFrame");
443   DISPLAY_REFLOW(aPresContext, this, aReflowInput, aMetrics, aStatus);
444   MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
445   NS_FRAME_TRACE(
446       NS_FRAME_TRACE_CALLS,
447       ("enter nsHTMLCanvasFrame::Reflow: availSize=%d,%d",
448        aReflowInput.AvailableWidth(), aReflowInput.AvailableHeight()));
449 
450   MOZ_ASSERT(mState & NS_FRAME_IN_REFLOW, "frame is not in reflow");
451 
452   WritingMode wm = aReflowInput.GetWritingMode();
453   LogicalSize finalSize = aReflowInput.ComputedSize();
454 
455   // stash this away so we can compute our inner area later
456   mBorderPadding = aReflowInput.ComputedLogicalBorderPadding(wm);
457 
458   finalSize.ISize(wm) += mBorderPadding.IStartEnd(wm);
459   finalSize.BSize(wm) += mBorderPadding.BStartEnd(wm);
460 
461   if (GetPrevInFlow()) {
462     nscoord y = GetContinuationOffset(&finalSize.ISize(wm));
463     finalSize.BSize(wm) -= y + mBorderPadding.BStart(wm);
464     finalSize.BSize(wm) = std::max(0, finalSize.BSize(wm));
465   }
466 
467   aMetrics.SetSize(wm, finalSize);
468   aMetrics.SetOverflowAreasToDesiredBounds();
469   FinishAndStoreOverflow(&aMetrics);
470 
471   // Reflow the single anon block child.
472   nsReflowStatus childStatus;
473   nsIFrame* childFrame = mFrames.FirstChild();
474   WritingMode childWM = childFrame->GetWritingMode();
475   LogicalSize availSize = aReflowInput.ComputedSize(childWM);
476   availSize.BSize(childWM) = NS_UNCONSTRAINEDSIZE;
477   NS_ASSERTION(!childFrame->GetNextSibling(), "HTML canvas should have 1 kid");
478   ReflowOutput childDesiredSize(aReflowInput.GetWritingMode());
479   ReflowInput childReflowInput(aPresContext, aReflowInput, childFrame,
480                                availSize);
481   ReflowChild(childFrame, aPresContext, childDesiredSize, childReflowInput, 0,
482               0, ReflowChildFlags::Default, childStatus, nullptr);
483   FinishReflowChild(childFrame, aPresContext, childDesiredSize,
484                     &childReflowInput, 0, 0, ReflowChildFlags::Default);
485 
486   NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS,
487                  ("exit nsHTMLCanvasFrame::Reflow: size=%d,%d",
488                   aMetrics.ISize(wm), aMetrics.BSize(wm)));
489   NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aMetrics);
490 }
491 
492 // FIXME taken from nsImageFrame, but then had splittable frame stuff
493 // removed.  That needs to be fixed.
494 // XXXdholbert As in nsImageFrame, this function's clients should probably
495 // just be calling GetContentRectRelativeToSelf().
GetInnerArea() const496 nsRect nsHTMLCanvasFrame::GetInnerArea() const {
497   nsMargin bp = mBorderPadding.GetPhysicalMargin(GetWritingMode());
498   nsRect r;
499   r.x = bp.left;
500   r.y = bp.top;
501   r.width = mRect.width - bp.left - bp.right;
502   r.height = mRect.height - bp.top - bp.bottom;
503   return r;
504 }
505 
BuildLayer(nsDisplayListBuilder * aBuilder,LayerManager * aManager,nsDisplayItem * aItem,const ContainerLayerParameters & aContainerParameters)506 already_AddRefed<Layer> nsHTMLCanvasFrame::BuildLayer(
507     nsDisplayListBuilder* aBuilder, LayerManager* aManager,
508     nsDisplayItem* aItem,
509     const ContainerLayerParameters& aContainerParameters) {
510   nsRect area = GetContentRectRelativeToSelf() + aItem->ToReferenceFrame();
511   HTMLCanvasElement* element = static_cast<HTMLCanvasElement*>(GetContent());
512   nsIntSize canvasSizeInPx = GetCanvasSize();
513 
514   nsPresContext* presContext = PresContext();
515   element->HandlePrintCallback(presContext);
516 
517   if (canvasSizeInPx.width <= 0 || canvasSizeInPx.height <= 0 || area.IsEmpty())
518     return nullptr;
519 
520   Layer* oldLayer =
521       aManager->GetLayerBuilder()
522           ? aManager->GetLayerBuilder()->GetLeafLayerFor(aBuilder, aItem)
523           : nullptr;
524   RefPtr<Layer> layer = element->GetCanvasLayer(aBuilder, oldLayer, aManager);
525   if (!layer) return nullptr;
526 
527   IntrinsicSize intrinsicSize = IntrinsicSizeFromCanvasSize(canvasSizeInPx);
528   AspectRatio intrinsicRatio = IntrinsicRatioFromCanvasSize(canvasSizeInPx);
529 
530   nsRect dest = nsLayoutUtils::ComputeObjectDestRect(
531       area, intrinsicSize, intrinsicRatio, StylePosition());
532 
533   gfxRect destGFXRect = presContext->AppUnitsToGfxUnits(dest);
534 
535   // Transform the canvas into the right place
536   gfxPoint p = destGFXRect.TopLeft() + aContainerParameters.mOffset;
537   Matrix transform = Matrix::Translation(p.x, p.y);
538   transform.PreScale(destGFXRect.Width() / canvasSizeInPx.width,
539                      destGFXRect.Height() / canvasSizeInPx.height);
540   layer->SetBaseTransform(gfx::Matrix4x4::From2D(transform));
541   if (layer->GetType() == layers::Layer::TYPE_CANVAS) {
542     RefPtr<CanvasLayer> canvasLayer = static_cast<CanvasLayer*>(layer.get());
543     canvasLayer->SetSamplingFilter(
544         nsLayoutUtils::GetSamplingFilterForFrame(this));
545     nsIntRect bounds;
546     bounds.SetRect(0, 0, canvasSizeInPx.width, canvasSizeInPx.height);
547     canvasLayer->SetBounds(bounds);
548   } else if (layer->GetType() == layers::Layer::TYPE_IMAGE) {
549     RefPtr<ImageLayer> imageLayer = static_cast<ImageLayer*>(layer.get());
550     imageLayer->SetSamplingFilter(
551         nsLayoutUtils::GetSamplingFilterForFrame(this));
552   }
553 
554   return layer.forget();
555 }
556 
UpdateWebRenderCanvasData(nsDisplayListBuilder * aBuilder,WebRenderCanvasData * aCanvasData)557 bool nsHTMLCanvasFrame::UpdateWebRenderCanvasData(
558     nsDisplayListBuilder* aBuilder, WebRenderCanvasData* aCanvasData) {
559   HTMLCanvasElement* element = static_cast<HTMLCanvasElement*>(GetContent());
560   return element->UpdateWebRenderCanvasData(aBuilder, aCanvasData);
561 }
562 
BuildDisplayList(nsDisplayListBuilder * aBuilder,const nsDisplayListSet & aLists)563 void nsHTMLCanvasFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
564                                          const nsDisplayListSet& aLists) {
565   if (!IsVisibleForPainting()) return;
566 
567   DisplayBorderBackgroundOutline(aBuilder, aLists);
568 
569   uint32_t clipFlags =
570       nsStyleUtil::ObjectPropsMightCauseOverflow(StylePosition())
571           ? 0
572           : DisplayListClipState::ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT;
573 
574   DisplayListClipState::AutoClipContainingBlockDescendantsToContentBox clip(
575       aBuilder, this, clipFlags);
576 
577   aLists.Content()->AppendNewToTop<nsDisplayCanvas>(aBuilder, this);
578 
579   DisplaySelectionOverlay(aBuilder, aLists.Content(),
580                           nsISelectionDisplay::DISPLAY_IMAGES);
581 }
582 
583 // get the offset into the content area of the image where aImg starts if it is
584 // a continuation. from nsImageFrame
GetContinuationOffset(nscoord * aWidth) const585 nscoord nsHTMLCanvasFrame::GetContinuationOffset(nscoord* aWidth) const {
586   nscoord offset = 0;
587   if (aWidth) {
588     *aWidth = 0;
589   }
590 
591   if (GetPrevInFlow()) {
592     for (nsIFrame* prevInFlow = GetPrevInFlow(); prevInFlow;
593          prevInFlow = prevInFlow->GetPrevInFlow()) {
594       nsRect rect = prevInFlow->GetRect();
595       if (aWidth) {
596         *aWidth = rect.width;
597       }
598       offset += rect.height;
599     }
600     offset -= mBorderPadding.GetPhysicalMargin(GetWritingMode()).top;
601     offset = std::max(0, offset);
602   }
603   return offset;
604 }
605 
AppendDirectlyOwnedAnonBoxes(nsTArray<OwnedAnonBox> & aResult)606 void nsHTMLCanvasFrame::AppendDirectlyOwnedAnonBoxes(
607     nsTArray<OwnedAnonBox>& aResult) {
608   MOZ_ASSERT(mFrames.FirstChild(), "Must have our canvas content anon box");
609   MOZ_ASSERT(!mFrames.FirstChild()->GetNextSibling(),
610              "Must only have our canvas content anon box");
611   aResult.AppendElement(OwnedAnonBox(mFrames.FirstChild()));
612 }
613 
614 #ifdef ACCESSIBILITY
AccessibleType()615 a11y::AccType nsHTMLCanvasFrame::AccessibleType() {
616   return a11y::eHTMLCanvasType;
617 }
618 #endif
619 
620 #ifdef DEBUG_FRAME_DUMP
GetFrameName(nsAString & aResult) const621 nsresult nsHTMLCanvasFrame::GetFrameName(nsAString& aResult) const {
622   return MakeFrameName(u"HTMLCanvas"_ns, aResult);
623 }
624 #endif
625