Bug 1379920 - Support canvas in layers free mode. r=kats
MozReview-Commit-ID: 42jOb3fzodb
--- a/dom/html/HTMLCanvasElement.cpp
+++ b/dom/html/HTMLCanvasElement.cpp
@@ -1,16 +1,17 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/HTMLCanvasElement.h"
+#include "gfxPrefs.h"
#include "ImageEncoder.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "Layers.h"
#include "MediaSegment.h"
#include "mozilla/Assertions.h"
#include "mozilla/Base64.h"
#include "mozilla/CheckedInt.h"
@@ -19,16 +20,17 @@
#include "mozilla/dom/File.h"
#include "mozilla/dom/HTMLCanvasElementBinding.h"
#include "mozilla/dom/MediaStreamTrack.h"
#include "mozilla/dom/MouseEvent.h"
#include "mozilla/dom/OffscreenCanvas.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/layers/AsyncCanvasRenderer.h"
+#include "mozilla/layers/WebRenderCanvasRenderer.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/Telemetry.h"
#include "nsAttrValueInlines.h"
#include "nsContentUtils.h"
#include "nsDisplayList.h"
#include "nsDOMJSUtils.h"
#include "nsIScriptSecurityManager.h"
@@ -1057,28 +1059,48 @@ HTMLCanvasElement::InvalidateCanvasConte
// We don't need to flush anything here; if there's no frame or if
// we plan to reframe we don't need to invalidate it anyway.
nsIFrame *frame = GetPrimaryFrame();
if (!frame)
return;
ActiveLayerTracker::NotifyContentChange(frame);
- Layer* layer = nullptr;
- if (damageRect) {
- nsIntSize size = GetWidthHeight();
- if (size.width != 0 && size.height != 0) {
- gfx::IntRect invalRect = gfx::IntRect::Truncate(*damageRect);
- layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS, &invalRect);
+ // When using layers-free WebRender, we cannot invalidate the layer (because there isn't one).
+ // Instead, we mark the CanvasRenderer dirty and scheduling an empty transaction
+ // which is effectively equivalent.
+ CanvasRenderer* renderer = nullptr;
+ if (gfxPrefs::WebRenderLayersFree() && frame->HasProperty(nsIFrame::WebRenderUserDataProperty())) {
+ nsIFrame::WebRenderUserDataTable* userDataTable =
+ frame->GetProperty(nsIFrame::WebRenderUserDataProperty());
+ RefPtr<WebRenderUserData> data;
+ userDataTable->Get(nsDisplayItem::TYPE_CANVAS, getter_AddRefs(data));
+ if (data && data->AsCanvasData()) {
+ renderer = data->AsCanvasData()->GetCanvasRenderer();
}
+ }
+
+ if (renderer) {
+ renderer->SetDirty();
+ frame->SchedulePaint(nsIFrame::PAINT_COMPOSITE_ONLY);
} else {
- layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS);
- }
- if (layer) {
- static_cast<CanvasLayer*>(layer)->Updated();
+ Layer* layer = nullptr;
+ if (damageRect) {
+ nsIntSize size = GetWidthHeight();
+ if (size.width != 0 && size.height != 0) {
+ gfx::IntRect invalRect = gfx::IntRect::Truncate(*damageRect);
+ layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS, &invalRect);
+ }
+ } else {
+ layer = frame->InvalidateLayer(nsDisplayItem::TYPE_CANVAS);
+ }
+
+ if (layer) {
+ static_cast<CanvasLayer*>(layer)->Updated();
+ }
}
/*
* Treat canvas invalidations as animation activity for JS. Frequently
* invalidating a canvas will feed into heuristics and cause JIT code to be
* kept around longer, for smoother animations.
*/
nsCOMPtr<nsIGlobalObject> global =
--- a/gfx/layers/wr/WebRenderLayerManager.h
+++ b/gfx/layers/wr/WebRenderLayerManager.h
@@ -169,31 +169,37 @@ public:
const APZTestData& GetAPZTestData() const
{ return mApzTestData; }
// Those are data that we kept between transactions. We used to cache some
// data in the layer. But in layers free mode, we don't have layer which
// means we need some other place to cached the data between transaction.
// We store the data in frame's property.
template<class T> already_AddRefed<T>
- CreateOrRecycleWebRenderUserData(nsDisplayItem* aItem)
+ CreateOrRecycleWebRenderUserData(nsDisplayItem* aItem, bool* aOutIsRecycled = nullptr)
{
MOZ_ASSERT(aItem);
nsIFrame* frame = aItem->Frame();
+ if (aOutIsRecycled) {
+ *aOutIsRecycled = true;
+ }
if (!frame->HasProperty(nsIFrame::WebRenderUserDataProperty())) {
frame->AddProperty(nsIFrame::WebRenderUserDataProperty(),
new nsIFrame::WebRenderUserDataTable());
}
nsIFrame::WebRenderUserDataTable* userDataTable =
frame->GetProperty(nsIFrame::WebRenderUserDataProperty());
RefPtr<WebRenderUserData>& data = userDataTable->GetOrInsert(aItem->GetPerFrameKey());
if (!data || (data->GetType() != T::Type())) {
data = new T(this);
+ if (aOutIsRecycled) {
+ *aOutIsRecycled = false;
+ }
}
MOZ_ASSERT(data);
MOZ_ASSERT(data->GetType() == T::Type());
if (T::Type() == WebRenderUserData::UserDataType::eCanvas) {
mLastCanvasDatas.PutEntry(data->AsCanvasData());
}
RefPtr<T> res = static_cast<T*>(data.get());
--- a/layout/generic/nsHTMLCanvasFrame.cpp
+++ b/layout/generic/nsHTMLCanvasFrame.cpp
@@ -5,16 +5,20 @@
/* rendering object for the HTML <canvas> element */
#include "nsHTMLCanvasFrame.h"
#include "nsGkAtoms.h"
#include "mozilla/Assertions.h"
#include "mozilla/dom/HTMLCanvasElement.h"
+#include "mozilla/layers/WebRenderBridgeChild.h"
+#include "mozilla/layers/WebRenderCanvasRenderer.h"
+#include "mozilla/layers/WebRenderLayer.h"
+#include "mozilla/layers/WebRenderLayerManager.h"
#include "nsDisplayList.h"
#include "nsLayoutUtils.h"
#include "nsStyleUtil.h"
#include "ImageLayers.h"
#include "Layers.h"
#include "ActiveLayerTracker.h"
#include <algorithm>
@@ -112,16 +116,96 @@ public:
virtual already_AddRefed<Layer> BuildLayer(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerLayerParameters& aContainerParameters) override
{
return static_cast<nsHTMLCanvasFrame*>(mFrame)->
BuildLayer(aBuilder, aManager, this, aContainerParameters);
}
+
+ virtual bool CreateWebRenderCommands(mozilla::wr::DisplayListBuilder& aBuilder,
+ const StackingContextHelper& aSc,
+ nsTArray<WebRenderParentCommand>& aParentCommands,
+ mozilla::layers::WebRenderLayerManager* aManager,
+ nsDisplayListBuilder* aDisplayListBuilder) override
+ {
+ HTMLCanvasElement* element = static_cast<HTMLCanvasElement*>(mFrame->GetContent());
+ switch(element->GetCurrentContextType()) {
+ case CanvasContextType::Canvas2D:
+ case CanvasContextType::WebGL1:
+ case CanvasContextType::WebGL2:
+ {
+ bool isRecycled;
+ RefPtr<WebRenderCanvasData> canvasData =
+ aManager->CreateOrRecycleWebRenderUserData<WebRenderCanvasData>(this, &isRecycled);
+ WebRenderCanvasRendererAsync* data =
+ static_cast<WebRenderCanvasRendererAsync*>(canvasData->GetCanvasRenderer());
+
+ if (isRecycled) {
+ static_cast<nsHTMLCanvasFrame*>(mFrame)->InitializeCanvasRenderer(aDisplayListBuilder, data);
+ }
+
+ data->UpdateCompositableClient();
+
+ // Push IFrame for async image pipeline.
+ // XXX Remove this once partial display list update is supported.
+
+ /* ScrollingLayersHelper scroller(this, aBuilder, aSc); */
+ nsIntSize canvasSizeInPx = data->GetSize();
+ IntrinsicSize intrinsicSize = IntrinsicSizeFromCanvasSize(canvasSizeInPx);
+ nsSize intrinsicRatio = IntrinsicRatioFromCanvasSize(canvasSizeInPx);
+
+ nsRect area = mFrame->GetContentRectRelativeToSelf() + ToReferenceFrame();
+ nsRect dest =
+ nsLayoutUtils::ComputeObjectDestRect(area, intrinsicSize, intrinsicRatio,
+ mFrame->StylePosition());
+
+ LayoutDeviceRect bounds = LayoutDeviceRect::FromAppUnits(
+ dest, mFrame->PresContext()->AppUnitsPerDevPixel());
+
+ // We don't push a stacking context for this async image pipeline here.
+ // Instead, we do it inside the iframe that hosts the image. As a result,
+ // a bunch of the calculations normally done as part of that stacking
+ // context need to be done manually and pushed over to the parent side,
+ // where it will be done when we build the display list for the iframe.
+ // That happens in WebRenderCompositableHolder.
+
+ wr::LayoutRect r = aSc.ToRelativeLayoutRect(bounds);
+ aBuilder.PushIFrame(r, data->GetPipelineId().ref());
+
+ gfx::Matrix4x4 scTransform;
+ if (data->NeedsYFlip()) {
+ scTransform = scTransform.PreTranslate(0, data->GetSize().height, 0).PreScale(1, -1, 1);
+ }
+
+ MaybeIntSize scaleToSize;
+ LayerRect scBounds(0, 0, bounds.width, bounds.height);
+ wr::ImageRendering filter = wr::ToImageRendering(nsLayoutUtils::GetSamplingFilterForFrame(mFrame));
+ wr::MixBlendMode mixBlendMode = wr::MixBlendMode::Normal;
+
+ aManager->WrBridge()->AddWebRenderParentCommand(OpUpdateAsyncImagePipeline(data->GetPipelineId().value(),
+ scBounds,
+ scTransform,
+ scaleToSize,
+ filter,
+ mixBlendMode));
+ break;
+ }
+ case CanvasContextType::ImageBitmap:
+ {
+ // TODO: Support ImageBitmap
+ break;
+ }
+ case CanvasContextType::NoContext:
+ return false;
+ }
+ return true;
+ }
+
virtual LayerState GetLayerState(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerLayerParameters& aParameters) override
{
if (HTMLCanvasElement::FromContent(mFrame->GetContent())->ShouldForceInactiveLayer(aManager))
return LAYER_INACTIVE;
// If compositing is cheap, just do that