[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" |
| 9 | #include "cc/content_layer.h" |
| 10 | #include "cc/layer.h" |
| 11 | #include "cc/nine_patch_layer.h" |
| 12 | #include "cc/solid_color_layer.h" |
| 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") { |
| 40 | new_layer = SolidColorLayer::create(); |
| 41 | } else if (layer_type == "ContentLayer") { |
| 42 | new_layer = ContentLayer::create(content_client); |
| 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 | |
| 56 | scoped_refptr<NinePatchLayer> nine_patch_layer = NinePatchLayer::create(); |
| 57 | |
| 58 | SkBitmap bitmap; |
| 59 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, image_width, image_height); |
| 60 | bitmap.allocPixels(NULL, NULL); |
| 61 | nine_patch_layer->setBitmap(bitmap, |
| 62 | gfx::Rect(aperture_x, aperture_y, aperture_width, aperture_height)); |
| 63 | |
| 64 | new_layer = nine_patch_layer; |
| 65 | } else { // Type "Layer" or "unknown" |
| 66 | new_layer = Layer::create(); |
| 67 | } |
| 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); |
| 72 | |
| 73 | double opacity; |
| 74 | if (dict->GetDouble("Opacity", &opacity)) |
| 75 | new_layer->setOpacity(opacity); |
| 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 | |
| 82 | gfx::Transform gfxTransform; |
| 83 | gfxTransform.matrix().setColMajord(transform); |
| 84 | new_layer->setTransform(gfxTransform); |
| 85 | |
| 86 | success &= dict->GetList("Children", &list); |
| 87 | for (ListValue::const_iterator it = list->begin(); |
| 88 | it != list->end(); ++it) { |
| 89 | new_layer->addChild(ParseTreeFromValue(*it, content_client)); |
| 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 |