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/layer.h"
10 #include "cc/layers/nine_patch_layer.h"
11 #include "cc/layers/picture_layer.h"
12 #include "cc/layers/solid_color_layer.h"
13 #include "cc/layers/texture_layer.h"
14 #include "cc/trees/layer_tree_settings.h"
20 scoped_refptr
<Layer
> ParseTreeFromValue(base::Value
* val
,
21 ContentLayerClient
* content_client
) {
22 base::DictionaryValue
* dict
;
24 success
&= val
->GetAsDictionary(&dict
);
25 std::string layer_type
;
26 success
&= dict
->GetString("LayerType", &layer_type
);
27 base::ListValue
* list
;
28 success
&= dict
->GetList("Bounds", &list
);
30 success
&= list
->GetInteger(0, &width
);
31 success
&= list
->GetInteger(1, &height
);
32 success
&= dict
->GetList("Position", &list
);
33 double position_x
, position_y
;
34 success
&= list
->GetDouble(0, &position_x
);
35 success
&= list
->GetDouble(1, &position_y
);
38 success
&= dict
->GetBoolean("DrawsContent", &draws_content
);
40 LayerSettings layer_settings
;
42 scoped_refptr
<Layer
> new_layer
;
43 if (layer_type
== "SolidColorLayer") {
44 new_layer
= SolidColorLayer::Create(layer_settings
);
45 } else if (layer_type
== "NinePatchLayer") {
46 success
&= dict
->GetList("ImageAperture", &list
);
47 int aperture_x
, aperture_y
, aperture_width
, aperture_height
;
48 success
&= list
->GetInteger(0, &aperture_x
);
49 success
&= list
->GetInteger(1, &aperture_y
);
50 success
&= list
->GetInteger(2, &aperture_width
);
51 success
&= list
->GetInteger(3, &aperture_height
);
53 base::ListValue
* bounds
;
54 success
&= dict
->GetList("ImageBounds", &bounds
);
55 double image_width
, image_height
;
56 success
&= bounds
->GetDouble(0, &image_width
);
57 success
&= bounds
->GetDouble(1, &image_height
);
59 success
&= dict
->GetList("Border", &list
);
60 int border_x
, border_y
, border_width
, border_height
;
61 success
&= list
->GetInteger(0, &border_x
);
62 success
&= list
->GetInteger(1, &border_y
);
63 success
&= list
->GetInteger(2, &border_width
);
64 success
&= list
->GetInteger(3, &border_height
);
67 success
&= dict
->GetBoolean("FillCenter", &fill_center
);
69 scoped_refptr
<NinePatchLayer
> nine_patch_layer
=
70 NinePatchLayer::Create(layer_settings
);
73 bitmap
.allocN32Pixels(image_width
, image_height
);
74 bitmap
.setImmutable();
75 nine_patch_layer
->SetBitmap(bitmap
);
76 nine_patch_layer
->SetAperture(
77 gfx::Rect(aperture_x
, aperture_y
, aperture_width
, aperture_height
));
78 nine_patch_layer
->SetBorder(
79 gfx::Rect(border_x
, border_y
, border_width
, border_height
));
80 nine_patch_layer
->SetFillCenter(fill_center
);
82 new_layer
= nine_patch_layer
;
83 } else if (layer_type
== "TextureLayer") {
84 new_layer
= TextureLayer::CreateForMailbox(layer_settings
, NULL
);
85 } else if (layer_type
== "PictureLayer") {
86 new_layer
= PictureLayer::Create(layer_settings
, content_client
);
87 } else { // Type "Layer" or "unknown"
88 new_layer
= Layer::Create(layer_settings
);
90 new_layer
->SetPosition(gfx::PointF(position_x
, position_y
));
91 new_layer
->SetBounds(gfx::Size(width
, height
));
92 new_layer
->SetIsDrawable(draws_content
);
95 if (dict
->GetDouble("Opacity", &opacity
))
96 new_layer
->SetOpacity(opacity
);
99 if (dict
->GetBoolean("ContentsOpaque", &contents_opaque
))
100 new_layer
->SetContentsOpaque(contents_opaque
);
103 // TODO(wjmaclean) At some time in the future we may wish to test that a
104 // reconstructed layer tree contains the correct linkage for the scroll
105 // clip layer. This is complicated by the fact that the json output doesn't
106 // (currently) re-construct the tree with the same layer IDs as the original.
107 // But, since a clip layer is always an ancestor of the scrollable layer, we
108 // can just count the number of upwards hops to the clip layer and write that
109 // into the json file (with 0 hops implying no clip layer, i.e. not
110 // scrollable). Reconstructing the tree can then be accomplished by passing
111 // the parent pointer to this function and traversing the same number of
112 // ancestors to determine the pointer to the clip layer. The LayerTreesMatch()
113 // function should then check that both original and reconstructed layers
114 // have the same positioning with respect to their clip layers.
116 // For now, we can safely indicate a layer is scrollable by giving it a
117 // pointer to itself, something not normally allowed in a working tree.
119 // https://code.google.com/p/chromium/issues/detail?id=330622
121 if (dict
->GetBoolean("Scrollable", &scrollable
))
122 new_layer
->SetScrollClipLayerId(scrollable
? new_layer
->id()
123 : Layer::INVALID_ID
);
126 if (dict
->GetBoolean("WheelHandler", &wheel_handler
))
127 new_layer
->SetHaveWheelEventHandlers(wheel_handler
);
130 if (dict
->GetBoolean("ScrollHandler", &scroll_handler
))
131 new_layer
->SetHaveScrollEventHandlers(scroll_handler
);
134 if (dict
->GetBoolean("Is3DSorted", &is_3d_sorted
)) {
135 // A non-zero context ID will put the layer into a 3D sorting context
136 new_layer
->Set3dSortingContextId(is_3d_sorted
? 1 : 0);
139 if (dict
->HasKey("TouchRegion")) {
140 success
&= dict
->GetList("TouchRegion", &list
);
142 for (size_t i
= 0; i
< list
->GetSize(); ) {
143 int rect_x
, rect_y
, rect_width
, rect_height
;
144 success
&= list
->GetInteger(i
++, &rect_x
);
145 success
&= list
->GetInteger(i
++, &rect_y
);
146 success
&= list
->GetInteger(i
++, &rect_width
);
147 success
&= list
->GetInteger(i
++, &rect_height
);
148 touch_region
.Union(gfx::Rect(rect_x
, rect_y
, rect_width
, rect_height
));
150 new_layer
->SetTouchEventHandlerRegion(touch_region
);
153 if (dict
->HasKey("ScrollBlocksOn")) {
154 success
&= dict
->GetList("ScrollBlocksOn", &list
);
155 ScrollBlocksOn blocks
;
157 for (size_t i
= 0; i
< list
->GetSize(); i
++) {
158 success
&= list
->GetString(i
, &str
);
159 if (str
== "StartTouch")
160 blocks
|= SCROLL_BLOCKS_ON_START_TOUCH
;
161 else if (str
== "WheelEvent")
162 blocks
|= SCROLL_BLOCKS_ON_WHEEL_EVENT
;
163 else if (str
== "ScrollEvent")
164 blocks
|= SCROLL_BLOCKS_ON_SCROLL_EVENT
;
170 success
&= dict
->GetList("DrawTransform", &list
);
171 double transform
[16];
172 for (int i
= 0; i
< 16; ++i
)
173 success
&= list
->GetDouble(i
, &transform
[i
]);
175 gfx::Transform layer_transform
;
176 layer_transform
.matrix().setColMajord(transform
);
177 new_layer
->SetTransform(layer_transform
);
179 success
&= dict
->GetList("Children", &list
);
180 for (base::ListValue::const_iterator it
= list
->begin();
181 it
!= list
->end(); ++it
) {
182 new_layer
->AddChild(ParseTreeFromValue(*it
, content_client
));
193 scoped_refptr
<Layer
> ParseTreeFromJson(std::string json
,
194 ContentLayerClient
* content_client
) {
195 scoped_ptr
<base::Value
> val
= base::test::ParseJson(json
);
196 return ParseTreeFromValue(val
.get(), content_client
);