Files
tubestation/layout/generic/nsHTMLCanvasFrame.cpp
Ryan VanderMeulen c55b45acd3 Backed out 46 changesets (bug 1022612) for B2G mochitest permafails on a CLOSED TREE.
Backed out changeset 34b3014a3112 (bug 1022612)
Backed out changeset 6ae9316fd909 (bug 1022612)
Backed out changeset b8f3749c95eb (bug 1022612)
Backed out changeset caab10bf6ca3 (bug 1022612)
Backed out changeset 0c57c620c898 (bug 1022612)
Backed out changeset fac64141a00a (bug 1022612)
Backed out changeset bf0df1c9d68b (bug 1022612)
Backed out changeset b42054800020 (bug 1022612)
Backed out changeset 667793b21194 (bug 1022612)
Backed out changeset f14ada64fe1b (bug 1022612)
Backed out changeset 75b837686bdf (bug 1022612)
Backed out changeset 66de53183a22 (bug 1022612)
Backed out changeset 0ff86ced4d46 (bug 1022612)
Backed out changeset 18eecc5b1ef7 (bug 1022612)
Backed out changeset 2763c4878de5 (bug 1022612)
Backed out changeset b72413ecc385 (bug 1022612)
Backed out changeset b23f1081afb8 (bug 1022612)
Backed out changeset f7e2c6a72043 (bug 1022612)
Backed out changeset 959917c9027d (bug 1022612)
Backed out changeset 0268a46f4880 (bug 1022612)
Backed out changeset 3388856a80ad (bug 1022612)
Backed out changeset e4b17cf0f806 (bug 1022612)
Backed out changeset 2f4e9da0e4b6 (bug 1022612)
Backed out changeset 489f6a7c0c03 (bug 1022612)
Backed out changeset 8369d9ad7ad3 (bug 1022612)
Backed out changeset 0758d2a06002 (bug 1022612)
Backed out changeset f2ae9cb22edb (bug 1022612)
Backed out changeset 9c48c6ee5dc2 (bug 1022612)
Backed out changeset fe7134400f08 (bug 1022612)
Backed out changeset cc2c5397ca8b (bug 1022612)
Backed out changeset a3d1a3e8b39d (bug 1022612)
Backed out changeset 8974b74b0eb0 (bug 1022612)
Backed out changeset 75f7dbb5a2a6 (bug 1022612)
Backed out changeset 2aa04a071e60 (bug 1022612)
Backed out changeset f2ab1bcd4c39 (bug 1022612)
Backed out changeset da9152b6ea29 (bug 1022612)
Backed out changeset 58abf5b0e148 (bug 1022612)
Backed out changeset 797058a09ad2 (bug 1022612)
Backed out changeset ea3e99a92ff0 (bug 1022612)
Backed out changeset adc4a4a7aa73 (bug 1022612)
Backed out changeset 7b18dedd1505 (bug 1022612)
Backed out changeset 055dd1921e8e (bug 1022612)
Backed out changeset 42fa2c97e989 (bug 1022612)
Backed out changeset cd594236388f (bug 1022612)
Backed out changeset 9eadc5fee43d (bug 1022612)
Backed out changeset 5cc8d30ff7c9 (bug 1022612)
2014-07-17 11:24:47 -04:00

345 lines
11 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/* rendering object for the HTML <canvas> element */
#include "nsHTMLCanvasFrame.h"
#include "nsGkAtoms.h"
#include "mozilla/dom/HTMLCanvasElement.h"
#include "nsDisplayList.h"
#include "nsLayoutUtils.h"
#include "Layers.h"
#include "ActiveLayerTracker.h"
#include <algorithm>
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::layers;
class nsDisplayCanvas : public nsDisplayItem {
public:
nsDisplayCanvas(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame)
: nsDisplayItem(aBuilder, aFrame)
{
MOZ_COUNT_CTOR(nsDisplayCanvas);
}
#ifdef NS_BUILD_REFCNT_LOGGING
virtual ~nsDisplayCanvas() {
MOZ_COUNT_DTOR(nsDisplayCanvas);
}
#endif
NS_DISPLAY_DECL_NAME("nsDisplayCanvas", TYPE_CANVAS)
virtual nsRegion GetOpaqueRegion(nsDisplayListBuilder* aBuilder,
bool* aSnap) MOZ_OVERRIDE {
*aSnap = false;
nsIFrame* f = Frame();
HTMLCanvasElement *canvas =
HTMLCanvasElement::FromContent(f->GetContent());
nsRegion result;
if (canvas->GetIsOpaque()) {
result = GetBounds(aBuilder, aSnap);
}
return result;
}
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
bool* aSnap) MOZ_OVERRIDE {
*aSnap = true;
nsHTMLCanvasFrame* f = static_cast<nsHTMLCanvasFrame*>(Frame());
return f->GetInnerArea() + ToReferenceFrame();
}
virtual already_AddRefed<Layer> BuildLayer(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerLayerParameters& aContainerParameters) MOZ_OVERRIDE
{
return static_cast<nsHTMLCanvasFrame*>(mFrame)->
BuildLayer(aBuilder, aManager, this, aContainerParameters);
}
virtual LayerState GetLayerState(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerLayerParameters& aParameters) MOZ_OVERRIDE
{
if (HTMLCanvasElement::FromContent(mFrame->GetContent())->ShouldForceInactiveLayer(aManager))
return LAYER_INACTIVE;
// If compositing is cheap, just do that
if (aManager->IsCompositingCheap() ||
ActiveLayerTracker::IsContentActive(mFrame))
return mozilla::LAYER_ACTIVE;
return LAYER_INACTIVE;
}
};
nsIFrame*
NS_NewHTMLCanvasFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
{
return new (aPresShell) nsHTMLCanvasFrame(aContext);
}
NS_QUERYFRAME_HEAD(nsHTMLCanvasFrame)
NS_QUERYFRAME_ENTRY(nsHTMLCanvasFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
NS_IMPL_FRAMEARENA_HELPERS(nsHTMLCanvasFrame)
void
nsHTMLCanvasFrame::Init(nsIContent* aContent,
nsContainerFrame* aParent,
nsIFrame* aPrevInFlow)
{
nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
// We can fill in the canvas before the canvas frame is created, in
// which case we never get around to marking the content as active. Therefore,
// we mark it active here when we create the frame.
ActiveLayerTracker::NotifyContentChange(this);
}
nsHTMLCanvasFrame::~nsHTMLCanvasFrame()
{
}
nsIntSize
nsHTMLCanvasFrame::GetCanvasSize()
{
nsIntSize size(0,0);
HTMLCanvasElement *canvas =
HTMLCanvasElement::FromContentOrNull(GetContent());
if (canvas) {
size = canvas->GetSize();
} else {
NS_NOTREACHED("couldn't get canvas size");
}
return size;
}
/* virtual */ nscoord
nsHTMLCanvasFrame::GetMinWidth(nsRenderingContext *aRenderingContext)
{
// XXX The caller doesn't account for constraints of the height,
// min-height, and max-height properties.
nscoord result = nsPresContext::CSSPixelsToAppUnits(GetCanvasSize().width);
DISPLAY_MIN_WIDTH(this, result);
return result;
}
/* virtual */ nscoord
nsHTMLCanvasFrame::GetPrefWidth(nsRenderingContext *aRenderingContext)
{
// XXX The caller doesn't account for constraints of the height,
// min-height, and max-height properties.
nscoord result = nsPresContext::CSSPixelsToAppUnits(GetCanvasSize().width);
DISPLAY_PREF_WIDTH(this, result);
return result;
}
/* virtual */ nsSize
nsHTMLCanvasFrame::GetIntrinsicRatio()
{
nsIntSize size(GetCanvasSize());
return nsSize(nsPresContext::CSSPixelsToAppUnits(size.width),
nsPresContext::CSSPixelsToAppUnits(size.height));
}
/* virtual */ nsSize
nsHTMLCanvasFrame::ComputeSize(nsRenderingContext *aRenderingContext,
nsSize aCBSize, nscoord aAvailableWidth,
nsSize aMargin, nsSize aBorder, nsSize aPadding,
uint32_t aFlags)
{
nsIntSize size = GetCanvasSize();
IntrinsicSize intrinsicSize;
intrinsicSize.width.SetCoordValue(nsPresContext::CSSPixelsToAppUnits(size.width));
intrinsicSize.height.SetCoordValue(nsPresContext::CSSPixelsToAppUnits(size.height));
nsSize intrinsicRatio = GetIntrinsicRatio(); // won't actually be used
return nsLayoutUtils::ComputeSizeWithIntrinsicDimensions(
aRenderingContext, this,
intrinsicSize, intrinsicRatio, aCBSize,
aMargin, aBorder, aPadding);
}
void
nsHTMLCanvasFrame::Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aMetrics,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
DO_GLOBAL_REFLOW_COUNT("nsHTMLCanvasFrame");
DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS,
("enter nsHTMLCanvasFrame::Reflow: availSize=%d,%d",
aReflowState.AvailableWidth(), aReflowState.AvailableHeight()));
NS_PRECONDITION(mState & NS_FRAME_IN_REFLOW, "frame is not in reflow");
aStatus = NS_FRAME_COMPLETE;
aMetrics.Width() = aReflowState.ComputedWidth();
aMetrics.Height() = aReflowState.ComputedHeight();
// stash this away so we can compute our inner area later
mBorderPadding = aReflowState.ComputedPhysicalBorderPadding();
aMetrics.Width() += mBorderPadding.left + mBorderPadding.right;
aMetrics.Height() += mBorderPadding.top + mBorderPadding.bottom;
if (GetPrevInFlow()) {
nscoord y = GetContinuationOffset(&aMetrics.Width());
aMetrics.Height() -= y + mBorderPadding.top;
aMetrics.Height() = std::max(0, aMetrics.Height());
}
aMetrics.SetOverflowAreasToDesiredBounds();
FinishAndStoreOverflow(&aMetrics);
// Reflow the single anon block child.
nsReflowStatus childStatus;
nsSize availSize(aReflowState.ComputedWidth(), NS_UNCONSTRAINEDSIZE);
nsIFrame* childFrame = mFrames.FirstChild();
NS_ASSERTION(!childFrame->GetNextSibling(), "HTML canvas should have 1 kid");
nsHTMLReflowMetrics childDesiredSize(aReflowState.GetWritingMode(), aMetrics.mFlags);
nsHTMLReflowState childReflowState(aPresContext, aReflowState, childFrame,
availSize);
ReflowChild(childFrame, aPresContext, childDesiredSize, childReflowState,
0, 0, 0, childStatus, nullptr);
FinishReflowChild(childFrame, aPresContext, childDesiredSize,
&childReflowState, 0, 0, 0);
NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS,
("exit nsHTMLCanvasFrame::Reflow: size=%d,%d",
aMetrics.Width(), aMetrics.Height()));
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
}
// FIXME taken from nsImageFrame, but then had splittable frame stuff
// removed. That needs to be fixed.
nsRect
nsHTMLCanvasFrame::GetInnerArea() const
{
nsRect r;
r.x = mBorderPadding.left;
r.y = mBorderPadding.top;
r.width = mRect.width - mBorderPadding.left - mBorderPadding.right;
r.height = mRect.height - mBorderPadding.top - mBorderPadding.bottom;
return r;
}
already_AddRefed<Layer>
nsHTMLCanvasFrame::BuildLayer(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
nsDisplayItem* aItem,
const ContainerLayerParameters& aContainerParameters)
{
nsRect area = GetContentRect() - GetPosition() + aItem->ToReferenceFrame();
HTMLCanvasElement* element = static_cast<HTMLCanvasElement*>(GetContent());
nsIntSize canvasSize = GetCanvasSize();
nsPresContext* presContext = PresContext();
element->HandlePrintCallback(presContext->Type());
if (canvasSize.width <= 0 || canvasSize.height <= 0 || area.IsEmpty())
return nullptr;
CanvasLayer* oldLayer = static_cast<CanvasLayer*>
(aManager->GetLayerBuilder()->GetLeafLayerFor(aBuilder, aItem));
nsRefPtr<CanvasLayer> layer = element->GetCanvasLayer(aBuilder, oldLayer, aManager);
if (!layer)
return nullptr;
gfxRect r = gfxRect(presContext->AppUnitsToGfxUnits(area.x),
presContext->AppUnitsToGfxUnits(area.y),
presContext->AppUnitsToGfxUnits(area.width),
presContext->AppUnitsToGfxUnits(area.height));
// Transform the canvas into the right place
gfx::Matrix transform;
gfxPoint p = r.TopLeft() + aContainerParameters.mOffset;
transform.Translate(p.x, p.y);
transform.Scale(r.Width()/canvasSize.width, r.Height()/canvasSize.height);
layer->SetBaseTransform(gfx::Matrix4x4::From2D(transform));
layer->SetFilter(nsLayoutUtils::GetGraphicsFilterForFrame(this));
layer->SetVisibleRegion(nsIntRect(0, 0, canvasSize.width, canvasSize.height));
return layer.forget();
}
void
nsHTMLCanvasFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
if (!IsVisibleForPainting(aBuilder))
return;
DisplayBorderBackgroundOutline(aBuilder, aLists);
DisplayListClipState::AutoClipContainingBlockDescendantsToContentBox
clip(aBuilder, this, DisplayListClipState::ASSUME_DRAWING_RESTRICTED_TO_CONTENT_RECT);
aLists.Content()->AppendNewToTop(
new (aBuilder) nsDisplayCanvas(aBuilder, this));
DisplaySelectionOverlay(aBuilder, aLists.Content(),
nsISelectionDisplay::DISPLAY_IMAGES);
}
nsIAtom*
nsHTMLCanvasFrame::GetType() const
{
return nsGkAtoms::HTMLCanvasFrame;
}
// get the offset into the content area of the image where aImg starts if it is a continuation.
// from nsImageFrame
nscoord
nsHTMLCanvasFrame::GetContinuationOffset(nscoord* aWidth) const
{
nscoord offset = 0;
if (aWidth) {
*aWidth = 0;
}
if (GetPrevInFlow()) {
for (nsIFrame* prevInFlow = GetPrevInFlow() ; prevInFlow; prevInFlow = prevInFlow->GetPrevInFlow()) {
nsRect rect = prevInFlow->GetRect();
if (aWidth) {
*aWidth = rect.width;
}
offset += rect.height;
}
offset -= mBorderPadding.top;
offset = std::max(0, offset);
}
return offset;
}
#ifdef ACCESSIBILITY
a11y::AccType
nsHTMLCanvasFrame::AccessibleType()
{
return a11y::eHTMLCanvasType;
}
#endif
#ifdef DEBUG_FRAME_DUMP
nsresult
nsHTMLCanvasFrame::GetFrameName(nsAString& aResult) const
{
return MakeFrameName(NS_LITERAL_STRING("HTMLCanvas"), aResult);
}
#endif