summaryrefslogtreecommitdiffstats
path: root/devtools/server/primitive.js
blob: 45346bc78072cf79c48bf1e438037b4827bc2769 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { on, once, off, emit } = require("sdk/event/core");
const { Class } = require("sdk/core/heritage");

const WebGLPrimitivesType = {
  "POINTS": 0,
  "LINES": 1,
  "LINE_LOOP": 2,
  "LINE_STRIP": 3,
  "TRIANGLES": 4,
  "TRIANGLE_STRIP": 5,
  "TRIANGLE_FAN": 6
};

/**
 * A utility for monitoring WebGL primitive draws. Takes a `tabActor`
 * and monitors primitive draws over time.
 */
const WebGLDrawArrays = "drawArrays";
const WebGLDrawElements = "drawElements";

var WebGLPrimitiveCounter = exports.WebGLPrimitiveCounter = Class({
  initialize: function (tabActor) {
    this.tabActor = tabActor;
  },

  destroy: function () {
    this.stopRecording();
  },

  /**
   * Starts monitoring primitive draws, storing the primitives count per tick.
   */
  resetCounts: function () {
    this._tris = 0;
    this._vertices = 0;
    this._points = 0;
    this._lines = 0;
    this._startTime = this.tabActor.docShell.now();
  },

  /**
   * Stops monitoring primitive draws, returning the recorded values.
   */
  getCounts: function () {
    var result = {
      tris: this._tris,
      vertices: this._vertices,
      points: this._points,
      lines: this._lines
    };

    this._tris = 0;
    this._vertices = 0;
    this._points = 0;
    this._lines = 0;
    return result;
  },

  /**
   * Handles WebGL draw primitive functions to catch primitive info.
   */
  handleDrawPrimitive: function (functionCall) {
    let { name, args } = functionCall.details;

    if (name === WebGLDrawArrays) {
      this._processDrawArrays(args);
    } else if (name === WebGLDrawElements) {
      this._processDrawElements(args);
    }
  },

  /**
   * Processes WebGL drawArrays method to count primitve numbers
   */
  _processDrawArrays: function (args) {
    let mode = args[0];
    let count = args[2];

    switch (mode) {
      case WebGLPrimitivesType.POINTS:
        this._vertices += count;
        this._points += count;
        break;
      case WebGLPrimitivesType.LINES:
        this._vertices += count;
        this._lines += (count / 2);
        break;
      case WebGLPrimitivesType.LINE_LOOP:
        this._vertices += count;
        this._lines += count;
        break;
      case WebGLPrimitivesType.LINE_STRIP:
        this._vertices += count;
        this._lines += (count - 1);
        break;
      case WebGLPrimitivesType.TRIANGLES:
        this._tris += (count / 3);
        this._vertices += count;
        break;
      case WebGLPrimitivesType.TRIANGLE_STRIP:
        this._tris += (count - 2);
        this._vertices += count;
        break;
      case WebGLPrimitivesType.TRIANGLE_FAN:
        this._tris += (count - 2);
        this._vertices += count;
        break;
      default:
        console.error("_processDrawArrays doesn't define this type.");
        break;
    }
  },

  /**
   * Processes WebGL drawElements method to count primitve numbers
   */
  _processDrawElements: function (args) {
    let mode = args[0];
    let count = args[1];

    switch (mode) {
      case WebGLPrimitivesType.POINTS:
        this._vertices += count;
        this._points += count;
        break;
      case WebGLPrimitivesType.LINES:
        this._vertices += count;
        this._lines += (count / 2);
        break;
      case WebGLPrimitivesType.LINE_LOOP:
        this._vertices += count;
        this._lines += count;
        break;
      case WebGLPrimitivesType.LINE_STRIP:
        this._vertices += count;
        this._lines += (count - 1);
        break;
      case WebGLPrimitivesType.TRIANGLES:
        let tris = count / 3;
        let vertex = tris * 3;

        if (tris > 1) {
          vertex = tris * 2;
        }
        this._tris += tris;
        this._vertices += vertex;
        break;
      case WebGLPrimitivesType.TRIANGLE_STRIP:
        this._tris += (count - 2);
        this._vertices += count;
        break;
      case WebGLPrimitivesType.TRIANGLE_FAN:
        this._tris += (count - 2);
        this._vertices += count;
      default:
        console.error("_processDrawElements doesn't define this type.");
        break;
    }
  }
});