forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsHTMLCanvasFrame.cpp
351 lines (290 loc) · 11.1 KB
/
nsHTMLCanvasFrame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* -*- 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 "nsHTMLParts.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsGkAtoms.h"
#include "nsHTMLCanvasFrame.h"
#include "mozilla/dom/HTMLCanvasElement.h"
#include "nsDisplayList.h"
#include "nsLayoutUtils.h"
#include "Layers.h"
#include "nsTransform2D.h"
#include "gfxContext.h"
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) {
*aSnap = false;
nsIFrame* f = GetUnderlyingFrame();
HTMLCanvasElement *canvas =
HTMLCanvasElement::FromContent(f->GetContent());
nsRegion result;
if (canvas->GetIsOpaque()) {
result = GetBounds(aBuilder, aSnap);
}
return result;
}
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) {
*aSnap = true;
nsHTMLCanvasFrame* f = static_cast<nsHTMLCanvasFrame*>(GetUnderlyingFrame());
return f->GetInnerArea() + ToReferenceFrame();
}
virtual already_AddRefed<Layer> BuildLayer(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const ContainerParameters& aContainerParameters)
{
return static_cast<nsHTMLCanvasFrame*>(mFrame)->
BuildLayer(aBuilder, aManager, this, aContainerParameters);
}
virtual LayerState GetLayerState(nsDisplayListBuilder* aBuilder,
LayerManager* aManager,
const FrameLayerBuilder::ContainerParameters& aParameters)
{
if (HTMLCanvasElement::FromContent(mFrame->GetContent())->ShouldForceInactiveLayer(aManager))
return LAYER_INACTIVE;
// If compositing is cheap, just do that
if (aManager->IsCompositingCheap())
return mozilla::LAYER_ACTIVE;
return mFrame->AreLayersMarkedActive() ? LAYER_ACTIVE : LAYER_INACTIVE;
}
};
nsIFrame*
NS_NewHTMLCanvasFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
{
return new (aPresShell) nsHTMLCanvasFrame(aContext);
}
NS_IMPL_FRAMEARENA_HELPERS(nsHTMLCanvasFrame)
NS_IMETHODIMP
nsHTMLCanvasFrame::Init(nsIContent* aContent,
nsIFrame* aParent,
nsIFrame* aPrevInFlow)
{
nsresult rv = nsSplittableFrame::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 layer active. Therefore,
// we mark it active here when we create the frame.
MarkLayersActive(nsChangeHint(0));
return rv;
}
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);
}
NS_IMETHODIMP
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.mComputedBorderPadding;
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 = NS_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(aMetrics.mFlags);
nsHTMLReflowState childReflowState(aPresContext, aReflowState, childFrame,
availSize);
ReflowChild(childFrame, aPresContext, childDesiredSize, childReflowState,
0, 0, 0, childStatus, nullptr);
FinishReflowChild(childFrame, aPresContext, &childReflowState,
childDesiredSize, 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);
return NS_OK;
}
// 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 ContainerParameters& 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
gfxMatrix transform;
transform.Translate(r.TopLeft() + aContainerParameters.mOffset);
transform.Scale(r.Width()/canvasSize.width, r.Height()/canvasSize.height);
layer->SetBaseTransform(gfx3DMatrix::From2D(transform));
layer->SetFilter(nsLayoutUtils::GetGraphicsFilterForFrame(this));
layer->SetVisibleRegion(nsIntRect(0, 0, canvasSize.width, canvasSize.height));
return layer.forget();
}
NS_IMETHODIMP
nsHTMLCanvasFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
if (!IsVisibleForPainting(aBuilder))
return NS_OK;
nsresult rv = DisplayBorderBackgroundOutline(aBuilder, aLists);
NS_ENSURE_SUCCESS(rv, rv);
nsDisplayList replacedContent;
rv = replacedContent.AppendNewToTop(
new (aBuilder) nsDisplayCanvas(aBuilder, this));
NS_ENSURE_SUCCESS(rv, rv);
rv = DisplaySelectionOverlay(aBuilder, &replacedContent,
nsISelectionDisplay::DISPLAY_IMAGES);
NS_ENSURE_SUCCESS(rv, rv);
WrapReplacedContentForBorderRadius(aBuilder, &replacedContent, aLists);
return NS_OK;
}
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 = NS_MAX(0, offset);
}
return offset;
}
#ifdef ACCESSIBILITY
a11y::AccType
nsHTMLCanvasFrame::AccessibleType()
{
return a11y::eHTMLCanvasType;
}
#endif
#ifdef DEBUG
NS_IMETHODIMP
nsHTMLCanvasFrame::GetFrameName(nsAString& aResult) const
{
return MakeFrameName(NS_LITERAL_STRING("HTMLCanvas"), aResult);
}
#endif