xref: /aosp_15_r20/frameworks/native/libs/ui/FrameStats.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <ui/FrameStats.h>
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker namespace android {
20*38e8c45fSAndroid Build Coastguard Worker 
isFixedSize() const21*38e8c45fSAndroid Build Coastguard Worker bool FrameStats::isFixedSize() const {
22*38e8c45fSAndroid Build Coastguard Worker     return false;
23*38e8c45fSAndroid Build Coastguard Worker }
24*38e8c45fSAndroid Build Coastguard Worker 
getFlattenedSize() const25*38e8c45fSAndroid Build Coastguard Worker size_t FrameStats::getFlattenedSize() const {
26*38e8c45fSAndroid Build Coastguard Worker     const size_t timestampSize = sizeof(nsecs_t);
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker     size_t size = timestampSize;
29*38e8c45fSAndroid Build Coastguard Worker     size += 3 * desiredPresentTimesNano.size() * timestampSize;
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker     return size;
32*38e8c45fSAndroid Build Coastguard Worker }
33*38e8c45fSAndroid Build Coastguard Worker 
flatten(void * buffer,size_t size) const34*38e8c45fSAndroid Build Coastguard Worker status_t FrameStats::flatten(void* buffer, size_t size) const {
35*38e8c45fSAndroid Build Coastguard Worker     if (size < getFlattenedSize()) {
36*38e8c45fSAndroid Build Coastguard Worker         return NO_MEMORY;
37*38e8c45fSAndroid Build Coastguard Worker     }
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker     nsecs_t* timestamps = reinterpret_cast<nsecs_t*>(buffer);
40*38e8c45fSAndroid Build Coastguard Worker     const size_t timestampSize = sizeof(nsecs_t);
41*38e8c45fSAndroid Build Coastguard Worker     size_t frameCount = desiredPresentTimesNano.size();
42*38e8c45fSAndroid Build Coastguard Worker 
43*38e8c45fSAndroid Build Coastguard Worker     memcpy(timestamps, &refreshPeriodNano, timestampSize);
44*38e8c45fSAndroid Build Coastguard Worker     timestamps += 1;
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker     memcpy(timestamps, desiredPresentTimesNano.array(), frameCount * timestampSize);
47*38e8c45fSAndroid Build Coastguard Worker     timestamps += frameCount;
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     memcpy(timestamps, actualPresentTimesNano.array(), frameCount * timestampSize);
50*38e8c45fSAndroid Build Coastguard Worker     timestamps += frameCount;
51*38e8c45fSAndroid Build Coastguard Worker 
52*38e8c45fSAndroid Build Coastguard Worker     memcpy(timestamps, frameReadyTimesNano.array(), frameCount * timestampSize);
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker 
unflatten(void const * buffer,size_t size)57*38e8c45fSAndroid Build Coastguard Worker status_t FrameStats::unflatten(void const* buffer, size_t size) {
58*38e8c45fSAndroid Build Coastguard Worker     const size_t timestampSize = sizeof(nsecs_t);
59*38e8c45fSAndroid Build Coastguard Worker 
60*38e8c45fSAndroid Build Coastguard Worker     if (size < timestampSize) {
61*38e8c45fSAndroid Build Coastguard Worker         return NO_MEMORY;
62*38e8c45fSAndroid Build Coastguard Worker     }
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker     nsecs_t const* timestamps = reinterpret_cast<nsecs_t const*>(buffer);
65*38e8c45fSAndroid Build Coastguard Worker     size_t frameCount = (size - timestampSize) / (3 * timestampSize);
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     memcpy(&refreshPeriodNano, timestamps, timestampSize);
68*38e8c45fSAndroid Build Coastguard Worker     timestamps += 1;
69*38e8c45fSAndroid Build Coastguard Worker 
70*38e8c45fSAndroid Build Coastguard Worker     desiredPresentTimesNano.resize(frameCount);
71*38e8c45fSAndroid Build Coastguard Worker     memcpy(desiredPresentTimesNano.editArray(), timestamps, frameCount * timestampSize);
72*38e8c45fSAndroid Build Coastguard Worker     timestamps += frameCount;
73*38e8c45fSAndroid Build Coastguard Worker 
74*38e8c45fSAndroid Build Coastguard Worker     actualPresentTimesNano.resize(frameCount);
75*38e8c45fSAndroid Build Coastguard Worker     memcpy(actualPresentTimesNano.editArray(), timestamps, frameCount * timestampSize);
76*38e8c45fSAndroid Build Coastguard Worker     timestamps += frameCount;
77*38e8c45fSAndroid Build Coastguard Worker 
78*38e8c45fSAndroid Build Coastguard Worker     frameReadyTimesNano.resize(frameCount);
79*38e8c45fSAndroid Build Coastguard Worker     memcpy(frameReadyTimesNano.editArray(), timestamps, frameCount * timestampSize);
80*38e8c45fSAndroid Build Coastguard Worker 
81*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
82*38e8c45fSAndroid Build Coastguard Worker }
83*38e8c45fSAndroid Build Coastguard Worker 
84*38e8c45fSAndroid Build Coastguard Worker } // namespace android
85