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