summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/extras/vsync/vsync_auditor.html
blob: 2d450a953ccb865aaea4574ccf69b1d89206c21d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/auditor.html">

<script>
'use strict';

function filterDuplicateTimestamps(timestamps) {
  var dedupedTimestamps = [];
  var lastTs = 0;
  for (var ts of timestamps) {
    if (ts - lastTs >= 1) {
      dedupedTimestamps.push(ts);
      lastTs = ts;
    }
  }
  return dedupedTimestamps;
}

tr.exportTo('tr.e.audits', function() {

  var VSYNC_COUNTER_PRECISIONS = {
    // Android. Some versions have VSYNC split out into VSYNC-app and VSYNC-sf.
    // Requires "gfx" systrace category to be enabled.
    'android.VSYNC-app': 15,
    'android.VSYNC': 15
  };

  var VSYNC_SLICE_PRECISIONS = {
    // Android.
    'RenderWidgetHostViewAndroid::OnVSync': 5,
    // Android. Very precise. Requires "gfx" systrace category to be enabled.
    'VSYNC': 10,
    // Linux. Very precise. Requires "gpu" tracing category to be enabled.
    'vblank': 10,
    // Mac. Derived from a Mac callback (CVDisplayLinkSetOutputCallback).
    'DisplayLinkMac::GetVSyncParameters': 5
  };

  var BEGIN_FRAME_SLICE_PRECISION = {
    'DisplayScheduler::BeginFrame': 10
  };

  /**
   * Auditor that analyzes the model and, if possible, adds data to it
   * indicating when vertical sync events took place.
   *
   * @constructor
   * @extends {tr.c.Auditor}
   */
  function VSyncAuditor(model) {
    tr.c.Auditor.call(this, model);
  };

  VSyncAuditor.prototype = {
    __proto__: tr.c.Auditor.prototype,

    runAnnotate: function() {
      this.model.device.vSyncTimestamps = this.findVSyncTimestamps(this.model);
    },

    /**
     * Returns an array of the most accurate VSync times available in the model.
     */
    findVSyncTimestamps: function(model) {
      var times = [];

      // Only keep the most precise VSync data.
      var maxPrecision = Number.NEGATIVE_INFINITY;
      var maxTitle = undefined;

      function useInstead(title, precisions) {
        var precision = precisions[title];
        if (precision === undefined)
          return false;

        if (title === maxTitle)
          return true;

        if (precision <= maxPrecision) {
          if (precision === maxPrecision) {
            console.warn('Encountered two different VSync events (' +
                maxTitle + ', ' + title + ') with the same precision, ' +
               'ignoring the newer one (' + title + ')');
          }
          return false;
        }
        maxPrecision = precision;
        maxTitle = title;
        times = [];

        return true;
      }

      for (var pid in model.processes) {
        var process = model.processes[pid];
        // Traverse process counters.
        for (var cid in process.counters) {
          if (useInstead(cid, VSYNC_COUNTER_PRECISIONS)) {
            var counter = process.counters[cid];
            for (var i = 0; i < counter.series.length; i++) {
              var series = counter.series[i];
              Array.prototype.push.apply(times, series.timestamps);
            }
          }
        }

        // Traverse thread slices.
        for (var tid in process.threads) {
          var thread = process.threads[tid];
          for (var i = 0; i < thread.sliceGroup.slices.length; i++) {
            var slice = thread.sliceGroup.slices[i];
            if (useInstead(slice.title, VSYNC_SLICE_PRECISIONS))
              times.push(slice.start);
            // We need to check not only that we have a Scheduler::BeginFrame
            // event, but also that we have one that has a frame time associated
            // with it. Older versions of Scheduler::BeginFrame don't have one.
            else if (useInstead(slice.title, BEGIN_FRAME_SLICE_PRECISION) &&
                slice.args.args && slice.args.args.frame_time_us)
              times.push(slice.args.args.frame_time_us / 1000.0);
          }
        }
      }
      times.sort(function(x, y) { return x - y; });
      return filterDuplicateTimestamps(times);
    }
  };

  tr.c.Auditor.register(VSyncAuditor);

  return {
    VSyncAuditor: VSyncAuditor
  };
});
</script>