1/*
2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "RenderHTMLCanvas.h"
28
29#include "CanvasRenderingContext.h"
30#include "Document.h"
31#include "Frame.h"
32#include "FrameView.h"
33#include "GraphicsContext.h"
34#include "HTMLCanvasElement.h"
35#include "HTMLNames.h"
36#include "ImageQualityController.h"
37#include "Page.h"
38#include "PaintInfo.h"
39#include "RenderView.h"
40#include <wtf/IsoMallocInlines.h>
41
42namespace WebCore {
43
44using namespace HTMLNames;
45
46WTF_MAKE_ISO_ALLOCATED_IMPL(RenderHTMLCanvas);
47
48RenderHTMLCanvas::RenderHTMLCanvas(HTMLCanvasElement& element, RenderStyle&& style)
49 : RenderReplaced(element, WTFMove(style), element.size())
50{
51 // Actual size is not known yet, report the default intrinsic size.
52 view().frameView().incrementVisuallyNonEmptyPixelCount(roundedIntSize(intrinsicSize()));
53}
54
55HTMLCanvasElement& RenderHTMLCanvas::canvasElement() const
56{
57 return downcast<HTMLCanvasElement>(nodeForNonAnonymous());
58}
59
60bool RenderHTMLCanvas::requiresLayer() const
61{
62 if (RenderReplaced::requiresLayer())
63 return true;
64
65 if (CanvasRenderingContext* context = canvasElement().renderingContext())
66 return context->isAccelerated();
67
68 return false;
69}
70
71void RenderHTMLCanvas::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
72{
73 GraphicsContext& context = paintInfo.context();
74
75 LayoutRect contentBoxRect = this->contentBoxRect();
76 contentBoxRect.moveBy(paintOffset);
77 LayoutRect replacedContentRect = this->replacedContentRect();
78 replacedContentRect.moveBy(paintOffset);
79
80 // Not allowed to overflow the content box.
81 bool clip = !contentBoxRect.contains(replacedContentRect);
82 GraphicsContextStateSaver stateSaver(paintInfo.context(), clip);
83 if (clip)
84 paintInfo.context().clip(snappedIntRect(contentBoxRect));
85
86 if (paintInfo.phase == PaintPhase::Foreground)
87 page().addRelevantRepaintedObject(this, intersection(replacedContentRect, contentBoxRect));
88
89 InterpolationQualityMaintainer interpolationMaintainer(context, ImageQualityController::interpolationQualityFromStyle(style()));
90 canvasElement().paint(context, replacedContentRect);
91}
92
93void RenderHTMLCanvas::canvasSizeChanged()
94{
95 IntSize canvasSize = canvasElement().size();
96 LayoutSize zoomedSize(canvasSize.width() * style().effectiveZoom(), canvasSize.height() * style().effectiveZoom());
97
98 if (zoomedSize == intrinsicSize())
99 return;
100
101 setIntrinsicSize(zoomedSize);
102
103 if (!parent())
104 return;
105 setNeedsLayoutIfNeededAfterIntrinsicSizeChange();
106}
107
108} // namespace WebCore
109