Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "gpu/tools/compositor_model_bench/render_tree.h" | |
| 6 | |
| 7 #include <sstream> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/json/json_reader.h" | |
| 13 #include "base/json/json_writer.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/values.h" | |
| 17 | |
| 18 #include "gpu/tools/compositor_model_bench/shaders.h" | |
| 19 | |
| 20 using base::JSONReader; | |
| 21 using base::JSONWriter; | |
| 22 using base::Value; | |
| 23 using file_util::ReadFileToString; | |
| 24 using std::string; | |
| 25 using std::vector; | |
| 26 | |
| 27 GLenum TextureFormatFromString(std::string format) { | |
| 28 if (format == "RGBA") | |
| 29 return GL_RGBA; | |
| 30 if (format == "RGB") | |
| 31 return GL_RGB; | |
| 32 if (format == "LUMINANCE") | |
| 33 return GL_LUMINANCE; | |
| 34 return GL_INVALID_ENUM; | |
| 35 } | |
| 36 | |
| 37 const char* TextureFormatName(GLenum format) { | |
| 38 switch (format) { | |
| 39 case GL_RGBA: | |
| 40 return "RGBA"; | |
| 41 case GL_RGB: | |
| 42 return "RGB"; | |
| 43 case GL_LUMINANCE: | |
| 44 return "LUMINANCE"; | |
| 45 default: | |
| 46 return "(unknown format)"; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 int FormatBytesPerPixel(GLenum format) { | |
| 51 switch (format) { | |
| 52 case GL_RGBA: | |
| 53 return 4; | |
| 54 case GL_RGB: | |
| 55 return 3; | |
| 56 case GL_LUMINANCE: | |
| 57 return 1; | |
| 58 default: | |
| 59 return 0; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void RenderNode::Accept(RenderNodeVisitor* v) { | |
| 64 v->BeginVisitRenderNode(this); | |
| 65 v->EndVisitRenderNode(this); | |
| 66 } | |
| 67 | |
| 68 void ContentLayerNode::Accept(RenderNodeVisitor* v) { | |
| 69 v->BeginVisitContentLayerNode(this); | |
| 70 typedef vector<RenderNode*>::iterator node_itr; | |
| 71 for (node_itr i = children.begin(); i != children.end(); ++i) { | |
| 72 (*i)->Accept(v); | |
| 73 } | |
| 74 v->EndVisitContentLayerNode(this); | |
| 75 } | |
| 76 | |
| 77 void CCNode::Accept(RenderNodeVisitor* v) { | |
| 78 v->BeginVisitCCNode(this); | |
| 79 v->EndVisitCCNode(this); | |
| 80 } | |
| 81 | |
| 82 RenderNode* InterpretNode(DictionaryValue* node); | |
| 83 | |
| 84 std::string ValueTypeAsString(Value::Type type) { | |
| 85 switch (type) { | |
| 86 case Value::TYPE_NULL: | |
| 87 return "NULL"; | |
| 88 case Value::TYPE_BOOLEAN: | |
| 89 return "BOOLEAN"; | |
| 90 case Value::TYPE_INTEGER: | |
| 91 return "INTEGER"; | |
| 92 case Value::TYPE_DOUBLE: | |
| 93 return "DOUBLE"; | |
| 94 case Value::TYPE_STRING: | |
| 95 return "STRING"; | |
| 96 case Value::TYPE_BINARY: | |
| 97 return "BINARY"; | |
| 98 case Value::TYPE_DICTIONARY: | |
| 99 return "DICTIONARY"; | |
| 100 case Value::TYPE_LIST: | |
| 101 return "LIST"; | |
| 102 default: | |
| 103 return "(UNKNOWN TYPE)"; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 // Makes sure that the key exists and has the type we expect. | |
| 108 bool VerifyDictionaryEntry(DictionaryValue* node, | |
| 109 const std::string& key, | |
| 110 Value::Type type) { | |
| 111 if (!node->HasKey(key)) { | |
| 112 LOG(ERROR) << "Missing value for key: " << key; | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 Value* child; | |
| 117 node->Get(key, &child); | |
| 118 if (!child->IsType(type)) { | |
| 119 LOG(ERROR) << key << " did not have the expected type " | |
| 120 "(expected " << ValueTypeAsString(type) << ")"; | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 // Makes sure that the list entry has the type we expect. | |
| 128 bool VerifyListEntry(ListValue* l, | |
| 129 int idx, | |
| 130 Value::Type type, | |
| 131 const char* listName = 0) { | |
| 132 // Assume the idx is valid (since we'll be able to generate a better | |
| 133 // error message for this elsewhere.) | |
| 134 Value* el; | |
| 135 l->Get(idx, &el); | |
| 136 if (!el->IsType(type)) { | |
| 137 LOG(ERROR) << (listName ? listName : "List") << "element " << idx << | |
| 138 " did not have the expected type (expected " << | |
| 139 ValueTypeAsString(type) << ")\n"; | |
| 140 return false; | |
| 141 } | |
| 142 | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 bool InterpretCommonContents(DictionaryValue* node, RenderNode* c) { | |
| 147 if (!VerifyDictionaryEntry(node, "layerID", Value::TYPE_INTEGER) || | |
| 148 !VerifyDictionaryEntry(node, "width", Value::TYPE_INTEGER) || | |
| 149 !VerifyDictionaryEntry(node, "height", Value::TYPE_INTEGER) || | |
| 150 !VerifyDictionaryEntry(node, "drawsContent", Value::TYPE_BOOLEAN) || | |
| 151 !VerifyDictionaryEntry(node, "targetSurfaceID", Value::TYPE_INTEGER) || | |
| 152 !VerifyDictionaryEntry(node, "transform", Value::TYPE_LIST) | |
| 153 ) { | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 157 node->GetInteger("layerID", &c->layerID); | |
| 158 node->GetInteger("width", &c->width); | |
| 159 node->GetInteger("height", &c->height); | |
| 160 node->GetBoolean("drawsContent", &c->drawsContent); | |
| 161 node->GetInteger("targetSurfaceID", &c->targetSurface); | |
| 162 ListValue* transform; | |
| 163 node->GetList("transform", &transform); | |
| 164 if (transform->GetSize() != 16) { | |
| 165 LOG(ERROR) << "4x4 transform matrix did not have 16 elements"; | |
| 166 return false; | |
| 167 } | |
| 168 for (int i = 0; i < 16; ++i) { | |
| 169 if (!VerifyListEntry(transform, i, Value::TYPE_DOUBLE, "Transform")) | |
| 170 return false; | |
| 171 double el; | |
| 172 transform->GetDouble(i, &el); | |
| 173 c->transform[i] = el; | |
| 174 } | |
| 175 if (node->HasKey("tiles")) { | |
| 176 if (!VerifyDictionaryEntry(node, "tiles", Value::TYPE_DICTIONARY)) | |
| 177 return false; | |
| 178 DictionaryValue* tiles_dict; | |
| 179 node->GetDictionary("tiles", &tiles_dict); | |
| 180 if (!VerifyDictionaryEntry(tiles_dict, "dim", Value::TYPE_LIST)) | |
| 181 return false; | |
| 182 ListValue* dim; | |
| 183 tiles_dict->GetList("dim", &dim); | |
| 184 if (!VerifyListEntry(dim, 0, Value::TYPE_INTEGER, "Tile dimension") || | |
| 185 !VerifyListEntry(dim, 1, Value::TYPE_INTEGER, "Tile dimension")) { | |
| 186 return false; | |
| 187 } | |
| 188 dim->GetInteger(0, &c->tile_width); | |
| 189 dim->GetInteger(1, &c->tile_height); | |
| 190 | |
| 191 if (!VerifyDictionaryEntry(tiles_dict, "info", Value::TYPE_LIST)) | |
| 192 return false; | |
| 193 ListValue* tiles; | |
| 194 tiles_dict->GetList("info", &tiles); | |
| 195 for (unsigned int i = 0; i < tiles->GetSize(); ++i) { | |
| 196 if (!VerifyListEntry(tiles, i, Value::TYPE_DICTIONARY, "Tile info")) | |
| 197 return false; | |
| 198 DictionaryValue* tdict; | |
| 199 tiles->GetDictionary(i, &tdict); | |
| 200 | |
| 201 if (!VerifyDictionaryEntry(tdict, "x", Value::TYPE_INTEGER) || | |
| 202 !VerifyDictionaryEntry(tdict, "y", Value::TYPE_INTEGER)) { | |
| 203 return false; | |
| 204 } | |
| 205 Tile t; | |
| 206 tdict->GetInteger("x", &t.x); | |
| 207 tdict->GetInteger("y", &t.y); | |
| 208 if (tdict->HasKey("texID")) { | |
| 209 if (!VerifyDictionaryEntry(tdict, "texID", Value::TYPE_INTEGER)) | |
| 210 return false; | |
| 211 tdict->GetInteger("texID", &t.texID); | |
| 212 } else { | |
| 213 t.texID = -1; | |
| 214 } | |
| 215 c->tiles.push_back(t); | |
| 216 } | |
| 217 } | |
| 218 return true; | |
| 219 } | |
| 220 | |
| 221 bool InterpretCCData(DictionaryValue* node, CCNode* c) { | |
| 222 if (!VerifyDictionaryEntry(node, "vertex_shader", Value::TYPE_STRING) || | |
| 223 !VerifyDictionaryEntry(node, "fragment_shader", Value::TYPE_STRING) || | |
| 224 !VerifyDictionaryEntry(node, "textures", Value::TYPE_LIST) | |
| 225 ) { | |
|
piman
2011/08/26 02:24:16
nit: the ')' (and the '{' with it) should be on th
| |
| 226 return false; | |
| 227 } | |
| 228 string vertex_shader_name, fragment_shader_name; | |
| 229 node->GetString("vertex_shader", &vertex_shader_name); | |
| 230 node->GetString("fragment_shader", &fragment_shader_name); | |
| 231 | |
| 232 c->vertex_shader = ShaderIDFromString(vertex_shader_name); | |
| 233 c->fragment_shader = ShaderIDFromString(fragment_shader_name); | |
| 234 ListValue* textures; | |
| 235 node->GetList("textures", &textures); | |
| 236 for (unsigned int i = 0; i < textures->GetSize(); ++i) { | |
| 237 if (!VerifyListEntry(textures, i, Value::TYPE_DICTIONARY, "Tex list")) | |
| 238 return false; | |
| 239 DictionaryValue* tex; | |
| 240 textures->GetDictionary(i, &tex); | |
| 241 | |
| 242 if (!VerifyDictionaryEntry(tex, "texID", Value::TYPE_INTEGER) || | |
| 243 !VerifyDictionaryEntry(tex, "height", Value::TYPE_INTEGER) || | |
| 244 !VerifyDictionaryEntry(tex, "width", Value::TYPE_INTEGER) || | |
| 245 !VerifyDictionaryEntry(tex, "format", Value::TYPE_STRING) | |
| 246 ) { | |
|
piman
2011/08/26 02:24:16
nit: the ')' (and the '{' with it) should be on th
| |
| 247 return false; | |
| 248 } | |
| 249 Texture t; | |
| 250 tex->GetInteger("texID", &t.texID); | |
| 251 tex->GetInteger("height", &t.height); | |
| 252 tex->GetInteger("width", &t.width); | |
| 253 | |
| 254 string formatName; | |
| 255 tex->GetString("format", &formatName); | |
| 256 t.format = TextureFormatFromString(formatName); | |
| 257 if (t.format == GL_INVALID_ENUM) { | |
| 258 LOG(ERROR) << "Unrecognized texture format in layer " << c->layerID << | |
| 259 " (format: " << formatName << ")\n" | |
| 260 "The layer had " << textures->GetSize() << " children."; | |
| 261 return false; | |
| 262 } | |
| 263 | |
| 264 c->textures.push_back(t); | |
| 265 } | |
| 266 | |
| 267 if (c->vertex_shader == SHADER_UNRECOGNIZED) { | |
| 268 LOG(ERROR) << "Unrecognized vertex shader name, layer " << c->layerID << | |
| 269 " (shader: " << vertex_shader_name << ")"; | |
| 270 return false; | |
| 271 } | |
| 272 | |
| 273 if (c->fragment_shader == SHADER_UNRECOGNIZED) { | |
| 274 LOG(ERROR) << "Unrecognized fragment shader name, layer " << c->layerID << | |
| 275 " (shader: " << fragment_shader_name << ")"; | |
| 276 return false; | |
| 277 } | |
| 278 | |
| 279 return true; | |
| 280 } | |
| 281 | |
| 282 RenderNode* InterpretContentLayer(DictionaryValue* node) { | |
| 283 ContentLayerNode* n = new ContentLayerNode; | |
| 284 if (!InterpretCommonContents(node, n)) | |
| 285 return NULL; | |
| 286 | |
| 287 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING) || | |
| 288 !VerifyDictionaryEntry(node, "skipsDraw", Value::TYPE_BOOLEAN) || | |
| 289 !VerifyDictionaryEntry(node, "children", Value::TYPE_LIST) | |
| 290 ) { | |
|
piman
2011/08/26 02:24:16
nit: the ')' (and the '{' with it) should be on th
| |
| 291 return false; | |
| 292 } | |
| 293 | |
| 294 string type; | |
| 295 node->GetString("type", &type); | |
| 296 DCHECK_EQ(type, "ContentLayer"); | |
| 297 node->GetBoolean("skipsDraw", &n->skipsDraw); | |
| 298 | |
| 299 ListValue* children; | |
| 300 node->GetList("children", &children); | |
| 301 for (unsigned int i = 0; i < children->GetSize(); ++i) { | |
| 302 DictionaryValue* childNode; | |
| 303 children->GetDictionary(i, &childNode); | |
| 304 RenderNode* child = InterpretNode(childNode); | |
| 305 if (child) | |
| 306 n->children.push_back(child); | |
| 307 } | |
| 308 | |
| 309 return n; | |
| 310 } | |
| 311 | |
| 312 RenderNode* InterpretCanvasLayer(DictionaryValue* node) { | |
| 313 CCNode* n = new CCNode; | |
| 314 if (!InterpretCommonContents(node, n)) | |
| 315 return NULL; | |
| 316 | |
| 317 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
| 318 return NULL; | |
| 319 } | |
| 320 | |
| 321 string type; | |
| 322 node->GetString("type", &type); | |
| 323 assert(type == "CanvasLayer"); | |
| 324 | |
| 325 if (!InterpretCCData(node, n)) | |
| 326 return NULL; | |
| 327 | |
| 328 return n; | |
| 329 } | |
| 330 | |
| 331 RenderNode* InterpretVideoLayer(DictionaryValue* node) { | |
| 332 CCNode* n = new CCNode; | |
| 333 if (!InterpretCommonContents(node, n)) | |
| 334 return NULL; | |
| 335 | |
| 336 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
| 337 return NULL; | |
| 338 } | |
| 339 | |
| 340 string type; | |
| 341 node->GetString("type", &type); | |
| 342 assert(type == "VideoLayer"); | |
| 343 | |
| 344 if (!InterpretCCData(node, n)) | |
| 345 return NULL; | |
| 346 | |
| 347 return n; | |
| 348 } | |
| 349 | |
| 350 RenderNode* InterpretImageLayer(DictionaryValue* node) { | |
| 351 CCNode* n = new CCNode; | |
| 352 if (!InterpretCommonContents(node, n)) | |
| 353 return NULL; | |
| 354 | |
| 355 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
| 356 return NULL; | |
| 357 } | |
| 358 | |
| 359 string type; | |
| 360 node->GetString("type", &type); | |
| 361 assert(type == "ImageLayer"); | |
| 362 | |
| 363 if (!InterpretCCData(node, n)) | |
| 364 return NULL; | |
| 365 | |
| 366 return n; | |
| 367 } | |
| 368 | |
| 369 RenderNode* InterpretNode(DictionaryValue* node) { | |
| 370 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
| 371 return NULL; | |
| 372 } | |
| 373 | |
| 374 string type; | |
| 375 node->GetString("type", &type); | |
| 376 if (type == "ContentLayer") | |
| 377 return InterpretContentLayer(node); | |
| 378 if (type == "CanvasLayer") | |
| 379 return InterpretCanvasLayer(node); | |
| 380 if (type == "VideoLayer") | |
| 381 return InterpretVideoLayer(node); | |
| 382 if (type == "ImageLayer") | |
| 383 return InterpretImageLayer(node); | |
| 384 | |
| 385 | |
| 386 string outjson; | |
| 387 JSONWriter::Write(node, true, &outjson); | |
| 388 LOG(ERROR) << "Unrecognized node type! JSON:\n\n" | |
| 389 "-----------------------\n" << | |
|
piman
2011/08/26 02:24:16
nit: indent (4 spaces)
| |
| 390 outjson << | |
| 391 "-----------------------"; | |
| 392 | |
| 393 return NULL; | |
| 394 } | |
| 395 | |
| 396 RenderNode* BuildRenderTreeFromFile(const FilePath& path) { | |
| 397 LOG(INFO) << "Reading " << path.LossyDisplayName(); | |
| 398 string contents; | |
| 399 if (!ReadFileToString(path, &contents)) | |
| 400 return NULL; | |
| 401 | |
| 402 scoped_ptr<Value> root; | |
| 403 int error_code = 0; | |
| 404 string error_message; | |
| 405 root.reset(JSONReader::ReadAndReturnError(contents, | |
| 406 true, | |
| 407 &error_code, | |
| 408 &error_message)); | |
| 409 if (!root.get()) { | |
| 410 LOG(ERROR) << "Failed to parse JSON file " << path.LossyDisplayName() << | |
| 411 "\n(" << error_message << ")"; | |
|
piman
2011/08/26 02:24:16
nit: 4 spaces
| |
| 412 return NULL; | |
| 413 } | |
| 414 | |
| 415 if (root->IsType(Value::TYPE_DICTIONARY)) { | |
| 416 DictionaryValue* v = static_cast<DictionaryValue*>(root.get()); | |
| 417 RenderNode* tree = InterpretContentLayer(v); | |
| 418 return tree; | |
| 419 } else { | |
| 420 LOG(ERROR) << path.LossyDisplayName() << | |
| 421 " doesn not encode a JSON dictionary."; | |
|
piman
2011/08/26 02:24:16
nit: 4 spaces
| |
| 422 return NULL; | |
| 423 } | |
| 424 } | |
| 425 | |
| OLD | NEW |