2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "core/inspector/InspectorLayerTreeAgent.h"
36 #include "core/dom/Document.h"
37 #include "core/frame/LocalFrame.h"
38 #include "core/inspector/IdentifiersFactory.h"
39 #include "core/inspector/InspectorNodeIds.h"
40 #include "core/inspector/InspectorState.h"
41 #include "core/inspector/InstrumentingAgents.h"
42 #include "core/loader/DocumentLoader.h"
43 #include "core/page/Page.h"
44 #include "core/rendering/RenderView.h"
45 #include "core/rendering/RenderWidget.h"
46 #include "core/rendering/compositing/CompositedLayerMapping.h"
47 #include "core/rendering/compositing/RenderLayerCompositor.h"
48 #include "platform/geometry/IntRect.h"
49 #include "platform/graphics/CompositingReasons.h"
50 #include "platform/graphics/GraphicsContextRecorder.h"
51 #include "platform/image-encoders/skia/PNGImageEncoder.h"
52 #include "platform/transforms/TransformationMatrix.h"
53 #include "public/platform/WebFloatPoint.h"
54 #include "public/platform/WebLayer.h"
55 #include "wtf/text/Base64.h"
56 #include "wtf/text/StringBuilder.h"
60 unsigned InspectorLayerTreeAgent::s_lastSnapshotId;
62 inline String idForLayer(const GraphicsLayer* graphicsLayer)
64 return String::number(graphicsLayer->platformLayer()->id());
67 static PassRefPtr<TypeBuilder::LayerTree::ScrollRect> buildScrollRect(const blink::WebRect& rect, const TypeBuilder::LayerTree::ScrollRect::Type::Enum& type)
69 RefPtr<TypeBuilder::DOM::Rect> rectObject = TypeBuilder::DOM::Rect::create()
72 .setHeight(rect.height)
73 .setWidth(rect.width);
74 RefPtr<TypeBuilder::LayerTree::ScrollRect> scrollRectObject = TypeBuilder::LayerTree::ScrollRect::create()
75 .setRect(rectObject.release())
77 return scrollRectObject.release();
80 static PassRefPtr<TypeBuilder::Array<TypeBuilder::LayerTree::ScrollRect> > buildScrollRectsForLayer(GraphicsLayer* graphicsLayer)
82 RefPtr<TypeBuilder::Array<TypeBuilder::LayerTree::ScrollRect> > scrollRects = TypeBuilder::Array<TypeBuilder::LayerTree::ScrollRect>::create();
83 blink::WebLayer* webLayer = graphicsLayer->platformLayer();
84 for (size_t i = 0; i < webLayer->nonFastScrollableRegion().size(); ++i) {
85 scrollRects->addItem(buildScrollRect(webLayer->nonFastScrollableRegion()[i], TypeBuilder::LayerTree::ScrollRect::Type::RepaintsOnScroll));
87 for (size_t i = 0; i < webLayer->touchEventHandlerRegion().size(); ++i) {
88 scrollRects->addItem(buildScrollRect(webLayer->touchEventHandlerRegion()[i], TypeBuilder::LayerTree::ScrollRect::Type::TouchEventHandler));
90 if (webLayer->haveWheelEventHandlers()) {
91 blink::WebRect webRect(webLayer->position().x, webLayer->position().y, webLayer->bounds().width, webLayer->bounds().height);
92 scrollRects->addItem(buildScrollRect(webRect, TypeBuilder::LayerTree::ScrollRect::Type::WheelEventHandler));
94 return scrollRects->length() ? scrollRects.release() : nullptr;
97 static PassRefPtr<TypeBuilder::LayerTree::Layer> buildObjectForLayer(GraphicsLayer* graphicsLayer, int nodeId)
99 blink::WebLayer* webLayer = graphicsLayer->platformLayer();
100 RefPtr<TypeBuilder::LayerTree::Layer> layerObject = TypeBuilder::LayerTree::Layer::create()
101 .setLayerId(idForLayer(graphicsLayer))
102 .setOffsetX(webLayer->position().x)
103 .setOffsetY(webLayer->position().y)
104 .setWidth(webLayer->bounds().width)
105 .setHeight(webLayer->bounds().height)
106 .setPaintCount(graphicsLayer->paintCount());
109 layerObject->setBackendNodeId(nodeId);
111 GraphicsLayer* parent = graphicsLayer->parent();
113 parent = graphicsLayer->replicatedLayer();
115 layerObject->setParentLayerId(idForLayer(parent));
116 if (!graphicsLayer->contentsAreVisible())
117 layerObject->setInvisible(true);
118 const TransformationMatrix& transform = graphicsLayer->transform();
119 if (!transform.isIdentity()) {
120 TransformationMatrix::FloatMatrix4 flattenedMatrix;
121 transform.toColumnMajorFloatArray(flattenedMatrix);
122 RefPtr<TypeBuilder::Array<double> > transformArray = TypeBuilder::Array<double>::create();
123 for (size_t i = 0; i < WTF_ARRAY_LENGTH(flattenedMatrix); ++i)
124 transformArray->addItem(flattenedMatrix[i]);
125 layerObject->setTransform(transformArray);
126 const FloatPoint3D& transformOrigin = graphicsLayer->transformOrigin();
127 // FIXME: rename these to setTransformOrigin*
128 if (webLayer->bounds().width > 0)
129 layerObject->setAnchorX(transformOrigin.x() / webLayer->bounds().width);
131 layerObject->setAnchorX(0.0);
132 if (webLayer->bounds().height > 0)
133 layerObject->setAnchorY(transformOrigin.y() / webLayer->bounds().height);
135 layerObject->setAnchorY(0.0);
136 layerObject->setAnchorZ(transformOrigin.z());
138 RefPtr<TypeBuilder::Array<TypeBuilder::LayerTree::ScrollRect> > scrollRects = buildScrollRectsForLayer(graphicsLayer);
140 layerObject->setScrollRects(scrollRects.release());
144 InspectorLayerTreeAgent::InspectorLayerTreeAgent(Page* page)
145 : InspectorBaseAgent<InspectorLayerTreeAgent>("LayerTree")
151 InspectorLayerTreeAgent::~InspectorLayerTreeAgent()
155 void InspectorLayerTreeAgent::trace(Visitor* visitor)
157 visitor->trace(m_page);
158 InspectorBaseAgent::trace(visitor);
161 void InspectorLayerTreeAgent::setFrontend(InspectorFrontend* frontend)
163 m_frontend = frontend->layertree();
166 void InspectorLayerTreeAgent::clearFrontend()
172 void InspectorLayerTreeAgent::restore()
174 // We do not re-enable layer agent automatically after navigation. This is because
175 // it depends on DOMAgent and node ids in particular, so we let front-end request document
176 // and re-enable the agent manually after this.
179 void InspectorLayerTreeAgent::enable(ErrorString*)
181 m_instrumentingAgents->setInspectorLayerTreeAgent(this);
182 if (LocalFrame* frame = m_page->deprecatedLocalMainFrame()) {
183 Document* document = frame->document();
184 if (document && document->lifecycle().state() >= DocumentLifecycle::CompositingClean)
185 layerTreeDidChange();
189 void InspectorLayerTreeAgent::disable(ErrorString*)
191 m_instrumentingAgents->setInspectorLayerTreeAgent(0);
192 m_snapshotById.clear();
196 void InspectorLayerTreeAgent::layerTreeDidChange()
198 m_frontend->layerTreeDidChange(buildLayerTree());
201 void InspectorLayerTreeAgent::didPaint(RenderObject*, const GraphicsLayer* graphicsLayer, GraphicsContext*, const LayoutRect& rect)
203 // Should only happen for FrameView paints when compositing is off. Consider different instrumentation method for that.
207 RefPtr<TypeBuilder::DOM::Rect> domRect = TypeBuilder::DOM::Rect::create()
210 .setWidth(rect.width())
211 .setHeight(rect.height());
212 m_frontend->layerPainted(idForLayer(graphicsLayer), domRect.release());
215 PassRefPtr<TypeBuilder::Array<TypeBuilder::LayerTree::Layer> > InspectorLayerTreeAgent::buildLayerTree()
217 RenderLayerCompositor* compositor = renderLayerCompositor();
218 if (!compositor || !compositor->inCompositingMode())
221 LayerIdToNodeIdMap layerIdToNodeIdMap;
222 RefPtr<TypeBuilder::Array<TypeBuilder::LayerTree::Layer> > layers = TypeBuilder::Array<TypeBuilder::LayerTree::Layer>::create();
223 buildLayerIdToNodeIdMap(compositor->rootRenderLayer(), layerIdToNodeIdMap);
224 gatherGraphicsLayers(compositor->rootGraphicsLayer(), layerIdToNodeIdMap, layers);
225 return layers.release();
228 void InspectorLayerTreeAgent::buildLayerIdToNodeIdMap(RenderLayer* root, LayerIdToNodeIdMap& layerIdToNodeIdMap)
230 if (root->hasCompositedLayerMapping()) {
231 if (Node* node = root->renderer()->generatingNode()) {
232 GraphicsLayer* graphicsLayer = root->compositedLayerMapping()->childForSuperlayers();
233 layerIdToNodeIdMap.set(graphicsLayer->platformLayer()->id(), idForNode(node));
236 for (RenderLayer* child = root->firstChild(); child; child = child->nextSibling())
237 buildLayerIdToNodeIdMap(child, layerIdToNodeIdMap);
238 if (!root->renderer()->isRenderIFrame())
240 FrameView* childFrameView = toFrameView(toRenderWidget(root->renderer())->widget());
241 if (RenderView* childRenderView = childFrameView->renderView()) {
242 if (RenderLayerCompositor* childCompositor = childRenderView->compositor())
243 buildLayerIdToNodeIdMap(childCompositor->rootRenderLayer(), layerIdToNodeIdMap);
247 void InspectorLayerTreeAgent::gatherGraphicsLayers(GraphicsLayer* root, HashMap<int, int>& layerIdToNodeIdMap, RefPtr<TypeBuilder::Array<TypeBuilder::LayerTree::Layer> >& layers)
249 int layerId = root->platformLayer()->id();
250 if (m_pageOverlayLayerIds.find(layerId) != WTF::kNotFound)
252 layers->addItem(buildObjectForLayer(root, layerIdToNodeIdMap.get(layerId)));
253 if (GraphicsLayer* replica = root->replicaLayer())
254 gatherGraphicsLayers(replica, layerIdToNodeIdMap, layers);
255 for (size_t i = 0, size = root->children().size(); i < size; ++i)
256 gatherGraphicsLayers(root->children()[i], layerIdToNodeIdMap, layers);
259 int InspectorLayerTreeAgent::idForNode(Node* node)
261 return InspectorNodeIds::idForNode(node);
264 RenderLayerCompositor* InspectorLayerTreeAgent::renderLayerCompositor()
266 RenderView* renderView = m_page->deprecatedLocalMainFrame()->contentRenderer();
267 RenderLayerCompositor* compositor = renderView ? renderView->compositor() : 0;
271 static GraphicsLayer* findLayerById(GraphicsLayer* root, int layerId)
273 if (root->platformLayer()->id() == layerId)
275 if (root->replicaLayer()) {
276 if (GraphicsLayer* layer = findLayerById(root->replicaLayer(), layerId))
279 for (size_t i = 0, size = root->children().size(); i < size; ++i) {
280 if (GraphicsLayer* layer = findLayerById(root->children()[i], layerId))
286 GraphicsLayer* InspectorLayerTreeAgent::layerById(ErrorString* errorString, const String& layerId)
289 int id = layerId.toInt(&ok);
291 *errorString = "Invalid layer id";
294 RenderLayerCompositor* compositor = renderLayerCompositor();
296 *errorString = "Not in compositing mode";
300 GraphicsLayer* result = findLayerById(compositor->rootGraphicsLayer(), id);
302 *errorString = "No layer matching given id found";
306 void InspectorLayerTreeAgent::compositingReasons(ErrorString* errorString, const String& layerId, RefPtr<TypeBuilder::Array<String> >& reasonStrings)
308 const GraphicsLayer* graphicsLayer = layerById(errorString, layerId);
311 CompositingReasons reasonsBitmask = graphicsLayer->compositingReasons();
312 reasonStrings = TypeBuilder::Array<String>::create();
313 for (size_t i = 0; i < kNumberOfCompositingReasons; ++i) {
314 if (!(reasonsBitmask & kCompositingReasonStringMap[i].reason))
316 reasonStrings->addItem(kCompositingReasonStringMap[i].shortName);
318 reasonsBitmask &= ~kCompositingReasonStringMap[i].reason;
321 ASSERT(!reasonsBitmask);
324 void InspectorLayerTreeAgent::makeSnapshot(ErrorString* errorString, const String& layerId, String* snapshotId)
326 GraphicsLayer* layer = layerById(errorString, layerId);
330 GraphicsContextRecorder recorder;
331 IntSize size = expandedIntSize(layer->size());
332 GraphicsContext* context = recorder.record(size, layer->contentsOpaque());
333 layer->paint(*context, IntRect(IntPoint(0, 0), size));
334 RefPtr<GraphicsContextSnapshot> snapshot = recorder.stop();
335 *snapshotId = String::number(++s_lastSnapshotId);
336 bool newEntry = m_snapshotById.add(*snapshotId, snapshot).isNewEntry;
337 ASSERT_UNUSED(newEntry, newEntry);
340 void InspectorLayerTreeAgent::loadSnapshot(ErrorString* errorString, const String& data, String* snapshotId)
342 Vector<char> snapshotData;
343 if (!base64Decode(data, snapshotData)) {
344 *errorString = "Invalid base64 encoding";
347 RefPtr<GraphicsContextSnapshot> snapshot = GraphicsContextSnapshot::load(snapshotData.data(), snapshotData.size());
349 *errorString = "Invalida snapshot format";
352 *snapshotId = String::number(++s_lastSnapshotId);
353 bool newEntry = m_snapshotById.add(*snapshotId, snapshot).isNewEntry;
354 ASSERT_UNUSED(newEntry, newEntry);
357 void InspectorLayerTreeAgent::releaseSnapshot(ErrorString* errorString, const String& snapshotId)
359 SnapshotById::iterator it = m_snapshotById.find(snapshotId);
360 if (it == m_snapshotById.end()) {
361 *errorString = "Snapshot not found";
364 m_snapshotById.remove(it);
367 const GraphicsContextSnapshot* InspectorLayerTreeAgent::snapshotById(ErrorString* errorString, const String& snapshotId)
369 SnapshotById::iterator it = m_snapshotById.find(snapshotId);
370 if (it == m_snapshotById.end()) {
371 *errorString = "Snapshot not found";
374 return it->value.get();
377 void InspectorLayerTreeAgent::replaySnapshot(ErrorString* errorString, const String& snapshotId, const int* fromStep, const int* toStep, const double* scale, String* dataURL)
379 const GraphicsContextSnapshot* snapshot = snapshotById(errorString, snapshotId);
382 OwnPtr<Vector<char> > base64Data = snapshot->replay(fromStep ? *fromStep : 0, toStep ? *toStep : 0, scale ? *scale : 1.0);
384 *errorString = "Image encoding failed";
388 url.appendLiteral("data:image/png;base64,");
389 url.reserveCapacity(url.length() + base64Data->size());
390 url.append(base64Data->begin(), base64Data->size());
391 *dataURL = url.toString();
394 void InspectorLayerTreeAgent::profileSnapshot(ErrorString* errorString, const String& snapshotId, const int* minRepeatCount, const double* minDuration, RefPtr<TypeBuilder::Array<TypeBuilder::Array<double> > >& outTimings)
396 const GraphicsContextSnapshot* snapshot = snapshotById(errorString, snapshotId);
399 OwnPtr<GraphicsContextSnapshot::Timings> timings = snapshot->profile(minRepeatCount ? *minRepeatCount : 1, minDuration ? *minDuration : 0);
400 outTimings = TypeBuilder::Array<TypeBuilder::Array<double> >::create();
401 for (size_t i = 0; i < timings->size(); ++i) {
402 const Vector<double>& row = (*timings)[i];
403 RefPtr<TypeBuilder::Array<double> > outRow = TypeBuilder::Array<double>::create();
404 for (size_t j = 0; j < row.size(); ++j)
405 outRow->addItem(row[j]);
406 outTimings->addItem(outRow.release());
410 void InspectorLayerTreeAgent::snapshotCommandLog(ErrorString* errorString, const String& snapshotId, RefPtr<TypeBuilder::Array<JSONObject> >& commandLog)
412 const GraphicsContextSnapshot* snapshot = snapshotById(errorString, snapshotId);
415 commandLog = TypeBuilder::Array<JSONObject>::runtimeCast(snapshot->snapshotCommandLog());
418 void InspectorLayerTreeAgent::willAddPageOverlay(const GraphicsLayer* layer)
420 m_pageOverlayLayerIds.append(layer->platformLayer()->id());
423 void InspectorLayerTreeAgent::didRemovePageOverlay(const GraphicsLayer* layer)
425 size_t index = m_pageOverlayLayerIds.find(layer->platformLayer()->id());
426 if (index == WTF::kNotFound)
428 m_pageOverlayLayerIds.remove(index);