• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.vibrator;
18 
19 import android.hardware.vibrator.IVibrator;
20 import android.os.VibratorInfo;
21 import android.os.vibrator.Flags;
22 import android.os.vibrator.PwleSegment;
23 import android.os.vibrator.VibrationEffectSegment;
24 import android.util.MathUtils;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 /**
30  * Adapter that splits Pwle segments with longer duration than the device capabilities.
31  *
32  * <p>This transformation replaces large {@link android.os.vibrator.PwleSegment} entries by a
33  * sequence of smaller segments that starts and ends at the same amplitudes/frequencies,
34  * interpolating the intermediate values.
35  *
36  * <p>The segments will not be changed if the device doesn't have
37  * {@link IVibrator#CAP_COMPOSE_PWLE_EFFECTS_V2}.
38  */
39 final class SplitPwleSegmentsAdapter implements VibrationSegmentsAdapter {
40 
41     @Override
adaptToVibrator(VibratorInfo info, List<VibrationEffectSegment> segments, int repeatIndex)42     public int adaptToVibrator(VibratorInfo info, List<VibrationEffectSegment> segments,
43             int repeatIndex) {
44         if (!Flags.normalizedPwleEffects()
45                 || !info.hasCapability(IVibrator.CAP_COMPOSE_PWLE_EFFECTS_V2)) {
46             // The vibrator does not have PWLE v2 capability, so keep the segments unchanged.
47             return repeatIndex;
48         }
49         int maxPwleDuration = (int) info.getMaxEnvelopeEffectDurationMillis();
50         if (maxPwleDuration <= 0) {
51             // No limit set to PWLE primitive duration.
52             return repeatIndex;
53         }
54 
55         int segmentCount = segments.size();
56         for (int i = 0; i < segmentCount; i++) {
57             if (!(segments.get(i) instanceof PwleSegment pwleSegment)) {
58                 continue;
59             }
60             int splits = ((int) pwleSegment.getDuration() + maxPwleDuration - 1) / maxPwleDuration;
61             if (splits <= 1) {
62                 continue;
63             }
64             segments.remove(i);
65             segments.addAll(i, splitPwleSegment(pwleSegment, splits));
66             int addedSegments = splits - 1;
67             if (repeatIndex > i) {
68                 repeatIndex += addedSegments;
69             }
70             i += addedSegments;
71             segmentCount += addedSegments;
72         }
73 
74         return repeatIndex;
75     }
76 
splitPwleSegment(PwleSegment pwleSegment, int splits)77     private static List<PwleSegment> splitPwleSegment(PwleSegment pwleSegment,
78             int splits) {
79         List<PwleSegment> pwleSegments = new ArrayList<>(splits);
80         float startFrequencyHz = pwleSegment.getStartFrequencyHz();
81         float endFrequencyHz = pwleSegment.getEndFrequencyHz();
82         long splitDuration = pwleSegment.getDuration() / splits;
83         float previousAmplitude = pwleSegment.getStartAmplitude();
84         float previousFrequencyHz = startFrequencyHz;
85         long accumulatedDuration = 0;
86 
87         for (int i = 1; i < splits; i++) {
88             accumulatedDuration += splitDuration;
89             float durationRatio = (float) accumulatedDuration / pwleSegment.getDuration();
90             float interpolatedFrequency =
91                     MathUtils.lerp(startFrequencyHz, endFrequencyHz, durationRatio);
92             float interpolatedAmplitude = MathUtils.lerp(pwleSegment.getStartAmplitude(),
93                     pwleSegment.getEndAmplitude(), durationRatio);
94             PwleSegment pwleSplit = new PwleSegment(
95                     previousAmplitude, interpolatedAmplitude,
96                     previousFrequencyHz, interpolatedFrequency,
97                     (int) splitDuration);
98             pwleSegments.add(pwleSplit);
99             previousAmplitude = pwleSplit.getEndAmplitude();
100             previousFrequencyHz = pwleSplit.getEndFrequencyHz();
101         }
102 
103         pwleSegments.add(
104                 new PwleSegment(previousAmplitude, pwleSegment.getEndAmplitude(),
105                         previousFrequencyHz, endFrequencyHz,
106                         (int) (pwleSegment.getDuration() - accumulatedDuration)));
107 
108         return pwleSegments;
109     }
110 }
111