Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | namespace android { |
| 25 | |
| 26 | BufferItem::BufferItem() : |
| 27 | mTransform(0), |
| 28 | mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 29 | mTimestamp(0), |
| 30 | mIsAutoTimestamp(false), |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 31 | mDataSpace(HAL_DATASPACE_UNKNOWN), |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 32 | mFrameNumber(0), |
| 33 | mSlot(INVALID_BUFFER_SLOT), |
| 34 | mIsDroppable(false), |
| 35 | mAcquireCalled(false), |
| 36 | mTransformToDisplayInverse(false) { |
| 37 | mCrop.makeInvalid(); |
| 38 | } |
| 39 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 40 | BufferItem::~BufferItem() {} |
| 41 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 42 | size_t BufferItem::getPodSize() const { |
| 43 | size_t c = sizeof(mCrop) + |
| 44 | sizeof(mTransform) + |
| 45 | sizeof(mScalingMode) + |
| 46 | sizeof(mTimestamp) + |
| 47 | sizeof(mIsAutoTimestamp) + |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 48 | sizeof(mDataSpace) + |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 49 | sizeof(mFrameNumber) + |
| 50 | sizeof(mSlot) + |
| 51 | sizeof(mIsDroppable) + |
| 52 | sizeof(mAcquireCalled) + |
| 53 | sizeof(mTransformToDisplayInverse); |
| 54 | return c; |
| 55 | } |
| 56 | |
| 57 | size_t BufferItem::getFlattenedSize() const { |
| 58 | size_t c = 0; |
| 59 | if (mGraphicBuffer != 0) { |
| 60 | c += mGraphicBuffer->getFlattenedSize(); |
| 61 | FlattenableUtils::align<4>(c); |
| 62 | } |
| 63 | if (mFence != 0) { |
| 64 | c += mFence->getFlattenedSize(); |
| 65 | FlattenableUtils::align<4>(c); |
| 66 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame^] | 67 | c += mSurfaceDamage.getFlattenedSize(); |
| 68 | FlattenableUtils::align<4>(c); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 69 | return sizeof(int32_t) + c + getPodSize(); |
| 70 | } |
| 71 | |
| 72 | size_t BufferItem::getFdCount() const { |
| 73 | size_t c = 0; |
| 74 | if (mGraphicBuffer != 0) { |
| 75 | c += mGraphicBuffer->getFdCount(); |
| 76 | } |
| 77 | if (mFence != 0) { |
| 78 | c += mFence->getFdCount(); |
| 79 | } |
| 80 | return c; |
| 81 | } |
| 82 | |
| 83 | status_t BufferItem::flatten( |
| 84 | void*& buffer, size_t& size, int*& fds, size_t& count) const { |
| 85 | |
| 86 | // make sure we have enough space |
| 87 | if (count < BufferItem::getFlattenedSize()) { |
| 88 | return NO_MEMORY; |
| 89 | } |
| 90 | |
| 91 | // content flags are stored first |
| 92 | uint32_t& flags = *static_cast<uint32_t*>(buffer); |
| 93 | |
| 94 | // advance the pointer |
| 95 | FlattenableUtils::advance(buffer, size, sizeof(uint32_t)); |
| 96 | |
| 97 | flags = 0; |
| 98 | if (mGraphicBuffer != 0) { |
| 99 | status_t err = mGraphicBuffer->flatten(buffer, size, fds, count); |
| 100 | if (err) return err; |
| 101 | size -= FlattenableUtils::align<4>(buffer); |
| 102 | flags |= 1; |
| 103 | } |
| 104 | if (mFence != 0) { |
| 105 | status_t err = mFence->flatten(buffer, size, fds, count); |
| 106 | if (err) return err; |
| 107 | size -= FlattenableUtils::align<4>(buffer); |
| 108 | flags |= 2; |
| 109 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame^] | 110 | status_t err = mSurfaceDamage.flatten(buffer, size); |
| 111 | if (err) return err; |
| 112 | size -= FlattenableUtils::align<4>(buffer); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 113 | |
| 114 | // check we have enough space (in case flattening the fence/graphicbuffer lied to us) |
| 115 | if (size < getPodSize()) { |
| 116 | return NO_MEMORY; |
| 117 | } |
| 118 | |
| 119 | FlattenableUtils::write(buffer, size, mCrop); |
| 120 | FlattenableUtils::write(buffer, size, mTransform); |
| 121 | FlattenableUtils::write(buffer, size, mScalingMode); |
| 122 | FlattenableUtils::write(buffer, size, mTimestamp); |
| 123 | FlattenableUtils::write(buffer, size, mIsAutoTimestamp); |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 124 | FlattenableUtils::write(buffer, size, mDataSpace); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 125 | FlattenableUtils::write(buffer, size, mFrameNumber); |
| 126 | FlattenableUtils::write(buffer, size, mSlot); |
| 127 | FlattenableUtils::write(buffer, size, mIsDroppable); |
| 128 | FlattenableUtils::write(buffer, size, mAcquireCalled); |
| 129 | FlattenableUtils::write(buffer, size, mTransformToDisplayInverse); |
| 130 | |
| 131 | return NO_ERROR; |
| 132 | } |
| 133 | |
| 134 | status_t BufferItem::unflatten( |
| 135 | void const*& buffer, size_t& size, int const*& fds, size_t& count) { |
| 136 | |
| 137 | if (size < sizeof(uint32_t)) |
| 138 | return NO_MEMORY; |
| 139 | |
| 140 | uint32_t flags = 0; |
| 141 | FlattenableUtils::read(buffer, size, flags); |
| 142 | |
| 143 | if (flags & 1) { |
| 144 | mGraphicBuffer = new GraphicBuffer(); |
| 145 | status_t err = mGraphicBuffer->unflatten(buffer, size, fds, count); |
| 146 | if (err) return err; |
| 147 | size -= FlattenableUtils::align<4>(buffer); |
| 148 | } |
| 149 | |
| 150 | if (flags & 2) { |
| 151 | mFence = new Fence(); |
| 152 | status_t err = mFence->unflatten(buffer, size, fds, count); |
| 153 | if (err) return err; |
| 154 | size -= FlattenableUtils::align<4>(buffer); |
| 155 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame^] | 156 | status_t err = mSurfaceDamage.unflatten(buffer, size); |
| 157 | if (err) return err; |
| 158 | size -= FlattenableUtils::align<4>(buffer); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 159 | |
| 160 | // check we have enough space |
| 161 | if (size < getPodSize()) { |
| 162 | return NO_MEMORY; |
| 163 | } |
| 164 | |
| 165 | FlattenableUtils::read(buffer, size, mCrop); |
| 166 | FlattenableUtils::read(buffer, size, mTransform); |
| 167 | FlattenableUtils::read(buffer, size, mScalingMode); |
| 168 | FlattenableUtils::read(buffer, size, mTimestamp); |
| 169 | FlattenableUtils::read(buffer, size, mIsAutoTimestamp); |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 170 | FlattenableUtils::read(buffer, size, mDataSpace); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 171 | FlattenableUtils::read(buffer, size, mFrameNumber); |
| 172 | FlattenableUtils::read(buffer, size, mSlot); |
| 173 | FlattenableUtils::read(buffer, size, mIsDroppable); |
| 174 | FlattenableUtils::read(buffer, size, mAcquireCalled); |
| 175 | FlattenableUtils::read(buffer, size, mTransformToDisplayInverse); |
| 176 | |
| 177 | return NO_ERROR; |
| 178 | } |
| 179 | |
| 180 | const char* BufferItem::scalingModeName(uint32_t scalingMode) { |
| 181 | switch (scalingMode) { |
| 182 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE"; |
| 183 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW"; |
| 184 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP"; |
| 185 | default: return "Unknown"; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | } // namespace android |