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 int layerID; | |
158 node->GetInteger("layerID", &layerID); | |
159 c->set_layerID(layerID); | |
160 int width; | |
161 node->GetInteger("width", &width); | |
162 c->set_width(width); | |
163 int height; | |
164 node->GetInteger("height", &height); | |
165 c->set_height(height); | |
166 bool drawsContent; | |
167 node->GetBoolean("drawsContent", &drawsContent); | |
168 c->set_drawsContent(drawsContent); | |
169 int targetSurface; | |
170 node->GetInteger("targetSurfaceID", &targetSurface); | |
171 c->set_targetSurface(targetSurface); | |
172 | |
173 ListValue* transform; | |
174 node->GetList("transform", &transform); | |
175 if (transform->GetSize() != 16) { | |
176 LOG(ERROR) << "4x4 transform matrix did not have 16 elements"; | |
177 return false; | |
178 } | |
179 float transform_mat[16]; | |
180 for (int i = 0; i < 16; ++i) { | |
181 if (!VerifyListEntry(transform, i, Value::TYPE_DOUBLE, "Transform")) | |
182 return false; | |
183 double el; | |
184 transform->GetDouble(i, &el); | |
185 transform_mat[i] = el; | |
186 } | |
187 c->set_transform(transform_mat); | |
188 | |
189 if (node->HasKey("tiles")) { | |
190 if (!VerifyDictionaryEntry(node, "tiles", Value::TYPE_DICTIONARY)) | |
191 return false; | |
192 DictionaryValue* tiles_dict; | |
193 node->GetDictionary("tiles", &tiles_dict); | |
194 if (!VerifyDictionaryEntry(tiles_dict, "dim", Value::TYPE_LIST)) | |
195 return false; | |
196 ListValue* dim; | |
197 tiles_dict->GetList("dim", &dim); | |
198 if (!VerifyListEntry(dim, 0, Value::TYPE_INTEGER, "Tile dimension") || | |
199 !VerifyListEntry(dim, 1, Value::TYPE_INTEGER, "Tile dimension")) { | |
200 return false; | |
201 } | |
202 int tile_width; | |
203 dim->GetInteger(0, &tile_width); | |
204 c->set_tile_width(tile_width); | |
205 int tile_height; | |
206 dim->GetInteger(1, &tile_height); | |
207 c->set_tile_height(tile_height); | |
208 | |
209 if (!VerifyDictionaryEntry(tiles_dict, "info", Value::TYPE_LIST)) | |
210 return false; | |
211 ListValue* tiles; | |
212 tiles_dict->GetList("info", &tiles); | |
213 for (unsigned int i = 0; i < tiles->GetSize(); ++i) { | |
214 if (!VerifyListEntry(tiles, i, Value::TYPE_DICTIONARY, "Tile info")) | |
215 return false; | |
216 DictionaryValue* tdict; | |
217 tiles->GetDictionary(i, &tdict); | |
218 | |
219 if (!VerifyDictionaryEntry(tdict, "x", Value::TYPE_INTEGER) || | |
220 !VerifyDictionaryEntry(tdict, "y", Value::TYPE_INTEGER)) { | |
221 return false; | |
222 } | |
223 Tile t; | |
224 tdict->GetInteger("x", &t.x); | |
225 tdict->GetInteger("y", &t.y); | |
226 if (tdict->HasKey("texID")) { | |
227 if (!VerifyDictionaryEntry(tdict, "texID", Value::TYPE_INTEGER)) | |
228 return false; | |
229 tdict->GetInteger("texID", &t.texID); | |
230 } else { | |
231 t.texID = -1; | |
232 } | |
233 c->add_tile(t); | |
234 } | |
235 } | |
236 return true; | |
237 } | |
238 | |
239 bool InterpretCCData(DictionaryValue* node, CCNode* c) { | |
240 if (!VerifyDictionaryEntry(node, "vertex_shader", Value::TYPE_STRING) || | |
241 !VerifyDictionaryEntry(node, "fragment_shader", Value::TYPE_STRING) || | |
242 !VerifyDictionaryEntry(node, "textures", Value::TYPE_LIST)) { | |
243 return false; | |
244 } | |
245 string vertex_shader_name, fragment_shader_name; | |
246 node->GetString("vertex_shader", &vertex_shader_name); | |
247 node->GetString("fragment_shader", &fragment_shader_name); | |
248 | |
249 c->set_vertex_shader(ShaderIDFromString(vertex_shader_name)); | |
250 c->set_fragment_shader(ShaderIDFromString(fragment_shader_name)); | |
251 ListValue* textures; | |
252 node->GetList("textures", &textures); | |
253 for (unsigned int i = 0; i < textures->GetSize(); ++i) { | |
254 if (!VerifyListEntry(textures, i, Value::TYPE_DICTIONARY, "Tex list")) | |
255 return false; | |
256 DictionaryValue* tex; | |
257 textures->GetDictionary(i, &tex); | |
258 | |
259 if (!VerifyDictionaryEntry(tex, "texID", Value::TYPE_INTEGER) || | |
260 !VerifyDictionaryEntry(tex, "height", Value::TYPE_INTEGER) || | |
261 !VerifyDictionaryEntry(tex, "width", Value::TYPE_INTEGER) || | |
262 !VerifyDictionaryEntry(tex, "format", Value::TYPE_STRING)) { | |
263 return false; | |
264 } | |
265 Texture t; | |
266 tex->GetInteger("texID", &t.texID); | |
267 tex->GetInteger("height", &t.height); | |
268 tex->GetInteger("width", &t.width); | |
269 | |
270 string formatName; | |
271 tex->GetString("format", &formatName); | |
272 t.format = TextureFormatFromString(formatName); | |
273 if (t.format == GL_INVALID_ENUM) { | |
274 LOG(ERROR) << "Unrecognized texture format in layer " << c->layerID() << | |
275 " (format: " << formatName << ")\n" | |
276 "The layer had " << textures->GetSize() << " children."; | |
277 return false; | |
278 } | |
279 | |
280 c->add_texture(t); | |
281 } | |
282 | |
283 if (c->vertex_shader() == SHADER_UNRECOGNIZED) { | |
284 LOG(ERROR) << "Unrecognized vertex shader name, layer " << c->layerID() << | |
285 " (shader: " << vertex_shader_name << ")"; | |
286 return false; | |
287 } | |
288 | |
289 if (c->fragment_shader() == SHADER_UNRECOGNIZED) { | |
290 LOG(ERROR) << "Unrecognized fragment shader name, layer " << c->layerID() << | |
291 " (shader: " << fragment_shader_name << ")"; | |
292 return false; | |
293 } | |
294 | |
295 return true; | |
296 } | |
297 | |
298 RenderNode* InterpretContentLayer(DictionaryValue* node) { | |
299 ContentLayerNode* n = new ContentLayerNode; | |
300 if (!InterpretCommonContents(node, n)) | |
301 return NULL; | |
302 | |
303 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING) || | |
304 !VerifyDictionaryEntry(node, "skipsDraw", Value::TYPE_BOOLEAN) || | |
305 !VerifyDictionaryEntry(node, "children", Value::TYPE_LIST)) { | |
306 return false; | |
307 } | |
308 | |
309 string type; | |
310 node->GetString("type", &type); | |
311 DCHECK_EQ(type, "ContentLayer"); | |
312 bool skipsDraw; | |
313 node->GetBoolean("skipsDraw", &skipsDraw); | |
314 n->set_skipsDraw(skipsDraw); | |
315 | |
316 ListValue* children; | |
317 node->GetList("children", &children); | |
318 for (unsigned int i = 0; i < children->GetSize(); ++i) { | |
319 DictionaryValue* childNode; | |
320 children->GetDictionary(i, &childNode); | |
321 RenderNode* child = InterpretNode(childNode); | |
322 if (child) | |
323 n->add_child(child); | |
324 } | |
325 | |
326 return n; | |
327 } | |
328 | |
329 RenderNode* InterpretCanvasLayer(DictionaryValue* node) { | |
330 CCNode* n = new CCNode; | |
331 if (!InterpretCommonContents(node, n)) | |
332 return NULL; | |
333 | |
334 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
335 return NULL; | |
336 } | |
337 | |
338 string type; | |
339 node->GetString("type", &type); | |
340 assert(type == "CanvasLayer"); | |
341 | |
342 if (!InterpretCCData(node, n)) | |
343 return NULL; | |
344 | |
345 return n; | |
346 } | |
347 | |
348 RenderNode* InterpretVideoLayer(DictionaryValue* node) { | |
349 CCNode* n = new CCNode; | |
350 if (!InterpretCommonContents(node, n)) | |
351 return NULL; | |
352 | |
353 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
354 return NULL; | |
355 } | |
356 | |
357 string type; | |
358 node->GetString("type", &type); | |
359 assert(type == "VideoLayer"); | |
360 | |
361 if (!InterpretCCData(node, n)) | |
362 return NULL; | |
363 | |
364 return n; | |
365 } | |
366 | |
367 RenderNode* InterpretImageLayer(DictionaryValue* node) { | |
368 CCNode* n = new CCNode; | |
369 if (!InterpretCommonContents(node, n)) | |
370 return NULL; | |
371 | |
372 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
373 return NULL; | |
374 } | |
375 | |
376 string type; | |
377 node->GetString("type", &type); | |
378 assert(type == "ImageLayer"); | |
379 | |
380 if (!InterpretCCData(node, n)) | |
381 return NULL; | |
382 | |
383 return n; | |
384 } | |
385 | |
386 RenderNode* InterpretNode(DictionaryValue* node) { | |
387 if (!VerifyDictionaryEntry(node, "type", Value::TYPE_STRING)) { | |
388 return NULL; | |
389 } | |
390 | |
391 string type; | |
392 node->GetString("type", &type); | |
393 if (type == "ContentLayer") | |
394 return InterpretContentLayer(node); | |
395 if (type == "CanvasLayer") | |
396 return InterpretCanvasLayer(node); | |
397 if (type == "VideoLayer") | |
398 return InterpretVideoLayer(node); | |
399 if (type == "ImageLayer") | |
400 return InterpretImageLayer(node); | |
401 | |
402 | |
403 string outjson; | |
404 JSONWriter::Write(node, true, &outjson); | |
405 LOG(ERROR) << "Unrecognized node type! JSON:\n\n" | |
406 "-----------------------\n" << | |
407 outjson << | |
408 "-----------------------"; | |
409 | |
410 return NULL; | |
411 } | |
412 | |
413 RenderNode* BuildRenderTreeFromFile(const FilePath& path) { | |
414 LOG(INFO) << "Reading " << path.LossyDisplayName(); | |
415 string contents; | |
416 if (!ReadFileToString(path, &contents)) | |
417 return NULL; | |
418 | |
419 scoped_ptr<Value> root; | |
420 int error_code = 0; | |
421 string error_message; | |
422 root.reset(JSONReader::ReadAndReturnError(contents, | |
423 true, | |
424 &error_code, | |
425 &error_message)); | |
426 if (!root.get()) { | |
427 LOG(ERROR) << "Failed to parse JSON file " << path.LossyDisplayName() << | |
428 "\n(" << error_message << ")"; | |
429 return NULL; | |
430 } | |
431 | |
432 if (root->IsType(Value::TYPE_DICTIONARY)) { | |
433 DictionaryValue* v = static_cast<DictionaryValue*>(root.get()); | |
434 RenderNode* tree = InterpretContentLayer(v); | |
435 return tree; | |
436 } else { | |
437 LOG(ERROR) << path.LossyDisplayName() << | |
438 " doesn not encode a JSON dictionary."; | |
439 return NULL; | |
440 } | |
441 } | |
442 | |
OLD | NEW |