blob: 9a1b198a7b772d423d50c56adde28aee44e974e8 [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]3393fa92013-10-18 03:37:5214#include "cc/layers/texture_layer.h"
[email protected]f8fef2bd2013-02-04 23:39:2215
16namespace cc {
17
18namespace {
19
20scoped_refptr<Layer> ParseTreeFromValue(base::Value* val,
21 ContentLayerClient* content_client) {
22 DictionaryValue* dict;
23 bool success = true;
24 success &= val->GetAsDictionary(&dict);
25 std::string layer_type;
26 success &= dict->GetString("LayerType", &layer_type);
27 ListValue* list;
28 success &= dict->GetList("Bounds", &list);
29 int width, height;
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);
36
37 bool draws_content;
38 success &= dict->GetBoolean("DrawsContent", &draws_content);
39
40 scoped_refptr<Layer> new_layer;
41 if (layer_type == "SolidColorLayer") {
[email protected]67be9b1d2013-03-08 02:27:4542 new_layer = SolidColorLayer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2243 } else if (layer_type == "ContentLayer") {
[email protected]7aba6662013-03-12 10:17:3444 new_layer = ContentLayer::Create(content_client);
[email protected]f8fef2bd2013-02-04 23:39:2245 } 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);
52
[email protected]3393fa92013-10-18 03:37:5253 ListValue* bounds;
54 success &= dict->GetList("ImageBounds", &bounds);
55 double image_width, image_height;
[email protected]89c8b482013-10-28 22:12:4156 success &= bounds->GetDouble(0, &image_width);
57 success &= bounds->GetDouble(1, &image_height);
[email protected]f8fef2bd2013-02-04 23:39:2258
[email protected]741fba422013-09-20 03:34:1459 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);
65
66 bool fill_center;
67 success &= dict->GetBoolean("FillCenter", &fill_center);
68
[email protected]37adf942013-03-08 04:43:1769 scoped_refptr<NinePatchLayer> nine_patch_layer = NinePatchLayer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2270
71 SkBitmap bitmap;
72 bitmap.setConfig(SkBitmap::kARGB_8888_Config, image_width, image_height);
73 bitmap.allocPixels(NULL, NULL);
[email protected]741fba422013-09-20 03:34:1474 bitmap.setImmutable();
[email protected]efbdb3a2013-10-04 00:35:1375 nine_patch_layer->SetBitmap(bitmap);
76 nine_patch_layer->SetAperture(
[email protected]f8fef2bd2013-02-04 23:39:2277 gfx::Rect(aperture_x, aperture_y, aperture_width, aperture_height));
[email protected]741fba422013-09-20 03:34:1478 nine_patch_layer->SetBorder(
79 gfx::Rect(border_x, border_y, border_width, border_height));
80 nine_patch_layer->SetFillCenter(fill_center);
81
[email protected]f8fef2bd2013-02-04 23:39:2282 new_layer = nine_patch_layer;
[email protected]3393fa92013-10-18 03:37:5283 } else if (layer_type == "TextureLayer") {
[email protected]89c8b482013-10-28 22:12:4184 new_layer = TextureLayer::CreateForMailbox(NULL);
[email protected]a49d0f8a2013-05-09 23:26:1985 } else if (layer_type == "PictureLayer") {
86 new_layer = PictureLayer::Create(content_client);
[email protected]f8fef2bd2013-02-04 23:39:2287 } else { // Type "Layer" or "unknown"
[email protected]7aba6662013-03-12 10:17:3488 new_layer = Layer::Create();
[email protected]f8fef2bd2013-02-04 23:39:2289 }
[email protected]7aba6662013-03-12 10:17:3490 new_layer->SetAnchorPoint(gfx::Point());
91 new_layer->SetPosition(gfx::PointF(position_x, position_y));
92 new_layer->SetBounds(gfx::Size(width, height));
93 new_layer->SetIsDrawable(draws_content);
[email protected]f8fef2bd2013-02-04 23:39:2294
95 double opacity;
96 if (dict->GetDouble("Opacity", &opacity))
[email protected]7aba6662013-03-12 10:17:3497 new_layer->SetOpacity(opacity);
[email protected]f8fef2bd2013-02-04 23:39:2298
[email protected]46c76952013-07-10 00:21:4599 bool contents_opaque;
100 if (dict->GetBoolean("ContentsOpaque", &contents_opaque))
101 new_layer->SetContentsOpaque(contents_opaque);
102
[email protected]d993e032013-06-07 00:16:16103 bool scrollable;
104 if (dict->GetBoolean("Scrollable", &scrollable))
105 new_layer->SetScrollable(scrollable);
106
[email protected]9d161d22013-10-29 20:54:10107 bool wheel_handler;
108 if (dict->GetBoolean("WheelHandler", &wheel_handler))
109 new_layer->SetHaveWheelEventHandlers(wheel_handler);
110
111 if (dict->HasKey("TouchRegion")) {
112 success &= dict->GetList("TouchRegion", &list);
113 cc::Region touch_region;
114 for (size_t i = 0; i < list->GetSize(); ) {
115 int rect_x, rect_y, rect_width, rect_height;
116 success &= list->GetInteger(i++, &rect_x);
117 success &= list->GetInteger(i++, &rect_y);
118 success &= list->GetInteger(i++, &rect_width);
119 success &= list->GetInteger(i++, &rect_height);
120 touch_region.Union(gfx::Rect(rect_x, rect_y, rect_width, rect_height));
121 }
122 new_layer->SetTouchEventHandlerRegion(touch_region);
123 }
124
[email protected]f8fef2bd2013-02-04 23:39:22125 success &= dict->GetList("DrawTransform", &list);
126 double transform[16];
127 for (int i = 0; i < 16; ++i)
128 success &= list->GetDouble(i, &transform[i]);
129
[email protected]ed511b8d2013-03-25 03:29:29130 gfx::Transform layer_transform;
131 layer_transform.matrix().setColMajord(transform);
132 new_layer->SetTransform(layer_transform);
[email protected]f8fef2bd2013-02-04 23:39:22133
134 success &= dict->GetList("Children", &list);
135 for (ListValue::const_iterator it = list->begin();
136 it != list->end(); ++it) {
[email protected]7aba6662013-03-12 10:17:34137 new_layer->AddChild(ParseTreeFromValue(*it, content_client));
[email protected]f8fef2bd2013-02-04 23:39:22138 }
139
140 if (!success)
141 return NULL;
142
143 return new_layer;
144}
145
146} // namespace
147
148scoped_refptr<Layer> ParseTreeFromJson(std::string json,
149 ContentLayerClient* content_client) {
150 scoped_ptr<base::Value> val = base::test::ParseJson(json);
151 return ParseTreeFromValue(val.get(), content_client);
152}
153
154} // namespace cc