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