Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / GraphicsLayerDebugInfo.cpp
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 #include "platform/graphics/GraphicsLayerDebugInfo.h"
22
23 #include "public/platform/WebGraphicsLayerDebugInfo.h"
24 #include "public/platform/WebVector.h"
25 #include "wtf/text/CString.h"
26
27 namespace blink {
28
29 GraphicsLayerDebugInfo::GraphicsLayerDebugInfo()
30     : m_compositingReasons(CompositingReasonNone)
31     , m_ownerNodeId(0)
32 {
33 }
34
35 GraphicsLayerDebugInfo::~GraphicsLayerDebugInfo() { }
36
37 void GraphicsLayerDebugInfo::appendAsTraceFormat(WebString* out) const
38 {
39     RefPtr<JSONObject> jsonObject = JSONObject::create();
40     appendLayoutRects(jsonObject.get());
41     appendAnnotatedInvalidateRects(jsonObject.get());
42     appendCompositingReasons(jsonObject.get());
43     appendDebugName(jsonObject.get());
44     appendOwnerNodeId(jsonObject.get());
45     *out = jsonObject->toJSONString();
46 }
47
48 GraphicsLayerDebugInfo* GraphicsLayerDebugInfo::clone() const
49 {
50     GraphicsLayerDebugInfo* toReturn = new GraphicsLayerDebugInfo();
51     for (size_t i = 0; i < m_currentLayoutRects.size(); ++i)
52         toReturn->currentLayoutRects().append(m_currentLayoutRects[i]);
53     toReturn->setCompositingReasons(m_compositingReasons);
54     toReturn->setOwnerNodeId(m_ownerNodeId);
55     toReturn->m_invalidations = m_invalidations;
56     toReturn->m_previousInvalidations = m_previousInvalidations;
57     return toReturn;
58 }
59
60 void GraphicsLayerDebugInfo::appendLayoutRects(JSONObject* jsonObject) const
61 {
62     RefPtr<JSONArray> jsonArray = JSONArray::create();
63     for (size_t i = 0; i < m_currentLayoutRects.size(); i++) {
64         const LayoutRect& rect = m_currentLayoutRects[i];
65         RefPtr<JSONObject> rectContainer = JSONObject::create();
66         RefPtr<JSONArray> rectArray = JSONArray::create();
67         rectArray->pushNumber(rect.x().toFloat());
68         rectArray->pushNumber(rect.y().toFloat());
69         rectArray->pushNumber(rect.maxX().toFloat());
70         rectArray->pushNumber(rect.maxY().toFloat());
71         rectContainer->setArray("geometry_rect", rectArray);
72         jsonArray->pushObject(rectContainer);
73     }
74     jsonObject->setArray("layout_rects", jsonArray);
75 }
76
77 void GraphicsLayerDebugInfo::appendAnnotatedInvalidateRects(JSONObject* jsonObject) const
78 {
79     RefPtr<JSONArray> jsonArray = JSONArray::create();
80     for (const auto& annotatedRect : m_previousInvalidations) {
81         RefPtr<JSONObject> rectContainer = JSONObject::create();
82         RefPtr<JSONArray> rectArray = JSONArray::create();
83         const FloatRect& rect = annotatedRect.rect;
84         rectArray->pushNumber(rect.x());
85         rectArray->pushNumber(rect.y());
86         rectArray->pushNumber(rect.width());
87         rectArray->pushNumber(rect.height());
88         rectContainer->setArray("geometry_rect", rectArray);
89         rectContainer->setString("reason", paintInvalidationReasonToString(annotatedRect.reason));
90         jsonArray->pushObject(rectContainer);
91     }
92     jsonObject->setArray("annotated_invalidation_rects", jsonArray);
93 }
94
95 void GraphicsLayerDebugInfo::appendCompositingReasons(JSONObject* jsonObject) const
96 {
97     RefPtr<JSONArray> jsonArray = JSONArray::create();
98     for (size_t i = 0; i < kNumberOfCompositingReasons; ++i) {
99         if (!(m_compositingReasons & kCompositingReasonStringMap[i].reason))
100             continue;
101         jsonArray->pushString(kCompositingReasonStringMap[i].description);
102     }
103     jsonObject->setArray("compositing_reasons", jsonArray);
104 }
105
106 void GraphicsLayerDebugInfo::appendDebugName(JSONObject* jsonObject) const
107 {
108     if (m_debugName.isEmpty())
109         return;
110
111     jsonObject->setString("layer_name", m_debugName);
112 }
113
114 void GraphicsLayerDebugInfo::appendOwnerNodeId(JSONObject* jsonObject) const
115 {
116     if (!m_ownerNodeId)
117         return;
118
119     jsonObject->setNumber("owner_node", m_ownerNodeId);
120 }
121
122 void GraphicsLayerDebugInfo::appendAnnotatedInvalidateRect(const FloatRect& rect, PaintInvalidationReason invalidationReason)
123 {
124     AnnotatedInvalidationRect annotatedRect = {
125         rect,
126         invalidationReason
127     };
128     m_invalidations.append(annotatedRect);
129 }
130
131 void GraphicsLayerDebugInfo::clearAnnotatedInvalidateRects()
132 {
133     m_previousInvalidations.clear();
134     m_previousInvalidations.swap(m_invalidations);
135 }
136
137 } // namespace blink