1/*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21
22#include "platform/graphics/GraphicsLayerDebugInfo.h"
23#include "public/platform/WebGraphicsLayerDebugInfo.h"
24#include "public/platform/WebVector.h"
25
26#include "wtf/text/CString.h"
27
28namespace blink {
29
30GraphicsLayerDebugInfo::GraphicsLayerDebugInfo()
31    : m_compositingReasons(CompositingReasonNone)
32{
33}
34
35GraphicsLayerDebugInfo::~GraphicsLayerDebugInfo() { }
36
37void GraphicsLayerDebugInfo::appendAsTraceFormat(WebString* out) const
38{
39    RefPtr<JSONObject> jsonObject = JSONObject::create();
40    appendLayoutRects(jsonObject.get());
41    appendCompositingReasons(jsonObject.get());
42    appendDebugName(jsonObject.get());
43    appendOwnerNodeId(jsonObject.get());
44    *out = jsonObject->toJSONString();
45}
46
47void GraphicsLayerDebugInfo::getAnnotatedInvalidationRects(WebVector<WebAnnotatedInvalidationRect>& result) const
48{
49    result.assign(m_invalidations.data(), m_invalidations.size());
50}
51
52GraphicsLayerDebugInfo* GraphicsLayerDebugInfo::clone() const
53{
54    GraphicsLayerDebugInfo* toReturn = new GraphicsLayerDebugInfo();
55    for (size_t i = 0; i < m_currentLayoutRects.size(); ++i)
56        toReturn->currentLayoutRects().append(m_currentLayoutRects[i]);
57    toReturn->setCompositingReasons(m_compositingReasons);
58    toReturn->setOwnerNodeId(m_ownerNodeId);
59    toReturn->m_invalidations = m_invalidations;
60    return toReturn;
61}
62
63void GraphicsLayerDebugInfo::appendLayoutRects(JSONObject* jsonObject) const
64{
65    RefPtr<JSONArray> jsonArray = JSONArray::create();
66    for (size_t i = 0; i < m_currentLayoutRects.size(); i++) {
67        const LayoutRect& rect = m_currentLayoutRects[i];
68        RefPtr<JSONObject> rectContainer = JSONObject::create();
69        RefPtr<JSONArray> rectArray = JSONArray::create();
70        rectArray->pushNumber(rect.x().toFloat());
71        rectArray->pushNumber(rect.y().toFloat());
72        rectArray->pushNumber(rect.maxX().toFloat());
73        rectArray->pushNumber(rect.maxY().toFloat());
74        rectContainer->setArray("geometry_rect", rectArray);
75        jsonArray->pushObject(rectContainer);
76    }
77    jsonObject->setArray("layout_rects", jsonArray);
78}
79
80void GraphicsLayerDebugInfo::appendCompositingReasons(JSONObject* jsonObject) const
81{
82    RefPtr<JSONArray> jsonArray = JSONArray::create();
83    for (size_t i = 0; i < kNumberOfCompositingReasons; ++i) {
84        if (!(m_compositingReasons & kCompositingReasonStringMap[i].reason))
85            continue;
86        jsonArray->pushString(kCompositingReasonStringMap[i].description);
87    }
88    jsonObject->setArray("compositing_reasons", jsonArray);
89}
90
91void GraphicsLayerDebugInfo::appendDebugName(JSONObject* jsonObject) const
92{
93    if (m_debugName.isEmpty())
94        return;
95
96    jsonObject->setString("layer_name", m_debugName);
97}
98
99void GraphicsLayerDebugInfo::appendOwnerNodeId(JSONObject* jsonObject) const
100{
101    if (!m_ownerNodeId)
102        return;
103
104    jsonObject->setNumber("owner_node", m_ownerNodeId);
105}
106
107void GraphicsLayerDebugInfo::appendAnnotatedInvalidateRect(const FloatRect& rect, WebInvalidationDebugAnnotations annotations)
108{
109    WebAnnotatedInvalidationRect annotatedRect = {
110        WebFloatRect(rect),
111        annotations
112    };
113    m_invalidations.append(annotatedRect);
114}
115
116void GraphicsLayerDebugInfo::clearAnnotatedInvalidateRects()
117{
118    m_invalidations.clear();
119}
120
121} // namespace blink
122