Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 17 | #define LOG_TAG "GraphicBuffer" |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 19 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 20 | #include <ui/GraphicBuffer.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 21 | |
| 22 | #include <cutils/atomic.h> |
| 23 | |
Jesse Hall | 7992781 | 2017-03-23 11:03:23 -0700 | [diff] [blame] | 24 | #include <grallocusage/GrallocUsageConversion.h> |
| 25 | |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 26 | #ifndef LIBUI_IN_VNDK |
| 27 | #include <ui/BufferHubBuffer.h> |
| 28 | #endif // LIBUI_IN_VNDK |
| 29 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 30 | #include <ui/Gralloc2.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 31 | #include <ui/GraphicBufferAllocator.h> |
| 32 | #include <ui/GraphicBufferMapper.h> |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 34 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | // =========================================================================== |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 38 | // Buffer and implementation of ANativeWindowBuffer |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 39 | // =========================================================================== |
| 40 | |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 41 | static uint64_t getUniqueId() { |
| 42 | static volatile int32_t nextId = 0; |
| 43 | uint64_t id = static_cast<uint64_t>(getpid()) << 32; |
| 44 | id |= static_cast<uint32_t>(android_atomic_inc(&nextId)); |
| 45 | return id; |
| 46 | } |
| 47 | |
Mathias Agopian | f543e5a | 2017-04-03 17:16:41 -0700 | [diff] [blame] | 48 | sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) { |
| 49 | return static_cast<GraphicBuffer *>(anwb); |
| 50 | } |
| 51 | |
Pawin Vongmasa | e672cd0 | 2019-02-14 16:01:29 -0800 | [diff] [blame] | 52 | GraphicBuffer* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer* buffer) { |
| 53 | return reinterpret_cast<GraphicBuffer*>(buffer); |
| 54 | } |
| 55 | |
| 56 | GraphicBuffer const* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer const* buffer) { |
| 57 | return reinterpret_cast<GraphicBuffer const*>(buffer); |
| 58 | } |
| 59 | |
| 60 | AHardwareBuffer* GraphicBuffer::toAHardwareBuffer() { |
| 61 | return reinterpret_cast<AHardwareBuffer*>(this); |
| 62 | } |
| 63 | |
| 64 | AHardwareBuffer const* GraphicBuffer::toAHardwareBuffer() const { |
| 65 | return reinterpret_cast<AHardwareBuffer const*>(this); |
| 66 | } |
| 67 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 68 | GraphicBuffer::GraphicBuffer() |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 69 | : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), |
Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 70 | mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0) |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 71 | { |
Dan Stoza | 01049c8 | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 72 | width = |
| 73 | height = |
| 74 | stride = |
| 75 | format = |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 76 | usage_deprecated = 0; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 77 | usage = 0; |
Mathias Agopian | 841abed | 2017-02-10 16:15:34 -0800 | [diff] [blame] | 78 | layerCount = 0; |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 79 | handle = nullptr; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 82 | // deprecated |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 83 | GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, |
Dan Stoza | 024e931 | 2016-08-24 12:17:29 -0700 | [diff] [blame] | 84 | PixelFormat inFormat, uint32_t inUsage, std::string requestorName) |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 85 | : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName) |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 86 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 89 | GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 90 | uint32_t inLayerCount, uint64_t inUsage, std::string requestorName) |
| 91 | : GraphicBuffer() { |
| 92 | mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, |
| 93 | std::move(requestorName)); |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 96 | // deprecated |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 97 | GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, |
| 98 | PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage, |
| 99 | uint32_t inStride, native_handle_t* inHandle, bool keepOwnership) |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 100 | : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 101 | inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage), |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 102 | inStride) |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 103 | { |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 106 | GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, |
| 107 | uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 108 | uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) |
| 109 | : GraphicBuffer() { |
| 110 | mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount, |
| 111 | inUsage, inStride); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 114 | #ifndef LIBUI_IN_VNDK |
| 115 | GraphicBuffer::GraphicBuffer(std::unique_ptr<BufferHubBuffer> buffer) : GraphicBuffer() { |
| 116 | if (buffer == nullptr) { |
| 117 | mInitCheck = BAD_VALUE; |
| 118 | return; |
| 119 | } |
| 120 | |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 121 | mInitCheck = initWithHandle(buffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE, |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 122 | buffer->desc().width, buffer->desc().height, |
| 123 | static_cast<PixelFormat>(buffer->desc().format), |
| 124 | buffer->desc().layers, buffer->desc().usage, buffer->desc().stride); |
Jiwen 'Steve' Cai | 29160fb | 2018-12-13 12:03:28 -0800 | [diff] [blame] | 125 | mBufferId = buffer->id(); |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 126 | mBufferHubBuffer = std::move(buffer); |
| 127 | } |
| 128 | #endif // LIBUI_IN_VNDK |
| 129 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 130 | GraphicBuffer::~GraphicBuffer() |
| 131 | { |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 132 | ATRACE_CALL(); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 133 | if (handle) { |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 134 | free_handle(); |
| 135 | } |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame] | 136 | for (auto& [callback, context] : mDeathCallbacks) { |
| 137 | callback(context, mId); |
| 138 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void GraphicBuffer::free_handle() |
| 142 | { |
| 143 | if (mOwner == ownHandle) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 144 | mBufferMapper.freeBuffer(handle); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 145 | } else if (mOwner == ownData) { |
| 146 | GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); |
| 147 | allocator.free(handle); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 148 | } |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 149 | handle = nullptr; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | status_t GraphicBuffer::initCheck() const { |
Dan Stoza | 133caac | 2014-12-01 15:15:31 -0800 | [diff] [blame] | 153 | return static_cast<status_t>(mInitCheck); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 156 | void GraphicBuffer::dumpAllocationsToSystemLog() |
| 157 | { |
| 158 | GraphicBufferAllocator::dumpToSystemLog(); |
| 159 | } |
| 160 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 161 | ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 162 | { |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 163 | return static_cast<ANativeWindowBuffer*>( |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 164 | const_cast<GraphicBuffer*>(this)); |
| 165 | } |
| 166 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 167 | status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 168 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage) |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 169 | { |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 170 | if (mOwner != ownData) |
| 171 | return INVALID_OPERATION; |
| 172 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 173 | if (handle && |
| 174 | static_cast<int>(inWidth) == width && |
| 175 | static_cast<int>(inHeight) == height && |
| 176 | inFormat == format && |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 177 | inLayerCount == layerCount && |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 178 | inUsage == usage) |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 179 | return NO_ERROR; |
| 180 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 181 | if (handle) { |
| 182 | GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); |
| 183 | allocator.free(handle); |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 184 | handle = nullptr; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 185 | } |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 186 | return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]"); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 189 | bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 190 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage) |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 191 | { |
| 192 | if (static_cast<int>(inWidth) != width) return true; |
| 193 | if (static_cast<int>(inHeight) != height) return true; |
| 194 | if (inFormat != format) return true; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 195 | if (inLayerCount != layerCount) return true; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 196 | if ((usage & inUsage) != inUsage) return true; |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 200 | status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 201 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage, |
| 202 | std::string requestorName) |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 203 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 204 | GraphicBufferAllocator& allocator = GraphicBufferAllocator::get(); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 205 | uint32_t outStride = 0; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 206 | status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 207 | inUsage, &handle, &outStride, mId, |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 208 | std::move(requestorName)); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 209 | if (err == NO_ERROR) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 210 | mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); |
| 211 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 212 | width = static_cast<int>(inWidth); |
| 213 | height = static_cast<int>(inHeight); |
| 214 | format = inFormat; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 215 | layerCount = inLayerCount; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 216 | usage = inUsage; |
| 217 | usage_deprecated = int(usage); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 218 | stride = static_cast<int>(outStride); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 219 | } |
| 220 | return err; |
| 221 | } |
| 222 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 223 | status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method, |
| 224 | uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 225 | uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) { |
| 226 | ANativeWindowBuffer::width = static_cast<int>(inWidth); |
| 227 | ANativeWindowBuffer::height = static_cast<int>(inHeight); |
| 228 | ANativeWindowBuffer::stride = static_cast<int>(inStride); |
| 229 | ANativeWindowBuffer::format = inFormat; |
| 230 | ANativeWindowBuffer::usage = inUsage; |
| 231 | ANativeWindowBuffer::usage_deprecated = int(inUsage); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 232 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 233 | ANativeWindowBuffer::layerCount = inLayerCount; |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 234 | |
| 235 | mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle; |
| 236 | |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 237 | if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) { |
| 238 | buffer_handle_t importedHandle; |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 239 | status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount, |
| 240 | inFormat, inUsage, inStride, &importedHandle); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 241 | if (err != NO_ERROR) { |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 242 | initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 243 | |
| 244 | return err; |
| 245 | } |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 246 | |
| 247 | if (method == TAKE_UNREGISTERED_HANDLE) { |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 248 | native_handle_close(inHandle); |
| 249 | native_handle_delete(const_cast<native_handle_t*>(inHandle)); |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 252 | inHandle = importedHandle; |
| 253 | mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 256 | ANativeWindowBuffer::handle = inHandle; |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 257 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 258 | return NO_ERROR; |
| 259 | } |
| 260 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 261 | status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel, |
| 262 | int32_t* outBytesPerStride) { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 263 | const Rect lockBounds(width, height); |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 264 | status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 265 | return res; |
| 266 | } |
| 267 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 268 | status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr, |
| 269 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 270 | if (rect.left < 0 || rect.right > width || |
| 271 | rect.top < 0 || rect.bottom > height) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 272 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
Dan Stoza | 01049c8 | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 273 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 274 | width, height); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 275 | return BAD_VALUE; |
| 276 | } |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 277 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 278 | status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel, |
| 279 | outBytesPerStride); |
| 280 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 281 | return res; |
| 282 | } |
| 283 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 284 | status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr) |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 285 | { |
| 286 | const Rect lockBounds(width, height); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 287 | status_t res = lockYCbCr(inUsage, lockBounds, ycbcr); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 288 | return res; |
| 289 | } |
| 290 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 291 | status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect, |
| 292 | android_ycbcr* ycbcr) |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 293 | { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 294 | if (rect.left < 0 || rect.right > width || |
| 295 | rect.top < 0 || rect.bottom > height) { |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 296 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 297 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 298 | width, height); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 299 | return BAD_VALUE; |
| 300 | } |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 301 | status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 302 | return res; |
| 303 | } |
| 304 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 305 | status_t GraphicBuffer::unlock() |
| 306 | { |
| 307 | status_t res = getBufferMapper().unlock(handle); |
| 308 | return res; |
| 309 | } |
| 310 | |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 311 | status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd, |
| 312 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 313 | const Rect lockBounds(width, height); |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 314 | status_t res = |
| 315 | lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 316 | return res; |
| 317 | } |
| 318 | |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 319 | status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd, |
| 320 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
| 321 | return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 324 | status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage, |
| 325 | const Rect& rect, void** vaddr, int fenceFd, |
| 326 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 327 | if (rect.left < 0 || rect.right > width || |
| 328 | rect.top < 0 || rect.bottom > height) { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 329 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 330 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 331 | width, height); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 332 | return BAD_VALUE; |
| 333 | } |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 334 | |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 335 | status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect, |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 336 | vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); |
| 337 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 338 | return res; |
| 339 | } |
| 340 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 341 | status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr, |
| 342 | int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 343 | { |
| 344 | const Rect lockBounds(width, height); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 345 | status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 346 | return res; |
| 347 | } |
| 348 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 349 | status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, |
| 350 | android_ycbcr* ycbcr, int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 351 | { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 352 | if (rect.left < 0 || rect.right > width || |
| 353 | rect.top < 0 || rect.bottom > height) { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 354 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 355 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 356 | width, height); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 357 | return BAD_VALUE; |
| 358 | } |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 359 | status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 360 | return res; |
| 361 | } |
| 362 | |
| 363 | status_t GraphicBuffer::unlockAsync(int *fenceFd) |
| 364 | { |
| 365 | status_t res = getBufferMapper().unlockAsync(handle, fenceFd); |
| 366 | return res; |
| 367 | } |
| 368 | |
Valerie Hau | ddbfaeb | 2019-02-01 09:54:20 -0800 | [diff] [blame] | 369 | status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 370 | uint32_t inLayerCount, uint64_t inUsage, |
| 371 | bool* outSupported) const { |
| 372 | return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage, |
| 373 | outSupported); |
| 374 | } |
| 375 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 376 | size_t GraphicBuffer::getFlattenedSize() const { |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 377 | #ifndef LIBUI_IN_VNDK |
| 378 | if (mBufferHubBuffer != nullptr) { |
| 379 | return 48; |
| 380 | } |
| 381 | #endif |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 382 | return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 383 | } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 384 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 385 | size_t GraphicBuffer::getFdCount() const { |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 386 | #ifndef LIBUI_IN_VNDK |
| 387 | if (mBufferHubBuffer != nullptr) { |
| 388 | return 0; |
| 389 | } |
| 390 | #endif |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 391 | return static_cast<size_t>(handle ? mTransportNumFds : 0); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 394 | status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const { |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 395 | #ifndef LIBUI_IN_VNDK |
| 396 | if (mBufferHubBuffer != nullptr) { |
Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame^] | 397 | return flattenBufferHubBuffer(buffer, size); |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 398 | } |
| 399 | #endif |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 400 | size_t sizeNeeded = GraphicBuffer::getFlattenedSize(); |
| 401 | if (size < sizeNeeded) return NO_MEMORY; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 402 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 403 | size_t fdCountNeeded = GraphicBuffer::getFdCount(); |
| 404 | if (count < fdCountNeeded) return NO_MEMORY; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 405 | |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 406 | int32_t* buf = static_cast<int32_t*>(buffer); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 407 | buf[0] = 'GB01'; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 408 | buf[1] = width; |
| 409 | buf[2] = height; |
| 410 | buf[3] = stride; |
| 411 | buf[4] = format; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 412 | buf[5] = static_cast<int32_t>(layerCount); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 413 | buf[6] = int(usage); // low 32-bits |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 414 | buf[7] = static_cast<int32_t>(mId >> 32); |
| 415 | buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull); |
| 416 | buf[9] = static_cast<int32_t>(mGenerationNumber); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 417 | buf[10] = 0; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 418 | buf[11] = 0; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 419 | buf[12] = int(usage >> 32); // high 32-bits |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 420 | |
| 421 | if (handle) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 422 | buf[10] = int32_t(mTransportNumFds); |
| 423 | buf[11] = int32_t(mTransportNumInts); |
| 424 | memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int)); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 425 | memcpy(buf + 13, handle->data + handle->numFds, |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 426 | static_cast<size_t>(mTransportNumInts) * sizeof(int)); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 427 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 428 | |
Dan Stoza | eea6d68 | 2015-04-20 12:07:13 -0700 | [diff] [blame] | 429 | buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 430 | size -= sizeNeeded; |
Andy McFadden | bc96e47 | 2014-03-17 16:48:23 -0700 | [diff] [blame] | 431 | if (handle) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 432 | fds += mTransportNumFds; |
| 433 | count -= static_cast<size_t>(mTransportNumFds); |
Andy McFadden | bc96e47 | 2014-03-17 16:48:23 -0700 | [diff] [blame] | 434 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 435 | return NO_ERROR; |
| 436 | } |
| 437 | |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 438 | status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds, |
| 439 | size_t& count) { |
Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame^] | 440 | // Check if size is not smaller than buf[0] is supposed to take. |
| 441 | if (size < sizeof(int)) { |
| 442 | return NO_MEMORY; |
| 443 | } |
| 444 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 445 | int const* buf = static_cast<int const*>(buffer); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 446 | |
| 447 | // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!! |
| 448 | // see H2BGraphicBufferProducer.cpp |
| 449 | uint32_t flattenWordCount = 0; |
| 450 | if (buf[0] == 'GB01') { |
| 451 | // new version with 64-bits usage bits |
| 452 | flattenWordCount = 13; |
| 453 | } else if (buf[0] == 'GBFR') { |
| 454 | // old version, when usage bits were 32-bits |
| 455 | flattenWordCount = 12; |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 456 | } else if (buf[0] == 'BHBB') { // BufferHub backed buffer. |
| 457 | #ifndef LIBUI_IN_VNDK |
Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame^] | 458 | return unflattenBufferHubBuffer(buffer, size); |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 459 | #else |
| 460 | return BAD_TYPE; |
| 461 | #endif |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 462 | } else { |
| 463 | return BAD_TYPE; |
| 464 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 465 | |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 466 | if (size < 12 * sizeof(int)) { |
| 467 | android_errorWriteLog(0x534e4554, "114223584"); |
| 468 | return NO_MEMORY; |
| 469 | } |
| 470 | |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 471 | const size_t numFds = static_cast<size_t>(buf[10]); |
| 472 | const size_t numInts = static_cast<size_t>(buf[11]); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 473 | |
Michael Lentine | c168b8a | 2015-02-18 10:14:18 -0800 | [diff] [blame] | 474 | // Limit the maxNumber to be relatively small. The number of fds or ints |
| 475 | // should not come close to this number, and the number itself was simply |
| 476 | // chosen to be high enough to not cause issues and low enough to prevent |
| 477 | // overflow problems. |
| 478 | const size_t maxNumber = 4096; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 479 | if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) { |
| 480 | width = height = stride = format = usage_deprecated = 0; |
| 481 | layerCount = 0; |
| 482 | usage = 0; |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 483 | handle = nullptr; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 484 | ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts); |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 485 | return BAD_VALUE; |
| 486 | } |
| 487 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 488 | const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 489 | if (size < sizeNeeded) return NO_MEMORY; |
| 490 | |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 491 | size_t fdCountNeeded = numFds; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 492 | if (count < fdCountNeeded) return NO_MEMORY; |
| 493 | |
| 494 | if (handle) { |
| 495 | // free previous handle if any |
| 496 | free_handle(); |
| 497 | } |
| 498 | |
| 499 | if (numFds || numInts) { |
| 500 | width = buf[1]; |
| 501 | height = buf[2]; |
| 502 | stride = buf[3]; |
| 503 | format = buf[4]; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 504 | layerCount = static_cast<uintptr_t>(buf[5]); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 505 | usage_deprecated = buf[6]; |
| 506 | if (flattenWordCount == 13) { |
| 507 | usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]); |
| 508 | } else { |
| 509 | usage = uint64_t(usage_deprecated); |
| 510 | } |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 511 | native_handle* h = |
| 512 | native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts)); |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 513 | if (!h) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 514 | width = height = stride = format = usage_deprecated = 0; |
| 515 | layerCount = 0; |
| 516 | usage = 0; |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 517 | handle = nullptr; |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 518 | ALOGE("unflatten: native_handle_create failed"); |
| 519 | return NO_MEMORY; |
| 520 | } |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 521 | memcpy(h->data, fds, numFds * sizeof(int)); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 522 | memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int)); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 523 | handle = h; |
| 524 | } else { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 525 | width = height = stride = format = usage_deprecated = 0; |
| 526 | layerCount = 0; |
| 527 | usage = 0; |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 528 | handle = nullptr; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 531 | mId = static_cast<uint64_t>(buf[7]) << 32; |
| 532 | mId |= static_cast<uint32_t>(buf[8]); |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 533 | |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 534 | mGenerationNumber = static_cast<uint32_t>(buf[9]); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 535 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 536 | mOwner = ownHandle; |
Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 537 | |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 538 | if (handle != nullptr) { |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 539 | buffer_handle_t importedHandle; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 540 | status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height), |
| 541 | uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle); |
Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 542 | if (err != NO_ERROR) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 543 | width = height = stride = format = usage_deprecated = 0; |
| 544 | layerCount = 0; |
| 545 | usage = 0; |
Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 546 | handle = nullptr; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 547 | ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err); |
Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 548 | return err; |
| 549 | } |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 550 | |
| 551 | native_handle_close(handle); |
| 552 | native_handle_delete(const_cast<native_handle_t*>(handle)); |
| 553 | handle = importedHandle; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 554 | mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); |
Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Dan Stoza | eea6d68 | 2015-04-20 12:07:13 -0700 | [diff] [blame] | 557 | buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 558 | size -= sizeNeeded; |
| 559 | fds += numFds; |
| 560 | count -= numFds; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 561 | return NO_ERROR; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame] | 564 | void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) { |
| 565 | mDeathCallbacks.emplace_back(deathCallback, context); |
| 566 | } |
| 567 | |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 568 | #ifndef LIBUI_IN_VNDK |
Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame^] | 569 | status_t GraphicBuffer::flattenBufferHubBuffer(void*& buffer, size_t& size) const { |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 570 | sp<NativeHandle> tokenHandle = mBufferHubBuffer->duplicate(); |
| 571 | if (tokenHandle == nullptr || tokenHandle->handle() == nullptr || |
| 572 | tokenHandle->handle()->numFds != 0) { |
| 573 | return BAD_VALUE; |
| 574 | } |
| 575 | |
| 576 | // Size needed for one label, one number of ints inside the token, one generation number and |
| 577 | // the token itself. |
| 578 | int numIntsInToken = tokenHandle->handle()->numInts; |
| 579 | const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int); |
| 580 | if (size < sizeNeeded) { |
| 581 | ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__, |
| 582 | static_cast<int>(sizeNeeded), static_cast<int>(size)); |
| 583 | return NO_MEMORY; |
| 584 | } |
| 585 | size -= sizeNeeded; |
| 586 | |
| 587 | int* buf = static_cast<int*>(buffer); |
| 588 | buf[0] = 'BHBB'; |
| 589 | buf[1] = numIntsInToken; |
| 590 | memcpy(buf + 2, tokenHandle->handle()->data, static_cast<size_t>(numIntsInToken) * sizeof(int)); |
| 591 | buf[2 + numIntsInToken] = static_cast<int32_t>(mGenerationNumber); |
| 592 | |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 593 | return NO_ERROR; |
| 594 | } |
| 595 | |
Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame^] | 596 | status_t GraphicBuffer::unflattenBufferHubBuffer(void const*& buffer, size_t& size) { |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 597 | const int* buf = static_cast<const int*>(buffer); |
| 598 | int numIntsInToken = buf[1]; |
| 599 | // Size needed for one label, one number of ints inside the token, one generation number and |
| 600 | // the token itself. |
| 601 | const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int); |
| 602 | if (size < sizeNeeded) { |
| 603 | ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__, |
| 604 | static_cast<int>(sizeNeeded), static_cast<int>(size)); |
| 605 | return NO_MEMORY; |
| 606 | } |
| 607 | size -= sizeNeeded; |
| 608 | native_handle_t* importToken = native_handle_create(/*numFds=*/0, /*numInts=*/numIntsInToken); |
| 609 | memcpy(importToken->data, buf + 2, static_cast<size_t>(buf[1]) * sizeof(int)); |
| 610 | sp<NativeHandle> importTokenHandle = NativeHandle::create(importToken, /*ownHandle=*/true); |
| 611 | std::unique_ptr<BufferHubBuffer> bufferHubBuffer = BufferHubBuffer::import(importTokenHandle); |
| 612 | if (bufferHubBuffer == nullptr || bufferHubBuffer.get() == nullptr) { |
| 613 | return BAD_VALUE; |
| 614 | } |
| 615 | // Reconstruct this GraphicBuffer object using the new BufferHubBuffer object. |
| 616 | if (handle) { |
| 617 | free_handle(); |
| 618 | } |
| 619 | mId = 0; |
| 620 | mGenerationNumber = static_cast<uint32_t>(buf[2 + numIntsInToken]); |
| 621 | mInitCheck = |
| 622 | initWithHandle(bufferHubBuffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE, |
| 623 | bufferHubBuffer->desc().width, bufferHubBuffer->desc().height, |
| 624 | static_cast<PixelFormat>(bufferHubBuffer->desc().format), |
| 625 | bufferHubBuffer->desc().layers, bufferHubBuffer->desc().usage, |
| 626 | bufferHubBuffer->desc().stride); |
| 627 | mBufferId = bufferHubBuffer->id(); |
| 628 | mBufferHubBuffer.reset(std::move(bufferHubBuffer.get())); |
| 629 | |
Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 630 | return NO_ERROR; |
| 631 | } |
| 632 | |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 633 | bool GraphicBuffer::isBufferHubBuffer() const { |
| 634 | return mBufferHubBuffer != nullptr; |
| 635 | } |
| 636 | #endif // LIBUI_IN_VNDK |
| 637 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 638 | // --------------------------------------------------------------------------- |
| 639 | |
| 640 | }; // namespace android |