[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 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. |
| 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] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 9 | #include "cc/layers/content_layer.h" |
| 10 | #include "cc/layers/layer.h" |
| 11 | #include "cc/layers/nine_patch_layer.h" |
[email protected] | a49d0f8a | 2013-05-09 23:26:19 | [diff] [blame] | 12 | #include "cc/layers/picture_layer.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 13 | #include "cc/layers/solid_color_layer.h" |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 14 | |
| 15 | namespace cc { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | scoped_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] | 67be9b1d | 2013-03-08 02:27:45 | [diff] [blame] | 41 | new_layer = SolidColorLayer::Create(); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 42 | } else if (layer_type == "ContentLayer") { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 43 | new_layer = ContentLayer::Create(content_client); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 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); |
| 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] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame^] | 57 | 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] | 37adf94 | 2013-03-08 04:43:17 | [diff] [blame] | 67 | scoped_refptr<NinePatchLayer> nine_patch_layer = NinePatchLayer::Create(); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 68 | |
| 69 | SkBitmap bitmap; |
| 70 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, image_width, image_height); |
| 71 | bitmap.allocPixels(NULL, NULL); |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame^] | 72 | bitmap.setImmutable(); |
[email protected] | 37adf94 | 2013-03-08 04:43:17 | [diff] [blame] | 73 | nine_patch_layer->SetBitmap(bitmap, |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 74 | gfx::Rect(aperture_x, aperture_y, aperture_width, aperture_height)); |
| 75 | |
[email protected] | 741fba42 | 2013-09-20 03:34:14 | [diff] [blame^] | 76 | 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] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 80 | new_layer = nine_patch_layer; |
[email protected] | a49d0f8a | 2013-05-09 23:26:19 | [diff] [blame] | 81 | } else if (layer_type == "PictureLayer") { |
| 82 | new_layer = PictureLayer::Create(content_client); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 83 | } else { // Type "Layer" or "unknown" |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 84 | new_layer = Layer::Create(); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 85 | } |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 86 | 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] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 90 | |
| 91 | double opacity; |
| 92 | if (dict->GetDouble("Opacity", &opacity)) |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 93 | new_layer->SetOpacity(opacity); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 94 | |
[email protected] | 46c7695 | 2013-07-10 00:21:45 | [diff] [blame] | 95 | bool contents_opaque; |
| 96 | if (dict->GetBoolean("ContentsOpaque", &contents_opaque)) |
| 97 | new_layer->SetContentsOpaque(contents_opaque); |
| 98 | |
[email protected] | d993e03 | 2013-06-07 00:16:16 | [diff] [blame] | 99 | bool scrollable; |
| 100 | if (dict->GetBoolean("Scrollable", &scrollable)) |
| 101 | new_layer->SetScrollable(scrollable); |
| 102 | |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 103 | 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] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 108 | gfx::Transform layer_transform; |
| 109 | layer_transform.matrix().setColMajord(transform); |
| 110 | new_layer->SetTransform(layer_transform); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 111 | |
| 112 | success &= dict->GetList("Children", &list); |
| 113 | for (ListValue::const_iterator it = list->begin(); |
| 114 | it != list->end(); ++it) { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 115 | new_layer->AddChild(ParseTreeFromValue(*it, content_client)); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (!success) |
| 119 | return NULL; |
| 120 | |
| 121 | return new_layer; |
| 122 | } |
| 123 | |
| 124 | } // namespace |
| 125 | |
| 126 | scoped_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 |