1/*
2 * Copyright (C) 2014 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#include <ui/FrameStats.h>
18
19namespace android {
20
21bool FrameStats::isFixedSize() const {
22    return false;
23}
24
25size_t FrameStats::getFlattenedSize() const {
26    const size_t timestampSize = sizeof(nsecs_t);
27
28    size_t size = timestampSize;
29    size += 3 * desiredPresentTimesNano.size() * timestampSize;
30
31    return size;
32}
33
34status_t FrameStats::flatten(void* buffer, size_t size) const {
35    if (size < getFlattenedSize()) {
36        return NO_MEMORY;
37    }
38
39    nsecs_t* timestamps = reinterpret_cast<nsecs_t*>(buffer);
40    const size_t timestampSize = sizeof(nsecs_t);
41    size_t frameCount = desiredPresentTimesNano.size();
42
43    memcpy(timestamps, &refreshPeriodNano, timestampSize);
44    timestamps += 1;
45
46    memcpy(timestamps, desiredPresentTimesNano.array(), frameCount * timestampSize);
47    timestamps += frameCount;
48
49    memcpy(timestamps, actualPresentTimesNano.array(), frameCount * timestampSize);
50    timestamps += frameCount;
51
52    memcpy(timestamps, frameReadyTimesNano.array(), frameCount * timestampSize);
53
54    return NO_ERROR;
55}
56
57status_t FrameStats::unflatten(void const* buffer, size_t size) {
58    const size_t timestampSize = sizeof(nsecs_t);
59
60    if (size < timestampSize) {
61        return NO_MEMORY;
62    }
63
64    nsecs_t const* timestamps = reinterpret_cast<nsecs_t const*>(buffer);
65    size_t frameCount = (size - timestampSize) / (3 * timestampSize);
66
67    memcpy(&refreshPeriodNano, timestamps, timestampSize);
68    timestamps += 1;
69
70    desiredPresentTimesNano.resize(frameCount);
71    memcpy(desiredPresentTimesNano.editArray(), timestamps, frameCount * timestampSize);
72    timestamps += frameCount;
73
74    actualPresentTimesNano.resize(frameCount);
75    memcpy(actualPresentTimesNano.editArray(), timestamps, frameCount * timestampSize);
76    timestamps += frameCount;
77
78    frameReadyTimesNano.resize(frameCount);
79    memcpy(frameReadyTimesNano.editArray(), timestamps, frameCount * timestampSize);
80
81    return NO_ERROR;
82}
83
84} // namespace android
85