[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" |
| 12 | #include "cc/layers/solid_color_layer.h" |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 13 | |
| 14 | namespace cc { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | scoped_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] | 67be9b1d | 2013-03-08 02:27:45 | [diff] [blame] | 40 | new_layer = SolidColorLayer::Create(); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 41 | } else if (layer_type == "ContentLayer") { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 42 | new_layer = ContentLayer::Create(content_client); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 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); |
| 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] | 37adf94 | 2013-03-08 04:43:17 | [diff] [blame] | 56 | scoped_refptr<NinePatchLayer> nine_patch_layer = NinePatchLayer::Create(); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 57 | |
| 58 | SkBitmap bitmap; |
| 59 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, image_width, image_height); |
| 60 | bitmap.allocPixels(NULL, NULL); |
[email protected] | 37adf94 | 2013-03-08 04:43:17 | [diff] [blame] | 61 | nine_patch_layer->SetBitmap(bitmap, |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 62 | 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] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 66 | new_layer = Layer::Create(); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 67 | } |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 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); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 72 | |
| 73 | double opacity; |
| 74 | if (dict->GetDouble("Opacity", &opacity)) |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 75 | new_layer->SetOpacity(opacity); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 76 | |
| 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] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame^] | 82 | gfx::Transform layer_transform; |
| 83 | layer_transform.matrix().setColMajord(transform); |
| 84 | new_layer->SetTransform(layer_transform); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 85 | |
| 86 | success &= dict->GetList("Children", &list); |
| 87 | for (ListValue::const_iterator it = list->begin(); |
| 88 | it != list->end(); ++it) { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 89 | new_layer->AddChild(ParseTreeFromValue(*it, content_client)); |
[email protected] | f8fef2bd | 2013-02-04 23:39:22 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | if (!success) |
| 93 | return NULL; |
| 94 | |
| 95 | return new_layer; |
| 96 | } |
| 97 | |
| 98 | } // namespace |
| 99 | |
| 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); |
| 104 | } |
| 105 | |
| 106 | } // namespace cc |