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/content_layer.h"
11 #include "cc/nine_patch_layer.h"
12 #include "cc/solid_color_layer.h"
18 scoped_refptr
<Layer
> ParseTreeFromValue(base::Value
* val
,
19 ContentLayerClient
* content_client
) {
20 DictionaryValue
* dict
;
22 success
&= val
->GetAsDictionary(&dict
);
23 std::string layer_type
;
24 success
&= dict
->GetString("LayerType", &layer_type
);
26 success
&= dict
->GetList("Bounds", &list
);
28 success
&= list
->GetInteger(0, &width
);
29 success
&= list
->GetInteger(1, &height
);
30 success
&= dict
->GetList("Position", &list
);
31 double position_x
, position_y
;
32 success
&= list
->GetDouble(0, &position_x
);
33 success
&= list
->GetDouble(1, &position_y
);
36 success
&= dict
->GetBoolean("DrawsContent", &draws_content
);
38 scoped_refptr
<Layer
> new_layer
;
39 if (layer_type
== "SolidColorLayer") {
40 new_layer
= SolidColorLayer::create();
41 } else if (layer_type
== "ContentLayer") {
42 new_layer
= ContentLayer::create(content_client
);
43 } else if (layer_type
== "NinePatchLayer") {
44 success
&= dict
->GetList("ImageAperture", &list
);
45 int aperture_x
, aperture_y
, aperture_width
, aperture_height
;
46 success
&= list
->GetInteger(0, &aperture_x
);
47 success
&= list
->GetInteger(1, &aperture_y
);
48 success
&= list
->GetInteger(2, &aperture_width
);
49 success
&= list
->GetInteger(3, &aperture_height
);
51 success
&= dict
->GetList("ImageBounds", &list
);
52 int image_width
, image_height
;
53 success
&= list
->GetInteger(0, &image_width
);
54 success
&= list
->GetInteger(1, &image_height
);
56 scoped_refptr
<NinePatchLayer
> nine_patch_layer
= NinePatchLayer::create();
59 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, image_width
, image_height
);
60 bitmap
.allocPixels(NULL
, NULL
);
61 nine_patch_layer
->setBitmap(bitmap
,
62 gfx::Rect(aperture_x
, aperture_y
, aperture_width
, aperture_height
));
64 new_layer
= nine_patch_layer
;
65 } else { // Type "Layer" or "unknown"
66 new_layer
= Layer::create();
68 new_layer
->setAnchorPoint(gfx::Point());
69 new_layer
->setPosition(gfx::PointF(position_x
, position_y
));
70 new_layer
->setBounds(gfx::Size(width
, height
));
71 new_layer
->setIsDrawable(draws_content
);
74 if (dict
->GetDouble("Opacity", &opacity
))
75 new_layer
->setOpacity(opacity
);
77 success
&= dict
->GetList("DrawTransform", &list
);
79 for (int i
= 0; i
< 16; ++i
)
80 success
&= list
->GetDouble(i
, &transform
[i
]);
82 gfx::Transform gfxTransform
;
83 gfxTransform
.matrix().setColMajord(transform
);
84 new_layer
->setTransform(gfxTransform
);
86 success
&= dict
->GetList("Children", &list
);
87 for (ListValue::const_iterator it
= list
->begin();
88 it
!= list
->end(); ++it
) {
89 new_layer
->addChild(ParseTreeFromValue(*it
, content_client
));
100 scoped_refptr
<Layer
> ParseTreeFromJson(std::string json
,
101 ContentLayerClient
* content_client
) {
102 scoped_ptr
<base::Value
> val
= base::test::ParseJson(json
);
103 return ParseTreeFromValue(val
.get(), content_client
);