1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/base.html">
9<link rel="import" href="/tracing/extras/chrome/cc/raster_task.html">
10<link rel="import" href="/tracing/model/event_set.html">
11<link rel="import" href="/tracing/ui/analysis/single_event_sub_view.html">
12<link rel="import" href="/tracing/ui/extras/chrome/cc/selection.html">
13<link rel="import" href="/tracing/ui/extras/chrome/cc/raster_task_view.html">
14
15<script>
16'use strict';
17
18tr.exportTo('tr.ui.e.chrome.cc', function() {
19  /**
20   * @constructor
21   */
22  function RasterTaskSelection(selection) {
23    tr.ui.e.chrome.cc.Selection.call(this);
24    var whySupported = RasterTaskSelection.whySuported(selection);
25    if (!whySupported.ok)
26      throw new Error('Fail: ' + whySupported.why);
27    this.slices_ = tr.b.asArray(selection);
28    this.tiles_ = this.slices_.map(function(slice) {
29      var tile = tr.e.cc.getTileFromRasterTaskSlice(slice);
30      if (tile === undefined)
31        throw new Error('This should never happen due to .supports check.');
32      return tile;
33    });
34  }
35  RasterTaskSelection.whySuported = function(selection) {
36    if (!(selection instanceof tr.model.EventSet))
37      return {ok: false, why: 'Must be selection'};
38
39    if (selection.length === 0)
40      return {ok: false, why: 'Selection must be non empty'};
41
42    var tile0;
43    for (var i = 0; i < selection.length; i++) {
44      var event = selection[i];
45      if (!(event instanceof tr.model.Slice))
46        return {ok: false, why: 'Not a slice'};
47
48      var tile = tr.e.cc.getTileFromRasterTaskSlice(selection[i]);
49      if (tile === undefined)
50        return {ok: false, why: 'No tile found'};
51
52      if (i === 0) {
53        tile0 = tile;
54      } else {
55        if (tile.containingSnapshot != tile0.containingSnapshot) {
56          return {
57            ok: false,
58            why: 'Raster tasks are from different compositor instances'
59          };
60        }
61      }
62    }
63    return {ok: true};
64  }
65
66  RasterTaskSelection.supports = function(selection) {
67    return RasterTaskSelection.whySuported(selection).ok;
68  };
69
70  RasterTaskSelection.prototype = {
71    __proto__: tr.ui.e.chrome.cc.Selection.prototype,
72
73    get specicifity() {
74      return 3;
75    },
76
77    get associatedLayerId() {
78      var tile0 = this.tiles_[0];
79      var allSameLayer = this.tiles_.every(function(tile) {
80        tile.layerId == tile0.layerId;
81      });
82      if (allSameLayer)
83        return tile0.layerId;
84      return undefined;
85    },
86
87    get extraHighlightsByLayerId() {
88      var highlights = {};
89      this.tiles_.forEach(function(tile, i) {
90        if (highlights[tile.layerId] === undefined)
91          highlights[tile.layerId] = [];
92        var slice = this.slices_[i];
93        highlights[tile.layerId].push({
94          colorKey: slice.title,
95          rect: tile.layerRect
96        });
97      }, this);
98      return highlights;
99    },
100
101    createAnalysis: function() {
102      var sel = new tr.model.EventSet();
103      this.slices_.forEach(function(slice) {
104        sel.push(slice);
105      });
106
107      var analysis;
108      if (sel.length == 1)
109        analysis = document.createElement('tr-ui-a-single-event-sub-view');
110      else
111        analysis = document.createElement('tr-ui-e-chrome-cc-raster-task-view');
112      analysis.selection = sel;
113      return analysis;
114    },
115
116    findEquivalent: function(lthi) {
117      // Raster tasks are only valid in one LTHI.
118      return undefined;
119    },
120
121    // RasterTaskSelection specific stuff follows.
122    get containingSnapshot() {
123      return this.tiles_[0].containingSnapshot;
124    }
125  };
126
127  return {
128    RasterTaskSelection: RasterTaskSelection
129  };
130});
131</script>
132