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.
7 base.require('model.layer_tree_host_impl');
9 base.exportTo('ccfv', function() {
11 var constants = ccfv.model.constants;
13 function unquoteIfNeeded(val) {
14 if (typeof val !== 'string')
16 if (val[0] == '{' && val[val.length - 1] == '}') {
17 return JSON.parse(val);
24 * A generic container for data from a cc trace.
29 this.lthiHistories = {};
32 getOrCreateLTHIHistory: function(id) {
33 if (this.lthiHistories[id] === undefined)
34 this.lthiHistories[id] = new ccfv.model.LayerTreeHostImplHistory(id);
35 return this.lthiHistories[id];
38 initFromTraceEvents: function(trace) {
39 var importer = new TraceImporter(this);
40 importer.importTrace(trace);
43 initFromFrameData: function(frameData) {
44 var importer = new TraceImporter(this);
45 importer.importFrameData(frameData);
49 function TraceImporter(model) {
53 TraceImporter.prototype = {
54 addWarning: function(msg) {},
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') {
63 'Expected Frame to have args.frame of type string.');
65 var frameData = unquoteIfNeeded(event.args.frame);
66 this.importFrameData(frameData);
71 importFrameData: function(frameData) {
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;
80 lthiID = frameData.lthi_id;
83 var lthiHistory = this.model.getOrCreateLTHIHistory(lthiID);
85 var lthi = lthiHistory.createNewLTHI();
88 if (frameData.device_viewport_size === undefined)
89 throw new Error('Expected device_viewport');
90 lthi.deviceViewportSize = frameData.device_viewport_size;
93 if (frameData.tiles === undefined)
94 throw new Error('Expected tiles');
95 frameData.tiles.forEach(function(tile) {
96 this.handleFrameTile(lthi, tile);
100 var activeTreeLayers = frameData.active_tree || [];
101 activeTreeLayers.forEach(function(layerImpl) {
102 this.handleLayerImpl(lthi, constants.ACTIVE_TREE, layerImpl);
106 handleFrameTile: function(lthi, tileData) {
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;
114 tileID = tileData.id;
116 var tile = lthi.getOrCreateTile(tileID);
118 tile.history.layerID = tileData.layer_id;
119 tile.history.picturePile = tileData.picture_pile;
120 tile.history.contentsScale = tileData.contents_scale;
122 tile.priority[0] = tileData.priority[0];
123 tile.priority[1] = tileData.priority[1];
124 tile.managedState = tileData.managed_state;
127 handleLayerImpl: function(lthi, whichTree, layerImplData) {
129 if (!layerImplData.id)
130 throw new Error('LayerImpls must have id');
131 layerID = layerImplData.id;
133 var layerImpl = lthi.getTree(whichTree).getOrCreateLayerImpl(layerID);
134 for (var k in layerImplData)
135 layerImpl.args[k] = layerImplData[k];