1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/test/layer_tree_json_parser.h"
7 #include "base/test/values_test_util.h"
8 #include "base/values.h"
9 #include "cc/layers/content_layer.h"
10 #include "cc/layers/layer.h"
11 #include "cc/layers/nine_patch_layer.h"
12 #include "cc/layers/picture_layer.h"
13 #include "cc/layers/solid_color_layer.h"
14 #include "cc/layers/texture_layer.h"
15 #include "cc/trees/layer_tree_settings.h"
21 scoped_refptr
<Layer
> ParseTreeFromValue(base::Value
* val
,
22 ContentLayerClient
* content_client
) {
23 base::DictionaryValue
* dict
;
25 success
&= val
->GetAsDictionary(&dict
);
26 std::string layer_type
;
27 success
&= dict
->GetString("LayerType", &layer_type
);
28 base::ListValue
* list
;
29 success
&= dict
->GetList("Bounds", &list
);
31 success
&= list
->GetInteger(0, &width
);
32 success
&= list
->GetInteger(1, &height
);
33 success
&= dict
->GetList("Position", &list
);
34 double position_x
, position_y
;
35 success
&= list
->GetDouble(0, &position_x
);
36 success
&= list
->GetDouble(1, &position_y
);
39 success
&= dict
->GetBoolean("DrawsContent", &draws_content
);
41 LayerSettings layer_settings
;
43 scoped_refptr
<Layer
> new_layer
;
44 if (layer_type
== "SolidColorLayer") {
45 new_layer
= SolidColorLayer::Create(layer_settings
);
46 } else if (layer_type
== "ContentLayer") {
47 new_layer
= ContentLayer::Create(layer_settings
, content_client
);
48 } else if (layer_type
== "NinePatchLayer") {
49 success
&= dict
->GetList("ImageAperture", &list
);
50 int aperture_x
, aperture_y
, aperture_width
, aperture_height
;
51 success
&= list
->GetInteger(0, &aperture_x
);
52 success
&= list
->GetInteger(1, &aperture_y
);
53 success
&= list
->GetInteger(2, &aperture_width
);
54 success
&= list
->GetInteger(3, &aperture_height
);
56 base::ListValue
* bounds
;
57 success
&= dict
->GetList("ImageBounds", &bounds
);
58 double image_width
, image_height
;
59 success
&= bounds
->GetDouble(0, &image_width
);
60 success
&= bounds
->GetDouble(1, &image_height
);
62 success
&= dict
->GetList("Border", &list
);
63 int border_x
, border_y
, border_width
, border_height
;
64 success
&= list
->GetInteger(0, &border_x
);
65 success
&= list
->GetInteger(1, &border_y
);
66 success
&= list
->GetInteger(2, &border_width
);
67 success
&= list
->GetInteger(3, &border_height
);
70 success
&= dict
->GetBoolean("FillCenter", &fill_center
);
72 scoped_refptr
<NinePatchLayer
> nine_patch_layer
=
73 NinePatchLayer::Create(layer_settings
);
76 bitmap
.allocN32Pixels(image_width
, image_height
);
77 bitmap
.setImmutable();
78 nine_patch_layer
->SetBitmap(bitmap
);
79 nine_patch_layer
->SetAperture(
80 gfx::Rect(aperture_x
, aperture_y
, aperture_width
, aperture_height
));
81 nine_patch_layer
->SetBorder(
82 gfx::Rect(border_x
, border_y
, border_width
, border_height
));
83 nine_patch_layer
->SetFillCenter(fill_center
);
85 new_layer
= nine_patch_layer
;
86 } else if (layer_type
== "TextureLayer") {
87 new_layer
= TextureLayer::CreateForMailbox(layer_settings
, NULL
);
88 } else if (layer_type
== "PictureLayer") {
89 new_layer
= PictureLayer::Create(layer_settings
, content_client
);
90 } else { // Type "Layer" or "unknown"
91 new_layer
= Layer::Create(layer_settings
);
93 new_layer
->SetPosition(gfx::PointF(position_x
, position_y
));
94 new_layer
->SetBounds(gfx::Size(width
, height
));
95 new_layer
->SetIsDrawable(draws_content
);
98 if (dict
->GetDouble("Opacity", &opacity
))
99 new_layer
->SetOpacity(opacity
);
101 bool contents_opaque
;
102 if (dict
->GetBoolean("ContentsOpaque", &contents_opaque
))
103 new_layer
->SetContentsOpaque(contents_opaque
);
106 // TODO(wjmaclean) At some time in the future we may wish to test that a
107 // reconstructed layer tree contains the correct linkage for the scroll
108 // clip layer. This is complicated by the fact that the json output doesn't
109 // (currently) re-construct the tree with the same layer IDs as the original.
110 // But, since a clip layer is always an ancestor of the scrollable layer, we
111 // can just count the number of upwards hops to the clip layer and write that
112 // into the json file (with 0 hops implying no clip layer, i.e. not
113 // scrollable). Reconstructing the tree can then be accomplished by passing
114 // the parent pointer to this function and traversing the same number of
115 // ancestors to determine the pointer to the clip layer. The LayerTreesMatch()
116 // function should then check that both original and reconstructed layers
117 // have the same positioning with respect to their clip layers.
119 // For now, we can safely indicate a layer is scrollable by giving it a
120 // pointer to itself, something not normally allowed in a working tree.
122 // https://code.google.com/p/chromium/issues/detail?id=330622
124 if (dict
->GetBoolean("Scrollable", &scrollable
))
125 new_layer
->SetScrollClipLayerId(scrollable
? new_layer
->id()
126 : Layer::INVALID_ID
);
129 if (dict
->GetBoolean("WheelHandler", &wheel_handler
))
130 new_layer
->SetHaveWheelEventHandlers(wheel_handler
);
133 if (dict
->GetBoolean("ScrollHandler", &scroll_handler
))
134 new_layer
->SetHaveScrollEventHandlers(scroll_handler
);
137 if (dict
->GetBoolean("Is3DSorted", &is_3d_sorted
)) {
138 // A non-zero context ID will put the layer into a 3D sorting context
139 new_layer
->Set3dSortingContextId(is_3d_sorted
? 1 : 0);
142 if (dict
->HasKey("TouchRegion")) {
143 success
&= dict
->GetList("TouchRegion", &list
);
145 for (size_t i
= 0; i
< list
->GetSize(); ) {
146 int rect_x
, rect_y
, rect_width
, rect_height
;
147 success
&= list
->GetInteger(i
++, &rect_x
);
148 success
&= list
->GetInteger(i
++, &rect_y
);
149 success
&= list
->GetInteger(i
++, &rect_width
);
150 success
&= list
->GetInteger(i
++, &rect_height
);
151 touch_region
.Union(gfx::Rect(rect_x
, rect_y
, rect_width
, rect_height
));
153 new_layer
->SetTouchEventHandlerRegion(touch_region
);
156 if (dict
->HasKey("ScrollBlocksOn")) {
157 success
&= dict
->GetList("ScrollBlocksOn", &list
);
158 ScrollBlocksOn blocks
;
160 for (size_t i
= 0; i
< list
->GetSize(); i
++) {
161 success
&= list
->GetString(i
, &str
);
162 if (str
== "StartTouch")
163 blocks
|= SCROLL_BLOCKS_ON_START_TOUCH
;
164 else if (str
== "WheelEvent")
165 blocks
|= SCROLL_BLOCKS_ON_WHEEL_EVENT
;
166 else if (str
== "ScrollEvent")
167 blocks
|= SCROLL_BLOCKS_ON_SCROLL_EVENT
;
173 success
&= dict
->GetList("DrawTransform", &list
);
174 double transform
[16];
175 for (int i
= 0; i
< 16; ++i
)
176 success
&= list
->GetDouble(i
, &transform
[i
]);
178 gfx::Transform layer_transform
;
179 layer_transform
.matrix().setColMajord(transform
);
180 new_layer
->SetTransform(layer_transform
);
182 success
&= dict
->GetList("Children", &list
);
183 for (base::ListValue::const_iterator it
= list
->begin();
184 it
!= list
->end(); ++it
) {
185 new_layer
->AddChild(ParseTreeFromValue(*it
, content_client
));
196 scoped_refptr
<Layer
> ParseTreeFromJson(std::string json
,
197 ContentLayerClient
* content_client
) {
198 scoped_ptr
<base::Value
> val
= base::test::ParseJson(json
);
199 return ParseTreeFromValue(val
.get(), content_client
);