• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #include <gui/IGraphicBufferProducer.h>
19 
20 namespace android {
21 
minFlattenedSize()22 constexpr size_t IGraphicBufferProducer::QueueBufferInput::minFlattenedSize() {
23     return sizeof(timestamp) +
24             sizeof(isAutoTimestamp) +
25             sizeof(dataSpace) +
26             sizeof(crop) +
27             sizeof(scalingMode) +
28             sizeof(transform) +
29             sizeof(stickyTransform) +
30             sizeof(getFrameTimestamps) +
31             sizeof(slot);
32 }
33 
getFlattenedSize() const34 size_t IGraphicBufferProducer::QueueBufferInput::getFlattenedSize() const {
35     return minFlattenedSize() +
36             fence->getFlattenedSize() +
37             surfaceDamage.getFlattenedSize() +
38             hdrMetadata.getFlattenedSize();
39 }
40 
getFdCount() const41 size_t IGraphicBufferProducer::QueueBufferInput::getFdCount() const {
42     return fence->getFdCount();
43 }
44 
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const45 status_t IGraphicBufferProducer::QueueBufferInput::flatten(
46         void*& buffer, size_t& size, int*& fds, size_t& count) const
47 {
48     if (size < getFlattenedSize()) {
49         return NO_MEMORY;
50     }
51 
52     FlattenableUtils::write(buffer, size, timestamp);
53     FlattenableUtils::write(buffer, size, isAutoTimestamp);
54     FlattenableUtils::write(buffer, size, dataSpace);
55     FlattenableUtils::write(buffer, size, crop);
56     FlattenableUtils::write(buffer, size, scalingMode);
57     FlattenableUtils::write(buffer, size, transform);
58     FlattenableUtils::write(buffer, size, stickyTransform);
59     FlattenableUtils::write(buffer, size, getFrameTimestamps);
60 
61     status_t result = fence->flatten(buffer, size, fds, count);
62     if (result != NO_ERROR) {
63         return result;
64     }
65     result = surfaceDamage.flatten(buffer, size);
66     if (result != NO_ERROR) {
67         return result;
68     }
69     FlattenableUtils::advance(buffer, size, surfaceDamage.getFlattenedSize());
70     result = hdrMetadata.flatten(buffer, size);
71     if (result != NO_ERROR) {
72         return result;
73     }
74     FlattenableUtils::advance(buffer, size, hdrMetadata.getFlattenedSize());
75     FlattenableUtils::write(buffer, size, slot);
76     return NO_ERROR;
77 }
78 
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)79 status_t IGraphicBufferProducer::QueueBufferInput::unflatten(
80         void const*& buffer, size_t& size, int const*& fds, size_t& count)
81 {
82     if (size < minFlattenedSize()) {
83         return NO_MEMORY;
84     }
85 
86     FlattenableUtils::read(buffer, size, timestamp);
87     FlattenableUtils::read(buffer, size, isAutoTimestamp);
88     FlattenableUtils::read(buffer, size, dataSpace);
89     FlattenableUtils::read(buffer, size, crop);
90     FlattenableUtils::read(buffer, size, scalingMode);
91     FlattenableUtils::read(buffer, size, transform);
92     FlattenableUtils::read(buffer, size, stickyTransform);
93     FlattenableUtils::read(buffer, size, getFrameTimestamps);
94 
95     fence = new Fence();
96     status_t result = fence->unflatten(buffer, size, fds, count);
97     if (result != NO_ERROR) {
98         return result;
99     }
100     result = surfaceDamage.unflatten(buffer, size);
101     if (result != NO_ERROR) {
102         return result;
103     }
104     FlattenableUtils::advance(buffer, size, surfaceDamage.getFlattenedSize());
105     result =  hdrMetadata.unflatten(buffer, size);
106     if (result != NO_ERROR) {
107         return result;
108     }
109     FlattenableUtils::advance(buffer, size, hdrMetadata.getFlattenedSize());
110     FlattenableUtils::read(buffer, size, slot);
111     return NO_ERROR;
112 }
113 
114 ////////////////////////////////////////////////////////////////////////
minFlattenedSize()115 constexpr size_t IGraphicBufferProducer::QueueBufferOutput::minFlattenedSize() {
116     return sizeof(width) + sizeof(height) + sizeof(transformHint) + sizeof(numPendingBuffers) +
117             sizeof(nextFrameNumber) + sizeof(bufferReplaced) + sizeof(maxBufferCount) +
118             sizeof(result);
119 }
getFlattenedSize() const120 size_t IGraphicBufferProducer::QueueBufferOutput::getFlattenedSize() const {
121     return minFlattenedSize() + frameTimestamps.getFlattenedSize();
122 }
123 
getFdCount() const124 size_t IGraphicBufferProducer::QueueBufferOutput::getFdCount() const {
125     return frameTimestamps.getFdCount();
126 }
127 
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const128 status_t IGraphicBufferProducer::QueueBufferOutput::flatten(
129         void*& buffer, size_t& size, int*& fds, size_t& count) const
130 {
131     if (size < getFlattenedSize()) {
132         return NO_MEMORY;
133     }
134 
135     FlattenableUtils::write(buffer, size, width);
136     FlattenableUtils::write(buffer, size, height);
137     FlattenableUtils::write(buffer, size, transformHint);
138     FlattenableUtils::write(buffer, size, numPendingBuffers);
139     FlattenableUtils::write(buffer, size, nextFrameNumber);
140     FlattenableUtils::write(buffer, size, bufferReplaced);
141     FlattenableUtils::write(buffer, size, maxBufferCount);
142 
143     status_t result = frameTimestamps.flatten(buffer, size, fds, count);
144     if (result != NO_ERROR) {
145         return result;
146     }
147     FlattenableUtils::write(buffer, size, result);
148     return NO_ERROR;
149 }
150 
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)151 status_t IGraphicBufferProducer::QueueBufferOutput::unflatten(
152         void const*& buffer, size_t& size, int const*& fds, size_t& count)
153 {
154     if (size < minFlattenedSize()) {
155         return NO_MEMORY;
156     }
157 
158     FlattenableUtils::read(buffer, size, width);
159     FlattenableUtils::read(buffer, size, height);
160     FlattenableUtils::read(buffer, size, transformHint);
161     FlattenableUtils::read(buffer, size, numPendingBuffers);
162     FlattenableUtils::read(buffer, size, nextFrameNumber);
163     FlattenableUtils::read(buffer, size, bufferReplaced);
164     FlattenableUtils::read(buffer, size, maxBufferCount);
165 
166     status_t result = frameTimestamps.unflatten(buffer, size, fds, count);
167     if (result != NO_ERROR) {
168         return result;
169     }
170     FlattenableUtils::read(buffer, size, result);
171     return NO_ERROR;
172 }
173 
174 ////////////////////////////////////////////////////////////////////////
minFlattenedSize()175 constexpr size_t IGraphicBufferProducer::RequestBufferOutput::minFlattenedSize() {
176     return sizeof(result) +
177             sizeof(int32_t); // IsBufferNull
178 }
179 
getFlattenedSize() const180 size_t IGraphicBufferProducer::RequestBufferOutput::getFlattenedSize() const {
181     return minFlattenedSize() + (buffer == nullptr ? 0 : buffer->getFlattenedSize());
182 }
183 
getFdCount() const184 size_t IGraphicBufferProducer::RequestBufferOutput::getFdCount() const {
185     return (buffer == nullptr ? 0 : buffer->getFdCount());
186 }
187 
flatten(void * & fBuffer,size_t & size,int * & fds,size_t & count) const188 status_t IGraphicBufferProducer::RequestBufferOutput::flatten(
189         void*& fBuffer, size_t& size, int*& fds, size_t& count) const {
190     if (size < getFlattenedSize()) {
191         return NO_MEMORY;
192     }
193 
194     FlattenableUtils::write(fBuffer, size, result);
195     const int32_t isBufferNull = (buffer == nullptr ? 1 : 0);
196     FlattenableUtils::write(fBuffer, size, isBufferNull);
197 
198     if (!isBufferNull) {
199         status_t status = buffer->flatten(fBuffer, size, fds, count);
200         if (status != NO_ERROR) {
201             return status;
202         }
203     }
204     return NO_ERROR;
205 }
206 
unflatten(void const * & fBuffer,size_t & size,int const * & fds,size_t & count)207 status_t IGraphicBufferProducer::RequestBufferOutput::unflatten(
208         void const*& fBuffer, size_t& size, int const*& fds, size_t& count) {
209     if (size < minFlattenedSize()) {
210         return NO_MEMORY;
211     }
212 
213     FlattenableUtils::read(fBuffer, size, result);
214     int32_t isBufferNull = 0;
215     FlattenableUtils::read(fBuffer, size, isBufferNull);
216     buffer = new GraphicBuffer();
217     if (!isBufferNull) {
218         status_t status = buffer->unflatten(fBuffer, size, fds, count);
219         if (status != NO_ERROR) {
220             return status;
221         }
222     }
223     return NO_ERROR;
224 }
225 
226 ////////////////////////////////////////////////////////////////////////
227 
getFlattenedSize() const228 size_t IGraphicBufferProducer::DequeueBufferInput::getFlattenedSize() const {
229     return sizeof(width) + sizeof(height) + sizeof(format) + sizeof(usage) +
230             sizeof(int32_t/*getTimestamps*/);
231 }
232 
flatten(void * buffer,size_t size) const233 status_t IGraphicBufferProducer::DequeueBufferInput::flatten(void* buffer, size_t size) const {
234     if (size < getFlattenedSize()) {
235         return NO_MEMORY;
236     }
237     FlattenableUtils::write(buffer, size, width);
238     FlattenableUtils::write(buffer, size, height);
239     FlattenableUtils::write(buffer, size, format);
240     FlattenableUtils::write(buffer, size, usage);
241     const int32_t getTimestampsInt = (getTimestamps ? 1 : 0);
242     FlattenableUtils::write(buffer, size, getTimestampsInt);
243 
244     return NO_ERROR;
245 }
246 
unflatten(void const * buffer,size_t size)247 status_t IGraphicBufferProducer::DequeueBufferInput::unflatten(void const* buffer, size_t size) {
248     if (size < getFlattenedSize()) {
249         return NO_MEMORY;
250     }
251 
252     FlattenableUtils::read(buffer, size, width);
253     FlattenableUtils::read(buffer, size, height);
254     FlattenableUtils::read(buffer, size, format);
255     FlattenableUtils::read(buffer, size, usage);
256     int32_t getTimestampsInt = 0;
257     FlattenableUtils::read(buffer, size, getTimestampsInt);
258     getTimestamps = (getTimestampsInt == 1);
259 
260     return NO_ERROR;
261 }
262 
263 ////////////////////////////////////////////////////////////////////////
264 
minFlattenedSize()265 constexpr size_t IGraphicBufferProducer::DequeueBufferOutput::minFlattenedSize() {
266     return sizeof(result) + sizeof(slot) + sizeof(bufferAge) + sizeof(int32_t/*hasTimestamps*/);
267 }
268 
getFlattenedSize() const269 size_t IGraphicBufferProducer::DequeueBufferOutput::getFlattenedSize() const {
270     return minFlattenedSize() +
271             fence->getFlattenedSize() +
272             (timestamps.has_value() ? timestamps->getFlattenedSize() : 0);
273 }
274 
getFdCount() const275 size_t IGraphicBufferProducer::DequeueBufferOutput::getFdCount() const {
276     return fence->getFdCount() +
277             (timestamps.has_value() ? timestamps->getFdCount() : 0);
278 }
279 
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const280 status_t IGraphicBufferProducer::DequeueBufferOutput::flatten(
281         void*& buffer, size_t& size, int*& fds, size_t& count) const {
282     if (size < getFlattenedSize()) {
283         return NO_MEMORY;
284     }
285 
286     FlattenableUtils::write(buffer, size, result);
287     FlattenableUtils::write(buffer, size, slot);
288     FlattenableUtils::write(buffer, size, bufferAge);
289     status_t status = fence->flatten(buffer, size, fds, count);
290     if (status != NO_ERROR) {
291         return result;
292     }
293     const int32_t hasTimestamps = timestamps.has_value() ? 1 : 0;
294     FlattenableUtils::write(buffer, size, hasTimestamps);
295     if (timestamps.has_value()) {
296         status = timestamps->flatten(buffer, size, fds, count);
297     }
298     return status;
299 }
300 
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)301 status_t IGraphicBufferProducer::DequeueBufferOutput::unflatten(
302         void const*& buffer, size_t& size, int const*& fds, size_t& count) {
303     if (size < minFlattenedSize()) {
304         return NO_MEMORY;
305     }
306 
307     FlattenableUtils::read(buffer, size, result);
308     FlattenableUtils::read(buffer, size, slot);
309     FlattenableUtils::read(buffer, size, bufferAge);
310 
311     fence = new Fence();
312     status_t status = fence->unflatten(buffer, size, fds, count);
313     if (status != NO_ERROR) {
314         return status;
315     }
316     int32_t hasTimestamps = 0;
317     FlattenableUtils::read(buffer, size, hasTimestamps);
318     if (hasTimestamps) {
319         timestamps.emplace();
320         status = timestamps->unflatten(buffer, size, fds, count);
321     }
322     return status;
323 }
324 
325 ////////////////////////////////////////////////////////////////////////
326 
getFlattenedSize() const327 size_t IGraphicBufferProducer::AttachBufferOutput::getFlattenedSize() const {
328     return sizeof(result) + sizeof(slot);
329 }
330 
flatten(void * buffer,size_t size) const331 status_t IGraphicBufferProducer::AttachBufferOutput::flatten(void* buffer, size_t size) const {
332     if (size < getFlattenedSize()) {
333         return NO_MEMORY;
334     }
335     FlattenableUtils::write(buffer, size, result);
336     FlattenableUtils::write(buffer, size, slot);
337 
338     return NO_ERROR;
339 }
340 
unflatten(void const * buffer,size_t size)341 status_t IGraphicBufferProducer::AttachBufferOutput::unflatten(void const* buffer, size_t size) {
342     if (size < getFlattenedSize()) {
343         return NO_MEMORY;
344     }
345     FlattenableUtils::read(buffer, size, result);
346     FlattenableUtils::read(buffer, size, slot);
347 
348     return NO_ERROR;
349 }
350 
351 ////////////////////////////////////////////////////////////////////////
352 
minFlattenedSize()353 constexpr size_t IGraphicBufferProducer::CancelBufferInput::minFlattenedSize() {
354     return sizeof(slot);
355 }
356 
getFlattenedSize() const357 size_t IGraphicBufferProducer::CancelBufferInput::getFlattenedSize() const {
358     return minFlattenedSize() + fence->getFlattenedSize();
359 }
360 
getFdCount() const361 size_t IGraphicBufferProducer::CancelBufferInput::getFdCount() const {
362     return fence->getFdCount();
363 }
364 
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const365 status_t IGraphicBufferProducer::CancelBufferInput::flatten(
366         void*& buffer, size_t& size, int*& fds, size_t& count) const {
367     if (size < getFlattenedSize()) {
368         return NO_MEMORY;
369     }
370 
371     FlattenableUtils::write(buffer, size, slot);
372     return fence->flatten(buffer, size, fds, count);
373 }
374 
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)375 status_t IGraphicBufferProducer::CancelBufferInput::unflatten(
376         void const*& buffer, size_t& size, int const*& fds, size_t& count) {
377     if (size < minFlattenedSize()) {
378         return NO_MEMORY;
379     }
380 
381     FlattenableUtils::read(buffer, size, slot);
382 
383     fence = new Fence();
384     return fence->unflatten(buffer, size, fds, count);
385 }
386 
387 ////////////////////////////////////////////////////////////////////////
388 
getFlattenedSize() const389 size_t IGraphicBufferProducer::QueryOutput::getFlattenedSize() const {
390     return sizeof(result) + sizeof(value);
391 }
392 
flatten(void * buffer,size_t size) const393 status_t IGraphicBufferProducer::QueryOutput::flatten(void* buffer, size_t size) const {
394     if (size < getFlattenedSize()) {
395         return NO_MEMORY;
396     }
397     FlattenableUtils::write(buffer, size, result);
398     FlattenableUtils::write(buffer, size, value);
399 
400     return NO_ERROR;
401 }
402 
unflatten(void const * buffer,size_t size)403 status_t IGraphicBufferProducer::QueryOutput::unflatten(void const* buffer, size_t size) {
404     if (size < getFlattenedSize()) {
405         return NO_MEMORY;
406     }
407     FlattenableUtils::read(buffer, size, result);
408     FlattenableUtils::read(buffer, size, value);
409 
410     return NO_ERROR;
411 }
412 
413 } // namespace android
414