1 /*
2  * Copyright 2010 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 <inttypes.h>
18 
19 #define LOG_TAG "QueueBufferInputOutput"
20 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21 //#define LOG_NDEBUG 0
22 
23 #include <gui/IGraphicBufferProducer.h>
24 
25 namespace android {
26 
minFlattenedSize()27 constexpr size_t IGraphicBufferProducer::QueueBufferInput::minFlattenedSize() {
28     return sizeof(timestamp) +
29             sizeof(isAutoTimestamp) +
30             sizeof(dataSpace) +
31             sizeof(crop) +
32             sizeof(scalingMode) +
33             sizeof(transform) +
34             sizeof(stickyTransform) +
35             sizeof(getFrameTimestamps);
36 }
37 
QueueBufferInput(const Parcel & parcel)38 IGraphicBufferProducer::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
39     parcel.read(*this);
40 }
41 
getFlattenedSize() const42 size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
43     return minFlattenedSize() +
44             fence->getFlattenedSize() +
45             surfaceDamage.getFlattenedSize() +
46             hdrMetadata.getFlattenedSize();
47 }
48 
getFdCount() const49 size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
50     return fence->getFdCount();
51 }
52 
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const53 status_t IGraphicBufferProducer::QueueBufferInput::flatten(
54         void*& buffer, size_t& size, int*& fds, size_t& count) const
55 {
56     if (size < getFlattenedSize()) {
57         return NO_MEMORY;
58     }
59 
60     FlattenableUtils::write(buffer, size, timestamp);
61     FlattenableUtils::write(buffer, size, isAutoTimestamp);
62     FlattenableUtils::write(buffer, size, dataSpace);
63     FlattenableUtils::write(buffer, size, crop);
64     FlattenableUtils::write(buffer, size, scalingMode);
65     FlattenableUtils::write(buffer, size, transform);
66     FlattenableUtils::write(buffer, size, stickyTransform);
67     FlattenableUtils::write(buffer, size, getFrameTimestamps);
68 
69     status_t result = fence->flatten(buffer, size, fds, count);
70     if (result != NO_ERROR) {
71         return result;
72     }
73     result = surfaceDamage.flatten(buffer, size);
74     if (result != NO_ERROR) {
75         return result;
76     }
77     FlattenableUtils::advance(buffer, size, surfaceDamage.getFlattenedSize());
78     return hdrMetadata.flatten(buffer, size);
79 }
80 
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)81 status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
82         void const*& buffer, size_t& size, int const*& fds, size_t& count)
83 {
84     if (size < minFlattenedSize()) {
85         return NO_MEMORY;
86     }
87 
88     FlattenableUtils::read(buffer, size, timestamp);
89     FlattenableUtils::read(buffer, size, isAutoTimestamp);
90     FlattenableUtils::read(buffer, size, dataSpace);
91     FlattenableUtils::read(buffer, size, crop);
92     FlattenableUtils::read(buffer, size, scalingMode);
93     FlattenableUtils::read(buffer, size, transform);
94     FlattenableUtils::read(buffer, size, stickyTransform);
95     FlattenableUtils::read(buffer, size, getFrameTimestamps);
96 
97     fence = new Fence();
98     status_t result = fence->unflatten(buffer, size, fds, count);
99     if (result != NO_ERROR) {
100         return result;
101     }
102     result = surfaceDamage.unflatten(buffer, size);
103     if (result != NO_ERROR) {
104         return result;
105     }
106     FlattenableUtils::advance(buffer, size, surfaceDamage.getFlattenedSize());
107     return hdrMetadata.unflatten(buffer, size);
108 }
109 
110 ////////////////////////////////////////////////////////////////////////
minFlattenedSize()111 constexpr size_t IGraphicBufferProducer::QueueBufferOutput::minFlattenedSize() {
112     return sizeof(width) + sizeof(height) + sizeof(transformHint) + sizeof(numPendingBuffers) +
113             sizeof(nextFrameNumber) + sizeof(bufferReplaced) + sizeof(maxBufferCount);
114 }
getFlattenedSize() const115 size_t IGraphicBufferProducer::QueueBufferOutput::getFlattenedSize() const {
116     return minFlattenedSize() + frameTimestamps.getFlattenedSize();
117 }
118 
getFdCount() const119 size_t IGraphicBufferProducer::QueueBufferOutput::getFdCount() const {
120     return frameTimestamps.getFdCount();
121 }
122 
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const123 status_t IGraphicBufferProducer::QueueBufferOutput::flatten(
124         void*& buffer, size_t& size, int*& fds, size_t& count) const
125 {
126     if (size < getFlattenedSize()) {
127         return NO_MEMORY;
128     }
129 
130     FlattenableUtils::write(buffer, size, width);
131     FlattenableUtils::write(buffer, size, height);
132     FlattenableUtils::write(buffer, size, transformHint);
133     FlattenableUtils::write(buffer, size, numPendingBuffers);
134     FlattenableUtils::write(buffer, size, nextFrameNumber);
135     FlattenableUtils::write(buffer, size, bufferReplaced);
136     FlattenableUtils::write(buffer, size, maxBufferCount);
137 
138     return frameTimestamps.flatten(buffer, size, fds, count);
139 }
140 
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)141 status_t IGraphicBufferProducer::QueueBufferOutput::unflatten(
142         void const*& buffer, size_t& size, int const*& fds, size_t& count)
143 {
144     if (size < minFlattenedSize()) {
145         return NO_MEMORY;
146     }
147 
148     FlattenableUtils::read(buffer, size, width);
149     FlattenableUtils::read(buffer, size, height);
150     FlattenableUtils::read(buffer, size, transformHint);
151     FlattenableUtils::read(buffer, size, numPendingBuffers);
152     FlattenableUtils::read(buffer, size, nextFrameNumber);
153     FlattenableUtils::read(buffer, size, bufferReplaced);
154     FlattenableUtils::read(buffer, size, maxBufferCount);
155 
156     return frameTimestamps.unflatten(buffer, size, fds, count);
157 }
158 
159 } // namespace android
160