File: filter.js

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (281 lines) | stat: -rw-r--r-- 7,863 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Represents a source for a draw call, or a log, etc.
//
class Source {
  static instances = [];

  constructor(json) {
    this.file_ = json.file;
    this.func_ = json.func;
    this.line_ = json.line;
    this.anno_ = json.anno;
    const index = parseInt(json.index);
    Source.instances[index] = this;
  }

  get file() { return this.file_; }
  get func() { return this.func_; }
  get anno() { return this.anno_; }
};

// Represents a draw call.
// This is currently only used for drawing rects and positional text.

class DrawCall {
  constructor(json) {
    // e.g. {"drawindex":"44", option":{"alpha":"1.000000","color":"#ffffff"}
    // ,"pos":"0.000000,763.000000","size":"255x5","source_index":"0",
    // "thread_id":"123456"}
    this.sourceIndex_ = parseInt(json.source_index);
    this.drawIndex_ = parseInt(json.drawindex);
    this.threadId_ = parseInt(json.thread_id);
    this.text = json.text;
    this.size_ = {
      width: json.size[0],
      height: json.size[1],
    };

    this.pos_ = {
      x: json.pos[0],
      y: json.pos[1],
    };
    if (json.option) {
      this.color_ = json.option.color;
      this.alpha_ = DrawCall.alphaIntToHex(json.option.alpha)
    }
    this.buffer_id = json.buff_id;
    if (json.uv_size && json.uv_pos) {
      this.uv_size = {
        width: json.uv_size[0],
        height: json.uv_size[1],
      };
      this.uv_pos = {
        x: json.uv_pos[0],
        y: json.uv_pos[1],
      };
    }
    else {
      this.uv_size = {
        width: 1.0,
        height: 1.0,
      };
      this.uv_pos = {
        x: 0.0,
        y: 0.0,
      };
    }
  }

  // Used in conversion of Json.
  static alphaIntToHex(value) {
    value = Math.trunc(value);
    value = Math.max(0, Math.min(value, 255));
    return value.toString(16).padStart(2, '0');
  }

  // Used internally to convert from UI filter to hex.
  static alphaFloatToHex(value) {
    value = Math.trunc(value * 255);
    value = Math.max(0, Math.min(value, 255));
    return value.toString(16).padStart(2, '0');
  }

  draw(context, buffer_map, threadConfig) {
    let filter = undefined;
    const filters = Filter.enabledInstances();
    // TODO: multiple filters can match the same draw call. For now, let's just
    // pick the earliest filter that matches, and let it decide what to do.
    for (const f of filters) {
      if (f.matches(Source.instances[this.sourceIndex_])) {
        filter = f;
        break;
      }
    }

    var color;
    var alpha;
    // If thread drawing is overriding filters.
    if (threadConfig.overrideFilters) {
      color = threadConfig.threadColor;
      alpha = threadConfig.threadAlpha;
    }
    // Otherwise, follow filter draw options.
    else {
      // No filters match this draw. So skip.
      if (!filter) return;
      if (!filter.shouldDraw) return;

      color = (filter && filter.drawColor) ? filter.drawColor : this.color_
      alpha = (filter && filter.fillAlpha) ?
      DrawCall.alphaFloatToHex(parseFloat(filter.fillAlpha) / 100) :
                                                              this.alpha_;
    }

    if (color && alpha) {
      context.fillStyle = color + alpha;
      context.fillRect(this.pos_.x,
                       this.pos_.y,
                       this.size_.width,
                       this.size_.height);
    }

    context.strokeStyle = color;
    context.strokeRect(this.pos_.x,
                       this.pos_.y,
                       this.size_.width,
                       this.size_.height);
    var buff_id = this.buffer_id.toString();
    if(buffer_map[buff_id]) {
      var buff_width = buffer_map[buff_id].width;
      var buff_height = buffer_map[buff_id].height;
      context.drawImage(buffer_map[buff_id],
                        this.uv_pos.x * buff_width,
                        this.uv_pos.y * buff_height,
                        this.uv_size.width * buff_width,
                        this.uv_size.height * buff_height,
                        this.pos_.x,
                        this.pos_.y,
                        this.size_.width,
                        this.size_.height);
    }
  }
};


// Represents a filter for draw calls. A filter specifies a selector (e.g.
// filename, and/or function name), and the action to take (e.g. skip draw, or
// color to use for draw, etc.) if the filter matches.
class Filter {
  static instances = [];

  constructor(enabled, selector, action, index) {
    this.selector_ = {
      filename: selector.filename,
      func: selector.func,
      anno: selector.anno,
    };

    // XXX: If there are multiple selectors that apply to the same draw, then
    // I guess the newest filter will take effect.
    this.action_ = {
      skipDraw: action.skipDraw,
      color: action.color,
      alpha: action.alpha,
    };

    this.enabled_ = enabled;

    if (index === undefined) {
      Filter.instances.push(this);
      this.index_ = Filter.instances.length - 1;
    }
    else {
      Filter.instances[index] = this;
      this.index_ = index;
    }
  }

  get enabled() { return this.enabled_; }
  set enabled(e) { this.enabled_ = e; }

  get file() { return this.selector_.filename || ""; }
  get func() { return this.selector_.func || ""; }
  get anno() { return this.selector_.anno || "" };

  get shouldDraw() { return !this.action_.skipDraw; }
   // undefined if using caller color
  get drawColor() { return this.action_.color; }
  // undefined if using caller alpha
  get fillAlpha() { return this.action_.alpha; }

  get index() { return this.index_; }

  get streamFilter() {
    return {
      selector: {
        file: this.selector_.filename,
        func: this.selector_.func,
        anno: this.selector_.anno
      },
      active: !this.action_.skipDraw,
      enabled: this.enabled_
    }
  }

  matches(source) {
    if (!(source instanceof Source)) return false;
    if (!this.enabled) return false;

    if (this.selector_.filename) {
      const m = source.file.search(this.selector_.filename);
      if (m == -1) return false;
    }

    if (this.selector_.func) {
      const m = source.func.search(this.selector_.func);
      if (m == -1) return false;
    }

    if (this.selector_.anno) {
      const m = source.anno.search(this.selector_.anno);
      if (m == -1) return false;
    }

    return true;
  }

  createUIString() {
    let str = '';
    if (this.selector_.filename) {
      const parts = this.selector_.filename.split('/');
      str += ` <i class="material-icons-outlined md-18">
      text_snippet</i>${parts[parts.length - 1]}`;
    }
    if (this.selector_.func) {
      str += ` <i class="material-icons-outlined md-18">
      code</i>${this.selector_.func}`;
    }
    if (this.selector_.anno) {
      str += ` <i class="material-icons-outlined md-18">
      message</i>${this.selector_.anno}`;
    }
    return str;
  }

  static enabledInstances() {
    return Filter.instances.filter(f => f.enabled);
  }

  static getFilter(index) {
    return Filter.instances[index];
  }

  static swapFilters(indexA, indexB) {
    var filterA = Filter.instances[indexA];
    var filterB = Filter.instances[indexB];
    filterA.index_ = indexB;
    filterB.index_ = indexA;
    Filter.instances[indexB] = filterA;
    Filter.instances[indexA] = filterB;
  }

  static deleteFilter(index) {
    Filter.instances.splice(index, 1);
    for (var i = index; i < Filter.instances.length; i++) {
      Filter.instances[i].index_ -= 1;
    }
  }

  static sendStreamFilters() {
    const message = {};
    message['method'] = 'VisualDebugger.filterStream';
    message['params'] = {
      filter: { filters: Filter.instances.map((f) => f.streamFilter) }
    };
    Connection.sendMessage(message);
  }
};