xref: /aosp_15_r20/external/perfetto/ui/src/common/resolution.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1*6dbdd20aSAndroid Build Coastguard Worker// Copyright (C) 2024 The Android Open Source Project
2*6dbdd20aSAndroid Build Coastguard Worker//
3*6dbdd20aSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*6dbdd20aSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*6dbdd20aSAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*6dbdd20aSAndroid Build Coastguard Worker//
7*6dbdd20aSAndroid Build Coastguard Worker//      http://www.apache.org/licenses/LICENSE-2.0
8*6dbdd20aSAndroid Build Coastguard Worker//
9*6dbdd20aSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*6dbdd20aSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*6dbdd20aSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*6dbdd20aSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*6dbdd20aSAndroid Build Coastguard Worker// limitations under the License.
14*6dbdd20aSAndroid Build Coastguard Worker
15*6dbdd20aSAndroid Build Coastguard Workerimport {BigintMath} from '../base/bigint_math';
16*6dbdd20aSAndroid Build Coastguard Workerimport {duration} from '../base/time';
17*6dbdd20aSAndroid Build Coastguard Workerimport {HighPrecisionTimeSpan} from '../base/high_precision_time_span';
18*6dbdd20aSAndroid Build Coastguard Worker
19*6dbdd20aSAndroid Build Coastguard Worker/**
20*6dbdd20aSAndroid Build Coastguard Worker * Work out an appropriate "resolution" for a given time span stretched over a
21*6dbdd20aSAndroid Build Coastguard Worker * given number of pixels.
22*6dbdd20aSAndroid Build Coastguard Worker *
23*6dbdd20aSAndroid Build Coastguard Worker * The returned value will be rounded down to the nearest power of 2, and will
24*6dbdd20aSAndroid Build Coastguard Worker * always be >= 1.
25*6dbdd20aSAndroid Build Coastguard Worker *
26*6dbdd20aSAndroid Build Coastguard Worker * @param timeSpan The span of time to represent.
27*6dbdd20aSAndroid Build Coastguard Worker * @param widthPx How many pixels we have to represent the time span.
28*6dbdd20aSAndroid Build Coastguard Worker * @returns The resultant resolution.
29*6dbdd20aSAndroid Build Coastguard Worker */
30*6dbdd20aSAndroid Build Coastguard Workerexport function calculateResolution(
31*6dbdd20aSAndroid Build Coastguard Worker  timeSpan: HighPrecisionTimeSpan,
32*6dbdd20aSAndroid Build Coastguard Worker  widthPx: number,
33*6dbdd20aSAndroid Build Coastguard Worker): duration {
34*6dbdd20aSAndroid Build Coastguard Worker  // Work out how much time corresponds to one pixel
35*6dbdd20aSAndroid Build Coastguard Worker  const timePerPixel = Number(timeSpan.duration) / widthPx;
36*6dbdd20aSAndroid Build Coastguard Worker
37*6dbdd20aSAndroid Build Coastguard Worker  // Round down to the nearest power of 2, noting that the smallest value this
38*6dbdd20aSAndroid Build Coastguard Worker  // function can return is 1
39*6dbdd20aSAndroid Build Coastguard Worker  return BigintMath.bitFloor(BigInt(Math.floor(timePerPixel)));
40*6dbdd20aSAndroid Build Coastguard Worker}
41