blob: 1c47e1ea87b56bf2fdac3c9e4adae4169a817faa [file] [log] [blame]
[email protected]f8fef2bd2013-02-04 23:39:221// 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.
4
5#include "cc/test/layer_tree_json_parser.h"
6
7#include "base/test/values_test_util.h"
8#include "base/values.h"
[email protected]cc3cfaa2013-03-18 09:05:529#include "cc/layers/content_layer.h"
10#include "cc/layers/layer.h"
11#include "cc/layers/nine_patch_layer.h"
[email protected]a49d0f8a2013-05-09 23:26:1912#include "cc/layers/picture_layer.h"
[email protected]cc3cfaa2013-03-18 09:05:5213#include "cc/layers/solid_color_layer.h"
[email protected]f8fef2bd2013-02-04 23:39:2214
15namespace cc {
16
17namespace {
18
19scoped_refptr<Layer> ParseTreeFromValue(base::Value* val,
20 ContentLayerClient* content_client) {
21 DictionaryValue* dict;
22 bool success = true;
23 success &= val->GetAsDictionary(&dict);
24 std::string layer_type;
25 success &= dict->GetString("LayerType", &layer_type);
26 ListValue* list;
27 success &= dict->GetList("Bounds", &list);
28 int width, height;
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);
35
36 bool draws_content;
37 success &= dict->GetBoolean("DrawsContent", &draws_content);
38
39 scoped_refptr<Layer> new_layer;
40 if (layer_type == "SolidColorLayer") {
[email protected]67be9b1d2013-03-08 02:27:4541 new_layer = SolidColorLayer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2242 } else if (layer_type == "ContentLayer") {
[email protected]7aba6662013-03-12 10:17:3443 new_layer = ContentLayer::Create(content_client);
[email protected]f8fef2bd2013-02-04 23:39:2244 } 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);
51
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);
56
[email protected]741fba422013-09-20 03:34:1457 success &= dict->GetList("Border", &list);
58 int border_x, border_y, border_width, border_height;
59 success &= list->GetInteger(0, &border_x);
60 success &= list->GetInteger(1, &border_y);
61 success &= list->GetInteger(2, &border_width);
62 success &= list->GetInteger(3, &border_height);
63
64 bool fill_center;
65 success &= dict->GetBoolean("FillCenter", &fill_center);
66
[email protected]37adf942013-03-08 04:43:1767 scoped_refptr<NinePatchLayer> nine_patch_layer = NinePatchLayer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2268
69 SkBitmap bitmap;
70 bitmap.setConfig(SkBitmap::kARGB_8888_Config, image_width, image_height);
71 bitmap.allocPixels(NULL, NULL);
[email protected]741fba422013-09-20 03:34:1472 bitmap.setImmutable();
[email protected]37adf942013-03-08 04:43:1773 nine_patch_layer->SetBitmap(bitmap,
[email protected]f8fef2bd2013-02-04 23:39:2274 gfx::Rect(aperture_x, aperture_y, aperture_width, aperture_height));
75
[email protected]741fba422013-09-20 03:34:1476 nine_patch_layer->SetBorder(
77 gfx::Rect(border_x, border_y, border_width, border_height));
78 nine_patch_layer->SetFillCenter(fill_center);
79
[email protected]f8fef2bd2013-02-04 23:39:2280 new_layer = nine_patch_layer;
[email protected]a49d0f8a2013-05-09 23:26:1981 } else if (layer_type == "PictureLayer") {
82 new_layer = PictureLayer::Create(content_client);
[email protected]f8fef2bd2013-02-04 23:39:2283 } else { // Type "Layer" or "unknown"
[email protected]7aba6662013-03-12 10:17:3484 new_layer = Layer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2285 }
[email protected]7aba6662013-03-12 10:17:3486 new_layer->SetAnchorPoint(gfx::Point());
87 new_layer->SetPosition(gfx::PointF(position_x, position_y));
88 new_layer->SetBounds(gfx::Size(width, height));
89 new_layer->SetIsDrawable(draws_content);
[email protected]f8fef2bd2013-02-04 23:39:2290
91 double opacity;
92 if (dict->GetDouble("Opacity", &opacity))
[email protected]7aba6662013-03-12 10:17:3493 new_layer->SetOpacity(opacity);
[email protected]f8fef2bd2013-02-04 23:39:2294
[email protected]46c76952013-07-10 00:21:4595 bool contents_opaque;
96 if (dict->GetBoolean("ContentsOpaque", &contents_opaque))
97 new_layer->SetContentsOpaque(contents_opaque);
98
[email protected]d993e032013-06-07 00:16:1699 bool scrollable;
100 if (dict->GetBoolean("Scrollable", &scrollable))
101 new_layer->SetScrollable(scrollable);
102
[email protected]f8fef2bd2013-02-04 23:39:22103 success &= dict->GetList("DrawTransform", &list);
104 double transform[16];
105 for (int i = 0; i < 16; ++i)
106 success &= list->GetDouble(i, &transform[i]);
107
[email protected]ed511b8d2013-03-25 03:29:29108 gfx::Transform layer_transform;
109 layer_transform.matrix().setColMajord(transform);
110 new_layer->SetTransform(layer_transform);
[email protected]f8fef2bd2013-02-04 23:39:22111
112 success &= dict->GetList("Children", &list);
113 for (ListValue::const_iterator it = list->begin();
114 it != list->end(); ++it) {
[email protected]7aba6662013-03-12 10:17:34115 new_layer->AddChild(ParseTreeFromValue(*it, content_client));
[email protected]f8fef2bd2013-02-04 23:39:22116 }
117
118 if (!success)
119 return NULL;
120
121 return new_layer;
122}
123
124} // namespace
125
126scoped_refptr<Layer> ParseTreeFromJson(std::string json,
127 ContentLayerClient* content_client) {
128 scoped_ptr<base::Value> val = base::test::ParseJson(json);
129 return ParseTreeFromValue(val.get(), content_client);
130}
131
132} // namespace cc