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/layers/content_layer.h" 10 #include "cc/layers/layer.h" 11 #include "cc/layers/nine_patch_layer.h" 12 #include "cc/layers/picture_layer.h" 13 #include "cc/layers/solid_color_layer.h" 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") { 41 new_layer = SolidColorLayer::Create(); 42 } else if (layer_type == "ContentLayer") { 43 new_layer = ContentLayer::Create(content_client); 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 57 scoped_refptr<NinePatchLayer> nine_patch_layer = NinePatchLayer::Create(); 58 59 SkBitmap bitmap; 60 bitmap.setConfig(SkBitmap::kARGB_8888_Config, image_width, image_height); 61 bitmap.allocPixels(NULL, NULL); 62 nine_patch_layer->SetBitmap(bitmap, 63 gfx::Rect(aperture_x, aperture_y, aperture_width, aperture_height)); 64 65 new_layer = nine_patch_layer; 66 } else if (layer_type == "PictureLayer") { 67 new_layer = PictureLayer::Create(content_client); 68 } else { // Type "Layer" or "unknown" 69 new_layer = Layer::Create(); 70 } 71 new_layer->SetAnchorPoint(gfx::Point()); 72 new_layer->SetPosition(gfx::PointF(position_x, position_y)); 73 new_layer->SetBounds(gfx::Size(width, height)); 74 new_layer->SetIsDrawable(draws_content); 75 76 double opacity; 77 if (dict->GetDouble("Opacity", &opacity)) 78 new_layer->SetOpacity(opacity); 79 80 bool contents_opaque; 81 if (dict->GetBoolean("ContentsOpaque", &contents_opaque)) 82 new_layer->SetContentsOpaque(contents_opaque); 83 84 bool scrollable; 85 if (dict->GetBoolean("Scrollable", &scrollable)) 86 new_layer->SetScrollable(scrollable); 87 88 success &= dict->GetList("DrawTransform", &list); 89 double transform[16]; 90 for (int i = 0; i < 16; ++i) 91 success &= list->GetDouble(i, &transform[i]); 92 93 gfx::Transform layer_transform; 94 layer_transform.matrix().setColMajord(transform); 95 new_layer->SetTransform(layer_transform); 96 97 success &= dict->GetList("Children", &list); 98 for (ListValue::const_iterator it = list->begin(); 99 it != list->end(); ++it) { 100 new_layer->AddChild(ParseTreeFromValue(*it, content_client)); 101 } 102 103 if (!success) 104 return NULL; 105 106 return new_layer; 107 } 108 109 } // namespace 110 111 scoped_refptr<Layer> ParseTreeFromJson(std::string json, 112 ContentLayerClient* content_client) { 113 scoped_ptr<base::Value> val = base::test::ParseJson(json); 114 return ParseTreeFromValue(val.get(), content_client); 115 } 116 117 } // namespace cc 118