OLD | NEW |
| (Empty) |
1 /* Copyright (c) 2012 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 'use strict'; | |
6 | |
7 base.require('model.layer_tree_host_impl'); | |
8 | |
9 base.exportTo('ccfv', function() { | |
10 | |
11 var constants = ccfv.model.constants; | |
12 | |
13 function unquoteIfNeeded(val) { | |
14 if (typeof val !== 'string') | |
15 return val; | |
16 if (val[0] == '{' && val[val.length - 1] == '}') { | |
17 return JSON.parse(val); | |
18 } else { | |
19 return val; | |
20 } | |
21 } | |
22 | |
23 /** | |
24 * A generic container for data from a cc trace. | |
25 * | |
26 * @constructor | |
27 */ | |
28 function Model() { | |
29 this.lthiHistories = {}; | |
30 } | |
31 Model.prototype = { | |
32 getOrCreateLTHIHistory: function(id) { | |
33 if (this.lthiHistories[id] === undefined) | |
34 this.lthiHistories[id] = new ccfv.model.LayerTreeHostImplHistory(id); | |
35 return this.lthiHistories[id]; | |
36 }, | |
37 | |
38 initFromTraceEvents: function(trace) { | |
39 var importer = new TraceImporter(this); | |
40 importer.importTrace(trace); | |
41 }, | |
42 | |
43 initFromFrameData: function(frameData) { | |
44 var importer = new TraceImporter(this); | |
45 importer.importFrameData(frameData); | |
46 } | |
47 }; | |
48 | |
49 function TraceImporter(model) { | |
50 this.model = model; | |
51 }; | |
52 | |
53 TraceImporter.prototype = { | |
54 addWarning: function(msg) {}, | |
55 | |
56 importTrace: function(trace) { | |
57 var events = trace.traceEvents; | |
58 for (var i = 0; i < events.length; i++) { | |
59 var event = events[i]; | |
60 if (event.name == 'Frame') { | |
61 if (event.args.frame === 'undefined') { | |
62 throw new Error( | |
63 'Expected Frame to have args.frame of type string.'); | |
64 } | |
65 var frameData = unquoteIfNeeded(event.args.frame); | |
66 this.importFrameData(frameData); | |
67 } | |
68 } | |
69 }, | |
70 | |
71 importFrameData: function(frameData) { | |
72 | |
73 var lthiID; | |
74 if (frameData.lthi_id === undefined) { | |
75 // Old versions used compositor_instance instead of lthiID. | |
76 if (frameData.compositor_instance === undefined) | |
77 throw new Error('Expected Frame to have a lthi_id field.'); | |
78 lthiID = frameData.compositor_instance; | |
79 } else { | |
80 lthiID = frameData.lthi_id; | |
81 } | |
82 | |
83 var lthiHistory = this.model.getOrCreateLTHIHistory(lthiID); | |
84 | |
85 var lthi = lthiHistory.createNewLTHI(); | |
86 | |
87 // Basic properties. | |
88 if (frameData.device_viewport_size === undefined) | |
89 throw new Error('Expected device_viewport'); | |
90 lthi.deviceViewportSize = frameData.device_viewport_size; | |
91 | |
92 // Tiles. | |
93 if (frameData.tiles === undefined) | |
94 throw new Error('Expected tiles'); | |
95 frameData.tiles.forEach(function(tile) { | |
96 this.handleFrameTile(lthi, tile); | |
97 }, this); | |
98 | |
99 // Layers | |
100 var activeTreeLayers = frameData.active_tree || []; | |
101 activeTreeLayers.forEach(function(layerImpl) { | |
102 this.handleLayerImpl(lthi, constants.ACTIVE_TREE, layerImpl); | |
103 }, this); | |
104 }, | |
105 | |
106 handleFrameTile: function(lthi, tileData) { | |
107 var tileID; | |
108 if (!tileData.id) { | |
109 // Some old files dont have id fields. Use picture_pile for backup id. | |
110 if (!tileData.picture_pile) | |
111 throw new Error('Tiles must have id'); | |
112 tileID = tileData.picture_pile; | |
113 } else { | |
114 tileID = tileData.id; | |
115 } | |
116 var tile = lthi.getOrCreateTile(tileID); | |
117 | |
118 tile.history.layerID = tileData.layer_id; | |
119 tile.history.picturePile = tileData.picture_pile; | |
120 tile.history.contentsScale = tileData.contents_scale; | |
121 | |
122 tile.priority[0] = tileData.priority[0]; | |
123 tile.priority[1] = tileData.priority[1]; | |
124 tile.managedState = tileData.managed_state; | |
125 }, | |
126 | |
127 handleLayerImpl: function(lthi, whichTree, layerImplData) { | |
128 var layerID; | |
129 if (!layerImplData.id) | |
130 throw new Error('LayerImpls must have id'); | |
131 layerID = layerImplData.id; | |
132 | |
133 var layerImpl = lthi.getTree(whichTree).getOrCreateLayerImpl(layerID); | |
134 for (var k in layerImplData) | |
135 layerImpl.args[k] = layerImplData[k]; | |
136 } | |
137 }; | |
138 | |
139 | |
140 return { | |
141 Model: Model, | |
142 } | |
143 }); | |
144 | |
OLD | NEW |