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