blob: 2ffeb0d3331c75a15bd5dc2e49bc30eec9a1e8ba [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 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 <gui/BufferItem.h>
18
19#include <ui/Fence.h>
20#include <ui/GraphicBuffer.h>
21
22#include <system/window.h>
23
24namespace android {
25
26BufferItem::BufferItem() :
Pablo Ceballosccdfd602015-10-07 15:05:45 -070027 mGraphicBuffer(NULL),
28 mFence(NULL),
Pablo Ceballos60d69222015-08-07 14:47:20 -070029 mCrop(Rect::INVALID_RECT),
Dan Stoza289ade12014-02-28 11:17:17 -080030 mTransform(0),
31 mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
32 mTimestamp(0),
33 mIsAutoTimestamp(false),
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -080034 mDataSpace(HAL_DATASPACE_UNKNOWN),
Dan Stoza289ade12014-02-28 11:17:17 -080035 mFrameNumber(0),
36 mSlot(INVALID_BUFFER_SLOT),
37 mIsDroppable(false),
38 mAcquireCalled(false),
Pablo Ceballosccdfd602015-10-07 15:05:45 -070039 mTransformToDisplayInverse(false),
Pablo Ceballos06312182015-10-07 16:32:12 -070040 mSurfaceDamage(),
Pablo Ceballosff95aab2016-01-13 17:09:58 -080041 mAutoRefresh(false),
Pablo Ceballos23b4abe2016-01-08 12:15:22 -080042 mQueuedBuffer(true),
43 mIsStale(false) {
Dan Stoza289ade12014-02-28 11:17:17 -080044}
45
Dan Stoza8dc55392014-11-04 11:37:46 -080046BufferItem::~BufferItem() {}
47
Dan Stozaeea6d682015-04-20 12:07:13 -070048template <typename T>
49static void addAligned(size_t& size, T /* value */) {
50 size = FlattenableUtils::align<sizeof(T)>(size);
51 size += sizeof(T);
52}
53
Dan Stoza289ade12014-02-28 11:17:17 -080054size_t BufferItem::getPodSize() const {
Dan Stozaeea6d682015-04-20 12:07:13 -070055 size_t size = 0;
56 addAligned(size, mCrop);
57 addAligned(size, mTransform);
58 addAligned(size, mScalingMode);
Chong Zhang47f674d2015-05-22 10:54:25 -070059 addAligned(size, mTimestampLo);
60 addAligned(size, mTimestampHi);
Dan Stozaeea6d682015-04-20 12:07:13 -070061 addAligned(size, mIsAutoTimestamp);
62 addAligned(size, mDataSpace);
Chong Zhang47f674d2015-05-22 10:54:25 -070063 addAligned(size, mFrameNumberLo);
64 addAligned(size, mFrameNumberHi);
Dan Stozaeea6d682015-04-20 12:07:13 -070065 addAligned(size, mSlot);
66 addAligned(size, mIsDroppable);
67 addAligned(size, mAcquireCalled);
68 addAligned(size, mTransformToDisplayInverse);
Pablo Ceballosbd3577e2016-06-20 17:40:34 -070069 addAligned(size, mAutoRefresh);
70 addAligned(size, mQueuedBuffer);
71 addAligned(size, mIsStale);
Dan Stozaeea6d682015-04-20 12:07:13 -070072 return size;
Dan Stoza289ade12014-02-28 11:17:17 -080073}
74
75size_t BufferItem::getFlattenedSize() const {
Dan Stozaeea6d682015-04-20 12:07:13 -070076 size_t size = sizeof(uint32_t); // Flags
Dan Stoza289ade12014-02-28 11:17:17 -080077 if (mGraphicBuffer != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070078 size += mGraphicBuffer->getFlattenedSize();
79 FlattenableUtils::align<4>(size);
Dan Stoza289ade12014-02-28 11:17:17 -080080 }
81 if (mFence != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070082 size += mFence->getFlattenedSize();
83 FlattenableUtils::align<4>(size);
Dan Stoza289ade12014-02-28 11:17:17 -080084 }
Dan Stozaeea6d682015-04-20 12:07:13 -070085 size += mSurfaceDamage.getFlattenedSize();
86 size = FlattenableUtils::align<8>(size);
87 return size + getPodSize();
Dan Stoza289ade12014-02-28 11:17:17 -080088}
89
90size_t BufferItem::getFdCount() const {
Dan Stozaeea6d682015-04-20 12:07:13 -070091 size_t count = 0;
Dan Stoza289ade12014-02-28 11:17:17 -080092 if (mGraphicBuffer != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070093 count += mGraphicBuffer->getFdCount();
Dan Stoza289ade12014-02-28 11:17:17 -080094 }
95 if (mFence != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070096 count += mFence->getFdCount();
Dan Stoza289ade12014-02-28 11:17:17 -080097 }
Dan Stozaeea6d682015-04-20 12:07:13 -070098 return count;
99}
100
101template <typename T>
102static void writeAligned(void*& buffer, size_t& size, T value) {
103 size -= FlattenableUtils::align<alignof(T)>(buffer);
104 FlattenableUtils::write(buffer, size, value);
Dan Stoza289ade12014-02-28 11:17:17 -0800105}
106
107status_t BufferItem::flatten(
108 void*& buffer, size_t& size, int*& fds, size_t& count) const {
109
110 // make sure we have enough space
Dan Stozaeea6d682015-04-20 12:07:13 -0700111 if (size < BufferItem::getFlattenedSize()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800112 return NO_MEMORY;
113 }
114
115 // content flags are stored first
116 uint32_t& flags = *static_cast<uint32_t*>(buffer);
117
118 // advance the pointer
119 FlattenableUtils::advance(buffer, size, sizeof(uint32_t));
120
121 flags = 0;
122 if (mGraphicBuffer != 0) {
123 status_t err = mGraphicBuffer->flatten(buffer, size, fds, count);
124 if (err) return err;
125 size -= FlattenableUtils::align<4>(buffer);
126 flags |= 1;
127 }
128 if (mFence != 0) {
129 status_t err = mFence->flatten(buffer, size, fds, count);
130 if (err) return err;
131 size -= FlattenableUtils::align<4>(buffer);
132 flags |= 2;
133 }
Dan Stozaeea6d682015-04-20 12:07:13 -0700134
Dan Stoza5065a552015-03-17 16:23:42 -0700135 status_t err = mSurfaceDamage.flatten(buffer, size);
136 if (err) return err;
Dan Stozaeea6d682015-04-20 12:07:13 -0700137 FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
Dan Stoza289ade12014-02-28 11:17:17 -0800138
Dan Stozaeea6d682015-04-20 12:07:13 -0700139 // Check we still have enough space
Dan Stoza289ade12014-02-28 11:17:17 -0800140 if (size < getPodSize()) {
141 return NO_MEMORY;
142 }
143
Dan Stozaeea6d682015-04-20 12:07:13 -0700144 writeAligned(buffer, size, mCrop);
145 writeAligned(buffer, size, mTransform);
146 writeAligned(buffer, size, mScalingMode);
Chong Zhang47f674d2015-05-22 10:54:25 -0700147 writeAligned(buffer, size, mTimestampLo);
148 writeAligned(buffer, size, mTimestampHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700149 writeAligned(buffer, size, mIsAutoTimestamp);
150 writeAligned(buffer, size, mDataSpace);
Chong Zhang47f674d2015-05-22 10:54:25 -0700151 writeAligned(buffer, size, mFrameNumberLo);
152 writeAligned(buffer, size, mFrameNumberHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700153 writeAligned(buffer, size, mSlot);
154 writeAligned(buffer, size, mIsDroppable);
155 writeAligned(buffer, size, mAcquireCalled);
156 writeAligned(buffer, size, mTransformToDisplayInverse);
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700157 writeAligned(buffer, size, mAutoRefresh);
158 writeAligned(buffer, size, mQueuedBuffer);
159 writeAligned(buffer, size, mIsStale);
Dan Stoza289ade12014-02-28 11:17:17 -0800160
161 return NO_ERROR;
162}
163
Dan Stozaeea6d682015-04-20 12:07:13 -0700164template <typename T>
165static void readAligned(const void*& buffer, size_t& size, T& value) {
166 size -= FlattenableUtils::align<alignof(T)>(buffer);
167 FlattenableUtils::read(buffer, size, value);
168}
169
Dan Stoza289ade12014-02-28 11:17:17 -0800170status_t BufferItem::unflatten(
171 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
172
Dan Stozaeea6d682015-04-20 12:07:13 -0700173 if (size < sizeof(uint32_t)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800174 return NO_MEMORY;
Dan Stozaeea6d682015-04-20 12:07:13 -0700175 }
Dan Stoza289ade12014-02-28 11:17:17 -0800176
177 uint32_t flags = 0;
178 FlattenableUtils::read(buffer, size, flags);
179
180 if (flags & 1) {
181 mGraphicBuffer = new GraphicBuffer();
182 status_t err = mGraphicBuffer->unflatten(buffer, size, fds, count);
183 if (err) return err;
184 size -= FlattenableUtils::align<4>(buffer);
185 }
186
187 if (flags & 2) {
188 mFence = new Fence();
189 status_t err = mFence->unflatten(buffer, size, fds, count);
190 if (err) return err;
191 size -= FlattenableUtils::align<4>(buffer);
192 }
Dan Stozaeea6d682015-04-20 12:07:13 -0700193
Dan Stoza5065a552015-03-17 16:23:42 -0700194 status_t err = mSurfaceDamage.unflatten(buffer, size);
195 if (err) return err;
Dan Stozaeea6d682015-04-20 12:07:13 -0700196 FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
Dan Stoza289ade12014-02-28 11:17:17 -0800197
Dan Stozaeea6d682015-04-20 12:07:13 -0700198 // Check we still have enough space
Dan Stoza289ade12014-02-28 11:17:17 -0800199 if (size < getPodSize()) {
200 return NO_MEMORY;
201 }
202
Dan Stozaeea6d682015-04-20 12:07:13 -0700203 readAligned(buffer, size, mCrop);
204 readAligned(buffer, size, mTransform);
205 readAligned(buffer, size, mScalingMode);
Chong Zhang47f674d2015-05-22 10:54:25 -0700206 readAligned(buffer, size, mTimestampLo);
207 readAligned(buffer, size, mTimestampHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700208 readAligned(buffer, size, mIsAutoTimestamp);
209 readAligned(buffer, size, mDataSpace);
Chong Zhang47f674d2015-05-22 10:54:25 -0700210 readAligned(buffer, size, mFrameNumberLo);
211 readAligned(buffer, size, mFrameNumberHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700212 readAligned(buffer, size, mSlot);
213 readAligned(buffer, size, mIsDroppable);
214 readAligned(buffer, size, mAcquireCalled);
215 readAligned(buffer, size, mTransformToDisplayInverse);
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700216 readAligned(buffer, size, mAutoRefresh);
217 readAligned(buffer, size, mQueuedBuffer);
218 readAligned(buffer, size, mIsStale);
Dan Stoza289ade12014-02-28 11:17:17 -0800219
220 return NO_ERROR;
221}
222
223const char* BufferItem::scalingModeName(uint32_t scalingMode) {
224 switch (scalingMode) {
225 case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
226 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
227 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
228 default: return "Unknown";
229 }
230}
231
232} // namespace android