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"
19 scoped_refptr
<Layer
> ParseTreeFromValue(base::Value
* val
,
20 ContentLayerClient
* content_client
) {
21 DictionaryValue
* dict
;
23 success
&= val
->GetAsDictionary(&dict
);
24 std::string layer_type
;
25 success
&= dict
->GetString("LayerType", &layer_type
);
27 success
&= dict
->GetList("Bounds", &list
);
29 success
&= list
->GetInteger(0, &width
);
30 success
&= list
->GetInteger(1, &height
);
31 success
&= dict
->GetList("Position", &list
);
32 double position_x
, position_y
;
33 success
&= list
->GetDouble(0, &position_x
);
34 success
&= list
->GetDouble(1, &position_y
);
37 success
&= dict
->GetBoolean("DrawsContent", &draws_content
);
39 scoped_refptr
<Layer
> new_layer
;
40 if (layer_type
== "SolidColorLayer") {
41 new_layer
= SolidColorLayer::Create();
42 } else if (layer_type
== "ContentLayer") {
43 new_layer
= ContentLayer::Create(content_client
);
44 } else if (layer_type
== "NinePatchLayer") {
45 success
&= dict
->GetList("ImageAperture", &list
);
46 int aperture_x
, aperture_y
, aperture_width
, aperture_height
;
47 success
&= list
->GetInteger(0, &aperture_x
);
48 success
&= list
->GetInteger(1, &aperture_y
);
49 success
&= list
->GetInteger(2, &aperture_width
);
50 success
&= list
->GetInteger(3, &aperture_height
);
52 success
&= dict
->GetList("ImageBounds", &list
);
53 int image_width
, image_height
;
54 success
&= list
->GetInteger(0, &image_width
);
55 success
&= list
->GetInteger(1, &image_height
);
57 scoped_refptr
<NinePatchLayer
> nine_patch_layer
= NinePatchLayer::Create();
60 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, image_width
, image_height
);
61 bitmap
.allocPixels(NULL
, NULL
);
62 nine_patch_layer
->SetBitmap(bitmap
,
63 gfx::Rect(aperture_x
, aperture_y
, aperture_width
, aperture_height
));
65 new_layer
= nine_patch_layer
;
66 } else if (layer_type
== "PictureLayer") {
67 new_layer
= PictureLayer::Create(content_client
);
68 } else { // Type "Layer" or "unknown"
69 new_layer
= Layer::Create();
71 new_layer
->SetAnchorPoint(gfx::Point());
72 new_layer
->SetPosition(gfx::PointF(position_x
, position_y
));
73 new_layer
->SetBounds(gfx::Size(width
, height
));
74 new_layer
->SetIsDrawable(draws_content
);
77 if (dict
->GetDouble("Opacity", &opacity
))
78 new_layer
->SetOpacity(opacity
);
80 success
&= dict
->GetList("DrawTransform", &list
);
82 for (int i
= 0; i
< 16; ++i
)
83 success
&= list
->GetDouble(i
, &transform
[i
]);
85 gfx::Transform layer_transform
;
86 layer_transform
.matrix().setColMajord(transform
);
87 new_layer
->SetTransform(layer_transform
);
89 success
&= dict
->GetList("Children", &list
);
90 for (ListValue::const_iterator it
= list
->begin();
91 it
!= list
->end(); ++it
) {
92 new_layer
->AddChild(ParseTreeFromValue(*it
, content_client
));
103 scoped_refptr
<Layer
> ParseTreeFromJson(std::string json
,
104 ContentLayerClient
* content_client
) {
105 scoped_ptr
<base::Value
> val
= base::test::ParseJson(json
);
106 return ParseTreeFromValue(val
.get(), content_client
);