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('base.bbox2');
8 base.require('model.constants');
9 base.require('model.tile');
10 base.require('model.layer_tree_impl');
12 base.exportTo('ccfv.model', function() {
14 var constants = ccfv.model.constants;
17 * Represents the history of a specific cc::LayerTreeHostImpl over time.
21 function LayerTreeHostImplHistory(id) {
23 this.lthiSnapshots = [];
25 this.allTileHistories_ = {};
26 this.allTilesBBox_ = undefined;
28 this.allLayerImplHistories_ = {};
31 LayerTreeHostImplHistory.prototype = {
32 createNewLTHI: function() {
33 var snapshotNumber = this.lthiSnapshots.length + 1;
34 var lthi = new LayerTreeHostImpl(this, snapshotNumber);
35 this.lthiSnapshots.push(lthi);
36 this.allTilesBBox_ = undefined;
37 this.allContentsScales_ = undefined;
41 getOrCreateTileHistory: function(tileID) {
42 if (!this.allTileHistories_[tileID])
43 this.allTileHistories_[tileID] = new ccfv.model.TileHistory(tileID);
44 return this.allTileHistories_[tileID];
47 getOrCreateLayerImplHistory: function(layerID) {
48 if (!this.allLayerImplHistories_[layerID])
49 this.allLayerImplHistories_[layerID] =
50 new ccfv.model.LayerImplHistory(layerID);
51 return this.allLayerImplHistories_[layerID];
54 get allContentsScales() {
55 if (this.allContentsScales_ !== undefined)
56 return this.allContentsScales_;
59 for (var tileID in this.allTileHistories_) {
60 var tileHistory = this.allTileHistories_[tileID];
61 scales[tileHistory.contentsScale] = true;
63 this.allContentsScales_ = base.dictionaryKeys(scales);
64 return this.allContentsScales_;
68 if (!this.allTilesBBox_) {
69 var bbox = new base.BBox2();
70 for (var tileID in this.allTileHistories_) {
71 var tileHistory = this.allTileHistories_[tileID];
72 for (var snapshotNumber in tileHistory.tilesBySnapshotNumber) {
73 var tile = tileHistory.tilesBySnapshotNumber[snapshotNumber];
74 if (tile.priority[0].current_screen_quad)
75 bbox.addQuad(tile.priority[0].current_screen_quad);
76 if (tile.priority[1].current_screen_quad)
77 bbox.addQuad(tile.priority[1].current_screen_quad);
80 this.allTilesBBox_ = bbox;
82 return this.allTilesBBox_;
87 * Represents a snapshot of the cc::LayerTreeHostImpl at an instant in time.
91 function LayerTreeHostImpl(history, snapshotNumber) {
92 this.history = history;
93 this.snapshotNumber = snapshotNumber;
94 this.deviceViewportSize = {}
98 // These fields are affected by the rebuildIfNeeded_() flow.
99 this.pendingTree = new ccfv.model.LayerTreeImpl(
100 this, constants.PENDING_TREE);
101 this.activeTree = new ccfv.model.LayerTreeImpl(
102 this, constants.ACTIVE_TREE);
104 this.tilesForTree_ = undefined;
105 this.inactiveTiles_ = [];
108 LayerTreeHostImpl.prototype = {
110 return this.history.id;
113 getTree: function(whichTree) {
114 if (whichTree === constants.ACTIVE_TREE)
115 return this.activeTree;
116 else if (whichTree === constants.PENDING_TREE)
117 return this.pendingTree;
119 throw new Error('Active or pending only.');
122 getOrCreateTile: function(tileID) {
123 var tileHistory = this.history.getOrCreateTileHistory(tileID);
124 var tile = tileHistory.getOrCreateTileForLTHI(this);
125 this.allTiles.push(tile);
127 this.activeTree.setTilesDirty();
128 this.pendingTree.setTilesDirty();
129 this.tilesForTree_ = undefined;
130 this.inactiveTiles_ = undefined;
135 get inactiveTiles() {
136 if (this.inactiveTiles_ === undefined)
137 this.rebuildTiles_();
138 return this.inactiveTiles_;
141 getTilesForTree: function(which_tree) {
142 if (this.tilesForTree_ === undefined)
143 this.rebuildTiles_();
144 return this.tilesForTree_[which_tree];
147 rebuildTiles_: function() {
148 this.tilesForTree_ = [[], []];
149 this.inactiveTiles_ = [];
150 this.allTiles.forEach(function(tile) {
151 if (tile.priority[constants.ACTIVE_TREE].is_live)
152 this.tilesForTree_[constants.ACTIVE_TREE].push(tile);
153 if (tile.priority[constants.PENDING_TREE].is_live)
154 this.tilesForTree_[constants.PENDING_TREE].push(tile);
155 if (tile.priority[constants.PENDING_TREE].is_live == false &&
156 tile.priority[constants.ACTIVE_TREE].is_live == false)
157 this.inactiveTiles_.push(tile);
163 LayerTreeHostImpl: LayerTreeHostImpl,
164 LayerTreeHostImplHistory: LayerTreeHostImplHistory