blob: 435e2bf690b816bed74f5c6c49d10722b3ca4c0c [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"
12#include "cc/layers/solid_color_layer.h"
[email protected]f8fef2bd2013-02-04 23:39:2213
14namespace cc {
15
16namespace {
17
18scoped_refptr<Layer> ParseTreeFromValue(base::Value* val,
19 ContentLayerClient* content_client) {
20 DictionaryValue* dict;
21 bool success = true;
22 success &= val->GetAsDictionary(&dict);
23 std::string layer_type;
24 success &= dict->GetString("LayerType", &layer_type);
25 ListValue* list;
26 success &= dict->GetList("Bounds", &list);
27 int width, height;
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);
34
35 bool draws_content;
36 success &= dict->GetBoolean("DrawsContent", &draws_content);
37
38 scoped_refptr<Layer> new_layer;
39 if (layer_type == "SolidColorLayer") {
[email protected]67be9b1d2013-03-08 02:27:4540 new_layer = SolidColorLayer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2241 } else if (layer_type == "ContentLayer") {
[email protected]7aba6662013-03-12 10:17:3442 new_layer = ContentLayer::Create(content_client);
[email protected]f8fef2bd2013-02-04 23:39:2243 } 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);
50
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);
55
[email protected]37adf942013-03-08 04:43:1756 scoped_refptr<NinePatchLayer> nine_patch_layer = NinePatchLayer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2257
58 SkBitmap bitmap;
59 bitmap.setConfig(SkBitmap::kARGB_8888_Config, image_width, image_height);
60 bitmap.allocPixels(NULL, NULL);
[email protected]37adf942013-03-08 04:43:1761 nine_patch_layer->SetBitmap(bitmap,
[email protected]f8fef2bd2013-02-04 23:39:2262 gfx::Rect(aperture_x, aperture_y, aperture_width, aperture_height));
63
64 new_layer = nine_patch_layer;
65 } else { // Type "Layer" or "unknown"
[email protected]7aba6662013-03-12 10:17:3466 new_layer = Layer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2267 }
[email protected]7aba6662013-03-12 10:17:3468 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);
[email protected]f8fef2bd2013-02-04 23:39:2272
73 double opacity;
74 if (dict->GetDouble("Opacity", &opacity))
[email protected]7aba6662013-03-12 10:17:3475 new_layer->SetOpacity(opacity);
[email protected]f8fef2bd2013-02-04 23:39:2276
77 success &= dict->GetList("DrawTransform", &list);
78 double transform[16];
79 for (int i = 0; i < 16; ++i)
80 success &= list->GetDouble(i, &transform[i]);
81
[email protected]ed511b8d2013-03-25 03:29:2982 gfx::Transform layer_transform;
83 layer_transform.matrix().setColMajord(transform);
84 new_layer->SetTransform(layer_transform);
[email protected]f8fef2bd2013-02-04 23:39:2285
86 success &= dict->GetList("Children", &list);
87 for (ListValue::const_iterator it = list->begin();
88 it != list->end(); ++it) {
[email protected]7aba6662013-03-12 10:17:3489 new_layer->AddChild(ParseTreeFromValue(*it, content_client));
[email protected]f8fef2bd2013-02-04 23:39:2290 }
91
92 if (!success)
93 return NULL;
94
95 return new_layer;
96}
97
98} // namespace
99
100scoped_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);
104}
105
106} // namespace cc