Re-enable index-basics-workers test to see if still times
[chromium-blink-merge.git] / tools / cc-frame-viewer / src / quad_view_viewport.js
blobe5bc2e18c93ff677b5028ab65967d8997c250691
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.
4  */
5 'use strict';
7 base.require('base.range');
8 base.require('ui.event_target');
10 base.exportTo('ccfv', function() {
11   function QuadViewViewport(bbox,
12                             opt_scale,
13                             opt_dontPadBbox, opt_devicePixelRatio) {
14     ui.EventTarget.call(this);
15     if (bbox.isEmpty)
16       throw new Error('Cannot initialize a viewport with an empty bbox');
18     this.setWorldBBox(bbox, opt_dontPadBbox);
20     var devicePixelRatio;
21     if (opt_devicePixelRatio)
22       devicePixelRatio = opt_devicePixelRatio;
23     else
24       devicePixelRatio = window.devicePixelRatio || 1;
26     var scale;
27     if (opt_scale) {
28       scale = opt_scale;
29     } else {
30       if (devicePixelRatio > 1)
31         scale = 0.25;
32       else
33         scale = 0.125;
34     }
35     this.scale_ = scale;
36     this.updateScale_();
37   }
39   QuadViewViewport.prototype = {
41     __proto__: ui.EventTarget.prototype,
43     set scale(scale) {
44       this.scale_ = scale;
45       this.updateScale_();
46       this.didChange_();
47     },
49     get scale() {
50       return this.scale_;
51     },
53     updateScale_: function() {
54       this.worldPixelsPerDevicePixel_ = this.scale_;
55       this.devicePixelsPerLayoutPixel_ = 1 / devicePixelRatio;
57       this.deviceWidth =
58         this.worldRect.width * this.worldPixelsPerDevicePixel_;
59       this.deviceHeight =
60         this.worldRect.height * this.worldPixelsPerDevicePixel_;
62       this.layoutWidth = this.deviceWidth * this.devicePixelsPerLayoutPixel_;
63       this.layoutHeight = this.deviceHeight * this.devicePixelsPerLayoutPixel_;
65       this.transformWorldToDevicePixels_ = mat2d.create();
66       this.transformDevicePixelsToWorld_ = mat2d.create();
67       this.updateTransform_();
68     },
70     setWorldBBox: function(bbox, opt_dontPadBbox) {
71       var world_rect = bbox.asRect();
72       var world_pad;
73       if (opt_dontPadBbox) {
74         world_pad = 0;
75       } else {
76         world_pad = Math.min(world_rect.width,
77                              world_rect.height) * 0.10;
78       }
80       world_rect.enlarge(world_pad);
81       this.worldRect = world_rect;
82       this.updateScale_();
83       this.updateTransform_();
84       this.didChange_();
85     },
87     updateTransform_: function() {
88       if (!this.transformWorldToDevicePixels_)
89         return;
91       mat2d.identity(this.transformWorldToDevicePixels_);
92       mat2d.translateInplaceXY(
93         this.transformWorldToDevicePixels_,
94           -this.worldRect.left, -this.worldRect.top);
95       mat2d.scaleInplaceXY(this.transformWorldToDevicePixels_,
96                            this.worldPixelsPerDevicePixel_,
97                            this.worldPixelsPerDevicePixel_);
99       mat2d.invert(this.transformDevicePixelsToWorld_,
100                    this.transformWorldToDevicePixels_);
101     },
103     layoutPixelsToWorldPixels2: function(v) {
104       var tmp = this.layoutPixelsToDevicePixels2(v);
105       return this.devicePixelsToWorldPixels2(tmp);
106     },
108     layoutPixelsToDevicePixels2: function(v) {
109       var res = vec2.create();
110       return vec2.scale(res, v, 1 / this.devicePixelsPerLayoutPixel_);
111     },
113     devicePixelsToWorldPixels2: function(v) {
114       var res = vec2.create();
115       vec2.transformMat2d(res, v, this.transformDevicePixelsToWorld_);
116       return res;
117     },
119     getWorldToDevicePixelTransform: function() {
120       return this.transformDevicePixelsToWorld_;
121     },
123     getDeviceLineWidthAssumingTransformIsApplied: function(
124         desired_device_line_width) {
125       return desired_device_line_width / this.worldPixelsPerDevicePixel_;
126     },
128     applyTransformToContext: function(ctx) {
129       var transform = this.transformWorldToDevicePixels_;
130       ctx.transform(transform[0], transform[1], transform[2],
131                     transform[3], transform[4], transform[5]);
132     },
134     forceRedrawAll: function() {
135       this.didChange_();
136     },
138     didChange_: function() {
139       base.dispatchSimpleEvent(this, 'change', false, false);
140     }
141   };
143   return {
144     QuadViewViewport: QuadViewViewport,
145   }