blob: 036ef1ecf721e0bcfb3368082430878ccd0227ad [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(),
41 mSingleBufferMode(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);
69 return size;
Dan Stoza289ade12014-02-28 11:17:17 -080070}
71
72size_t BufferItem::getFlattenedSize() const {
Dan Stozaeea6d682015-04-20 12:07:13 -070073 size_t size = sizeof(uint32_t); // Flags
Dan Stoza289ade12014-02-28 11:17:17 -080074 if (mGraphicBuffer != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070075 size += mGraphicBuffer->getFlattenedSize();
76 FlattenableUtils::align<4>(size);
Dan Stoza289ade12014-02-28 11:17:17 -080077 }
78 if (mFence != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070079 size += mFence->getFlattenedSize();
80 FlattenableUtils::align<4>(size);
Dan Stoza289ade12014-02-28 11:17:17 -080081 }
Dan Stozaeea6d682015-04-20 12:07:13 -070082 size += mSurfaceDamage.getFlattenedSize();
83 size = FlattenableUtils::align<8>(size);
84 return size + getPodSize();
Dan Stoza289ade12014-02-28 11:17:17 -080085}
86
87size_t BufferItem::getFdCount() const {
Dan Stozaeea6d682015-04-20 12:07:13 -070088 size_t count = 0;
Dan Stoza289ade12014-02-28 11:17:17 -080089 if (mGraphicBuffer != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070090 count += mGraphicBuffer->getFdCount();
Dan Stoza289ade12014-02-28 11:17:17 -080091 }
92 if (mFence != 0) {
Dan Stozaeea6d682015-04-20 12:07:13 -070093 count += mFence->getFdCount();
Dan Stoza289ade12014-02-28 11:17:17 -080094 }
Dan Stozaeea6d682015-04-20 12:07:13 -070095 return count;
96}
97
98template <typename T>
99static void writeAligned(void*& buffer, size_t& size, T value) {
100 size -= FlattenableUtils::align<alignof(T)>(buffer);
101 FlattenableUtils::write(buffer, size, value);
Dan Stoza289ade12014-02-28 11:17:17 -0800102}
103
104status_t BufferItem::flatten(
105 void*& buffer, size_t& size, int*& fds, size_t& count) const {
106
107 // make sure we have enough space
Dan Stozaeea6d682015-04-20 12:07:13 -0700108 if (size < BufferItem::getFlattenedSize()) {
Dan Stoza289ade12014-02-28 11:17:17 -0800109 return NO_MEMORY;
110 }
111
112 // content flags are stored first
113 uint32_t& flags = *static_cast<uint32_t*>(buffer);
114
115 // advance the pointer
116 FlattenableUtils::advance(buffer, size, sizeof(uint32_t));
117
118 flags = 0;
119 if (mGraphicBuffer != 0) {
120 status_t err = mGraphicBuffer->flatten(buffer, size, fds, count);
121 if (err) return err;
122 size -= FlattenableUtils::align<4>(buffer);
123 flags |= 1;
124 }
125 if (mFence != 0) {
126 status_t err = mFence->flatten(buffer, size, fds, count);
127 if (err) return err;
128 size -= FlattenableUtils::align<4>(buffer);
129 flags |= 2;
130 }
Dan Stozaeea6d682015-04-20 12:07:13 -0700131
Dan Stoza5065a552015-03-17 16:23:42 -0700132 status_t err = mSurfaceDamage.flatten(buffer, size);
133 if (err) return err;
Dan Stozaeea6d682015-04-20 12:07:13 -0700134 FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
Dan Stoza289ade12014-02-28 11:17:17 -0800135
Dan Stozaeea6d682015-04-20 12:07:13 -0700136 // Check we still have enough space
Dan Stoza289ade12014-02-28 11:17:17 -0800137 if (size < getPodSize()) {
138 return NO_MEMORY;
139 }
140
Dan Stozaeea6d682015-04-20 12:07:13 -0700141 writeAligned(buffer, size, mCrop);
142 writeAligned(buffer, size, mTransform);
143 writeAligned(buffer, size, mScalingMode);
Chong Zhang47f674d2015-05-22 10:54:25 -0700144 writeAligned(buffer, size, mTimestampLo);
145 writeAligned(buffer, size, mTimestampHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700146 writeAligned(buffer, size, mIsAutoTimestamp);
147 writeAligned(buffer, size, mDataSpace);
Chong Zhang47f674d2015-05-22 10:54:25 -0700148 writeAligned(buffer, size, mFrameNumberLo);
149 writeAligned(buffer, size, mFrameNumberHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700150 writeAligned(buffer, size, mSlot);
151 writeAligned(buffer, size, mIsDroppable);
152 writeAligned(buffer, size, mAcquireCalled);
153 writeAligned(buffer, size, mTransformToDisplayInverse);
Dan Stoza289ade12014-02-28 11:17:17 -0800154
155 return NO_ERROR;
156}
157
Dan Stozaeea6d682015-04-20 12:07:13 -0700158template <typename T>
159static void readAligned(const void*& buffer, size_t& size, T& value) {
160 size -= FlattenableUtils::align<alignof(T)>(buffer);
161 FlattenableUtils::read(buffer, size, value);
162}
163
Dan Stoza289ade12014-02-28 11:17:17 -0800164status_t BufferItem::unflatten(
165 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
166
Dan Stozaeea6d682015-04-20 12:07:13 -0700167 if (size < sizeof(uint32_t)) {
Dan Stoza289ade12014-02-28 11:17:17 -0800168 return NO_MEMORY;
Dan Stozaeea6d682015-04-20 12:07:13 -0700169 }
Dan Stoza289ade12014-02-28 11:17:17 -0800170
171 uint32_t flags = 0;
172 FlattenableUtils::read(buffer, size, flags);
173
174 if (flags & 1) {
175 mGraphicBuffer = new GraphicBuffer();
176 status_t err = mGraphicBuffer->unflatten(buffer, size, fds, count);
177 if (err) return err;
178 size -= FlattenableUtils::align<4>(buffer);
179 }
180
181 if (flags & 2) {
182 mFence = new Fence();
183 status_t err = mFence->unflatten(buffer, size, fds, count);
184 if (err) return err;
185 size -= FlattenableUtils::align<4>(buffer);
186 }
Dan Stozaeea6d682015-04-20 12:07:13 -0700187
Dan Stoza5065a552015-03-17 16:23:42 -0700188 status_t err = mSurfaceDamage.unflatten(buffer, size);
189 if (err) return err;
Dan Stozaeea6d682015-04-20 12:07:13 -0700190 FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
Dan Stoza289ade12014-02-28 11:17:17 -0800191
Dan Stozaeea6d682015-04-20 12:07:13 -0700192 // Check we still have enough space
Dan Stoza289ade12014-02-28 11:17:17 -0800193 if (size < getPodSize()) {
194 return NO_MEMORY;
195 }
196
Dan Stozaeea6d682015-04-20 12:07:13 -0700197 readAligned(buffer, size, mCrop);
198 readAligned(buffer, size, mTransform);
199 readAligned(buffer, size, mScalingMode);
Chong Zhang47f674d2015-05-22 10:54:25 -0700200 readAligned(buffer, size, mTimestampLo);
201 readAligned(buffer, size, mTimestampHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700202 readAligned(buffer, size, mIsAutoTimestamp);
203 readAligned(buffer, size, mDataSpace);
Chong Zhang47f674d2015-05-22 10:54:25 -0700204 readAligned(buffer, size, mFrameNumberLo);
205 readAligned(buffer, size, mFrameNumberHi);
Dan Stozaeea6d682015-04-20 12:07:13 -0700206 readAligned(buffer, size, mSlot);
207 readAligned(buffer, size, mIsDroppable);
208 readAligned(buffer, size, mAcquireCalled);
209 readAligned(buffer, size, mTransformToDisplayInverse);
Dan Stoza289ade12014-02-28 11:17:17 -0800210
211 return NO_ERROR;
212}
213
214const char* BufferItem::scalingModeName(uint32_t scalingMode) {
215 switch (scalingMode) {
216 case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
217 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
218 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
219 default: return "Unknown";
220 }
221}
222
223} // namespace android